Skip to content

Commit 0a87bf1

Browse files
committed
refactor: [torrust#852] enrich field types in HttpTracker config struct
If the next major config version the `TslConfig` shoud always contain valid file paths and the whole field should be optional in the parent strcut `HttpTracker`: ```rust pub struct HttpTracker { pub enabled: bool, pub bind_address: SocketAddr, pub ssl_enabled: bool, #[serde(flatten)] pub tsl_config: Optional<TslConfig>, } pub struct TslConfig { pub ssl_cert_path: PathBuf, pub ssl_key_path: PathBuf, } ``` That mean, the user could provide it or not, but if it's provided file paths can't be empty.
1 parent 80e44e4 commit 0a87bf1

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

packages/configuration/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::sync::Arc;
1111
use std::{env, fs};
1212

1313
use derive_more::Constructor;
14+
use serde::{Deserialize, Serialize};
15+
use serde_with::{serde_as, NoneAsEmptyString};
1416
use thiserror::Error;
1517
use torrust_tracker_located_error::{DynError, LocatedError};
1618

@@ -157,3 +159,14 @@ impl From<figment::Error> for Error {
157159
}
158160
}
159161
}
162+
163+
#[serde_as]
164+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
165+
pub struct TslConfig {
166+
/// Path to the SSL certificate file.
167+
#[serde_as(as = "NoneAsEmptyString")]
168+
pub ssl_cert_path: Option<String>,
169+
/// Path to the SSL key file.
170+
#[serde_as(as = "NoneAsEmptyString")]
171+
pub ssl_key_path: Option<String>,
172+
}
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
13
use serde::{Deserialize, Serialize};
2-
use serde_with::{serde_as, NoneAsEmptyString};
4+
use serde_with::serde_as;
5+
6+
use crate::TslConfig;
37

48
/// Configuration for each HTTP tracker.
59
#[serde_as]
@@ -11,25 +15,21 @@ pub struct HttpTracker {
1115
/// The format is `ip:port`, for example `0.0.0.0:6969`. If you want to
1216
/// listen to all interfaces, use `0.0.0.0`. If you want the operating
1317
/// system to choose a random port, use port `0`.
14-
pub bind_address: String,
18+
pub bind_address: SocketAddr,
1519
/// Weather the HTTP tracker will use SSL or not.
1620
pub ssl_enabled: bool,
17-
/// Path to the SSL certificate file. Only used if `ssl_enabled` is `true`.
18-
#[serde_as(as = "NoneAsEmptyString")]
19-
pub ssl_cert_path: Option<String>,
20-
/// Path to the SSL key file. Only used if `ssl_enabled` is `true`.
21-
#[serde_as(as = "NoneAsEmptyString")]
22-
pub ssl_key_path: Option<String>,
21+
/// TSL config. Only used if `ssl_enabled` is true.
22+
#[serde(flatten)]
23+
pub tsl_config: TslConfig,
2324
}
2425

2526
impl Default for HttpTracker {
2627
fn default() -> Self {
2728
Self {
2829
enabled: false,
29-
bind_address: String::from("0.0.0.0:7070"),
30+
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 7070),
3031
ssl_enabled: false,
31-
ssl_cert_path: None,
32-
ssl_key_path: None,
32+
tsl_config: TslConfig::default(),
3333
}
3434
}
3535
}

packages/test-helpers/src/configuration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn ephemeral() -> Configuration {
4949
// Ephemeral socket address for HTTP tracker
5050
let http_port = 0u16;
5151
config.http_trackers[0].enabled = true;
52-
config.http_trackers[0].bind_address = format!("127.0.0.1:{}", &http_port);
52+
config.http_trackers[0].bind_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), http_port);
5353

5454
// Ephemeral sqlite database
5555
let temp_directory = env::temp_dir();
@@ -139,7 +139,7 @@ pub fn ephemeral_ipv6() -> Configuration {
139139
let ipv6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), 0);
140140

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

145145
cfg

src/bootstrap/jobs/http_tracker.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ pub async fn start_job(
4040
version: Version,
4141
) -> Option<JoinHandle<()>> {
4242
if config.enabled {
43-
let socket = config
44-
.bind_address
45-
.parse::<std::net::SocketAddr>()
46-
.expect("it should have a valid http tracker bind address");
43+
let socket = config.bind_address;
4744

48-
let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
49-
.await
50-
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));
45+
let tls = make_rust_tls(
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"));
5152

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

src/servers/http/server.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,15 @@ mod tests {
234234
let tracker = initialize_with_configuration(&cfg);
235235
let config = &cfg.http_trackers[0];
236236

237-
let bind_to = config
238-
.bind_address
239-
.parse::<std::net::SocketAddr>()
240-
.expect("Tracker API bind_address invalid.");
241-
242-
let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
243-
.await
244-
.map(|tls| tls.expect("tls config failed"));
237+
let bind_to = config.bind_address;
238+
239+
let tls = make_rust_tls(
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"));
245246

246247
let register = &Registar::default();
247248

tests/servers/http/environment.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ impl Environment<Stopped> {
3131

3232
let config = Arc::new(configuration.http_trackers[0].clone());
3333

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

39-
let tls = block_on(make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path))
40-
.map(|tls| tls.expect("tls config failed"));
36+
let tls = block_on(make_rust_tls(
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"));
4142

4243
let server = HttpServer::new(Launcher::new(bind_to, tls));
4344

0 commit comments

Comments
 (0)