Skip to content

fix(cloud): shrink launcher IAM policy under IAM's 6144-char cap - #13029

Merged
chenmingwei23 merged 2 commits into
mainfrom
fix/cloud-iam-policy-under-6144
Sep 24, 2026
Merged

chenmingwei23 merged 2 commits into
mainfrom
fix/cloud-iam-policy-under-6144

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

kirocrew cloud iam-policy prints a customer-managed IAM policy for a non-admin
operator to apply. That policy on main was 6467 characters counted without
whitespace
— over AWS's hard cap of 6,144 characters for a customer-managed
policy (whitespace not counted). Applying the printed JSON therefore failed with
LimitExceeded, so every operator who is not already an admin could not create
the launcher policy at all.

Why it matters

The generated policy is the only supported way a least-privilege operator
stands up the kirocrew cloud launcher. Over the cap it is unusable, which
blocks the entire non-admin launch path. It is also prep for #3355 (EC2 Spot),
which adds ~152 characters and cannot fit while main is already over.

What changed (motivation → approach → change)

Root cause: the policy accumulated statements that are individually
least-privilege but collectively push the compact JSON over the quota. The fix
shrinks by merging statements that share the same Effect + Resource +
Condition
— a merge that is permission-neutral by construction (the effective
Effect × Action × Resource × Condition tuple set is unchanged) — plus no other
change. Three merges:

  • Ec2SecurityGroupRulesTagged + Ec2DestructiveTagged + Ec2LifecycleTagged
    → Ec2ManagedResourceMutateTagged (all Resource: "*" +
    aws:ResourceTag/kirocrew:managed=true).
  • IamPutRolePolicyForInstance + IamTagRoleOnManaged →
    IamPutRolePolicyAndTagRoleOnManaged (both the kirocrew-ec2-* role ARN +
    the managed-tag condition).
  • CloudFormationRead + TagDiscovery + Ec2Discovery + Route53DnsPreflight
    → CloudFormationAndResourceDiscovery (all Resource: "*", no Condition;
    all read-only).

Result: 6467 → 5818 characters whitespace-free (29 → 23 statements), under
the 6,144 hard cap and under a self-imposed 5,900 headroom target so #3355 fits.

The security rationale of every merged statement is preserved in the combined
comments (the tag-gate reasoning, the iam:TagRole non-spoofable-distinguisher
argument, the DNS-preflight note, the exclusion of ListCommandInvocations
etc.). No condition, resource scope, or action was added or dropped.

Missing-Resource statement: the task flagged a statement on main
reportedly missing a Resource key. I did not find one — every statement in
policy_document() carries exactly one of Resource/NotResource (the existing
test_is_valid_policy_shape asserts this and passes both before and after). The
single Deny (DenyStartSessionOutsideTheLane) uses NotResource, which is
correct and intentional. Nothing to fix here.

CHANGELOG: deliberately not touched. This repo's docs/build/changelog.md
and the check_changelog_history.py gate state that a feature/fix PR does not
touch CHANGELOG.md
— the release PR writes the section, [Unreleased] and
per-PR entries are refused, and shipped sections are immutable. The commit
subject is the record until a version bump. Adding an entry here would either
fail the gate's intent or freeze an unreleased section; the correct action under
this repo's own rule is to leave it out. (This is a deliberate deviation from the
literal "add a CHANGELOG entry" instruction, driven by the repo's documented
gate.)

Backwards compatibility

Compatible: the effective permission set is byte-for-byte identical before and
after (proven by test_shrink_preserved_the_permission_set_byte_for_byte, which
compares canonical tuple sets of the pre-shrink fixture vs. the new policy — 118
tuples, equal). Statement Sids changed, but Sid is a human label with no
authorization effect; the only in-repo consumers of these Sids are this
module's own tests, updated in the same commit.

