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

fix(secretsmanager): allow templated string creation #2010

Merged
merged 7 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-secretsmanager/lib/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface SecretProps {
* @default 32 characters with upper-case letters, lower-case letters, punctuation and numbers (at least one from each
* category), per the default values of ``SecretStringGenerator``.
*/
generateSecretString?: SecretStringGenerator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why rid of the semicolon?

generateSecretString?: SecretStringGenerator | TemplatedSecretStringGenerator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unions do not translate very nicely across programming languages since some languages don't support them, so we forbid unions at the L2 layer. The solution is usually to define an abstract class with a bunch of static factory methods for the various options.

Copy link
Contributor Author

@jogold jogold Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An easy solution here could be to integrate generateStringKey and secretStringTemplate in the SecretStringGenerator interface (which is a prop of SecretProps) and add validation (if one is specified the other must also be specified), what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But... TemplatedSecretStringGenerator extends SecretStringGenerator, so you are able to pass an instance of that in and it'll be used just as you'd expect... The signature already enables that. I'll admit that discoverability isn't all so great, but I don't see why altering the signature is useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work:

new secretsmanager.Secret(this, 'Secret', {
      generateSecretString: {
        secretStringTemplate: JSON.stringify({ username: 'username' }),
        generateStringKey: 'password',
        excludeCharacters: '"@/\\'
      }
    })
  Types of property 'generateSecretString' are incompatible.
    Type '{ passwordLength: number; secretStringTemplate: string; generateStringKey: string; excludeCharacters: string; }' is not assignable to type 'SecretStringGenerator'.
      Object literal may only specify known properties, and 'secretStringTemplate' does not exist in type 'SecretStringGenerator'.

You mean like this?

new secretsmanager.Secret(this, 'Resource', {
      generateSecretString: ({
        secretStringTemplate: JSON.stringify({ username: 'username' }),
        generateStringKey: 'password',
        excludeCharacters: '"@/\\'
      }) as secretsmanager.TemplatedSecretStringGenerator
    });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomainMuller it's not a comfortable interface for value-typed interfaces. TSC is not going to search the whole type space for a type that matches your literal object AND happens to be assignable to a SecretStringGenerator.

It's actually good that it doesn't because I'm not sure the semantics would be preserved when using it via JSII.

We can either make 2 arguments at the top-level:

secretStringGenerator?: SecretStringGenerator;
templatedSecretStringGenerator?: TemplatedSecretStringGenerator;

OR

Fold the 2 structures together and do some run-time analysis

OR

Define an interface and 2 classes and some factory functions.

Preferences?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If nobody expresses a preference by Monday I'm going to pick something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fold the 2 structures together and do some run-time analysis

I have a preference for this solution...


/**
* A name for the secret. Note that deleting secrets from SecretsManager does not happen immediately, but after a 7 to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@
}
}
}
},
"TemplatedSecret3D98B577": {
"Type": "AWS::SecretsManager::Secret",
"Properties": {
"GenerateSecretString": {
"GenerateStringKey": "password",
"SecretStringTemplate": "{\"username\":\"user\"}"
}
}
},
"OtherUser6093621C": {
"Type": "AWS::IAM::User",
"Properties": {
"LoginProfile": {
"Password": {
"Fn::Join": [
"",
[
"{{resolve:secretsmanager:",
{
"Ref": "TemplatedSecret3D98B577"
},
":SecretString:password::}}"
]
]
}
},
"UserName": {
"Fn::Join": [
"",
[
"{{resolve:secretsmanager:",
{
"Ref": "TemplatedSecret3D98B577"
},
":SecretString:username::}}"
]
]
}
}
}
}
}
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-secretsmanager/test/integ.secret.lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ class SecretsManagerStack extends cdk.Stack {
const role = new iam.Role(this, 'TestRole', { assumedBy: new iam.AccountRootPrincipal() });

/// !show
// Default secret
const secret = new secretsManager.Secret(this, 'Secret');
secret.grantRead(role);

new iam.User(this, 'User', {
password: secret.stringValue
});

// Templated secret
const templatedSecret = new secretsManager.Secret(this, 'TemplatedSecret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({ username: 'user' }),
generateStringKey: 'password'
}
});

new iam.User(this, 'OtherUser', {
userName: templatedSecret.jsonFieldValue('username'),
password: templatedSecret.jsonFieldValue('password')
});
/// !hide
}
}
Expand Down
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-secretsmanager/test/test.secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ export = {
test.done();
},

'templated secret string'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new secretsmanager.Secret(stack, 'Secret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({ username: 'username' }),
generateStringKey: 'password'
}
});

// THEN
expect(stack).to(haveResource('AWS::SecretsManager::Secret', {
GenerateSecretString: {
SecretStringTemplate: '{"username":"username"}',
GenerateStringKey: 'password'
}
}));

test.done();
},

'grantRead'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down