Skip to content

Commit 09ce4b7

Browse files
committed
fix(cloudtrail): emit error if trailName is not set for organization trail
1 parent e70c347 commit 09ce4b7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/aws-cdk-lib/aws-cloudtrail/lib/cloudtrail.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ export interface TrailProps {
9898
readonly snsTopic?: sns.ITopic;
9999

100100
/**
101-
* The name of the trail. We recommend customers do not set an explicit name.
101+
* The name of the trail.
102+
*
103+
* Required when `isOrganizationTrail` is set to true to attach the necessary permissions.
102104
*
103105
* @default - AWS CloudFormation generated name.
104106
*/
@@ -272,6 +274,10 @@ export class Trail extends Resource {
272274

273275
if (props.isOrganizationTrail) {
274276
if (props.orgId !== undefined) {
277+
if (props.trailName === undefined) {
278+
throw new Error('trailName is required for organization trail');
279+
}
280+
275281
this.s3bucket.addToResourcePolicy(new iam.PolicyStatement({
276282
resources: [this.s3bucket.arnForObjects(
277283
`AWSLogs/${props.orgId}/*`,

packages/aws-cdk-lib/aws-cloudtrail/test/cloudtrail.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,17 @@ describe('cloudtrail', () => {
470470
Annotations.fromStack(stack).hasWarning('/TestStack/Trail', 'Skipped attaching a policy to the bucket to allow organization trail to write logs to it because this is an organization trail but orgId is not specified. Consider specifying orgId to attach missing permissions [ack: @aws-cdk/aws-cloudtrail:missingOrgIdForOrganizationTrail]');
471471
});
472472

473+
test('organizationTrail with orgId but without trailName fails', () => {
474+
// GIVEN
475+
const stack = getTestStack();
476+
477+
// WHEN
478+
expect(() => new Trail(stack, 'ErrorTrail', {
479+
isOrganizationTrail: true,
480+
orgId: 'o-xxxxxxxxx',
481+
})).toThrow('trailName is required for organization trail');
482+
});
483+
473484
test('encryption keys', () => {
474485
const stack = new Stack();
475486
const key = new kms.Key(stack, 'key');

0 commit comments

Comments
 (0)