Skip to content

fix(egress): purge stale mitmproxy CA on startup#1371

Open
Pangjiping wants to merge 1 commit into
opensandbox-group:mainfrom
Pangjiping:fix/egress-purge-stale-mitm-ca
Open

fix(egress): purge stale mitmproxy CA on startup#1371
Pangjiping wants to merge 1 commit into
opensandbox-group:mainfrom
Pangjiping:fix/egress-purge-stale-mitm-ca

Conversation

@Pangjiping

Copy link
Copy Markdown
Collaborator

Summary

Fixes part of #1370.

mitmproxy's CA lives in the egress container's ephemeral confdir (/var/lib/mitmproxy/.mitmproxy/). Every time the egress container restarts, a fresh CA is generated with a new fingerprint. The previous generation's public cert, however, may still be on the pod-scoped shared volume at /opt/opensandbox/mitmproxy-ca-cert.pem (an emptyDir under Kubernetes / a docker named volume, both survive container restarts).

Without any purge, an agent container starting or restarting alongside egress passes its bootstrap readiness check [ -f "$MITM_CA" ] && [ -s "$MITM_CA" ] on the stale file before egress has had a chance to overwrite it, and installs a CA that no longer matches what mitmproxy will sign with. Subsequent HTTPS from agent then fails with unable to get local issuer certificate.

What this change does

Erase /opt/opensandbox/mitmproxy-ca-cert.pem as early as possible in egress main() — immediately after logger initialization, before any telemetry / policy / dnsproxy / iptables / mitmproxy setup. This forces the agent's bootstrap wait-loop to block until this generation's SyncRootCA writes the current CA back to the shared volume.

Log level is warn: on a healthy first pod startup the file does not exist and nothing is logged; a removed stale exported CA warning is real signal that egress just restarted, which is useful when diagnosing HTTPS-from-agent problems.

Scope

Fixes: pod-container co-restart (both containers restarting together within the same pod, with emptyDir surviving). This is scenario C in #1370.

Does NOT fix: egress-alone container restart (scenario B in #1370). In that case, the agent's bootstrap has long finished and never re-reads the file, so no amount of egress-side purging helps. That case still requires an agent-side reload path (watcher in execd), which is intentionally out of scope for this PR.

Not affected: whole-pod recreation (scenario A) already goes through a fresh shared volume and works correctly.

In-place egress process restart (uncommon)

If the egress process is somehow restarted while its container stays up (uncommon under normal K8s / Docker lifecycles, since egress is PID 1), mitmproxy will reuse the CA already in confdir. The purge deletes the shared-volume copy, SyncRootCA writes back the same bytes moments later, and any agent bootstrap running concurrently only blocks for the duration of that gap. Functionally a no-op; the warn line correctly reports "previous egress generation left it behind".

Tests

Added unit tests for the pure helper purgeStaleExportedCAFrom covering:

  • Existing file is removed.
  • Absent file is a no-op (first-boot path).
  • Unrelated files (e.g. merged-ca-certificates.pem) are left untouched.

Local verification:

cd components/egress
go build -mod=mod ./...
go vet -mod=mod ./...
go test -mod=mod ./...

All pass.

Files changed

  • components/egress/main.go — call mitmproxy.PurgeStaleExportedCA() after withLogger, before telemetry init.
  • components/egress/pkg/mitmproxy/cacert_export.go — new exported PurgeStaleExportedCA() and internal testable purgeStaleExportedCAFrom.
  • components/egress/pkg/mitmproxy/cacert_export_test.go — three focused unit tests.

mitmproxy's CA lives in the egress container's ephemeral confdir
(/var/lib/mitmproxy/.mitmproxy/), so every egress container restart
regenerates a fresh CA. The previous generation's public cert may
still be on the pod-scoped shared volume; without a purge, an agent
container starting or restarting alongside egress passes its bootstrap
readiness check on the stale file and installs a CA that no longer
matches what mitmproxy will sign with.

Erase /opt/opensandbox/mitmproxy-ca-cert.pem as early as possible in
egress main() (immediately after logger initialization, before any
telemetry / policy / iptables / mitm setup) so the agent's wait-loop
blocks until this generation's SyncRootCA has completed.

Scope:
- Closes the race for pod-container co-restart (both containers
  restarting together with an emptyDir that survived).
- Does NOT address the egress-alone container restart case where the
  agent's bootstrap has long finished and never re-reads the file;
  that still requires an agent-side reload path.

Refs opensandbox-group#1370
@github-actions github-actions Bot added component/egress size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 22, 2026
@Pangjiping Pangjiping added the bug Something isn't working label Jul 22, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2b49e52342

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread components/egress/main.go
jwx0925
jwx0925 previously approved these changes Jul 22, 2026

@jwx0925 jwx0925 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 73526d7485

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread components/execd/bootstrap.sh Outdated
@Pangjiping
Pangjiping force-pushed the fix/egress-purge-stale-mitm-ca branch from 73526d7 to 2b49e52 Compare July 22, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working component/egress component/execd size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants