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
75 changes: 1 addition & 74 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use tokio::sync::RwLock;
use torrust_index_located_error::LocatedError;
use url::Url;

use self::v1::tracker::ApiToken;
use crate::web::api::server::DynError;

pub type Settings = v1::Settings;
Expand Down Expand Up @@ -43,21 +42,11 @@ pub const ENV_VAR_CONFIG_TOML: &str = "TORRUST_INDEX_CONFIG_TOML";
/// The `index.toml` file location.
pub const ENV_VAR_CONFIG_TOML_PATH: &str = "TORRUST_INDEX_CONFIG_TOML_PATH";

/// Token needed to communicate with the Torrust Tracker.
/// Deprecated. Use `TORRUST_INDEX_CONFIG_OVERRIDE_TRACKER__TOKEN`.
pub const ENV_VAR_TRACKER_API_ADMIN_TOKEN: &str = "TORRUST_INDEX_TRACKER_API_TOKEN";

/// Secret key used to encrypt and decrypt
/// Deprecated. Use `TORRUST_INDEX_CONFIG_OVERRIDE_AUTH__SECRET_KEY`.
pub const ENV_VAR_AUTH_SECRET_KEY: &str = "TORRUST_INDEX_AUTH_SECRET_KEY";

/// Information required for loading config
#[derive(Debug, Default, Clone)]
pub struct Info {
config_toml: Option<String>,
config_toml_path: String,
tracker_api_token: Option<ApiToken>,
auth_secret_key: Option<String>,
}

impl Info {
Expand All @@ -71,8 +60,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_tracker_api_admin_token = ENV_VAR_TRACKER_API_ADMIN_TOKEN.to_string();
let env_var_auth_secret_key = ENV_VAR_AUTH_SECRET_KEY.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 @@ -89,17 +76,9 @@ impl Info {
default_config_toml_path
};

let tracker_api_token = env::var(env_var_tracker_api_admin_token)
.ok()
.map(|token| ApiToken::new(&token));

let auth_secret_key = env::var(env_var_auth_secret_key).ok();

Ok(Self {
config_toml,
config_toml_path,
tracker_api_token,
auth_secret_key,
})
}
}
Expand Down Expand Up @@ -278,19 +257,7 @@ impl Configuration {
.merge(Env::prefixed(CONFIG_OVERRIDE_PREFIX).split(CONFIG_OVERRIDE_SEPARATOR))
};

//println!("figment: {figment:#?}");

let mut settings: Settings = figment.extract()?;

if let Some(ref token) = info.tracker_api_token {
// todo: remove when using only Figment env var name: `TORRUST_INDEX_CONFIG_OVERRIDE_TRACKER__TOKEN`
settings.override_tracker_api_token(token);
};

if let Some(ref secret_key) = info.auth_secret_key {
// todo: remove when using only Figment env var name: `TORRUST_INDEX_CONFIG_OVERRIDE_AUTH__SECRET_KEY`
settings.override_auth_secret_key(secret_key);
};
let settings: Settings = figment.extract()?;

Ok(settings)
}
Expand Down Expand Up @@ -459,8 +426,6 @@ mod tests {
let info = Info {
config_toml: Some(default_config_toml()),
config_toml_path: String::new(),
tracker_api_token: None,
auth_secret_key: None,
};

let settings = Configuration::load_settings(&info).expect("Failed to load configuration from info");
Expand All @@ -471,23 +436,6 @@ mod tests {
});
}

#[tokio::test]
async fn configuration_should_allow_to_override_the_tracker_api_token_provided_in_the_toml_file_deprecated() {
let info = Info {
config_toml: Some(default_config_toml()),
config_toml_path: String::new(),
tracker_api_token: Some(ApiToken::new("OVERRIDDEN API TOKEN")),
auth_secret_key: None,
};

let configuration = Configuration::load(&info).expect("Failed to load configuration from info");

assert_eq!(
configuration.get_all().await.tracker.token,
ApiToken::new("OVERRIDDEN API TOKEN")
);
}

#[tokio::test]
async fn configuration_should_allow_to_override_the_tracker_api_token_provided_in_the_toml_file() {
figment::Jail::expect_with(|jail| {
Expand All @@ -499,8 +447,6 @@ mod tests {
let info = Info {
config_toml: Some(default_config_toml()),
config_toml_path: String::new(),
tracker_api_token: None,
auth_secret_key: None,
};

let settings = Configuration::load_settings(&info).expect("Could not load configuration from file");
Expand All @@ -511,23 +457,6 @@ mod tests {
});
}

#[tokio::test]
async fn configuration_should_allow_to_override_the_authentication_secret_key_provided_in_the_toml_file_deprecated() {
let info = Info {
config_toml: Some(default_config_toml()),
config_toml_path: String::new(),
tracker_api_token: None,
auth_secret_key: Some("OVERRIDDEN AUTH SECRET KEY".to_string()),
};

let configuration = Configuration::load(&info).expect("Failed to load configuration from info");

assert_eq!(
configuration.get_all().await.auth.secret_key,
SecretKey::new("OVERRIDDEN AUTH SECRET KEY")
);
}

#[tokio::test]
async fn configuration_should_allow_to_override_the_authentication_secret_key_provided_in_the_toml_file() {
figment::Jail::expect_with(|jail| {
Expand All @@ -539,8 +468,6 @@ mod tests {
let info = Info {
config_toml: Some(default_config_toml()),
config_toml_path: String::new(),
tracker_api_token: None,
auth_secret_key: None,
};

let settings = Configuration::load_settings(&info).expect("Could not load configuration from file");
Expand Down
8 changes: 0 additions & 8 deletions src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ pub struct Settings {
}

impl Settings {
pub fn override_tracker_api_token(&mut self, tracker_api_token: &ApiToken) {
self.tracker.override_tracker_api_token(tracker_api_token);
}

pub fn override_auth_secret_key(&mut self, auth_secret_key: &str) {
self.auth.override_secret_key(auth_secret_key);
}

pub fn remove_secrets(&mut self) {
self.tracker.token = ApiToken::new("***");
if let Some(_password) = self.database.connect_url.password() {
Expand Down