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(cloudtrail): Invalid resource for policy when using sendToCloudWatchLogs #1851

Merged
merged 2 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is cloudWatchLogsArn optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is meaningless if you do not enable output in CloudWatch Logs.


const streamArn = `${this.cloudWatchLogsRoleArn}:log-stream:*`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait why was this not a static analysis error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field is string | undefined (there is no value if you don't enable CloudWatch/Logs output) - and unassigned it is undefined, which is valid. Interpolating undefined is totally valid (and it'll output a literal undefined).

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