Skip to content

Commit

Permalink
fix(rds): split DatabaseClusterEngine and DatabaseInstanceEngine into…
Browse files Browse the repository at this point in the history
… separate types

Currently, InstanceEngine extended ClusterEngine,
which meant you could pass InstanceEngine when creating a Cluster,
which would fail at deploy time.
This change splits the two classes into distinct types,
without a subtyping relationship between the two.
  • Loading branch information
skinny85 committed Jun 12, 2020
1 parent 9fb0d48 commit 6da44bd
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 156 deletions.
46 changes: 46 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { BaseEngine } from './engine';

/**
* A database cluster engine. Provides mapping to the serverless application
* used for secret rotation.
*/
export class DatabaseClusterEngine extends BaseEngine {
public static readonly AURORA = new DatabaseClusterEngine(
'aurora',
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '5.6', parameterGroupFamily: 'aurora5.6' },
],
);

public static readonly AURORA_MYSQL = new DatabaseClusterEngine(
'aurora-mysql',
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '5.7', parameterGroupFamily: 'aurora-mysql5.7' },
],
);

public static readonly AURORA_POSTGRESQL = new DatabaseClusterEngine(
'aurora-postgresql',
secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '9.6', parameterGroupFamily: 'aurora-postgresql9.6' },
{ engineMajorVersion: '10', parameterGroupFamily: 'aurora-postgresql10' },
{ engineMajorVersion: '11', parameterGroupFamily: 'aurora-postgresql11' },
],
);

/**
* Get the latest parameter group family for this engine. Latest is determined using semver on the engine major version.
* When `engineVersion` is specified, return the parameter group family corresponding to that engine version.
* Return undefined if no parameter group family is defined for this engine or for the requested `engineVersion`.
*/
public parameterGroupFamily(engineVersion?: string): string | undefined {
return this.calculateParameterGroupForEngineVersion(engineVersion);
}
}
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { CfnDeletionPolicy, Construct, Duration, RemovalPolicy, Resource, Token } from '@aws-cdk/core';
import { DatabaseClusterEngine } from './cluster-engine';
import { DatabaseClusterAttributes, IDatabaseCluster } from './cluster-ref';
import { DatabaseSecret } from './database-secret';
import { Endpoint } from './endpoint';
import { ClusterParameterGroup, IParameterGroup } from './parameter-group';
import { BackupProps, DatabaseClusterEngine, InstanceProps, Login, RotationMultiUserOptions } from './props';
import { BackupProps, InstanceProps, Login, RotationMultiUserOptions } from './props';
import { CfnDBCluster, CfnDBInstance, CfnDBSubnetGroup } from './rds.generated';

/**
Expand Down
53 changes: 53 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { compare } from './private/version';
import { ParameterGroupFamily } from './props';

/** @internal */
export abstract class BaseEngine {
/**
* The engine.
*/
public readonly name: string;

/**
* The single user secret rotation application.
*/
public readonly singleUserRotationApplication: secretsmanager.SecretRotationApplication;

/**
* The multi user secret rotation application.
*/
public readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;

private readonly parameterGroupFamilies?: ParameterGroupFamily[];

constructor(
name: string,
singleUserRotationApplication: secretsmanager.SecretRotationApplication,
multiUserRotationApplication: secretsmanager.SecretRotationApplication,
parameterGroupFamilies?: ParameterGroupFamily[]) {

this.name = name;
this.singleUserRotationApplication = singleUserRotationApplication;
this.multiUserRotationApplication = multiUserRotationApplication;
this.parameterGroupFamilies = parameterGroupFamilies;
}

protected calculateParameterGroupForEngineVersion(engineVersion: string | undefined) {
if (this.parameterGroupFamilies === undefined) {
return undefined;
}
if (engineVersion) {
const family = this.parameterGroupFamilies.find(x => engineVersion.startsWith(x.engineMajorVersion));
if (family) {
return family.parameterGroupFamily;
}
} else if (this.parameterGroupFamilies.length > 0) {
const sorted = this.parameterGroupFamilies.slice().sort((a, b) => {
return compare(a.engineMajorVersion, b.engineMajorVersion);
}).reverse();
return sorted[0].parameterGroupFamily;
}
return undefined;
}
}
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './cluster';
export * from './cluster-ref';
export * from './cluster-engine';
export * from './instance-engine';
export * from './props';
export * from './parameter-group';
export * from './database-secret';
Expand Down
139 changes: 139 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/instance-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { BaseEngine } from './engine';

