Skip to content

feat(spored): egress-proxy mode — allowlisted forward proxy with idle-terminate (#1515 phase 2) - #1517

Merged
scttfrdmn merged 2 commits into
mainfrom
feat/1515-spored-egress-proxy
Nov 3, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
feat/1515-spored-egress-proxy

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Refs #1515 (phase 2 of the build plan: the proxy as a mode of spored).

This PR is self-contained. Nothing creates a proxy instance, wires launches to one, sets proxy env in member bootstraps or adds spawn egress-proxy commands; phases 3–5 do those.

What it adds

  • pkg/egressproxy, standard library only:
    • HTTPS: CONNECT host:443, tunnelled byte for byte with no TLS interception.
    • Plain HTTP: absolute-URI GET/HEAD to port 80, with hop-by-hop and all Proxy-* headers stripped both ways. The upstream Host is the URI's host, never the client's Host header.
    • Allowlist: egressproxy.Allowlist, one exported list with a Why per entry. An entry matches exactly or by a one-label *. wildcard. Hosts are normalised for case and one trailing dot; IP literals, non-ASCII names and malformed labels are refused.
    • Refusals: a 403 (plain text, nosniff) for a host, port or method that isn't allowed, or a non-absolute request. Over the connection cap is a 503. An unresolvable or unreachable host is a 502.
    • SSRF guard: the name is resolved once. The request is refused if ANY address is loopback, link-local (IMDS, fe80::/10), RFC 1918 or ULA (incl. fd00:ec2::/32), CGNAT, unspecified, multicast or reserved, or is a mapped, NAT64, 6to4 or Teredo form of one. The proxy then dials the vetted IP only, never the name, which closes the rebinding gap.
    • Limits: dial, header, response and per-connection idle timeouts, and a concurrent-connection cap.
    • Activity: LastActivity() and Stats(). Activity is updated on accept and while bytes flow; refusals don't count.
    • Logging: one line per request (client, method, host, port, verdict, reason, bytes, duration). Never a path, query or header.
    • Persistence: LoadActivity, SaveActivity (atomic) and InitialActivity.
  • spored egress-proxy: runs the normal lifecycle daemon (runDaemon is now runDaemonWith(nil)) plus the proxy.
    • Flags: --listen (default: the private IP), --port 3128, --idle 336h, --state-file /var/lib/spored/egress-proxy-activity.json and --max-conns 256.
  • pkg/agent.ExternalIdle / SetExternalIdle: a minimal hook in checkAndAct's idle step. When it is set, it replaces spored's CPU, network and session idle signals. Past the timeout it calls the existing a.terminate(...), the TTL's path, retried each tick and announced as ttl_expired only once accepted. It never stops or hibernates.
  • Coherence tests:
    • pkg/userdata and pkg/launcher assert that the EFA installer origin and the AWS CLI origin they fetch from are on the allowlist.
    • The activity file is registered in instancestate.Paths, so an imaged proxy doesn't inherit it. TestEveryWrittenStatePathIsCovered caught this.

Design choices

  • A subcommand, not a tag-driven mode. What the instance is for is fixed by its systemd ExecStart, which only its user data writes. A tag could be flipped by anything that holds ec2:CreateTags. No other spored ever opens a listener.
  • Restarts don't reset the idle clock. On startup, last activity is the later of the persisted time and the instance's launch time, not the process start. With the process start, the weekly patch reboot (or any spored upgrade) would keep an unused proxy alive forever. A persisted time in the future is clamped to now.
  • Idle is set before bind. If the listener can't bind, the proxy still expires, and the bind is retried every 30 s.

Revert gates (each mutation compiled; the named tests failed; then the change was reverted)

mutation failing tests
SSRF address check disabled TestNonPublicResolutionIsRefused, TestDialerSeamDoesNotBypassTheAddressCheck
suffix matching → strings.HasSuffix TestAllowedCannotBeBypassed, ExampleAllowed
re-resolve at dial time (rebinding) TestDialUsesTheVettedAddressNotANewLookup (dialled 169.254.169.254)
external idle hook removed from checkAndAct TestExternalIdleTerminatesThroughTheTerminatePath, TestExternalIdleRetriesARefusedTerminate
idle expiry calls stop instead of terminate same two
InitialActivity ignores persisted/launch time TestInitialActivity, TestEgressProxyActivitySurvivesRestart

Checks

  • make check passes.
  • go test -race ./pkg/egressproxy/... ./pkg/agent/... ./cmd/spored/... passes with AWS env stripped.
  • Full go test ./... passes.
  • semgrep scan --config=auto --error . is clean.
  • gitleaks is clean.
  • spored builds for linux/amd64, linux/arm64 and windows/amd64.
  • No public API change: pkg/egressproxy is not in the public surface.

Deferred to later phases

  • Phase 3: proxy instance creation, SG, and find-or-create.
  • Phase 4: member http_proxy/no_proxy.
  • Phase 5: spawn egress-proxy CLI and reaper coverage.
  • Live test: no live run; nothing here runs on an instance yet.
  • AL2023 repos: the AL2023 dnf repos (S3-hosted al2023-repos-*) are not on the allowlist. They should go through the S3 endpoint via no_proxy in phase 4, and that needs confirming in the phase 6 live test.

@scttfrdmn
scttfrdmn force-pushed the feat/1515-spored-egress-proxy branch from 2208ff9 to 6c710f7 Compare November 3, 2026 07:25
@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.77293% with 56 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/egressproxy/proxy.go 89.76% 26 Missing ⚠️
cmd/spored/egress_proxy.go 86.23% 15 Missing ⚠️
pkg/egressproxy/activity.go 72.72% 9 Missing ⚠️
cmd/spored/main.go 16.66% 5 Missing ⚠️
pkg/agent/external_idle.go 90.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 2c0abab into main Nov 3, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the feat/1515-spored-egress-proxy branch November 3, 2026 07:51
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