Skip to content

reaper: spawn reaper deploy/arm/status/teardown — run the reaper in your own account - #636

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/625-reaper-deploy-command
Oct 3, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/625-reaper-deploy-command

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Refs #625. PR 2 of 2 — the commands. PR #635 (merged) publishes the artifact and fixed the permission set.

What this unblocks

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 role's trust policy to Deny. Least privilege didn't 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      # installs it, in DRY RUN
$ spawn reaper status      # deployed? armed? on what schedule?
$ spawn reaper arm         # start actually reclaiming
$ spawn reaper teardown

Design points worth reviewing

  • Deploys unarmed. REAPER_DRY_RUN=true; arming is a separate confirmed step. This terminates instances, and one command should not start doing that in an account whose tagging it has never been seen to evaluate. spawn's own reaper was rolled out in dry-run first.
  • Re-deploying an armed reaper does not disarm it. Silently reverting to dry-run would remove protection the operator deliberately enabled — the test for this is the one I'd most want reviewed.
  • EventBridge Rule, not Scheduler. A rule needs no separate IAM invoke role; Scheduler would add another principal and another trust policy for a lagotto#170-style bug to hide in.
  • No CloudFormation, per lagotto#154 and the standing preference.
  • cmd/ constructs no SDK clients. TestNoNewAWSSDKImportsInCmd caught my first attempt, which built four clients inline. Writing reaperdeploy.New(cfg) was the right answer to that gate; adding four services to its allowlist would have been the wrong one.
  • The function name is load-bearing. It must keep the spawn-ttl-reaper prefix or Nothing warns when an account has no TTL-reaper coverage, so the 'everything dies eventually' invariant silently doesn't hold #624's DetectReaperCoverage won't see it — a working reaper that doctor reports as absent is no better than the bug Nothing warns when an account has no TTL-reaper coverage, so the 'everything dies eventually' invariant silently doesn't hold #624 fixed. A test pins it.

Verified end to end on the account that actually needs it

doctor      ⚠ not covered
deploy      + role, + bucket, + artifact, + function, + schedule
doctor      ✓ in-account reaper spawn-ttl-reaper-selfscan
invoke      dry-run=true → FSxScanned:3 Errors:0, Reaped:0
arm         (confirmation prompt) → ARMED
launch      t3.micro, --ttl 1m
invoke      REAPED i-0befb34f37c81fbe4 (reapertest) — ttl-deadline   [dry-run=false, Reaped:1]
teardown    - 7 resources removed
doctor      ⚠ not covered

FSxScanned: 3 with Errors: 0 is the specific proof that #635's FSx grants matter — without them that scan is an AccessDenied, and the orphan net silently does nothing. The reap is attributed to the reaper by its own log line rather than inferred, since spored also enforces TTL.

