Skip to content

fix(egress-proxy): delete leaves no security group behind (#1651) - #1659

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1651-egress-sg-leak
Nov 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1651-egress-sg-leak

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1651. Part of the Leave-no-trace epic spore-host/spore-host#752.

Root causes (confirmed in code)

In pkg/egressproxy/deploy:

  • DeleteGroups deleted the proxy group first and returned on the first failure, so the clients group was never attempted.
  • Delete turned a GroupInUseError into Kept with a nil error, so the command exited 0 with groups left.
  • groupUsers checks instances only (instance.group-id) and never network interfaces. A terminated proxy's ENI outlives it, so the group delete then meets DependencyViolation.
  • The proxy group's ingress rule names the clients group. If the proxy group's delete failed, the clients group was undeletable (DependencyViolation), even if it had been tried.
  • The DependencyViolation retry was fixed 10 s polling for 2 minutes.
  • A terminate-wait timeout returned before any group was tried.

In lambda/ttl-reaper/netresources.go: DescribeSecurityGroups read only the first page, and a DependencyViolation on delete was counted as skipped without a log line.

Fix

Delete now:

  1. terminates and waits for up to 5 min, collecting errors instead of returning;
  2. waits for up to 5 min (ENIReleaseTimeout) until no ENI references either group (DescribeNetworkInterfaces, group-id filter), re-checking instances on each poll;
  3. revokes the rules where one group to be deleted names the other. A group that is staying keeps any group its rules name, and is never modified;
  4. deletes both groups, each whatever happened to the other, retrying DependencyViolation with doubling backoff (5 s up to 1 min) for up to 10 min (GroupDeleteTimeout);
  5. returns every failure joined. Kept groups ([]*GroupInUseError with Users / NetworkInterfaces / ReferencedBy) are in the error, so the command exits non-zero whenever anything is left.

The timeouts are configurable through DeleteOptions, and zero means the default. DeleteGroups is the same pass with no waiting. If DescribeNetworkInterfaces is denied, the ENI wait is skipped and the DependencyViolation retry covers it (see the IAM note below). In cmd, the delete failure keeps %w, so ErrGroupsInUse stays matchable, and every kept group is printed with its reason.

Reaper: DescribeSecurityGroups is paginated, and a DependencyViolation is logged with the group and the error. It is still not recorded as a failure, so it does not alarm.

IAM

spawn egress-proxy delete now also calls ec2:DescribeNetworkInterfaces, ec2:RevokeSecurityGroupIngress and ec2:RevokeSecurityGroupEgress. docs/reference/cli-iam-actions.md is regenerated.

I did not add ec2:DescribeNetworkInterfaces to the nested-spawn role. Doing so put its inline fallback over IAM's 10240-byte cap (TestSporedRolesFitTheInlineLimitWhenMixed: 10256 bytes). Instead, a caller that is denied the ENI read relies on the retry. Owner's call whether to make room for it.

Tests

TestDeleteRemovesBothGroups (table): happy path; proxy-group delete fails and clients is still deleted; ENI lingers across polls, is released, then delete succeeds; ENI never released, so the group is kept with the ENI named after a bounded wait; cross-reference revoked before any delete; DependencyViolation retried with backoff and succeeds; window expires and the error is returned; window is configurable; ENI read denied, so retry; in-use group kept, reported, error. Also updated: TestDeleteGroups, TestDeleteRefusesAStructuralFailure, TestDeleteKeepsAClientsGroupInUse, TestDeleteWaitIsBounded, and cmd TestEgressProxyDeleteKeepsAClientsGroupInUse, which had codified the exit-0 bug. Reaper: TestNetSweepReadsEveryPageOfGroups; TestNetSweepToleratesDependencyViolation now asserts the log line.

Checked against a revert that compiles (the original logic behind the new signatures): the ENI-wait, clients-still-attempted, revoke-ordering, window, in-use-error and terminate-timeout cases all fail on it. Both new reaper assertions fail on the original netresources.go.

Gates

  • make check: fmt, vet (all GOOS) and golangci-lint (10 modules) clean. The hermetic-exec step fails spuriously because the clone path contains a space, so go vet ./... and go test ./... were run from the module root, plus lambda/ttl-reaper and lambda/sweep-orchestrator, all passing with no AWS env.
  • semgrep scan --config=auto --error on the changed Go files: 0 findings.
  • gofmt clean.

Needs a LIVE check

Create an egress proxy in the dev account, run spawn egress-proxy delete, and confirm both spawn-egress-proxy-<vpc> and spawn-egress-clients-<vpc> are gone and the command exits 0. The ENI and DependencyViolation timing is only modelled by fakes. Also check the error-code match for an UnauthorizedOperation from DescribeNetworkInterfaces.

`spawn egress-proxy delete` stopped at the first group it could not delete,
so the clients group was never tried; it checked instances but never
network interfaces; a kept group exited 0; and it gave up on a
DependencyViolation after two minutes. Seen in a real account with both
groups left and no ENIs (#749).

Delete now terminates and waits (5m), still trying the groups on a
timeout; waits up to 5m for interfaces to release either group; revokes
the rules where one group names the other; and deletes both, each
whatever happened to the other, retrying DependencyViolation with
backoff for up to 10m. Every failure is joined into the error, kept
groups included, so the command exits non-zero when anything is left.

The TTL reaper's net sweep paginates DescribeSecurityGroups and logs a
DependencyViolation instead of skipping it silently.
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.81988% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/egressproxy/deploy/groups.go 88.65% 16 Missing ⚠️
pkg/egressproxy/deploy/teardown.go 83.33% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn

Copy link
Copy Markdown
Contributor Author

LIVE check (dev 435415984226, us-east-1, 2026-11-16, CLI built from this branch)

  1. A private Ubuntu 24.04 launch (--no-public-ip --bootstrap-egress proxy) created egress proxy i-0f933a4c576d2656b and both groups: spawn-egress-proxy-vpc-059233de1f89e92d3 and spawn-egress-clients-vpc-059233de1f89e92d3. The member instance was still shutting down, so the ENI wait got exercised.
  2. spawn egress-proxy delete --vpc vpc-059233de1f89e92d3 --yes exited 0 in 35 s: the proxy was terminated and both groups deleted.
  3. Independent leak check: no instances, egress SGs, unattached EIPs, available volumes or available ENIs in that VPC.

The fallback for a nested-spawn head (DescribeNetworkInterfaces denied, relying on the DependencyViolation retry) is covered by the unit tests only.

@scttfrdmn
scttfrdmn marked this pull request as ready for review November 16, 2026 19:34
@scttfrdmn
scttfrdmn merged commit 1226975 into main Nov 16, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1651-egress-sg-leak branch November 16, 2026 19:36
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.

egress-proxy delete leaves both SGs behind

1 participant