Skip to content

Conversation

daschaa
Copy link
Contributor

@daschaa daschaa commented Jan 27, 2023

Fixes #23727

I have not added an integration test for SM2 because i can not deploy in the China Region.


All Submissions:

Adding new Construct Runtime Dependencies:

  • This PR adds new construct runtime dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Jan 27, 2023

@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels Jan 27, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team January 27, 2023 08:38
@github-actions github-actions bot added the admired-contributor [Pilot] contributed between 13-24 PRs to the CDK label Jan 27, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@aws-cdk-automation aws-cdk-automation dismissed their stale review January 27, 2023 09:04

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

@Jacco
Copy link
Contributor

Jacco commented Jan 27, 2023

Great to see another PR on this issue. There are a few differences :-)

I viewed the code and have a few remarks:

  • the grantGenerateMac and grantVerifyMac methods are available and callable without error on non-HMAC keys. Especially for imported keys it is not obvious to users of the IKey that they should not use grantDecrypt grantEncrypt.
  • the extra error that is triggered on enableKeyRotation=True for HMAC keys is not necessary since it would already triggered
  • when creating HMAC keys defaulting keyUsage to GENERATE_VERIFY_MAC give slightly better experience

I like the fact that your PR is a lot simpler than mine. I had to go through changing the context provider for kms keys which leads to all kinds of trouble...

@daschaa
Copy link
Contributor Author

daschaa commented Jan 27, 2023

@Jacco What a coincidence that we both have pushed this in the same time. Thank you very much for the valuable feedback.

I agree that the grantVerify and grantGenerate methods do not check if the key is a HMAC one. But I'm not sure if I have to throw an error since the methods specifically tell what's happening and do not hide something. I guess the overhead in code/logic is not worth the benefit. In the end the policy is just extended.
I would like to hear a third opinion from someone of the CDK Team on this - I'm not sure what's best practice.

Regarding the check of the key rotation I just wanted to have a good error message :-)

And regarding the default usage I think this is a very good idea and I will definitely add this to the code.

Thanks again! ❤️

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

Usually I only provide feedback on the older PR when there's a duplicate, but since y'all are conversing on this issue, I'm going to provide feedback on both so that maybe in collaboration we can come to a better solution.

Just one comment inline but it has pretty major repercussions on the PR. Let me know what you think, please.

@@ -966,16 +967,40 @@ describe('key specs and key usages', () => {
});
});

