feat: Read .env file to load CLICKHOUSE_ environment variables#223
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds project-local .env discovery to clickhousectl so CLICKHOUSE_-prefixed credentials can be sourced from the nearest ancestor .env file, with per-key precedence preserved (real exported env vars win).
Changes:
- Introduces a new
dotenvmodule that walks up fromcwd, parses the closest.env, and snapshots onlyCLICKHOUSE_keys without mutatingstd::env. - Extends cloud credential resolution to fall back to the
.envsnapshot when shell env vars are unset, and surfaces provenance in debug/status output. - Adds unit + integration tests to validate walk-up behavior and request auth header equivalence.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents .env support, discovery rules, and precedence behavior. |
| crates/clickhousectl/tests/cli_request_shape_test.rs | Adds end-to-end tests proving .env creds produce identical Basic auth requests and that shell env overrides .env. |
| crates/clickhousectl/src/main.rs | Initializes dotenv snapshot early and updates cloud auth status env-row reporting to include .env provenance. |
| crates/clickhousectl/src/dotenv.rs | New module that discovers/parses .env into an in-memory OnceLock snapshot, filtering to CLICKHOUSE_ keys. |
| crates/clickhousectl/src/cloud/client.rs | Updates auth resolver to consult .env snapshot as env-tier fallback and adds provenance helper used in debug descriptions. |
| crates/clickhousectl/src/cloud/cli.rs | Updates CLI help text to mention .env-backed env var behavior. |
| crates/clickhousectl/Cargo.toml | Adds dotenvy dependency. |
| Cargo.lock | Locks dotenvy and updates transitive dependencies (including windows-sys). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
when BOTH key and secret come exclusively from the file. Mixed shell+ .env provenance now renders plain "Active".
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 571537b. Configure here.
An exported-but-empty CLICKHOUSE_CLOUD_API_KEY= (or a bare KEY= line in .env) previously counted as a real credential: it shadowed a populated .env value and resolved to empty Basic-auth creds (→ 401). Collapse empty to absent at a single chokepoint (non_empty), routed through the shared env_or_dotenv merge so the resolver, the .env provenance helper, and the cloud auth status table can never disagree about what's present. Also fix a stale comment claiming .env is read from ancestor directories; the loader is cwd-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Thread the credentials-file and OAuth-token loaders through resolve_auth_with_sources as injection points (mirroring env_lookup), so the env/dotenv precedence tests no longer short-circuit on a saved .clickhouse/credentials.json under the test cwd. Production behaviour is unchanged — the thin resolve_auth wrapper passes the real loaders. Add credentials_file_overrides_env, which populates both the file and env tiers and asserts the file tier wins — now testable thanks to the injectable loader. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
# Conflicts: # Cargo.lock
sdairs
approved these changes
Jun 2, 2026
iskakaushik
approved these changes
Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Motivation: With agentic signups involving a third party, a seamless handoff improves the experiences of managing the clickhouse cluster. One pattern we have seen is the the third party cli signs up for clickhouse and storages the access creds in local .env.
Test plan
Note
Medium Risk
Changes authentication credential resolution and precedence; mistakes could pick up wrong keys from a local
.env, though scope is limited to cwd-onlyCLICKHOUSE_keys and shell env still overrides.Overview
Adds project-local
.envsupport for ClickHouse Cloud API credentials:CLICKHOUSE_CLOUD_API_KEYandCLICKHOUSE_CLOUD_API_SECRETcan live incwd/.env(onlyCLICKHOUSE_-prefixed keys) and are merged into the existing env tier without mutating the process environment.Resolver behavior: Shell exports win per key over
.env; overall order stays CLI flags →.clickhouse/credentials.json→ env (shell +.envfallback) → OAuth. Empty values are treated as absent. A newdotenvmodule loads viadotenvy, caches inOnceLock, andmaincallsdotenv::init()at startup.UX / debugging:
cloud auth statusand--debugenv descriptions use sharedenv_cred_presence/dotenv_env_provenanceso they match what actually authenticates; the.envpath is shown only when both creds come solely from the file.Tests: Injected env/credentials/token lookups for unit tests; integration tests assert
.envproduces the same Basic auth header as shell env and that shell overrides file.Reviewed by Cursor Bugbot for commit 9632709. Bugbot is set up for automated code reviews on this repo. Configure here.