Skip to content

Added mTLS between ate system components - #237

Merged
Michael Taufen (mtaufen) merged 22 commits into
agent-substrate:mainfrom
zoez7:mtls
Jul 24, 2026
Merged

Michael Taufen (mtaufen) merged 22 commits into
agent-substrate:mainfrom
zoez7:mtls

Conversation

@zoez7

@zoez7 Zoe Zhao (zoez7) commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #170
Establishes mutual TLS between all ate system components, and updates the certificate plumbing it depends on.

Main changes:

  1. The atenet router now verifies ate apiserver' serving certificate, and presents its client cert to ate apiserver. Previously the connection used InsecureSkipVerify.
  2. AteApi server verifies atelet's serving cert.

Minor bug fixes:

  1. Prevent servicednssigner from signing a cert with no DNS SANs. Also updated valkey cluster's cert configuration, because it was relying on the cert with empty DNS.
  • Tests pass
  • Appropriate changes to documentation are included in the PR

@ahmedtd

Copy link
Copy Markdown
Collaborator

Can you talk a little bit about the background of splitting out separate CAs / signers for the workerpools and ate-system components?

If at all possible, I would prefer to restrict us to two signers:

  • One that issues certs with DNS SANs based on the K8s services that a pod is part of (though opt-in with an annotation or label is fine).
  • One that issues SPIFFE certs that encode the k8s service account identity of the pod (and potentially, the pod ID, node ID etc in an additional extension).

The reason is that we can be reasonably confident that signers like these will land in upstream K8s in the near-ish future. The certificates here aren't special or unique to substrate, and they aren't part of the value proposition of the project. So I think we should plan to rebase on native K8s capabilities as they become available. That's easiest if we don't get very custom with the signers.

@zoez7

Copy link
Copy Markdown
Collaborator Author

I think the main motivations for splitting out separate CAs / signers was, if I mount the "podidentity" CA bundle as trusted client CAs in ate-system services, workerpool pods (therefore actors) would also be able to connect to ate-system servers like the Valkey cluster and atelet server.

But after typing it out loud - it seems this needs to be solved anyway (probably in a later milestone) by authorization inside servers such as valkey, instead of using separate CAs. Does that make sense to you?

@ahmedtd

Copy link
Copy Markdown
Collaborator

I think the main motivations for splitting out separate CAs / signers was, if I mount the "podidentity" CA bundle as trusted client CAs in ate-system services, workerpool pods (therefore actors) would also be able to connect to ate-system servers like the Valkey cluster and atelet server.

But after typing it out loud - it seems this needs to be solved anyway (probably in a later milestone) by authorization inside servers such as valkey, instead of using separate CAs. Does that make sense to you?

Yeah --- we should always draw a bright line between authentication and authorization. Assuming that a given client is authorized just because they were able to get a certificate from a particular CA has caused a lot of security problems in the past.

Unfortunately, for the specific case of Valkey we might be limited by its capabilities, in which case it might require a separate signer. Let me check the docs.

@ahmedtd

Copy link
Copy Markdown
Collaborator

It looks like valkey can do it: https://valkey.io/topics/tls/#:~:text=Client%20certificate%20authentication

@zoez7
Zoe Zhao (zoez7) force-pushed the mtls branch 3 times, most recently from 683d085 to 2e34e49 Compare June 16, 2026 01:16
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved
Comment thread cmd/atenet/internal/router/router.go Resolved

@bowei Bowei Du (bowei) 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.

some nits

Comment thread cmd/atenet/internal/router/config.go Resolved

@bowei Bowei Du (bowei) 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.

Router changes look ok.

@bowei

Copy link
Copy Markdown
Collaborator

needs rebase

@bowei

Copy link
Copy Markdown
Collaborator

Ping on this?

Rebased onto main and integrated with the ateapi-auth (mtls|jwt) mode that
landed on main in agent-substrate#248.

Router (client):
- ateapi-auth=mtls now performs real mutual TLS: the router presents its
  podidentity credential bundle (--ateapi-client-cert) as a client cert and
  verifies ateapi's serving cert against the servicedns trust bundle
  (--ateapi-ca-certs), instead of the previous insecure TLS dial. No insecure
  fallback: missing material is a hard error.
- ateapi-auth=jwt (from agent-substrate#248) is unchanged and still routes through
  ateapiauth.DialOptions; otel tracing/metrics are preserved.

ateapi (server):
- The ateapiauth server interceptor now injects an authenticated principal
  (internal/principal) into the request context in both modes: the mtls
  authenticator extracts the client's SPIFFE identity from its mTLS peer
  cert, and the jwt authenticator records the verified token subject
  (VerifyBearerToken now returns the principal ID). The principal carries a
  Kind (mtls|jwt) and is included in RPC request logs.

Supporting changes:
- Move credbundle to internal/credbundle and add ClientLoader for client certs.
- podcertcontroller signers, valkey, and install-ate.sh updated to provision and
  mount the podidentity/servicedns bundles; manifests mount them into the
  router and apiserver pods.
The --ateapi-client-cert path is provided via the manifest instead of
a code default, matching how the other cert-material flags are wired.
Drop --ateapi-ca-certs: it duplicated --ateapi-ca-file (both are the CA
bundle used to verify ateapi's serving cert, split by auth mode). The
router now dials ateapi via ateapiauth.DialOptions in both modes, like
atecontroller, instead of hand-rolling the mtls TLS config. This also
gains TLS 1.2 minimum and --ateapi-server-name support in mtls mode.

Path defaults move to the manifest (--ateapi-ca-file previously
defaulted to the SA ca.crt, which never matches ateapi's servicedns-
signed serving cert), and the router's servicedns-ca trust bundle now
mounts at /run/servicedns-ca, matching ate-controller and deduping the
jwt overlay's second mount of the same bundle.
Port the file-layout refactors from PR agent-substrate#237 that were lost when the
mtls branch was redone on a newer main:

- Move NewRouterCmd from cmd/atenet/internal into the router package
  as cmd.go; root.go now calls router.NewRouterCmd().
- Move the config struct to config.go, unexport it (routerConfig), and
  group the ateapi client-auth fields under a nested authConfig.
- Drop the self-signed Envoy cert fallback: --envoy-cert-path is now
  required for HTTPS serving; SetTlsConfig takes just (port, path) and
  xDS always references the cert by filename.

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.

With PodCertificates and short lifetimes, should be be reloading this dynamically at some fraction of expiry time?

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.

More broadly (maybe a question for Taahir Ahmed (@ahmedtd) ), how is CA rotation expected to work with Pod Certificates?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

For CA rotation in the future, we can copy what DCD does today and use a sidecar that reloads the CA trust bundle

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.

Would we then terminate tls in the sidecar?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The sidecar can only fetch the updated CA bundle, and write to a file path. The binary itself still needs to periodically reload the CA bundle from that filepath.

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.

That's what I'm asking, should we have the binary auto-reload, or require it to be restarted for rotation?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think we should have the binary auto-reload to avoid restarting just for cert rotation. (The CA rotation operation today in GKE is very complex and requires multiple rounds of restarting kube-apiserver, definitely not fun to debug those)

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.

Would we then terminate tls in the sidecar?

Comment thread internal/substratex509/substratex509.go Resolved

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 will balloon over time as we add RBAC, but i think it's a good starting pointj

Comment on lines +32 to +34
// GoogleSubstratePEN is the ASN.1 Private Enterprise Number arc used to
// name X.509 extensions that communicate Substrate-specific concepts.
GoogleSubstratePEN = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 12}

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.

What does this have to do with google?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The OID prefix 1.3.6.1.4.1.11129 is registered under the Google's enterprise number 11129 https://pki.goog/oids/index.html, after we donate this to CNCF, we'll replace with a new different OID under CNCF.

Both dialDirect and dialPortForward attached the ate-client bearer token
over a TLS channel with InsecureSkipVerify, allowing an active MITM to
capture and replay the token. Build the client TLS config from the live
servicedns ClusterTrustBundle published by podcertcontroller (the
out-of-cluster equivalent of the projected volume in-cluster clients
mount) and pin ServerName to the cert's api.ate-system.svc DNS SAN.
}

