@@ -10,10 +10,10 @@ import (
1010 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/client"
1111 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/config"
1212 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/hostname"
13- "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/identity"
1413 log "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/log"
1514 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/utils"
1615 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/api"
16+ "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/identity"
1717 "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version"
1818 "github.com/rancher-sandbox/cluster-api-provider-elemental/pkg/agent/osplugin"
1919 "github.com/spf13/cobra"
5050func main () {
5151 fs := vfs .OSFS
5252 osPluginLoader := osplugin .NewLoader ()
53- client := client .NewClient ()
53+ client := client .NewClient (version . Version )
5454 commandRunner := utils .NewCommandRunner ()
5555 cmd := newCommand (fs , osPluginLoader , commandRunner , client )
5656 if err := cmd .Execute (); err != nil {
@@ -103,13 +103,13 @@ func newCommand(fs vfs.FS, pluginLoader osplugin.Loader, commandRunner utils.Com
103103 return fmt .Errorf ("Initializing plugin: %w" , err )
104104 }
105105 // Initialize Identity
106- identityManager := identity .NewDummyManager (fs , conf .Agent .WorkDir )
107- signingKey , err := identityManager .LoadSigningKeyOrCreateNew ()
106+ identityManager := identity .NewManager (fs , conf .Agent .WorkDir )
107+ identity , err := identityManager .LoadSigningKeyOrCreateNew ()
108108 if err != nil {
109109 return fmt .Errorf ("initializing identity: %w" , err )
110110 }
111111 // Initialize Elemental API Client
112- if err := client .Init (fs , signingKey , conf ); err != nil {
112+ if err := client .Init (fs , identity , conf ); err != nil {
113113 return fmt .Errorf ("initializing Elemental API client: %w" , err )
114114 }
115115 // Get current hostname
@@ -120,10 +120,14 @@ func newCommand(fs vfs.FS, pluginLoader osplugin.Loader, commandRunner utils.Com
120120 // Register
121121 if registerFlag {
122122 log .Info ("Registering Elemental Host" )
123+ pubKey , err := identity .MarshalPublic ()
124+ if err != nil {
125+ return fmt .Errorf ("marshalling host public key: %w" , err )
126+ }
123127 var registration * api.RegistrationResponse
124- hostname , registration = handleRegistration (client , osPlugin , conf .Agent .Reconciliation )
128+ hostname , registration = handleRegistration (client , osPlugin , pubKey , conf . Registration . Token , conf .Agent .Reconciliation )
125129 log .Infof ("Successfully registered as '%s'" , hostname )
126- if err := handlePostRegistration (osPlugin , hostname , signingKey , registration ); err != nil {
130+ if err := handlePostRegistration (osPlugin , hostname , identity , registration ); err != nil {
127131 return fmt .Errorf ("handling post registration: %w" , err )
128132 }
129133 // Exit program if --install was not called
@@ -134,7 +138,7 @@ func newCommand(fs vfs.FS, pluginLoader osplugin.Loader, commandRunner utils.Com
134138 // Install
135139 if installFlag {
136140 log .Info ("Installing Elemental" )
137- handleInstall (client , osPlugin , hostname , conf .Agent .Reconciliation )
141+ handleInstall (client , osPlugin , hostname , conf .Registration . Token , conf . Agent .Reconciliation )
138142 log .Info ("Installation successful" )
139143 handlePost (osPlugin , conf .Agent .PostInstall .PowerOff , conf .Agent .PostInstall .Reboot )
140144 return nil
@@ -143,7 +147,7 @@ func newCommand(fs vfs.FS, pluginLoader osplugin.Loader, commandRunner utils.Com
143147 // Reset
144148 if resetFlag {
145149 log .Info ("Resetting Elemental" )
146- handleReset (client , osPlugin , conf .Agent . Reconciliation , hostname )
150+ handleReset (client , osPlugin , hostname , conf .Registration . Token , conf . Agent . Reconciliation )
147151 log .Info ("Reset successful" )
148152 handlePost (osPlugin , conf .Agent .PostReset .PowerOff , conf .Agent .PostReset .Reboot )
149153 return nil
@@ -221,7 +225,7 @@ func getConfig(fs vfs.FS) (config.Config, error) {
221225 return conf , nil
222226}
223227
224- func handleRegistration (client client.Client , osPlugin osplugin.Plugin , registrationRecoveryPeriod time.Duration ) (string , * api.RegistrationResponse ) {
228+ func handleRegistration (client client.Client , osPlugin osplugin.Plugin , pubKey [] byte , registrationToken string , registrationRecoveryPeriod time.Duration ) (string , * api.RegistrationResponse ) {
225229 hostnameFormatter := hostname .NewFormatter (osPlugin )
226230 var newHostname string
227231 var registration * api.RegistrationResponse
@@ -235,7 +239,7 @@ func handleRegistration(client client.Client, osPlugin osplugin.Plugin, registra
235239 }
236240 // Fetch remote Registration
237241 log .Debug ("Fetching remote registration" )
238- registration , err = client .GetRegistration ()
242+ registration , err = client .GetRegistration (registrationToken )
239243 if err != nil {
240244 log .Error (err , "getting remote Registration" )
241245 registrationError = true
@@ -257,7 +261,8 @@ func handleRegistration(client client.Client, osPlugin osplugin.Plugin, registra
257261 Name : newHostname ,
258262 Annotations : registration .HostAnnotations ,
259263 Labels : registration .HostLabels ,
260- }); err != nil {
264+ PubKey : string (pubKey ),
265+ }, registrationToken ); err != nil {
261266 log .Error (err , "registering new ElementalHost" )
262267 registrationError = true
263268 continue
@@ -267,7 +272,7 @@ func handleRegistration(client client.Client, osPlugin osplugin.Plugin, registra
267272 return newHostname , registration
268273}
269274
270- func handlePostRegistration (osPlugin osplugin.Plugin , hostnameToSet string , signingKey [] byte , registration * api.RegistrationResponse ) error {
275+ func handlePostRegistration (osPlugin osplugin.Plugin , hostnameToSet string , id identity. Identity , registration * api.RegistrationResponse ) error {
271276 // Persist registered hostname
272277 if err := osPlugin .PersistHostname (hostnameToSet ); err != nil {
273278 return fmt .Errorf ("persisting hostname '%s': %w" , hostnameToSet , err )
@@ -282,14 +287,18 @@ func handlePostRegistration(osPlugin osplugin.Plugin, hostnameToSet string, sign
282287 return fmt .Errorf ("persisting agent config file '%s': %w" , configPath , err )
283288 }
284289 // Persist identity file
290+ identityBytes , err := id .Marshal ()
291+ if err != nil {
292+ return fmt .Errorf ("marshalling identity: %w" , err )
293+ }
285294 privateKeyPath := fmt .Sprintf ("%s/%s" , agentConfig .Agent .WorkDir , identity .PrivateKeyFile )
286- if err := osPlugin .PersistFile (signingKey , privateKeyPath , 0640 , 0 , 0 ); err != nil {
295+ if err := osPlugin .PersistFile (identityBytes , privateKeyPath , 0640 , 0 , 0 ); err != nil {
287296 return fmt .Errorf ("persisting private key file '%s': %w" , privateKeyPath , err )
288297 }
289298 return nil
290299}
291300
292- func handleInstall (client client.Client , osPlugin osplugin.Plugin , hostname string , installationRecoveryPeriod time.Duration ) {
301+ func handleInstall (client client.Client , osPlugin osplugin.Plugin , hostname string , registrationToken string , installationRecoveryPeriod time.Duration ) {
293302 cloudConfigAlreadyApplied := false
294303 alreadyInstalled := false
295304 installationError := false
@@ -304,7 +313,7 @@ func handleInstall(client client.Client, osPlugin osplugin.Plugin, hostname stri
304313 var err error
305314 if ! cloudConfigAlreadyApplied || ! alreadyInstalled {
306315 log .Debug ("Fetching remote registration" )
307- registration , err = client .GetRegistration ()
316+ registration , err = client .GetRegistration (registrationToken )
308317 if err != nil {
309318 log .Error (err , "getting remote Registration" )
310319 installationError = true
@@ -354,7 +363,7 @@ func handleInstall(client client.Client, osPlugin osplugin.Plugin, hostname stri
354363 }
355364}
356365
357- func handleReset (client client.Client , osPlugin osplugin.Plugin , resetRecoveryPeriod time. Duration , hostname string ) {
366+ func handleReset (client client.Client , osPlugin osplugin.Plugin , hostname string , registrationToken string , resetRecoveryPeriod time. Duration ) {
358367 resetError := false
359368 alreadyReset := false
360369 for {
@@ -375,7 +384,7 @@ func handleReset(client client.Client, osPlugin osplugin.Plugin, resetRecoveryPe
375384 if ! alreadyReset {
376385 // Fetch remote Registration
377386 log .Debug ("Fetching remote registration" )
378- registration , err := client .GetRegistration ()
387+ registration , err := client .GetRegistration (registrationToken )
379388 if err != nil {
380389 log .Error (err , "getting remote Registration" )
381390 resetError = true
0 commit comments