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(cloudtrail): enable CloudTrail Insights on Trail #23099

Merged
merged 8 commits into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/README.md
Expand Up @@ -197,3 +197,17 @@ new cloudtrail.Trail(this, 'OrganizationTrail', {
isOrganizationTrail: true,
});
```

## CloudTrail Insights

Set `InsightSelector` to enable Insight.
Insights selector values can be `ApiCallRateInsight`, `ApiErrorRateInsight`, or both.

```ts
new Trail(stack, 'Insights', {
insightTypes: [
InsightType.API_CALL_RATE,
InsightType.API_ERROR_RATE,
],
});
```
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/lib/cloudtrail.ts
Expand Up @@ -127,6 +127,13 @@ export interface TrailProps {
* @default - false
*/
readonly isOrganizationTrail?: boolean

/**
* A JSON string that contains the insight types you want to log on a trail.
*
* @default - No Value.
*/
readonly insightTypes?: InsightType[]
}

/**
Expand Down Expand Up @@ -158,6 +165,23 @@ export enum ReadWriteType {
NONE = 'None',
}

/**
* Util element for InsightSelector
*/
export class InsightType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[non-blocking] What are the reasons to use a class instead of an enum here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies. I'm just left with something that was created with expandability in mind during development.

/**
* The type of insights to log on a trail. (API Call Rate)
*/
public static readonly API_CALL_RATE = new InsightType('ApiCallRateInsight');

/**
* The type of insights to log on a trail. (API Error Rate)
*/
public static readonly API_ERROR_RATE = new InsightType('ApiErrorRateInsight');

protected constructor(public readonly value: string) {}
}

/**
* Cloud trail allows you to log events that happen in your AWS account
* For example:
Expand Down Expand Up @@ -213,6 +237,7 @@ export class Trail extends Resource {
private s3bucket: s3.IBucket;
private eventSelectors: EventSelector[] = [];
private topic: sns.ITopic | undefined;
private insightTypeValues: InsightSelector[] | undefined;

constructor(scope: Construct, id: string, props: TrailProps = {}) {
super(scope, id, {
Expand Down Expand Up @@ -283,6 +308,12 @@ export class Trail extends Resource {
throw new Error('Both kmsKey and encryptionKey must not be specified. Use only encryptionKey');
}

if (props.insightTypes) {
this.insightTypeValues = props.insightTypes.map(function(t) {
return { insightType: t.value };
});
}

// TODO: not all regions support validation. Use service configuration data to fail gracefully
const trail = new CfnTrail(this, 'Resource', {
isLogging: true,
Expand All @@ -298,6 +329,7 @@ export class Trail extends Resource {
snsTopicName: this.topic?.topicName,
eventSelectors: this.eventSelectors,
isOrganizationTrail: props.isOrganizationTrail,
insightSelectors: this.insightTypeValues,
});

this.trailArn = this.getResourceArnAttribute(trail.attrArn, {
Expand Down Expand Up @@ -502,3 +534,7 @@ interface EventSelectorData {
readonly type: string;
readonly values: string[];
}

interface InsightSelector {
readonly insightType?: string;
}
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cloudtrail/package.json
Expand Up @@ -115,7 +115,8 @@
},
"awslint": {
"exclude": [
"events-method-signature:@aws-cdk/aws-cloudtrail.Trail.onEvent"
"events-method-signature:@aws-cdk/aws-cloudtrail.Trail.onEvent",
"docs-public-apis:@aws-cdk/aws-cloudtrail.InsightType.value"
]
},
"engines": {
Expand Down
79 changes: 77 additions & 2 deletions packages/@aws-cdk/aws-cloudtrail/test/cloudtrail.test.ts
Expand Up @@ -7,7 +7,7 @@ import * as s3 from '@aws-cdk/aws-s3';
import * as sns from '@aws-cdk/aws-sns';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { Stack } from '@aws-cdk/core';
import { ManagementEventSources, ReadWriteType, Trail } from '../lib';
import { ManagementEventSources, ReadWriteType, Trail, InsightType } from '../lib';

const ExpectedBucketPolicyProperties = {
PolicyDocument: {
Expand Down Expand Up @@ -702,4 +702,79 @@ describe('cloudtrail', () => {
});
});
});
});
describe('insights ', () => {
test('no properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightTypes: [],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [],
});
});
test('API Call Rate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightTypes: [
InsightType.API_CALL_RATE,
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [{
InsightType: 'ApiCallRateInsight',
}],
});
});
test('API Error Rate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightTypes: [
InsightType.API_ERROR_RATE,
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [{
InsightType: 'ApiErrorRateInsight',
}],
});
});
test('duplicate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightTypes: [
InsightType.API_CALL_RATE,
InsightType.API_CALL_RATE,
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [
{
InsightType: 'ApiCallRateInsight',
},
{
InsightType: 'ApiCallRateInsight',
},
],
});
});
test('ALL properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightTypes: [
InsightType.API_CALL_RATE,
InsightType.API_ERROR_RATE,
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [
{
InsightType: 'ApiCallRateInsight',
},
{
InsightType: 'ApiErrorRateInsight',
},
],
});
});
});
});

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,32 @@
{
"version": "22.0.0",
"files": {
"33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c": {
"source": {
"path": "asset.33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"9e54867c184c79374c51ba171db494a5736a30294e618efa94e199b94e58868e": {
"source": {
"path": "aws-cdk-cloudtrail-inshights-test.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "9e54867c184c79374c51ba171db494a5736a30294e618efa94e199b94e58868e.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}