aws-cdk: aws_secretsmanager: fromSecretNameV2 doesnt work
What is the problem?
Given this code:
my_secret = secretsmanager.Secret.from_secret_name_v2(
self, "my_secret",
secret_name="hello"
)
my_secret.grant_read(s3_event_handler)
the resulting iam policy has a bunch of question marks in it:
- Action:
- secretsmanager:GetSecretValue
- secretsmanager:DescribeSecret
Effect: Allow
Resource:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :secretsmanager:eu-west-1:123123123123:secret:hello-??????
which is invalid
Reproduction Steps
see above
What did you expect to happen?
valid iam policy
What actually happened?
invalid iam policy
CDK CLI Version
2.8.0 (build 8a5eb49)
Framework Version
2.8.0
Node.js Version
v14.18.3
OS
Ubuntu 20.04.3 LTS
Language
Python
Language Version
No response
Other information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 30 (7 by maintainers)
This regression broke all of our new deployments - no change on our end, and existing deployments work fine (we’re using CDK to deploy into ECS Fargate). But anything new fails.
Similar issue here. The problem occurs when I want to add a secret to the environemnt via ContainerDefinitionOptions.
See: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ContainerDefinitionOptions.html#secrets
The task fails to start and points to missing permission for the partial secret arn:
ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secret from asm: service call has been retried 1 time(s): failed to fetch secret $PARTIAL_ARN_HERE from secrets manager: AccessDeniedException: User: $USER is not authorized to perform: secretsmanager:GetSecretValue on resource: $PARTIAL_ARN_HERE because no identity-based policy allows the secretsmanager:GetSecretValue action status code: 400The policy is in place and contains the correct resource arn. Because the message is pointing to the arn without suffix, I assume this might be related.
Please note according to this document, it is strongly not recommended to name your secret with a hyphen followed by 6 characters such as
foo-abcdef.Now let’s dive deep into this sample in CDK
And my lambda code in python
I am creating a secret named
foo-abcdefwith its dummy valueBAR, which is not recommended in the doc above. Let’s see the partial and full ARNs:As you can see, the partial ARN now ends with
foo-abcdefwhich is NOT supposed to be used in CLI/SDK.OK, if we pass this confusing ARN an env var to lambda and let the SDK get secret value in Lambda. You get
AccessDeniedExceptionerror:The
AccessDeniedExceptionis not due to lambda role not having sufficient permission to the secret, but the SDK is getting incorrect secret.Conslusion
fromSecretCompleteArn()method to other services or CLI/SDK whenever possible. You may pass partial ARN only when your secrets do not end with a hyphen and 6 characters. But it is a risk you definitely should avoid.-??????will be appended)resources=["*"]for security reason.If this function should not be used it would be best to deprecate and delete it.
This really shouldn’t be closed. I’m encountering the same error. Trying to import using fromSecretNameV2 result in an incomplete ARN.
I am currently facing the exact same issue. const secret = sm.Secret.fromSecretNameV2(this, “db-credentials”, secretName); resources: [secret.secretArn],
secretArn does not return fullArn. It is missing the last random suffix. Is this fixed or is there a better way to get the Arn?
Thanks Sanjay
I am also having this problem now for some reason. This used to work.
@mkesper We’ll bring it up to the core team maintainers for discussion and see what is the best for the customers.
Because the secrets always have the suffix, secret from name V2 does sufficient permissions but you must loopup the actual ARN in your function or whatever.
This should be reopened. Just encountered this on Decemeber, 2022. Workaround is using
fromSecretCompleteArn()I just discovered that importing a secret with
from_secret_name_v2does NOT check that the secret exists and allows both the synth operation and deployment, even though it is used forgrant_readoperations, etc.My current use case is that I need to give a Lambda execution role the ability to get a secret value using the SDK. For me, both “secretArn” and “secretFullArn” does not return the full ARN with the secret-manager suffix from the “fromSecretNameV2” function. The full ARN with the suffix is needed in the execution role for the Lambda to access the secret value.
I’m not sure if this is what was inferred from the comments above, but my current workaround for this is to append the “???” wildcard onto the “secretArn” value that is returned from “fromSecretNameV2”, like so:
inlinePolicies: { accessStackSecrets: new iam.PolicyDocument({ statements: [new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ "secretsmanager:GetSecretValue", ], resources: [${stackSecret.secretArn}-???] })], }) }It seems a bit strange that “secretFullArn” is an option, but returns nothing despite the ARN containing the secret-manager suffix.