aws-cdk: Aws cdk don't provide a way to create secrets with specified values
In the link below it’s specified in the docs that we can create secrets from a JSON file containing the values of the secrets.
aws secretsmanager create-secret --name MyTestDatabaseSecret \
--description "My test database secret created with the CLI" \
--secret-string file://mycreds.json
Unfortunately, I don’t find a way to do this with aws cdk. The create-secret method doesn’t accept values of the secrets themselves and code like below will autogenerate a secret.
new secretsmanager.Secret(this, 'Secret', {
description: 'secret description,
secretName: 'secretName'
});
Secrets manager docs from CLI: https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/create-secret.html#examples
AWS CDK secrets manager docs: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-secretsmanager.Secret.html
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 130
- Comments: 47 (20 by maintainers)
Commits related to this issue
- feat(secretsmanager): Specify secret value at creation Enables customers to supply their own secret value in the cases where an auto- generated value is not viable. This exposes the secret value in t... — committed to aws/aws-cdk by njlynch 4 years ago
- feat(secretsmanager): Specify secret value at creation (#9594) Enables customers to supply their own secret value in the cases where an auto- generated value is not viable. This exposes the secret v... — committed to aws/aws-cdk by njlynch 4 years ago
- feat(secretsmanager): create secrets with specified values Enables customers to supply their own secret value in the cases where an auto- generated value is not viable. The secret value is typed to h... — committed to aws/aws-cdk by njlynch 3 years ago
- feat(secretsmanager): create secrets with specified values (#18098) Enables customers to supply their own secret value in the cases where an auto- generated value is not viable. The secret value is ... — committed to aws/aws-cdk by njlynch 3 years ago
- feat(secretsmanager): create secrets with specified values (#18098) Enables customers to supply their own secret value in the cases where an auto- generated value is not viable. The secret value is ... — committed to TikiTDO/aws-cdk by njlynch 3 years ago
#9594 closed this issue, but then #9610 reverted it. I wanted to share the rationale on this issue for those that have been waiting for the functionality.
Supplying the secretString directly in the CDK exposes the secret in all of the CDK outputs (
cdk.outand outputs of commands likecdk synth), in the CloudFormation template itself (visible in the console and via the SDK/CLI), and increases the risk of a secret being committed to source. Even with appropriate warnings in the documentation, the risk of accidental leakage was deemed too high by the team.The preferred guidance is to use the AWS SDK/CLI/console to store these secrets, and then import them into you CDK app.
If you still want to achieve the same effect, you can always either use escape hatches or write your own construct.
@njlynch Can we re-open this? it’s a real pain that people are going to resort to less secure alternatives as workarounds. There’s no reason that I cannot specify
This would allow me to store a secret in SecretsManager and wouldn’t leave any confidential information in the template. We’re forcing users to use L1 resources and escape hatches because we think they might do something dumb, but we don’t try to stop them from spinning up 100 p4d.24xlarge instances.
Ah here we go. Another one of those problems fabricated by CDK. Lovely.
Just feed in the secrets via CloudFormation parameters with NoEcho option. Use a dummy value if you can’t feed in optional values during CICD process and change it afterwards. Otherwise feed it once on first deploy.
But hey, feel free to sit around and assume CDK users are idiots who’s going to hard code password into templates when using Secrets Manager. Better remove the environment variables in lambda/task definition while at it. You never know when people are going to hard code passwords in there.
@damian-keska-f @clafollett just use the L1 to create the secret and pass in the value. It works perfectly fine. You wasting your time screwing around with the L2 (as usual). https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-secretsmanager.CfnSecret.html
Thanks everyone for continuing to 👍 and comment on this issue with your use cases. When we reverted the initial implementation, we were worried about the potential for accidentally leaking secrets when specifying the initial string value, and wanted to intentionally add a little friction to make it more likely a user doing it knew the consequences.
Given the feedback on this issue, I’m going to re-open.
One possible compromise that still addresses the original concerns would be to re-enable passing in a
secretString, but require that the value was aToken. This means that creating a secret for an IAM user’s credentials (@bmVsc29u 's use case) or from the output of a custom resource (@richardhboyd 's example) would be supported, but just providing a hard-coded ‘p@ssw0rd’ would still require some degree of work-around.I would love to get feedback on if the above solves (most) use cases, or if there’s still a strong need to hard-code an insecure plaintext string password.
+1 too to create a SecretString in addition of the generateSecretString Props.
So, basically, what you’re suggesting is to spend few days on research and write like 500 lines of code to just store a setting securely. Really? I mean, you’re joking, right? Isn’t cdk supposed to automate things, to make deployment easier? How about you do it instead? Like, write a method that makes all those dirty hacks you mentioned- creates lambdas and whatnot. And just expose the method to the user!
Here is a no-brainer:
Why should an end user care about writing lambda functions if all they want is to store 1 value securely?!
And don’t even start talking about security. Let me tell you what an average user would do in case they can’t get it to work. Noone is going to spend days figuring out how to implement all those hacks you’ve mentioned. They’ll just hardcode a value in code. Yes, that’s right! And add a TODO comment with a link to this issue on github: “todo: store password in aws (if they ever implement support for it)”.
Man, I really wish devs would care about user feedback.
The “escape hatch” listed above no longer works.
results in
What does seem to work is to instead use
So the updated hack would be:
AWS folks - appreciate the friendly engagement on this issue, but I have to stress how frustrating this is to us. We’re trying to do the right thing and store data in secrets in a programmatic way, and the response from CDK feels like “lol nope.”
Just to copy what @njlynch said above, it’s very easy to accidentally leak your secrets when specifying the value, because the value will be present in the generated CloudFormation templates, outputs of commands, and others. I won’t rehash the discussion here, but the team does not plan on changing this in the future.
Of course, CDK always lets you go down to the L1 construct if you want to. Here’s an example stack for CDK v2:
This should produce:
@makemefeelgr8 First off, chill the hell out. I took the time out of my day to answer your question, and I don’t see why I that deserves some random person whining at me that it’s too much work. If you don’t like the answer then submit a PR. That’s what I do whenever there’s a feature missing in a framework I use. It’s far more effective than complaining.
Second, you can scroll up a few comments where I literally have a snippet of almost all the code you need to do this. It’s far from 500 lines, and it certainly doesn’t need “a few days of research.”
Third, you’re working on an cloud based infrastructure. Research is literally part of the job, as is coding. If you don’t like this then you might be in the wrong line of work.
Fourth, as someone else has already pointed out, there is a perfectly serviceable workaround if you’re fine with having secrets in your CF template. What more, this workaround was posted in the comment prior to yours, so you could have saved a lot of time if you did a few minutes of research.
Fifth, the reason that they made it hard to put secrets in the template is to avoid exactly the scenario you outlined.
Sixth, this repo literally has thousands of issues open, with PRs getting merged constantly. Clearly they care about user feedback enough to run a huge open source project to simplify a process that used to be much, much more painful than it is now. I understand being annoyed that things don’t get fixed as quickly as you’d like, but they are running a huge system used by countless organizations big and small. A single user complaining at the bottom of a nearly 2 year old issue probably isn’t going to be super high priority.
I for one appreciate the fact that the rest of us have at least some say in the process.
The decision just to revert the ability to provide a secret string on secret creation is quite unsatisfying.
First the decision is not documented. It took me a while to get to the reasoning here, after I read the CDK API documentation, the CDK code and searching the open and closed issues. I was short before creating a feature request.
Furthermore the documentation explicitly says to use the aws_secretsmanager to store secret strings in AWS:
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ssm-readme.html#creating-new-ssm-parameters-in-your-cdk-app
So currently there seems no viable solution to store a secret string in AWS with CDK.
Instead of reverting the feature, wouldn’t it be possible to implement another feature which let CDK hide sensitive content?
How come there is a way to create a secret, but no easy way to set it’s value? How do you expect one to even use this feature? Like, create a secret using CDK and then open AWS website to fill in the value? Really? Don’t you guys have APIs?
Let me show you how your competitors do it: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-key-vault?view=azure-devops
Just check this out!
az keyvault secret set --vault-name 'ContosoKeyVault' --name 'SQLPassword' --value 'Pa$$w0rd'Now that’s a solid reason to choose Azure over AWS.
I know it is probably not a best solution, but working one.
Thanks again for all the feedback and 👍s. As I’ve received no counter-points or complaints, I think we can proceed with the above approach of allowing Tokened plaintext secrets.
I can’t provide a concrete ETA for an implementation. However, I’d be happy to work with a member of the community if someone wants to contribute a solution. #9594 could be used as the basis for the implementation, with a few minor updates to introduce the restriction of Tokenized input and some additional docs + tests.
we need this for the scenario:
accessKeyper usersecrets managerhence, either cloudformation provide a way to inject secrets directly to
secrets manager, or allowsecrets managerto storeaccess secret keyviaAccessKey’s property would be accepted. But I am worry about the secrets may leave some foot prints through thesecretStringway.@makemefeelgr8 Make a secret. Make a lambda. Give it a role with write access to the secret. Pass the secret ARN as an ENV variable. Start the lambda to populate the secret.
Also, your example comparing apples and oranges. CDK is not the CLI interface. It’s a template generator.
If you want to use the AWS CLI like in your example, the command is
aws secretmanager put-secret-value, you can get the docs here.Why are you so confrontational?
If you don’t care about leaking secret values in Cfn template, there is a workaround available: https://github.com/aws/aws-cdk/issues/5810#issuecomment-914054341
Much appreciated. I’ve just run into this issue myself in spinning up new S3 Buckets, Users, Policies and IAM CfnAccessKey. I was hoping to create the Keys and then store the generated secrets in SecretManager so we could grab them from the Console to put into our 3rd Party vendors system for bucket integration. I would definitely be interested in seeing a solution for this. Here is an example usage:
Hi! After discussing this further internally we’ve decided to go ahead and provide an
Tokenized implementation as discussed above. Note that there are still some potential sharp edges here and you should always use care when working with secret values, but pull #18098 should cover many use cases.Everyone take a guess on what people will do when they cannot save a pre-existing secret in AWS Secret Manager: … … … My answer: they will save it in AWS System Manager Parameter Store
Using ECS with a private repository is another example when this would be useful. The
ContainerImage.fromRegistrymethod requires a Secret instance and this makes it hard to provide an authentication token.For anyone stumbling across this and using the Python version of the AWS CDK: you need to use the
CfnSecretClass instead of theSecretClass if you want to do it the “insecure” way. There you will find asecret_stringproperty https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.aws_secretsmanager/CfnSecret.htmlFor example in this code, I have a file called
secrets_config.jsonthat holds the secret values and read the values from there to store them in Secrets Manager. The file isn’t checked into Git (it’s in .gitignore) and users of the repository are instructed to create the file before executingcdk deploy@njlynch Man, you’re my hero! The bug was hanging there for 2 years and you’ve just fixed it. Thanks!
@Dzhuneyt There’s no need to hard-code anything in your code base though. My approach is when I need to update a secret I can create a temporary file and store it in a location outside of the code repo. When the deployer detects that this file is present it is first GPG encrypted using a SecretManger password. That encrypted file is uploaded as an asset, and once it’s stored in the target bucket it is decrypted, parsed, and written to another SecretManager value using an S3 onCreate handler. It’s a huge pain to maintain all this, but it’s certainly doable and reasonably secure.
This has the secondary benefit that you can trivially let people upload new secrets, without giving them any additional dashboard access which you then need to manage and audit. Unfortunately all this involves a lot of song and dance which an official solution could easily hide away.
Libraries like react use abnormally long property names for properties that are insecure, like dangerouslySetInnerHTML. Maybe it would be a good idea to do something similar, if it’s that dangerous.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
I’m not that good at English, sorry about that. Somehow, it sounded perfectly normal in my head.
I totally would, if it was a free open source project without a multi-billion corporation behind it. AWS is neither free, nor cheap. People are paying them a ton of money every day. So, how about they use some of those funds to actually hire developers and fix those “thousands of issues”? As you’ve mentioned, this one is almost 2 year old, and it have not even been addressed. So, this made me kinda disappointed. At the very least, they could have just closed it, with a comment like “will not implement”.
Would passing a stack parameter value into a secret be considered secure? I guess that way it will also never get coded into the template or anywhere in cdk.out. Would this also work with the approach described by @njlynch ?
I have the same scenario and was frustrated by this in the beginning, but on a second thought, it is much more secure to generate your GitHub Personal Access Key manually and create the SSM manually and hardcode a reference to it inside your codebase, instead of going the “tempting” and “easy” way of hardcoding the token itself in your codebase. It’s like putting your .env files into source control, but much more dangerous. Don’t do it. Yes, your infrastructure is ephemeral nowadays, using tools like CDK or Terraform, but your personal access keys are not. Replacing them is difficult, once compromised, and reverting damages is even more difficult. Would you hardcode your password inside the source code? Probably not.
On the other hand though, I’m sure there are valid use cases where you might want to use SSM and provide their values in plain text. Something outside the scope of GitHub Access Tokens.
E.g IPs that point to other parts of the infrastructure, API keys, password salts (not actual passwords), etc.