Skip to content

fix(atunnel): reload the egress trust bundle when it changes - #4216

Open
yjjnls wants to merge 1 commit into
agent-substrate:mainfrom
yjjnls:fix/egress-reload-trust-bundle
Open

yjjnls wants to merge 1 commit into
agent-substrate:mainfrom
yjjnls:fix/egress-reload-trust-bundle

Conversation

@yjjnls

@yjjnls yjjnls commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #4207.

Problem

NewClient reads TrustBundlePath once and pins it as tls.Config.RootCAs (internal/atunnel/egress/client.go, the os.ReadFile before the Client literal). The client certificate already reloads through the GetClientCertificate callback — acquireConn retires a pooled connection when the certificate changes — but the trust side never does.

PrepareEgress builds one client per actor activation (internal/atunnel/tunnel.go), so an activation that spans a servicedns CA rotation keeps trusting only the old CA. Once its pooled connection drops, or the gateway restarts holding a certificate from the new CA, every outbound call for that actor fails until it is suspended and resumed. Long-running agent actors and long RL/eval runs are the ones that span a rotation.

Fix

Re-read the bundle when the file behind TrustBundlePath changes, keyed by modification time and size, and install it on each new connection:

tlsConfig := c.tlsConfig.Clone()
tlsConfig.RootCAs = c.rootCAsForDial()

Why this shape rather than a ticker or fsnotify:

  • Steady state costs one os.Stat per dial. Dials are per-connection, not per-request, so this is negligible next to the handshake itself.
  • No background goroutine, so no shutdown ordering to get wrong and nothing to leak if a client is dropped without Close.
  • A partial write must not take egress down. During rotation the bundle can be empty, truncated, or briefly missing. In those cases rootCAsForDial returns the last known-good pool (falling back to the pool built in NewClient), so actors keep connecting with the CA they already trust instead of failing closed on a half-written file.
  • The check is under a mutex only around the cache lookup/refresh; the file read happens once per change, not per dial.

Tests

Both live in internal/atunnel/egress/client_test.go:

  • TestClientReloadsTrustBundleAfterRotation — starts the client pointed at a stale CA so the first handshake fails, rewrites the bundle, and dials again on the same Client. Without the fix this fails with x509: certificate signed by unknown authority; verified by reverting the one-line RootCAs assignment.
  • TestClientKeepsLastGoodTrustBundleOnPartialWrite — dials successfully, then replaces the bundle with an empty file and dials again. Must still succeed, pinning the "don't fail closed mid-rotation" half.

go test ./internal/atunnel/egress/ -count=1 and -race both pass; go vet and gofmt clean.

Scope

Client-side trust only. The TrustBundlePath config surface, the certificate reload path, and the tunnel/activation lifecycle are unchanged. Alex Bulankou (@AlexBulankou) — this is your report; happy to adjust the reload strategy if you had a different one in mind (a watcher, or reload-on-handshake-failure plus one retry, are both reasonable alternatives).

An activation that outlives a servicedns CA rotation kept verifying the
egress gateway with the bundle read at NewClient time, so every outbound
call failed until the actor was suspended and resumed. The client
certificate already reloads through GetClientCertificate, but RootCAs was
a fixed pool built once in NewClient.

Re-read the bundle on each new gateway connection, keyed by the file's
modification time and size, so the steady state costs one stat per dial
and no background goroutine or watcher. A bundle caught mid-rotation
(empty, truncated, or unreadable) keeps the last known-good pool: a
partial write must not take egress down for every actor on the node.

Refs agent-substrate#4207

Signed-off-by: yjjnls <yjjnls@users.noreply.github.com>

@aramase Anish Ramasekar (aramase) 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.

The per-dial reload in runDial is the right place for the #4207 fix. I have one concern about how the reload is built, plus one about the test. Both are inline.

// rotation. A bundle that is missing, truncated, or empty mid-rotation keeps
// the last known-good pool: a partial write must not take egress down for
// every actor on the node.
func (c *Client) rootCAsForDial() *x509.CertPool {

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.

This adds a second stat-keyed trust-pool cache next to credbundle.PoolLoader (internal/credbundle/credbundle.go:145-167), which already does this. ateapiauth uses it for RootCAs (internal/ateapiauth/client.go:61), and #4207 suggested it for this fix. The copy also behaves differently in two ways:

  • It drops the os.SameFile check, which handles kubelet's symlink swap of projected files.
  • Every Stat or Read error and an empty parse silently return the last good pool (lines 165-182).

If the bundle becomes permanently unreadable (mount problem, wrong path after an upgrade), egress keeps trusting the old CA with no log. The first sign is x509: certificate signed by unknown authority once the gateway rotates, which is the #4207 failure with the cause hidden. The partial-write case doesn't come up on the default path: /run/servicedns.podcert.ate.dev/trust-bundle.pem is a projected volume, which kubelet writes atomically.

Suggestion:

  • Store credbundle.PoolLoader(cfg.TrustBundlePath) on the Client and call it once in NewClient, so a missing or empty bundle still fails at construction.
  • In runDial, set RootCAs from it and fail the dial with a wrapped error when it errors.
  • If last-good fallback is wanted, put it in credbundle with a log line, so every consumer behaves the same.

// TestClientKeepsLastGoodTrustBundleOnPartialWrite pins the other half of
// #4207: a bundle caught mid-rotation (truncated, empty, or unreadable) must
// not take egress down. The last known-good pool keeps verifying.
func TestClientKeepsLastGoodTrustBundleOnPartialWrite(t *testing.T) {

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.

This test also passes with the base client.go: main pins the pool, so an empty file never matters there. AGENTS.md asks that each new test fail without the change. With the PoolLoader change above, a case where the bundle becomes unreadable and the dial fails with a clear error would cover the new behavior, and it fails on main.

This branch has not been deployed

No deployments
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.

atunnel egress client loads its CA trust bundle once, so a servicedns CA rotation breaks egress for long-running actors

2 participants