Skip to content

Commit 4982aca

Browse files
authoredNov 24, 2021
feat(docdb): implement audit and profiler logs (#17570)
closes #17478 *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ec4187c commit 4982aca

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
 

‎packages/@aws-cdk/aws-docdb/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,19 @@ cluster.addRotationMultiUser('MyUser', { // Add rotation using the multi user sc
116116
The rotation will start as soon as this user exists.
117117

118118
See also [@aws-cdk/aws-secretsmanager](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-secretsmanager/README.md) for credentials rotation of existing clusters.
119+
120+
## Audit and profiler Logs
121+
122+
Sending audit or profiler needs to be configured in two places:
123+
124+
1. Check / create the needed options in your ParameterGroup for [audit](https://docs.aws.amazon.com/documentdb/latest/developerguide/event-auditing.html#event-auditing-enabling-auditing) and
125+
[profiler](https://docs.aws.amazon.com/documentdb/latest/developerguide/profiling.html#profiling.enable-profiling) logs.
126+
2. Enable the corresponding option(s) when creating the `DatabaseCluster`:
127+
128+
```ts
129+
const cluster = new DatabaseCluster(this, 'Database', {
130+
...,
131+
exportProfilerLogsToCloudWatch: true, // Enable sending profiler logs
132+
exportAuditLogsToCloudWatch: true, // Enable sending audit logs
133+
});
134+
```

‎packages/@aws-cdk/aws-docdb/lib/cluster.ts

+29
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ export interface DatabaseClusterProps {
146146
* @default - false
147147
*/
148148
readonly deletionProtection?: boolean;
149+
150+
/**
151+
* Whether the profiler logs should be exported to CloudWatch.
152+
* Note that you also have to configure the profiler log export in the Cluster's Parameter Group.
153+
*
154+
* @see https://docs.aws.amazon.com/documentdb/latest/developerguide/profiling.html#profiling.enable-profiling
155+
* @default false
156+
*/
157+
readonly exportProfilerLogsToCloudWatch?: boolean;
158+
159+
/**
160+
* Whether the audit logs should be exported to CloudWatch.
161+
* Note that you also have to configure the audit log export in the Cluster's Parameter Group.
162+
*
163+
* @see https://docs.aws.amazon.com/documentdb/latest/developerguide/event-auditing.html#event-auditing-enabling-auditing
164+
* @default false
165+
*/
166+
readonly exportAuditLogsToCloudWatch?: boolean;
149167
}
150168

151169
/**
@@ -346,6 +364,15 @@ export class DatabaseCluster extends DatabaseClusterBase {
346364
}
347365
this.securityGroupId = securityGroup.securityGroupId;
348366

367+
// Create the CloudwatchLogsConfiguratoin
368+
const enableCloudwatchLogsExports: string[] = [];
369+
if (props.exportAuditLogsToCloudWatch) {
370+
enableCloudwatchLogsExports.push('audit');
371+
}
372+
if (props.exportProfilerLogsToCloudWatch) {
373+
enableCloudwatchLogsExports.push('profiler');
374+
}
375+
349376
// Create the secret manager secret if no password is specified
350377
let secret: DatabaseSecret | undefined;
351378
if (!props.masterUser.password) {
@@ -383,6 +410,8 @@ export class DatabaseCluster extends DatabaseClusterBase {
383410
backupRetentionPeriod: props.backup?.retention?.toDays(),
384411
preferredBackupWindow: props.backup?.preferredWindow,
385412
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
413+
// EnableCloudwatchLogsExports
414+
enableCloudwatchLogsExports: enableCloudwatchLogsExports.length > 0 ? enableCloudwatchLogsExports : undefined,
386415
// Encryption
387416
kmsKeyId: props.kmsKey?.keyArn,
388417
storageEncrypted,

‎packages/@aws-cdk/aws-docdb/test/cluster.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,70 @@ describe('DatabaseCluster', () => {
588588
}));
589589
});
590590

591+
test('can configure CloudWatchLogs for audit', () => {
592+
// GIVEN
593+
const stack = testStack();
594+
const vpc = new ec2.Vpc(stack, 'VPC');
595+
596+
// WHEN
597+
new DatabaseCluster(stack, 'Database', {
598+
masterUser: {
599+
username: 'admin',
600+
},
601+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
602+
vpc,
603+
exportAuditLogsToCloudWatch: true,
604+
});
605+
606+
// THEN
607+
expectCDK(stack).to(haveResource('AWS::DocDB::DBCluster', {
608+
EnableCloudwatchLogsExports: ['audit'],
609+
}));
610+
});
611+
612+
test('can configure CloudWatchLogs for profiler', () => {
613+
// GIVEN
614+
const stack = testStack();
615+
const vpc = new ec2.Vpc(stack, 'VPC');
616+
617+
// WHEN
618+
new DatabaseCluster(stack, 'Database', {
619+
masterUser: {
620+
username: 'admin',
621+
},
622+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
623+
vpc,
624+
exportProfilerLogsToCloudWatch: true,
625+
});
626+
627+
// THEN
628+
expectCDK(stack).to(haveResource('AWS::DocDB::DBCluster', {
629+
EnableCloudwatchLogsExports: ['profiler'],
630+
}));
631+
});
632+
633+
test('can configure CloudWatchLogs for all logs', () => {
634+
// GIVEN
635+
const stack = testStack();
636+
const vpc = new ec2.Vpc(stack, 'VPC');
637+
638+
// WHEN
639+
new DatabaseCluster(stack, 'Database', {
640+
masterUser: {
641+
username: 'admin',
642+
},
643+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
644+
vpc,
645+
exportAuditLogsToCloudWatch: true,
646+
exportProfilerLogsToCloudWatch: true,
647+
});
648+
649+
// THEN
650+
expectCDK(stack).to(haveResource('AWS::DocDB::DBCluster', {
651+
EnableCloudwatchLogsExports: ['audit', 'profiler'],
652+
}));
653+
});
654+
591655
test('single user rotation', () => {
592656
// GIVEN
593657
const stack = testStack();

0 commit comments

Comments
 (0)
Please sign in to comment.