Skip to content

Conversation

viren-nadkarni
Copy link
Member

@viren-nadkarni viren-nadkarni commented May 4, 2023

Summary

This PR adds support for cross-accounts access to following operations in KMS:

  • CreateGrant
  • DescribeKey
  • GetKeyRotationStatus
  • GetPublicKey
  • ListGrants
  • RetireGrant
  • RevokeGrant
  • Decrypt
  • Encrypt
  • GenerateDataKey
  • GenerateDataKeyPair
  • GenerateDataKeyPairWithoutPlaintext
  • GenerateDataKeyWithoutPlaintext
  • GenerateMac
  • Sign
  • Verify
  • VerifyMac

In essence, this means that it is possible to use KMS keys belonging to other accounts. This works only when using key ARNs for above operations. Furthermore, this must happen in the same region.

Notes

This PR changes the nature of Grant Tokens. This will therefore break existing pods/persisted states that have grants.

@viren-nadkarni viren-nadkarni self-assigned this May 4, 2023
@viren-nadkarni viren-nadkarni added the semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases label May 4, 2023
@coveralls
Copy link

coveralls commented May 4, 2023

Coverage Status

Coverage: 82.27% (+0.01%) from 82.256% when pulling 23b2ca9 on kms-cross-accounts into 30b6eb3 on master.

@viren-nadkarni viren-nadkarni changed the title KMS: Add cross-accounts support KMS: implement cross-accounts access May 4, 2023
@github-actions
Copy link

github-actions bot commented May 4, 2023

LocalStack Community integration with Pro

2 036 tests   1 754 ✔️  1h 16m 45s ⏱️
       2 suites     282 💤
       2 files           0

Results for commit 23b2ca9.

♻️ This comment has been updated with latest results.

Comment on lines 551 to 552
if not is_valid_key_arn(self.metadata["KeyId"]):
self.metadata["KeyId"] = kms_key_arn(self.metadata["KeyId"], account_id, region_name)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is changed to always store the KMS key ARN rather than just the ID.

This is required for cross-account key retiring.

Comment on lines +566 to +567
decoded_token = account_id + ":" + region_name + ":" + long_uid()
self.token = to_str(base64.b64encode(to_bytes(decoded_token)))
Copy link
Member Author

Choose a reason for hiding this comment

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

Similary, the account ID and region is now encoded in the grant token to help locate the grant when being operated on across-accounts.

raise InvalidGrantIdException()
if store.grants[grant_id].metadata["KeyId"] != key_id:
_, _, grant_key_id = parse_key_arn(grant.metadata.get("KeyId"))
if key_id != grant_key_id:
raise ValidationError(f"Invalid KeyId={key_id} specified for grant {grant_id}")

Copy link
Member Author

@viren-nadkarni viren-nadkarni May 5, 2023

Choose a reason for hiding this comment

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

This is refactored to simplify the control flow. Only the common logic is retained in this method.

While RevokeGrant and RetireGrant both delete a grant, the parameters they take are different.

@viren-nadkarni viren-nadkarni marked this pull request as ready for review May 5, 2023 14:28
Comment on lines +469 to +470
else:
grant_store = self._get_store(context.account_id, context.region)
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition will never occur; in all the cases where grant_token is None, L458 will throw an exception.

Copy link
Member Author

@viren-nadkarni viren-nadkarni May 9, 2023

Choose a reason for hiding this comment

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

@sannya-singal: If grant token is not set, but both grant ID and key ID is set, this condition will occur.

Indeed there was no test for this, I've added one.

@sannya-singal sannya-singal self-requested a review May 9, 2023 10:24
@viren-nadkarni viren-nadkarni merged commit 2977f13 into master May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants