Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:_
Expand All @@ -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
```

Expand Down
4 changes: 2 additions & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/benchmarking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions docs/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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
```

Expand Down
4 changes: 2 additions & 2 deletions docs/profiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 \
Expand Down
17 changes: 5 additions & 12 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,7 +49,6 @@ pub struct TrackerPolicy {
pub struct Info {
config_toml: Option<String>,
config_toml_path: String,
api_admin_token: Option<String>,
}

impl Info {
Expand All @@ -65,9 +60,8 @@ impl Info {
///
#[allow(clippy::needless_pass_by_value)]
pub fn new(default_config_toml_path: String) -> Result<Self, Error> {
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} ...");
Expand All @@ -87,7 +81,6 @@ impl Info {
Ok(Self {
config_toml,
config_toml_path,
api_admin_token: env::var(env_var_api_admin_token).ok(),
})
}
}
Expand Down Expand Up @@ -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>,
Expand Down
36 changes: 2 additions & 34 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions share/container/entry_script_sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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.
///
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/console/ci/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/console/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down