Skip to content

feat(reaper): delete unused per-task spawn-instance-* IAM roles; repair empty reused profiles (#1300, #1318) - #1321

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/1300-reap-instance-roles
Oct 26, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/1300-reap-instance-roles

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1300
Fixes #1318

Per-task spawn-instance-<hash> IAM roles and instance profiles (one per distinct policy config, minted by pkg/aws.CreateOrGetInstanceProfile) now have a deleter, per spore-host/spore-host#610: the TTL reaper (new REAPER_INSTANCE_ROLES, default report) and spawn cleanup --instance-roles, both through one predicate in the new pkg/instancerole.

The rule (instancerole.Decide)

A role and its same-name profile are deleted only when ALL hold:

  • name starts spawn-instance- and is not spored-instance-role/spored-instance-profile (refused by name too);
  • the role is tagged spawn:managed=true, and the profile (if it still exists) too;
  • the role is in no other instance profile;
  • no instance in ANY region enabled in the account (ec2:DescribeRegions, AllRegions=false, merged with REAPER_REGIONS), in state pending/running/stopping/stopped/shutting-down, has the profile attached (read from IamInstanceProfile.Arn in the DescribeInstances response, not an ARN filter, so a filter that silently matched nothing cannot read as "unused"); if the region list or any region cannot be read, nothing is deleted;
  • the role's and the profile's CreateDate are at least 24 h ago (a young profile is a launch recreating it after a partial delete);
  • RoleLastUsed is absent or at least 24 h ago.

Delete order: RemoveRoleFromInstanceProfile, DeleteRolePolicy (each inline), DetachRolePolicy (each managed), DeleteInstanceProfile, DeleteRole; stops at the first failure; NoSuchEntity counts as done.

Untagged legacy roles: none exist from spawn. buildIAMTags has written spawn:managed=true on these roles and profiles since the commit that introduced the spawn-instance-%s name (spore-host 52ae503, 2026-01-14), so an untagged spawn-instance-* role is not spawn's and is kept. No name-prefix adoption rule was added.

Reaper

  • Once per account per run (IAM is global), after the regional scans; in-use check over every enabled region of the account (new regionsFor on each account), not just REAPER_REGIONS.
  • REAPER_INSTANCE_ROLES / InstanceRoles / INSTANCE_ROLES / spawn reaper deploy --instance-roles: off|report|reap, default and typo = report (new deletion behaviour). DryRun wins. Preserved across deploys on all three paths (PARAM_MAP + merge, reaperconfig.Settable census, template default).
  • Logs WOULD delete role … / DELETED role … (DELETED added to ActionSentinels so idleness counts it). Summary: instance_roles_scanned, _deleted, _would_delete, _skipped, _accounts_denied (AccessDenied on ListRoles = member role not redeployed; not counted as Errors).

IAM (pkg/reaperiam, SAM ScanSelf branch, cross-account member role — held equal by the existing drift gates)

Allow ec2:DescribeRegions (added to the existing read-only Describe statement) on *
Allow iam:ListRoles                                   on *
Allow iam:GetRole, iam:ListInstanceProfilesForRole,
      iam:ListRolePolicies, iam:ListAttachedRolePolicies on arn:aws:iam::*:role/spawn-instance-*
Allow iam:GetInstanceProfile                          on arn:aws:iam::*:instance-profile/spawn-instance-*
Allow iam:RemoveRoleFromInstanceProfile, iam:DeleteRolePolicy,
      iam:DetachRolePolicy, iam:DeleteRole            on arn:aws:iam::*:role/spawn-instance-*
      if StringEquals iam:ResourceTag/spawn:managed = true
Allow iam:RemoveRoleFromInstanceProfile, iam:DeleteInstanceProfile
                                                      on arn:aws:iam::*:instance-profile/spawn-instance-*
      if StringEquals iam:ResourceTag/spawn:managed = true

(In both CFN templates the account is ${AWS::AccountId} via !Sub.) No mutating IAM action on *. New gate TestIAMMutationsAreScopedToPerTaskRoles checks all three copies: every discovered mutating iam: action is on exactly those two ARNs, carries the tag condition, matches none of spored-instance-role/-profile, spawn-ttl-reaper-ec2, spawn-ttl-reaper-selfscan-role, spawn-pool-worker-*, a user role; and does match a real spawn-instance role and profile.

Not verified against AWS (no AWS calls in this work): IAM simulation of allow + deny, and one real PutRolePolicy of the scan-self document, are still needed before release (memory: ${aws:PrincipalAccount} passed every static check and broke every launch in #1069).

#1318: empty reused profile

CreateOrGetInstanceProfile, when it finds an existing profile, now checks its Roles: re-attaches the role if the profile is empty (LimitExceeded from a concurrent re-add is fine), and errors if it holds a different role. Gate TestCreateOrGetInstanceProfile_RepairsAnEmptyProfile (substrate) fails against a compiling revert (0 roles). The reaper side keeps a pair whose profile is under 24 h old.

CLI

  • spawn cleanup --instance-roles [--dry-run] [--yes]: previews with the same Sweep(reap=false), confirms, then re-sweeps with reap (each role re-decided at delete time). Always checks every enabled region via the same instancerole.EnabledRegions, account-wide.
  • spawn footprint names the deleters under the Global section.

Gates, each watched failing against a compiling revert

in-use / young / recently-used / untagged / shared-role checks each dropped from Decide; shared role made a candidate; profile deleted before role removed; report mode deleting; dry run not winning; unreadable region skipped; stopped dropped from live states; unset parsing to reap; template default reap; InstanceRoles dropped from PARAM_MAP; dropped from Settable; flag removed; env not read; delete grant widened to role/*; condition removed; cross-account grant widened to spawn-*; SAM grant on *; sweep not called from run(); footprint counting all roles. All 23 caught. Second round (enabled regions, #1318, after rebase onto #1322): reaper using REAPER_REGIONS only (an instance in ap-south-1, outside REAPER_REGIONS, holds the profile), region-list error ignored, AllRegions=true, empty region list accepted, profile age dropped, DescribeRegions dropped from each of the 3 policy copies, the #1318 re-attach disabled, plus census/PARAM_MAP/DELETED sentinel/in-use/IAM-scope re-checked on the merged lists. All caught.

Checks

make check: all tests pass incl. nested modules; check-build-tags fails on /private/tmp paths, and fails the same on unmodified origin/main. make lint 0 issues; make check-docs clean; semgrep scan --config=auto --error . 0 findings. No pkg/aws exported symbol changed (comment only), so no api-snapshot.

Follow-up filed: #1319 (spawn-pool-worker roles of an undrained pool are never deleted).

Rebased onto #1322 (empty buckets): list conflicts resolved keeping both sides (Settable, flags, PARAM_MAP/echo/census, Summary, account struct, knownAWSServices + iamActionFor); one DELETED sentinel shared by both sweeps.

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.53608% with 45 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/instancerole/instancerole.go 85.36% 24 Missing ⚠️
cmd/cleanup.go 71.42% 18 Missing ⚠️
pkg/aws/iam.go 83.33% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn force-pushed the feat/1300-reap-instance-roles branch from 753a269 to fb19441 Compare October 26, 2026 21:29
@scttfrdmn scttfrdmn changed the title feat(reaper): delete unused per-task spawn-instance-* IAM roles and profiles (#1300) feat(reaper): delete unused per-task spawn-instance-* IAM roles; repair empty reused profiles (#1300, #1318) Oct 26, 2026
…rofiles; repair empty reused profiles (#1300, #1318)

pkg/instancerole holds the one rule (Decide) and the delete, shared by the
TTL reaper (REAPER_INSTANCE_ROLES / InstanceRoles / INSTANCE_ROLES /
spawn reaper deploy --instance-roles, default report) and
spawn cleanup --instance-roles. A role and its profile go only when both are
tagged spawn:managed=true and 24h old, no instance in any enabled region
(DescribeRegions; pending through stopped) has the profile, and the role is
unused for 24h; an unreadable region or region list means nothing is deleted.
spored-instance-role is never touched. IAM grants in pkg/reaperiam, the SAM
ScanSelf branch and the member role allow the deletes only on spawn-instance-*
tagged spawn:managed=true. CreateOrGetInstanceProfile re-attaches the role to
an existing empty profile.

Refs #1300
Refs #1318
@scttfrdmn
scttfrdmn force-pushed the feat/1300-reap-instance-roles branch from fb19441 to 1570b98 Compare October 26, 2026 21:45
@scttfrdmn
scttfrdmn merged commit 1b9f5b6 into main Oct 26, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the feat/1300-reap-instance-roles branch October 26, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant