Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(core): unable to create JSON secret using secret values #20461

Closed
rix0rrr opened this issue May 23, 2022 · 1 comment · Fixed by #21091
Closed

(core): unable to create JSON secret using secret values #20461

rix0rrr opened this issue May 23, 2022 · 1 comment · Fixed by #21091
Assignees
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p1

Comments

@rix0rrr
Copy link
Contributor

rix0rrr commented May 23, 2022

Describe the bug

See #20033 (comment)

With the new API, creating Secrets with compound values looks like this:

const secretValue = secretsmanager.Secret(this, 'Secret', {
  secretStringValue: cdk.SecretValue.unsafePlainText(JSON.stringify({
    username: user.userName,
    database: 'foo',
    password: accessKey.secretAccessKey.unsafeUnwrap(),
  }))
});

Which is not a great experience.

Expected Behavior

Can create JSON secret containing another secret.

Current Behavior

Not allowed.

Reproduction Steps

See linked example.

Possible Solution

I've considered making SecretValue contain compound values, but the API doesn't quite become satisfying:

SecretValue.jsonObject({
    username: 'admin',
    password: SecretValue.ssmSecure(...),
})

Is fine as far as it goes, but the problem with this API is it cannot be distinguished from this invocation:

SecretValue.jsonObject({
    username: 'admin',
    password: 'p4$$w0rd',   // UNSAFE
})

So it would need to be called unsafeJsonObject, but since when used properly this is the normal course for Secret usage, I don't want to call a method unsafe if it isn't actually unsafe.

It also seems that creating compound SecretValues is only used for initializing a Secret, and isn't used anywhere else. So, I'm thinking a better solution is to make it a feature of new sm.Secret:

const secretValue = secretsmanager.Secret(this, 'Secret', {
  secretObjectValue: {
    username: SecretValue.unsafePlainText(user.userName),
    database: SecretValue.unsafePlainText('foo'),
    password: accessKey.secretAccessKey,
  },
});

The type of secretObjectValue would be { [x: string]: SecretValue }

When writing a unit test for this, be sure to test with the @aws-cdk/core.preventUnsafePlaintextSecrets flag on, and update the documentation on SecretValue.unsafePlainText() to explain other reasonable use cases for static strings in here.

Additional Information/Context

No response

CDK CLI Version

Framework Version

No response

Node.js Version

OS

Language

Typescript

Language Version

No response

Other information

No response

@rix0rrr rix0rrr added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 23, 2022
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label May 23, 2022
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels May 23, 2022
@rix0rrr rix0rrr removed their assignment May 23, 2022
@rix0rrr rix0rrr added the good first issue Related to contributions. See CONTRIBUTING.md label Jun 20, 2022
@corymhall corymhall self-assigned this Jul 11, 2022
@mergify mergify bot closed this as completed in #21091 Jul 13, 2022
mergify bot pushed a commit that referenced this issue Jul 13, 2022
A common use case is to create key/value secrets where the values could
be either strings _or_ other secret values. Currently this is possible,
but the user experience is not great. This PR introduces a new input
prop `secretObjectValue` which is of type `{ [key: string]: SecretValue }`.

For example, you can now create a JSON secret:
```ts
new secretsmanager.Secret(stack, 'JSONSecret', {
  secretObjectValue: {
    username: SecretValue.unsafePlainText(user.userName), // intrinsic reference, not exposed as plaintext
    database: SecretValue.unsafePlainText('foo'), // rendered as plain text, but not a secret
    password: accessKey.secretAccessKey, // SecretValue
  },
});
```

I've also updated the docs to better reflect what `unsafe` means given
this new context.

fixes #20461


----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants