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

fix(aws-cloudtrail): correct created log policy when sendToCloudWatchLogs is true #1966

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-cloudtrail/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ export class CloudTrail extends cdk.Construct {

logsRole = new iam.Role(this, 'LogsRole', { assumedBy: new iam.ServicePrincipal(cloudTrailPrincipal) });

const streamArn = `${logsRole.roleArn}:log-stream:*`;
logsRole.addToPolicy(new iam.PolicyStatement()
.addActions("logs:PutLogEvents", "logs:CreateLogStream")
.addResource(streamArn));
.addResource(logGroup.logGroupArn));
}
if (props.managementEvents) {
const managementEvent = {
Expand Down Expand Up @@ -181,6 +180,13 @@ export class CloudTrail extends cdk.Construct {
this.cloudTrailArn = trail.trailArn;
const s3BucketPolicy = s3bucket.node.findChild("Policy").node.findChild("Resource") as s3.CfnBucketPolicy;
trail.node.addDependency(s3BucketPolicy);

// If props.sendToCloudWatchLogs is set to true then the trail needs to depend on the created logsRole
// so that it can create the log stream for the log group. This ensures the logsRole is created and propagated
// before the trail tries to create the log stream.
if (logsRole !== undefined) {
trail.node.addDependency(logsRole);
}
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/@aws-cdk/aws-cloudtrail/test/test.cloudtrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const ExpectedBucketPolicyProperties = {
}
};

const logsRolePolicyName = 'MyAmazingCloudTrailLogsRoleDefaultPolicy61DC49E7';
const logsRoleName = 'MyAmazingCloudTrailLogsRoleF2CCF977';

export = {
'constructs the expected resources': {
'with no properties'(test: Test) {
Expand Down Expand Up @@ -83,15 +86,15 @@ export = {
Effect: 'Allow',
Action: ['logs:PutLogEvents', 'logs:CreateLogStream'],
Resource: {
'Fn::Join': ['', [{ 'Fn::GetAtt': ['MyAmazingCloudTrailLogsRoleF2CCF977', 'Arn'] }, ':log-stream:*']],
'Fn::GetAtt': ['MyAmazingCloudTrailLogGroupAAD65144', 'Arn'],
}
}]
},
PolicyName: 'MyAmazingCloudTrailLogsRoleDefaultPolicy61DC49E7',
PolicyName: logsRolePolicyName,
Roles: [{ Ref: 'MyAmazingCloudTrailLogsRoleF2CCF977' }],
}));
const trail: any = stack.toCloudFormation().Resources.MyAmazingCloudTrail54516E8D;
test.deepEqual(trail.DependsOn, ['MyAmazingCloudTrailS3Policy39C120B0']);
test.deepEqual(trail.DependsOn, [logsRolePolicyName, logsRoleName, 'MyAmazingCloudTrailS3Policy39C120B0']);
test.done();
},
'enabled and custom retention'(test: Test) {
Expand All @@ -110,7 +113,7 @@ export = {
RetentionInDays: 7
}));
const trail: any = stack.toCloudFormation().Resources.MyAmazingCloudTrail54516E8D;
test.deepEqual(trail.DependsOn, ['MyAmazingCloudTrailS3Policy39C120B0']);
test.deepEqual(trail.DependsOn, [logsRolePolicyName, logsRoleName, 'MyAmazingCloudTrailS3Policy39C120B0']);
test.done();
},
},
Expand Down