Skip to content

reaper: publish the Lambda artifact, and grant the permissions it actually uses - #635

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/625-publish-reaper-artifact
Oct 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/625-publish-reaper-artifact

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Refs #625. PR 1 of 2 — the prerequisites. PR 2 adds spawn reaper deploy, which cannot download an artifact that doesn't exist yet, so this has to land and be released first.

1. Publish the artifact

Nothing could install the reaper. lambda/ttl-reaper is deployed by hand from a CFN template in this repo, and spawn's releases carry only spawn and spored (grep -i reaper .github/workflows/ → nothing). So an account whose organization forbids cross-account trust — where the reaper's "assume a role in your account" model is simply unavailable — had no path to a backstop at all.

A GoReleaser before hook builds ttl-reaper_lambda_linux_arm64.zip; release.extra_files attaches it. Same model lagotto uses for its capacity-poller.

Two details that matter and differ from a copy-paste of lagotto's hook:

  • It builds the package (.), not main.go. The reaper is three files, and go build main.go fails outright here:
    ./main.go:533:21: undefined: accountOutcome
    
    The Makefile carries the same warning from Wire DNSExpiryEligible into the reaper: the prober can now prove what the unmanaged-subdomain report refused to guess #466. lagotto's hook does use main.go and works only because its lambda is currently single-file — it will break the day that changes. Worth filing there; I didn't touch it from here.
  • zip -j, so bootstrap sits at the archive root. A nested path yields Runtime.InvalidEntrypoint at invoke time — after a successful deploy, which is the worst place to discover it.

