Skip to content

Commit

Permalink
Merge branch 'main' into mrgrain/fix/upgrade-deps-in-samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 7, 2022
2 parents d71e406 + 8a1460e commit c667bac
Show file tree
Hide file tree
Showing 39 changed files with 2,032 additions and 196 deletions.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
/**
* 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.

Original file line number Diff line number Diff line change
@@ -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": {}
}
Loading

0 comments on commit c667bac

Please sign in to comment.