@@ -5,11 +5,13 @@ package daemon
55
66import (
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
3639func 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