Verified locally: bootstrap at root, 25.2 MiB zipped / 66.2 MiB unzipped (inside Lambda's 50/250 MiB limits), ELF 64-bit ARM aarch64 matching the template's Architectures: [arm64].

Also widened .gitignore: lambda/*.zip matches only the lambda/ directory itself, so the new artifact sat untracked and was one git add -A away from committing 25 MiB.

2. Grant the permissions the reaper actually calls

The function's policy granted ec2:Describe*/TerminateInstances and no fsx: or ssm: actions. The reaper's code calls:

call purpose granted?
fsx:DescribeFileSystems, fsx:DeleteFileSystem the #210 ephemeral-orphan net no
ssm:SendCommand, ssm:GetCommandInvocation REAPER_GRACEFUL shutdown no

In cross-account mode those run under the assumed spawn-ttl-reaper-ec2 role, which does grant FSx — I verified against the live role in a spore-launching account. So the gap only ever affected scan-self, which is precisely the mode a self-hosted reaper uses, and it failed silently: instances got terminated while the orphaned 1200 GiB filesystem #613 was reported for was quietly left behind.

pkg/reaperiam now holds the set as data, shared by the deploy path (PR 2) and by a drift test that reads the reaper's own source — its *API interface declarations and SDK client call sites — and fails if any discovered call has no grant. That's the structural end of the class behind #622 and lagotto #149/#151/#153, and it's why this belongs here rather than being bolted on after the deploy command.

Confirmed the gate fails with fsx: removed:

the reaper calls AWS actions the scan-self policy does not grant: fsx:DeleteFileSystem, fsx:DescribeFileSystems

Two things the tests caught in my own work

  • The reverse test found a bug in my discovery regex. TestPolicyGrantsNothingTheReaperDoesNotCall flagged three ec2 grants as uncalled — because ^type ([a-z]+)API never matched ec2API (the digit). So the gate was silently not checking the three most important actions, and would have passed vacuously. Without writing the "no unused grants" direction I'd have shipped a gate that didn't check TerminateInstances.
  • ec2:DescribeTags is never called. The template and the cross-account role both grant it; the reaper reads tags from the DescribeInstances response (tagMap(inst.Tags)). Dropped from the scan-self set — harmless where it is, but an unused grant is a leftover, and the test keeps the list honest in both directions.

The template's own ScanSelf policy is fixed too, so the hand-deployed path stops differing from the CLI one. sam validate: "is a valid SAM Template."

Gates

gofmt · go vet ./... · go build ./... · go test ./... all green · make check-docs clean · go mod tidy clean in root and lambda/ttl-reaper · sam validate passes · YAML parses.

goreleaser check reports the config valid with a pre-existing brews deprecation — identical output on clean main, so not from this change.

Suggested bump: PATCH (a new release asset plus a permission fix; no CLI surface change). A release is needed before PR 2's download-by-version path can be exercised — PR 2 will carry an --artifact escape hatch so it's testable in the meantime.

…it actually uses

Two prerequisites for a self-hostable reaper (#625).

1. Publish the artifact. Nothing could install the reaper: lambda/ttl-reaper was
   deployed by hand from a CFN template in this repo, and spawn's releases carried
   only spawn and spored. So an account whose org forbids cross-account trust — where
   the reaper's 'assume a role in your account' model is simply unavailable — had no
   path to a backstop at all. A GoReleaser before-hook builds
   ttl-reaper_lambda_linux_arm64.zip and release.extra_files attaches it, mirroring
   how lagotto publishes its capacity-poller.

   The hook builds the PACKAGE ('.'), not main.go. The reaper is three files and
   'go build main.go' fails outright here ('undefined: accountOutcome'); the Makefile
   carries the same warning from #466. lagotto's equivalent hook does use main.go and
   works only because its lambda is currently single-file — worth filing there.
   'zip -j' keeps bootstrap at the archive root, or Lambda throws
   Runtime.InvalidEntrypoint at invoke time, after a successful deploy.

   Also widened .gitignore: 'lambda/*.zip' matched only the lambda/ directory itself,
   leaving the new 25 MiB artifact one 'git add -A' from being committed.

2. Grant what the reaper calls. Its execution policy granted ec2 describe/terminate
   and NO fsx: or ssm: actions, while the code calls fsx:DescribeFileSystems/
   DeleteFileSystem (#210 ephemeral-orphan net) and ssm:SendCommand/
   GetCommandInvocation (REAPER_GRACEFUL). Cross-account mode runs those under the
   assumed role, which does grant fsx — verified against the live role — so the gap
   only bit scan-self, and silently: terminate worked while the orphaned 1200 GiB
   filesystem #613 was reported for was left behind.

   pkg/reaperiam holds the set as data, and a test discovers the reaper's AWS calls by
   reading its own *API interfaces and client call sites, failing if any lacks a
   grant. Confirmed it fails with fsx removed. The reverse test — granting nothing
   uncalled — caught a bug in my own discovery regex ([a-z]+ never matched 'ec2'
   because of the digit, so the three ec2 actions were silently unchecked) and
   established that ec2:DescribeTags, which the template and cross-account role both
   grant, is never called: the reaper reads tags from the DescribeInstances response.

   The template's own ScanSelf policy is fixed too, so the hand-deployed path stops
   differing from the CLI one. sam validate passes.

Refs #625
@scttfrdmn
scttfrdmn merged commit e13e763 into main Oct 3, 2026
6 checks passed
@scttfrdmn
scttfrdmn deleted the feat/625-publish-reaper-artifact branch October 3, 2026 00:56
scttfrdmn added a commit that referenced this pull request Oct 3, 2026
…our own account (#636)

* feat(reaper): spawn reaper deploy/arm/status/teardown — run the reaper in your own account

The reaper's normal shape is a Lambda in spore.host's infra account assuming a role in
each launching account. That requires the launching account to trust an EXTERNAL
principal, which many organizations forbid outright — attempting it in one produced an
automated security finding in 12 minutes and the org's tooling flipped the trust policy
to Deny. Least privilege did not help; the objection was the external principal itself.

So those accounts had no backstop at all. spored enforces TTL from INSIDE each
instance, so nothing reclaimed a stopped instance past its deadline or a filesystem
that outlived its instance. One sat stopped for 13 days, still billing EBS. #624 made
that detectable; this makes it fixable.

'spawn reaper deploy' converges role -> artifact -> function -> EventBridge rule with
direct API calls, no CloudFormation. An EventBridge RULE rather than Scheduler because
a rule needs no separate invoke role — one fewer principal for a lagotto#170-style
authorization bug to hide in.

It deploys UNARMED (REAPER_DRY_RUN=true). Arming is a separate confirmed step, because
this terminates instances and a single command should not start doing that in an
account whose tagging it has never been seen to evaluate. Re-deploying an ARMED reaper
preserves that state: silently reverting it to dry-run would remove protection the
operator deliberately enabled.

cmd/ constructs no SDK clients — TestNoNewAWSSDKImportsInCmd caught the first attempt,
and writing reaperdeploy.New(cfg) was the right answer to that gate rather than adding
four services to its allowlist.

Verified end to end on a real account (the one with no coverage):
  doctor '!! not covered' -> deploy -> doctor 'OK in-account reaper spawn-ttl-reaper-selfscan'
  -> dry-run invoke: FSxScanned:3 Errors:0, reaped nothing (the fsx grants from #635 are
     what make that scan succeed rather than AccessDeny)
  -> arm -> 1-minute-TTL instance -> 'REAPED i-0befb34f37c81fbe4 — ttl-deadline', dry-run=false
  -> teardown removed all 7 resources -> doctor back to '!! not covered'
Account left exactly as found; leak-checked (no instances, no function, no role, 0
available volumes, test bucket removed).

Two defects found and fixed in my own code along the way:
- Teardown claimed 'removed rule X' on a second run when the rule was already gone:
  EventBridge's DeleteRule is idempotent and returns success for a missing rule, so
  err==nil proved nothing. It now checks DescribeRule first. Claiming work that did not
  happen is the same defect class as the rest of this work, just smaller.
- The IAM-propagation wait made the unit suite take 40s; it is now injectable and the
  suite runs in 0.18s.

Also: adding the eventbridge SDK to the root module required 'go mod tidy' in
lambda/ttl-reaper (own go.mod + replace ../..), which would otherwise have failed
govulncheck CI with 'updates to go.mod needed'.

Refs #625

* fix(ci): tidy the lambda modules the root dependency change affected, and classify 'reaper' for the Tier 0 gate

Two CI failures, both my doing.

1. 'go: updates to go.mod needed' in lambda/sweep-orchestrator (and alert-handler,
   autoscale-orchestrator), failing both govulncheck and the lambda build job. I had
   identified these three as already untidy on main and reasoned they were
   pre-existing and out of scope — correct until I added the eventbridge SDK to the
   root module. They carry 'replace ../..', so changing the root's requirements
   changed what they resolve, pushing them from untidy-but-resolvable to
   needs-tidy-to-build. Changing the root module makes them my problem; tidied.

   Every module in the repo is now tidy, which it was not before.

2. TestTier0_CommandCoverageGate: a new top-level command must be classified as Tier
   0-covered or explicitly deferred with a reason, so coverage decisions can't be
   skipped silently. 'reaper' is deferred: Substrate doesn't route Lambda tag ops
   (substrate#1142), and the value of the command is a scheduled function that
   actually terminates things, which no emulator can demonstrate. The reason records
   the real-account verification that was done instead, and where the pure logic is
   unit-tested.

Refs #625

* chore: don't rebuild the tracked autoscale-orchestrator binary

A compiled 22 MiB binary is checked into the repo at
lambda/autoscale-orchestrator/autoscale-orchestrator and is NOT in .gitignore, so
running 'go build ./...' in that module to verify the tidy rewrote it and the previous
commit carried a 23 MB binary diff. Restored to main's bytes.

The tracked binary is a pre-existing problem — a build artifact in version control,
unlike every other lambda whose binary is gitignored — but removing it is someone's
deliberate-or-not decision about deploy tooling, not something to fold into this PR.
Noted on the issue instead.
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.

1 participant