Skip to content

Decompose v3 database configuration #1490

Closed

Description

@josecelano

Context

The Torrust Tracker handles these secrets:

  • API tokens
  • Database passwords

You can find those secrets in the configuration in these sections:

# Database secrets
[core.database]
driver = "mysql"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"

# API token
[http_api.access_tokens]
admin = "MyAccessToken"

When the tracker starts, it prints to the console the current configuration used (after merging all sources with Figment)

To avoid exposing these secrets, the configuration includes methods to mask them.

During the app setup:

pub fn setup() -> (Configuration, AppContainer) {
    #[cfg(not(test))]
    check_seed();

    let configuration = initialize_configuration();

    if let Err(e) = configuration.validate() {
        panic!("Configuration error: {e}");
    }

    initialize_global_services(&configuration);

    tracing::info!("Configuration:\n{}", configuration.clone().mask_secrets().to_json());

    let app_container = AppContainer::initialize(&configuration);

    (configuration, app_container)
}

The configuration is cloned and secrets are masked (mask_secrets). That function calls the other config section to mask all the secrets in the configuration.

pub fn mask_secrets(mut self) -> Self {
    self.core.database.mask_secrets();

    if let Some(ref mut api) = self.http_api {
        api.mask_secrets();
    }

    self
}

Problem

Even with that functionality, secrets may leak, as happened here:

#1441

In that case, they were exposed via tracing instrumentation.

Solution

I've been considering using the newtype pattern to create a custom type for "Secret" that implements Debug and Display traits without exposing the secrets.

It would be something like:

use std::fmt;

/// Secret wrapper around a `String`
#[derive(Clone, PartialEq, Eq)]
pub struct Secret(String);

impl Secret {
    pub fn new(value: impl Into<String>) -> Self {
        Secret(value.into())
    }

    /// Access the inner value (use with care!)
    pub fn expose(&self) -> &str {
        &self.0
    }

    /// Consume the secret and extract the value
    pub fn into_inner(self) -> String {
        self.0
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "\"***\"")
    }
}

impl fmt::Display for Secret {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "***")
    }
}

But after a preliminary research with AI, there are some crates related to this topic:

I have only read a little bit but it seems:

  • secrecyprovided a wrapped type for secrets.
  • zeroize makes sure memory is cleaned after using the secrets.

Since secrecy also ensures secrets are securely wiped from memory when dropped, I would choose it. Another advantage is that you can clearly identify all secrets in the app.

Notes

For the database password we use a URL type.

[core.database]
driver = "mysql"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"

That means the whole value is not a secret. It's only a substring (db_user_secret_password).

We could need to split that information into multiple configuration values to isolate the secret. In fact, that's something I would do anyway. I have always thought the configuration is too coupled to the underlying database drivers. Using an URL for the DB connection string might not be a common practice. However, if we split it we can end up having config values that only make sense for some DB drivers. For example:

SQLite:

[core.database]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

MySQL:

[core.database]
driver = "mysql"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"

Proposal:

[core.database]

[core.database.sqlite]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

[core.database.mysql]
user = "db_user"
password = "db_user_secret_password"
host = "mysql"
port = "3306"
database = "torrust_tracker"

It could be implemented as a non-breaking change if we prioritise the old field "path".

cc @da2ce7

Activity

  1. added
    - Admin -
    - User -
    Code Cleanup / Refactoring
    Security
    on Apr 30, 2025
  2. added theissue type on Apr 30, 2025
  3. josecelano commented on Jun 24, 2026

    @josecelano
    MemberAuthor

    Alternatively we could consider the whole URL as a secret.

    Should we use the URL because it normalizes the connection config value? or should we split it into its components?

    I prefer to split it because:

    • It's easier for user to fill the independent values
    • We can validate the part separately (user name restrictions, etc)
    • In a future a new database engine may not fit in this URL format
    • In the current solution the "path" can be either a file path and a URL.

    In the tracker deployer we have already use the split version:

    https://github.com/torrust/torrust-tracker-deployer/blob/main/src/application/command_handlers/create/config/tracker/tracker_core_section.rs

    cc @da2ce7

  4. josecelano commented on Jul 15, 2026

    @josecelano
    MemberAuthor

    Spec update (2026-07-15)

    This issue has been incorporated into EPIC #1978 — Configuration Overhaul (schema v3.0.0) as subissue #7 of 9. The title has also been updated to reflect the broader scope.

    New title: "Decompose database config and overhaul secrets with secrecy crate"
    Spec: docs/issues/open/1490-1978-decompose-database-config-and-overhaul-secrets.md

    Key design decisions

    Database config: Decomposed into an enum with driver-specific variants. No backward-compatibility fallback — clean break for config schema v3.0.0:

    pub struct ConnectionInfo {
        pub host: String,
        pub port: u16,
        pub user: String,
        pub password: Secret<String>,
        pub database: String,
    }
    
    pub enum Database {
        Sqlite3 { path: String },
        MySQL(ConnectionInfo),
        PostgreSQL(ConnectionInfo),
    }

    New TOML format:

    # SQLite
    [core.database]
    driver = "sqlite3"
    path = "/var/lib/torrust/tracker/database/sqlite3.db"
    
    # MySQL
    [core.database]
    driver = "mysql"
    host = "mysql"
    port = 3306
    user = "db_user"
    password = "db_user_secret_password"
    database = "torrust_tracker"

    The original description proposed keeping the old path field as a fallback — this is dropped since v3.0.0 is a breaking change anyway.

    API tokens: AccessTokens changes from HashMap<String, String> to HashMap<String, Secret<String>>.

    Ripple effect: ~35 files total (~25 for DB decomposition + ~10 for AccessTokens). All changes are mechanical — the compiler catches everything.

    EPIC position: Depends on #1640 (subissue #3) since both touch Core. Can run in parallel with #1415, #1453, #889 after #1640 is merged.

  5. added 3 commits that reference this issue on Jul 15, 2026
  6. self-assigned this
    on Jul 15, 2026
  7. josecelano commented on Aug 21, 2026

    @josecelano
    MemberAuthor

    Planning update: #2079 is now the prerequisite API-token secrecy refactor. It protects API tokens in v2 and v3 while retaining legacy database URLs and their existing masking. #1490 follows: it decomposes v3 database configuration and introduces ConnectionInfo.password: Secret<String> for the new isolated password.\n\nFor implementation, the Git-tracked specification is the source of truth: docs/issues/open/1490-1978-decompose-database-configuration.md. The GitHub issue body is informational and may be stale.

  8. changed the title [-]Overhaul Secrets: consider using `secrecy` to handle secrets[/-] [+]Decompose v3 database configuration[/+] on Aug 21, 2026
  9. added a commit that references this issue on Aug 21, 2026
  10. added 2 commits that reference this issue on Aug 24, 2026
    5dc47fc
    7aad6e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- Admin -- User -Code Cleanup / RefactoringSecurity

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions