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

fix(core): adding warnings to alert when properties will be overwritten #33507

Merged
merged 6 commits into from
Feb 20, 2025

Conversation

IkeNefcy
Copy link
Contributor

Issue

Closes #32468

Reason for this change

When using a custom resource, the values for serviceToken and serviceTimeout are added to the construct prop properties during the synth. Thus passing those values through to the lambda. The issue is that these values can be overwritten if you also include the exact keys in your own properties argument.

So if I include serviceToken, which is a required arg, then I set properties

properties: {
  ServiceToken: 'something else',
},

the value of serviceToken is set to ServiceToken, then my property I wrote to ServiceToken takes over and replaces the value with my own.

This change is to add a warning to the user so they can understand that what they are doing is overwriting that key, as well as add some more detailed flavor text to the properties and readme to help convey this.

Description of changes

Previously the props like serviceToken were being written directly to properties, along with the user provided properties broken out with ... notation.

I moved the automatically added props out of this

    const constructPropertiesPassed = {
      ServiceToken: props.serviceToken,
      ServiceTimeout: props.serviceTimeout?.toSeconds().toString(),
    };

    const hasCommonKeys = Object.keys(properties).some(key => key in constructPropertiesPassed);

    if (hasCommonKeys) {
      Annotations.of(this).addWarningV2('@aws-cdk/core:customResourcePropDuplicate', `CustomResource properties should not contain keys that are automatically added by the CDK. Found: ${Object.keys(properties).filter(key => key in constructPropertiesPassed)}`);
    }

    this.resource = new CfnResource(this, 'Default', {
      type,
      properties: {
        ...constructPropertiesPassed,
        ...properties,
      },
    });

This allowed for a simple comparison between the 2 dicts, which allows for the warning to be initiated from.

Description of how you validated changes

I added a test to check if this warning is being generated.

I did not change any integs because the actual synth in the end is the exact same as before.

Checklist


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

@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels Feb 19, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team February 19, 2025 16:24
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.

(This review is outdated)

Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.17%. Comparing base (8728d5b) to head (f5034f2).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #33507      +/-   ##
==========================================
+ Coverage   82.16%   82.17%   +0.01%     
==========================================
  Files         119      119              
  Lines        6857     6862       +5     
  Branches     1157     1158       +1     
==========================================
+ Hits         5634     5639       +5     
  Misses       1120     1120              
  Partials      103      103              
Flag Coverage Δ
suite.unit 82.17% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk ∅ <ø> (∅)
packages/aws-cdk-lib/core 82.17% <100.00%> (+0.01%) ⬆️

@IkeNefcy
Copy link
Contributor Author

Exemption Request : No integs need to be changed, this is just adding a warning test during synth. As far as I'm aware this is not something we can test with integs. The actual Synth'ed template should be the same as before.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Feb 19, 2025
@IkeNefcy IkeNefcy marked this pull request as ready for review February 19, 2025 16:56
@IkeNefcy IkeNefcy requested a review from a team as a code owner February 19, 2025 16:56
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 19, 2025
@IkeNefcy
Copy link
Contributor Author

Open to any verbiage changes

@samson-keung samson-keung self-assigned this Feb 19, 2025
Copy link
Contributor

@samson-keung samson-keung left a comment

Choose a reason for hiding this comment

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

A few minor comments on the wording. Thank you for making the change.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 20, 2025
@samson-keung samson-keung added pr-linter/exempt-integ-test The PR linter will not require integ test changes and removed pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. labels Feb 20, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review February 20, 2025 00:14

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

@mergify mergify bot dismissed samson-keung’s stale review February 20, 2025 15:13

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 20, 2025
Copy link
Contributor

@samson-keung samson-keung left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

Copy link
Contributor

mergify bot commented Feb 20, 2025

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: f5034f2
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

Copy link
Contributor

mergify bot commented Feb 20, 2025

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

@mergify mergify bot merged commit b06daf8 into aws:main Feb 20, 2025
20 checks passed
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 Feb 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CustomResource: ServiceToken clobbered by properties
3 participants