Skip to content
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

KMS: Fixup ImportKeyMaterial for non-symmetric keys #10116

Merged
merged 7 commits into from Mar 20, 2024

Conversation

uubk
Copy link
Contributor

@uubk uubk commented Jan 24, 2024

Motivation

KMS allows you to import key material, that is, create KMS keys with "private keys" you already have. In the context of Localstack this is fairly important now that the old "local-kms" backend was removed since the new backend cannot be preseeded at the moment and controlling key material can be very important in testing.

Changes

This PR drops the restriction on encrypt/decrypt for the KeyUsage in ImportKeyMaterial (the restriction for GetParametersForImport was already dropped as part of the fix for #9111) and should therefore fix #10115. It then fixes the issues that crop up when trying to use this with existing code - notably the hardcoded assumption that all imported key material is a symmetric key. Finally, it adds a new test to track this usecase and fixes a few other small issues you bump into when you update the KMS test snapshots.

Copy link
Collaborator

@localstack-bot localstack-bot left a comment

Choose a reason for hiding this comment

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

Welcome to LocalStack! Thanks for raising your first Pull Request and landing in your contributions. Our team will reach out with any reviews or feedbacks that we have shortly. We recommend joining our Slack Community and share your PR on the #community channel to share your contributions with us. Please make sure you are following our contributing guidelines and our Code of Conduct.

@localstack-bot
Copy link
Collaborator

localstack-bot commented Jan 24, 2024

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

@uubk
Copy link
Contributor Author

uubk commented Jan 24, 2024

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

tests/aws/services/kms/test_kms.snapshot.json Outdated Show resolved Hide resolved
tests/aws/services/kms/test_kms.py Show resolved Hide resolved
tests/aws/services/kms/test_kms.validation.json Outdated Show resolved Hide resolved
@sannya-singal
Copy link
Contributor

Hi @uubk, thanks for contributing to localstack 🚀.

I have requested a few changes, kindly also make sure that the CI checks are green. 🙂

@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 Mar 14, 2024
localstack/services/kms/models.py Outdated Show resolved Hide resolved
localstack/services/kms/models.py Outdated Show resolved Hide resolved
@uubk uubk force-pushed the fixup-kms-ecc-import branch 2 times, most recently from e7868a1 to e4fe42b Compare March 14, 2024 09:06
@uubk
Copy link
Contributor Author

uubk commented Mar 14, 2024

Hi @uubk, thanks for contributing to localstack 🚀.

I have requested a few changes, kindly also make sure that the CI checks are green. 🙂

Hey - thanks for the review!
All checks are green except test-selection - that one will presumably not run for external contributors since it is complaining about the missing GITHUB_API_TOKEN (sounds like secret protection rules).

@uubk uubk requested a review from sannya-singal March 14, 2024 09:23
localstack/services/kms/models.py Outdated Show resolved Hide resolved
localstack/services/kms/models.py Outdated Show resolved Hide resolved
Comment on lines 215 to 224
key = load_der_private_key(material, password=None)
self.public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.DER,
crypto_serialization.PublicFormat.SubjectPublicKeyInfo,
)
self.private_key = key.private_bytes(
crypto_serialization.Encoding.DER,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I see similar logic used in

self.private_key = key.private_bytes(
crypto_serialization.Encoding.DER,
crypto_serialization.PrivateFormat.PKCS8,
crypto_serialization.NoEncryption(),
)
self.public_key = key.public_key().public_bytes(
crypto_serialization.Encoding.DER,
crypto_serialization.PublicFormat.SubjectPublicKeyInfo,

Can we optimise this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved it to its own function

@sannya-singal
Copy link
Contributor

Hi @uubk, thanks for contributing to localstack 🚀.
I have requested a few changes, kindly also make sure that the CI checks are green. 🙂

Hey - thanks for the review! All checks are green except test-selection - that one will presumably not run for external contributors since it is complaining about the missing GITHUB_API_TOKEN (sounds like secret protection rules).

This should be fixed by #10459, kindly rebase the PR.

AWS KMS allows importing keys for any of the supported KMS operations,
not just encryption/decryption. This was already fixed for
GetParametersForImport (#9111), however, the check
for ImportKeyMaterial remained - lets drop it.
When calling ImportKeyMaterial, we cannot assume that we always import a
symmetric key. Check for this case in KmsCryptoKey and make sure we set
the correct member. Also note that not all asymmetric keys are RSA.
Unfortunately, the _name_ of the third argument to key.sign() appears to
be _platform dependant_. Split up the argument construction so that we
can avoid referring to it by name.
Much like the case for symmetric keys, this test generates a sample key
and imports it, but uses it for sign/verify instead of encrypt/decrypt.
@uubk
Copy link
Contributor Author

uubk commented Mar 15, 2024

Hi @uubk, thanks for contributing to localstack 🚀.
I have requested a few changes, kindly also make sure that the CI checks are green. 🙂

Hey - thanks for the review! All checks are green except test-selection - that one will presumably not run for external contributors since it is complaining about the missing GITHUB_API_TOKEN (sounds like secret protection rules).

This should be fixed by #10459, kindly rebase the PR.

Yes, can confirm! The pipeline is now green, thanks!

@uubk uubk requested a review from sannya-singal March 15, 2024 12:16
@sannya-singal
Copy link
Contributor

Thanks @uubk for these changes and fixing reviews patiently! 🎉 LGTM!

@sannya-singal sannya-singal merged commit 9d29716 into localstack:master Mar 20, 2024
27 checks passed
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: Localstack still does not support import a key material when key usage is SIGN_VERIFY
3 participants