From 5a7291131f71f31b01c9c9df4d329ae6f1b65390 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Tue, 7 Jun 2022 16:43:07 -0400 Subject: [PATCH] Make current key update on rotation --- internal/cmd/commands/dev/dev.go | 2 +- internal/cmd/commands/server/server.go | 2 +- internal/daemon/worker/auth_rotation.go | 8 +++++++- internal/daemon/worker/auth_rotation_test.go | 3 +++ internal/daemon/worker/status.go | 5 +---- internal/daemon/worker/worker.go | 6 ++++-- internal/daemon/worker/worker_test.go | 2 +- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/internal/cmd/commands/dev/dev.go b/internal/cmd/commands/dev/dev.go index 76b42fe93..6d3c65904 100644 --- a/internal/cmd/commands/dev/dev.go +++ b/internal/cmd/commands/dev/dev.go @@ -715,7 +715,7 @@ func (c *Command) Run(args []string) int { c.InfoKeys = append(c.InfoKeys, "worker auth registration request") c.Info["worker auth registration request"] = req c.InfoKeys = append(c.InfoKeys, "worker auth current key id") - c.Info["worker auth current key id"] = c.worker.WorkerAuthCurrentKeyId + c.Info["worker auth current key id"] = c.worker.WorkerAuthCurrentKeyId.Load() c.InfoKeys = append(c.InfoKeys, "worker auth storage path") c.Info["worker auth storage path"] = c.worker.WorkerAuthStorage.BaseDir() if err := c.StoreWorkerAuthReq(c.worker.WorkerAuthRegistrationRequest, c.worker.WorkerAuthStorage.BaseDir()); err != nil { diff --git a/internal/cmd/commands/server/server.go b/internal/cmd/commands/server/server.go index 848d21e35..dd453d3da 100644 --- a/internal/cmd/commands/server/server.go +++ b/internal/cmd/commands/server/server.go @@ -468,7 +468,7 @@ func (c *Command) Run(args []string) int { c.InfoKeys = append(c.InfoKeys, "worker auth registration request") c.Info["worker auth registration request"] = c.worker.WorkerAuthRegistrationRequest c.InfoKeys = append(c.InfoKeys, "worker auth current key id") - c.Info["worker auth current key id"] = c.worker.WorkerAuthCurrentKeyId + c.Info["worker auth current key id"] = c.worker.WorkerAuthCurrentKeyId.Load() // Write the WorkerAuth request to a file if err := c.StoreWorkerAuthReq(c.worker.WorkerAuthRegistrationRequest, c.worker.WorkerAuthStorage.BaseDir()); err != nil { diff --git a/internal/daemon/worker/auth_rotation.go b/internal/daemon/worker/auth_rotation.go index ae3b7c28e..cdf3f38b7 100644 --- a/internal/daemon/worker/auth_rotation.go +++ b/internal/daemon/worker/auth_rotation.go @@ -182,7 +182,7 @@ func rotateWorkerAuth(ctx context.Context, w *Worker, currentNodeCreds *types.No // Call handle on the inner response, which will be encrypted to the // new node credentials. This will also store the result, // overwriting the current credentials. - _, err = newNodeCreds.HandleFetchNodeCredentialsResponse( + newNodeCreds, err = newNodeCreds.HandleFetchNodeCredentialsResponse( ctx, w.WorkerAuthStorage, fetchResp, @@ -192,5 +192,11 @@ func rotateWorkerAuth(ctx context.Context, w *Worker, currentNodeCreds *types.No return berrors.Wrap(ctx, err, op) } + newKeyId, err := nodeenrollment.KeyIdFromPkix(newNodeCreds.CertificatePublicKeyPkix) + if err != nil { + return berrors.Wrap(ctx, err, op) + } + w.WorkerAuthCurrentKeyId.Store(newKeyId) + return nil } diff --git a/internal/daemon/worker/auth_rotation_test.go b/internal/daemon/worker/auth_rotation_test.go index f5468c406..c35e6213e 100644 --- a/internal/daemon/worker/auth_rotation_test.go +++ b/internal/daemon/worker/auth_rotation_test.go @@ -102,6 +102,9 @@ func TestRotationTicking(t *testing.T) { require.NoError(err) assert.NotEqual(currKey, currNodeCreds.CertificatePublicKeyPkix) currKey = currNodeCreds.CertificatePublicKeyPkix + currKeyId, err := nodeenrollment.KeyIdFromPkix(currNodeCreds.CertificatePublicKeyPkix) + require.NoError(err) + assert.Equal(currKeyId, w.Worker().WorkerAuthCurrentKeyId.Load()) // Stop and start the client connections to ensure the new credentials // are valid; if not, we won't establish a new connection and rotation diff --git a/internal/daemon/worker/status.go b/internal/daemon/worker/status.go index 4bb3ed1cd..9891fe924 100644 --- a/internal/daemon/worker/status.go +++ b/internal/daemon/worker/status.go @@ -145,10 +145,7 @@ func (w *Worker) sendWorkerStatus(cancelCtx context.Context) { statusCtx, statusCancel := context.WithTimeout(cancelCtx, common.StatusTimeout) defer statusCancel() - var keyId string - if w.WorkerAuthCurrentKeyId != "" { - keyId = w.WorkerAuthCurrentKeyId - } + keyId := w.WorkerAuthCurrentKeyId.Load() if w.conf.RawConfig.Worker.Name == "" && keyId == "" { event.WriteError(statusCtx, op, errors.New("worker name and keyId are both empty; one is needed to identify a worker"), diff --git a/internal/daemon/worker/worker.go b/internal/daemon/worker/worker.go index 7c759d276..99af08590 100644 --- a/internal/daemon/worker/worker.go +++ b/internal/daemon/worker/worker.go @@ -80,7 +80,7 @@ type Worker struct { // The storage for node enrollment WorkerAuthStorage *nodeefile.Storage - WorkerAuthCurrentKeyId string + WorkerAuthCurrentKeyId *ua.String WorkerAuthRegistrationRequest string workerAuthSplitListener *nodeenet.SplitListener @@ -110,6 +110,7 @@ func New(conf *Config) (*Worker, error) { tags: new(atomic.Value), updateTags: ua.NewBool(false), nonceFn: base62.Random, + WorkerAuthCurrentKeyId: new(ua.String), } w.lastStatusSuccess.Store((*LastStatusInformation)(nil)) @@ -282,10 +283,11 @@ func (w *Worker) Start() error { if err != nil { return fmt.Errorf("error encoding worker auth registration request: %w", err) } - w.WorkerAuthCurrentKeyId, err = nodeenrollment.KeyIdFromPkix(nodeCreds.CertificatePublicKeyPkix) + currentKeyId, err := nodeenrollment.KeyIdFromPkix(nodeCreds.CertificatePublicKeyPkix) if err != nil { return fmt.Errorf("error deriving worker auth key id: %w", err) } + w.WorkerAuthCurrentKeyId.Store(currentKeyId) } // Regardless, we want to load the currentKeyId currentKeyId, err := nodeenrollment.KeyIdFromPkix(nodeCreds.CertificatePublicKeyPkix) diff --git a/internal/daemon/worker/worker_test.go b/internal/daemon/worker/worker_test.go index 017bd978f..028c4e152 100644 --- a/internal/daemon/worker/worker_test.go +++ b/internal/daemon/worker/worker_test.go @@ -260,7 +260,7 @@ func TestSetupWorkerAuthStorage(t *testing.T) { // Validate existing key was loaded or new key was created and loaded if tt.expKeyId != "" { - assert.Equal(t, tt.expKeyId, tw.Worker().WorkerAuthCurrentKeyId) + assert.Equal(t, tt.expKeyId, tw.Worker().WorkerAuthCurrentKeyId.Load()) } else { assert.NotEmpty(t, tw.Worker().WorkerAuthCurrentKeyId.Load()) }