Account left exactly as found: no instances, no function, no role, 0 available volumes, and the test artifact bucket removed by hand (teardown deliberately leaves it and says so — emptying someone's S3 bucket is a bigger liberty than this command should take).

Two defects I found in my own code

  • teardown claimed to remove a rule that was already gone. EventBridge's DeleteRule is idempotent — it returns success for a missing rule — so err == nil proved nothing, and a second teardown printed - removed rule spawn-ttl-reaper-selfscan-schedule. Found by running teardown twice against real AWS. It now checks DescribeRule first. Claiming work that didn't happen is the same defect class as the rest of this session's fixes, just smaller.
  • The IAM-propagation wait made the unit suite take 40 seconds. make check runs short tests; a 10s sleep per role-creating test is not acceptable. Now injectable; 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". The three pre-existing untidy lambda modules (alert-handler, autoscale-orchestrator, sweep-orchestrator) are unchanged — they fail on clean main too.

spawn doctor's uncovered message now names spawn reaper deploy as the remedy, and docs/durable-storage-fsx.md documents the self-hosted path.

Gates

gofmt · go vet ./... · go build ./... · go test ./... all green · make check-docs clean with docs-gen/reaper.md committed · go mod tidy clean in root and lambda/ttl-reaper · staticcheck zero added (by-message diff).

Suggested bump: MINOR (new commands). Note the download-by-version path still can't be exercised until a release carries the asset — --artifact is how this was tested, and is also the answer for mirrored/air-gapped setups.

…r 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
@codecov

codecov Bot commented Oct 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.77951% with 221 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/reaper.go 7.35% 126 Missing ⚠️
pkg/reaperdeploy/deployer.go 66.90% 67 Missing and 26 partials ⚠️
pkg/doctor/aws_prober.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

… 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
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.
@scttfrdmn
scttfrdmn merged commit 2eabaff into main Oct 3, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the feat/625-reaper-deploy-command branch October 3, 2026 02:13
scttfrdmn added a commit that referenced this pull request Oct 3, 2026
Third attempt at the presence half, and the first that actually runs.

Attempt 1: 'git diff base...HEAD' at fetch depth 1 — died with 'no merge base'
(exit 128) before reaching any logic, which would have failed EVERY pull request.

Attempt 2: deepen main to 100 commits — still no merge base, because the PR branch
is itself fetched at depth 1, so no shared object exists locally. The gate then took
its warn-and-skip fail-safe on every run. That is the worse outcome: it reads as
enforcement while enforcing nothing, which is exactly the defect #624 fixed in the
reaper-coverage check.

Attempt 3: 'gh api repos/{repo}/pulls/{n}/files'. GitHub already knows what the PR
changed; no history needed, nothing to go shallow. All context still arrives via env,
so there is no '${{ }}' in the script body and Semgrep's run-shell-injection rule
stays satisfied.

Validated against three real PRs rather than reasoned about:
  #638 (this one: test + CI + docs)     -> SKIP, correct
  #636 (4 non-test .go + CHANGELOG.md)  -> PASS
  #627 (2 non-test .go, no changelog)   -> FAIL, the PR that prompted all of this

The API-failure and empty-result paths still warn and pass: a gate must fail on a
known violation, not on its own inability to measure one.
scttfrdmn added a commit that referenced this pull request Oct 3, 2026
* ci: enforce the changelog policy instead of trusting habit

CLAUDE.md requires a CHANGELOG entry under [Unreleased] in the SAME PR as any
user-facing change, and the release ritual promotes that section verbatim. Nothing
checked either half, and both broke within a week.

#627 merged with no entry at all. It surfaced at the next release, which hit an EMPTY
[Unreleased] and had to reconstruct the entries from the diff at tag time — precisely
when you least want to be reverse-engineering what a change did.

Separately, three PRs each inserted their own '### Added' at the top of [Unreleased].
That merges cleanly for git and badly for Keep a Changelog: the section ended up with
duplicate '### Added' and '### Fixed' headings, consolidated by hand at release time.
Then it happened a second time.

Two halves, because they need different things:

- changelog_test.go (plain Go test, runs on push and locally): [Unreleased] has no
  duplicate groups, no unknown group names, no entries outside a group; and every
  single-version release heading has a compare link.
- A CI step (PR-only, since it needs the base branch): a PR that changes non-test Go
  source must touch CHANGELOG.md. Test-only, CI-only and docs-gen-only changes are
  exempt — there is nothing user-facing to describe — and the failure message says
  what to write and why the gate exists.

Format checks are scoped to [Unreleased] deliberately. Run over the whole file they
report duplicate groups in roughly a dozen SHIPPED releases and two invented group
names ('### CI', '### Changed (internal refactors — no behavior change)'). Gating
frozen history would force either a mass rewrite of released notes or a permanently
red test, and a permanently red test gets disabled — worse than no test. The job is to
stop new occurrences in the one section still being edited, which is also the only
place the problem creates work.

The compare-link check skips range headings like '[0.36.0 – 0.36.13]', which
summarise a span and have no single diff to point at; demanding a link there would be
a false positive that teaches people to ignore the test.

Each check was verified to fail on the real mistake it describes, and CLAUDE.md now
says the policy is enforced rather than advisory.

* fix(ci): the changelog gate had two bugs of its own

1. Shell injection. '${{ github.base_ref }}' interpolated into a run: block is a
   script-injection vector, and Semgrep's run-shell-injection rule blocked the PR —
   correctly. It is now passed via env: and referenced as $BASE_REF, with nothing from
   the github context in the script body.

2. It would have failed EVERY pull request. 'git diff base...HEAD' needs a merge base,
   the checkout is shallow, and a depth-1 fetch has no common ancestor — so the step
   died with 'fatal: origin/main...HEAD: no merge base' (exit 128) before any of the
   logic ran. Not a false positive: a total block on all work, shipped in CI. Caught
   on this PR's own first run.

   Now it deepens the fetch, resolves the merge base explicitly, and diffs from it, so
   the unresolvable case is detectable rather than fatal. When the merge base cannot be
   found (long-lived branch, force-pushed base) it WARNS and passes: a gate must fail
   on a known violation, not on its own inability to measure one.

Both paths verified locally — the normal case computes the right file list and
verdict, and a disconnected history warns and exits 0.

* fix(ci): ask the API what the PR changed, not a shallow clone

Third attempt at the presence half, and the first that actually runs.

Attempt 1: 'git diff base...HEAD' at fetch depth 1 — died with 'no merge base'
(exit 128) before reaching any logic, which would have failed EVERY pull request.

Attempt 2: deepen main to 100 commits — still no merge base, because the PR branch
is itself fetched at depth 1, so no shared object exists locally. The gate then took
its warn-and-skip fail-safe on every run. That is the worse outcome: it reads as
enforcement while enforcing nothing, which is exactly the defect #624 fixed in the
reaper-coverage check.

Attempt 3: 'gh api repos/{repo}/pulls/{n}/files'. GitHub already knows what the PR
changed; no history needed, nothing to go shallow. All context still arrives via env,
so there is no '${{ }}' in the script body and Semgrep's run-shell-injection rule
stays satisfied.

Validated against three real PRs rather than reasoned about:
  #638 (this one: test + CI + docs)     -> SKIP, correct
  #636 (4 non-test .go + CHANGELOG.md)  -> PASS
  #627 (2 non-test .go, no changelog)   -> FAIL, the PR that prompted all of this

The API-failure and empty-result paths still warn and pass: a gate must fail on a
known violation, not on its own inability to measure one.
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