From 409a958f64bdca97d6b336e422a3667ca0651cad Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Wed, 11 Dec 2019 09:57:35 +0100 Subject: [PATCH 1/3] fix(rds): do not allow aurora engines when using DatabaseInstance Aurora instances can only be created inside a cluster and should use the `DatabaseCluster` construct. Closes #5345 --- packages/@aws-cdk/aws-rds/lib/instance.ts | 4 ++++ packages/@aws-cdk/aws-rds/test/test.instance.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 1fab29f7e2e9d..331ceb041e75b 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -670,6 +670,10 @@ abstract class DatabaseInstanceSource extends DatabaseInstanceNew implements IDa constructor(scope: Construct, id: string, props: DatabaseInstanceSourceProps) { super(scope, id, props); + if (/aurora/.test(props.engine.name)) { + throw new Error('Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`.'); + } + this.secretRotationApplication = props.engine.secretRotationApplication; this.sourceCfnProps = { diff --git a/packages/@aws-cdk/aws-rds/test/test.instance.ts b/packages/@aws-cdk/aws-rds/test/test.instance.ts index 9c012ea3fd32d..13ca58a4b9794 100644 --- a/packages/@aws-cdk/aws-rds/test/test.instance.ts +++ b/packages/@aws-cdk/aws-rds/test/test.instance.ts @@ -622,6 +622,22 @@ export = { } })); + test.done(); + }, + + 'throws with aurora engines'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + + // THEN + test.throws(() => new rds.DatabaseInstance(stack, 'Instance', { + engine: rds.DatabaseInstanceEngine.AURORA_POSTGRESQL, + instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL), + masterUsername: 'admin', + vpc, + }), /Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`./); + test.done(); } }; From c95c49788c29c49b2c7a48a6ffe8e95d4b88d911 Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Tue, 24 Dec 2019 09:45:13 +0100 Subject: [PATCH 2/3] compile-time error with isDatabaseInstanceEngine --- packages/@aws-cdk/aws-rds/lib/instance.ts | 3 +++ packages/@aws-cdk/aws-rds/test/test.instance.ts | 16 ---------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 823f2400b5537..bcaadaf8598a4 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -166,6 +166,9 @@ export class DatabaseInstanceEngine extends DatabaseClusterEngine { public static readonly SQL_SERVER_EX = new DatabaseInstanceEngine('sqlserver-ex', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER); public static readonly SQL_SERVER_WEB = new DatabaseInstanceEngine('sqlserver-web', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER); /* tslint:enable max-line-length */ + + /** To make it a compile-time error to pass a DatabaseClusterEngine where a DatabaseInstanceEngine is expected. */ + public readonly isDatabaseInstanceEngine = true; } /** diff --git a/packages/@aws-cdk/aws-rds/test/test.instance.ts b/packages/@aws-cdk/aws-rds/test/test.instance.ts index c58b2eb51c5fb..30b519d4432f7 100644 --- a/packages/@aws-cdk/aws-rds/test/test.instance.ts +++ b/packages/@aws-cdk/aws-rds/test/test.instance.ts @@ -625,22 +625,6 @@ export = { test.done(); }, - 'throws with aurora engines'(test: Test) { - // GIVEN - const stack = new cdk.Stack(); - const vpc = new ec2.Vpc(stack, 'VPC'); - - // THEN - test.throws(() => new rds.DatabaseInstance(stack, 'Instance', { - engine: rds.DatabaseInstanceEngine.AURORA_POSTGRESQL, - instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL), - masterUsername: 'admin', - vpc, - }), /Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`./); - - test.done(); - }, - 'throws when trying to add rotation to an instance without secret'(test: Test) { // GIVEN const stack = new cdk.Stack(); From 9d4415edecbbf9855032204e2d9d4e887d18cbad Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Tue, 24 Dec 2019 09:49:27 +0100 Subject: [PATCH 3/3] remove test on engine name --- packages/@aws-cdk/aws-rds/lib/instance.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index bcaadaf8598a4..32e8f6e0ba4e4 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -675,10 +675,6 @@ abstract class DatabaseInstanceSource extends DatabaseInstanceNew implements IDa constructor(scope: Construct, id: string, props: DatabaseInstanceSourceProps) { super(scope, id, props); - if (/aurora/.test(props.engine.name)) { - throw new Error('Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`.'); - } - this.singleUserRotationApplication = props.engine.singleUserRotationApplication; this.multiUserRotationApplication = props.engine.multiUserRotationApplication;