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 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
17 changes: 7 additions & 10 deletions packages/@aws-cdk/aws-cloudtrail/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ export enum LogRetention {
export class CloudTrail extends cdk.Construct {

public readonly cloudTrailArn: string;
private readonly cloudWatchLogsRoleArn?: string;
private readonly cloudWatchLogsGroupArn?: string;
private eventSelectors: EventSelector[] = [];

constructor(scope: cdk.Construct, id: string, props: CloudTrailProps = {}) {
Expand All @@ -143,20 +141,19 @@ export class CloudTrail extends cdk.Construct {
.addServicePrincipal(cloudTrailPrincipal)
.setCondition("StringEquals", {'s3:x-amz-acl': "bucket-owner-full-control"}));

let logGroup: logs.CfnLogGroup | undefined;
let logsRole: iam.IRole | undefined;
if (props.sendToCloudWatchLogs) {
const logGroup = new logs.CfnLogGroup(this, "LogGroup", {
logGroup = new logs.CfnLogGroup(this, "LogGroup", {
retentionInDays: props.cloudWatchLogsRetentionTimeDays || LogRetention.OneYear
});
this.cloudWatchLogsGroupArn = logGroup.logGroupArn;

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

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

}
if (props.managementEvents) {
const managementEvent = {
Expand All @@ -176,8 +173,8 @@ export class CloudTrail extends cdk.Construct {
kmsKeyId: props.kmsKey && props.kmsKey.keyArn,
s3BucketName: s3bucket.bucketName,
s3KeyPrefix: props.s3KeyPrefix,
cloudWatchLogsLogGroupArn: this.cloudWatchLogsGroupArn,
cloudWatchLogsRoleArn: this.cloudWatchLogsRoleArn,
cloudWatchLogsLogGroupArn: logGroup && logGroup.logGroupArn,
cloudWatchLogsRoleArn: logsRole && logsRole.roleArn,
snsTopicName: props.snsTopic,
eventSelectors: this.eventSelectors
});
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