Skip to content

Commit

Permalink
fix(iam): override Role.applyRemovalPolicy for customizeRoles (#31652)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

Closes #31651 

### Reason for this change



`Role.customizeRoles` throws an Error if there is a construct that calls `applyRemovalPolicy` internally. This means users cannot use with some constructs like `RestApi`.

```
Error: Cannot apply RemovalPolicy: no child or not a CfnResource. Apply the removal policy on the CfnResource directly.
```

This can be reproduced with:

```typescript
const app = new App();
Role.customizeRoles(app);
const stack = new Stack(app, 'Stack');
new RestApi(stack, 'RestApi');
```

Or explicitly:
 
```typescript
const app = new App();
Role.customizeRoles(app);
const stack = new Stack(app, 'Stack');
const role = new Role(stack, 'Role', { assumedBy: new ServicePrincipal('sns.amazonaws.com') });
role.applyRemovalPolicy(RemovalPolicy.DESTROY);
```

### Description of changes



While it might be possible to fix `RestApi`, there could be other constructs within aws-cdk-lib that also call `Role.applyRemovalPolicy`. Moreover, it's nearly impossible to make library users aware of this.

Since `Role` implements the `IResource` interface, it has the responsibility to respond to the `applyRemovalPolicy` call. Therefore, I think it would be good to override `applyRemovalPolicy` in the `Role` class.

### Description of how you validated changes



Fixed the existing unit test to change behavior.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
konokenj authored Oct 8, 2024
1 parent 81b47b7 commit 35ed5c6
Show file tree
Hide file tree
Showing 13 changed files with 850 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"Resources": {
"RestApi0C43BF4B": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": "RestApi"
}
},
"RestApiAccount7C83CF5A": {
"Type": "AWS::ApiGateway::Account",
"Properties": {
"CloudWatchRoleArn": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::",
{
"Ref": "AWS::AccountId"
},
":role/precreated-role-api"
]
]
}
},
"DependsOn": [
"RestApi0C43BF4B"
],
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"RestApiDeployment180EC503f7fb2c75a4565f0d9e459f179f2a7481": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"Description": "Automatically created by the RestApi construct",
"RestApiId": {
"Ref": "RestApi0C43BF4B"
}
},
"DependsOn": [
"RestApiGET0F59260B"
]
},
"RestApiDeploymentStageprod3855DE66": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "RestApiDeployment180EC503f7fb2c75a4565f0d9e459f179f2a7481"
},
"RestApiId": {
"Ref": "RestApi0C43BF4B"
},
"StageName": "prod"
},
"DependsOn": [
"RestApiAccount7C83CF5A"
]
},
"RestApiGET0F59260B": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "NONE",
"HttpMethod": "GET",
"Integration": {
"Type": "MOCK"
},
"ResourceId": {
"Fn::GetAtt": [
"RestApi0C43BF4B",
"RootResourceId"
]
},
"RestApiId": {
"Ref": "RestApi0C43BF4B"
}
}
}
},
"Outputs": {
"RestApiEndpoint0551178A": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "RestApi0C43BF4B"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "RestApiDeploymentStageprod3855DE66"
},
"/"
]
]
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 35ed5c6

Please sign in to comment.