Skip to content

Reload TLS CA certificates (caConfig) without restart - #117387

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
sanjams2:ca-hot-reload
Sep 2, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
sanjams2:ca-hot-reload

Conversation

@sanjams2

@sanjams2 sanjams2 commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

TLS CA certificates configured with openSSL.server.caConfig / openSSL.client.caConfig (and caConfig of composable protocols) are now reloaded without a restart when the file changes or on SYSTEM RELOAD CONFIG, in the same way as certificateFile and privateKeyFile. This makes it possible to rotate the CA that is used to verify clients, other servers and Keeper nodes (including Raft connections between Keepers) without restarting anything.


Motivation

CertificateReloader already hot-reloads the server/client certificate and key, but the trusted CA certificates (caConfig) are loaded once by Poco::Net::Context at startup and never refreshed. Rotating an internal root CA therefore requires restarting every server and Keeper, which is not practical when CAs are rotated frequently and have to be picked up within minutes.

How it works

An SSL_CTX that is shared between threads must not be modified after it is in use (see openssl-threads(7)), and an X509_STORE has no API to remove certificates (lookups up-ref objects after dropping the store lock, so objects must stay alive as long as the store). So, just like the existing certificate reload does not touch the context and instead installs SSL_CTX_set_cert_cb once and applies the current MultiVersion snapshot per connection, this change:

  • keeps a MultiVersion<CAStore> (an owned X509_STORE) per prefix, rebuilt whenever the caConfig path, its modification time, or loadDefaultCAFile changes. The store is built by the same Poco code that builds it at startup, so after a reload the set of trusted certificates is exactly what a restart would give (file, hashed directory and default CAs behave the same);
  • installs SSL_CTX_set_cert_verify_callback once per context (the default server/client contexts, per-protocol contexts from TLSHandler, and the NuRaft contexts via registerAdditionalContext). OpenSSL calls it instead of X509_verify_cert for every peer certificate verification on both the client and the server side. The callback runs X509_verify_cert on an X509_STORE_CTX that is initialised with the current store and otherwise carries over everything libssl prepared (untrusted chain, SSL ex-data, DANE state, ssl_client/ssl_server purpose, verify params such as depth/host/auth level/flags, and the per-connection verify callback used by Poco and boost::asio), then reports error, depth and the verified chain back. For a prefix without caConfig there is no snapshot and the callback simply calls X509_verify_cert on the original context, i.e. today's behaviour.

In-flight handshakes keep the snapshot they started with; new handshakes use the new one. Established connections and TLS sessions are not re-verified (same as for certificate reload).

The caConfig paths (and the client certificate/key, which were not watched before) are added to the files watched by ConfigReloader in both clickhouse-server and clickhouse-keeper, so replacing the file on disk is enough; SYSTEM RELOAD CONFIG works too.

Tests

tests/integration/test_reload_ca_certificate:

  • server side: HTTPS with verificationMode=relaxed; a client certificate issued by a second CA is rejected, becomes accepted once that CA is added to the caConfig bundle (picked up automatically from the file change), and certificates of the first CA are rejected once it is removed from the bundle;
  • client side: url('https://...') fails with certificate verify failed while the server's CA is not in the client caConfig and succeeds after it is swapped in;
  • Keeper: a 3-node Raft cluster over TLS goes through a full CA rotation (trust old+new → switch node certificates → drop old CA) with Raft connections killed after each step; the cluster keeps working and the Raft port rejects client certificates from the old CA afterwards.

Workflow [PR]
Sync PR [sync-upstream/pr/117387]

Version info

  • Merged into: 26.9.1.550-master (included in 26.9 and later)
  • Backported to: 26.8.14.2, 26.7.16.2

CertificateReloader already hot-reloads the certificate and private key, but
the trusted CA certificates were loaded once by Poco at startup and never
refreshed, so rotating a CA required restarting every server and Keeper.

Keep a MultiVersion snapshot of an X509_STORE per prefix, rebuilt whenever the
caConfig path, its modification time or loadDefaultCAFile changes, and verify
peer certificates against the current snapshot from an
SSL_CTX_set_cert_verify_callback installed once per context (default server
and client contexts, per-protocol contexts and the NuRaft contexts). The
SSL_CTX itself is never modified after initialization, same as for the
existing certificate callback.

The caConfig paths (and the client certificate and key) are now also watched
by ConfigReloader in clickhouse-server and clickhouse-keeper, so replacing the
file on disk is enough to trigger the reload.
@fm4v fm4v added the can be tested Allows running workflows for external contributors label Aug 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a617d50]

Summary: ✅


AI Review

Summary

This PR adds hot reload for TLS trust stores from openSSL.*.caConfig, wires the new verification callback through server, protocol, and Keeper contexts, and updates the docs/tests accordingly. The main CA-store swap logic is coherent, but the config watcher still freezes the watched TLS paths at startup, so a caConfig path change only works for the explicit SYSTEM RELOAD CONFIG that applied it and is not watched afterwards.

Findings
⚠️ Majors
  • [src/Common/Config/ConfigReloader.cpp:230] The new watcher logic only follows the startup extra_paths snapshot. After SYSTEM RELOAD CONFIG changes openSSL.*.caConfig or protocols.*.caConfig to another file/directory, later on-disk rotations of that new target do not trigger background reloads because ConfigReloader never refreshes its watched TLS path list. programs/keeper/Keeper.cpp:645-650 has the same frozen-path issue. Suggested fix: rebuild the watched TLS path set from the newly loaded config (or update extra_paths) after each successful reload.
Tests
  • ⚠️ Add a regression that changes caConfig to another file or directory, runs SYSTEM RELOAD CONFIG once, then modifies the new target on disk and verifies the CA store is picked up without another explicit reload.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit a617d50.

Metric Baseline Current Δ
Lines 88.60% 88.60% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 80.90% 80.90% +0.00%

Changed lines: Changed C/C++ lines covered: 164/188 (87.23%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 31, 2026
Comment thread src/Server/CertificateReloader.cpp Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Aug 31, 2026
Comment thread src/Server/CertificateReloader.cpp Resolved
Comment thread src/Server/CertificateReloader.cpp Resolved
…in system.certificates

The reloaded CA store read loadDefaultCAFile with a fallback of false, while
Poco::Net::SSLManager, TLSHandler and PostgreSQLHandler create their contexts
with a default of true. A configuration with caConfig and no explicit
loadDefaultCAFile would therefore stop trusting the default CA certificates as
soon as CertificateReloader took over verification. Use Poco's default.

Keeper's Raft contexts default loadDefaultCAFile to false, so they share the
reloaded CA certificates only if their setting matches the primary context
(always the case when it is set explicitly) and keep their own store otherwise.

system.certificates took the CA paths from the contexts created at startup.
Take them from CertificateReloader when it manages the CA certificates of the
prefix, so that the table follows a changed caConfig path after a reload.

Add tests for both.
Comment thread src/Coordination/KeeperServer.cpp Resolved
Comment thread src/Server/CertificateReloader.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a617d50a7 with master 8627fa22f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

10 object files changed (+58.28 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Server/CertificateReloader.cpp.o 155.35 KiB 202.89 KiB +47.54 KiB (+30.60%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

18 translation units recompiled, 87 s compile time in total, 18 of them have a recent master baseline.

Job report

…efaultCAFile, and of caConfig directories

Keeper creates the contexts for Raft connections itself and, unlike Poco,
does not trust the default CA certificates when loadDefaultCAFile is not
configured. Instead of skipping the CA reload for such contexts, keep a second
set of CA certificates for them that is loaded from the same caConfig with
their own default, so that Raft connections always follow caConfig and keep
trusting exactly what they trusted at startup.

When caConfig is a directory, also take the names and modification times of the
files in it into account, and let ConfigReloader watch the files in it, so that
replacing a certificate file in place is noticed without SYSTEM RELOAD CONFIG.

files.emplace(path_to_add);

/// E.g. a directory with CA certificates, a change of a file in it should be noticed too.

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.

addIfExists now notices changes inside a directory, but the set of roots it walks is still the constructor-time extra_paths snapshot. programs/server/Server.cpp:2676-2693 and programs/keeper/Keeper.cpp:645-650 build that list once at startup, and ConfigReloader never refreshes it after a successful SYSTEM RELOAD CONFIG.

That means a config reload can switch openSSL.*.caConfig (or protocols.*.caConfig) to a different file or directory, but later rotations of that new target will not trigger automatic reloads because the watcher is still following the old path. The new test_system_certificates_follows_reload and test_ca_directory cases both mask this by issuing another SYSTEM RELOAD CONFIG after the path change.

This breaks the PR's advertised contract that the certificates referenced by the current config are watched. Please rebuild the watched TLS path list from the newly loaded config (or otherwise update extra_paths) after each successful reload, and add a regression that changes caConfig, reloads once, then verifies the new target is picked up without another explicit reload.

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.

This is pre-existing rather than new here. ConfigReloader has taken extra_paths at construction since certificate watching was added in 062b1c4, so certificateFile/privateKeyFile have the same behaviour on master today; this PR just adds the CA paths to that list.

The path change itself is applied by the SYSTEM RELOAD CONFIG that makes it, and the normal rotation flow (replacing the contents of the configured file/dir in place) is fully covered by the watcher.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 2, 2026
Merged via the queue into ClickHouse:master with commit 8ad3a1d Sep 2, 2026
175 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 2, 2026
@saudurehman

Copy link
Copy Markdown

@rschu1ze @melvynator Request to backport this to 25.12

@rschu1ze

Copy link
Copy Markdown
Member

@saudurehman This PR is too big and too dangerous to backport, sorry.

@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 27, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 27, 2026
rschu1ze added a commit that referenced this pull request Sep 27, 2026
Backport #117387 to 26.8: Reload TLS CA certificates (caConfig) without restart
rschu1ze added a commit that referenced this pull request Sep 27, 2026
Backport #117387 to 26.7: Reload TLS CA certificates (caConfig) without restart
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants