diff --git a/Containerfile b/Containerfile index 590b0a13b..79fae692f 100644 --- a/Containerfile +++ b/Containerfile @@ -95,16 +95,16 @@ FROM gcr.io/distroless/cc-debian12:debug as runtime RUN ["/busybox/cp", "-sp", "/busybox/sh","/busybox/cat","/busybox/ls","/busybox/env", "/bin/"] COPY --from=gcc --chmod=0555 /usr/local/bin/su-exec /bin/su-exec -ARG TORRUST_TRACKER_PATH_CONFIG="/etc/torrust/tracker/tracker.toml" -ARG TORRUST_TRACKER_DATABASE_DRIVER="sqlite3" +ARG TORRUST_TRACKER_CONFIG_TOML_PATH="/etc/torrust/tracker/tracker.toml" +ARG TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER="Sqlite3" ARG USER_ID=1000 ARG UDP_PORT=6969 ARG HTTP_PORT=7070 ARG API_PORT=1212 ARG HEALTH_CHECK_API_PORT=1313 -ENV TORRUST_TRACKER_PATH_CONFIG=${TORRUST_TRACKER_PATH_CONFIG} -ENV TORRUST_TRACKER_DATABASE_DRIVER=${TORRUST_TRACKER_DATABASE_DRIVER} +ENV TORRUST_TRACKER_CONFIG_TOML_PATH=${TORRUST_TRACKER_CONFIG_TOML_PATH} +ENV TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER} ENV USER_ID=${USER_ID} ENV UDP_PORT=${UDP_PORT} ENV HTTP_PORT=${HTTP_PORT} diff --git a/README.md b/README.md index 8431c00e4..754d2d5b7 100644 --- a/README.md +++ b/README.md @@ -84,14 +84,14 @@ cp ./share/default/config/tracker.development.sqlite3.toml ./storage/tracker/etc vim ./storage/tracker/etc/tracker.toml # Run the tracker with the updated configuration: -TORRUST_TRACKER_PATH_CONFIG="./storage/tracker/etc/tracker.toml" cargo run +TORRUST_TRACKER_CONFIG_TOML_PATH="./storage/tracker/etc/tracker.toml" cargo run ``` _Optionally, you may choose to supply the entire configuration as an environmental variable:_ ```sh # Use a configuration supplied on an environmental variable: -TORRUST_TRACKER_CONFIG=$(cat "./storage/tracker/etc/tracker.toml") cargo run +TORRUST_TRACKER_CONFIG_TOML=$(cat "./storage/tracker/etc/tracker.toml") cargo run ``` _For deployment, you **should** override the `api_admin_token` by using an environmental variable:_ @@ -102,8 +102,8 @@ gpg --armor --gen-random 1 10 | tee ./storage/tracker/lib/tracker_api_admin_toke chmod go-rwx ./storage/tracker/lib/tracker_api_admin_token.secret # Override secret in configuration using an environmental variable: -TORRUST_TRACKER_CONFIG=$(cat "./storage/tracker/etc/tracker.toml") \ - TORRUST_TRACKER_API_ADMIN_TOKEN=$(cat "./storage/tracker/lib/tracker_api_admin_token.secret") \ +TORRUST_TRACKER_CONFIG_TOML=$(cat "./storage/tracker/etc/tracker.toml") \ + TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN=$(cat "./storage/tracker/lib/tracker_api_admin_token.secret") \ cargo run ``` diff --git a/compose.yaml b/compose.yaml index 672ca6d0f..1d425c743 100644 --- a/compose.yaml +++ b/compose.yaml @@ -4,8 +4,8 @@ services: image: torrust-tracker:release tty: true environment: - - TORRUST_TRACKER_DATABASE_DRIVER=${TORRUST_TRACKER_DATABASE_DRIVER:-mysql} - - TORRUST_TRACKER_API_ADMIN_TOKEN=${TORRUST_TRACKER_API_ADMIN_TOKEN:-MyAccessToken} + - TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER:-MySQL} + - TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN=${TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN:-MyAccessToken} networks: - server_side ports: diff --git a/docs/benchmarking.md b/docs/benchmarking.md index 7c82df14c..2a3f1f8b0 100644 --- a/docs/benchmarking.md +++ b/docs/benchmarking.md @@ -39,7 +39,7 @@ Build and run the tracker: ```console cargo build --release -TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" ./target/release/torrust-tracker +TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.udp.benchmarking.toml" ./target/release/torrust-tracker ``` Run the load test with: diff --git a/docs/containers.md b/docs/containers.md index 6622e29b2..a0ba59d4b 100644 --- a/docs/containers.md +++ b/docs/containers.md @@ -147,10 +147,10 @@ Environmental variables are loaded through the `--env`, in the format `--env VAR The following environmental variables can be set: -- `TORRUST_TRACKER_PATH_CONFIG` - The in-container path to the tracker configuration file, (default: `"/etc/torrust/tracker/tracker.toml"`). -- `TORRUST_TRACKER_API_ADMIN_TOKEN` - Override of the admin token. If set, this value overrides any value set in the config. -- `TORRUST_TRACKER_DATABASE_DRIVER` - The database type used for the container, (options: `sqlite3`, `mysql`, default `sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file. -- `TORRUST_TRACKER_CONFIG` - Load config from this environmental variable instead from a file, (i.e: `TORRUST_TRACKER_CONFIG=$(cat tracker-tracker.toml)`). +- `TORRUST_TRACKER_CONFIG_TOML_PATH` - The in-container path to the tracker configuration file, (default: `"/etc/torrust/tracker/tracker.toml"`). +- `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN` - Override of the admin token. If set, this value overrides any value set in the config. +- `TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER` - The database type used for the container, (options: `Sqlite3`, `MySQL`, default `Sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file. +- `TORRUST_TRACKER_CONFIG_TOML` - Load config from this environmental variable instead from a file, (i.e: `TORRUST_TRACKER_CONFIG_TOML=$(cat tracker-tracker.toml)`). - `USER_ID` - The user id for the runtime crated `torrust` user. Please Note: This user id should match the ownership of the host-mapped volumes, (default `1000`). - `UDP_PORT` - The port for the UDP tracker. This should match the port used in the configuration, (default `6969`). - `HTTP_PORT` - The port for the HTTP tracker. This should match the port used in the configuration, (default `7070`). @@ -205,7 +205,7 @@ mkdir -p ./storage/tracker/lib/ ./storage/tracker/log/ ./storage/tracker/etc/ ## Run Torrust Tracker Container Image docker run -it \ - --env TORRUST_TRACKER_API_ADMIN_TOKEN="MySecretToken" \ + --env TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN="MySecretToken" \ --env USER_ID="$(id -u)" \ --publish 0.0.0.0:7070:7070/tcp \ --publish 0.0.0.0:6969:6969/udp \ @@ -227,7 +227,7 @@ mkdir -p ./storage/tracker/lib/ ./storage/tracker/log/ ./storage/tracker/etc/ ## Run Torrust Tracker Container Image podman run -it \ - --env TORRUST_TRACKER_API_ADMIN_TOKEN="MySecretToken" \ + --env TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN="MySecretToken" \ --env USER_ID="$(id -u)" \ --publish 0.0.0.0:7070:7070/tcp \ --publish 0.0.0.0:6969:6969/udp \ @@ -255,7 +255,7 @@ docker build --target release --tag torrust-tracker:release --file Containerfile mkdir -p ./storage/tracker/lib/ ./storage/tracker/log/ ./storage/tracker/etc/ USER_ID=$(id -u) \ - TORRUST_TRACKER_API_ADMIN_TOKEN="MySecretToken" \ + TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN="MySecretToken" \ docker compose up --build ``` diff --git a/docs/profiling.md b/docs/profiling.md index 406560f3c..8038f9e77 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -35,7 +35,7 @@ To generate the graph you will need to: ```console cargo build --profile=release-debug --bin=profiling ./target/release/aquatic_udp_load_test -c "load-test-config.toml" -sudo TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" /home/USER/.cargo/bin/flamegraph -- ./target/release-debug/profiling 60 +sudo TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.udp.benchmarking.toml" /home/USER/.cargo/bin/flamegraph -- ./target/release-debug/profiling 60 ``` __NOTICE__: You need to install the `aquatic_udp_load_test` program. @@ -92,7 +92,7 @@ Build and the binary for profiling: ```console RUSTFLAGS='-g' cargo build --release --bin profiling \ - && export TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" \ + && export TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.udp.benchmarking.toml" \ && valgrind \ --tool=callgrind \ --callgrind-out-file=callgrind.out \ diff --git a/packages/configuration/src/lib.rs b/packages/configuration/src/lib.rs index 588feb87e..62792c271 100644 --- a/packages/configuration/src/lib.rs +++ b/packages/configuration/src/lib.rs @@ -24,14 +24,10 @@ pub const TORRENT_PEERS_LIMIT: usize = 74; /// The whole `tracker.toml` file content. It has priority over the config file. /// Even if the file is not on the default path. -const ENV_VAR_CONFIG: &str = "TORRUST_TRACKER_CONFIG"; +const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML"; /// The `tracker.toml` file location. -pub const ENV_VAR_PATH_CONFIG: &str = "TORRUST_TRACKER_PATH_CONFIG"; - -/// Env var to overwrite API admin token. -/// Deprecated: use `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN`. -const ENV_VAR_API_ADMIN_TOKEN: &str = "TORRUST_TRACKER_API_ADMIN_TOKEN"; +pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH"; pub type Configuration = v1::Configuration; pub type UdpTracker = v1::udp_tracker::UdpTracker; @@ -53,7 +49,6 @@ pub struct TrackerPolicy { pub struct Info { config_toml: Option, config_toml_path: String, - api_admin_token: Option, } impl Info { @@ -65,9 +60,8 @@ impl Info { /// #[allow(clippy::needless_pass_by_value)] pub fn new(default_config_toml_path: String) -> Result { - let env_var_config_toml = ENV_VAR_CONFIG.to_string(); - let env_var_config_toml_path = ENV_VAR_PATH_CONFIG.to_string(); - let env_var_api_admin_token = ENV_VAR_API_ADMIN_TOKEN.to_string(); + let env_var_config_toml = ENV_VAR_CONFIG_TOML.to_string(); + let env_var_config_toml_path = ENV_VAR_CONFIG_TOML_PATH.to_string(); let config_toml = if let Ok(config_toml) = env::var(env_var_config_toml) { println!("Loading configuration from environment variable {config_toml} ..."); @@ -87,7 +81,6 @@ impl Info { Ok(Self { config_toml, config_toml_path, - api_admin_token: env::var(env_var_api_admin_token).ok(), }) } } @@ -146,7 +139,7 @@ impl AnnouncePolicy { pub enum Error { /// Unable to load the configuration from the environment variable. /// This error only occurs if there is no configuration file and the - /// `TORRUST_TRACKER_CONFIG` environment variable is not set. + /// `TORRUST_TRACKER_CONFIG_TOML` environment variable is not set. #[error("Unable to load from Environmental Variable: {source}")] UnableToLoadFromEnvironmentVariable { source: LocatedError<'static, dyn std::error::Error + Send + Sync>, diff --git a/packages/configuration/src/v1/mod.rs b/packages/configuration/src/v1/mod.rs index 8e15d65ca..8d45270b8 100644 --- a/packages/configuration/src/v1/mod.rs +++ b/packages/configuration/src/v1/mod.rs @@ -9,7 +9,7 @@ //! with the same content as the file. //! //! Configuration can not only be loaded from a file, but also from an -//! environment variable `TORRUST_TRACKER_CONFIG`. This is useful when running +//! environment variable `TORRUST_TRACKER_CONFIG_TOML`. This is useful when running //! the tracker in a Docker container or environments where you do not have a //! persistent storage or you cannot inject a configuration file. Refer to //! [`Torrust Tracker documentation`](https://docs.rs/torrust-tracker) for more @@ -288,10 +288,6 @@ impl Default for Configuration { } impl Configuration { - fn override_api_admin_token(&mut self, api_admin_token: &str) { - self.http_api.override_admin_token(api_admin_token); - } - /// Returns the tracker public IP address id defined in the configuration, /// and `None` otherwise. #[must_use] @@ -331,11 +327,7 @@ impl Configuration { .merge(Env::prefixed(CONFIG_OVERRIDE_PREFIX).split(CONFIG_OVERRIDE_SEPARATOR)) }; - let mut config: Configuration = figment.extract()?; - - if let Some(ref token) = info.api_admin_token { - config.override_api_admin_token(token); - }; + let config: Configuration = figment.extract()?; Ok(config) } @@ -469,7 +461,6 @@ mod tests { let info = Info { config_toml: Some(empty_configuration), config_toml_path: "tracker.toml".to_string(), - api_admin_token: None, }; let configuration = Configuration::load(&info).expect("Could not load configuration from file"); @@ -491,7 +482,6 @@ mod tests { let info = Info { config_toml: Some(config_toml), config_toml_path: String::new(), - api_admin_token: None, }; let configuration = Configuration::load(&info).expect("Could not load configuration from file"); @@ -515,7 +505,6 @@ mod tests { let info = Info { config_toml: None, config_toml_path: "tracker.toml".to_string(), - api_admin_token: None, }; let configuration = Configuration::load(&info).expect("Could not load configuration from file"); @@ -534,27 +523,6 @@ mod tests { let info = Info { config_toml: Some(default_config_toml()), config_toml_path: String::new(), - api_admin_token: None, - }; - - let configuration = Configuration::load(&info).expect("Could not load configuration from file"); - - assert_eq!( - configuration.http_api.access_tokens.get("admin"), - Some("NewToken".to_owned()).as_ref() - ); - - Ok(()) - }); - } - - #[test] - fn configuration_should_allow_to_overwrite_the_default_tracker_api_token_for_admin_with_the_deprecated_env_var_name() { - figment::Jail::expect_with(|_jail| { - let info = Info { - config_toml: Some(default_config_toml()), - config_toml_path: String::new(), - api_admin_token: Some("NewToken".to_owned()), }; let configuration = Configuration::load(&info).expect("Could not load configuration from file"); diff --git a/packages/primitives/src/lib.rs b/packages/primitives/src/lib.rs index aeb4d0d4e..47a837a9b 100644 --- a/packages/primitives/src/lib.rs +++ b/packages/primitives/src/lib.rs @@ -52,7 +52,9 @@ pub struct NumberOfBytes(pub i64); /// For more information about persistence. #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)] pub enum DatabaseDriver { - // TODO: Move to the database crate once that gets its own crate. + // TODO: + // - Move to the database crate once that gets its own crate. + // - Rename serialized values to lowercase: `sqlite3` and `mysql`. /// The Sqlite3 database driver. Sqlite3, /// The `MySQL` database driver. diff --git a/share/container/entry_script_sh b/share/container/entry_script_sh index 4f98e6622..8c704ea67 100644 --- a/share/container/entry_script_sh +++ b/share/container/entry_script_sh @@ -26,29 +26,29 @@ chmod -R 2770 /var/lib/torrust /var/log/torrust /etc/torrust # Install the database and config: -if [ -n "$TORRUST_TRACKER_DATABASE_DRIVER" ]; then - if cmp_lc "$TORRUST_TRACKER_DATABASE_DRIVER" "sqlite3"; then +if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" ]; then + if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" "Sqlite3"; then - # Select sqlite3 empty database + # Select Sqlite3 empty database default_database="/usr/share/torrust/default/database/tracker.sqlite3.db" - # Select sqlite3 default configuration + # Select Sqlite3 default configuration default_config="/usr/share/torrust/default/config/tracker.container.sqlite3.toml" - elif cmp_lc "$TORRUST_TRACKER_DATABASE_DRIVER" "mysql"; then + elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" "MySQL"; then - # (no database file needed for mysql) + # (no database file needed for MySQL) - # Select default mysql configuration + # Select default MySQL configuration default_config="/usr/share/torrust/default/config/tracker.container.mysql.toml" else - echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_DATABASE_DRIVER\"." - echo "Please Note: Supported Database Types: \"sqlite3\", \"mysql\"." + echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\"." + echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"." exit 1 fi else - echo "Error: \"\$TORRUST_TRACKER_DATABASE_DRIVER\" was not set!"; exit 1; + echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\" was not set!"; exit 1; fi install_config="/etc/torrust/tracker/tracker.toml" diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 03dfd9a2f..6b607bd6f 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -1,6 +1,6 @@ //! Initialize configuration from file or env var. //! -//! All environment variables are prefixed with `TORRUST_TRACKER_BACK_`. +//! All environment variables are prefixed with `TORRUST_TRACKER_`. use torrust_tracker_configuration::{Configuration, Info}; @@ -11,7 +11,7 @@ pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/tracker.developmen /// There are two methods to inject the configuration: /// /// 1. By using a config file: `tracker.toml`. -/// 2. Environment variable: `TORRUST_TRACKER_CONFIG`. The variable contains the same contents as the `tracker.toml` file. +/// 2. Environment variable: `TORRUST_TRACKER_CONFIG_TOML`. The variable contains the same contents as the `tracker.toml` file. /// /// Environment variable has priority over the config file. /// @@ -20,7 +20,7 @@ pub const DEFAULT_PATH_CONFIG: &str = "./share/default/config/tracker.developmen /// # Panics /// /// Will panic if it can't load the configuration from either -/// `./tracker.toml` file or the env var `TORRUST_TRACKER_CONFIG`. +/// `./tracker.toml` file or the env var `TORRUST_TRACKER_CONFIG_TOML`. #[must_use] pub fn initialize_configuration() -> Configuration { let info = Info::new(DEFAULT_PATH_CONFIG.to_string()).expect("info to load configuration is not valid"); diff --git a/src/console/ci/e2e/runner.rs b/src/console/ci/e2e/runner.rs index 1a4746800..945a87033 100644 --- a/src/console/ci/e2e/runner.rs +++ b/src/console/ci/e2e/runner.rs @@ -46,7 +46,7 @@ pub fn run() { // Besides, if we don't use port 0 we should get the port numbers from the tracker configuration. // We could not use docker, but the intention was to create E2E tests including containerization. let options = RunOptions { - env_vars: vec![("TORRUST_TRACKER_CONFIG".to_string(), tracker_config.to_string())], + env_vars: vec![("TORRUST_TRACKER_CONFIG_TOML".to_string(), tracker_config.to_string())], ports: vec![ "6969:6969/udp".to_string(), "7070:7070/tcp".to_string(), diff --git a/src/console/profiling.rs b/src/console/profiling.rs index e0867159f..52e11913f 100644 --- a/src/console/profiling.rs +++ b/src/console/profiling.rs @@ -27,7 +27,7 @@ //! //! ```text //! RUSTFLAGS='-g' cargo build --release --bin profiling \ -//! && export TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" \ +//! && export TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.udp.benchmarking.toml" \ //! && valgrind \ //! --tool=callgrind \ //! --callgrind-out-file=callgrind.out \ @@ -40,7 +40,7 @@ //! //! ```text //! RUSTFLAGS='-g' cargo build --release --bin profiling \ -//! && export TORRUST_TRACKER_PATH_CONFIG="./share/default/config/tracker.udp.benchmarking.toml" \ +//! && export TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.udp.benchmarking.toml" \ //! && valgrind \ //! --tool=callgrind \ //! --callgrind-out-file=callgrind.out \ diff --git a/src/lib.rs b/src/lib.rs index 22bc133e1..6fd5da15f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,10 +213,10 @@ //! documentation for the [torrust-tracker-configuration crate](https://docs.rs/torrust-tracker-configuration). //! //! Alternatively to the `tracker.toml` file you can use one environment -//! variable `TORRUST_TRACKER_CONFIG` to pass the configuration to the tracker: +//! variable `TORRUST_TRACKER_CONFIG_TOML` to pass the configuration to the tracker: //! //! ```text -//! TORRUST_TRACKER_CONFIG=$(cat ./share/default/config/tracker.development.sqlite3.toml) ./target/release/torrust-tracker +//! TORRUST_TRACKER_CONFIG_TOML=$(cat ./share/default/config/tracker.development.sqlite3.toml) ./target/release/torrust-tracker //! ``` //! //! In the previous example you are just setting the env var with the contents @@ -225,7 +225,7 @@ //! The env var contains the same data as the `tracker.toml`. It's particularly //! useful in you are [running the tracker with docker](https://github.com/torrust/torrust-tracker/tree/develop/docker). //! -//! > NOTICE: The `TORRUST_TRACKER_CONFIG` env var has priority over the `tracker.toml` file. +//! > NOTICE: The `TORRUST_TRACKER_CONFIG_TOML` env var has priority over the `tracker.toml` file. //! //! By default, if you don’t specify any `tracker.toml` file, the application //! will use `./share/default/config/tracker.development.sqlite3.toml`.