Repository navigation
Description
Overview
Three related MySQL configuration bugs were discovered during the Hetzner demo deployment (#405). All three stem from the same area of the codebase (MySQL setup in the deployer) and are best fixed together.
Bug 1 — MySQL DSN hardcoded in tracker.toml with no URL-encoding: The tracker configuration template builds the MySQL DSN by interpolating raw credential values directly into the URL string. If the password contains URL-reserved characters the DSN becomes malformed and the tracker fails to connect. Additionally, the DSN (including the plaintext password) ends up in a mounted config file rather than an environment variable, violating the project's secret-handling convention.
Bug 2 — MySQL root password is not user-configurable: The deployer silently derives the MySQL root password as {app_password}_root (hardcoded in src/application/services/rendering/docker_compose.rs). Users have no way to supply their own root password via the environment configuration JSON.
Bug 3 — No validation that MySQL app username is not "root": The MySQL Docker image reserves the root username for the built-in administrator account. If a user supplies "root" as the app DB username in their environment JSON, Docker will refuse to initialize the database. The domain type MysqlConfig validates for an empty username but not for this reserved value.
Specification
See detailed specification: docs/issues/410-bug-multiple-mysql-configuration-issues.md
Implementation Plan
Phase 1: Reject reserved MySQL username (Bug 3)
- In
MysqlConfigError(mysql.rs): addReservedUsernamevariant - Add
help()arm forReservedUsernamewith actionable fix instructions - In
MysqlConfig::new(): addif username == "root"guard returningErr(MysqlConfigError::ReservedUsername) - Add unit test
it_should_reject_root_as_username
Phase 2: Make root password configurable (Bug 2)
-
schemas/environment-config.json: add optionalroot_passwordstring to the MySQL database object -
MysqlConfig(mysql.rs): add optionalroot_passwordfield; update constructor and accessor - Deserialization/config-loading path: thread the optional field through to the application layer
-
create_mysql_contexts(docker_compose.rs): replaceformat!("{password}_root")with user-supplied value or a randomly generated password
Phase 3: Move DSN to env var override and add URL-encoding (Bug 1)
- Add
percent-encodingtoCargo.toml -
TrackerServiceConfig(env/context.rs): add optional database path field -
EnvContext::new_with_mysql: percent-encode username and password, build the full DSN string -
TrackerContext(tracker_config/context.rs): removeMysqlTemplateConfigand the MySQL branch that builds it -
templates/docker-compose/.env.tera: addTORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__PATHinside{%- if mysql %} -
templates/docker-compose/docker-compose.yml.tera: inject the new env var into the tracker serviceenvironment:section -
templates/tracker/tracker.toml.tera: remove the MySQLpath =line; add a comment
Phase 4: Tests
-
mysql.rs: addit_should_reject_root_as_usernameunit test -
env/context.rs: add test thatnew_with_mysqlproduces a correctly percent-encoded DSN for a password containing special characters -
env/context.rs: add test thatnew(SQLite) leaves the database path field asNone -
tracker_config/context.rs: remove or update the test that referencedmysql_passwordonMysqlTemplateConfig - Run
cargo testto verify all tests pass
Phase 5: Linting and pre-commit
- Run linters:
cargo run --bin linter all - Run pre-commit:
./scripts/pre-commit.sh
Acceptance Criteria
Note for Contributors: These criteria define what the PR reviewer will check. Use this as your pre-review checklist before submitting the PR to minimize back-and-forth iterations.
Quality Checks:
- Pre-commit checks pass:
./scripts/pre-commit.sh
Bug 3:
-
MysqlConfig::new()returnsErr(MysqlConfigError::ReservedUsername)when username is"root" -
MysqlConfigError::ReservedUsernamehas ahelp()message with an actionable fix - A unit test for the reserved username rejection exists and passes
Bug 2:
- The environment configuration JSON schema accepts an optional
root_passwordfield in the MySQL database object - When
root_passwordis provided in the env JSON it is used asMYSQL_ROOT_PASSWORDin the rendered.env - When
root_passwordis omitted, a randomly generated password is used — it is not derived from the app password -
create_mysql_contextsno longer containsformat!("{password}_root")
Bug 1:
- The rendered
tracker.tomlfor a MySQL deployment does not contain the database password - The rendered
.envfile containsTORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__PATHwith a correctly percent-encoded DSN when MySQL is configured - The rendered
.envfile does not containTORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__PATHwhen SQLite is configured - The rendered
docker-compose.ymlinjectsTORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__PATHinto the tracker service environment when MySQL is configured - A MySQL password containing URL-reserved characters (e.g.
@,+,/) produces a valid, correctly encoded DSN in the.envfile - A MySQL password with only alphanumeric characters is rendered unchanged
-
MysqlTemplateConfigno longer exists intracker_config/context.rs -
cargo machetereports no unused dependencies
Related
- Triggered by: Deploy Hetzner demo tracker and document the process #405 - Deploy Hetzner Demo Tracker and Document the Process
- Upstream: torrust/torrust-tracker#1606 — Document DSN password URL-encoding requirement for MySQL connection string
Activity
docs: add issue specification for #410
fix: [torrust#410] reject 'root' as MySQL app username (Bug 3)
feat(mysql): make root_password configurable, generate at creation time
feat(mysql): move DSN to env var override with percent-encoding (Bug 1)
chore(mysql): fix linting issues from Phase 5
chore: remove closed issue documentation files
Merge torrust#424: fix(mysql): [torrust#410] Fix multiple MySQL confi…