Skip to content

KMS: RotateKeyOnDemand api update #12806

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
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

willdefig
Copy link

@willdefig willdefig commented Jun 26, 2025

Motivation

In response to the latest changes to KMS that AWS have made it is now possible to rotate keys on demand if they are and external key,
https://aws.amazon.com/blogs/security/how-to-use-on-demand-rotation-for-aws-kms-imported-keys/

Also there is a new item in the metadata for the key to show the CurrentKeyMaterialId this should update when the key is rotated.

Changes

  • Allow RotateKeyOnDemand to accept EXTERNAL Type keys

Tests

  • Changed test_rotate_key_on_demand_fails_for_key_with_imported_key_material to test_rotate_key_on_demand_succeeds_for_key_with_imported_key_material
  • Added test_key_before_and_after_rotations_on_imported_key to fully test the import and rotation of an EXTERNAL key

resolves #12801

@willdefig willdefig requested a review from sannya-singal as a code owner June 26, 2025 08:27
@localstack-bot
Copy link
Collaborator

localstack-bot commented Jun 26, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@sannya-singal sannya-singal added this to the Playground milestone Jun 26, 2025
@sannya-singal sannya-singal requested a review from k-a-il June 26, 2025 08:41
@sannya-singal sannya-singal added semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases aws:kms AWS Key Management Service labels Jun 26, 2025
@willdefig
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

@willdefig
Copy link
Author

recheck

localstack-bot added a commit that referenced this pull request Jun 26, 2025
Copy link
Contributor

@k-a-il k-a-il left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @willdefig thank you for your contribution 🚀
As mentioned in my comment below, I believe it’s important to add the test case described in the article to check that this implementation aligns with AWS behavior. Without this, the behavior may not fully match AWS KMS behavior.

Comment on lines 1459 to 1464
def test_rotate_key_on_demand_succeeds_for_key_with_imported_key_material(
self, kms_create_key, aws_client, snapshot
):
key_id = kms_create_key(Origin="EXTERNAL")["KeyId"]

with pytest.raises(ClientError) as e:
aws_client.kms.rotate_key_on_demand(KeyId=key_id)
snapshot.match("error-response", e.value.response)
response = aws_client.kms.rotate_key_on_demand(KeyId=key_id)
snapshot.match("rotate-on-demand-response", response)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an integration test to confirm that a KMS key with imported key material can be created and used, and that after rotating the key material using the rotate_key_on_demand operation, the KMS key continues to work correctly for both encryption and decryption? The test should also verify that data encrypted with the previous key material can still be successfully decrypted.

This test case is described in this AWS blog post about using on-demand rotation for imported keys.

- Creates Key
- Imports custom Key Material
- Creates Encrypted data key
- Tests it can use it to decrypt
- Creates new key material
- Imports new material
- Rotates new material
- Creates new encrypted key data
- Tests it can decrypt that
- Tests it can still decrypt old key data
 - updated test to include checking rotated keys are still usable.

 - Tidied up import_key_material importing for EXTERNAL keys

 -  rotate_key_on_demand stopped key being recreated if using EXTERNAL keys
 - Fix for TestKMS::test_key_rotations_limits failing due to the last change
@localstack-bot
Copy link
Collaborator

Currently, only patch changes are allowed on master. Your PR labels (aws:kms, semver: minor) indicate that it cannot be merged into the master at this time.

Comment on lines +1472 to +1479
def test_rotate_key_on_demand_succeeds_for_key_with_imported_key_material(
self, kms_create_key, aws_client, snapshot
):
key_id = kms_create_key(Origin="EXTERNAL")["KeyId"]
key_id = kms_create_key(
Origin="EXTERNAL", KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"
)["KeyId"]
response = aws_client.kms.rotate_key_on_demand(KeyId=key_id)
snapshot.match("rotate-on-demand-response", response)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge this test with the test_key_before_and_after_rotations_on_imported_key test? The logic is the same, aside from the snapshot, which can be added to the test_key_before_and_after_rotations_on_imported_key

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing

Comment on lines +1205 to +1206
response_data = {"KeyId": key_to_import_material_to.metadata["KeyId"]}
response_data["KeyMaterialId"] = key_material.hex()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can merge these two lines into one or define values directly in the ImportKeyMaterialResponse

Comment on lines +1509 to +1517
assert retry(
function=_verify_key_meta,
retries=2,
sleep=1.0,
aws_client=aws_client,
key_id=key_id,
item_key="CurrentKeyMaterialId",
item_value=first_key_response["KeyMaterialId"],
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to capture a snapshot of the aws_client.kms.describe_key response to record the state both before and after the key material is rotated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:kms AWS Key Management Service semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: KMS RotateKeyOnDemand now supports imported keys
4 participants