Skip to content

Add the default_session_user server setting - #110179

Merged
alexey-milovidov merged 81 commits into
masterfrom
default-session-user
Aug 24, 2026
Merged

alexey-milovidov merged 81 commits into
masterfrom
default-session-user

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Adds a way to configure which user is assumed when a client connects without specifying a user name, instead of the hardcoded default.

  • A new server setting, default_session_user (default default), is applied when the user name is empty: an HTTP request without the user parameter and X-ClickHouse-User header (including Basic credentials with an empty username and a key without a user), a native protocol Hello packet with an empty user name (previously rejected, now accepted), MySQL and PostgreSQL handshakes with an empty user name, a gRPC query without user_name, an Arrow Flight call without an authorization header (or with Basic credentials with an empty user name), and the web terminal auth message without a user field. Setting the value to an empty string prohibits connections without a user name. HTTP handlers with a fixed user (the user key inside handler of an http_handlers rule, or the user key of a prometheus protocol) authenticate as their configured user and ignore the setting, including the empty reject mode.
  • Composable protocol endpoints (the protocols section) can override the setting per endpoint with a default_session_user key, so different listening ports can serve different anonymous users. The key is looked up from the endpoint's module through impl references, and the endpoint is restarted on config reload when the value changes.
  • Interserver connections never use the default session user: they identify themselves with a special marker instead of a user name and are authenticated by the cluster secret and the initial user. Configuring default_session_user on an interserver composable protocol is rejected as a configuration error.

The NativeProtocol.md specification and the composable protocols documentation are updated accordingly. Covered by the new integration test test_default_session_user (all protocols including gRPC and Arrow Flight, per-endpoint overrides, the explicit reject mode, fixed-user handlers on an endpoint with the reject mode, and a two-node cluster with a secret verifying that remote queries still run as the initiating user).

Changelog category (leave one):

  • New Feature

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

Added the default_session_user server setting: the user to authenticate as when a client connects without specifying a user name (previously hardcoded to default; the native, MySQL, and PostgreSQL protocols now accept an empty user name instead of rejecting it, and Arrow Flight honors the setting instead of its own hardcoded default). Composable protocol endpoints in the protocols section can override it per endpoint with a default_session_user key, so different listening ports can serve different anonymous users. Setting it to an empty string prohibits connections without a user name. Interserver connections never use the default session user.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

alexey-milovidov and others added 2 commits July 12, 2026 19:06
When a client connects with an empty user name, the server now authenticates
it as the configured default session user instead of the hardcoded `default`:
- HTTP: the `user` parameter, `X-ClickHouse-User` header and Basic credentials;
- native protocol: an empty user name in the `Hello` packet is now accepted
  instead of being rejected;
- MySQL and PostgreSQL wire protocols: an empty user name in the handshake;
- gRPC: an empty `user_name` in the query info;
- web terminal: an omitted user field in the auth message.

Setting the value to an empty string prohibits connections without a user name.
Interserver connections never use the default session user: they identify
themselves with a special marker instead of a user name and are authenticated
by the cluster secret and the initial user.

Composable protocol endpoints (the `protocols` section) can override the
setting per endpoint with a `default_session_user` key, so different listening
ports can serve different anonymous users; the endpoint is restarted on config
reload when the value changes.

Co-Authored-By: Claude Fable 5 <[email protected]>
pymysql substitutes the OS user name for an empty user name on the client
side, and users with a plaintext password (even an empty one) are asked for
it with `AuthenticationCleartextPassword` over the PostgreSQL protocol.

Co-Authored-By: Claude Fable 5 <[email protected]>
@mintlify

mintlify Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 12, 2026, 8:27 PM

@clickhouse-gh

clickhouse-gh Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b3ff303]

Summary: ✅


AI Review

Summary

The PR adds the default_session_user server setting, threads it through the HTTP, native TCP, MySQL, PostgreSQL, gRPC, Arrow Flight, and web terminal authentication entrypoints, and adds the composable-protocol reload logic and regression coverage needed for per-endpoint overrides and the empty-string reject mode. I reviewed the current head together with all prior review threads and did not find a remaining correctness, safety, compatibility, or evidence gap that still warrants an inline comment.

Final Verdict

✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.10% 87.10% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 475/499 (95.19%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 12, 2026
alexey-milovidov and others added 3 commits July 13, 2026 00:45
…tting

With the `default_session_user` server setting, an HTTP request that passes
an `X-ClickHouse-Key` header without an `X-ClickHouse-User` header no longer
has an empty user name (which was previously rejected with `Code: 516`);
instead it authenticates as the default session user (`default` by default).
As the `default` user uses `no_password`, the supplied key is ignored and the
query succeeds, so the expected output for that line becomes `1`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…EQUIRED_PASSWORD

The earlier update assumed the `default` user uses `no_password` and would ignore
the supplied key. In the stateless-test configuration the `default` user has an empty
password (`<password></password>` in `programs/server/users.xml`), so a non-empty
`X-ClickHouse-Key` without a user name authenticates as `default` and fails the
password check. Because the user name is `default` and interactive Basic
authentication is enabled for the request, `AccessControl::authenticate` remaps
`AUTHENTICATION_FAILED` (516) to `REQUIRED_PASSWORD` (194). Verified against a local
server built from this branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread programs/server/Server.cpp Resolved
alexey-milovidov and others added 3 commits July 13, 2026 09:09
…terminal

The `/webterminal` WebSocket handler seeded its default user directly from the
global `default_session_user` server setting, so a composable HTTP endpoint with
its own `<default_session_user>` (or an empty one to disable anonymous logins)
authenticated anonymous WebSocket sessions as the wrong user. The per-endpoint
override was already threaded into the regular HTTP handlers through
`HTTPHandlerConnectionConfig`, but `WebTerminalRequestHandler` never received it.

Plumb the override into `WebTerminalRequestHandler` (via a creator lambda in
`addDefaultHandlersFactory`, like the dynamic query handler) and seed `auth_user`
from it when present. An empty override leaves `auth_user` empty, so an auth
message without a "user" field fails authentication, matching the "prohibit
anonymous connections" semantics of the other protocols.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…g reload

The reload check in `updateServers` marked a listener dirty when *any*
`default_session_user` in the `impl` chain changed, even when a closer module
already overrode it and the effective value for the endpoint was unchanged. That
restarted the listener on config reload for no behavioral change whenever a
shadowed base module was edited.

Mirror the lookup used by `buildProtocolStackFromConfig`: the value closest to
the endpoint wins. A new `getEffectiveDefaultSessionUser` helper walks the chain
with "first value wins" semantics, and the reload path now restarts the endpoint
only when the effective value actually differs between the previous and the new
config. This also correctly detects the case where the override is removed from a
closer module and the effective value falls back to a base module's value.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ration test

`test_default_session_user` previously exercised only the query protocols, so the
anonymous-user contract on the web terminal and the config-reload path was
unproven.

Add `test_webterminal_default_session_user`: a `/webterminal` WebSocket auth
message without a "user" field must authenticate as the endpoint's per-endpoint
`default_session_user` (a new `http_webterminal` endpoint), verified via
`system.session_log`, while an explicit user name is unaffected.

Add `test_config_reload_default_session_user`: in a single `SYSTEM RELOAD CONFIG`,
change one endpoint's own (effective) value and a shadowed base value. The
endpoint whose effective value changed is restarted and serves the new user, the
endpoint whose effective value did not change keeps its user, and the trace log
confirms only the former was reloaded for `default_session_user`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Server/HTTPHandlerFactory.cpp Resolved
Comment thread src/Server/HTTPHandlerFactory.cpp Resolved
alexey-milovidov and others added 4 commits July 13, 2026 12:28
…eb terminal rules and the keeper Prometheus factory

Two authenticated-HTTP-side paths ignored the per-endpoint
`default_session_user` override:

1. A composable HTTP endpoint that exposes the web terminal via a custom
   `handlers` section (a `rule` with `handler.type = webterminal`) built
   `WebTerminalRequestHandler` without the override, so an omitted-user
   WebSocket session authenticated as the global default user (and an empty
   per-endpoint override did not disable anonymous access). The custom
   `webterminal` rule now uses the same creator-lambda pattern as
   `addDefaultHandlersFactory`, passing the override.

2. `createKeeperPrometheusHandlerFactory` dropped the override, so with
   `prometheus_keeper_metrics_only` a composable `type = prometheus` listener
   built its handlers with `connection_config.default_session_user == nullopt`.
   The optional is now threaded through into `createPrometheusHandlerFactoryImpl`,
   matching `createPrometheusHandlerFactory`. Note that the keeper factory serves
   only the `Metrics` protocol, which is exposed without authentication
   (`MetricsImpl` does not create a session), so this threading has no runtime
   effect today; it keeps the two Prometheus factory entry points symmetric.
…etheus endpoints in the integration test

Adds `test_custom_webterminal_rule_default_session_user`: a composable HTTP
endpoint (port 8128) that serves the web terminal through a custom
`http_handlers` section referencing a `webterminal` rule, and asserts that a
WebSocket auth message without a `user` field logs in as the endpoint's
`default_session_user` (proving the override reaches the custom rule).

Adds `test_prometheus_keeper_metrics_default_session_user`: two composable
`type = prometheus` endpoints (ports 9108/9109) served through the
keeper-metrics-only factory. Since the `Metrics` protocol is exposed without
authentication, both scrapes succeed anonymously regardless of the endpoint's
`default_session_user` (including an empty override); the test documents this
scope boundary and guards against metrics exposition accidentally requiring
authentication.
…` handlers

The comments in `users.xml`, `users.yaml.example` and the user settings doc said
that an omitted user name always falls back to `default`, which is now governed by
the `default_session_user` server setting. The composable protocols doc implied that
`default_session_user` applies to all `prometheus` handlers, while the metrics
exposition endpoints (including Keeper metrics-only endpoints) never authenticate
and ignore the setting.

Co-Authored-By: Claude Fable 5 <[email protected]>
Pulls in #110260, the fix for the flaky
test `02346_text_index_bug108519_qcc_skip_index` under randomized
`materialize_statistics_on_insert`, which was the only failing test in
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110179&sha=b2143caac05dd213da79134fa637667a64d5efd5&name_0=PR

Co-Authored-By: Claude Fable 5 <[email protected]>
alexey-milovidov and others added 4 commits July 13, 2026 19:44
… fixed-user HTTP handlers

An HTTP handler with configured credentials (the `user` key inside
`handler` of an `http_handlers` rule, or the `user` key of a `prometheus`
protocol) authenticates as the configured user via `AlwaysAllowCredentials`
regardless of the request, so a request without a user name must not be
resolved through the default session user: with `default_session_user`
set to an empty string, the empty-name check used to throw
`AUTHENTICATION_FAILED` before the configured credentials were applied,
breaking fixed-user handlers as soon as anonymous logins were disabled.
The configured user name is already checked at the top of
`authenticateUserByHTTP`.

Covered by the new integration test
`test_fixed_user_handler_with_anonymous_logins_disabled`: a
`predefined_query_handler` with a fixed user on a composable endpoint
with an empty `default_session_user` override serves a request without
credentials as the configured user, while the default handlers on the
same endpoint keep rejecting anonymous requests. The setting description
and the composable protocols documentation are clarified accordingly.

Addresses a review finding on #110179

Co-Authored-By: Claude Fable 5 <[email protected]>
The review noted that `test_default_session_user` did not exercise the
gRPC path of the setting. gRPC is not a composable protocol, so only the
global setting applies to it: a query without a user name runs as the
global default session user, and an explicitly specified user is not
affected. The pre-generated protobuf modules are copied from
`test_grpc_protocol/pb2`, following the `test_session_log` pattern.

Addresses a review note on #110179

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Core/ServerSettings.cpp Resolved
alexey-milovidov and others added 3 commits July 15, 2026 00:26
`assert_login_success` only checked whether a matching `LoginSuccess` row had
ever existed in `system.session_log`, so a row left by an earlier test logging
in the same user on the same interface (e.g. `explicit_user` over HTTP) could
satisfy a later web terminal assertion even if the WebSocket path regressed.
The helper is now a context manager that compares the count of matching rows
before and after the wrapped action.

Co-Authored-By: Claude Fable 5 <[email protected]>
…oaden access

The empty user name is resolved to the configured value before authentication,
so the substituted user's password, network, and other access checks apply as
usual: an empty user name is an alias for the configured user name, and
accepting it on the native, MySQL, and PostgreSQL protocols (which previously
rejected it) grants no access beyond what specifying the same user name
explicitly already gives.

Co-Authored-By: Claude Fable 5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok. Reviewed all the code. Approved.

@alexey-milovidov alexey-milovidov self-assigned this Aug 23, 2026
@rschu1ze rschu1ze removed their assignment Aug 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 24, 2026
Merged via the queue into master with commit ed7e57e Aug 24, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the default-session-user branch August 24, 2026 05:22
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 24, 2026
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 24, 2026
… reloader

test_config_reload_prometheus_handlers_switch_to_fixed_user failed twice on
master on Integration tests (amd_msan, 1/8) with the endpoint's
<default_session_user> restart line missing from the server log.

A listener's restart decision in updateServers compares the incoming config
against the config recorded by the previous reload. SYSTEM RELOAD CONFIG is
not the only reloader: the background ConfigReloader polls every
config_reload_interval_ms (default 2000 ms) through the same updater, keyed on
the config file's mtime, which replace_in_config's sed -i bumps. When a poller
tick lands between a test's config edit and its explicit reload, the poller
consumes the difference and records the edited config; the explicit reload then
compares an edited config against an edited config, decides nothing changed,
and logs nothing. In the failing run the three "will reload" lines carry no
query id and run on the poller thread 352 ms before the test's SYSTEM RELOAD
CONFIG, which logged no listener lines at all. The <default_session_user>
variant is lost rather than merely late, because it is gated on the handler set
consuming the setting, which the same edit removes.

Pin config_reload_interval_ms to one hour for the module's three instances, so
SYSTEM RELOAD CONFIG is the only reloader and the one-edit-set-to-one-reload
assumption these tests already make actually holds. This follows the existing
idiom in test_zookeeper_connection_log and test_shard_names.

The same change also repairs three "assert not contains_in_log" sites, which
were passing vacuously under this race: a negative assertion is satisfied
whenever the poller already consumed the edit, so it failed silently open. No
polling scheme could fix those, only removing the competing reloader.

Forcing the race (a 50 ms poller and a 1 s gap between the two edits)
reproduces the exact CI assertion 20 times out of 20, with the line absent from
the whole server log. With this change under the same forcing, 20 of 20 pass
and the line appears attributed to the reload query's own query id. The full
module is 29 of 29 green.

Related: ClickHouse#110179
@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov #110179 is merged, so this is my last note here.

I promised above to open a fix PR for the Keeper ConnectionLoss and did not open one, so I owe you the reason rather than silence. It is in my earlier comment: the correct fix needs the log index the leader assigns, which client_req_stream discards, so it becomes a contrib/NuRaft change plus a submodule bump inside the queue #99123 restructures. I asked which way you wanted that taken and got no answer, and I am not going to pick an owner-level scope fork on my own.

Nothing has moved on master, and my earlier line numbers have drifted, so at 09e0e517578: KeeperStateMachine::apply_snapshot spans 761 to 984 and still makes no commit_callback, onCommit or response_callback call; commit_callback still fires only at 744 inside commit; the dispatcher still advances only by head match (KeeperRequestDispatcher.cpp:1044) and still fails the batch with ZCONNECTIONLOSS from the operation_timeout_ms * 10 watchdog at 615; use_new_dispatcher is still true by default. The signature has not recurred, but the server-side message is invisible to the test result database, so that is not evidence.

One word unblocks it: NuRaft now, wait for #99123, or leave it as known. I can also file a tracking issue so it does not only live in a merged pull request, but I will not open one unless you want it.

@fm4v

fm4v commented Aug 31, 2026

Copy link
Copy Markdown
Member

@groeneai write a test when user in default_session_user is specified but it's not declared in the users config

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. I will add the case to tests/integration/test_default_session_user/ (the existing coverage only has a nonexistent user supplied by the client, not default_session_user itself naming an undeclared one) and post the PR link here.

@groeneai

Copy link
Copy Markdown
Collaborator

#117386

The name is not validated when the configuration is loaded, since it is resolved through AccessControl, which also serves users created by SQL. So the server starts and the substituted name simply fails authentication: the new case asserts 403 and undeclared_user: Authentication failed on an anonymous HTTP request to a node whose global default_session_user names an undeclared user, plus an explicit-user control. The existing nonexistent-user assertion did not cover this, because there the name comes from the client and nothing is substituted.

pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Sep 1, 2026
…d in the users configuration

`default_session_user` substitutes a configured user name whenever a client
connects without one. Its integration suite covered the setting naming a
declared user on every interface, and the empty-string reject mode on every
interface, but not the value naming a user that is absent from the users
configuration: the ordinary typo, and the state a server is in whenever the
shipped default `default` is used while the `default` user has been removed
from the users configuration. The one existing nonexistent-user assertion
supplies the name from the client in the native Hello, so no substitution
happens on that path and the setting is not exercised.

Nothing validates the name earlier. The only config-time check on the setting
concerns the interserver protocol, so the server starts normally and the
failure is deferred to connect time: the name is substituted for the empty
user name and then authenticated like any other, and
`AccessControl::authenticate` reports `AUTHENTICATION_FAILED` under the
configured name, which HTTP answers with 403. That is the setting's own
documented contract, an empty user name being merely an alias for the
configured user name, so the connection stays subject to the substituted
user's authentication.

The test points the global `default_session_user` of the module's
otherwise-idle `node2` at a name absent from `configs/users.xml` and makes one
anonymous HTTP request plus one explicit-user control. `node2` no longer mounts
`configs/config.xml`, whose endpoints nothing on that node uses; it keeps the
cluster definition the interserver test needs and the reload-interval pin the
fixture asserts.

Requested by @ fm4v in
ClickHouse#110179 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>

This branch was successfully deployed

1 active deployment
staging - docs — b3ff303a Deployed Aug 23, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants