-
Notifications
You must be signed in to change notification settings - Fork 600
fix: prevent refreshAliasesTask from reverting rotated aliases in aws_kms plugin #6805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
angabini
wants to merge
1
commit into
spiffe:main
Choose a base branch
from
angabini:fix-aws-kms-refresh-alias-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1396,6 +1396,100 @@ func TestRefreshAliases(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestRefreshAliasesSkipsRotatedAlias(t *testing.T) { | ||
| // This test verifies that refreshAliasesTask does not revert an alias | ||
| // that was rotated by another replica (e.g. the leader). After configure, | ||
| // we simulate a rotation by pointing the alias to a new key in the fake | ||
| // store. The refresh should detect the mismatch via DescribeKey and skip | ||
| // the UpdateAlias call, leaving the alias pointing to the new key. | ||
| ts := setupTest(t) | ||
|
|
||
| fakeEntries := []fakeKeyEntry{ | ||
| { | ||
| AliasName: aws.String("alias/SPIRE_SERVER/test_example_org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/id_01"), | ||
| KeyID: aws.String("key_id_01"), | ||
| KeySpec: types.KeySpecEccNistP256, | ||
| Enabled: true, | ||
| PublicKey: []byte("foo"), | ||
| CreationDate: &unixEpoch, | ||
| AliasLastUpdatedDate: &unixEpoch, | ||
| }, | ||
| } | ||
| ts.fakeKMSClient.setEntries(fakeEntries) | ||
|
|
||
| refreshAliasesSignal := make(chan error) | ||
| ts.plugin.hooks.refreshAliasesSignal = refreshAliasesSignal | ||
|
|
||
| _, err := ts.plugin.Configure(ctx, configureRequestWithDefaults(t)) | ||
| require.NoError(t, err) | ||
|
|
||
| // Wait for refresh alias task to be initialized | ||
| _ = waitForSignal(t, refreshAliasesSignal) | ||
|
|
||
| // Simulate a rotation by another replica: create a new key and point | ||
| // the alias to it. The plugin's in-memory cache still has key_id_01. | ||
| rotatedEntry := fakeKeyEntry{ | ||
| KeyID: aws.String("key_id_rotated"), | ||
| KeySpec: types.KeySpecEccNistP256, | ||
| Enabled: true, | ||
| PublicKey: []byte("bar"), | ||
| CreationDate: &unixEpoch, | ||
| AliasLastUpdatedDate: &unixEpoch, | ||
| } | ||
| ts.fakeKMSClient.store.SaveKeyEntry(&rotatedEntry) | ||
| err = ts.fakeKMSClient.store.SaveAlias(*rotatedEntry.KeyID, "alias/SPIRE_SERVER/test_example_org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/id_01") | ||
| require.NoError(t, err) | ||
|
|
||
| // Record the alias state before refresh | ||
| aliasBefore := ts.fakeKMSClient.store.aliases["alias/SPIRE_SERVER/test_example_org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/id_01"] | ||
| require.Equal(t, "key_id_rotated", *aliasBefore.KeyEntry.KeyID) | ||
|
|
||
| // Trigger refreshAliasesTask | ||
| ts.clockHook.Add(6 * time.Hour) | ||
| err = waitForSignal(t, refreshAliasesSignal) | ||
| require.NoError(t, err) | ||
|
|
||
| // Assert: the alias must still point to the rotated key, not reverted | ||
| aliasAfter := ts.fakeKMSClient.store.aliases["alias/SPIRE_SERVER/test_example_org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/id_01"] | ||
| require.Equal(t, "key_id_rotated", *aliasAfter.KeyEntry.KeyID, "alias should not have been reverted to the stale cached key") | ||
| } | ||
|
|
||
| func TestRefreshAliasesDescribeKeyError(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could also add a test for the malformed response branch (where DescribeKey succeeds but returns nil KeyMetadata or nil Arn)? |
||
| ts := setupTest(t) | ||
|
|
||
| fakeEntries := []fakeKeyEntry{ | ||
| { | ||
| AliasName: aws.String("alias/SPIRE_SERVER/test_example_org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/id_01"), | ||
| KeyID: aws.String("key_id_01"), | ||
| KeySpec: types.KeySpecEccNistP256, | ||
| Enabled: true, | ||
| PublicKey: []byte("foo"), | ||
| CreationDate: &unixEpoch, | ||
| AliasLastUpdatedDate: &unixEpoch, | ||
| }, | ||
| } | ||
| ts.fakeKMSClient.setEntries(fakeEntries) | ||
|
|
||
| refreshAliasesSignal := make(chan error) | ||
| ts.plugin.hooks.refreshAliasesSignal = refreshAliasesSignal | ||
|
|
||
| _, err := ts.plugin.Configure(ctx, configureRequestWithDefaults(t)) | ||
| require.NoError(t, err) | ||
|
|
||
| // Wait for refresh alias task to be initialized | ||
| _ = waitForSignal(t, refreshAliasesSignal) | ||
|
|
||
| // Inject a DescribeKey error after configure | ||
| ts.fakeKMSClient.setDescribeKeyErr("describe key failure") | ||
|
|
||
| // Trigger refreshAliasesTask | ||
| ts.clockHook.Add(6 * time.Hour) | ||
| err = waitForSignal(t, refreshAliasesSignal) | ||
|
|
||
| require.NotNil(t, err) | ||
| require.Equal(t, "describe key failure", err.Error()) | ||
| } | ||
|
|
||
| func TestDisposeAliases(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this path should also append to errs, similar to the DescribeKey error path.