/**
* A database instance engine. Provides mapping to DatabaseEngine used for
* secret rotation.
*/
export class DatabaseInstanceEngine extends BaseEngine {
public static readonly MARIADB = new DatabaseInstanceEngine(
'mariadb',
secretsmanager.SecretRotationApplication.MARIADB_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.MARIADB_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '10.0', parameterGroupFamily: 'mariadb10.0' },
{ engineMajorVersion: '10.1', parameterGroupFamily: 'mariadb10.1' },
{ engineMajorVersion: '10.2', parameterGroupFamily: 'mariadb10.2' },
{ engineMajorVersion: '10.3', parameterGroupFamily: 'mariadb10.3' },
],
);

public static readonly MYSQL = new DatabaseInstanceEngine(
'mysql',
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.MYSQL_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '5.6', parameterGroupFamily: 'mysql5.6' },
{ engineMajorVersion: '5.7', parameterGroupFamily: 'mysql5.7' },
{ engineMajorVersion: '8.0', parameterGroupFamily: 'mysql8.0' },
],
);

public static readonly ORACLE_EE = new DatabaseInstanceEngine(
'oracle-ee',
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-ee-11.2' },
{ engineMajorVersion: '12.1', parameterGroupFamily: 'oracle-ee-12.1' },
{ engineMajorVersion: '12.2', parameterGroupFamily: 'oracle-ee-12.2' },
{ engineMajorVersion: '18', parameterGroupFamily: 'oracle-ee-18' },
{ engineMajorVersion: '19', parameterGroupFamily: 'oracle-ee-19' },
],
);

public static readonly ORACLE_SE2 = new DatabaseInstanceEngine(
'oracle-se2',
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '12.1', parameterGroupFamily: 'oracle-se2-12.1' },
{ engineMajorVersion: '12.2', parameterGroupFamily: 'oracle-se2-12.2' },
{ engineMajorVersion: '18', parameterGroupFamily: 'oracle-se2-18' },
{ engineMajorVersion: '19', parameterGroupFamily: 'oracle-se2-19' },
],
);

public static readonly ORACLE_SE1 = new DatabaseInstanceEngine(
'oracle-se1',
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-se1-11.2' },
],
);

public static readonly ORACLE_SE = new DatabaseInstanceEngine(
'oracle-se',
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-se-11.2' },
],
);

public static readonly POSTGRES = new DatabaseInstanceEngine(
'postgres',
secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '9.3', parameterGroupFamily: 'postgres9.3' },
{ engineMajorVersion: '9.4', parameterGroupFamily: 'postgres9.4' },
{ engineMajorVersion: '9.5', parameterGroupFamily: 'postgres9.5' },
{ engineMajorVersion: '9.6', parameterGroupFamily: 'postgres9.6' },
{ engineMajorVersion: '10', parameterGroupFamily: 'postgres10' },
{ engineMajorVersion: '11', parameterGroupFamily: 'postgres11' },
],
);

public static readonly SQL_SERVER_EE = new DatabaseInstanceEngine(
'sqlserver-ee',
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-ee-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-ee-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-ee-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-ee-14.0' },
],
);

public static readonly SQL_SERVER_SE = new DatabaseInstanceEngine(
'sqlserver-se',
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-se-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-se-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-se-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-se-14.0' },
],
);

public static readonly SQL_SERVER_EX = new DatabaseInstanceEngine(
'sqlserver-ex',
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-ex-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-ex-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-ex-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-ex-14.0' },
],
);

public static readonly SQL_SERVER_WEB = new DatabaseInstanceEngine(
'sqlserver-web',
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER,
secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER,
[
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-web-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-web-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-web-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-web-14.0' },
],
);

/** To make it a compile-time error to pass a DatabaseClusterEngine where a DatabaseInstanceEngine is expected. */
public readonly isDatabaseInstanceEngine = true;
}
87 changes: 2 additions & 85 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { CfnDeletionPolicy, Construct, Duration, IResource, Lazy, RemovalPolicy, Resource, SecretValue, Stack, Token } from '@aws-cdk/core';
import { DatabaseSecret } from './database-secret';
import { Endpoint } from './endpoint';
import { DatabaseInstanceEngine } from './instance-engine';
import { IOptionGroup } from './option-group';
import { IParameterGroup } from './parameter-group';
import { DatabaseClusterEngine, RotationMultiUserOptions } from './props';
import { RotationMultiUserOptions } from './props';
import { CfnDBInstance, CfnDBInstanceProps, CfnDBSubnetGroup } from './rds.generated';

