codebuild: PipelineProject Construct: Add option to suppress creation of a AWS::Logs::ResourcePolicy when using logging via aws-logs.LogGroups or be descriptive about the behavior #24656
Labels
@aws-cdk/aws-codebuild
Related to AWS CodeBuild
effort/medium
Medium work item – several days of effort
feature-request
A feature should be added or improved.
p2
Describe the feature
Add in an optional parameter to "disable" (or remove) the auto-generated Resource Policy, or add to the description of the CloudWatchLoggingOptions interface that a AWS::Logs::ResourcePolicy will be auto-generated (just to help others avoid the headache of the weird behavior)
Use Case
Related to issues "#17615" and "#17544", I believe it may be a good workaround to the unexpected Resource Policy being added without the user's knowledge when using the PipelineProject Construct.
Proposed Solution
We can add another parameter to the CloudWatchLoggingOptions interface (something like "suppressResourcePolicy")
On the handler, it simply checks if logging is configured and if it's enabled/disabled.
aws-cdk/packages/@aws-cdk/aws-codebuild/lib/project.ts
Lines 1452 to 1466 in 60a5b2a
After "cloudWatchLogs.logGroup?.grantWrite(this);" is executed, we can then do another check to see if we should do remove it or not:
I wasn't able to find any another potential ways that '.node.tryRemoveChild("Policy")' wouldn't be applicable except if the "Id" may not be "Policy", but I wasn't see where the Id may be changed either.
In the case that the resource Id on tree.json isn't consistent, it can also just be explicitly stated on the documentation that this will be auto-populated given some certain conditions. This way users don't get too confused with the unexpected resource and can try to remediate it themselves by removing it from the LogGroup that it's tied to.
Other Information
I believe the issue of a ResourcePolicy being added unexpectedly is due to the behavior of the "grantWrite()" method being called when checking if logging is being defined or not.
aws-cdk/packages/@aws-cdk/aws-codebuild/lib/project.ts
Lines 1452 to 1466 in 60a5b2a
To my understanding of how this works, essentially grantWrite() will try to do the right thing by applying some policy to a valid Grantable-type object, and in this case it would be some IAM Role.
Given that the stack is account agnostic and an IAM Role is being used similar to the following:
The synthesized template will include the AWS::Logs::ResourcePolicy under the above conditions:
the PipelineProject will invoke the grantWrite() under the hood, and grantWrite() will infer information based on what role information is currently passed to the PipelineProject where it'll be created if an aws_iam.IRole is supplied or not:
In the case of an imported role, to my understanding of the grant() method being used by "grantWrite()" this seems to try inferring if the IAM Role to reference is part of the same account or not via "addToPrincipalOrResource()". Because the stack is account agnostic, it then infers this to potentially be cross-account since we can't guarantee that the imported IAM role will be from the same target account.
Testing this out with a simple stack reflects this behavior when just isolating it to executing "grantWrite()" against an imported role resulting in the same ResourcePolicy to appear without my consent
But when manually creating a role and granting it write permissions, the resource policy disappears within the account agnostic stack:
Because this stems from the LogGroup Construct primarily, I was expecting this to potentially be documented explicitly on the aws_logs overview, or the LogGroup Construct document. I found on the overview that this may be implied:
But given the unexpected results not being explicit/implied on the aws_codebuild.PipelineProject docs, it's understanding how this may be missed as users would have to dig into the source to eventually see this. But this is assuming my findings are correct and would like to hopefully get this cross-checked by others to confirm my understanding of this behavior.
Overall, this seems to be an expected but unfortunate outcome due to how "addToPrincipalOrResource()" infers information about the CDK Stack.
aws-cdk/packages/@aws-cdk/aws-logs/lib/log-group.ts
Lines 173 to 175 in 60a5b2a
aws-cdk/packages/@aws-cdk/aws-logs/lib/log-group.ts
Lines 193 to 202 in 60a5b2a
aws-cdk/packages/@aws-cdk/aws-iam/lib/grant.ts
Lines 122 to 158 in 9d1093f
So, instead of modifying the behavior of "addToPrincipalOrResource()", instead it may just be easier to be clear with how the PipelineProject Construct configures logging automatically on behalf of the user. Otherwise, it may be easier to give users the option to disable it themselves by adding another property and checking if it's set to true or not.
Acknowledgements
CDK version used
2.69.0
Environment details (OS name and version, etc.)
Windows 10 | Node v16.13.0
The text was updated successfully, but these errors were encountered: