Skip to content

Commit 15c237d

Browse files
authored
feat(cli/daemon): peer-ready defaults for routing (#1268)
1 parent 8e047cb commit 15c237d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

cli/cmd/daemon/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func bindCredentialEnvVars(v *viper.Viper) {
8484
_ = v.BindEnv("server.database.postgres.username")
8585
_ = v.BindEnv("server.database.postgres.password")
8686

87+
_ = v.BindEnv("server.routing.bootstrap_peers")
88+
8789
_ = v.BindEnv("server.store.oci.auth_config.username")
8890
_ = v.BindEnv("server.store.oci.auth_config.password")
8991
_ = v.BindEnv("server.store.oci.auth_config.access_token")
@@ -106,6 +108,7 @@ func resolveRelativePaths(cfg *DaemonConfig) {
106108
}
107109

108110
cfg.Server.Store.OCI.LocalDir = resolve(cfg.Server.Store.OCI.LocalDir)
111+
cfg.Server.Routing.KeyPath = resolve(cfg.Server.Routing.KeyPath)
109112
cfg.Server.Routing.DatastoreDir = resolve(cfg.Server.Routing.DatastoreDir)
110113
cfg.Server.Database.SQLite.Path = resolve(cfg.Server.Database.SQLite.Path)
111114
}

cli/cmd/daemon/daemon.config.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
# directory (default: ~/.agntcy/dir/, override with --data-dir).
88
# Use absolute paths to pin a location regardless of --data-dir.
99
#
10-
# Credentials can be set via environment variables with the prefix
10+
# Override any setting via environment variables with the prefix
1111
# DIRECTORY_DAEMON_ followed by the uppercased, underscore-delimited path:
12+
# DIRECTORY_DAEMON_SERVER_LISTEN_ADDRESS
13+
# DIRECTORY_DAEMON_SERVER_ROUTING_LISTEN_ADDRESS
14+
# DIRECTORY_DAEMON_SERVER_ROUTING_BOOTSTRAP_PEERS (comma-separated)
1215
# DIRECTORY_DAEMON_SERVER_DATABASE_POSTGRES_USERNAME
1316
# DIRECTORY_DAEMON_SERVER_DATABASE_POSTGRES_PASSWORD
1417
# DIRECTORY_DAEMON_SERVER_STORE_OCI_AUTH_CONFIG_USERNAME
@@ -28,8 +31,11 @@ server:
2831
verification:
2932
enabled: true
3033
routing:
31-
listen_address: "/ip4/0.0.0.0/tcp/0"
34+
listen_address: "/ip4/0.0.0.0/tcp/8999"
35+
key_path: "node.key"
3236
datastore_dir: "routing"
37+
# bootstrap_peers:
38+
# - "/dns4/remote-dir.example.com/tcp/8999/p2p/<peer-id>"
3339
gossipsub:
3440
enabled: true
3541
database:

cli/cmd/daemon/start.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ package daemon
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"os"
1011
"os/signal"
1112
"syscall"
1213

14+
networkinit "github.com/agntcy/dir/cli/cmd/network/init"
1315
reconciler "github.com/agntcy/dir/reconciler/service"
1416
"github.com/agntcy/dir/server"
1517
ocilib "github.com/agntcy/dir/server/store/oci"
@@ -33,6 +35,7 @@ The daemon blocks until SIGINT or SIGTERM is received.`,
3335
RunE: runStart,
3436
}
3537

38+
//nolint:cyclop
3639
func runStart(cmd *cobra.Command, _ []string) error {
3740
running, pid, err := readPID()
3841
if err != nil {
@@ -52,6 +55,12 @@ func runStart(cmd *cobra.Command, _ []string) error {
5255
return fmt.Errorf("failed to load config: %w", err)
5356
}
5457

58+
if cfg.Server.Routing.KeyPath != "" {
59+
if err := ensureKeyFile(cfg.Server.Routing.KeyPath); err != nil {
60+
return fmt.Errorf("failed to ensure peer identity key: %w", err)
61+
}
62+
}
63+
5564
ctx, cancel := context.WithCancel(cmd.Context())
5665
defer cancel()
5766

@@ -110,6 +119,29 @@ func runStart(cmd *cobra.Command, _ []string) error {
110119
return nil
111120
}
112121

122+
// ensureKeyFile generates a persistent Ed25519 identity key if one does not
123+
// already exist at path. Uses the same PKCS#8 PEM format as `dirctl network init`.
124+
func ensureKeyFile(path string) error {
125+
if _, err := os.Stat(path); err == nil {
126+
return nil
127+
} else if !errors.Is(err, os.ErrNotExist) {
128+
return fmt.Errorf("failed to stat key file: %w", err)
129+
}
130+
131+
_, pemData, err := networkinit.GenerateED25519OpenSSLKey()
132+
if err != nil {
133+
return fmt.Errorf("failed to generate Ed25519 key: %w", err)
134+
}
135+
136+
if err := os.WriteFile(path, pemData, 0o600); err != nil { //nolint:mnd
137+
return fmt.Errorf("failed to write key file: %w", err)
138+
}
139+
140+
logger.Info("Generated persistent peer identity key", "path", path)
141+
142+
return nil
143+
}
144+
113145
// newTagLister returns a registry.TagLister for the reconciler's indexer.
114146
// When a local OCI directory is configured, a local oci.Store is opened.
115147
// Otherwise a remote ORAS repository is created from the OCI config.

0 commit comments

Comments
 (0)