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(iam): Role policies cannot grow beyond 10k #20400

Merged
merged 10 commits into from May 25, 2022

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented May 18, 2022

(This change has been split off from #20189 because that PR was growing
too big)

We add all Role policy statements to the Inline policy, which
has a maximums size of 10k. Especially when creating CDK Pipelines
that deploy to a lot of environments, the list of Role ARNs
the Pipeline should be allowed to assume exceeds this size.

Roles also have the ability to have Managed Policies attached
(10 by default, 20 with a quota increase), each of them can be 6k
in size. By spilling over from inline policies into Managed Policies
we can get a total of 70k of statements attached to reach Role.

This PR introduces IComparablePrincipal to be able to value-compare
two principals: since we want to merge first before we split (to get the
most bang for our buck), we now need to do statement merging during the
prepare phase, while we are still working on the object graph (instead
of the rendered CloudFormation template).

  • That means statement merging had to be modified to work on
    PolicyStatement objects, which requires being able to compare
    Principal objects.

Closes #19276, closes #19939, closes #19835.


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

We add all Role policy statements to the Inline policy, which
has a maximums size of 10k. Especially when creating CDK Pipelines
that deploy to a lot of environments, the list of Role ARNs
the Pipeline should be allowed to assume exceeds this size.

Roles also have the ability to have Managed Policies attached
(10 by default, 20 with a quota increase), each of them can be 6k
in size. By spilling over from inline policies into Managed Policies
we can get a total of 70k of statements attached to reach Role.

This PR introduces `IComparablePrincipal` to be able to value-compare
two principals: since we want to merge first before we split (to get the
most bang for our buck), we now need to do statement merging during the
prepare phase, while we are still working on the object graph (instead
of the rendered CloudFormation template).
* That means statement merging had to be modified to work on
  PolicyStatement objects, which requires being able to compare
  Principal objects.

Closes #19276, closes #19939, closes #19835.
@rix0rrr rix0rrr requested a review from a team May 18, 2022 13:06
@rix0rrr rix0rrr self-assigned this May 18, 2022
@gitpod-io
Copy link

gitpod-io bot commented May 18, 2022

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels May 18, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team May 18, 2022 13:06
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label May 18, 2022
@rix0rrr rix0rrr added effort/large Large work item – several weeks of effort and removed effort/medium Medium work item – several days of effort labels May 18, 2022
Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

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

Nice work on this @rix0rrr! This PR is enormous, so this is a first pass, mainly style comments. My only concern with this PR is the estimation of ARN lengths. The estimates are probably safe but in the event a user does something we didn't forsee, they should have a way of specifying the ARN length.

packages/@aws-cdk/aws-iam/lib/policy-document.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/policy-document.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/policy-document.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/private/merge-statements.ts Outdated Show resolved Hide resolved
let ret = 0;

const actionEstimate = 20;
const arnEstimate = 120; // A safe (over)estimate on how long ARNs typically are
Copy link
Contributor

Choose a reason for hiding this comment

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

Per this doc: https://docs.aws.amazon.com/IAM/latest/APIReference/API_Policy.html

ARNs can be up to 2048 characters in length. The only way the estimate would be (dangerously) inaccurate is if the user imports an ARN, in which case they likely know the length of it themselves. Can we add an optional argument to ARN import methods, arnLength?: number, which we could use here? This way if users run into a deploy time issue they can specify the ARN length and _estimateSize() can correct for that here.

packages/@aws-cdk/aws-iam/lib/private/merge-statements.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/util.ts Outdated Show resolved Hide resolved
let ret = 0;

const actionEstimate = 20;
const arnEstimate = 120; // A safe (over)estimate on how long ARNs typically are
Copy link
Contributor

Choose a reason for hiding this comment

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

https://docs.aws.amazon.com/IAM/latest/APIReference/API_Role.html mentions 2048 as the upper boundary.

Does the "typically" here imply computations based on average?

@rix0rrr
Copy link
Contributor Author

rix0rrr commented May 19, 2022

The build fails because the test success depends on #20396 being merged first.

* Because we often can't know the length of an ARN (it may be a token and only
* available at deployment time) we'll have to estimate it.
*
* The estimate can be overridden by setting the `@aws-cdk/aws-iam.arnSizeEstimate` context key.
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the option of a value that can be set.

if (!this.defaultPolicy || this._didSplit) {
return;
}
this._didSplit = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I understanding correctly that this is a marker for an (attempted) split? The true holds whether or not the split actually happens.


// Validate sizes
//
// Not entirely accurate, because our "Ref"s and "Fn::GetAtt"s actually need to be evaluated
Copy link
Contributor

Choose a reason for hiding this comment

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

How likely is that with enough time (code changes) this test will start failing?

Copy link
Contributor

@Naumel Naumel left a comment

Choose a reason for hiding this comment

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

The size approximation can be tweaked, the problem is complex and the solution addresses the main concerns.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 2ec115b
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify
Copy link
Contributor

mergify bot commented May 25, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/large Large work item – several weeks of effort p1
Projects
None yet
4 participants