Make current key update on rotation

This commit is contained in:
Jeff Mitchell authored and irenarindos committed 2022-06-10 09:33:09 -04:00
1 parent 16d95262a6
commit 5a7291131f
7 files changed
+18 -10

No files matched your search

+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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 {
+7 -1
View File
@@ -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
}
@@ -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
+1 -4
View File
@@ -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"),
+4 -2
View File
@@ -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)
+1 -1
View File
@@ -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())
}