Skip to content

Commit

Permalink
feat!: [torrust#938] add mandatory config options
Browse files Browse the repository at this point in the history
Some configuration options are mandatory. The tracker will panic if the user doesn't provide an explicit value for them from one of the configuration sources: TOML or ENV VARS.

The mandatory options are:

```toml
[metadata]
schema_version = "2.0.0"

[logging]
threshold = "info"

[core]
private = false
listed = false
```
  • Loading branch information
josecelano committed Aug 2, 2024
1 parent f95b16a commit 54d961c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ pub enum Error {

#[error("Unsupported configuration version: {version}")]
UnsupportedVersion { version: Version },

#[error("Missing mandatory option: {path}")]
MissingMandatoryOption { path: String },

#[error("Missing figment metadata for mandatory option: {path}")]
MissingMandatoryOptionMetadata { path: String },
}

impl From<figment::Error> for Error {
Expand Down
45 changes: 45 additions & 0 deletions packages/configuration/src/v2_0_0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ impl Configuration {
.merge(Env::prefixed(CONFIG_OVERRIDE_PREFIX).split(CONFIG_OVERRIDE_SEPARATOR))
};

Self::check_mandatory_options(&figment)?;

let config: Configuration = figment.extract()?;

if config.metadata.schema_version != Version::new(VERSION_2_0_0) {
Expand All @@ -343,6 +345,49 @@ impl Configuration {
Ok(config)
}

/// Some configuration options are mandatory. The tracker will panic if
/// the user doesn't provide an explicit value for them from one of the
/// configuration sources: TOML or ENV VARS.
///
/// # Errors
///
/// Will return an error if a mandatory configuration option is only
/// obtained by default value (code), meaning the user hasn't overridden it.
fn check_mandatory_options(figment: &Figment) -> Result<(), Error> {
let mandatory_options = ["metadata.schema_version", "logging.threshold", "core.private", "core.listed"];

for mandatory_option in mandatory_options {
let value = figment
.find_value(mandatory_option)
.map_err(|_err| Error::MissingMandatoryOption {
path: mandatory_option.to_owned(),
})?;

let metadata = figment
.get_metadata(value.tag())
.ok_or_else(|| Error::MissingMandatoryOptionMetadata {
path: mandatory_option.to_owned(),
})?;

match &metadata.source {
Some(source) => {
if let figment::Source::Code(_) = source {
return Err(Error::MissingMandatoryOption {
path: mandatory_option.to_owned(),
});
};
}
None => {
return Err(Error::MissingMandatoryOption {
path: mandatory_option.to_owned(),
})
}
}
}

Ok(())
}

/// Saves the configuration to the configuration file.
///
/// # Errors
Expand Down
7 changes: 7 additions & 0 deletions share/default/config/tracker.container.mysql.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
threshold = "info"

[core]
listed = false
private = false

[core.database]
driver = "mysql"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
Expand Down
7 changes: 7 additions & 0 deletions share/default/config/tracker.container.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
threshold = "info"

[core]
listed = false
private = false

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

Expand Down
7 changes: 7 additions & 0 deletions share/default/config/tracker.development.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
threshold = "info"

[core]
listed = false
private = false

[[udp_trackers]]
bind_address = "0.0.0.0:6969"

Expand Down
7 changes: 7 additions & 0 deletions share/default/config/tracker.e2e.container.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
threshold = "info"

[core]
listed = false
private = false

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

Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.udp.benchmarking.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ schema_version = "2.0.0"
threshold = "error"

[core]
listed = false
private = false
remove_peerless_torrents = false
tracker_usage_statistics = false

Expand Down

0 comments on commit 54d961c

Please sign in to comment.