8000 feat(access): present a client certificate on HTTPS connections to ClickHouse by khrisrichardson · Pull Request #2171 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content

feat(access): present a client certificate on HTTPS connections to ClickHouse - #2171

Open
khrisrichardson wants to merge 1 commit into
Altinity:0.28.0from
khrisrichardson:access-client-certificate
Open

khrisrichardson wants to merge 1 commit into
Altinity:0.28.0from
khrisrichardson:access-client-certificate

Conversation

@khrisrichardson
@khrisrichardson khrisrichardson commented Nov 23, 2026 •
Copy link
Copy Markdown

Part 1 of #2163: the operator can present a client certificate to ClickHouse.

What

New clickhouse.access.clientCertFile and clickhouse.access.clientKeyFile. When both are set, the operator and the metrics exporter present that certificate on every HTTPS connection, so a server with verificationMode: strict accepts them. The operator user still authenticates with its password.

How

  • The pair is read with tls.LoadX509KeyPair at each new TLS handshake (GetClientCertificate), keeping the last good certificate if a read fails. Any rotation, including a kubelet ..data swap or an in-place rewrite, is used without a restart.
  • A pair that fails to load (a key that doesn't match, missing files) keeps the last good one and is logged once. Before the first good load, the handshake fails instead of going out without a certificate.
  • verify is unchanged: pair mTLS with verify: Strict.
  • With the fields unset, the TLS registry keys and DSNs are exactly as before.
  • The CRD, config, install bundles and Helm values are regenerated, and the docs are updated.

Tests

  • Unit tests:
    • a handshake against a strict test server;
    • rotation, including a symlink swap and an in-place rewrite with an identical inode, mtime and size;
    • a mismatched pair, deleted files, a repeated failure;
    • the registry key.
  • e2e test_010089, with and without the certificate, rotated to an untrusted one and back on the same pod. It passed on a local minikube against this commit.

Possible follow-ups

  • Part 2, certificate authentication of the operator user, once a driver approach is agreed.
  • Mounting the Secret with Helm: the chart can't add a volume today. I can add extraVolumes/extraVolumeMounts, or a clientCertSecretRef, whichever you prefer.

Disclosure: drafted with AI assistance; I reviewed the code and text before submitting.

🤖 Generated with Claude Code

@ringerc
ringerc commented Nov 24, 2026
Copy link
Copy Markdown

It doesn't look like this can actually be used with the Altinity-shipped Helm chart. They don't have any way to inject additional volumes and mounts.

This probably requires a pre-req PR to add extraVolumes & extraMounts to the Helm chart and other deployment templates. It could arguably just be rolled into this PR depending on project policies re PR separation and maintainer preferences, but keeping it separate would be cleaner IMO.

It doesn't add support for backup Jobs. The PR body should probably call it out as a limit and cite #2141. It should also incorporate any needed changes so that when a fix for #2141 does land, this is actually usable for backup jobs too.

I think this can be made to support backup Job s without needing #2141, and it's probably better that way. I suggest changing the interface for this PR so it can support a cert/key pair in a kube Secret directly, not just by file path and mount. (I suggest you ALSO keep the file-path based mechanism; some cert injection only supports file-path, some only supports Secret). This would be something like a clickhouse.access.clientCertSecretRef option containing a Secret name for a secret containing cert and key, and optionally CA validation bundle (CA bundle should also be configurable as separate Secret as this is common). Then the operator could inject this into the Jobs it creates. The Backup job would need to (1) mount the Secret into the clickhouse-backup container (2) set CLICKHOUSE_TLS_CERT, CLICKHOUSE_TLS_KEY and CLICKHOUSE_TLS_CA (3) set CLICKHOUSE_SECURE=true.

An LLM review of this PR had the following additional comments:

LLM review findings

I've read the issue and the PR's Go changes, and I'm about to write the review. I haven't run anything. The tests, e2e and the verbosity default below are unverified. My local master is stale against the PR's base, so I ignored the file-stat diff.

Review of PR 2171 (part 1 of #2163)

What it does: It adds clickhouse.access.clientCertFile and clientKeyFile. The operator and metrics exporter present that certificate on HTTPS handshakes through tls.Config.GetClientCertificate. The pair is cached and reloaded when file identity, mtime or size changes. The registry key and DSN are unchanged when the fields are unset.

Does it fix #2163? Only the mTLS-handshake half, which the issue's own "Impact" section names as the main point. The PR is explicit about this. It leaves four items open:

  • certificate auth of the operator user, including the X-ClickHouse-SSL-Certificate-Auth header;
  • no-auth mode for an Envoy proxy;
  • the backup sidecar;
  • a way to mount the Secret.

That scoping matches what was agreed in the issue thread.

Main concerns

  1. It can't be used from the shipped install paths. The Deployment and Helm chart have no way to mount a Secret into the operator or exporter containers. Users must patch the Deployment by hand, and the PR says so. Until the mount exists, the feature isn't usable as shipped. I'd want extraVolumes/extraVolumeMounts in this PR, or at least a stated follow-up.

  2. The loader is more complicated than it needs to be. client_cert.go is about 180 lines of stamp comparison, failure memoisation and a shared loader registry. GetClientCertificate runs only on a new connection, and the pool caps connection lifetime at 10 minutes. Calling tls.LoadX509KeyPair on every handshake, with a last-good fallback, would cost microseconds and drop the stamp logic. The stamp logic also has gaps:

    • An in-place rewrite with the same inode, mtime and size is missed.
    • Remembering the failure by error text means a stat error followed by the same bad files re-parses and re-logs.

    That is a lot of state machine for a feature that the PR's own timing argument says doesn't need it.

  3. Reload warnings are at V(1). keepOrFail logs "unable to reload client certificate, keeping the previous one" with l.V(1).F().Warning. If the default operator verbosity is below 1, a failed rotation is invisible until the old certificate expires. I didn't check the default behind ${VERBOSITY}. A failed rotation should be logged at default verbosity, at least after a small delay-retry to allow for a partially written/replaced cert/key pair.

  4. A lone path is dropped silently. Setting only one of clientCertFile/clientKeyFile logs an error at config load and falls back to non-mTLS. A missing file, by contrast, makes the handshake fail. Both are misconfigurations, so they should behave the same. A hard failure is better, because the operator would otherwise connect without a certificate and fail confusingly against a strict server.

Smaller points

  • Mismatch handling is correct. LoadX509KeyPair rejects a mismatched cert and key, and stamps are taken before the read, so a rotation that straddles the read gets re-read on the next handshake.
  • Reload is on handshake, not a file watcher. Ringerc suggested a watcher, and the author's counterargument is sound: the ..data symlink swap, and the fact that the pool bounds switch-over anyway. Fine for part 1.
  • The client certificate is operator-scoped only. OverlayTLS deliberately doesn't overlay it, so a per-CHI security.tls can't override it. That's reasonable, but the docs should say it.
  • The new endpoint path is covered. All callers go through NewClusterConnectionParamsFromCHOpConfig and NewEndpointConnectionParams, and both call SetClientCertificate, so I found no path that skips the certificate.
  • Cluster-level params look fine. Clone keeping GetClientCertificate is asserted by the unit tests, which use a real strict-mode handshake. I haven't run them.
  • Generated files are large but mechanical. The PR touches many files, mostly generated install bundles that all get the same additions.

Verdict

The approach is correct and the scope is sensible. I'd ask for these before merge:

  • the volume-mount story (item 1);
  • default-visibility logging of reload failures (item 3);
  • consistent hard failure for a lone path (item 4).

I'd also suggest simplifying the loader (item 2), but that's optional if the maintainers are happy with the tests.


The agent also complained

mTLS doesn't verify the server unless verify is set. With only a client certificate, resolveInsecureSkipVerify still returns InsecureSkipVerify=true. The operator then presents its certificate to an unverified server. The certificate isn't secret, but this is a security trap. It follows ringerc's request to avoid side effects, and the docs and comment explain it. I'd still add a startup warning when a client certificate is set with verify empty or None.

but I don't really agree with this; verifying the server and the server verifying the client are different things.

... and some LLM commentary on the backup / Job support

LLM commentary on Backup support

#2141 is about letting users customise the backup Job and Pod: a jobTemplate with labels, volumes, env, extra containers and so on. If it lands with volumes and env, a user could mount a Secret and set CLICKHOUSE_TLS_* themselves. For that to work, this PR needs a few changes. I haven't read the backup builder code, so what follows is based on #2141 and the earlier grep.

  1. Use a Secret-based interface, not only file paths. Backup pods are built by the operator, so they can't reuse the operator's file-path settings. Either of these works:

    • Add clickhouse.access.clientCertSecretRef (Secret name, with cert, key and CA keys). The operator mounts it and sets the file paths itself.
    • Keep paths and let each consumer mount the Secret.

    With a Secret reference, the same setting can drive the operator, the exporter and the backup Job. The Helm mount question also goes away.

  2. Make backups consume the same setting. In the backup builder, when a client cert is configured:

    • mount the Secret into the clickhouse-backup container;
    • set CLICKHOUSE_TLS_CERT, CLICKHOUSE_TLS_KEY and CLICKHOUSE_TLS_CA;
    • set CLICKHOUSE_SECURE=true.

    ClickHouseBackupEnv* constants live in pkg/model/chi/config/const.go. Use them there rather than adding string literals.

  3. Don't make the env injection depend on ClickHouseBackup CR provides no way to configure pod labels/attributes and other fields #2141. If ClickHouseBackup CR provides no way to configure pod labels/attributes and other fields #2141's jobTemplate merge allows only user-supplied fields, operator-injected env and volumes still need to survive the merge. Pick a precedence rule now: the user's explicit env wins over the injected one. Test it.

  4. Handle CLICKHOUSE_SKIP_VERIFY explicitly. The builder already sets it. In mTLS mode it should follow the same verify setting as the operator, not a separate default. Otherwise backups skip server verification while the operator doesn't.

  5. Add a backup e2e test. Use the strict-mode CHI from test_010089 and run a backup and restore against it. This is the real check that backup mTLS works, and it will catch the issues above.

  6. Document the limit now. Until the backup side lands, say in the PR and docs that backups don't support strict mTLS yet.

If you'd rather keep this PR small, items 1 and 6 are the minimum. Item 1 is the one that is costly to change later. A public config shape of file paths only would need a second field for the Secret.

…ickHouse

Add clickhouse.access.clientCertFile and clickhouse.access.clientKeyFile.
When both are set, the operator (and the metrics exporter) presents that
certificate on every HTTPS connection to ClickHouse, so servers running
with verificationMode=strict accept it. The operator user still
authenticates with its password over the mutually-authenticated
connection.

- Both or neither: a lone path is logged at config load and every HTTPS
  connection fails, the same as missing files, rather than connecting
  without a certificate. The files are not read at load.
- The pair is read with tls.LoadX509KeyPair on every new TLS handshake
  through GetClientCertificate, so any rotation (an in-place rewrite, or
  a kubelet ..data symlink swap) is used without a restart. The cost is
  one read and parse per handshake; the operator's connections are
  pooled, so handshakes are rare.
- A pair that fails to load, including a certificate whose key does not
  match it or files that have gone missing, keeps the last good one, so
  deleting the files does not withdraw a certificate in use; revoking it
  is the server's decision. The failure is logged as a warning at default
  verbosity, once per distinct failure. Before the first good load the
  handshake fails rather than going out without a certificate.
- A client certificate counts as a TLS knob for the tls_config registry
  key, so it never shares the legacy registration. The paths enter the
  key quoted, so a path containing the separator cannot collide. Keys,
  DSNs and TLS configs are unchanged when the fields are unset.
- verify polarity is unchanged: setting a client certificate changes
  nothing else, so pair mTLS with an explicit verify: Strict.
- The client certificate is operator-wide: a CHI's security.tls cannot
  set or override it. Backup Jobs do not present it yet (Altinity#2141).
- CRD, config.yaml, install bundles and the Helm chart regenerated; docs
  updated, including the fallback above, the two limits above, and that
  a subPath mount never receives updates.
- e2e test_010089 (TestFlows): a strict server with and without the
  client certificate, and rotation to an untrusted certificate and back
  with the same operator pod. Passes on a local minikube (v1.39.0,
  Kubernetes v1.37.0, docker driver) in 6m22s.

Refs Altinity#2163

Signed-off-by: Khris Richardson <khris.richardson@gmail.com>
@khrisrichardson
khrisrichardson force-pushed the access-client-certificate branch from ae2d6ca to 9d60d66 Compare November 24, 2026 01:24
@khrisrichardson
Copy link
Copy Markdown
Author

Thanks for the careful review, and for passing the LLM notes along.

On Helm: as the description notes, the chart can't mount a volume into the operator or exporter today, so this PR can only be used with a patched Deployment. I agree a separate PR is cleaner. Proposed order:

  1. A small Helm PR adding extraVolumes/extraVolumeMounts for the operator and exporter containers, so the file paths here work from the chart.
  2. After ClickHouseBackup CR provides no way to configure pod labels/attributes and other fields #2141 lands, a clientCertSecretRef (cert and key, plus an optional CA bundle as its own Secret) that the operator injects into the backup Jobs: mount the Secret and set CLICKHOUSE_TLS_CERT/_KEY/_CA and CLICKHOUSE_SECURE=true. The file-path fields stay, since some cert injection only supports files.

Does that order work for you? If you'd rather have the Secret ref before #2141, I can look at that instead.

Changes pushed to this PR:

  • Simpler loader (item 2 of the LLM notes): the stat/identity stamp is gone. The pair is now read with tls.LoadX509KeyPair on every new handshake, keeping the last good certificate if the read fails. That also fixes the in-place rewrite with the same inode, mtime and size, which now has a test. The cost is one read and parse per handshake, and the operator's pooled connections handshake rarely.
  • Reload warnings: still logged once per distinct failure, and logged again after a good load in between. They are now plain Warning calls without V(1). For what it's worth, Announcer.Warning ignores the verbosity level and always logs, so these were already visible at default verbosity; the code now reads the way it behaves.
  • A lone clientCertFile or clientKeyFile is now a hard failure, like missing files: it is logged at config load and every https connection fails, rather than connecting without a certificate.
  • Docs: a CHI's security.tls can't set or override the client certificate (it is operator-wide), and backup Jobs don't support strict mTLS yet (ClickHouseBackup CR provides no way to configure pod labels/attributes and other fields #2141).

On server verification: agreed, that's separate. It stays tied to verify, as now.

Disclosure: drafted with AI assistance; I reviewed the code and text before submitting.

@ringerc
ringerc commented Nov 24, 2026
Copy link
Copy Markdown

@khrisrichardson I think (2) can be done now, as I don't believe the clientCertSecretRef approach depends on #2141. It would make it possible for backups to use TLS certs now.

@khrisrichardson
Copy link
Copy Markdown
Author

Agreed, (2) doesn't need #2141. Reading the backup code, the certificate actually belongs on the clickhouse-backup sidecar rather than the Jobs, since the Jobs only call its REST API. I'll open it as a separate PR on top of this one, so this one stays reviewable.

khrisrichardson added a commit to khrisrichardson/clickhouse-operator that referenced this pull request Nov 25, 2026
…extraInitContainers

Let the chart add volumes, volume mounts and containers to the operator
pod, for example to provide the client certificate that
clickhouse.access.clientCertFile/clientKeyFile point to (Altinity#2171): from a
Secret, or written by a sidecar such as spiffe-helper. extraInitContainers
allows a native sidecar (restartPolicy: Always, Kubernetes 1.33+), which
starts before the operator. The clickhouse-operator and metrics-exporter
each read their own copy of the operator configuration, so each gets its
own mounts. All five default to empty and render nothing.

Refs Altinity#2163, Altinity#2171

Signed-off-by: Khris Richardson <khris.richardson@gmail.com>
@khrisrichardson
Copy link
Copy Markdown
Author

@ringerc FYI: #2173 adds extraVolumes/extraVolumeMounts and extraContainers/extraInitContainers to the Helm chart, so the client certificate for this PR can be mounted from a Secret or written by a spiffe-helper sidecar (with an example of each).

@khrisrichardson
Copy link
Copy Markdown
Author

Could a maintainer approve the workflow runs for this PR? As a first-time contributor here, CI is waiting on approval and hasn't run yet.

khrisrichardson added a commit to khrisrichardson/clickhouse-operator that referenced this pull request Dec 4, 2026
…extraInitContainers

Let the chart add volumes, volume mounts and containers to the operator
pod, for example to provide the client certificate that
clickhouse.access.clientCertFile/clientKeyFile point to (Altinity#2171): from a
Secret, or written by a sidecar such as spiffe-helper. extraInitContainers
allows a native sidecar (restartPolicy: Always, Kubernetes 1.33+), which
starts before the operator. The clickhouse-operator and metrics-exporter
each read their own copy of the operator configuration, so each gets its
own mounts. All five default to empty and render nothing.

Refs Altinity#2163, Altinity#2171

Signed-off-by: Khris Richardson <khris.richardson@gmail.com>

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

0