Skip to content

fix(namespace): reload rotated mTLS certificates in the REST client - #8456

Merged
Xuanwo merged 2 commits into
lance-format:mainfrom
maswin:feat/rest-namespace-tls-cert-reload
Sep 25, 2026
Merged

Xuanwo merged 2 commits into
lance-format:mainfrom
maswin:feat/rest-namespace-tls-cert-reload

Conversation

@maswin

@maswin maswin commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Trino x Lance plugin started throwing CertificateExpired exception when trying to talk to iceberg rest catalog and the certs in trino co-ordinator instance got rotated.

RestNamespace::from_builder reads the mTLS certificate, key and CA once at construction
and bakes them into a single long-lived reqwest::Client. A reqwest::Identity cannot be
hot-swapped inside a live client, so the process keeps presenting the certificate it read at
startup forever.

Any environment that rotates short-lived certificates on disk therefore breaks once the
loaded certificate passes its notAfter. The server rejects the handshake and the client
sees a received TLS alert:

reqwest::Error { kind: Request, url: "https://<catalog-host>:7004/api/lance/<ns>/v1/namespace/%24/list?delimiter=%24",
  source: hyper::Error(Io, Custom { kind: InvalidData, error: "received fatal alert: CertificateExpired" }) }

(received matters: the peer is rejecting our client cert. A stale CA bundle or bad server
cert would surface locally as InvalidCertificate(Expired) / UnknownIssuer.)

Fix

RestClient now holds the client in an ArcSwap<reqwest::Client> and hands it out by value
(cheap — reqwest::Client is a handle around an Arc). Alongside it, the cert/key/CA paths,
the digest of the last-loaded content and the last-checked Instant are kept so the client
can be rebuilt.

The check is lazy, on the request path, gated by an interval:

  • If the interval has elapsed, the PEM files are re-read and the client is rebuilt only if
    the content changed
    . Content, not mtime: rotation is often a symlink swap or an atomic
    rename. A digest keeps the comparison cheap without retaining private key bytes.
  • Rebuilding drops the connection pool, which is correct (pooled connections were
    authenticated with the old certificate) and exactly why it is gated on a real change.
  • Material that fails to read or build keeps the current client and retries on the next
    check, so a truncated read mid-rotation can't replace a working identity with one the
    server would reject.
  • No background thread or task. A few-KB std::fs::read at most once per interval.
  • arc-swap was already a workspace dependency; no new dependencies.

New property tls.reload_interval_seconds, parsed alongside the other tls.* keys, with a
matching builder setter. 0 disables reloading. Properties pass through the Python and Java
bindings opaquely, so both pick this up without binding changes; the tls.* list in
RestNamespace's javadoc is updated.

Verification

cargo build -p lance-namespace -p lance-namespace-impls --features rest -p lance
cargo test -p lance-namespace-impls --features rest
cargo fmt --all
cargo clippy --all --tests --benches -- -D warnings

@github-actions github-actions Bot added bug Something isn't working A-python Python bindings A-java Java bindings + JNI A-deps Dependency updates A-namespace Namespace impls and removed bug Something isn't working labels Aug 10, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 10, 2026
/// Create a new REST namespace from builder
pub(crate) fn from_builder(builder: RestNamespaceBuilder) -> Self {
// Build reqwest client WITHOUT default headers - we'll apply headers per-request
let mut client_builder = reqwest::Client::builder();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this changes the behavior on startup. Before a bad CA didn’t drop a valid cert/key, and vice versa. read_material() now fails the whole load, and new() falls back to empty material.

Reload being atomic makes sense to me, but I think construction should probably keep the old behavior and load the identity and CA independently but i can be convinced otherwise. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're right, that wasn't intentional. Atomicity is there to protect a client that's already working, and at startup there isn't one - so failing the whole load just means one bad path takes out the good pieces too. Worst case someone has a stale tls.ssl_ca_cert lying around that doesn't matter today, and now it kills their mTLS identity as well.

Fixed: construction loads the identity and the CA separately again, and warns with the file name when one of them fails instead of dropping it silently like the old code did. Reload stays atomic since it has a working client to fall back on.

`RestNamespace::from_builder` read the mTLS certificate, key and CA once and
baked them into a long-lived `reqwest::Client`. A `reqwest::Identity` cannot be
swapped inside a live client, so a process kept presenting the certificate it
read at startup forever. Environments that rotate short-lived certificates on
disk - SPIFFE/Istio, Vault PKI, cert-manager, Netflix Metatron - therefore broke
once the loaded certificate passed its `notAfter`, with every request failing on
a received `CertificateExpired` alert until the process restarted.

The client is now published through an `ArcSwap` and rebuilt when the content of
the configured PEM files changes. The check runs on the request path, gated by
`tls.reload_interval_seconds` (default 300, `0` disables reloading), and compares
the digest of the file content rather than modification times, since rotation is
commonly an atomic rename or a symlink swap. Unchanged content leaves the client
and its connection pool untouched, and material that fails to load or build keeps
the current client instead of downgrading it mid-rotation.

Construction degrades per piece instead, since it has no working client to keep:
a CA certificate that cannot be loaded no longer drops a usable client identity,
and each piece that is skipped is logged with the file that caused it. The digest
is left unset whenever something was skipped, so the next check retries it and
upgrades to the complete material once the missing file lands.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@maswin
maswin force-pushed the feat/rest-namespace-tls-cert-reload branch from 4ac3159 to 1b4be07 Compare September 18, 2026 06:54
@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 18, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Gate recommendation: approve with a non-blocking risk.

Since the earlier assessment, the startup correction from this review thread now loads the client identity and CA independently, preserves each usable piece, and leaves incomplete material retryable. Request-path reloads still publish only a complete generation and keep the working client on partial or invalid rotations.

The remaining operational risk is verification breadth: unit tests cover reload timing, content changes, startup degradation, and retry, but not a peer-observed mTLS handshake through identity/CA rotation or a concurrent cutover. A local end-to-end mTLS rotation test would make that contract durable.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 18, 2026

@geruh geruh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome lgtm now, thanks @maswin! I'll let some others take a look

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Sep 25, 2026
@Xuanwo
Xuanwo merged commit 0423d8f into lance-format:main Sep 25, 2026
36 of 39 checks passed
BubbleCal pushed a commit that referenced this pull request Sep 26, 2026
The REST namespace loads its client identity with
`reqwest::Identity::from_pem`, which only produces a rustls identity.
`lance-namespace-impls` enables only reqwest's rustls features, but when
another crate in the same build enables reqwest's `default-tls`
(native-tls), reqwest's `ClientBuilder` defaults to native-tls and
rejects the identity when the client is built. In the workspace this
happens through `lance-examples` (via `hf-hub`'s default features),
which is why the four `rest::tls` tests from #8456 fail in the Linux CI
jobs on `main`. Downstream builds hit the same thing whenever any
dependency pulls in reqwest's default features, and then mTLS stops
working.

`build_client` now selects rustls explicitly, so the identity and CA
certificate are always applied by the backend that parsed them.

`test_malformed_ca_certificate_is_dropped_on_its_own` no longer requires
the malformed CA certificate to be rejected at build time. With
native-tls compiled in, reqwest parses the certificate when it is read,
so it is dropped at load time instead. The test still checks that the
identity survives and that the dropped material is retried.

Checked with `cargo test -p lance-namespace-impls -p lance-examples
--features lance-namespace-impls/rest --lib rest` on Linux (4 failures
on `main`, all pass here) and on macOS, and with `--features rest`
alone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-deps Dependency updates A-java Java bindings + JNI A-namespace Namespace impls A-python Python bindings bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants