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

feat(rds): Validate log types for clusters #9797

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster-engine.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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