Skip to content

fix(cloud): grant ec2:RevokeSecurityGroupEgress so cloud launch can create its security group - #15478

Closed
jcjaskula-aws wants to merge 1 commit into
kirodotdev:mainfrom
jcjaskula-aws:fix/iam-revoke-security-group-egress
Closed

jcjaskula-aws wants to merge 1 commit into
kirodotdev:mainfrom
jcjaskula-aws:fix/iam-revoke-security-group-egress

Conversation

@jcjaskula-aws

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Make kirocrew cloud launch able to create its stack with the launcher policy that kirocrew cloud iam-policy prints.

It cannot today. The launch fails at InstanceSecurityGroup and rolls the stack back, on every launch, in every account:

CREATE_FAILED  InstanceSecurityGroup
  Resource handler returned message: "You are not authorized to perform this
  operation. User: arn:aws:sts::<ACCOUNT>:assumed-role/<LAUNCHER> is not
  authorized to perform: ec2:RevokeSecurityGroupEgress on resource:
  arn:aws:ec2:us-east-1:<ACCOUNT>:security-group/sg-0fa7d65063d4d3789 because no
  identity-based policy allows the ec2:RevokeSecurityGroupEgress action."
  (HandlerErrorCode: AccessDenied)
ROLLBACK_COMPLETE  kirocrew-kc-<id>

Ec2ManagedResourceMutateTagged (src/kiro_crew/cloud/iam.py:412) grants AuthorizeSecurityGroupEgress, AuthorizeSecurityGroupIngress and RevokeSecurityGroupIngress. One half of a symmetric pair is absent.

Why it matters

The generated policy is the documented path for anyone launching a remote crew without admin credentials, and this blocks it unconditionally — there is no account, region or option combination where the current policy reaches the Instance resource. Each attempt costs a full CloudFormation create-and-rollback cycle, and the error names an action the operator cannot find anywhere in the policy they just applied, so the natural response is to suspect their own setup.

Not a goal

What changed (motivation → approach → change)

src/kiro_crew/cloud/templates/kirocrew-ec2.yaml:199 declares an inline SecurityGroupEgress block on the instance security group. EC2 attaches an implicit allow-all egress rule to every new security group, so when a CloudFormation security group declares egress, CloudFormation must revoke that implicit rule before applying the declared one. Every launch therefore calls ec2:RevokeSecurityGroupEgress — an action no statement grants.

The fix is one action added to Ec2ManagedResourceMutateTagged, beside its Authorize counterpart. Putting it there rather than in a new statement matters: it inherits that statement's aws:ResourceTag/kirocrew:managed=true condition, so it applies only to security groups this launcher created — identical scoping to the RevokeSecurityGroupIngress verb next to it, and no new reach.

Three files move with it, because the change would otherwise land red or leave the docs wrong:

  • test/test_cloud_iam.py — the exact set-equality pin on the merged action list (deliberately strict, so action creep on a Resource: "*" statement cannot land green) and the destructive-verb tag-scope assertion.
  • test/fixtures/cloud_iam_policy_pre_shrink.json — the permission-neutrality test compares pre-shrink and current policies as tuple sets for equality, so the verb belongs in the fixture's Ec2DestructiveTagged too (same Effect, Resource and Condition, so the tuple matches).
  • docs/system-specs/modules/cloud.md:477 — the documented destructive-verb list, which had the same blind spot as the code.

Backwards compatibility

Compatible: this adds one Allow action to an existing tag-conditioned statement. Nothing that works today stops working, and an operator running the previous policy is unaffected until they regenerate it.

Tests

  • test_template_declared_egress_requires_the_egress_revoke (new) — reads the CloudFormation template and asserts that a declared SecurityGroupEgress block is paired with the egress revoke grant. Removing either side lands red instead of surfacing as a failed launch in a user's account. It reaches the template through pathlib.Path(iam.__file__), not the working directory, to satisfy the Working-Directory Path Gate.
  • The existing set-equality pin on Ec2ManagedResourceMutateTagged's action list and the destructive-verb tag-scope assertion are updated to include the new verb, so both keep failing on an unintended action rather than silently absorbing it.
  • Full file: 65 passed in test/test_cloud_iam.py, including the permission-neutrality tuple comparison and both policy-size gates.
  • Compact policy size 5,818 → 5,850 chars, under the 6,044 headroom gate and IAM's 6,144 managed-policy cap.

Manual verification

Verified against a live launch. With the current policy the stack rolls back at InstanceSecurityGroup with the error quoted above; with this action added, the security group, instance role and instance profile all reach CREATE_COMPLETE and the launch proceeds to the Instance resource — where it then hits #15473, which is why that one is filed separately.

Related Issues

Sibling defects from the same pass: #15473, #15474, #15475.

Pattern harvest

Rule candidate: review-prompt
Pattern: an IAM policy grants one half of an Authorize/Revoke (or Create/Delete, Put/Get) pair for a resource the same codebase declares, so the paired call is only discovered at runtime as AccessDenied. The generalizable form is to check a generated policy against the calls the shipped infrastructure template forces, rather than against the list of calls the code makes directly — the CloudFormation-implied revoke has no call site in this repository to notice.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

