Skip to content

feat(configuration): protect API tokens with secrecy - #2081

Merged
josecelano merged 9 commits into
torrust:developfrom
josecelano:2079-adopt-secrecy-for-sensitive-configuration
Aug 24, 2026
Merged

josecelano merged 9 commits into
torrust:developfrom
josecelano:2079-adopt-secrecy-for-sensitive-configuration

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

  • adopt the current stable secrecy::SecretString type for v2 and v3 configuration API tokens
  • preserve TOML configuration syntax through an explicit persistence boundary while redacting generic Serde and JSON output
  • restrict secret exposure to TOML persistence, authentication, and the REST API test-client boundary
  • add redaction and serialization regression coverage, a repository ADR, and issue-template ADR guidance

Validation

  • cargo +stable test -p torrust-tracker-configuration
  • cargo +stable test -p torrust-tracker-axum-rest-api-server
  • cargo +stable test -p torrust-tracker-core
  • cargo +stable test --workspace
  • linter all
  • pre-push suite: nightly format/check/docs plus stable all-target tests

Closes #2079

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adopts secrecy::SecretString for v2/v3 configuration REST API access tokens, ensuring tokens are redacted in generic Serde/JSON output while still being persisted to TOML through an explicit, reviewable serialization boundary. It also updates the REST API authentication boundary and project documentation/policy to match the new secret-handling convention.

Changes:

  • Introduce a shared AccessTokens = HashMap<String, SecretString> alias and add the secrecy dependency (with serde support) in the configuration and REST API server crates.
  • Redact access tokens in general serialization (e.g., JSON) while preserving TOML persistence via an explicit TOML write boundary.
  • Add regression tests, an ADR, and update issue templates + skills to document the new secret-handling policy.

Reviewed changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/bootstrap/app.rs Switch startup configuration logging to rely on the updated to_json() redaction boundary.
packages/configuration/src/v3_0_0/tracker_api.rs Migrate v3 HTTP API tokens to SecretString and add redacted output serialization + TOML persistence helper.
packages/configuration/src/v3_0_0/mod.rs Implement explicit TOML persistence override for v3 access tokens; ensure JSON output is redacted.
packages/configuration/src/v2_0_0/tracker_api.rs Migrate v2 HTTP API tokens to SecretString and add redacted output serialization + TOML persistence helper.
packages/configuration/src/v2_0_0/mod.rs Implement explicit TOML persistence override for v2 access tokens; ensure JSON output is redacted.
packages/configuration/src/lib.rs Define shared AccessTokens alias as SecretString values.
packages/configuration/Cargo.toml Add secrecy dependency with serde feature.
packages/axum-rest-api-server/src/v1/middlewares/auth.rs Update authentication to compare incoming tokens via ExposeSecret.
packages/axum-rest-api-server/src/testing/environment.rs Expose token only at the test-client boundary when constructing connection info.
packages/axum-rest-api-server/Cargo.toml Add secrecy dependency.
docs/templates/ISSUE.md Extend issue template with an “Architectural Decisions” section.
docs/issues/open/2079-adopt-secrecy-for-sensitive-configuration.md Update implementation spec to reflect SecretString choice, boundaries, and evidence.
docs/adrs/index.md Register the new secrecy ADR in the ADR index.
docs/adrs/20260822094338_adopt_secrecy_for_sensitive_values.md New ADR documenting secrecy::SecretString convention and boundaries.
Cargo.lock Lockfile updates for added dependencies.
.github/skills/dev/rust-code-quality/handle-secrets/SKILL.md Update secret-handling guidance to SecretString and explicit serialization boundaries.
.github/skills/dev/planning/create-issue/SKILL.md Add guidance to link ADRs from issue specs.
.github/skills/dev/git-workflow/push-changes/SKILL.md Document PATH + hook-log handling for restricted sandboxes.
.github/skills/dev/git-workflow/commit-changes/SKILL.md Document PATH + hook-log handling for restricted sandboxes (while preserving signing/hooks).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@josecelano
josecelano force-pushed the 2079-adopt-secrecy-for-sensitive-configuration branch from 5795e4b to 0920f34 Compare August 24, 2026 08:33
@josecelano

Copy link
Copy Markdown
Member Author

ACK 0920f34

@josecelano
josecelano merged commit 8966479 into torrust:develop Aug 24, 2026
11 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a post-merge review of the change from #2081 as it stands at f72f93c5c on the default branch.

What still runs: REST API access tokens are secrecy::SecretString values in the shared AccessTokens map (packages/configuration/src/lib.rs:38), used by the v2 and v3 HttpApi. Their derived Serialize writes every token as *** (packages/configuration/src/v3_0_0/tracker_api.rs:95); Configuration::save_to_file (packages/configuration/src/v3_0_0/mod.rs:471) is the one path that writes the real values back to TOML, through serialize_toml_for_persistence (packages/configuration/src/v3_0_0/mod.rs:482); to_redacted_json feeds the startup configuration log (src/bootstrap/app.rs:66); and a token value is read only for the authentication comparison (packages/axum-rest-api-server/src/v1/middlewares/auth.rs:160), for that persistence table and for the REST API test client (packages/axum-rest-api-server/src/testing/environment.rs:193). The tracker loads only the v3 schema, so the v2 copies are not reached at runtime. Related gaps that existed at the merge have since been closed and are not repeated below: the token comparison became constant-time in 90d7a63, the complete TORRUST_TRACKER_CONFIG_TOML source stopped being printed at load in 8dea740, configuration errors stopped printing the value of a secret option in fbd9008, and the REST API request log stopped carrying the admin token in 8cfc07b.

[Minor] The configuration accepts an empty or redacted access-token value; this is a hardening point rather than an exposure of a correctly configured tracker. Generic serialization of HttpApi writes every token as *** (packages/configuration/src/v3_0_0/tracker_api.rs:95), and the public Configuration::mask_secrets returns a complete configuration whose tokens hold that marker (packages/configuration/src/v3_0_0/mod.rs:526). Code that saves either form instead of calling save_to_file writes a file whose token is the published marker, and the tracker loads it without complaint, because Configuration::validate checks only core and application (packages/configuration/src/v3_0_0/mod.rs:536); the startup log, which shows every token as ***, gives no sign that anything is wrong. An empty token, accepted the same way since before #2081, is matched by an empty credential (packages/axum-rest-api-server/src/v1/middlewares/auth.rs:119, packages/axum-rest-api-server/src/v1/middlewares/auth.rs:160). No code in this repository saves either form, and the API is disabled and bound to loopback by default (packages/configuration/src/v3_0_0/mod.rs:356, packages/configuration/src/v3_0_0/tracker_api.rs:62), so the weakness needs a configuration mistake to matter. Smallest fix: have Configuration::validate refuse an empty token and the literal ***, with a fixed message that names the label and not the value.

[Minor] The environment-override test for the admin token no longer checks the value it is named for. configuration_should_allow_to_overwrite_the_default_tracker_api_token_for_admin_with_an_env_var (packages/configuration/src/v3_0_0/mod.rs:1253) asserts only that the Debug text holds SecretBox<str>([REDACTED]) and lacks NewToken. Both hold whatever value the override produced, so the test still passes if TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN yields *** or an empty string, and that variable is the documented way to set the container admin token (docs/containers.md:255). Alongside it, persisted_configuration_toml_should_include_access_tokens (packages/configuration/src/v3_0_0/mod.rs:1289) checks only that the token text appears, while the database password has a test that persists it and loads it back (packages/configuration/src/v3_0_0/mod.rs:1324). Smallest fix: assert through the persistence boundary the module already tests, checking with assert! that the output of serialize_toml_for_persistence holds the expected admin entry, which keeps the value out of any failure message, and add a test that persists access tokens and loads them back.

[Minor] The persistence boundary panics instead of returning its error. Configuration::save_to_file returns Result and documents an Err case, but writes through expect (packages/configuration/src/v3_0_0/mod.rs:472). #2081 moved the E2E tracker configuration writer onto it (src/console/ci/qbittorrent_e2e/tracker/config_builder.rs:256), so the with_context at src/console/ci/qbittorrent_e2e/tracker/config_builder.rs:257 can no longer fire: an unwritable or full workspace now aborts the runner with a panic that does not name the file, where the replaced fs::write call returned an error naming it. Smallest fix: map the io::Error into a new variant of the #[non_exhaustive] configuration Error and return it.

Checked and found sound: save_to_file writes the real tokens and the v3 database password, and the E2E writer test confirms the real admin token is written rather than ***; to_redacted_json and the derived Debug keep tokens and the database password out of the startup log; the loading defaults carry no http_api table, so the redacting serializer never feeds *** into a loaded configuration; the only reads of a token value are the three boundaries above; secrecy 0.10.3 is locked with the checksum recorded at the merge, with its serde feature enabled only in the configuration crate; and the dropped root toml dependency has no remaining use in the root package.

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.

Adopt secrecy for sensitive configuration

3 participants