Repository navigation
Session key pairs: a named deleter, never a workstation key (#1621) - #1637
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
scttfrdmn
force-pushed
the
hardening/1621-session-key-deleter
branch
from
November 14, 2026 20:17
9c114c3 to
f9556f8
Compare
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
force-pushed
the
hardening/1621-session-key-deleter
branch
from
November 15, 2026 02:00
f9556f8 to
a041ade
Compare
scttfrdmn
marked this pull request as ready for review
November 15, 2026 02:18
This was referenced Nov 15, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1621
What changes
Telling the two kinds of key apart, by tag at import (
pkg/awsImportKeyPair):--nested-spawnrole session (NestedSpawnRoleFromCallerARN) is taggedspawn:purpose=ssh-key-session. Its private half lives on the head and dies with it;~/.spawn/keysthat every launch from that machine reuses, staysspawn: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 EC2CreateTimeis 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.netresources.go,reapSessionKeyPairs): runs in the network-resource sweep underREAPER_NET_RESOURCES.reportlogsWOULD reap session key pair ...andreapdeletes. Counted inNetReaped/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 oneec2:DeleteKeyPairstatement conditioned onec2:ResourceTag/spawn:managed=trueandec2:ResourceTag/spawn:purpose=ssh-key-session:pkg/reaperiam(the scan-self policyspawn reaper deploywrites;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:ResourceTagis on anec2:action. No spored or nested-role policy document changes, sosporedPolicySchemais untouched. The nested role'sImportKeyPairgrant only requiresaws:RequestTag/spawn:managed=true, and its tag-on-create exemption already coversImportKeyPair.Session keys imported before this release are tagged
ssh-keyand 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 noCreateTime, 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:keyPairPurposeForreturns 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 oneDeleteKeyPairstatement 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.reportdeletes nothing.cmd:--session-keysdry run then run, and the workstation key is never shown.docs/reference/cli-iam-actions.mdis regenerated. cfn-lint is clean.make checkis clean apart from the known space-in-path failures. Semgrep is clean.Deploy steps
spawn reaper deploy(or the SAM deploy oflambda/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--regionexplicitly.TTLReaperCrossAccountRole(435415984226 and the others inREAPER_ROLE_ARNS): update that stack fromdeployment/cloudformation/ttl-reaper-cross-account-role.yaml.session key pairs: describe key pairs: ...AccessDeniedper region and deletes nothing. That is harmless.Simulate (
aws iam simulate-principal-policyon each reaper role: the function role, and the member-account cross-account role)--context-entries ec2:ResourceTag/...)ec2:DeleteKeyPairec2:DeleteKeyPairec2:DeleteKeyPairec2:DeleteKeyPairec2:DescribeKeyPairsAlso do one real
put-role-policy/stack update (steps 1 and 2) before relying on it.Live checks for the owner
spawn launch --nested-spawn --ttl 1h), and from it launch one child with--ttl 20m. Thenaws ec2 describe-key-pairs --filters Name=key-name,Values=spawn-key-*shows the head's key withspawn:purpose=ssh-key-session. A launch from your workstation leaves yourspawn-key-<you>atspawn:purpose=ssh-key.spawn cleanup --session-keys --all --dry-run, which lists it. Then either run without--dry-run, or let the reaper's next run (underREAPER_NET_RESOURCES=reap) logREAPED session key pair. The key is then gone, and your workstation key is still present.spawn orphans).