Skip to content

tls-util: trust the system store for postgres verify-full - #41347

Open
jasonhernandez wants to merge 1 commit into
MaterializeInc:mainfrom
jasonhernandez:jason/tls-util-system-roots
Open

jasonhernandez wants to merge 1 commit into
MaterializeInc:mainfrom
jasonhernandez:jason/tls-util-system-roots

Conversation

@jasonhernandez

Copy link
Copy Markdown
Contributor

Motivation

Postgres connections in verify-full mode without an explicit root cert could never verify a server with a publicly issued cert. mz-tls-util builds the connector with vendored OpenSSL, which looks for roots under its compiled-in /usr/local/ssl. That directory is empty in our images, so no roots were loaded and the connection failed closed. This affects Postgres sources, the metadata store client (postgres-client) and mz-debug.

Description

make_tls now starts from an empty store and picks roots by mode:

  • Root cert supplied: only that cert is trusted. As in libpq, it replaces the system store rather than adding to it, so pinning a private CA keeps public CAs out.
  • verify-full, no root cert: the system bundle (/etc/ssl/certs/ca-certificates.crt on Debian), located with openssl-probe the same way native-tls does.
  • verify-ca, no root cert: nothing, so verification fails. Without a hostname check, any publicly issued cert for any domain would pass against the system store. libpq applies the same rule to sslrootcert=system.
  • require without a root cert, disable: no change.

Clearing the store also stops roots from OpenSSL's defaults or SSL_CERT_FILE being trusted alongside an explicit root cert. In prod images that store was empty, so the only practical change is in environments that set SSL_CERT_FILE, such as the testdrive image.

Risks

A setup that relied on SSL_CERT_FILE plus an explicit root cert being trusted together now trusts only the root cert. Nothing in-repo does this. Every path still fails closed when roots are missing or unreadable.

Verification

New test verify_ca_ignores_default_store checks that verify-ca without a root cert does not fall back to the system store. The existing verify_full_trusts_default_store covers the verify-full path. Not tested against a real public-CA Postgres server or inside a production image.

Release notes

This release will verify PostgreSQL connections that use SSL MODE verify-full without SSL CERTIFICATE AUTHORITY against the system's trusted CA certificates.

🤖 Generated with Claude Code

Vendored OpenSSL looks for roots under /usr/local/ssl, which is empty in
our images, so verify-full without an explicit root cert failed against
servers with publicly issued certs. Load the system store found by
openssl-probe for verify-full only. An explicit root cert replaces it, and
verify-ca never uses it, since without a hostname check any public cert
would pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@def-

def- commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review

1. MEDIUM -- Preserve configured trust paths in verify-full

src/tls-util/src/lib.rs:106

A deployment that sets SSL_CERT_FILE and SSL_CERT_DIR to trust only its private CA now also accepts certificates for the database hostname issued by public CAs. Conversely, a deployment that supplies private roots through a colon-separated SSL_CERT_DIR can lose those roots and fail to reconnect.

Details

The base connector lets OpenSSL interpret both overrides. The replacement uses openssl-probe-0.2.2/src/lib.rs:82, which appends existing system directories even when a private directory was supplied. Its environment lookup at openssl-probe-0.2.2/src/lib.rs:121 checks the entire directory list as one filesystem path, so a valid list of two directories is discarded. Clearing the original store also drops roots installed directly under the vendored OpenSSL default, /usr/local/ssl. Preserve the configured OpenSSL locations and use system discovery as a fallback.

2. MEDIUM -- Keep client certificate intermediates available for mutual TLS

src/tls-util/src/lib.rs:75

A PostgreSQL source using SSL MODE require with a client certificate can stop authenticating after upgrade. If the client’s intermediate CA is in SSL_CERT_FILE and PostgreSQL trusts its root, clearing the store makes Materialize send only the leaf. PostgreSQL then rejects the client on reconnect.

Details

src/tls-util/src/lib.rs:80 installs one client certificate. OpenSSL previously completed its outgoing chain from the connector’s default store. With no per-connection root, require now leaves that store empty. Install the server-verification policy with set_verify_cert_store and keep the issuers needed to build the client chain available separately. OpenSSL certificate chain construction

3. MEDIUM -- Give existing verify-ca sources a migration path

src/tls-util/src/lib.rs:110

An existing verify-ca source that trusts its upstream through an operator-provided SSL_CERT_FILE stops ingesting on its first reconnect after upgrade. The PostgreSQL connection reference documents that omitting SSL CERTIFICATE AUTHORITY uses system defaults, and the proposed release note gives these operators no indication that they must change their connections.

Details

The planner accepts this combination at src/sql/src/plan/statement/ddl/connection.rs:472. The base connector used the configured CA, while this branch always leaves the verification store empty. Tell operators to set SSL CERTIFICATE AUTHORITY to their private CA before upgrading, both in the release note and at doc/user/data/examples/create_connection.yml:547. Return a configuration error naming the missing CA so new connections identify the remedy before attempting a handshake.

4. MEDIUM -- Avoid retaining a full CA bundle per source connection

src/tls-util/src/lib.rs:104

Every verify-full source connection without an explicit CA parses and retains its own copy of the system bundle. A running source holds three such connections for replication, metadata and schema validation. With a 148-certificate bundle, 60 live OpenSSL contexts used about 49 MiB more resident memory than the base directory-backed configuration.

Details

SYSTEM_ROOTS caches paths only. Each call from src/postgres-util/src/tunnel.rs:219 builds a new context and eagerly loads the file. The long-lived callers are at src/storage/src/source/postgres/replication.rs:295 and src/storage/src/source/postgres/replication.rs:548. The measurement used vendored OpenSSL 3.6.3 with SSL_CERT_DIR=/etc/ssl/certs, where the base connector could already look up the same issuers on demand. Share the parsed roots across connections using the same bundle, with refresh when it changes.

5. MEDIUM -- Handle valid Unix trust paths without panicking

src/tls-util/src/lib.rs:104

If SSL_CERT_FILE or SSL_CERT_DIR names an existing path containing non-UTF-8 bytes, opening a verify-full connection without an explicit CA panics. In clusterd this aborts the replica and interrupts its other sources.

Details

The probe preserves the raw path from var_os. The pinned Rust binding calls to_str().unwrap() on each supplied path at openssl-0.10.82/src/ssl/mod.rs:1012, before it can return an OpenSSL error. The base connector passed these environment settings directly to OpenSSL, which accepts Unix path bytes. The process-wide panic hook is installed at src/clusterd/src/lib.rs:195. Keep byte-valued paths on OpenSSL’s native lookup path, or return a configuration error before calling the binding.

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.

2 participants