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(custom-resources): AwsCustomResource requires a policy which updates immutable roles #20966

Merged
merged 4 commits into from Aug 5, 2022

Conversation

haslam22
Copy link
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

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
Contributor

Thank you for this PR @haslam22 :)

@haslam22
Copy link
Contributor Author

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

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.

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

* @see Policy.fromStatements
* @see Policy.fromSdkCalls
*/
readonly policy: AwsCustomResourcePolicy;
readonly policy?: AwsCustomResourcePolicy;
Copy link
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
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.

@@ -344,6 +350,10 @@ export class AwsCustomResource extends Construct implements iam.IGrantable {
throw new Error('At least `onCreate`, `onUpdate` or `onDelete` must be specified.');
}

if (!props.role && !props.policy) {
throw new Error('Either `policy` or `role` must be specified.');
Copy link
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
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
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
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
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
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
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
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
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
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).

josephedward pushed a commit to josephedward/aws-cdk that referenced this pull request Aug 30, 2022
…tes immutable roles (aws#20966)

This fixes aws#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:

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

### Adding new Unconventional Dependencies:

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

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] 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
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