Tests

  • test_shrink_preserved_the_permission_set_byte_for_byte (new): flattens
    the pre-shrink policy (kept as fixture test/fixtures/cloud_iam_policy_pre_shrink.json)
    and the current policy into canonical (Effect, Action, Resource-form, Resource, Condition) tuple sets and asserts equality; also asserts the fixture
    really was over 6144 (so it is the genuine pre-shrink state).
  • test_policy_fits_iam_managed_policy_limit (new): asserts the
    whitespace-free length is <= 6144 (docstring cites the AWS IAM
    "Managed policy size" quota, non-adjustable) and <= 5900 for feat(cloud): support EC2 Spot Instances in kirocrew cloud launch (--spot) #3355 headroom.
  • Sid-pinning tests updated, not weakened to the merged names
    (test_authorize_security_group_is_tag_gated, test_lifecycle_is_tag_scoped,
    test_destructive_ec2_verbs_tag_scoped, test_put_role_policy_is_tag_scoped,
    test_tag_role_gated_on_existing_managed_tag,
    test_put_role_policy_scoped_no_dead_boundary_condition,
    test_put_role_policy_and_passrole_not_tag_scoped_regression,
    test_cloudformation_mutation_scoped_to_kirocrew_stacks) — every assertion
    retained.

Manual verification

N/A — unit coverage sufficient. The change is text + tests only, no AWS calls;
equivalence and size are both machine-checked. Ran locally: full
test/test_cloud_*.py suite (1073 passed), flake8, isort --check,
black --check, mypy on both files (all clean), and the changelog-history gate
against the merge base (shipped sections intact).

Related Issues

Prep for #3355 (EC2 Spot launch).

Pattern harvest

Rule candidate: review-prompt
Pattern: an IAM/JSON policy generator that grows near a hard size quota should
carry a size-gate test AND an equivalence-against-fixture test, so statements can
be merged for size without silently changing the permission set.

