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(ecs): unnecessary CloudWatch logs ResourcePolicy #28495

Merged
merged 9 commits into from
Jan 10, 2024

Conversation

sakurai-ryo
Copy link
Contributor

@sakurai-ryo sakurai-ryo commented Dec 26, 2023

This PR modified to avoid creating unnecessary ResourcePolicy in CloudWatch Logs.

issue summary

The related issue reports an error when using the awslogs driver on ECS.
This error is caused by the creation of a ResourcePolicy in CloudWatch Logs that reaches the maximum number of ResourcePolicies.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html

Current behavior

In some cases, this ResourcePolicy will be created and in other cases it will not be created.
Currently, Grant.addToPrincipalOrResource is used to grant permissions to ExecutionRole and Log Group in the ECS taskDef.

this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole());

return iam.Grant.addToPrincipalOrResource({

public static addToPrincipalOrResource(options: GrantWithResourceOptions): Grant {

Grant.addToPrincipalOrResource first grants permissions to the Grantee (ExecutionRole) and creates a resource base policy for cross account access in cases where certain conditions are not met.
This condition is determined by the contents of the principalAccount of the ExecutionRole and the accountID in the env.account and whether or not these are Tokens, but in this scenario, cross account access is not necessary.

if (result.success && sameAccount) {

Also, when the LogGroup.grantWrite call was added to aws-log-driver.ts, the ResourcePolicy for logs could not be created from CFn and only granted to the ExecutionRole.
#1291
スクリーンショット 2023-12-27 1 08 20
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ReleaseHistory.html

Therefore, the resource base policy should not be necessary when using the awslogs driver.

Major changes

This PR changed to grant permissions only to ExecutionRole when using the awslogs driver.
With this fix, ResourcePolicy will no longer be created when using the awslogs driver.
I don't consider this a breaking change, as it changes the content of the generated template, but does not change the behavior of forwarding logs to CloudWatch Logs.
However, if this is a breaking change, I think it is necessary to use the feature flag.

fixes #22307, fixes #20313


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

@aws-cdk-automation aws-cdk-automation requested a review from a team December 26, 2023 16:09
@github-actions github-actions bot added admired-contributor [Pilot] contributed between 13-24 PRs to the CDK feature-request A feature should be added or improved. p1 labels Dec 26, 2023
@sakurai-ryo sakurai-ryo changed the title fix(ecs): remove unneeded log group resource policy fix(ecs): remove unnecessary logs ResourcePolicy Dec 26, 2023
this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole());
// These policies are required for the Execution role to use awslogs driver.
// In cases where `addToExecutionRolePolicy` is not implemented in some cases,
// for example, when used from aws-batch construct,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort and removed feature-request A feature should be added or improved. labels Dec 26, 2023
@sakurai-ryo sakurai-ryo marked this pull request as ready for review December 28, 2023 02:56
@sakurai-ryo sakurai-ryo changed the title fix(ecs): remove unnecessary logs ResourcePolicy fix(ecs): remove unnecessary CloudWatch logs ResourcePolicy Dec 28, 2023
@sakurai-ryo sakurai-ryo changed the title fix(ecs): remove unnecessary CloudWatch logs ResourcePolicy fix(ecs): unnecessary CloudWatch logs ResourcePolicy Dec 28, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 28, 2023
@@ -135,7 +136,16 @@ export class AwsLogDriver extends LogDriver {
? `${this.props.maxBufferSize.toBytes({ rounding: SizeRoundingBehavior.FLOOR })}b`
: undefined;

this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole());
// These policies are required for the Execution role to use awslogs driver.
// In cases where `addToExecutionRolePolicy` is not implemented in some cases,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// In cases where `addToExecutionRolePolicy` is not implemented in some cases,
// In cases where `addToExecutionRolePolicy` is not implemented,

@sakurai-ryo
Copy link
Contributor Author

@paulhcsun
Thanks! fixed it.

Copy link
Contributor

@paulhcsun paulhcsun left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me. Your reasoning for why resource base policy shouldn't be needed for aws log driver makes sense. I just have one concern about cases where addToPrincipalPolicy is not implemented, if that isn't an issue then I'm good to approve.

@sakurai-ryo
Copy link
Contributor Author

@paulhcsun
Thanks for your feedback!

In this case, I believe that addToPrincipalPolicy is always implemented.
This is because the obtainExecutionRole method creates and returns an ExecutionRole using the Role construct if one does not exist, and the Role construct implements the addToPrincipalPolicy method.


https://github.com/sakurai-ryo/aws-cdk/blob/10ed1948beb0f83c1b978da9c0a656aa01a382cb/packages/aws-cdk-lib/aws-iam/lib/role.ts#L521

@paulhcsun
Copy link
Contributor

paulhcsun commented Jan 9, 2024

Thanks for clarifying @sakurai-ryo, I think I just read the method names incorrectly there for addToPrincipalPolicy and addExecutionRolePolicy. A couple more questions to help me understand things a bit better as I'm not too familiar with aws-log-driver:

  1. Could you clarify how you reached this conclusion from the attached PR that

when the LogGroup.grantWrite call was added to aws-log-driver.ts, the ResourcePolicy for logs could not be created from CFn and only granted to the ExecutionRole. #1291

I don't see any explicit mention of that in the PR itself.

  1. The image attached below the above quote mentions that AWS::Logs::ResourcePolicy resource is used to create a IAM policy. Is this the same workflow for which we are no longer creating the resource policy? And if so would this break anything for users that may be using the generated ResourcePolicy to create IAM policies?

Thanks in advance for taking the time to have this discussion with me to better understand the changes. I know this is will be a big win for many people dealing with this same issue, I just want to be sure that we won't need a feature flag for it.

@sakurai-ryo
Copy link
Contributor Author

@paulhcsun
Thank you, too, for digging deeper into this issue.
The change in the IAM Policy is significant, and we should discuss it thoroughly.

The implementation for setting IAM Policy when using AWSLogs driver was added in 2018 (#1291), while AWS::Logs::ResourcePolicy was introduced in 2021 and implemented in the CDK (#17015).
Since no other commits have changed AWSLogs driver codes about granting policy, it should have worked without AWS::Logs::ResourcePolicy until 2021.

I do not think this PR breaks anything that may be a breaking change.
As per these documents, access is allowed if either the Identity base policy or the Resource base policy is Allow for access by the same account.
In addition, IAM's evaluation logic evaluates the Identity base policy after the Resource base policy, so even if log forwarding is denied by Permissions Boundary etc, if it is allowed by ExecutionRole, log forwarding will be permitted without any problem.
https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html#policy-eval-denyallow
https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html#policies_evaluation_example

Sorry if I misunderstood what you meant.

@paulhcsun
Copy link
Contributor

@sakurai-ryo
Thanks for your response, those answer both of my questions and I'm comfortable moving forward with this without a feature flag. Thanks again for the contribution, this will be a very helpful win for the community!

Copy link
Contributor

mergify bot commented Jan 10, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jan 10, 2024
Copy link
Contributor

mergify bot commented Jan 10, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: f12e2f1
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 5f96d13 into aws:main Jan 10, 2024
9 checks passed
Copy link
Contributor

mergify bot commented Jan 10, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

GavinZZ pushed a commit that referenced this pull request Jan 11, 2024
This PR modified to avoid creating unnecessary `ResourcePolicy` in CloudWatch Logs.

The related issue reports an error when using the awslogs driver on ECS.
This error is caused by the creation of a ResourcePolicy in CloudWatch Logs that reaches the maximum number of ResourcePolicies.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html

In some cases, this ResourcePolicy will be created and in other cases it will not be created.
Currently, `Grant.addToPrincipalOrResource` is used to grant permissions to ExecutionRole and Log Group in the ECS taskDef.
https://github.com/aws/aws-cdk/blob/607dccb0fd920d25f0fe2613b83c9830322c439e/packages/aws-cdk-lib/aws-ecs/lib/log-drivers/aws-log-driver.ts#L138
https://github.com/aws/aws-cdk/blob/607dccb0fd920d25f0fe2613b83c9830322c439e/packages/aws-cdk-lib/aws-logs/lib/log-group.ts#L194
https://github.com/aws/aws-cdk/blob/607dccb0fd920d25f0fe2613b83c9830322c439e/packages/aws-cdk-lib/aws-iam/lib/grant.ts#L122

`Grant.addToPrincipalOrResource` first grants permissions to the Grantee (ExecutionRole) and creates a resource base policy for cross account access in cases where certain conditions are not met.
This condition is determined by the contents of the `principalAccount` of the ExecutionRole and the accountID in the `env.account` and whether or not these are Tokens, but in this scenario, cross account access is not necessary.
https://github.com/aws/aws-cdk/blob/607dccb0fd920d25f0fe2613b83c9830322c439e/packages/aws-cdk-lib/aws-iam/lib/grant.ts#L141

Also, when the `LogGroup.grantWrite` call was added to `aws-log-driver.ts`, the ResourcePolicy for logs could not be created from CFn and only granted to the ExecutionRole.
#1291
![スクリーンショット 2023-12-27 1 08 20](https://github.com/aws/aws-cdk/assets/58683719/5a17a041-d560-45fa-bac6-cdc3894b18bc)
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ReleaseHistory.html

Therefore, the resource base policy should not be necessary when using the awslogs driver.

This PR changed to grant permissions only to ExecutionRole when using the awslogs driver.
With this fix, ResourcePolicy will no longer be created when using the awslogs driver.
I don't consider this a breaking change, as it changes the content of the generated template, but does not change the behavior of forwarding logs to CloudWatch Logs.
However, if this is a breaking change, I think it is necessary to use the feature flag.

fixes #22307, fixes #20313

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
3 participants