func verifyAteletServerCert(bundle *x509bundle.Bundle, expectedID spiffeid.ID, expectedPodUID string) (func(tls.ConnectionState) error, error) {
if expectedPodUID == "" {

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.

should we also be checking that expectedID is nonempty?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in bbe2c7b.

@mtaufen

Copy link
Copy Markdown
Collaborator

Thanks Zoe Zhao (@zoez7) for incorporating all the feedback! I think we're good to merge and we can follow up with the dynamic cert rotation and update authn docs as follow-ups.

@mtaufen
Michael Taufen (mtaufen) merged commit 9890219 into agent-substrate:main Jul 24, 2026
11 checks passed
Chuang Wang (chuangw6) added a commit to chuangw6/substrate that referenced this pull request Jul 28, 2026
ateapi has rejected credential-less calls since agent-substrate#237: the interceptor
takes identity from an mTLS client certificate, or failing that an
`authorization: Bearer <jwt>` header. The benchmark still dialed with
server-side TLS only, so every call returned `Unauthenticated: missing
bearer token` and no load test could start.

agent-substrate#237 gave the ate-system components a pod certificate and the flags to
present it. The benchmark has its own Deployment, outside
manifests/ate-install/, so it was missed.

Project a podidentity certificate into the locust pod and present it:
the Go boomer through ateapiauth.DialOptions, the Python user classes
through a new common/ateapi_channel.py that replaces the channel setup
duplicated across all five of them. This matches the base install
rather than the opt-in ATE_ATEAPI_CLIENT_AUTH=token overlay, and gives
the benchmark a SPIFFE ID instead of an opaque JWT subject.

Python's gRPC has no per-handshake reload hook, so unlike the Go client
it reads the bundle once per channel. Each locust User builds its own
channel in on_start, so a respawn picks up a rotated certificate.

This also drops the InsecureSkipVerify from DialControl. The servicedns
trust bundle was already mounted; nothing was using it.
Max Smythe (maxsmythe) pushed a commit that referenced this pull request Jul 28, 2026
The benchmark has been unrunnable on `main` since #237 landed. Every
ateapi
call fails with:

```
rpc error: code = Unauthenticated desc = missing bearer token
```

`ateapiauth`'s interceptor takes identity from an mTLS client
certificate, or
failing that an `authorization: Bearer <jwt>` header, and rejects
anything
else. The benchmark's clients still dialed with server-side TLS only.

#237 gave the ate-system components a pod certificate and the flags to
present
it. The benchmark has its own Deployment, outside
`manifests/ate-install/`, so
it was missed.

## What this does

- Projects a `podidentity.podcert.ate.dev/identity` certificate into the
locust
  pod, mounted in all three containers.
- Go boomer: `DialControl` now builds its options through
`ateapiauth.DialOptions` with `ClientCredBundle`, so
`credbundle.ClientLoader`
re-reads the bundle on every handshake and rotations need no restart.
This
also drops the `InsecureSkipVerify` it was using — the servicedns trust
bundle
  was already mounted and nothing was reading it.
- Python user classes: a new `common/ateapi_channel.py` replaces the
five-line
  channel setup duplicated across all five of them.

Certificate rather than token because that is what the base install
gives the
other components — the token path is the opt-in
`ATE_ATEAPI_CLIENT_AUTH=token`
overlay — and because it yields a SPIFFE ID rather than an opaque JWT
subject
for a future authz layer to key on.

One asymmetry worth knowing about: Python's gRPC has no per-handshake
reload
hook, so it reads the bundle once per channel. Each locust `User` builds
its own
channel in `on_start`, so a respawn picks up a rotated certificate, and
an
established connection is unaffected by the old one expiring.

## Verification

Deployed to a GKE cluster running `main`. Before: every call rejected,
boomer
retrying `on_start` in a loop. After: every call authenticated as
`spiffe://cluster.local/ns/benchmarking/sa/default`, and no
`Unauthenticated`
in the api-server log.

The SPIFFE ID is the part that matters. Only `mtlsPeerIdentity` logs
`Authentication successful`; the JWT branch is silent. So an absence of
errors
alone would not distinguish the certificate being accepted from a token
falling
through behind it — seeing the SPIFFE ID does.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Zoe Zhao (zoez7) pushed a commit that referenced this pull request Jul 28, 2026
Resolves the `TODO: Introduce caching.` on `credbundle.Loader` /
`credbundle.ClientLoader`. The TODO dates back to the package's
introduction; since #237 established mTLS between all ate system
components, every inter-component handshake is on this path.

## Problem

`Loader`/`ClientLoader` re-read and re-parse the credential bundle from
disk on every TLS handshake so that pod-certificate rotations get picked
up. That costs ~125µs and ~120 allocations per handshake (RSA bundle)
for a file that almost never changes.

## Approach: stat-validated cache

Each loader keeps the last successful parse together with the
`os.FileInfo` taken just before that parse. A handshake stats the file
(~1µs) and serves the cached certificate while file identity
(`os.SameFile`, i.e. device+inode on Unix), mtime, and size all match;
any mismatch re-reads and re-parses.

Invalidation and eviction:

- The kubelet rotates projected-volume contents (including pod
certificates) by writing a fresh timestamped directory and atomically
swapping a symlink, so a rotation always surfaces as a change of file
identity and is picked up on the **next handshake** — no staleness
window. mtime and size additionally cover in-place rewrites.
- **No TTL**: freshness is derived from the file, never from time, so an
unchanged bundle is never re-parsed and a changed one is never served.
(A refresh-interval floor to throttle stats was considered and rejected:
it would introduce a staleness window to save a ~1µs syscall that only
runs at connection-establishment rate. fsnotify was also considered and
rejected: watching through symlink swaps is error-prone and adds watcher
lifecycle for no latency benefit over the stat.)
- **Memory**: exactly one parsed certificate per loader (one loader per
`tls.Config`), replaced on rotation — nothing unbounded to evict.
- **Failure semantics unchanged**: stat/read/parse errors are returned
to the caller (never a stale certificate), the previous entry stays in
place, and every later handshake retries until a parse succeeds —
exactly the uncached behavior.
- The file can change between the stat and the read; the parse is then
stored against the older stat, so the next handshake detects the
mismatch and re-reads. The cache never lags a rotation by more than one
handshake.

## Benchmarks

Apple M5 Pro, RSA-2048 bundle:

| | ns/op | B/op | allocs/op |
|---|---:|---:|---:|
| `BenchmarkLoaderCached` | 1,001 | 304 | 2 |
| `BenchmarkParseUncached` | 124,843 | 19,544 | 120 |

`Parse` stays exported and uncached; `Loader`/`ClientLoader` signatures
are unchanged, so no call sites move.

- [x] Tests pass (`go test ./internal/credbundle/ -race`: cache-hit
proof via unchanged-stat corruption, atomic-rename rotation, in-place
rewrite, missing-file error, error recovery, concurrent handshakes under
the race detector)
- [x] Appropriate changes to documentation are included in the PR (doc
comments on `Loader`/`ClientLoader`/`certCache` describe the caching,
rotation, and failure semantics)
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.

5 participants