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(redshift): Fix Redshift User Secret Multi-User Rotation #28853

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions packages/@aws-cdk/aws-redshift-alpha/lib/database-secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export interface DatabaseSecretProps {
* @default default master key
*/
readonly encryptionKey?: kms.IKey;

/**
* The master secret which will be used to rotate this secret.
*
* @default - no master secret information will be included
*/
readonly masterSecret?: secretsmanager.ISecret;
}

/**
Expand All @@ -30,10 +37,13 @@ export class DatabaseSecret extends secretsmanager.Secret {
encryptionKey: props.encryptionKey,
generateSecretString: {
passwordLength: 30, // Redshift password could be up to 64 characters
secretStringTemplate: JSON.stringify({ username: props.username }),
secretStringTemplate: JSON.stringify({
username: props.username,
masterarn: props.masterSecret?.secretArn,
}),
generateStringKey: 'password',
excludeCharacters: '"@/\\\ \'',
},
});
}
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-redshift-alpha/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class User extends UserBase {
const secret = new DatabaseSecret(this, 'Secret', {
username,
encryptionKey: props.encryptionKey,
masterSecret: props.adminUser,
});
const attachedSecret = secret.attach(props.cluster);
this.password = attachedSecret.secretValueFromJson('password');
Expand Down
32 changes: 31 additions & 1 deletion packages/@aws-cdk/aws-redshift-alpha/test/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ describe('cluster user', () => {
let cluster: redshift.ICluster;
const databaseName = 'databaseName';
let databaseOptions: redshift.DatabaseOptions;
let databaseOptionsWithAdminUser: redshift.DatabaseOptions;

beforeEach(() => {
stack = new cdk.Stack();
vpc = new ec2.Vpc(stack, 'VPC');
cluster = new redshift.Cluster(stack, 'Cluster', {
const clusterImpl = new redshift.Cluster(stack, 'Cluster', {
vpc: vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
Expand All @@ -25,10 +26,15 @@ describe('cluster user', () => {
},
publiclyAccessible: true,
});
cluster = clusterImpl;
databaseOptions = {
cluster,
databaseName,
};
databaseOptionsWithAdminUser = {
...databaseOptions,
adminUser: clusterImpl.secret,
};
});

it('creates using custom resource', () => {
Expand Down Expand Up @@ -62,6 +68,30 @@ describe('cluster user', () => {
});
});

it('creates database secret with admin user secret', () => {
const user = new redshift.User(stack, 'User', databaseOptionsWithAdminUser);

Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::Secret', {
GenerateSecretString: {
SecretStringTemplate: {
'Fn::Join': [
'',
[
`{"username":"${cdk.Names.uniqueId(user).toLowerCase()}","masterarn":"`,
{
Ref: 'ClusterSecretAttachment769E6258',
},
'"}',
],
],
},
},
});
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::SecretTargetAttachment', {
SecretId: { Ref: 'UserSecretE2C04A69' },
});
});

it('username property is pulled from custom resource', () => {
const user = new redshift.User(stack, 'User', databaseOptions);

Expand Down
Loading