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

feat(kms): allow fromLookup method to return dummy key if target key was not found #31676

Merged
merged 7 commits into from
Oct 17, 2024

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Oct 6, 2024

Issue # (if applicable)

Closes #31574.

Reason for this change

The fromLookup method causes an error if the target key was not found. However it would be also good not to cause an error in that case.

Description of changes

Added returnDummyKeyOnMissing in KeyLookupOptions. If the property is set to true, the context method will not cause an error and will return a dummy key if the key was not found.

Originally, I thought to make the method to return undefined in that case, but the return type of method is IKey. If we change the type to IKey | undefined, it should be a breaking change.

  public static fromLookup(scope: Construct, id: string, options: KeyLookupOptions): IKey {

So I decided to return a dummy key with a dummy key id '1234abcd-12ab-34cd-56ef-1234567890ab'. The dummy key id had been defined originally (see: https://github.com/aws/aws-cdk/blob/v2.161.0/packages/aws-cdk-lib/aws-kms/lib/key.ts#L686).

The property returnDummyKeyOnMissing will be passed to ignoreErrorOnMissingContext added in the PR. If the ignoreErrorOnMissingContext is true and the key doesn't exist, an error will be suppressed in the ContextProvider.

Additional information

see: #31574 (comment)

Description of how you validated changes

Both of unit and integ tests

Checklist


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

@aws-cdk-automation aws-cdk-automation requested a review from a team October 6, 2024 15:29
@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 distinguished-contributor [Pilot] contributed 50+ PRs to the CDK labels Oct 6, 2024
@go-to-k go-to-k changed the title feat(kms): allow fromLookup method to return a dummy key if target key was not found feat(kms): allow fromLookup method to return a dummy key if target key was not found Oct 6, 2024
@go-to-k go-to-k marked this pull request as ready for review October 6, 2024 15:29
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.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Oct 6, 2024
@go-to-k go-to-k marked this pull request as draft October 6, 2024 15:40
@go-to-k go-to-k marked this pull request as ready for review October 6, 2024 15:55
@go-to-k go-to-k changed the title feat(kms): allow fromLookup method to return a dummy key if target key was not found feat(kms): allow fromLookup method to return dummy key if target key was not found Oct 6, 2024
@go-to-k
Copy link
Contributor Author

go-to-k commented Oct 9, 2024

Exemption Request: The code changes in Context Providers only affect the KMS context (lookup) method, they could be covered by unit tests for context-providers and integ tests for KMS module.

@aws-cdk-automation aws-cdk-automation added pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Oct 9, 2024
returnDummyKeyOnMissing: true,
});

if (dummy.keyId === '1234abcd-12ab-34cd-56ef-1234567890ab') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a more user-friendly way to allow detecting dummy results? Maybe the 1234abcd-12ab-34cd-56ef-1234567890ab value can be exposed as a constant from the KMS package (something like kms.Key.DUMMY_KEY_ID)? Or potentially even more friendly would be a method like:

public get isLookupDummy() {
  return this.keyId === Key.DEFAULT_DUMMY_KEY_ID;
}

And then in the future if the implementation were to change to, for example, allow more dynamic dummy values (such as what the SSM parameter does) that could be done without a breaking API change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea! I will add a new static variable DEFAULT_DUMMY_KEY_ID to the Key class.

However, as this dummy id is used by an imported key (IKey), the isLookupDummy method needs to be added to the interface as well as the KeyBase. But it would be a breaking change. This is because users who implement their own constructs using the interface will get an error.

Copy link
Contributor Author

@go-to-k go-to-k Oct 14, 2024

Choose a reason for hiding this comment

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

The following approach is possible, but is this necessary enough to make it a static method...?

export class Key extends KeyBase {
  // ...
  // ...
  public static isLookupDummy(key: IKey): boolean {
    return key.keyId === Key.DEFAULT_DUMMY_KEY_ID;
  }

I didn't add this for now, just added DEFAULT_DUMMY_KEY_ID:

9e8a164

I set the variable name to "DEFAULT_"DUMMY_KEY_ID in anticipation of changing the dummy key ID in the future. If I set it to DUMMY_KEY_ID, I thought we would be in trouble if we made it possible to change the dummy key ID in the future. Please let me know if you feel different.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh good call on the interface issue!

I do in a way like the static method. It's almost reminiscent of, for example, iam.Role.isRole. I could envision that in the future (hypothetically, it'd be possible to someday detect this instead in fromLookup and return a class other than Import) the body would instead read something like:

public static isLookupDummy(key: IKey): boolean {
  return LOOKUP_DUMMY_SYMBOL in key;
}

In this case, the method actually allows us to make that change (otherwise, we'd be locked to the documented keyId-based approach).

This non-failing dummy result for context lookups just feels a bit new overall and I think unlike the SSM parameter situation, detecting this case is actually pretty impactful. We may want to give ourselves some flexibility via abstractions.

But then again, I see and appreciate how simple and straightforward DEFAULT_DUMMY_KEY_ID is; so I am not going to withhold an approval over this. If you feel that the static method may actually be too much abstraction for now, I'll happily go ahead and add my approval 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, the method actually allows us to make that change (otherwise, we'd be locked to the documented keyId-based approach).

Yeah, agree with you.

I have changed, could you please take a look at it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! I like that! I appreciate you making the adjustments.

defaultDummyKeyId

Revert "defaultDummyKeyId"

This reverts commit e297b40.

add readonly
Key.fromLookup()
@aws-cdk-automation aws-cdk-automation added pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Oct 15, 2024
Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

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

Nicely written, appreciate it!

@GavinZZ
Copy link
Contributor

GavinZZ commented Oct 16, 2024

I have kicked off the build to run in the test pipeline. Will update the labels once the build finishes running.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 16, 2024
@GavinZZ GavinZZ added the pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested label Oct 17, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review October 17, 2024 21:39

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

@aws-cdk-automation aws-cdk-automation removed the pr/needs-cli-test-run This PR needs CLI tests run against it. label Oct 17, 2024
Copy link
Contributor

mergify bot commented Oct 17, 2024

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: 6bb2351
  • 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 34bdeca into aws:main Oct 17, 2024
8 of 9 checks passed
Copy link
Contributor

mergify bot commented Oct 17, 2024

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).

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
distinguished-contributor [Pilot] contributed 50+ 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 pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(aws-cdk-lib/aws-kms): Allow testing for the existence of a KMS key by alias name
4 participants