Skip to content

Commit 8ca2457

Browse files
committed
refactor: [torrust#852] enrich field types in HttpApi config struct
1 parent ef24513 commit 8ca2457

File tree

9 files changed

+56
-94
lines changed

9 files changed

+56
-94
lines changed

packages/configuration/src/v1/tracker_api.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::collections::HashMap;
2+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
23

34
use serde::{Deserialize, Serialize};
4-
use serde_with::{serde_as, NoneAsEmptyString};
5+
use serde_with::serde_as;
6+
7+
use crate::TslConfig;
58

69
pub type AccessTokens = HashMap<String, String>;
710

@@ -15,30 +18,26 @@ pub struct HttpApi {
1518
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
1619
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1720
/// system to choose a random port, use port `0`.
18-
pub bind_address: String,
21+
pub bind_address: SocketAddr,
1922
/// Weather the HTTP API will use SSL or not.
2023
pub ssl_enabled: bool,
21-
/// Path to the SSL certificate file. Only used if `ssl_enabled` is `true`.
22-
#[serde_as(as = "NoneAsEmptyString")]
23-
pub ssl_cert_path: Option<String>,
24-
/// Path to the SSL key file. Only used if `ssl_enabled` is `true`.
25-
#[serde_as(as = "NoneAsEmptyString")]
26-
pub ssl_key_path: Option<String>,
24+
/// TSL config. Only used if `ssl_enabled` is true.
25+
#[serde(flatten)]
26+
pub tsl_config: TslConfig,
2727
/// Access tokens for the HTTP API. The key is a label identifying the
2828
/// token and the value is the token itself. The token is used to
2929
/// authenticate the user. All tokens are valid for all endpoints and have
30-
/// the all permissions.
30+
/// all permissions.
3131
pub access_tokens: AccessTokens,
3232
}
3333

3434
impl Default for HttpApi {
3535
fn default() -> Self {
3636
Self {
3737
enabled: true,
38-
bind_address: String::from("127.0.0.1:1212"),
38+
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1212),
3939
ssl_enabled: false,
40-
ssl_cert_path: None,
41-
ssl_key_path: None,
40+
tsl_config: TslConfig::default(),
4241
access_tokens: [(String::from("admin"), String::from("MyAccessToken"))]
4342
.iter()
4443
.cloned()

packages/test-helpers/src/configuration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn ephemeral() -> Configuration {
3535
// Ephemeral socket address for API
3636
let api_port = 0u16;
3737
config.http_api.enabled = true;
38-
config.http_api.bind_address = format!("127.0.0.1:{}", &api_port);
38+
config.http_api.bind_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), api_port);
3939

4040
// Ephemeral socket address for Health Check API
4141
let health_check_api_port = 0u16;
@@ -138,7 +138,7 @@ pub fn ephemeral_ipv6() -> Configuration {
138138

139139
let ipv6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0);
140140

141-
cfg.http_api.bind_address.clone_from(&ipv6.to_string());
141+
cfg.http_api.bind_address.clone_from(&ipv6);
142142
cfg.http_trackers[0].bind_address.clone_from(&ipv6);
143143
cfg.udp_trackers[0].bind_address = ipv6;
144144

src/bootstrap/jobs/http_tracker.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use log::info;
1818
use tokio::task::JoinHandle;
1919
use torrust_tracker_configuration::HttpTracker;
2020

21-
use super::make_rust_tls_from_path_buf;
21+
use super::make_rust_tls;
2222
use crate::core;
2323
use crate::servers::http::server::{HttpServer, Launcher};
2424
use crate::servers::http::Version;
@@ -42,13 +42,9 @@ pub async fn start_job(
4242
if config.enabled {
4343
let socket = config.bind_address;
4444

45-
let tls = make_rust_tls_from_path_buf(
46-
config.ssl_enabled,
47-
&config.tsl_config.ssl_cert_path,
48-
&config.tsl_config.ssl_key_path,
49-
)
50-
.await
51-
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));
45+
let tls = make_rust_tls(config.ssl_enabled, &config.tsl_config)
46+
.await
47+
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));
5248

