Skip to content
Search Journal
Search journal entries and projects across Journal.
Rust & Systemsdeep dive

Detecting Configuration Drift Without Printing Secrets

How EnvSentry turns dotenv, JSON and TOML into one deterministic configuration model, then reports structural drift without leaking compared values.

The line diff will show that a config file has changed. It won’t tell you that a key is missing, duplicated or has the wrong shape. EnvSentry compares dotenv, JSON and TOML files using the same document model. The comparison code doesn’t need to know which parser was used, and the report won’t print the values.

There are three crates:

  • envsentry-core handles paths, values, documents, diagnostics and sensitivity. It doesn’t read files or write to the terminal.
  • envsentry-parsers parses dotenv, JSON and TOML files into that model.
  • envsentry-cli handles arguments, reads files, selects a parser based on the file name and renders the result.

The CLI reads UTF-8 files into a ConfigDocument, asks the core to compare them and prints the diagnostics. Adding another format shouldn’t mean adding another branch to the differ.

The entries are kept in a Vec so the differ can see duplicate keys. If they were stored in a map, that information would be lost before comparison. Containers get an entry for their own path as well as entries for their children. That way an empty object is still represented as a structure, and an object isn’t treated as a scalar at the same path. Array indexes are included in the path.

Keys can contain dots, brackets, backslashes and control characters. The escaping makes sure a displayed path doesn’t become another ambiguous format. Control characters stay on one line so a key can’t be used to poke the terminal.

When parsing dotenv, the process environment isn’t changed, and duplicate assignments are allowed. JSON must have an object at the root, and duplicate keys are allowed, including nested ones. The TOML parser rejects duplicate keys. I don’t reimplement that behaviour.

The core stores paths in a BTreeMap. It checks for missing, unexpected and duplicated paths. If a path appears once on both sides, it compares the types first and then the text. EnvSentry compares the text exactly, so 01 and 1 won’t match. Empty and whitespace-only values are checked too.

Diagnostics are ordered by path, code, level, source and location. The same inputs produce the same output, which makes snapshots in CI useful.

Missing and duplicated keys are errors. Unexpected keys, empty values, and value and type mismatches are warnings. Using --strict makes warnings fail the process, but they remain warnings. Using check --schema ignores value and type differences while still reporting structural differences and empty values.

Finding Default severity --strict effect
Missing key Error Still fails.
Duplicate key Error Still fails.
Unexpected key Warning Fails policy; remains a warning.
Empty value Warning Fails policy; remains a warning.
Value or type mismatch Warning Fails policy; remains a warning.

An exit code of 0 means the policy passed. An exit code of 1 means findings caused it to fail. An exit code of 2 means the tool itself failed because of invalid arguments, an unsupported format, I/O, parsing or report writing.

EnvSentry reads the values into memory so it can compare them. Text and JSON reports contain paths and diagnostics, not the old or new values. ConfigValue also redacts its raw field in Debug output, and parser errors don’t echo input excerpts.

The core also exposes a SensitivityPolicy. It looks for names such as PASSWORD, SECRET, TOKEN, API_KEY, DATABASE_URL and PRIVATE_KEY. Matching is case-insensitive and supports * patterns. Explicit classifications take precedence over path names, and array indexes are removed before classification. Compared values are left out of every report.

The CLI reads each file in full before comparing it.

ERROR MISSING_KEY  DATABASE_URL  source=.env.example
ERROR DUPLICATE_KEY  API_TOKEN  source=.env
WARNING VALUE_MISMATCH  LOG_LEVEL
  • Building Deterministic Systems

    Canonical representations and stable identities make fingerprints, diagnostics and text transformations reproducible across runs.