Skip to content

fix(reaper): grant ssm: on the cross-account role so REAPER_GRACEFUL works there - #657

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/652-crossaccount-ssm-grants
Oct 4, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/652-crossaccount-ssm-grants

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #652.

The gap

REAPER_GRACEFUL asks spored to shut down cleanly over SSM before the external terminate. In cross-account mode — the production configuration (ScanSelf=false, RoleArns → spawn-ttl-reaper-ec2) those calls run under the assumed role, which granted no ssm: actions at all.

#625 added them to the Lambda's own execution role, which covers scan-self only. So the asymmetry didn't close — it inverted: scan-self got the grants, and the mode actually deployed didn't.

Checked the consequence before fixing

A missing grant can mask a worse bug, so that came first. It doesn't here: tryGracefulPreStop is best-effort and r.terminate(...) after it always runs, so #72's hard-deadline guarantee held throughout. Graceful mode silently became an immediate hard kill — degraded, not dangerous. Good to know before adding a grant that changes behaviour.

Why it survived #625's drift gate

That gate only ever read the scan-self policy. Both roles are now checked against the same discovered call set, plus a second test that fails if either role grants something the other doesn't — they do the same per-account work, one natively and one assumed, so a one-sided grant means a feature that works in one deployment mode and silently no-ops in the other.

Confirmed by removing the new grants: both gates fail, naming ssm:SendCommand and ssm:GetCommandInvocation.

Scoping

SendCommand is limited to spawn:managed instances — the same tag rail as TerminateInstances — plus the single AWS-RunShellScript document. GetCommandInvocation has no resource-level ARN and only reads the result of a command this role issued.

A contradiction that wasn't

pkg/reaperiam read as though fsx:DeleteFileSystem couldn't be tag-conditioned, while the cross-account role tag-conditions it. I flagged that as a discrepancy on #613. Both are true — they're different keys. FSx exposes no service key fsx:ResourceTag for the action; the global aws:ResourceTag/${TagKey} does work. Verified by IAM policy simulation:

tag present and "true"   -> allowed
tag absent               -> implicitDeny
tag present and "false"  -> implicitDeny

Deliberately not adopting the condition in scan-self yet. The simulation proves IAM evaluates the key; it cannot prove FSx populates it at request time, because the simulator takes that context from the caller. If FSx doesn't supply it, the condition fails closed — deletes denied, filesystems leak. That's the safe direction but still a regression. Cross-account already carries that dependency; extending it to the path #625 exists to make work should wait for a real ephemeral-FSx reap (#613). The comment now says all of this instead of implying the IAM rail covers something it doesn't.

Verification

aws cloudformation validate-template passes; gofmt, go vet, full go test -short ./..., and semgrep scan --config=auto --error . (0 findings) all clean. No deploy — the cross-account role lives in other accounts and rolling it out is a separate, explicit action.

@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…works there

The graceful path asks spored to shut down cleanly over SSM before the external
terminate. In cross-account mode — the PRODUCTION configuration (ScanSelf=false,
RoleArns pointing at spawn-ttl-reaper-ec2) — those calls run under the assumed
role, and that role granted no ssm: actions at all.

So the asymmetry did not close, it inverted: scan-self got the grants and the
mode actually deployed did not.

Checked the consequence before fixing, because a missing grant can mask a worse
bug. It does not here: tryGracefulPreStop is best-effort and
`r.terminate(...)` after it ALWAYS runs, so #72's hard-deadline guarantee held
throughout. Graceful mode silently became an immediate hard kill — degraded, not
dangerous.

The #625 drift gate only ever read the scan-self policy, which is precisely why
this survived it. Both roles are now checked against the same discovered call
set, plus a second test that fails if either role grants something the other
does not — the two do the same per-account work, one natively and one assumed, so
a one-sided grant means a feature that works in one deployment mode and silently
no-ops in the other. Confirmed by removing the new grants: both gates fail,
naming ssm:SendCommand and ssm:GetCommandInvocation.

Scoping: SendCommand is limited to spawn:managed instances (the same tag rail as
TerminateInstances) plus the single AWS-RunShellScript document;
GetCommandInvocation has no resource-level ARN and only reads the result of a
command this role issued.

Also corrects a misleading comment that read as a contradiction. pkg/reaperiam
said fsx:DeleteFileSystem "cannot be tag-conditioned", while the cross-account
role tag-conditions it. Both are true — they are DIFFERENT keys. FSx exposes no
service key fsx:ResourceTag for the action; the global aws:ResourceTag/${TagKey}
does work. Verified by IAM policy simulation:

  tag present and "true"   -> allowed
  tag absent               -> implicitDeny
  tag present and "false"  -> implicitDeny

Deliberately NOT adopting the condition in scan-self yet. The simulation proves
IAM evaluates the key; it cannot prove FSx populates it at request time, because
the simulator takes that context from the caller. If FSx does not supply it the
condition fails closed — deletes denied, filesystems leak — which is the safe
direction but still a regression. Cross-account already carries that dependency;
extending it to the path #625 exists to make work should wait for a real
ephemeral-FSx reap (#613).

CloudFormation validate-template passes. gofmt, vet, full short tests and
semgrep (0 findings) clean.

Fixes #652
@scttfrdmn
scttfrdmn force-pushed the fix/652-crossaccount-ssm-grants branch from 460ea9a to 9b989d1 Compare October 4, 2026 02:22
@scttfrdmn
scttfrdmn merged commit 24b36e7 into main Oct 4, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/652-crossaccount-ssm-grants branch October 4, 2026 02:32
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.

REAPER_GRACEFUL cannot work cross-account: the spawn-ttl-reaper-ec2 role grants no ssm: actions

1 participant