aws-cdk: core: permissions boundary not being applied to custom resource role
Describe the bug
I’m deploying a stack through CDK pipelines and have a permissions boundary configured within cdk.json. Every role is being configured app wide with the permissions boundary apart from one which seems to be created by CDK itself for my AwsCustomResource.
Expected Behavior
I expect the permissions boundary to be applied the CDK application app wide and not miss any roles deployed by the CDK application
Current Behavior
The CDK created role is failing to add the permissions boundary to the application.
Reproduction Steps
CDK pipelines is being deployed using bootstrapped roles with a custom qualifier and where the permission boundary is required.
synth_object = cdk.DefaultStackSynthesizer(
qualifier=config["cdk_synth_qualifier"],
)
AwsCustomResource
response = AwsCustomResource(
self,
"describe-enis",
on_update={
"service": "EC2",
"action": "describeNetworkInterfaces",
"output_paths": output_paths,
"parameters": {"NetworkInterfaceIds": eni_ids},
"physical_resource_id": PhysicalResourceId.of(str(random.random())),
},
policy=AwsCustomResourcePolicy.from_statements(
statements=[
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=["ec2:DescribeNetworkInterfaces"],
resources=["*"],
),
],
),
)
The role in question not being provided the permission boundary AWSCDKCfnUtilsProviderCustomResourceProviderRoleFE0EE867
"AWSCDKCfnUtilsProviderCustomResourceProviderRoleFE0EE867": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
},
"ManagedPolicyArns": [
{
"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
]
},
The cdk.json config
"@aws-cdk/core:permissionsBoundary": {
"name": "cdk-permissions-boundary-policy"
}
Possible Solution
No response
Additional Information/Context
I have tried adding the permission boundary to the stack itself, as well as the custom resource itself following the documentation here: https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_iam/README.html#permissions-boundaries
I have also tried creating a custom aspect to add the stack and the stage but none of these worked either: https://github.com/aws/aws-cdk/issues/3242#issuecomment-561064190
CDK CLI Version
2.141.0
Framework Version
No response
Node.js Version
v22.1.0
OS
Sonoma 14.2.1
Language
Python
Language Version
3.12.3
Other information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 6
- Comments: 21 (4 by maintainers)
I found some Typescript CDK code which reproduces the same bug if that helps:
Or this ghost-role (AWSCDKCfnUtilsProviderCustomResourceProviderRoleFE0EE867) is not amendable…
Error:
Stack:
_Originally posted by @nocquidant in https://github.com/aws/aws-cdk/issues/22972#issuecomment-2066024383_
Had a back and forth with AWS premium support and was recommended the following solution, which worked for us (thanks Greg!).
The solution for us was to use
iam.Role.customize_roles(https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_iam/Role.html#aws_cdk.aws_iam.Role.customize_roles) which allowed us to specify custom roles for the roles created by CDK. The only drawback here is that all roles in the stack itself will be replaced by whatever is specified incustomize_roles. Any role not specified incustomize_roleswill not be found in your CloudFormation template on synth and will display an error.First thing to do is synth your application as normal and take a note of the path of all the roles in the stack. You can find the path under
"Metadata": { "aws:cdk:path": "CdkPipelines/ToolingDeployment/tooling-ApiGateway/AWSCDKCfnUtilsProviderCustomResourceProvider/Role" }.Then at the top of the stack place your
customize_roleswith the specified paths and your own custom created role. Your own roles have to be created outside of said stack.It’s important to set
prevent_synthesisasTrueor else the CloudFormation template will contain the CDK created roles. It will however error out if a certain role is no addressed incustomize_roles.Hope this helps!
I’ve managed to isolate where exactly the role is being created.
AWSCDKCfnUtilsProviderCustomResourceProviderRoleFE0EE867and it’s corresponding LambdaAWSCDKCfnUtilsProviderCustomResourceProviderHandlerCF82AA57are created when you reference a certain construct value withinAwsCustomResource.From my example this is when I reference
vpc_endpoint_network_interface_idsin the below example.If I replace the
parametersmodule in the AwsSdkCall with a random list of strings and synth the roleAWSCDKCfnUtilsProviderCustomResourceProviderRoleFE0EE867and LambdaAWSCDKCfnUtilsProviderCustomResourceProviderHandlerCF82AA57disappear.I tried looking around for what this Lambda does but I can’t find anything. It’s completely without context of the CDK app hence why adding a permission boundary with an custom aspect or an escape hatch won’t work.
I’m not sure of a work around since the only way to make the custom resource work is to add interface ID’s manually which is not really the point of my use case.
Hi Khurana,
I have added you to my repo so you can reproduce the bug. Please let me know how it goes.
Cheers,
David
On Wed, May 29, 2024 at 4:39 AM Shailja Khurana @.***> wrote: