Skip to content

Session key pairs: a named deleter, never a workstation key (#1621) - #1637

Merged
scttfrdmn merged 4 commits into
mainfrom
hardening/1621-session-key-deleter
Nov 15, 2026
Merged

scttfrdmn merged 4 commits into
mainfrom
hardening/1621-session-key-deleter

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1621

What changes

Telling the two kinds of key apart, by tag at import (pkg/aws ImportKeyPair):

  • a key imported by a --nested-spawn role session (NestedSpawnRoleFromCallerARN) is tagged spawn:purpose=ssh-key-session. Its private half lives on the head and dies with it;
  • every other import, such as the workstation key in ~/.spawn/keys that every launch from that machine reuses, stays spawn:purpose=ssh-key. An instance under any other role (a dev box with an instance profile) is not a nested head, so its key stays a workstation key.

One rule, pkg/keypairsweep, used by two named deleters: a session key pair is deleted when no instance in its region (pending through stopped) was launched with it and its EC2 CreateTime is at least 1 hour old. The EC2 tag filter is not trusted, so the tag is re-checked on every key returned. A workstation key never gets past selection.

  • TTL reaper (netresources.go, reapSessionKeyPairs): runs in the network-resource sweep under REAPER_NET_RESOURCES. report logs WOULD reap session key pair ... and reap deletes. Counted in NetReaped/NetSkipped.
  • spawn cleanup --session-keys: the same sweep with a preview, a prompt (--yes, --dry-run), then a fresh pass. It covers any principal and touches nothing else.

IAM: the workstation key is protected by IAM, not only by code. All three reaper policy sources get ec2:DescribeKeyPairs (read) and one ec2:DeleteKeyPair statement conditioned on ec2:ResourceTag/spawn:managed=true and ec2:ResourceTag/spawn:purpose=ssh-key-session:

  • pkg/reaperiam (the scan-self policy spawn reaper deploy writes; SessionKeyTagCondition);
  • lambda/ttl-reaper/template.yaml (TTLReaperFunction, ScanSelf);
  • deployment/cloudformation/ttl-reaper-cross-account-role.yaml (member accounts).

There are no policy variables, and ec2:ResourceTag is on an ec2: action. No spored or nested-role policy document changes, so sporedPolicySchema is untouched. The nested role's ImportKeyPair grant only requires aws:RequestTag/spawn:managed=true, and its tag-on-create exemption already covers ImportKeyPair.

Session keys imported before this release are tagged ssh-key and are not collected. Delete those by hand (the CHANGELOG says so).

Tests

  • pkg/keypairsweep: the Key pairs a nested-spawn head imports are never deleted (no deleter, not reaped) #1621 leak is deleted. A key a stopped child uses, a 10-minute-old key, a key with no CreateTime, and a workstation key (even when EC2 returns one) are all kept. Dry run deletes nothing, a failed delete is reported, and the request filters on the session purpose.
  • pkg/aws: keyPairPurposeFor returns the session purpose for a nested role session and the workstation purpose for an IAM user, an SSO session, a non-nested instance role, and an unknown caller.
  • pkg/reaperiam: exactly one DeleteKeyPair statement exists, scoped to the session purpose, and equal to the sweep's constant. The existing drift tests hold the SAM and cross-account templates equal to it.
  • Reaper: only the unused session key is deleted, and report deletes nothing. cmd: --session-keys dry run then run, and the workstation key is never shown.
  • The iamcoverage gate passes with all three reaper variants. docs/reference/cli-iam-actions.md is regenerated. cfn-lint is clean. make check is clean apart from the known space-in-path failures. Semgrep is clean.

Deploy steps

  1. Operated reaper (966362334030): spawn reaper deploy (or the SAM deploy of lambda/ttl-reaper/template.yaml). This updates the function and its role, including the scan-self inline policy. Snapshot the stack parameters first and diff them for zero drift afterwards. Pass --region explicitly.
  2. Each member account with TTLReaperCrossAccountRole (435415984226 and the others in REAPER_ROLE_ARNS): update that stack from deployment/cloudformation/ttl-reaper-cross-account-role.yaml.
  3. Until both are done, the sweep logs session key pairs: describe key pairs: ...AccessDenied per region and deletes nothing. That is harmless.

Simulate (aws iam simulate-principal-policy on each reaper role: the function role, and the member-account cross-account role)

action resource tags (--context-entries ec2:ResourceTag/...) expect
ec2:DeleteKeyPair managed=true, purpose=ssh-key-session allowed
ec2:DeleteKeyPair managed=true, purpose=ssh-key implicitDeny
ec2:DeleteKeyPair managed=true only implicitDeny
ec2:DeleteKeyPair none implicitDeny
ec2:DescribeKeyPairs n/a allowed

Also do one real put-role-policy/stack update (steps 1 and 2) before relying on it.

Live checks for the owner

  1. Import tag: launch a nested head (spawn launch --nested-spawn --ttl 1h), and from it launch one child with --ttl 20m. Then aws ec2 describe-key-pairs --filters Name=key-name,Values=spawn-key-* shows the head's key with spawn:purpose=ssh-key-session. A launch from your workstation leaves your spawn-key-<you> at spawn:purpose=ssh-key.
  2. Deleter: after both instances are gone and the key is over 1 hour old, run spawn cleanup --session-keys --all --dry-run, which lists it. Then either run without --dry-run, or let the reaper's next run (under REAPER_NET_RESOURCES=reap) log REAPED session key pair. The key is then gone, and your workstation key is still present.
  3. Verify no instance from step 1 is left (spawn orphans).

@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.87611% with 25 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/cleanup_session_keys.go 66.66% 14 Missing ⚠️
pkg/keypairsweep/keypairsweep.go 83.01% 9 Missing ⚠️
cmd/cleanup.go 60.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn force-pushed the hardening/1621-session-key-deleter branch from 9c114c3 to f9556f8 Compare November 14, 2026 20:17
scttfrdmn and others added 4 commits November 14, 2026 17:58
A key pair a --nested-spawn head imports (spawn-key-<user>) had no
deleter: its private half dies with the head, and nothing removed the EC2
key pair. Such keys are now tagged spawn:purpose=ssh-key-session at
import; every other import (the workstation key ~/.spawn/keys reuses)
stays spawn:purpose=ssh-key.

pkg/keypairsweep deletes a session key once no instance in its region
(pending through stopped) uses it and it is an hour old, re-checking the
tag on every key. The TTL reaper's network-resource sweep and the new
spawn cleanup --session-keys both apply it. The reaper's DeleteKeyPair
grant (pkg/reaperiam, the SAM template, the cross-account role) is
conditioned on the session purpose, so IAM denies it a workstation key.
@scttfrdmn
scttfrdmn force-pushed the hardening/1621-session-key-deleter branch from f9556f8 to a041ade Compare November 15, 2026 02:00
@scttfrdmn
scttfrdmn marked this pull request as ready for review November 15, 2026 02:18
@scttfrdmn
scttfrdmn merged commit f334e8e into main Nov 15, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the hardening/1621-session-key-deleter branch November 15, 2026 02:18
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.

Key pairs a nested-spawn head imports are never deleted (no deleter, not reaped)

1 participant