Skip to content

Commit

Permalink
feat(rds): Validate log types for clusters (#9797)
Browse files Browse the repository at this point in the history
This builds on #9772 to add validation for the types of logs that are available
on each cluster type (by engine).

Notes:
* Currently a draft: I want feedback on this approach before either adding some
  constants for ease-of-use and porting a similar approach to instances.
* This is built off of #9772, so includes the changes from #9772 while it is
  pending a push/merge.
* Added pr-linter/exempt-readme label (for now) as the current change is validation only.
  Will add README changes if/when this includes some usable constants.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
njlynch committed Aug 19, 2020
1 parent f6c25ad commit 85fdeb5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster-engine.ts
Expand Up @@ -61,6 +61,9 @@ export interface IClusterEngine extends IEngine {
/** The application used by this engine to perform rotation for a multi-user scenario. */
readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;

/** The log types that are available with this engine type */
readonly supportedLogTypes: string[];

/**
* Method called when the engine is used to create a new cluster.
*/
Expand All @@ -81,6 +84,7 @@ abstract class ClusterEngineBase implements IClusterEngine {
public readonly parameterGroupFamily?: string;
public readonly singleUserRotationApplication: secretsmanager.SecretRotationApplication;
public readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;
public abstract readonly supportedLogTypes: string[];

private readonly defaultPort?: number;

Expand Down Expand Up @@ -116,6 +120,8 @@ interface MysqlClusterEngineBaseProps {
}

abstract class MySqlClusterEngineBase extends ClusterEngineBase {
public readonly supportedLogTypes: string[] = ['error', 'general', 'slowquery', 'audit'];

constructor(props: MysqlClusterEngineBaseProps) {
super({
...props,
Expand Down Expand Up @@ -408,6 +414,8 @@ export interface AuroraPostgresClusterEngineProps {
}

class AuroraPostgresClusterEngine extends ClusterEngineBase {
public readonly supportedLogTypes: string[] = ['postgresql'];

constructor(version?: AuroraPostgresEngineVersion) {
super({
engineType: 'aurora-postgresql',
Expand Down
21 changes: 14 additions & 7 deletions packages/@aws-cdk/aws-rds/lib/cluster.ts
Expand Up @@ -613,13 +613,20 @@ export class DatabaseCluster extends DatabaseClusterBase {
}

private setLogRetention(props: DatabaseClusterProps) {
if (props.cloudwatchLogsExports && props.cloudwatchLogsRetention) {
for (const log of props.cloudwatchLogsExports) {
new lambda.LogRetention(this, `LogRetention${log}`, {
logGroupName: `/aws/rds/cluster/${this.clusterIdentifier}/${log}`,
retention: props.cloudwatchLogsRetention,
role: props.cloudwatchLogsRetentionRole,
});
if (props.cloudwatchLogsExports) {
const unsupportedLogTypes = props.cloudwatchLogsExports.filter(logType => !props.engine.supportedLogTypes.includes(logType));
if (unsupportedLogTypes.length > 0) {
throw new Error(`Unsupported logs for the current engine type: ${unsupportedLogTypes.join(',')}`);
}

if (props.cloudwatchLogsRetention) {
for (const log of props.cloudwatchLogsExports) {
new lambda.LogRetention(this, `LogRetention${log}`, {
logGroupName: `/aws/rds/cluster/${this.clusterIdentifier}/${log}`,
retention: props.cloudwatchLogsRetention,
role: props.cloudwatchLogsRetentionRole,
});
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/@aws-cdk/aws-rds/test/test.cluster-engine.ts
Expand Up @@ -107,4 +107,12 @@ export = {

test.done();
},
};

'supported log types'(test: Test) {
const mysqlLogTypes = ['error', 'general', 'slowquery', 'audit'];
test.deepEqual(DatabaseClusterEngine.aurora({ version: AuroraEngineVersion.VER_1_22_2 }).supportedLogTypes, mysqlLogTypes);
test.deepEqual(DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_2_08_1 }).supportedLogTypes, mysqlLogTypes);
test.deepEqual(DatabaseClusterEngine.auroraPostgres({ version: AuroraPostgresEngineVersion.VER_9_6_9 }).supportedLogTypes, ['postgresql']);
test.done();
},
}
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-rds/test/test.cluster.ts
Expand Up @@ -1155,6 +1155,29 @@ export = {
test.done();
},

'throws if given unsupported CloudWatch log exports'(test: Test) {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');

test.throws(() => {
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA,
masterUser: {
username: 'admin',
password: cdk.SecretValue.plainText('tooshort'),
},
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
},
cloudwatchLogsExports: ['error', 'general', 'slowquery', 'audit', 'thislogdoesnotexist', 'neitherdoesthisone'],
});
}, /Unsupported logs for the current engine type: thislogdoesnotexist,neitherdoesthisone/);

test.done();
},

'does not throw (but adds a node error) if a (dummy) VPC does not have sufficient subnets'(test: Test) {
// GIVEN
const stack = testStack();
Expand Down

0 comments on commit 85fdeb5

Please sign in to comment.