-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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(secretsmanager/rds): support credential rotation #2052
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0a39df7
feat(secretsmanager): add construct for RDS rotation single user
jogold 1f1742b
Use a IConnectable for rds rotation
jogold bce6128
Replace SecretTargetAttachment by AttachedSecret
jogold 81f2c5b
Move rds rotation to aws-rds
jogold 6554f2f
Implement ISecretAttachmentTarget for cluster
jogold 2a99fb4
Update secretsmanager README
jogold 87cf249
Add secret and rotation to cluster
jogold d16eaa2
Add integration tests for cluster rotation
jogold e381a81
Update README for aws-rds
jogold e2b9f13
Typo
jogold f014a07
Fix dependencies
jogold 9aa5fe7
Rename test
jogold f4b4403
Remove default username in DatabaseSecret
jogold 304c1d5
Fix typos
jogold ea1a32e
Merge branch 'master' into secret-rotation
jogold df59fca
Update README for aws-rds
jogold fdb3236
Move literal example to new line
rix0rrr 312d551
Merge branch 'master' into secret-rotation
jogold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import ec2 = require('@aws-cdk/aws-ec2'); | ||
import secretsmanager = require('@aws-cdk/aws-secretsmanager'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { IClusterParameterGroup } from './cluster-parameter-group'; | ||
import { DatabaseClusterImportProps, Endpoint, IDatabaseCluster } from './cluster-ref'; | ||
import { DatabaseSecret } from './database-secret'; | ||
import { BackupProps, DatabaseClusterEngine, InstanceProps, Login } from './props'; | ||
import { CfnDBCluster, CfnDBInstance, CfnDBSubnetGroup } from './rds.generated'; | ||
import { DatabaseEngine, RotationSingleUser, RotationSingleUserOptions } from './rotation-single-user'; | ||
|
||
/** | ||
* Properties for a new database cluster | ||
|
@@ -91,16 +94,67 @@ export interface DatabaseClusterProps { | |
} | ||
|
||
/** | ||
* Create a clustered database with a given number of instances. | ||
* A new or imported clustered database. | ||
*/ | ||
export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster { | ||
export abstract class DatabaseClusterBase extends cdk.Construct implements IDatabaseCluster { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
/** | ||
* Import an existing DatabaseCluster from properties | ||
*/ | ||
public static import(scope: cdk.Construct, id: string, props: DatabaseClusterImportProps): IDatabaseCluster { | ||
return new ImportedDatabaseCluster(scope, id, props); | ||
} | ||
|
||
/** | ||
* Identifier of the cluster | ||
*/ | ||
public abstract readonly clusterIdentifier: string; | ||
/** | ||
* Identifiers of the replicas | ||
*/ | ||
public abstract readonly instanceIdentifiers: string[]; | ||
|
||
/** | ||
* The endpoint to use for read/write operations | ||
*/ | ||
public abstract readonly clusterEndpoint: Endpoint; | ||
|
||
/** | ||
* Endpoint to use for load-balanced read-only operations. | ||
*/ | ||
public abstract readonly readerEndpoint: Endpoint; | ||
|
||
/** | ||
* Endpoints which address each individual replica. | ||
*/ | ||
public abstract readonly instanceEndpoints: Endpoint[]; | ||
|
||
/** | ||
* Access to the network connections | ||
*/ | ||
public abstract readonly connections: ec2.Connections; | ||
|
||
/** | ||
* Security group identifier of this database | ||
*/ | ||
public abstract readonly securityGroupId: string; | ||
|
||
public abstract export(): DatabaseClusterImportProps; | ||
|
||
/** | ||
* Renders the secret attachment target specifications. | ||
*/ | ||
public asSecretAttachmentTarget(): secretsmanager.SecretAttachmentTargetProps { | ||
return { | ||
targetId: this.clusterIdentifier, | ||
targetType: secretsmanager.AttachmentTargetType.Cluster | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Create a clustered database with a given number of instances. | ||
*/ | ||
export class DatabaseCluster extends DatabaseClusterBase implements IDatabaseCluster { | ||
/** | ||
* Identifier of the cluster | ||
*/ | ||
|
@@ -136,10 +190,33 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster { | |
*/ | ||
public readonly securityGroupId: string; | ||
|
||
/** | ||
* The secret attached to this cluster | ||
*/ | ||
public readonly secret?: secretsmanager.ISecret; | ||
|
||
/** | ||
* The database engine of this cluster | ||
*/ | ||
public readonly engine: DatabaseClusterEngine; | ||
|
||
/** | ||
* The VPC where the DB subnet group is created. | ||
*/ | ||
public readonly vpc: ec2.IVpcNetwork; | ||
|
||
/** | ||
* The subnets used by the DB subnet group. | ||
*/ | ||
public readonly vpcPlacement?: ec2.VpcPlacementStrategy; | ||
|
||
constructor(scope: cdk.Construct, id: string, props: DatabaseClusterProps) { | ||
super(scope, id); | ||
|
||
const subnets = props.instanceProps.vpc.subnets(props.instanceProps.vpcPlacement); | ||
this.vpc = props.instanceProps.vpc; | ||
this.vpcPlacement = props.instanceProps.vpcPlacement; | ||
|
||
const subnets = this.vpc.subnets(this.vpcPlacement); | ||
|
||
// Cannot test whether the subnets are in different AZs, but at least we can test the amount. | ||
if (subnets.length < 2) { | ||
|
@@ -158,17 +235,27 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster { | |
}); | ||
this.securityGroupId = securityGroup.securityGroupId; | ||
|
||
let secret; | ||
if (!props.masterUser.password) { | ||
secret = new DatabaseSecret(this, 'Secret', { | ||
username: props.masterUser.username, | ||
encryptionKey: props.masterUser.kmsKey | ||
}); | ||
} | ||
|
||
this.engine = props.engine; | ||
|
||
const cluster = new CfnDBCluster(this, 'Resource', { | ||
// Basic | ||
engine: props.engine, | ||
engine: this.engine, | ||
dbClusterIdentifier: props.clusterIdentifier, | ||
dbSubnetGroupName: subnetGroup.ref, | ||
vpcSecurityGroupIds: [this.securityGroupId], | ||
port: props.port, | ||
dbClusterParameterGroupName: props.parameterGroup && props.parameterGroup.parameterGroupName, | ||
// Admin | ||
masterUsername: props.masterUser.username, | ||
masterUserPassword: props.masterUser.password, | ||
masterUsername: secret ? secret.jsonFieldValue('username') : props.masterUser.username, | ||
masterUserPassword: secret ? secret.jsonFieldValue('password') : props.masterUser.password, | ||
backupRetentionPeriod: props.backup && props.backup.retentionDays, | ||
preferredBackupWindow: props.backup && props.backup.preferredWindow, | ||
preferredMaintenanceWindow: props.preferredMaintenanceWindow, | ||
|
@@ -182,6 +269,12 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster { | |
this.clusterEndpoint = new Endpoint(cluster.dbClusterEndpointAddress, cluster.dbClusterEndpointPort); | ||
this.readerEndpoint = new Endpoint(cluster.dbClusterReadEndpointAddress, cluster.dbClusterEndpointPort); | ||
|
||
if (secret) { | ||
this.secret = secret.addTargetAttachment('AttachedSecret', { | ||
target: this | ||
}); | ||
} | ||
|
||
const instanceCount = props.instances != null ? props.instances : 2; | ||
if (instanceCount < 1) { | ||
throw new Error('At least one instance is required'); | ||
|
@@ -220,6 +313,23 @@ export class DatabaseCluster extends cdk.Construct implements IDatabaseCluster { | |
this.connections = new ec2.Connections({ securityGroups: [securityGroup], defaultPortRange }); | ||
} | ||
|
||
/** | ||
* Adds the single user rotation of the master password to this cluster. | ||
*/ | ||
public addRotationSingleUser(id: string, options: RotationSingleUserOptions = {}): RotationSingleUser { | ||
if (!this.secret) { | ||
throw new Error('Cannot add single user rotation for a cluster without secret.'); | ||
} | ||
return new RotationSingleUser(this, id, { | ||
secret: this.secret, | ||
engine: toDatabaseEngine(this.engine), | ||
vpc: this.vpc, | ||
vpcPlacement: this.vpcPlacement, | ||
target: this, | ||
...options | ||
}); | ||
} | ||
|
||
/** | ||
* Export a Database Cluster for importing in another stack | ||
*/ | ||
|
@@ -248,7 +358,7 @@ function databaseInstanceType(instanceType: ec2.InstanceType) { | |
/** | ||
* An imported Database Cluster | ||
*/ | ||
class ImportedDatabaseCluster extends cdk.Construct implements IDatabaseCluster { | ||
class ImportedDatabaseCluster extends DatabaseClusterBase implements IDatabaseCluster { | ||
/** | ||
* Default port to connect to this database | ||
*/ | ||
|
@@ -308,3 +418,20 @@ class ImportedDatabaseCluster extends cdk.Construct implements IDatabaseCluster | |
return this.props; | ||
} | ||
} | ||
|
||
/** | ||
* Transforms a DatbaseClusterEngine to a DatabaseEngine. | ||
* | ||
* @param engine the engine to transform | ||
*/ | ||
function toDatabaseEngine(engine: DatabaseClusterEngine): DatabaseEngine { | ||
switch (engine) { | ||
case DatabaseClusterEngine.Aurora: | ||
case DatabaseClusterEngine.AuroraMysql: | ||
return DatabaseEngine.Mysql; | ||
case DatabaseClusterEngine.AuroraPostgresql: | ||
return DatabaseEngine.Postgres; | ||
default: | ||
throw new Error('Unknown engine'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import kms = require('@aws-cdk/aws-kms'); | ||
import secretsmanager = require('@aws-cdk/aws-secretsmanager'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
|
||
/** | ||
* Construction properties for a DatabaseSecret. | ||
*/ | ||
export interface DatabaseSecretProps { | ||
/** | ||
* The username. | ||
*/ | ||
username: string; | ||
|
||
/** | ||
* The KMS key to use to encrypt the secret. | ||
* | ||
* @default default master key | ||
*/ | ||
encryptionKey?: kms.IEncryptionKey; | ||
} | ||
|
||
/** | ||
* A database secret. | ||
*/ | ||
export class DatabaseSecret extends secretsmanager.Secret { | ||
constructor(scope: cdk.Construct, id: string, props: DatabaseSecretProps) { | ||
super(scope, id, { | ||
encryptionKey: props.encryptionKey, | ||
generateSecretString: ({ | ||
passwordLength: 30, // Oracle password cannot have more than 30 characters | ||
secretStringTemplate: JSON.stringify({ username: props.username }), | ||
generateStringKey: 'password', | ||
excludeCharacters: '"@/\\' | ||
}) as secretsmanager.TemplatedSecretStringGenerator | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import ec2 = require('@aws-cdk/aws-ec2'); | ||
import kms = require('@aws-cdk/aws-kms'); | ||
|
||
/** | ||
* The engine for the database cluster | ||
|
@@ -69,10 +70,18 @@ export interface Login { | |
/** | ||
* Password | ||
* | ||
* Do not put passwords in your CDK code directly. Import it from a Stack | ||
* Parameter or the SSM Parameter Store instead. | ||
* Do not put passwords in your CDK code directly. | ||
* | ||
* @default a Secrets Manager generated password | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
*/ | ||
password?: string; | ||
|
||
/** | ||
* KMS encryption key to encrypt the generated secret. | ||
* | ||
* @default default master key | ||
*/ | ||
password: string; | ||
kmsKey?: kms.IEncryptionKey; | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be on a line by itself.