Skip to content

Commit

Permalink
feat!: remove deprecated env var
Browse files Browse the repository at this point in the history
The env var `TORRUST_TRACKER_API_ADMIN_TOKEN` was replaced with `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN`.

After the migration to Figment all configuration options can be
overwritten.
  • Loading branch information
josecelano committed May 14, 2024
1 parent ef15e0b commit a4d2adf
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 39 deletions.
6 changes: 0 additions & 6 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const ENV_VAR_CONFIG_TOML: &str = "TORRUST_TRACKER_CONFIG_TOML";
/// The `tracker.toml` file location.
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_TRACKER_CONFIG_TOML_PATH";

/// Env var to overwrite API admin token.
const ENV_VAR_HTTP_API_ACCESS_TOKENS_ADMIN: &str = "TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN";

pub type Configuration = v1::Configuration;
pub type UdpTracker = v1::udp_tracker::UdpTracker;
pub type HttpTracker = v1::http_tracker::HttpTracker;
Expand All @@ -52,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 @@ -66,7 +62,6 @@ impl Info {
pub fn new(default_config_toml_path: String) -> Result<Self, Error> {
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 env_var_api_admin_token = ENV_VAR_HTTP_API_ACCESS_TOKENS_ADMIN.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 @@ -86,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
34 changes: 1 addition & 33 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
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

0 comments on commit a4d2adf

Please sign in to comment.