Skip to content

Commit 4164737

Browse files
authored
Merge branch 'master' into fix-drain-hook-lambda
2 parents 9ac0686 + 62a91b7 commit 4164737

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed

packages/@aws-cdk/aws-rds/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ It's also possible to create user credentials together with the instance/cluster
247247
```ts
248248
const myUserSecret = new rds.DatabaseSecret(this, 'MyUserSecret', {
249249
username: 'myuser',
250+
secretName: 'my-user-secret', // optional, defaults to a CloudFormation-generated name
250251
masterSecret: instance.secret,
251252
excludeCharacters: '{}[]()\'"/\\', // defaults to the set " %+~`#$&*()|[]{}:;<>?!'/@\"\\"
252253
});

packages/@aws-cdk/aws-rds/lib/database-secret.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export interface DatabaseSecretProps {
1414
*/
1515
readonly username: string;
1616

17+
/**
18+
* A name for the secret.
19+
*
20+
* @default - A name is generated by CloudFormation.
21+
*/
22+
readonly secretName?: string;
23+
1724
/**
1825
* The KMS key to use to encrypt the secret.
1926
*
@@ -60,6 +67,7 @@ export class DatabaseSecret extends secretsmanager.Secret {
6067
super(scope, id, {
6168
encryptionKey: props.encryptionKey,
6269
description: `Generated by the CDK for stack: ${Aws.STACK_NAME}`,
70+
secretName: props.secretName,
6371
generateSecretString: {
6472
passwordLength: 30, // Oracle password cannot have more than 30 characters
6573
secretStringTemplate: JSON.stringify({

packages/@aws-cdk/aws-rds/lib/private/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function renderCredentials(scope: Construct, engine: IEngine, credentials
9494
renderedCredentials = Credentials.fromSecret(
9595
new DatabaseSecret(scope, 'Secret', {
9696
username: renderedCredentials.username,
97+
secretName: renderedCredentials.secretName,
9798
encryptionKey: renderedCredentials.encryptionKey,
9899
excludeCharacters: renderedCredentials.excludeCharacters,
99100
// if username must be referenced as a string we can safely replace the
@@ -131,4 +132,4 @@ export function helperRemovalPolicy(basePolicy?: RemovalPolicy): RemovalPolicy {
131132
*/
132133
export function renderUnless<A>(value: A, suppressValue: A): A | undefined {
133134
return value === suppressValue ? undefined : value;
134-
}
135+
}