5349
match version {
5450
Version::V1 => Some(start_v1(socket, tls, tracker.clone(), form).await),

src/bootstrap/jobs/mod.rs

+27-41
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,13 @@ pub struct Started {
2020
pub address: std::net::SocketAddr,
2121
}
2222

23-
pub async fn make_rust_tls(enabled: bool, cert: &Option<String>, key: &Option<String>) -> Option<Result<RustlsConfig, Error>> {
23+
pub async fn make_rust_tls(enabled: bool, tsl_config: &TslConfig) -> Option<Result<RustlsConfig, Error>> {
2424
if !enabled {
2525
info!("TLS not enabled");
2626
return None;
2727
}
2828

29-
if let (Some(cert), Some(key)) = (cert, key) {
30-
info!("Using https: cert path: {cert}.");
31-
info!("Using https: key path: {key}.");
32-
33-
Some(
34-
RustlsConfig::from_pem_file(cert, key)
35-
.await
36-
.map_err(|err| Error::BadTlsConfig {
37-
source: (Arc::new(err) as DynError).into(),
38-
}),
39-
)
40-
} else {
41-
Some(Err(Error::MissingTlsConfig {
42-
location: Location::caller(),
43-
}))
44-
}
45-
}
46-
47-
pub async fn make_rust_tls_from_path_buf(
48-
enabled: bool,
49-
cert: &Option<Utf8PathBuf>,
50-
key: &Option<Utf8PathBuf>,
51-
) -> Option<Result<RustlsConfig, Error>> {
52-
if !enabled {
53-
info!("TLS not enabled");
54-
return None;
55-
}
56-
57-
if let (Some(cert), Some(key)) = (cert, key) {
29+
if let (Some(cert), Some(key)) = (tsl_config.ssl_cert_path.clone(), tsl_config.ssl_key_path.clone()) {
5830
info!("Using https: cert path: {cert}.");
5931
info!("Using https: key path: {key}.");
6032

@@ -75,27 +47,41 @@ pub async fn make_rust_tls_from_path_buf(
7547
#[cfg(test)]
7648
mod tests {
7749

50+
use camino::Utf8PathBuf;
51+
use torrust_tracker_configuration::TslConfig;
52+
7853
use super::make_rust_tls;
7954

8055
#[tokio::test]
8156
async fn it_should_error_on_bad_tls_config() {
82-
let (bad_cert_path, bad_key_path) = (Some("bad cert path".to_string()), Some("bad key path".to_string()));
83-
let err = make_rust_tls(true, &bad_cert_path, &bad_key_path)
84-
.await
85-
.expect("tls_was_enabled")
86-
.expect_err("bad_cert_and_key_files");
57+
let err = make_rust_tls(
58+
true,
59+
&TslConfig {
60+
ssl_cert_path: Some(Utf8PathBuf::from("bad cert path")),
61+
ssl_key_path: Some(Utf8PathBuf::from("bad key path")),
62+
},
63+
)
64+
.await
65+
.expect("tls_was_enabled")
66+
.expect_err("bad_cert_and_key_files");
8767

8868
assert!(err
8969
.to_string()
9070
.contains("bad tls config: No such file or directory (os error 2)"));
9171
}
9272

9373
#[tokio::test]
94-
async fn it_should_error_on_missing_tls_config() {
95-
let err = make_rust_tls(true, &None, &None)
96-
.await
97-
.expect("tls_was_enabled")
98-
.expect_err("missing_config");
74+
async fn it_should_error_on_missing_cert_or_key_paths() {
75+
let err = make_rust_tls(
76+
true,
77+
&TslConfig {
78+
ssl_cert_path: None,
79+
ssl_key_path: None,
80+
},
81+
)
82+
.await
83+
.expect("tls_was_enabled")
84+
.expect_err("missing_config");
9985

10086
assert_eq!(err.to_string(), "tls config missing");
10187
}
@@ -105,9 +91,9 @@ use std::panic::Location;
10591
use std::sync::Arc;
10692

10793
use axum_server::tls_rustls::RustlsConfig;
108-
use camino::Utf8PathBuf;
10994
use log::info;
11095
use thiserror::Error;
96+
use torrust_tracker_configuration::TslConfig;
11197
use torrust_tracker_located_error::{DynError, LocatedError};
11298

11399
/// Error returned by the Bootstrap Process.

src/bootstrap/jobs/tracker_apis.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ pub async fn start_job(
6161
version: Version,
6262
) -> Option<JoinHandle<()>> {
6363
if config.enabled {
64-
let bind_to = config
65-
.bind_address
66-
.parse::<std::net::SocketAddr>()
67-
.expect("it should have a valid tracker api bind address");
64+
let bind_to = config.bind_address;
6865

69-
let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
66+
let tls = make_rust_tls(config.ssl_enabled, &config.tsl_config)
7067
.await
7168
.map(|tls| tls.expect("it should have a valid tracker api tls configuration"));
7269

src/servers/apis/server.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,9 @@ mod tests {
275275

276276
let tracker = initialize_with_configuration(&cfg);
277277

278-
let bind_to = config
279-
.bind_address
280-
.parse::<std::net::SocketAddr>()
281-
.expect("Tracker API bind_address invalid.");
278+
let bind_to = config.bind_address;
282279

283-
let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
280+
let tls = make_rust_tls(config.ssl_enabled, &config.tsl_config)
284281
.await
285282
.map(|tls| tls.expect("tls config failed"));
286283

src/servers/http/server.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ mod tests {
224224
use torrust_tracker_test_helpers::configuration::ephemeral_mode_public;
225225

226226
use crate::bootstrap::app::initialize_with_configuration;
227-
use crate::bootstrap::jobs::make_rust_tls_from_path_buf;
227+
use crate::bootstrap::jobs::make_rust_tls;
228228
use crate::servers::http::server::{HttpServer, Launcher};
229229
use crate::servers::registar::Registar;
230230

@@ -236,13 +236,9 @@ mod tests {
236236

237237
let bind_to = config.bind_address;
238238

239-
let tls = make_rust_tls_from_path_buf(
240-
config.ssl_enabled,
241-
&config.tsl_config.ssl_cert_path,
242-
&config.tsl_config.ssl_key_path,
243-
)
244-
.await
245-
.map(|tls| tls.expect("tls config failed"));
239+
let tls = make_rust_tls(config.ssl_enabled, &config.tsl_config)
240+
.await
241+
.map(|tls| tls.expect("tls config failed"));
246242

247243
let register = &Registar::default();
248244

tests/servers/api/environment.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,9 @@ impl Environment<Stopped> {
3333

3434
let config = Arc::new(configuration.http_api.clone());
3535

36-
let bind_to = config
37-
.bind_address
38-
.parse::<std::net::SocketAddr>()
39-
.expect("Tracker API bind_address invalid.");
36+
let bind_to = config.bind_address;
4037

41-
let tls = block_on(make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path))
42-
.map(|tls| tls.expect("tls config failed"));
38+
let tls = block_on(make_rust_tls(config.ssl_enabled, &config.tsl_config)).map(|tls| tls.expect("tls config failed"));
4339

4440
let server = ApiServer::new(Launcher::new(bind_to, tls));
4541

tests/servers/http/environment.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use futures::executor::block_on;
44
use torrust_tracker::bootstrap::app::initialize_with_configuration;
5-
use torrust_tracker::bootstrap::jobs::make_rust_tls_from_path_buf;
5+
use torrust_tracker::bootstrap::jobs::make_rust_tls;
66
use torrust_tracker::core::Tracker;
77
use torrust_tracker::servers::http::server::{HttpServer, Launcher, Running, Stopped};
88
use torrust_tracker::servers::registar::Registar;
@@ -33,12 +33,7 @@ impl Environment<Stopped> {
3333

3434
let bind_to = config.bind_address;
3535

36-
let tls = block_on(make_rust_tls_from_path_buf(
37-
config.ssl_enabled,
38-
&config.tsl_config.ssl_cert_path,
39-
&config.tsl_config.ssl_key_path,
40-
))
41-
.map(|tls| tls.expect("tls config failed"));
36+
let tls = block_on(make_rust_tls(config.ssl_enabled, &config.tsl_config)).map(|tls| tls.expect("tls config failed"));
4237

4338
let server = HttpServer::new(Launcher::new(bind_to, tls));
4439

0 commit comments

Comments
 (0)