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

feat(docdb): add the ability to exclude characters when generating passwords #17262

Merged
merged 4 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-docdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ your instances will be launched privately or publicly:
const cluster = new DatabaseCluster(this, 'Database', {
masterUser: {
username: 'myuser' // NOTE: 'admin' is reserved by DocumentDB
excludeCharacters: '\"@/:', // optional, defaults to the set "\"@/"
},
instanceType: ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
vpcSubnets: {
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-docdb/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
secret = new DatabaseSecret(this, 'Secret', {
username: props.masterUser.username,
encryptionKey: props.masterUser.kmsKey,
excludeCharacters: props.masterUser.excludeCharacters,
});
}

Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-docdb/lib/database-secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export interface DatabaseSecretProps {
* @default - no master secret information will be included
*/
readonly masterSecret?: ISecret;

/**
* Characters to not include in the generated password.
*
* @default "\"@/"
*/
readonly excludeCharacters?: string;
}

/**
Expand Down Expand Up @@ -61,7 +68,7 @@ export class DatabaseSecret extends Secret {
masterarn: props.masterSecret?.secretArn,
}),
generateStringKey: 'password',
excludeCharacters: '"@/',
excludeCharacters: props.excludeCharacters ?? '"@/',
},
});
}
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-docdb/lib/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export interface Login {
* @default default master key
*/
readonly kmsKey?: kms.IKey;

/**
* Specifies characters to not include in generated passwords.
*
* @default "\"@/"
*/
readonly excludeCharacters?: string;
}

/**
Expand Down
25 changes: 24 additions & 1 deletion packages/@aws-cdk/aws-docdb/test/cluster.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect as expectCDK, haveResource, ResourcePart, arrayWith } from '@aws-cdk/assert-internal';
import { expect as expectCDK, haveResource, ResourcePart, arrayWith, haveResourceLike, objectLike } from '@aws-cdk/assert-internal';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as kms from '@aws-cdk/aws-kms';
import * as cdk from '@aws-cdk/core';
Expand Down Expand Up @@ -293,6 +293,29 @@ describe('DatabaseCluster', () => {
}));
});

test('creates a secret with excludeCharacters', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');

// WHEN
new DatabaseCluster(stack, 'Database', {
masterUser: {
username: 'admin',
excludeCharacters: '"@/()[]',
},
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
});

// THEN
expectCDK(stack).to(haveResourceLike('AWS::SecretsManager::Secret', {
GenerateSecretString: objectLike({
ExcludeCharacters: '\"@/()[]',
}),
}));
});

test('create an encrypted cluster with custom KMS key', () => {
// GIVEN
const stack = testStack();
Expand Down