packages/@aws-cdk/aws-rds/lib/props.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ export interface BackupProps {
126126
* Base options for creating Credentials.
127127
*/
128128
export interface CredentialsBaseOptions {
129+
/**
130+
* The name of the secret.
131+
*
132+
* @default - A name is generated by CloudFormation.
133+
*/
134+
readonly secretName?: string;
135+
129136
/**
130137
* KMS encryption key to encrypt the generated secret.
131138
*
@@ -232,6 +239,14 @@ export abstract class Credentials {
232239
*/
233240
public abstract readonly username: string;
234241

242+
/**
243+
* The name to use for the Secret if a new Secret is to be generated in
244+
* SecretsManager for these Credentials.
245+
*
246+
* @default - A name is generated by CloudFormation.
247+
*/
248+
public abstract readonly secretName?: string;
249+
235250
/**
236251
* Whether the username should be referenced as a string and not as a dynamic
237252
* reference to the username in the secret.

packages/@aws-cdk/aws-rds/test/cluster.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as cxapi from '@aws-cdk/cx-api';
1010
import { testFutureBehavior } from 'cdk-build-tools/lib/feature-flag';
1111
import {
1212
AuroraEngineVersion, AuroraMysqlEngineVersion, AuroraPostgresEngineVersion, CfnDBCluster, Credentials, DatabaseCluster,
13-
DatabaseClusterEngine, DatabaseClusterFromSnapshot, ParameterGroup, PerformanceInsightRetention, SubnetGroup,
13+
DatabaseClusterEngine, DatabaseClusterFromSnapshot, ParameterGroup, PerformanceInsightRetention, SubnetGroup, DatabaseSecret,
1414
} from '../lib';
1515

1616
describe('cluster', () => {
@@ -1763,6 +1763,52 @@ describe('cluster', () => {
17631763

17641764
});
17651765

1766+
test('can set custom name to database secret by fromSecret', () => {
1767+
// GIVEN
1768+
const stack = testStack();
1769+
const vpc = new ec2.Vpc(stack, 'VPC');
1770+
const secretName = 'custom-secret-name';
1771+
const secret = new DatabaseSecret(stack, 'Secret', {
1772+
username: 'admin',
1773+
secretName,
1774+
} );
1775+
1776+
// WHEN
1777+
new DatabaseCluster(stack, 'Database', {
1778+
engine: DatabaseClusterEngine.aurora({ version: AuroraEngineVersion.VER_1_22_2 }),
1779+
credentials: Credentials.fromSecret(secret),
1780+
instanceProps: {
1781+
vpc,
1782+
},
1783+
});
1784+
1785+
// THEN
1786+
expect(stack).toHaveResourceLike('AWS::SecretsManager::Secret', {
1787+
Name: secretName,
1788+
});
1789+
});
1790+
1791+
test('can set custom name to database secret by fromGeneratedSecret', () => {
1792+
// GIVEN
1793+
const stack = testStack();
1794+
const vpc = new ec2.Vpc(stack, 'VPC');
1795+
const secretName = 'custom-secret-name';
1796+
1797+
// WHEN
1798+
new DatabaseCluster(stack, 'Database', {
1799+
engine: DatabaseClusterEngine.aurora({ version: AuroraEngineVersion.VER_1_22_2 }),
1800+
credentials: Credentials.fromGeneratedSecret('admin', { secretName }),
1801+
instanceProps: {
1802+
vpc,
1803+
},
1804+
});
1805+
1806+
// THEN
1807+
expect(stack).toHaveResourceLike('AWS::SecretsManager::Secret', {
1808+
Name: secretName,
1809+
});
1810+
});
1811+
17661812
test('can set public accessibility for database cluster with instances in private subnet', () => {
17671813
// GIVEN
17681814
const stack = testStack();

packages/@aws-cdk/aws-rds/test/instance.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,44 @@ describe('instance', () => {
12041204
MasterUsername: 'postgres', // username is a string
12051205
MasterUserPassword: '{{resolve:ssm-secure:/dbPassword:1}}', // reference to SSM
12061206
});
1207+
});
12071208

1209+
test('can set custom name to database secret by fromSecret', () => {
1210+
// WHEN
1211+
const secretName = 'custom-secret-name';
1212+
const secret = new rds.DatabaseSecret(stack, 'Secret', {
1213+
username: 'admin',
1214+
secretName,
1215+
} );
1216+
new rds.DatabaseInstance(stack, 'Instance', {
1217+
engine: rds.DatabaseInstanceEngine.mysql({
1218+
version: rds.MysqlEngineVersion.VER_8_0_19,
1219+
}),
1220+
credentials: rds.Credentials.fromSecret(secret),
1221+
vpc,
1222+
});
12081223

1224+
// THEN
1225+
expect(stack).toHaveResourceLike('AWS::SecretsManager::Secret', {
1226+
Name: secretName,
1227+
});
1228+
});
1229+
1230+
test('can set custom name to database secret by fromGeneratedSecret', () => {
1231+
// WHEN
1232+
const secretName = 'custom-secret-name';
1233+
new rds.DatabaseInstance(stack, 'Instance', {
1234+
engine: rds.DatabaseInstanceEngine.mysql({
1235+
version: rds.MysqlEngineVersion.VER_8_0_19,
1236+
}),
1237+
credentials: rds.Credentials.fromGeneratedSecret('admin', { secretName }),
1238+
vpc,
1239+
});
1240+
1241+
// THEN
1242+
expect(stack).toHaveResourceLike('AWS::SecretsManager::Secret', {
1243+
Name: secretName,
1244+
});
12091245
});
12101246

12111247
test('can set publiclyAccessible to false with public subnets', () => {
@@ -1274,4 +1310,4 @@ test.each([
12741310
DeletionPolicy: subnetValue,
12751311
UpdateReplacePolicy: subnetValue,
12761312
}, ResourcePart.CompleteDefinition);
1277-
});
1313+
});

0 commit comments

Comments
 (0)