Skip to content

Commit

Permalink
feat(cloudtrail): enable trail insight
Browse files Browse the repository at this point in the history
  • Loading branch information
watany-dev committed Nov 26, 2022
1 parent 82c3646 commit d2f6a01
Show file tree
Hide file tree
Showing 17 changed files with 1,693 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/README.md
Expand Up @@ -197,3 +197,21 @@ 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', {
insightSelectors: [
{
insightType: Insight.TYPE_API_CALL_RATE,
},
{
insightType: Insight.TYPE_API_ERROR_RATE,
},
],
});
```
38 changes: 38 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 insightSelectors?: InsightSelector[]
}

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

/**
* Util element for InsightSelector
*/
export class Insight {

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

/**
* The type of insights to log on a trail. (API Error Rate)
*/
public static readonly TYPE_API_ERROR_RATE = '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 @@ -298,6 +323,7 @@ export class Trail extends Resource {
snsTopicName: this.topic?.topicName,
eventSelectors: this.eventSelectors,
isOrganizationTrail: props.isOrganizationTrail,
insightSelectors: props.insightSelectors,
});

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

/**
* A JSON string that contains a list of insight types that are logged on a trail.
*/
export interface InsightSelector {
/**
* The type of insights to log on a trail.
*
* @default - No Value.
*/
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.Insight.value"
]
},
"engines": {
Expand Down
91 changes: 89 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, Insight } from '../lib';

const ExpectedBucketPolicyProperties = {
PolicyDocument: {
Expand Down Expand Up @@ -702,4 +702,91 @@ describe('cloudtrail', () => {
});
});
});
});
describe('insights ', () => {
test('no properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightSelectors: [],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [],
});
});
test('API Call Rate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightSelectors: [
{
insightType: Insight.TYPE_API_CALL_RATE,
},
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [{
InsightType: 'ApiCallRateInsight',
}],
});
});
test('API Call Rate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightSelectors: [
{
insightType: Insight.TYPE_API_ERROR_RATE,
},
],
});
Template.fromStack(stack).hasResourceProperties('AWS::CloudTrail::Trail', {
InsightSelectors: [{
InsightType: 'ApiErrorRateInsight',
}],
});
});
test('duplicate properties', () => {
const stack = getTestStack();
new Trail(stack, 'MyAmazingCloudTrail', {
insightSelectors: [
{
insightType: Insight.TYPE_API_CALL_RATE,
},
{
insightType: Insight.TYPE_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', {
insightSelectors: [
{
insightType: Insight.TYPE_API_CALL_RATE,
},
{
insightType: Insight.TYPE_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": "21.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": {}
}

0 comments on commit d2f6a01

Please sign in to comment.