Skip to content

feat(agent): select the upstream client certificate per connection, and put the source identity in the pool key (#842) - #863

Merged
bpalermo merged 2 commits into
mainfrom
feat/842-hashable-source-identity
Sep 20, 2026
Merged

bpalermo merged 2 commits into
mainfrom
feat/842-hashable-source-identity

Conversation

@bpalermo

@bpalermo bpalermo commented Sep 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #842.

Replaces the per-identity transport_socket_matches + transport_socket_matcher on every mesh cluster with one transport socket whose custom_tls_certificate_selector resolves the client certificate per connection, and drops connection_pool_per_downstream_connection.

Both halves are the same mechanism — the source SPIFFE ID in a downstream-shared filter-state object — so they ship together. Removing the flag without the hashable key re-opens the #831 leak, and it fails open.

The filter state

Mesh-originating chains stamp a third object, under the name Envoy's filter_state_override upstream certificate mapper hardcodes (envoy.tls.certificate_mappers.on_demand_secret), with the envoy.hashable_string factory.

That factory is the whole point: set_filter_state's default envoy.string builds a Router::StringAccessorImpl, which is not Envoy::Hashable, and CommonUpstreamTransportSocketFactory::hashKey folds a shared filter-state object into the upstream pool key only behind a dynamic_cast to Hashable.

aether.source.spiffe_id keeps being stamped for one release: it is what a pre-#842 cluster's matcher reads, so rollback stays hitless (the dual-key overlap #815 used), and it is the access log's source_spiffe_id. Retiring it and the netns copy together is a follow-up.

The cluster

custom_tls_certificate_selector:
  envoy.tls.certificate_selectors.on_demand_secret
    config_source:      ads (the agent's own stream)
    certificate_mapper: ...upstream_certificate_mappers.filter_state_override
                          default_value: <node SVID>
    prefetch_secret_names: [<node SVID>]

The mapper returns the filter-state string verbatim as the SDS secret name — which works only because aether's secret names are SPIFFE IDs (SpiffeIDFromPod feeds both ends).

Consequence beyond tidiness: a mesh cluster now has no workload input at all. #815 release two made cluster bytes invariant under pod churn within a ServiceAccount; the first pod of a new ServiceAccount arriving, and the last one leaving, still rewrote every mesh cluster on the node — each re-warming for the full 15 s EDS initial_fetch_timeout and draining every upstream pool on the warming→active swap. Those are free now.

max_session_keys: 0 (from #834, for #829) becomes a precondition rather than just a defence: a client context supports a custom certificate selector only with session resumption off.

Decisions called out in the issue

default_value = the node SVID (spiffe://<td>/ns/aether-system/sa/aether-agent, the agent's own workload ID — not a /node/ ID, per #825). It is what a connection with no source filter state gets, which is exactly the set of connections the removed matcher's on_no_match served, and the same identity it presented. Same behaviour, same failure signature to watch, no new semantics. min_len: 1 is satisfied structurally: the cache emits no socket at all before the node SVID is served.

The inboundready_<pod> probe cluster is untouched. It has no selector — it keeps its statically named node-SVID certificate and its own MaxSessionKeys: 0 (#836/#840). A health checker carries no filter state, so had it used the selector it would have taken default_value on every probe: the same certificate, by a longer route, while breaking the #836 boundary that nothing the probe does may be observable by application traffic.

The edge is untouched. One identity, no source filter state, and SDS comes straight from the SPIRE agent rather than the agent's ADS stream. A selector would add a first-connection handshake pause and buy nothing.

The waypoint split survives, as a constant. SNI is a property of the transport socket, not of the certificate, so a waypoint-enabled cluster still needs two sockets — but they are now a fixed two (local, waypoint) selected by endpoint metadata, independent of the node's identity set, with bounded match names instead of SPIFFE IDs.

⚠ BREAKS A LIVE ALERT — cross-repo, needs a k8s-talos-main change

envoy_cluster_match_count_total{aether_transport_socket_match=...} no longer exists for mesh clusters, because they have no transport-socket matches. AetherClusterIdentityNoMatch (k8s-talos-main, GitOps #109) therefore matches nothing and evaluates to a clean zero — a vacuous gate, not an error, so nothing surfaces the loss.

It must be removed or repointed in the same window this ships. docs/runbook.md, "#842", proposes the replacement, in order of strength:

  1. the destination access log's downstream_peer_uri_san — already documented as the authoritative cross-check, now the primary;
  2. a - source_spiffe_id on source-reporter lines, which is precisely the condition that makes the mapper fall back to default_value (and is per request, not per connection);
  3. cluster.<c>.on_demand_secret.{cert_requested,cert_updated,cert_active} — the selector's own stats. A liveness signal for the mechanism; they do not identify which identity.

Handshake-pause cost

On-demand SDS pauses the handshake on the first connection per (secret name, worker, cluster). The fetch is a local round trip to the node agent over its ADS UDS, with the secret already in the snapshot.

The cluster itself does not warm on any of it, and that is a change in the other direction: a statically referenced SDS certificate is fetched through a warming provider, so a client certificate the agent failed to serve used to hold its cluster in warming for initial_fetch_timeout and then bring it up with upstream_context_secrets_not_ready. The on-demand path creates its providers with warm=false (SecretManagerImpl::DynamicSecretProviders::findOrCreate), so no mesh cluster blocks on a client-certificate secret any more — including the prefetched node SVID. The cost moves from cluster warming, which is node-wide, to one paused handshake, which is not.

prefetch_secret_names carries the node SVID, so the no-filter-state path never pauses at all. Workload identities are deliberately not prefetched — listing them would put the node's identity set back into every cluster's bytes, the exact churn #815 and this change remove.

Net: strictly cheaper than what it replaces. connection_pool_per_downstream_connection forced a full mTLS handshake on every downstream connection, measured at ~25 ms of a 29 ms p50 hop (#735).

Tests

//test/mtlspool gained a real SDS server — certificate resolution now happens during the handshake, so file-backed certs no longer exercise it — and three distinct assertions against the pinned Envoy, plus the negative control. The single variable between the positive and negative runs is the object factory, applied by rewriting production's own output.

--- PASS: TestSourceIdentityIsNotPooledAcrossServiceAccounts (0.94s)
      A req[0..10]: destination verified .../sa/source-a on upstream connection 1
      B req[0..10]: destination verified .../sa/source-b on upstream connection 2
--- PASS: TestUpstreamPoolsPartitionByIdentity (0.56s)
      upstream connections: source-a=[1] source-b=[2] total=2
--- PASS: TestCertificateMapperSelectsPerConnectionCertificate (1.06s)
--- PASS: TestSharedPoolLeaksSourceIdentity (0.91s)
      source-b requests verified as something other than source-b: 11/11

total=2 is the observable that distinguishes #842 from the old flag: 1 would be the leak, 22 would be per-downstream-connection pooling. --concurrency 1 is what makes the exact number meaningful.

//test/envoy_validate gained a non-vacuity guard — after checking that an empty default_value really does make envoy --mode validate fail:

error initializing configuration: Proto constraint validation failed
  (ConfigValidationError.DefaultValue: value length must be at least 1 characters)

That probe also established that the TypedExtensionConfig name is informational — a corrupted one validates fine, since resolution is by type URL — so the names are pinned in a unit test instead, and the comments that claimed otherwise were corrected.

Inverted rather than deleted, each with the issue number on it:

  • TestServiceClusterBytesChangeOnFirstAndLastPodOfServiceAccount → must now be unchanged;
  • TestCachedMTLSClusterInvalidatedOnWorkloadChange → byte-identity across a new-ServiceAccount ADD/DEL;
  • TestNodeProxyPerSourceClustersPoolPerDownstreamConnection → asserts the flag is off and max_session_keys is 0, together;
  • TestNoNetnsPathInServiceClusterBytes → now also forbids any workload SPIFFE ID in a cluster's bytes.

bazel test //... — 107/107 pass. Lint clean. make deps-audit OK (no new modules; the bindings already ship in go-control-plane/envoy v1.39.0, and both extensions are present in the pinned proxy binary).

Not established

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

@bpalermo
bpalermo force-pushed the feat/842-hashable-source-identity branch 2 times, most recently from 3f32dca to 43f4b87 Compare September 20, 2026 14:55
…nd put the source identity in the pool key (#842)

Replaces the per-identity `transport_socket_matches` + `transport_socket_matcher`
on every mesh cluster with ONE transport socket whose
`custom_tls_certificate_selector` resolves the client certificate per
connection, and drops `connection_pool_per_downstream_connection`.

Both halves are the same mechanism — the source SPIFFE ID in a
downstream-shared filter-state object — so they ship together.

## The filter state

Mesh-originating chains now stamp a third object, under the name Envoy's
`filter_state_override` upstream certificate mapper hardcodes
(`envoy.tls.certificate_mappers.on_demand_secret`) and with the
`envoy.hashable_string` factory. That factory is the whole point:
`set_filter_state`'s default `envoy.string` builds a
`Router::StringAccessorImpl`, which is not `Envoy::Hashable`, and
`CommonUpstreamTransportSocketFactory::hashKey` folds a shared filter-state
object into the upstream pool key only behind a `dynamic_cast` to `Hashable`.

`aether.source.spiffe_id` keeps being stamped for one release: it is what a
pre-#842 cluster's matcher reads, so a rollback stays hitless (the dual-key
overlap #815 used), and it is the access log's `source_spiffe_id`.

## The cluster

    custom_tls_certificate_selector:
      envoy.tls.certificate_selectors.on_demand_secret
        config_source:      ads (the agent's own stream)
        certificate_mapper: ...upstream_certificate_mappers.filter_state_override
                              default_value: <node SVID>
        prefetch_secret_names: [<node SVID>]

The mapper returns the filter-state string VERBATIM as the SDS secret name,
which works only because aether's secret names are SPIFFE IDs. `default_value`
is the node SVID — the same identity, for the same connections, that the removed
matcher's `on_no_match` presented. Workload identities are deliberately NOT
prefetched: listing them would put the node's identity set back into every
cluster's bytes.

Consequence beyond tidiness: a mesh cluster now has NO workload input at all.
#815 release two made cluster bytes invariant under pod churn within a
ServiceAccount; the first pod of a new ServiceAccount arriving and the last one
leaving still rewrote every mesh cluster on the node, each re-warming for the
full 15 s EDS initial_fetch_timeout and draining every upstream pool on the
warming→active swap. Those are free now.

`max_session_keys: 0` (from #834, for #829) becomes a precondition rather than
just a defence: a client context supports a custom certificate selector only
with session resumption off.

## What is deliberately unchanged

- The `inboundready_<pod>` probe cluster keeps its own statically named
  certificate and its own `MaxSessionKeys: 0` (#836/#840). A health checker
  carries no filter state, so a selector there would take `default_value` on
  every probe — the same node SVID by a longer route — while breaking the #836
  boundary that nothing the probe does may be observable by application traffic.
- The edge keeps `EdgeUpstreamTransportSocket`: one identity, no source filter
  state, SDS straight from the SPIRE agent. A selector would add a handshake
  pause and buy nothing.
- The server-identity SAN pin and the SDS-rotated trust bundle. Never per-source.
- The proposal 019 waypoint split, which is about SNI, not certificates — but
  its matcher is now a constant over two fixed sockets (`local`, `waypoint`)
  instead of a two-level tree over the identity set.

## BREAKS A LIVE ALERT — cross-repo

`envoy_cluster_match_count_total{aether_transport_socket_match=...}` does not
exist for mesh clusters any more, because they have no transport-socket matches.
`AetherClusterIdentityNoMatch` (k8s-talos-main, GitOps #109) therefore matches
nothing and evaluates to a clean zero — a vacuous gate, not an error. It must be
removed or repointed in the same window this ships. docs/runbook.md, "#842",
proposes the replacement: the destination access log's
`downstream_peer_uri_san` (already the authoritative check) plus a `-`
`source_spiffe_id` on source-reporter lines, which is exactly the condition that
makes the mapper fall back to `default_value`.

## Tests

`//test/mtlspool` gained a real SDS server (certificate resolution now happens
DURING the handshake, so file-backed certs no longer exercise it) and three
distinct assertions against the pinned Envoy, plus the negative control:

  - 11/11 requests from each source verified as that source;
  - EXACTLY TWO upstream connections, one per identity — the observable that
    distinguishes #842 from the old flag, which would give 22;
  - each connection presented the originating pod's own certificate, never
    `default_value`;
  - negative control, `envoy.string` instead of `envoy.hashable_string` as the
    ONLY change: 11/11 of source-B's requests verified as source-A, on ONE
    shared connection.

`//test/envoy_validate` gained a non-vacuity guard, after checking that an empty
`default_value` really does make `envoy --mode validate` fail. It also
established that the TypedExtensionConfig `name` is informational — a corrupted
one validates fine — so the names are pinned in a unit test instead.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo force-pushed the feat/842-hashable-source-identity branch from 43f4b87 to 239e5ea Compare September 20, 2026 14:56
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.

Make the source-identity filter state Hashable so the mesh can drop connection_pool_per_downstream_connection

1 participant