Skip to content

Gate the reaper's YAML IAM copies against pkg/reaperiam; fix the SAM ScanSelf drift (#985) - #991

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/985
Oct 17, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/985

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #985

Stacked on #981 (fix/950 → #961 → #951). Review and merge after those.

What was wrong

lambda/ttl-reaper/template.yaml attaches its own copy of the scan-self policy when ScanSelf=true, and nothing compared it with pkg/reaperiam. It had drifted in two ways:

Change

  • template.yaml: added the network-resource grants. SSM is now split the way reaperiam splits it: SendCommand on arn:aws:ec2:*:*:instance/* under ssm:resourceTag/spawn:managed=true, SendCommand on document/AWS-RunShellScript, and GetCommandInvocation on *.
  • New gate, pkg/reaperiam/template_drift_test.go:
    • Parses both YAML files with yaml.v3 and keeps CFN intrinsics as long-form maps, so a !Sub cannot silently compare equal to a literal.
    • Splits every statement into one grant per action, then requires set equality with ScanSelfStatements in effect, action, resources and condition. Grouping actions into fewer or more statements does not count as drift.
    • SAM template: finds the ScanSelf branch from the Condition that tests ScanSelf=true, without relying on its name. Fails if any ec2/fsx/ssm action appears anywhere else in the function's policies.
    • Fails on an empty parse, on an element it does not compare (such as NotAction), and on any intrinsic other than ${AWS::AccountId}, which it normalises to *.
  • Cross-account role:

I chose a gate over generating the template. The template is hand-commented SAM with intrinsics, and a generator would add a build step and a committed artefact that would itself need a drift gate.

Evidence

The new tests pass on the change. I watched them fail against each of these reverts, all of which compile:

  • the old template.yaml (lists 7 missing grants and the extra ssm:SendCommand on [*])
  • the old cross-account role (ec2:DescribeTags extra)
  • a changed ssm:resourceTag key in the cross-account role
  • reaperiam's SendCommand Condition: nil (both YAML copies fail)
  • a ssm:SendCommand statement moved outside the ScanSelf branch
  • the ScanSelf condition renamed so discovery finds nothing

Other checks:

  • sam validate --lint passes on both templates.
  • golangci-lint is clean on pkg/reaperiam.
  • make check-nested passes.
  • make check: the only failure is TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker, because no Docker daemon is running here. It is unrelated.

Deploy (not done; no AWS touched)

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ScanSelf drift (#985)

The SAM template's ScanSelf branch kept its own copy of the scan-self policy
and no gate read it. It had drifted: no #685 network-resource grants
(DescribeSecurityGroups/DescribePlacementGroups/DeleteSecurityGroup/
DeletePlacementGroup/CreateTags), and ssm:SendCommand on '*' with no
condition, i.e. root shell on any SSM-managed instance in the account.

- template.yaml: add the network-resource grants and split SSM the way
  reaperiam does (SendCommand on instances under ssm:resourceTag/spawn:managed,
  SendCommand on the AWS-RunShellScript document, GetCommandInvocation on *).
- pkg/reaperiam/template_drift_test.go: parse template.yaml and the
  cross-account role with yaml.v3 (CFN intrinsics kept as long-form maps),
  explode every statement per action, and require set equality with
  ScanSelfStatements in effect, action, resources and condition. The ScanSelf
  branch is discovered from the Condition testing ScanSelf=true; ec2/fsx/ssm
  actions anywhere else on the function fail; empty parses fail.
- Cross-account role: drop the unused ec2:DescribeTags and a no-op !Sub.
  One recorded, narrower-only exception: fsx:DeleteFileSystem keeps its
  aws:ResourceTag condition there (#613).

Watched the gate fail against: the old template, the old cross-account role,
a changed ssm condition key, reaperiam's SendCommand condition set to nil, a
SendCommand statement moved outside the branch, and a renamed condition.

(#985)
@scttfrdmn
scttfrdmn merged commit 3fe7609 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/985 branch October 17, 2026 02:37
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.

ttl-reaper template.yaml ScanSelf policy has drifted from pkg/reaperiam: no network-resource grants, and unconditioned ssm:SendCommand

1 participant