Skip to content

Commit

Permalink
fix(cloudtrail): Invalid resource for policy when using sendToCloudWa…
Browse files Browse the repository at this point in the history
…tchLogs

Sets `this.cloudWatchLogsGroupArn` before using it, such that a correct
resource ARN is used in the policy generated for CloudTrail to be able
to create and use the required log stream.

Fixes #1848
  • Loading branch information
RomainMuller committed Feb 25, 2019
1 parent cec8564 commit 0fac1e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 2 additions & 3 deletions packages/@aws-cdk/aws-cloudtrail/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,13 @@ export class CloudTrail extends cdk.Construct {
});
this.cloudWatchLogsGroupArn = logGroup.logGroupArn;

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

const streamArn = `${this.cloudWatchLogsRoleArn}:log-stream:*`;
logsRole.addToPolicy(new iam.PolicyStatement()
.addActions("logs:PutLogEvents", "logs:CreateLogStream")
.addResource(streamArn));
this.cloudWatchLogsRoleArn = logsRole.roleArn;

}
if (props.managementEvents) {
const managementEvent = {
Expand Down
16 changes: 14 additions & 2 deletions packages/@aws-cdk/aws-cloudtrail/test/test.cloudtrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,20 @@ export = {
expect(stack).to(haveResource("AWS::S3::BucketPolicy", ExpectedBucketPolicyProperties));
expect(stack).to(haveResource("AWS::Logs::LogGroup"));
expect(stack).to(haveResource("AWS::IAM::Role"));
expect(stack).to(haveResource("AWS::Logs::LogGroup", {
RetentionInDays: 365
expect(stack).to(haveResource("AWS::Logs::LogGroup", { RetentionInDays: 365 }));
expect(stack).to(haveResource("AWS::IAM::Policy", {
PolicyDocument: {
Version: '2012-10-17',
Statement: [{
Effect: 'Allow',
Action: ['logs:PutLogEvents', 'logs:CreateLogStream'],
Resource: {
'Fn::Join': ['', [{ 'Fn::GetAtt': ['MyAmazingCloudTrailLogsRoleF2CCF977', 'Arn'] }, ':log-stream:*']],
}
}]
},
PolicyName: 'MyAmazingCloudTrailLogsRoleDefaultPolicy61DC49E7',
Roles: [{ Ref: 'MyAmazingCloudTrailLogsRoleF2CCF977' }],
}));
const trail: any = stack.toCloudFormation().Resources.MyAmazingCloudTrail54516E8D;
test.deepEqual(trail.DependsOn, ['MyAmazingCloudTrailS3Policy39C120B0']);
Expand Down

0 comments on commit 0fac1e4

Please sign in to comment.