From c9481fc5f2205b4a768852941a5695002b0b8179 Mon Sep 17 00:00:00 2001 From: Kazuho Cryer-Shinozuka Date: Tue, 8 Oct 2024 23:23:32 +0900 Subject: [PATCH 1/3] docs(route53): fix broken heading (#31693) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Issue # (if applicable) None. ### Reason for this change The headings are set incorrectly, causing the table of contents to be misaligned. スクリーンショット 2024-10-08 20 13 13 ### Description of changes Remove heading and made some minor adjustments to the document. ### Description of how you validated changes None ### 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* --- packages/aws-cdk-lib/aws-route53/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-route53/README.md b/packages/aws-cdk-lib/aws-route53/README.md index 1c26a12541242..27914033f704d 100644 --- a/packages/aws-cdk-lib/aws-route53/README.md +++ b/packages/aws-cdk-lib/aws-route53/README.md @@ -105,11 +105,14 @@ new route53.ARecord(this, 'ARecord', { ``` To create an A record of type alias with target set to another record created outside CDK: -### This function registers the given input i.e. DNS Name(string) of an existing record as an AliasTarget to the new ARecord. To register a target that is created as part of CDK use this instead https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_route53_targets-readme.html -```ts +This function registers the given input i.e. DNS Name(string) of an existing record as an AliasTarget to the new ARecord. To register a target that is created as part of CDK use this instead. + +Detailed information can be found in the [documentation](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_route53_targets-readme.html). +```ts declare const myZone: route53.HostedZone; + const targetRecord = 'existing.record.cdk.local'; const record = route53.ARecord.fromARecordAttributes(this, 'A', { zone: myZone, From 81b47b75022750d6d8f64f889d0bae0f3ea0f4d3 Mon Sep 17 00:00:00 2001 From: Matsuda Date: Wed, 9 Oct 2024 01:46:26 +0900 Subject: [PATCH 2/3] docs(events): update link about key policy for event bus (#31674) ### Issue # (if applicable) N/A ### Reason for this change The link about key policy for an Event Bus has been updated. ### Description of changes Updated a link in docs. ### Description of how you validated changes No tests because only docs has been changed. ### 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* --- packages/aws-cdk-lib/aws-events/lib/event-bus.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-events/lib/event-bus.ts b/packages/aws-cdk-lib/aws-events/lib/event-bus.ts index 4999351a625a3..f7b7e94d745e5 100644 --- a/packages/aws-cdk-lib/aws-events/lib/event-bus.ts +++ b/packages/aws-cdk-lib/aws-events/lib/event-bus.ts @@ -353,8 +353,11 @@ export class EventBus extends EventBusBase { resourceName: eventBus.name, }); - // Allow EventBridge to use customer managed key - // See https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-encryption-event-bus-key-policy.html + /** + * Allow EventBridge to use customer managed key + * + * @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-encryption-key-policy.html#eb-encryption-key-policy-bus + */ if (props?.kmsKey) { props?.kmsKey.addToResourcePolicy(new iam.PolicyStatement({ resources: ['*'], From 35ed5c64db787c74abc21ce75176eaa8d240689f Mon Sep 17 00:00:00 2001 From: Kenji Kono <93309555+konokenj@users.noreply.github.com> Date: Wed, 9 Oct 2024 02:37:53 +0900 Subject: [PATCH 3/3] fix(iam): override Role.applyRemovalPolicy for customizeRoles (#31652) ### 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* --- ...efaultTestDeployAssertE3E7D2A4.assets.json | 19 + ...aultTestDeployAssertE3E7D2A4.template.json | 36 ++ .../cdk.out | 1 + .../iam-policy-report.json | 40 +++ .../iam-policy-report.txt | 43 +++ .../integ-customize-roles-restapi.assets.json | 19 + ...nteg-customize-roles-restapi.template.json | 144 ++++++++ .../integ.json | 20 ++ .../manifest.json | 145 ++++++++ .../tree.json | 326 ++++++++++++++++++ .../test/integ.customize-roles-restapi.ts | 42 +++ packages/aws-cdk-lib/aws-iam/lib/role.ts | 15 +- .../aws-cdk-lib/aws-iam/test/role.test.ts | 2 +- 13 files changed, 850 insertions(+), 2 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.txt create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.assets.json new file mode 100644 index 0000000000000..8bbe077289ad2 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.assets.json @@ -0,0 +1,19 @@ +{ + "version": "38.0.1", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "IntegTestDefaultTestDeployAssertE3E7D2A4.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/IntegTestDefaultTestDeployAssertE3E7D2A4.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/cdk.out new file mode 100644 index 0000000000000..c6e612584e352 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"38.0.1"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.json new file mode 100644 index 0000000000000..5bf553722bfed --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.json @@ -0,0 +1,40 @@ +{ + "roles": [ + { + "roleConstructPath": "integ-customize-roles-restapi/RestApi/CloudWatchRole", + "roleName": "precreated-role-api", + "missing": false, + "assumeRolePolicy": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "apigateway.amazonaws.com" + } + } + ], + "managedPolicyArns": [ + "arn:(PARTITION):iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs" + ], + "managedPolicyStatements": [], + "identityPolicyStatements": [] + }, + { + "roleConstructPath": "integ-customize-roles-restapi/Role", + "roleName": "precreated-role", + "missing": false, + "assumeRolePolicy": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "sns.amazonaws.com" + } + } + ], + "managedPolicyArns": [], + "managedPolicyStatements": [], + "identityPolicyStatements": [] + } + ] +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.txt b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.txt new file mode 100644 index 0000000000000..70dcad97ab6ad --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/iam-policy-report.txt @@ -0,0 +1,43 @@ + (integ-customize-roles-restapi/RestApi/CloudWatchRole) + +AssumeRole Policy: +[ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "apigateway.amazonaws.com" + } + } +] + +Managed Policy ARNs: +[ + "arn:(PARTITION):iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs" +] + +Managed Policies Statements: +NONE + +Identity Policy Statements: +NONE (integ-customize-roles-restapi/Role) + +AssumeRole Policy: +[ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "sns.amazonaws.com" + } + } +] + +Managed Policy ARNs: +NONE + +Managed Policies Statements: +NONE + +Identity Policy Statements: +NONE \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.assets.json new file mode 100644 index 0000000000000..7b3fab795d4a7 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.assets.json @@ -0,0 +1,19 @@ +{ + "version": "38.0.1", + "files": { + "f692d0500df97f81556e1e1627880690167cd1d83aecfb1f9c16ed86c73797f9": { + "source": { + "path": "integ-customize-roles-restapi.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "f692d0500df97f81556e1e1627880690167cd1d83aecfb1f9c16ed86c73797f9.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.template.json new file mode 100644 index 0000000000000..84cf2aeb6ad18 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ-customize-roles-restapi.template.json @@ -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", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ.json new file mode 100644 index 0000000000000..2e88ad6a89995 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/integ.json @@ -0,0 +1,20 @@ +{ + "version": "38.0.1", + "testCases": { + "IntegTest/DefaultTest": { + "stacks": [ + "integ-customize-roles-restapi" + ], + "cdkCommandOptions": { + "deploy": { + "enabled": false + }, + "destroy": { + "enabled": false + } + }, + "assertionStack": "IntegTest/DefaultTest/DeployAssert", + "assertionStackName": "IntegTestDefaultTestDeployAssertE3E7D2A4" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/manifest.json new file mode 100644 index 0000000000000..88e643710795a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/manifest.json @@ -0,0 +1,145 @@ +{ + "version": "38.0.1", + "artifacts": { + "integ-customize-roles-restapi.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-customize-roles-restapi.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-customize-roles-restapi": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "integ-customize-roles-restapi.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "notificationArns": [], + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/f692d0500df97f81556e1e1627880690167cd1d83aecfb1f9c16ed86c73797f9.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-customize-roles-restapi.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "integ-customize-roles-restapi.assets" + ], + "metadata": { + "/integ-customize-roles-restapi/RestApi/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApi0C43BF4B" + } + ], + "/integ-customize-roles-restapi/RestApi/Account": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiAccount7C83CF5A" + } + ], + "/integ-customize-roles-restapi/RestApi/Deployment/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiDeployment180EC503f7fb2c75a4565f0d9e459f179f2a7481" + } + ], + "/integ-customize-roles-restapi/RestApi/DeploymentStage.prod/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiDeploymentStageprod3855DE66" + } + ], + "/integ-customize-roles-restapi/RestApi/Endpoint": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiEndpoint0551178A" + } + ], + "/integ-customize-roles-restapi/RestApi/Default/GET/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiGET0F59260B" + } + ], + "/integ-customize-roles-restapi/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-customize-roles-restapi/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "integ-customize-roles-restapi" + }, + "IntegTestDefaultTestDeployAssertE3E7D2A4.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "IntegTestDefaultTestDeployAssertE3E7D2A4.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "IntegTestDefaultTestDeployAssertE3E7D2A4": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "IntegTestDefaultTestDeployAssertE3E7D2A4.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "notificationArns": [], + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "IntegTestDefaultTestDeployAssertE3E7D2A4.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "IntegTestDefaultTestDeployAssertE3E7D2A4.assets" + ], + "metadata": { + "/IntegTest/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/IntegTest/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "IntegTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/tree.json new file mode 100644 index 0000000000000..4c229eb69e4d9 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.js.snapshot/tree.json @@ -0,0 +1,326 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "integ-customize-roles-restapi": { + "id": "integ-customize-roles-restapi", + "path": "integ-customize-roles-restapi", + "children": { + "RestApi": { + "id": "RestApi", + "path": "integ-customize-roles-restapi/RestApi", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-customize-roles-restapi/RestApi/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi", + "aws:cdk:cloudformation:props": { + "name": "RestApi" + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "CloudWatchRole": { + "id": "CloudWatchRole", + "path": "integ-customize-roles-restapi/RestApi/CloudWatchRole", + "children": { + "ImportCloudWatchRole": { + "id": "ImportCloudWatchRole", + "path": "integ-customize-roles-restapi/RestApi/CloudWatchRole/ImportCloudWatchRole", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "PrecreatedRoleCloudWatchRole": { + "id": "PrecreatedRoleCloudWatchRole", + "path": "integ-customize-roles-restapi/RestApi/CloudWatchRole/PrecreatedRoleCloudWatchRole", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Account": { + "id": "Account", + "path": "integ-customize-roles-restapi/RestApi/Account", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Account", + "aws:cdk:cloudformation:props": { + "cloudWatchRoleArn": { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::", + { + "Ref": "AWS::AccountId" + }, + ":role/precreated-role-api" + ] + ] + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Deployment": { + "id": "Deployment", + "path": "integ-customize-roles-restapi/RestApi/Deployment", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-customize-roles-restapi/RestApi/Deployment/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Deployment", + "aws:cdk:cloudformation:props": { + "description": "Automatically created by the RestApi construct", + "restApiId": { + "Ref": "RestApi0C43BF4B" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeploymentStage.prod": { + "id": "DeploymentStage.prod", + "path": "integ-customize-roles-restapi/RestApi/DeploymentStage.prod", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-customize-roles-restapi/RestApi/DeploymentStage.prod/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage", + "aws:cdk:cloudformation:props": { + "deploymentId": { + "Ref": "RestApiDeployment180EC503f7fb2c75a4565f0d9e459f179f2a7481" + }, + "restApiId": { + "Ref": "RestApi0C43BF4B" + }, + "stageName": "prod" + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Endpoint": { + "id": "Endpoint", + "path": "integ-customize-roles-restapi/RestApi/Endpoint", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Default": { + "id": "Default", + "path": "integ-customize-roles-restapi/RestApi/Default", + "children": { + "GET": { + "id": "GET", + "path": "integ-customize-roles-restapi/RestApi/Default/GET", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-customize-roles-restapi/RestApi/Default/GET/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Method", + "aws:cdk:cloudformation:props": { + "authorizationType": "NONE", + "httpMethod": "GET", + "integration": { + "type": "MOCK" + }, + "resourceId": { + "Fn::GetAtt": [ + "RestApi0C43BF4B", + "RootResourceId" + ] + }, + "restApiId": { + "Ref": "RestApi0C43BF4B" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Role": { + "id": "Role", + "path": "integ-customize-roles-restapi/Role", + "children": { + "ImportRole": { + "id": "ImportRole", + "path": "integ-customize-roles-restapi/Role/ImportRole", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "PrecreatedRoleRole": { + "id": "PrecreatedRoleRole", + "path": "integ-customize-roles-restapi/Role/PrecreatedRoleRole", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "integ-customize-roles-restapi/BootstrapVersion", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "integ-customize-roles-restapi/CheckBootstrapVersion", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "PolicySynthesizer": { + "id": "PolicySynthesizer", + "path": "PolicySynthesizer", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "IntegTest": { + "id": "IntegTest", + "path": "IntegTest", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "IntegTest/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "IntegTest/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "IntegTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "IntegTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "IntegTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.ts new file mode 100644 index 0000000000000..aba7fb4bd223c --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.customize-roles-restapi.ts @@ -0,0 +1,42 @@ +import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam'; +import { App, Stack, RemovalPolicy } from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import { RestApi } from 'aws-cdk-lib/aws-apigateway'; + +const app = new App(); +const stack = new Stack(app, 'integ-customize-roles-restapi'); +Role.customizeRoles(stack, { + usePrecreatedRoles: { + 'integ-customize-roles-restapi/Role': 'precreated-role', + 'integ-customize-roles-restapi/RestApi/CloudWatchRole': + 'precreated-role-api', + }, +}); + +// test not throws an error with RestApi that calls `applyRemovalPolicy` internally +const api = new RestApi(stack, 'RestApi', { + cloudWatchRole: true, +}); +api.root.addMethod('GET'); + +// test not throws an error with explicitly calling `applyRemovalPolicy` +const role = new Role(stack, 'Role', { + assumedBy: new ServicePrincipal('sns.amazonaws.com'), +}); +role.applyRemovalPolicy(RemovalPolicy.DESTROY); + +/** + * This test will not deploy and is only used to provide an example + * of the synthesized iam policy report + */ +new IntegTest(app, 'IntegTest', { + testCases: [stack], + cdkCommandOptions: { + deploy: { + enabled: false, + }, + destroy: { + enabled: false, + }, + }, +}); diff --git a/packages/aws-cdk-lib/aws-iam/lib/role.ts b/packages/aws-cdk-lib/aws-iam/lib/role.ts index c72a569e1214f..37484fdf0f2ff 100644 --- a/packages/aws-cdk-lib/aws-iam/lib/role.ts +++ b/packages/aws-cdk-lib/aws-iam/lib/role.ts @@ -13,7 +13,7 @@ import { ImportedRole } from './private/imported-role'; import { MutatingPolicyDocumentAdapter } from './private/policydoc-adapter'; import { PrecreatedRole } from './private/precreated-role'; import { AttachedPolicies, UniqueStringSet } from './private/util'; -import { ArnFormat, Duration, Resource, Stack, Token, TokenComparison, Aspects, Annotations } from '../../core'; +import { ArnFormat, Duration, Resource, Stack, Token, TokenComparison, Aspects, Annotations, RemovalPolicy } from '../../core'; import { getCustomizeRolesConfig, getPrecreatedRoleConfig, CUSTOMIZE_ROLES_CONTEXT_KEY, CustomizeRoleConfig } from '../../core/lib/helpers-internal'; const MAX_INLINE_SIZE = 10000; @@ -631,6 +631,19 @@ export class Role extends Resource implements IRole { return this.immutableRole; } + /** + * Skip applyRemovalPolicy if role synthesis is prevented by customizeRoles. + * Because in this case, this construct does not have a CfnResource in the tree. + * @override + * @param policy RemovalPolicy + */ + public applyRemovalPolicy(policy: RemovalPolicy): void { + const config = this.getPrecreatedRoleConfig(); + if (!config.preventSynthesis) { + super.applyRemovalPolicy(policy); + } + } + private validateRole(): string[] { const errors = new Array(); errors.push(...this.assumeRolePolicy?.validateForResourcePolicy() ?? []); diff --git a/packages/aws-cdk-lib/aws-iam/test/role.test.ts b/packages/aws-cdk-lib/aws-iam/test/role.test.ts index 1733b256400c1..5cec09456f042 100644 --- a/packages/aws-cdk-lib/aws-iam/test/role.test.ts +++ b/packages/aws-cdk-lib/aws-iam/test/role.test.ts @@ -249,7 +249,7 @@ describe('customizeRoles', () => { // THEN expect(() => { role.applyRemovalPolicy(RemovalPolicy.DESTROY); - }).toThrow(/Cannot apply RemovalPolicy/); + }).not.toThrow(/Cannot apply RemovalPolicy/); expect(() => { new CfnResource(stack, 'MyResource2', { type: 'AWS::Custom',