Skip to content

Commit e341e98

Browse files
committed
feat: [#424] remove secrets from settings API endpoint
These fields: - data.tracker.token - data.database.connect_url - data.mail.password - data.auth.secret_key are replaced with asterisks.
1 parent 1388e76 commit e341e98

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ impl TorrustIndex {
389389
fn override_tracker_api_token(&mut self, tracker_api_token: &str) {
390390
self.tracker.override_tracker_api_token(tracker_api_token);
391391
}
392+
393+
pub fn remove_secrets(&mut self) {
394+
self.tracker.token = "***".to_owned();
395+
self.database.connect_url = "***".to_owned();
396+
self.mail.password = "***".to_owned();
397+
self.auth.secret_key = "***".to_owned();
398+
}
392399
}
393400

394401
/// The configuration service.

src/services/settings.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,30 @@ impl Service {
3434
return Err(ServiceError::Unauthorized);
3535
}
3636

37-
Ok(self.configuration.get_all().await)
37+
let torrust_index_configuration = self.configuration.get_all().await;
38+
39+
Ok(torrust_index_configuration)
40+
}
41+
42+
/// It gets all the settings making the secrets with asterisks.
43+
///
44+
/// # Errors
45+
///
46+
/// It returns an error if the user does not have the required permissions.
47+
pub async fn get_all_masking_secrets(&self, user_id: &UserId) -> Result<TorrustIndex, ServiceError> {
48+
let user = self.user_repository.get_compact(user_id).await?;
49+
50+
// Check if user is administrator
51+
// todo: extract authorization service
52+
if !user.administrator {
53+
return Err(ServiceError::Unauthorized);
54+
}
55+
56+
let mut torrust_index_configuration = self.configuration.get_all().await;
57+
58+
torrust_index_configuration.remove_secrets();
59+
60+
Ok(torrust_index_configuration)
3861
}
3962

4063
/// It gets only the public settings.

src/web/api/server/v1/contexts/settings/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub async fn get_all_handler(State(app_data): State<Arc<AppData>>, Extract(maybe
2222
Err(error) => return error.into_response(),
2323
};
2424

25-
let all_settings = match app_data.settings_service.get_all(&user_id).await {
25+
let all_settings = match app_data.settings_service.get_all_masking_secrets(&user_id).await {
2626
Ok(all_settings) => all_settings,
2727
Err(error) => return error.into_response(),
2828
};

tests/e2e/environment.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ impl TestEnv {
8585
self.starting_settings.clone()
8686
}
8787

88+
/// Returns the server starting settings if the servers was already started,
89+
/// masking secrets with asterisks.
90+
pub fn server_settings_masking_secrets(&self) -> Option<Settings> {
91+
match self.starting_settings.clone() {
92+
Some(mut settings) => {
93+
settings.tracker.token = "***".to_owned();
94+
settings.database.connect_url = "***".to_owned();
95+
settings.mail.password = "***".to_owned();
96+
settings.auth.secret_key = "***".to_owned();
97+
Some(settings)
98+
}
99+
None => None,
100+
}
101+
}
102+
88103
/// Provides the API server socket address.
89104
/// For example: `localhost:3001`.
90105
pub fn server_socket_addr(&self) -> Option<String> {

tests/e2e/web/api/v1/contexts/settings/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn it_should_allow_admins_to_get_all_the_settings() {
6161

6262
let res: AllSettingsResponse = serde_json::from_str(&response.body).unwrap();
6363

64-
assert_eq!(res.data, env.server_settings().unwrap());
64+
assert_eq!(res.data, env.server_settings_masking_secrets().unwrap());
6565

6666
assert_json_ok_response(&response);
6767
}

0 commit comments

Comments
 (0)