(aws-ssm): Generated ssm:GetParameters policy contains double forward slashes in resource ARN #26990
Labels
@aws-cdk/aws-ssm
Related to AWS Systems Manager
bug
This issue is a bug.
closed-for-staleness
This issue was automatically closed because it hadn't received any attention in a while.
response-requested
Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Describe the bug
A CodeBuild Project can take a prop
environmentVariables
, which is of typeBuildEnvironmentVariable
.BuildEnvironmentVariable
has a propvalue
, which can take SSM Parameter names. Per the docs:I'm trying to create a
StringParameter
in my stack with the name/foo/bar
. Let's call itfooBar
:And when I'm defining my CodeBuild Project prop's
environmentVariables
, I want to passfooBar.parameterName
(per the docs) into thevalue
field:I'm able to successfully build and deploy my app, but when I check the
ssm:GetParameters
policy attached to the project role, I see that the resource ARN contains double forward slashes:This causes my CodeBuild job to fail since the project role has been given permissions to the wrong resource:
I noticed that when the CDK serializes environment variables, it has logic that strips SSM parameter names of the leading slash if it contains one. However, because I'm passing in the
parameterName
property of a construct, the string value is a reference to the parameter resource name and not the actual name itself. One can see it in their generated CloudFormation template:Because the value is a reference and not the name itself (
/foo/bar
), the leading slash is not detected. This results in a malformed resource ARN at runtime, leading to the bad policy.Expected Behavior
The project role should have a policy attached that has the correct resource ARN for the string parameters without the double slash:
Current Behavior
It contains double slashes, resulting in a failed CodeBuild job:
Reproduction Steps
Create a
StringParameter
in your stack with aparameterName
that contains a forward slash:Create a CodeBuild
Project
that passes infooBar.parameterName
as thevalue
forBuildEnvironmentVariableType
when defining theProject
'senvironmentVariables
property:Possible Solution
The root cause is that when removing leading forward slashes from the parameter name, it's not taken into account that the value could be an unresolved reference to the parameter name and not the actual parameter name itself. This results in the actual parameter name never being stripped of its leading forward slash.
I'm not sure about a fix but a workaround is to pass the parameter name as a hardcoded string rather than using the
parameterName
property of aStringParameter
. While this is a straightforward workaround, I'd love to see a long-term fix since from an interface-perspective, it's awkward to not be able to reference theStringParameter
construct that I made that already encapsulates and exposes the parameter name. It's also inconsistent with the docs since it specifically says to use theparameterName
property as the value.Additional Information/Context
No response
CDK CLI Version
2.77.0
Framework Version
No response
Node.js Version
18.16.3
OS
Amazon Linux 2
Language
Typescript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: