Repository navigation
Reload TLS CA certificates (caConfig) without restart - #117387
Conversation
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.
|
Workflow [PR], commit [a617d50] Summary: ✅
AI ReviewSummaryThis PR adds hot reload for TLS trust stores from Findings
|
| 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
…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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes10 object files changed (+58.28 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units18 translation units recompiled, 87 s compile time in total, 18 of them have a recent master baseline. |
…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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
@rschu1ze @melvynator Request to backport this to 25.12 |
|
@saudurehman This PR is too big and too dangerous to backport, sorry. |
Backport #117387 to 26.8: Reload TLS CA certificates (caConfig) without restart
Backport #117387 to 26.7: Reload TLS CA certificates (caConfig) without restart
…fig) without restart
…fig) without restart
Changelog category (leave one):
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(andcaConfigof composableprotocols) are now reloaded without a restart when the file changes or onSYSTEM RELOAD CONFIG, in the same way ascertificateFileandprivateKeyFile. 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
CertificateReloaderalready hot-reloads the server/client certificate and key, but the trusted CA certificates (caConfig) are loaded once byPoco::Net::Contextat 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_CTXthat is shared between threads must not be modified after it is in use (seeopenssl-threads(7)), and anX509_STOREhas 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 installsSSL_CTX_set_cert_cbonce and applies the currentMultiVersionsnapshot per connection, this change:MultiVersion<CAStore>(an ownedX509_STORE) per prefix, rebuilt whenever thecaConfigpath, its modification time, orloadDefaultCAFilechanges. 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);SSL_CTX_set_cert_verify_callbackonce per context (the default server/client contexts, per-protocol contexts fromTLSHandler, and the NuRaft contexts viaregisterAdditionalContext). OpenSSL calls it instead ofX509_verify_certfor every peer certificate verification on both the client and the server side. The callback runsX509_verify_certon anX509_STORE_CTXthat is initialised with the current store and otherwise carries over everything libssl prepared (untrusted chain, SSL ex-data, DANE state,ssl_client/ssl_serverpurpose, 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 withoutcaConfigthere is no snapshot and the callback simply callsX509_verify_certon 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
caConfigpaths (and the client certificate/key, which were not watched before) are added to the files watched byConfigReloaderin both clickhouse-server and clickhouse-keeper, so replacing the file on disk is enough;SYSTEM RELOAD CONFIGworks too.Tests
tests/integration/test_reload_ca_certificate:verificationMode=relaxed; a client certificate issued by a second CA is rejected, becomes accepted once that CA is added to thecaConfigbundle (picked up automatically from the file change), and certificates of the first CA are rejected once it is removed from the bundle;url('https://...')fails withcertificate verify failedwhile the server's CA is not in the clientcaConfigand succeeds after it is swapped in;Workflow [PR]
Sync PR [sync-upstream/pr/117387]
Version info
26.9.1.550-master(included in26.9and later)26.8.14.2,26.7.16.2