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

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

merged 2 commits into from
Mar 7, 2019

Conversation

RobinsonAndrew
Copy link
Contributor

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

add the correct resource (log group) to the the log role policy instead of adding the log role as a resource
make the created cloud trail depend on the logs policy when the prop sendToCloudWatchLogs is set to true

Fixes #1963


Pull Request Checklist

  • Testing
    • Unit test added (prefer not to modify an existing test, otherwise, it's probably a breaking change)
    • CLI change?: coordinate update of integration tests with team
    • cdk-init template change?: coordinated update of integration tests with team
  • [x ] Docs
    • jsdocs: All public APIs documented
    • README: README and/or documentation topic updated
  • Title and Description
    • Change type: title prefixed with fix, feat will appear in changelog
    • Title: use lower-case and doesn't end with a period
    • Breaking?: last paragraph: "BREAKING CHANGE: <describe what changed + link for details>"
    • Issues: Indicate issues fixed via: "Fixes #xxx" or "Closes #xxx"
  • Sensitive Modules (requires 2 PR approvers)
    • IAM Policy Document (in @aws-cdk/aws-iam)
    • EC2 Security Groups and ACLs (in @aws-cdk/aws-ec2)
    • Grant APIs (only if not based on official documentation with a reference)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

…Logs is set to true

add the correct resource (log group) to the the log role policy instead of adding the log role as a resource
make the created cloud trail depend on the logs policy when the prop sendToCloudWatchLogs is set to true

fixes #1963
@RobinsonAndrew RobinsonAndrew requested a review from a team as a code owner March 6, 2019 17:43
@@ -150,7 +150,7 @@ export class CloudTrail extends cdk.Construct {

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

const streamArn = `${logsRole.roleArn}:log-stream:*`;
const streamArn = `${logGroup.logGroupArn}:*`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you drop 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.

Can you confirm that the :* suffix is actually necessary? I believe the logGroupArn will already end in :*.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

log-stream was dropped as the cloud trail fails to deploy with it. The reason been that the logs:CreateLogStream action should be on the log group resource and not on the (as yet to be created) log stream. I think the logs:PutLogEvents action could be scoped to just the log stream if we want to be stricter about the permissions.

I tried without :* and it works fine. I'll make the change.


if (logsRole !== undefined) {
const logsRolePolicy = logsRole.node.findChild("DefaultPolicy").node.findChild("Resource");
trail.node.addDependency(logsRolePolicy);
Copy link
Contributor

@sam-goodwin sam-goodwin Mar 7, 2019

Choose a reason for hiding this comment

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

Can you add a comment explaining why we need this?

Also, I think dependencies have been lifted to the Construct level so you can reduce the amount of node traversal:

const logsRolePolicy = logsRole.node.findChild("DefaultPolicy");
trail.addDependency(logsRolePolicy);

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't even need to do that, you can do:

trail.node.addDependency(logsRolePolicy);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume you mean

trail.node.addDependency(logsRole);

I tried this and it works. It adds both role and the policy as dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add this as a comment:

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.

Without the dependency the cloud trail fails to deploy with the same error as the referenced issue.

Copy link
Contributor

@sam-goodwin sam-goodwin left a comment

Choose a reason for hiding this comment

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

Thanks for the bug-fix :)

@sam-goodwin sam-goodwin merged commit f06ff8e into aws:master Mar 7, 2019
@RobinsonAndrew RobinsonAndrew deleted the RobinsonAndrew/fix-cloudtrail-send-logs-to-cloudwatch branch March 9, 2019 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cloudtrail with sendToCloudWatchLogs: true, InvalidCloudWatchLogsLogGroupArnException
3 participants