test('invalid combinations of key specs and key usages', () => {
test.each([
Copy link
Contributor

Choose a reason for hiding this comment

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

This really concerns me. I know you didn't write this original contract but this many invalid options that won't be discovered until synth time makes me uneasy. Is there a contract update we can make here to enforce the valid combos better?

Copy link
Contributor

@Jacco Jacco Feb 12, 2023

Choose a reason for hiding this comment

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

Maybe it would be better to create a separate class for Hmac keys? Maybe there actually should be a few classes:

SymmetricKey

  • only one to have enableKeyRotation in props
  • usage is always ENCRYPT_DECRYPT

HmacKey

  • usage is always GENERATE_VERIFY_MAC

ShaKey

  • usage is always SIGN_VERIFY

RsaKey

  • usage options SIGN_VERIFY or ENCRYPT_DESCRYPT

But also the base needs some (breaking?) refactorings because I think a key that has no ENCRYPT_DECRYPT usage should not have grantDescrypt/grantEncrypt methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jacco How can we ensure backwards compatibility with this separation?
When someone upgrades to a potential version with a class for each key, how will the Basic Key class behave?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TheRealAmazonKendra Do you mean extending the tests to make the invalid combinations clearer? Or changing the logic inside the production code?

@Jacco
Copy link
Contributor

Jacco commented Feb 12, 2023

Maybe it would be better to create a separate class for Hmac keys? Maybe there actually should be a few classes:

SymmetricKey

  • only one to have enableKeyRotation in props
  • usage is always ENCRYPT_DECRYPT

HmacKey

  • usage is always GENERATE_VERIFY_MAC

ShaKey

  • usage is always SIGN_VERIFY

RsaKey

  • usage options SIGN_VERIFY or ENCRYPT_DESCRYPT

But also the base needs some (breaking?) refactorings because I think a key that has no ENCRYPT_DECRYPT usage should not have grantDescrypt/grantEncrypt methods.

@Jacco
Copy link
Contributor

Jacco commented Feb 12, 2023

@daschaa yeah I know, will be hard. Maybe in a new package? aws-kms-v2? I actually haven't seen anything like that happening in the cdk project.

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review March 3, 2023 13:37

Pull request has been modified.

@daschaa
Copy link
Contributor Author

daschaa commented Mar 3, 2023

@TheRealAmazonKendra I have changed the tests so that a test case for every deny list entry is added. It's not yet perfect, because I decided to not export the deny list from the Key class but instead duplicating it to the tests. My thought was that the deny list should stay internal and if i export it, it is publicly available - I think this wouldn't be so good because we don't want to expose the list just for the tests.

I'm looking forward for your feedback and I hope I understood your review correctly 😄

Copy link
Contributor

@MrArnoldPalmer MrArnoldPalmer left a comment

Choose a reason for hiding this comment

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

I agree that the grantVerify and grantGenerate methods do not check if the key is a HMAC one. But I'm not sure if I have to throw an error since the methods specifically tell what's happening and do not hide something. I guess the overhead in code/logic is not worth the benefit. In the end the policy is just extended.
I would like to hear a third opinion from someone of the CDK Team on this - I'm not sure what's best practice.

So usually if we have the ability to detect an incompatible combination during synth, than we will and throw an error. So if the user is able to call grantGenerateMac and grantVerifyMac on keys where that policy doesn't make sense, I do think we should throw an error there.

@MrArnoldPalmer
Copy link
Contributor

Otherwise I feel like this looks pretty good. Agreed that invalid combination issue is annoying and would be nice if we could fix but I don't see a way without breaking changes.

@mergify mergify bot dismissed MrArnoldPalmer’s stale review March 9, 2023 06:34

Pull request has been modified.

@daschaa
Copy link
Contributor Author

daschaa commented Mar 11, 2023

I agree that the grantVerify and grantGenerate methods do not check if the key is a HMAC one. But I'm not sure if I have to throw an error since the methods specifically tell what's happening and do not hide something. I guess the overhead in code/logic is not worth the benefit. In the end the policy is just extended.
I would like to hear a third opinion from someone of the CDK Team on this - I'm not sure what's best practice.

So usually if we have the ability to detect an incompatible combination during synth, than we will and throw an error. So if the user is able to call grantGenerateMac and grantVerifyMac on keys where that policy doesn't make sense, I do think we should throw an error there.

@MrArnoldPalmer I added a guard statement to the grant*Mac methods. Can you have a look if it is good like that? :)

@daschaa daschaa requested a review from MrArnoldPalmer March 20, 2023 12:10
Copy link
Contributor

@MrArnoldPalmer MrArnoldPalmer left a comment

Choose a reason for hiding this comment

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

Just a leftover console.log and a question.

@mergify mergify bot dismissed MrArnoldPalmer’s stale review March 21, 2023 11:49

Pull request has been modified.

@daschaa daschaa requested a review from MrArnoldPalmer March 22, 2023 08:10
Copy link
Contributor

@MrArnoldPalmer MrArnoldPalmer left a comment

Choose a reason for hiding this comment

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

Looks good, thank you @daschaa

@mergify
Copy link
Contributor

mergify bot commented Mar 22, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: c400686
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit f2f3c21 into aws:main Mar 23, 2023
@mergify
Copy link
Contributor

mergify bot commented Mar 23, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

homakk pushed a commit to homakk/aws-cdk that referenced this pull request Mar 28, 2023
Fixes aws#23727

I have not added an integration test for SM2 because i can not deploy in the China Region.

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-kms): Add support for HMAC keys
5 participants