/**
Expand Down Expand Up @@ -148,90 +149,6 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
}
}

/**
* A database instance engine. Provides mapping to DatabaseEngine used for
* secret rotation.
*/
export class DatabaseInstanceEngine extends DatabaseClusterEngine {
/* tslint:disable max-line-length */
public static readonly MARIADB = new DatabaseInstanceEngine('mariadb', secretsmanager.SecretRotationApplication.MARIADB_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.MARIADB_ROTATION_MULTI_USER, [
{ engineMajorVersion: '10.0', parameterGroupFamily: 'mariadb10.0' },
{ engineMajorVersion: '10.1', parameterGroupFamily: 'mariadb10.1' },
{ engineMajorVersion: '10.2', parameterGroupFamily: 'mariadb10.2' },
{ engineMajorVersion: '10.3', parameterGroupFamily: 'mariadb10.3' },
]);

public static readonly MYSQL = new DatabaseInstanceEngine('mysql', secretsmanager.SecretRotationApplication.MYSQL_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.MYSQL_ROTATION_MULTI_USER, [
{ engineMajorVersion: '5.6', parameterGroupFamily: 'mysql5.6' },
{ engineMajorVersion: '5.7', parameterGroupFamily: 'mysql5.7' },
{ engineMajorVersion: '8.0', parameterGroupFamily: 'mysql8.0' },
]);

public static readonly ORACLE_EE = new DatabaseInstanceEngine('oracle-ee', secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-ee-11.2' },
{ engineMajorVersion: '12.1', parameterGroupFamily: 'oracle-ee-12.1' },
{ engineMajorVersion: '12.2', parameterGroupFamily: 'oracle-ee-12.2' },
{ engineMajorVersion: '18', parameterGroupFamily: 'oracle-ee-18' },
{ engineMajorVersion: '19', parameterGroupFamily: 'oracle-ee-19' },
]);

public static readonly ORACLE_SE2 = new DatabaseInstanceEngine('oracle-se2', secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER, [
{ engineMajorVersion: '12.1', parameterGroupFamily: 'oracle-se2-12.1' },
{ engineMajorVersion: '12.2', parameterGroupFamily: 'oracle-se2-12.2' },
{ engineMajorVersion: '18', parameterGroupFamily: 'oracle-se2-18' },
{ engineMajorVersion: '19', parameterGroupFamily: 'oracle-se2-19' },
]);

public static readonly ORACLE_SE1 = new DatabaseInstanceEngine('oracle-se1', secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-se1-11.2' },
]);

public static readonly ORACLE_SE = new DatabaseInstanceEngine('oracle-se', secretsmanager.SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.ORACLE_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11.2', parameterGroupFamily: 'oracle-se-11.2' },
]);

public static readonly POSTGRES = new DatabaseInstanceEngine('postgres', secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.POSTGRES_ROTATION_MULTI_USER, [
{ engineMajorVersion: '9.3', parameterGroupFamily: 'postgres9.3' },
{ engineMajorVersion: '9.4', parameterGroupFamily: 'postgres9.4' },
{ engineMajorVersion: '9.5', parameterGroupFamily: 'postgres9.5' },
{ engineMajorVersion: '9.6', parameterGroupFamily: 'postgres9.6' },
{ engineMajorVersion: '10', parameterGroupFamily: 'postgres10' },
{ engineMajorVersion: '11', parameterGroupFamily: 'postgres11' },
]);

public static readonly SQL_SERVER_EE = new DatabaseInstanceEngine('sqlserver-ee', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-ee-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-ee-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-ee-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-ee-14.0' },
]);

public static readonly SQL_SERVER_SE = new DatabaseInstanceEngine('sqlserver-se', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-se-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-se-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-se-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-se-14.0' },
]);

public static readonly SQL_SERVER_EX = new DatabaseInstanceEngine('sqlserver-ex', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-ex-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-ex-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-ex-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-ex-14.0' },
]);

public static readonly SQL_SERVER_WEB = new DatabaseInstanceEngine('sqlserver-web', secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_SINGLE_USER, secretsmanager.SecretRotationApplication.SQLSERVER_ROTATION_MULTI_USER, [
{ engineMajorVersion: '11', parameterGroupFamily: 'sqlserver-web-11.0' },
{ engineMajorVersion: '12', parameterGroupFamily: 'sqlserver-web-12.0' },
{ engineMajorVersion: '13', parameterGroupFamily: 'sqlserver-web-13.0' },
{ engineMajorVersion: '14', parameterGroupFamily: 'sqlserver-web-14.0' },
]);
/* 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;
}

/**
* The license model.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-rds/lib/option-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import { Construct, IResource, Resource } from '@aws-cdk/core';
import { DatabaseInstanceEngine } from './instance';
import { DatabaseInstanceEngine } from './instance-engine';
import { CfnOptionGroup } from './rds.generated';

/**
Expand Down
Loading

0 comments on commit 6da44bd

Please sign in to comment.