The launcher policy printed by `kirocrew cloud iam-policy` grants
ec2:AuthorizeSecurityGroupEgress and ec2:RevokeSecurityGroupIngress but
not ec2:RevokeSecurityGroupEgress, while kirocrew-ec2.yaml declares an
inline SecurityGroupEgress block on the instance security group.

CloudFormation must revoke the implicit allow-all egress rule EC2
attaches to a new security group before it can apply a declared egress
rule, so every launch calls RevokeSecurityGroupEgress and stops at
InstanceSecurityGroup with AccessDenied, rolling the stack back. The
grant lands in Ec2ManagedResourceMutateTagged beside its Authorize
counterpart, so it stays gated on aws:ResourceTag/kirocrew:managed=true
and applies only to security groups this launcher created.

The added test reads the template and asserts the pairing, so removing
the grant or the declared egress block lands red rather than surfacing
as a failed launch.
@jcjaskula-aws
jcjaskula-aws requested a review from a team as a code owner September 30, 2026 16:22
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Sep 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — ✅ PASS

Design-level review of 1483ee58ca5fa563b5ac0659285fc00cd6857f5d via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Nothing to check.

[DESIGN-REVIEWED] 1483ee5

Description read

[DESCRIPTION-READ] 81d4d94064fff1fa8aa0f75727c91b69e4a557b457b85be74b76a8501e87523b

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ✅ PASS

Premise-level review of 1483ee58ca5fa563b5ac0659285fc00cd6857f5d via the fork AI-review pipeline — 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: PASS

Verify the manual claim: only the author's live launch shows the stack now passes InstanceSecurityGroup; the added test pins the grant, not the launch.

The defect is confirmed on base: src/kiro_crew/cloud/iam.py:414-424 grants AuthorizeSecurityGroupEgress with no revoke counterpart, kirocrew-ec2.yaml:199 declares inline egress that forces CloudFormation to revoke EC2's implicit allow-all rule, and RevokeSecurityGroupEgress appears nowhere on base (grepped repo-wide: 0 matches), so the new test fails on base — checkable provenance. Every companion edit is declared and mechanically required: the set-equality pin and destructive-verb loop would land red without updating, the pre-shrink fixture feeds the permission-neutrality tuple comparison, and the cloud.md list update satisfies the same-commit spec invariant.

What this change ships

Inventory (6 items) — 6 justified

Intent: FIX — make kirocrew cloud launch succeed with the policy kirocrew cloud iam-policy prints, which today rolls back every stack at InstanceSecurityGroup.

  1. Generated launcher policy now grants ec2:RevokeSecurityGroupEgress, tag-conditioned like its siblings — justified
  2. New test ties the template's declared egress block to the revoke grant — justified
  3. Existing action-set pin and destructive-verb assertion extended to the new verb — justified
  4. Pre-shrink fixture gains the verb so the permission-neutrality pin stays exact — justified
  5. cloud.md destructive-verb list now names the egress revoke — justified
  6. Comment in iam.py recording why both rule directions need Authorize and Revoke — justified

[FIRST-PRINCIPLES-REVIEWED] 1483ee5

Description read

[DESCRIPTION-READ] 807f105fac5cd565f444333bf4caefd5e0e28664294877ffbd4a0a55b7ea5389

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ⚠️ review incomplete

Reviewed 1483ee58ca5fa563b5ac0659285fc00cd6857f5d via the fork AI-review pipeline; updated in place on each push.

No completed GPT verdict for this commit; see the Fork GPT 5.6 Review job logs.

Captured review output -- no verdict stamp for this commit, so this is NOT a verdict

Incomplete review: pass(es) 1 2 did not complete.

The pass-2 verdict was not published because both calls are required. Re-run the workflow for a complete review.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5 Review (fork) — ✅ no blocking findings

Reviewed 1483ee58ca5fa563b5ac0659285fc00cd6857f5d via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 1483ee5

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running labels Sep 30, 2026
@jcjaskula-aws

Copy link
Copy Markdown
Contributor Author

Superseded by #15237, merged at 23:22 UTC today, which lands the same fix: ec2:RevokeSecurityGroupEgress is granted in Ec2ManagedResourceMutateTagged under the same aws:ResourceTag/kirocrew:managed gate (cloud/iam.py:436), the destructive-verb list in docs/system-specs/modules/cloud.md:557 is updated with the CloudFormation explanation, and the coverage there is broader than what this PR added: test_revoke_security_group_egress_is_granted_tag_scoped plus the schema-driven test_cloud_iam_cfn_schema.py, which derives the required permissions from the public CloudFormation schemas rather than pinning one verb by hand.

Closing as redundant. One assertion here is not covered upstream, in case it is wanted separately: this PR guarded the grant with a check that the template still declares inline SecurityGroupEgress, so the reason the grant exists is verified rather than assumed, and the grant is known to be removable if the template ever stops declaring it.

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 1, 2026
@jcjaskula-aws
jcjaskula-aws deleted the fix/iam-revoke-security-group-egress branch October 6, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cloud launch: iam:PassRole denied because IamPassRoleToEc2 gates on aws:ResourceTag, which AWS documents as unreliable for PassRole

1 participant