Skip to content

feat(config): take fields from the environment and secrets from files - #848

Closed
cjorge-thegraph wants to merge 1 commit into
mainfrom
cjorge/read-secrets-from-files-and-env
Closed

cjorge-thegraph wants to merge 1 commit into
mainfrom
cjorge/read-secrets-from-files-and-env

Conversation

@cjorge-thegraph

Copy link
Copy Markdown
Contributor

Dipper reads its whole configuration from one JSON file, so every secret it needs (the database password, the signer's key, the Kafka password, API keys, the Slack webhook) has to sit in that file next to settings that are not secret at all. The file can then only be kept as a secret, out of review, and rotating one key means rewriting all of it.

Two sources now apply to the file before it is read:

  • Environment overrides: DIPPER__<SECTION>__<FIELD> sets one field, each __ stepping into a section: DIPPER__DB__MAX_CONNECTIONS=40 sets db.max_connections. The value is read as JSON unless the file already holds a string there or it is not JSON, in which case it is a string, so a numeric password stays a string.
  • Values from files: a <field>_file member, at any depth, names a file holding the field's value: "password_file": "/secrets/db-password" sets db.password to the file's contents, less 1 final line break. DIPPER__DB__PASSWORD_FILE does the same from the environment, and setting a field both ways is refused.

Both work on the file's JSON before it becomes a Config, so deny_unknown_fields still refuses a misspelt field, and no error repeats a value read from either source. A file nothing overrides is read exactly as before, its errors still naming a line and column. No field name in the config ends in _file today, so the suffix can't collide; it is implemented in config/sources.rs without new dependencies. README.md and k8s/README.md describe both, including mounting a Secret as files so the ConfigMap holds no secret.

Tests: load_from_file_reads_the_db_password_from_a_file failed on main (unknown field `password_file` ) and passes now. 14 unit tests in config::sources cover typing, nesting, _file through the environment, conflicts and unreadable files; 2 end-to-end tests load the example config through overrides and check plain-file errors keep their line. just fmt-check, just check and just test-unit pass locally.

🤖 Generated with Claude Code

Dipper read its whole configuration from one JSON file, so every secret it needs (the database
password, the signer's key, the Kafka password, API keys, the Slack webhook) had to sit in that
file next to settings that are not secret at all. The file could then only be kept as a secret,
out of review, and rotating one key meant rewriting all of it.

Two sources now apply to the file before it is read:

- An environment variable `DIPPER__<SECTION>__<FIELD>` overrides one field, each `__` stepping
  into a section: `DIPPER__DB__MAX_CONNECTIONS=40` sets `db.max_connections`. Its value is read
  as JSON, unless the file holds a string there or it is not JSON, when it is a string, so a
  numeric password stays a string.
- A `<field>_file` member names a file holding the field's value, at any depth:
  `"password_file": "/secrets/db-password"` sets `db.password` to the file's contents, less 1
  final line break. `DIPPER__DB__PASSWORD_FILE` does the same from the environment, and setting
  a field both ways is refused.

Both run on the file's JSON before it becomes a `Config`, so a misspelt field is still refused
as unknown, and no error repeats a value read from either source. A file nothing overrides is
read exactly as before, its errors still naming a line and column.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cjorge-thegraph

Copy link
Copy Markdown
Contributor Author

Replaced by #857, which takes the approach agreed in review: secret fields typed as such (inline values allowed, never shown or quoted in errors, URLs masked with a fingerprint) and config from several files merged in order, instead of _file references and DIPPER__ environment overrides.

cjorge-thegraph added a commit that referenced this pull request Nov 23, 2026
…#857)

Replaces #848. Instead of `_file` references and `DIPPER__` environment
overrides, dipper now knows which fields are secrets by their type, and
its configuration can come from several files merged in order.

**The problem:** dipper read its whole configuration from 1 JSON file,
so every secret sat next to settings that are not secret. The file could
then only be kept as a secret, out of review. The secret fields were
also typed unevenly:
- The RPC provider URLs, which carry API keys, were plain `Url`s, and
the Kafka `sasl_password` was a plain `String` with hand-written
redaction.
- serde quoted a value it could not read in its error, so a mistyped
secret ended up in a log: `invalid type: string "hunter2-secret",
expected u32`.

## Secrets known by type

- **`Hidden<T>`** (dipper-core) leaves a value it can't deserialise out
of the error, and so does the signer key's hex adapter. The error keeps
its position.
- **Kafka:** the `sasl_password`s in dipper-producer are now
`Hidden<String>`. dipper-producer depends on dipper-core for this; it
already depended on `thegraph-core`.
- **`SecretUrl`** (new) is used for `chain_client.providers` and
`alerts.slack_webhook_url`.
  - **Shown:** the URL's scheme, host, port and the shape of its path.
- **Masked:** the user and password, every query value (its name stays),
the fragment, and every path segment but a version such as `v2`.
- **Fingerprint:** when anything was masked, a short keccak fingerprint
of the whole URL follows: `https://mainnet.infura.io/v3/*** (fingerprint
e1d789)`. 2 log lines can then be told to use the same key or not,
without showing it.
- **Why mask every segment:** a key can look like any word
(`/v2/rpc-key`), so dipper doesn't guess which segment holds it; the
fingerprint is what tells keys apart.
  - **Errors:** a URL that fails to parse is left out of the error.
- **Code:** `ChainClientConfig::provider_urls()` exposes the real URLs
for connecting.
- **Visible change:** the startup config dump now shows the webhook's
host and its path masked. Before, the whole webhook was hidden, and the
providers showed by host only.

## Config from several files

- **Usage:** `dipper-service config.json [secrets.json ...]`. Every
argument is a file, merged in order: objects key by key, anything else
(a string, a number, a list, `null`) replaced by the later file.
- **Read once:** the merged result is deserialised once, so
`deny_unknown_fields` still refuses a misspelt field in any file.
- **Each file is read whole:** it holds 1 JSON value and nothing after
it, so a stray `}` that closes the configuration early is refused rather
than leaving the later sections unread. A key given twice in 1 object of
a file is refused, with 1 file or several.
- **Errors name the field** through `serde_path_to_error` (`db.password:
…`), and leave out any string the field held. A secret given in the
wrong shape, such as a provider URL with its key where the list of
providers belongs, is not quoted either. A single file still reports its
line and column.
- **Failed loads:** a missing or unreadable file, or no file at all,
exits with code 2 and a usage line rather than a panic.
- **Where secrets live:**
  - A test setup keeps everything inline in 1 file.
- Production keeps `config.json` free of secrets in the ConfigMap,
reviewable in git, and adds a `secrets.json` holding only the secret
fields. That file lives in a Secret, for example one External Secrets
renders from the secret store.
- **Docs:** `k8s/README.md` shows the shape of `secrets.json` and the
extra mount and `args`. It notes that a list (`chain_client.providers`)
is replaced whole, and that a changed secret needs a restart. The main
README lists the secret fields.

## Dropped from #848

- `_file` references
- the `DIPPER__` environment overrides
- the guessing between JSON and string values

That removes the review's findings on #848: ambiguous types, values in
errors, map defaults wiped by an override, unreachable list items, and
the UTF-8 panic.

**Not done:** a startup warning when a secret field sits inline in the
1st file while a 2nd is given, which would catch a secret committed to
the ConfigMap. It is easy to add if wanted.

## Tests

- `a_rejected_secret_is_left_out_of_the_error` (dipper-core) failed on
`main` with the quoted value above, and passes now.
- `SecretUrl`: hosted RPC URLs (Alchemy, Infura, Ankr, QuickNode), a
word-like key, Slack, user-info, query and fragment, a URL with nothing
to mask, fingerprints, `Debug`/`Display`, and rejected values.
- Loader:
  - a secrets file merging into the example config;
  - the same config from 1 file or 2;
  - merge rules;
  - an unknown field in a later file;
- errors naming `db.password` and `alerts.slack_webhook_url` without
their values;
- a secret in the wrong shape (a provider URL where a list belongs, a
signing key where the signer's section belongs) left out of the error;
  - anything after the configuration in a file refused;
  - a key given twice in 1 of several files refused, naming that file;
- a later file that isn't JSON refused, naming it and quoting none of
it;
  - line numbers for a single file;
  - no file or a missing file;
  - the logged config masking keys.
- The existing dump test still finds no `rpc-key` in the output. The
webhook test now checks that the host shows with the path masked.
- On current `main` (sqlx 0.9, Rust 1.94): `cargo +nightly fmt`, `cargo
clippy --workspace --all-targets --all-features -D warnings` and `cargo
nextest run --all-features` (1,284 tests) pass locally.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@MoonBoi9001
MoonBoi9001 deleted the cjorge/read-secrets-from-files-and-env branch November 26, 2026 13:01
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.

1 participant