diff --git a/pkg/server/options/encryptionconfig/config.go b/pkg/server/options/encryptionconfig/config.go index 8fb0ae768..6d40e5427 100644 --- a/pkg/server/options/encryptionconfig/config.go +++ b/pkg/server/options/encryptionconfig/config.go @@ -376,13 +376,18 @@ func (h *kmsv2PluginProbe) rotateDEKOnKeyIDChange(ctx context.Context, statusKey ExpirationTimestamp: expirationTimestamp, CacheKey: cacheKey, }) - klog.V(6).InfoS("successfully rotated DEK", - "uid", uid, - "newKeyID", resp.KeyID, - "oldKeyID", state.KeyID, - "expirationTimestamp", expirationTimestamp.Format(time.RFC3339), - ) - return nil + + // it should be logically impossible for the new state to be invalid but check just in case + _, errGen = h.getCurrentState() + if errGen == nil { + klog.V(6).InfoS("successfully rotated DEK", + "uid", uid, + "newKeyID", resp.KeyID, + "oldKeyID", state.KeyID, + "expirationTimestamp", expirationTimestamp.Format(time.RFC3339), + ) + return nil + } } return fmt.Errorf("failed to rotate DEK uid=%q, errState=%v, errGen=%v, statusKeyID=%q, encryptKeyID=%q, stateKeyID=%q, expirationTimestamp=%s", diff --git a/pkg/server/options/encryptionconfig/config_test.go b/pkg/server/options/encryptionconfig/config_test.go index 003441da3..bac46f3f1 100644 --- a/pkg/server/options/encryptionconfig/config_test.go +++ b/pkg/server/options/encryptionconfig/config_test.go @@ -1703,7 +1703,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { { name: "happy path, with previous state", service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called - state: validState("2", now), + state: validState(t, "2", now), statusKeyID: "2", wantState: envelopekmsv2.State{ KeyID: "2", @@ -1716,7 +1716,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { { name: "previous state expired but key ID matches", service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called - state: validState("3", now.Add(-time.Hour)), + state: validState(t, "3", now.Add(-time.Hour)), statusKeyID: "3", wantState: envelopekmsv2.State{ KeyID: "3", @@ -1729,7 +1729,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { { name: "previous state expired but key ID does not match", service: &testKMSv2EnvelopeService{keyID: "4"}, - state: validState("3", now.Add(-time.Hour)), + state: validState(t, "3", now.Add(-time.Hour)), statusKeyID: "4", wantState: envelopekmsv2.State{ KeyID: "4", @@ -1746,7 +1746,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { { name: "service down but key ID does not match", service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, - state: validState("4", now.Add(7*time.Minute)), + state: validState(t, "4", now.Add(7*time.Minute)), statusKeyID: "5", wantState: envelopekmsv2.State{ KeyID: "4", @@ -1778,7 +1778,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { { name: "invalid service response, with previous state", service: &testKMSv2EnvelopeService{keyID: "3", encryptAnnotations: map[string][]byte{"panda": nil}}, - state: validState("2", now), + state: validState(t, "2", now), statusKeyID: "3", wantState: envelopekmsv2.State{ KeyID: "2", @@ -1831,17 +1831,52 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { if errString(err) != tt.wantErr { t.Errorf("rotateDEKOnKeyIDChange() error = %v, wantErr %v", err, tt.wantErr) } + + // if the old or new state is valid, we should be able to use it + if _, stateErr := h.getCurrentState(); stateErr == nil || err == nil { + transformer := envelopekmsv2.NewEnvelopeTransformer( + &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called + "panda", + h.getCurrentState, + ) + + dataCtx := value.DefaultContext(sampleContextText) + originalText := []byte(sampleText) + + transformedData, err := transformer.TransformToStorage(ctx, originalText, dataCtx) + if err != nil { + t.Fatal(err) + } + + untransformedData, stale, err := transformer.TransformFromStorage(ctx, transformedData, dataCtx) + if err != nil { + t.Fatal(err) + } + if stale { + t.Error("unexpected stale data") + } + if !bytes.Equal(untransformedData, originalText) { + t.Fatalf("incorrect transformation, want: %v, got: %v", originalText, untransformedData) + } + } }) } } -func validState(keyID string, exp time.Time) envelopekmsv2.State { +func validState(t *testing.T, keyID string, exp time.Time) envelopekmsv2.State { + t.Helper() + + transformer, resp, cacheKey, err := envelopekmsv2.GenerateTransformer(testContext(t), "", &testKMSv2EnvelopeService{keyID: keyID}) + if err != nil { + t.Fatal(err) + } return envelopekmsv2.State{ - Transformer: &resourceTransformer{}, - EncryptedDEK: []byte{1}, - KeyID: keyID, + Transformer: transformer, + EncryptedDEK: resp.Ciphertext, + KeyID: resp.KeyID, + Annotations: resp.Annotations, ExpirationTimestamp: exp, - CacheKey: []byte{1}, + CacheKey: cacheKey, } }