Skip to content

fix(custom-resources): AwsCustomResource requires a policy which updates immutable roles#20966

Merged
mergify[bot] merged 4 commits intoaws:mainfrom
haslam22:main
Aug 5, 2022
Merged

fix(custom-resources): AwsCustomResource requires a policy which updates immutable roles#20966
mergify[bot] merged 4 commits intoaws:mainfrom
haslam22:main

Conversation

@haslam22
Copy link
Copy Markdown
Contributor

@haslam22 haslam22 commented Jul 2, 2022

This fixes #13232 by making the policy property optional in AwsCustomResource as long as role is specified.

Previously, if an immutable role was provided to AwsCustomResource, an IAM policy was still created with a reference to the provided role. This resulted in a call to iam:PutRolePolicy to update the immutable role when deploying the stack.

The underlying motivation for using an immutable role here is to support a restricted corporate environment where IAM role changes are not allowed.


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional 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
Copy Markdown

gitpod-io bot commented Jul 2, 2022

@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 Jul 2, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team July 2, 2022 17:12
@mattiamatrix
Copy link
Copy Markdown
Contributor

Thank you for this PR @haslam22 :)

@haslam22
Copy link
Copy Markdown
Contributor Author

No problem @mattiamatrix - apologies for not spotting your original PR on this and thanks for resolving the duplication :)

Copy link
Copy Markdown
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.

Thank you for your contribution! Just a few quick comments inline.

* @see Policy.fromSdkCalls
*/
readonly policy: AwsCustomResourcePolicy;
readonly policy?: AwsCustomResourcePolicy;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like to see an integration test for when policy is not provided and role is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added an integration test which creates an AwsCustomResource with a role but no policy. Got this passing locally and updated the snapshot.

}

if (!props.role && !props.policy) {
throw new Error('Either `policy` or `role` must be specified.');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This makes it sound like they are mutually exclusive. Can you refine this message a little more?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was tricky to get correct, but I've modified the message slightly now to mention both can be used together.

// If the policy was deleted first, then the function might lose permissions to delete the custom resource
// This is here so that the policy doesn't get removed before onDelete is called
this.customResource.node.addDependency(policy);
// Create the policy statements for the custom resource function role, or use the user-provided ones
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why move the location of this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The old code created the policy first, followed by the custom resource, and finally made the custom resource dependent on the policy. It felt a bit more natural to first create the custom resource, and then create the policy + dependency on the policy together in the same if(props.policy) { ... } block.

The alternative is to not move this around and wrap both the policy creation and dependency addition with if(props.policy), but it feels neater in one block.

@TheRealAmazonKendra
Copy link
Copy Markdown
Contributor

Oh, actually one more note. On a fix, you want to describe the problem, not the solution you're implementing. Take a look at our changelog for examples.

@mergify mergify bot dismissed TheRealAmazonKendra’s stale review August 4, 2022 22:52

Pull request has been modified.

@haslam22 haslam22 changed the title fix(custom-resources): make policy optional if role specified in AwsCustomResource fix(custom-resources): AwsCustomResource requires a policy which updates immutable roles Aug 4, 2022
@haslam22
Copy link
Copy Markdown
Contributor Author

haslam22 commented Aug 4, 2022

Oh, actually one more note. On a fix, you want to describe the problem, not the solution you're implementing. Take a look at our changelog for examples.

Thanks for the review @TheRealAmazonKendra. I've updated the PR title and addressed the other comments.

Copy link
Copy Markdown
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.

Looks good. Thanks for your contribution!

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 5, 2022

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
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: e09ebf5
  • 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 a02ef9c into aws:main Aug 5, 2022
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Aug 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

[custom-resources] allow for no policy to be specified

4 participants