Checklist

  • At most two commits (one here), Conventional Commits title (fix: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — N/A; CHANGELOG intentionally untouched per the repo gate (see above)
  • No secrets, credentials, or internal references in the diff

@iamwhatever
iamwhatever requested review from a team and cixuuz September 23, 2026 07:25
@iamwhatever iamwhatever added the drive-to-green PR claimed by drive-to-green pipeline label Sep 23, 2026
@iamwhatever iamwhatever self-assigned this Sep 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 5488b808de11fb925c96d02f59a13b766cd35b3d and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5488b80

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 5488b808de11fb925c96d02f59a13b766cd35b3d: <one-sentence reason>

@iamwhatever
iamwhatever force-pushed the fix/cloud-iam-policy-under-6144 branch from c06e63a to c295f25 Compare September 23, 2026 07:35
@github-actions

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ⚠️ review incomplete

GPT 5.6 did not produce a complete verdict for c295f250d673ed148c93e2b8e96dad49eeee1d47; inspect the workflow logs and re-run it.

This comment is updated in place on each push.

See the GPT 5.6 Review job logs; this commit has no completed GPT verdict.

False positive or not applicable? A repository writer can comment:
/ai-review override gpt c295f250d673ed148c93e2b8e96dad49eeee1d47: <one-sentence reason>

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Sep 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of 5488b808de11fb925c96d02f59a13b766cd35b3d — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Verify once against live AWS that aws iam create-policy accepts the merged output — CI proves size and equivalence, never an actual apply.

Suggestions

  • test_shrink_preserved_the_permission_set_byte_for_byte goes red on the very next intentional policy change (feat(cloud): support EC2 Spot Instances in kirocrew cloud launch (--spot) #3355 adds actions), and updating the fixture then contradicts its "genuine pre-shrink state" premise (assert len(old) > 6144); either document the fixture-update protocol in the test or scope it as a one-time migration proof so the next contributor doesn't weaken it ad hoc.

[DESIGN-REVIEWED] 5488b80

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — 🟡 CONCERNS

Premise-level review of 5488b808de11fb925c96d02f59a13b766cd35b3d — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The 383-line pre-shrink fixture and byte-for-byte equivalence test are a one-shot proof guaranteed to go red on #3355 — the feature this PR reserves headroom for.

Not justified as shipped

  • Item 6 — oversized: test_shrink_preserved_the_permission_set_byte_for_byte asserts strict tuple-set equality against a frozen snapshot, and its last line asserts the fixture is > 6144 chars — so feat(cloud): support EC2 Spot Instances in kirocrew cloud launch (--spot) #3355 ("adds ~152 characters", per the description) must fail the equality, and updating the fixture to clear it then fails the over-cap assertion. A verification that only ever validates this diff ships as a permanent gate; the new size test already carries the durable invariant.
    Subtraction: delete test/fixtures/cloud_iam_policy_pre_shrink.json and test_shrink_preserved_the_permission_set_byte_for_byte once this merge is reviewed, keeping test_policy_fits_iam_managed_policy_limit as the lasting pin.
    Clears when: the fixture and equivalence test are deleted, or the PR states how feat(cloud): support EC2 Spot Instances in kirocrew cloud launch (--spot) #3355 clears the guaranteed-red test.

What this change ships

Inventory (8 items) — 7 justified

Intent: make the printed launcher IAM policy applicable again by a non-admin operator (it exceeded AWS's 6,144-char cap) — a FIX, with provenance (the new size test fails on base).

  1. Printed launcher policy fits the cap again (6467 → 5818 whitespace-free chars) — justified
  2. Three tag-gated EC2 statements print as one Ec2ManagedResourceMutateTagged; Sids renamed — justified
  3. iam:PutRolePolicy + iam:TagRole print as one merged, still tag-gated statement — justified
  4. Four account-wide read-only discovery statements print as one — justified
  5. CI now fails if the policy exceeds 6,144 chars or a 6,044 headroom gate (description says 5,900; substance identical) — justified
  6. A frozen 383-line pre-shrink fixture plus byte-for-byte equivalence test pin the permission set — oversized: one-shot proof shipped as a permanent gate that feat(cloud): support EC2 Spot Instances in kirocrew cloud launch (--spot) #3355 must break
  7. cloud.md spec updated for the merged TagRole statement in the same commit — justified
  8. SG-rule test hardened to an exact 9-action set pin on the wildcard-resource statement — justified

[FIRST-PRINCIPLES-REVIEWED] 5488b80

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review — ✅ no blocking findings

Reviewed 5488b808de11fb925c96d02f59a13b766cd35b3d — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 5488b80

Verdict parsed from the review's SHA-scoped output markers for commit 5488b808de11fb925c96d02f59a13b766cd35b3d.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 5488b808de11fb925c96d02f59a13b766cd35b3d: <one-sentence reason>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 23, 2026
The `kirocrew cloud iam-policy` output was 6467 characters whitespace-free,
over AWS's non-adjustable 6,144-char managed-policy quota, so applying the
printed JSON failed with LimitExceeded for any non-admin operator.

Shrink to 5818 whitespace-free by merging statements that share Effect +
Resource + Condition (permission-neutral by construction):

- Ec2SecurityGroupRulesTagged + Ec2DestructiveTagged + Ec2LifecycleTagged ->
  Ec2ManagedResourceMutateTagged (Resource "*", managed-tag condition)
- IamPutRolePolicyForInstance + IamTagRoleOnManaged ->
  IamPutRolePolicyAndTagRoleOnManaged (role ARN, managed-tag condition)
- CloudFormationRead + TagDiscovery + Ec2Discovery + Route53DnsPreflight ->
  CloudFormationAndResourceDiscovery (Resource "*", no condition)

The effective permission set is identical before and after: a new test
flattens the pre-shrink policy (kept as a fixture) and the current policy into
canonical (Effect, Action, Resource-form, Resource, Condition) tuple sets and
asserts equality. A size-gate test asserts the whitespace-free length is at
most 6144 (citing the AWS quota) and at most 5900 for headroom so EC2 Spot
(#3355, roughly +152 chars) still fits.

Sid-pinning tests updated to the merged statement names; no assertions dropped.
@iamwhatever
iamwhatever force-pushed the fix/cloud-iam-policy-under-6144 branch from c295f25 to d270b09 Compare September 23, 2026 07:59
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Fable 5) — ⏭️ skipped

Revision 5488b808de11fb925c96d02f59a13b766cd35b3d touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 23, 2026
@iamwhatever
iamwhatever force-pushed the fix/cloud-iam-policy-under-6144 branch from 0d6556f to 18f22f1 Compare September 23, 2026 21:36
Remove test/test_knowledge.py from .github/black-baseline.txt: the file
graduated to black-clean on main, so the baselined-file gate fails until
the stale entry is pruned (0 new offenders, 1 graduated entry). Same
base-defect fix applied to the other wave-3 PRs.

Also tighten two assertions on the merged IamPutRolePolicyAndTagRoleOnManaged
statement to full set-equality (was subset `in`), per a-ratchet-may-only-tighten:
a future third verb on that role-ARN statement now reds the test instead of
landing un-caught. Test-only, permission-neutral.
@iamwhatever
iamwhatever force-pushed the fix/cloud-iam-policy-under-6144 branch from 18f22f1 to 5488b80 Compare September 23, 2026 22:21
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Sep 23, 2026
@chenmingwei23
chenmingwei23 merged commit 8bb4c55 into main Sep 24, 2026
137 of 138 checks passed
@chenmingwei23
chenmingwei23 deleted the fix/cloud-iam-policy-under-6144 branch September 24, 2026 01:17
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 24, 2026
@dwu96 dwu96 removed the drive-to-green PR claimed by drive-to-green pipeline label Sep 24, 2026
wknight1 pushed a commit to wknight1/KiroCrew that referenced this pull request Oct 5, 2026
…odotdev#13029)

* fix(cloud): shrink launcher IAM policy under IAM's 6144-char cap

The `kirocrew cloud iam-policy` output was 6467 characters whitespace-free,
over AWS's non-adjustable 6,144-char managed-policy quota, so applying the
printed JSON failed with LimitExceeded for any non-admin operator.

Shrink to 5818 whitespace-free by merging statements that share Effect +
Resource + Condition (permission-neutral by construction):

- Ec2SecurityGroupRulesTagged + Ec2DestructiveTagged + Ec2LifecycleTagged ->
  Ec2ManagedResourceMutateTagged (Resource "*", managed-tag condition)
- IamPutRolePolicyForInstance + IamTagRoleOnManaged ->
  IamPutRolePolicyAndTagRoleOnManaged (role ARN, managed-tag condition)
- CloudFormationRead + TagDiscovery + Ec2Discovery + Route53DnsPreflight ->
  CloudFormationAndResourceDiscovery (Resource "*", no condition)

The effective permission set is identical before and after: a new test
flattens the pre-shrink policy (kept as a fixture) and the current policy into
canonical (Effect, Action, Resource-form, Resource, Condition) tuple sets and
asserts equality. A size-gate test asserts the whitespace-free length is at
most 6144 (citing the AWS quota) and at most 5900 for headroom so EC2 Spot
(kirodotdev#3355, roughly +152 chars) still fits.

Sid-pinning tests updated to the merged statement names; no assertions dropped.

* chore: prune black baseline entries the gate flags

Remove test/test_knowledge.py from .github/black-baseline.txt: the file
graduated to black-clean on main, so the baselined-file gate fails until
the stale entry is pruned (0 new offenders, 1 graduated entry). Same
base-defect fix applied to the other wave-3 PRs.

Also tighten two assertions on the merged IamPutRolePolicyAndTagRoleOnManaged
statement to full set-equality (was subset `in`), per a-ratchet-may-only-tighten:
a future third verb on that role-ARN statement now reds the test instead of
landing un-caught. Test-only, permission-neutral.

---------

Co-authored-by: iamwhatever <iamwhatever@users.noreply.github.com>
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.

3 participants