Skip to content

Commit

Permalink
refactor: [#852] enrich field types in HttpTracker config struct
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed May 10, 2024
1 parent 1475ead commit fc191f7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
13 changes: 13 additions & 0 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::sync::Arc;
use std::{env, fs};

use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use thiserror::Error;
use torrust_tracker_located_error::{DynError, LocatedError};

Expand Down Expand Up @@ -157,3 +159,14 @@ impl From<figment::Error> for Error {
}
}
}

#[serde_as]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
pub struct TslConfig {
/// Path to the SSL certificate file.
#[serde_as(as = "NoneAsEmptyString")]
pub ssl_cert_path: Option<String>,
/// Path to the SSL key file.
#[serde_as(as = "NoneAsEmptyString")]
pub ssl_key_path: Option<String>,
}
22 changes: 11 additions & 11 deletions packages/configuration/src/v1/http_tracker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use serde_with::serde_as;

use crate::TslConfig;

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

impl Default for HttpTracker {
fn default() -> Self {
Self {
enabled: false,
bind_address: String::from("0.0.0.0:7070"),
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 7070),
ssl_enabled: false,
ssl_cert_path: None,
ssl_key_path: None,
tsl_config: TslConfig::default(),
}
}
}
4 changes: 2 additions & 2 deletions packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn ephemeral() -> Configuration {
// Ephemeral socket address for HTTP tracker
let http_port = 0u16;
config.http_trackers[0].enabled = true;
config.http_trackers[0].bind_address = format!("127.0.0.1:{}", &http_port);
config.http_trackers[0].bind_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), http_port);

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

cfg.http_api.bind_address.clone_from(&ipv6.to_string());
cfg.http_trackers[0].bind_address.clone_from(&ipv6.to_string());
cfg.http_trackers[0].bind_address.clone_from(&ipv6);
cfg.udp_trackers[0].bind_address = ipv6;

cfg
Expand Down
15 changes: 8 additions & 7 deletions src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ pub async fn start_job(
version: Version,
) -> Option<JoinHandle<()>> {
if config.enabled {
let socket = config
.bind_address
.parse::<std::net::SocketAddr>()
.expect("it should have a valid http tracker bind address");
let socket = config.bind_address;

let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
.await
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));
let tls = make_rust_tls(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
)
.await
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));

match version {
Version::V1 => Some(start_v1(socket, tls, tracker.clone(), form).await),
Expand Down
17 changes: 9 additions & 8 deletions src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,15 @@ mod tests {
let tracker = initialize_with_configuration(&cfg);
let config = &cfg.http_trackers[0];

let bind_to = config
.bind_address
.parse::<std::net::SocketAddr>()
.expect("Tracker API bind_address invalid.");

let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
.await
.map(|tls| tls.expect("tls config failed"));
let bind_to = config.bind_address;

let tls = make_rust_tls(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
)
.await
.map(|tls| tls.expect("tls config failed"));

let register = &Registar::default();

Expand Down
13 changes: 7 additions & 6 deletions tests/servers/http/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ impl Environment<Stopped> {

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

let bind_to = config
.bind_address
.parse::<std::net::SocketAddr>()
.expect("Tracker API bind_address invalid.");
let bind_to = config.bind_address;

let tls = block_on(make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path))
.map(|tls| tls.expect("tls config failed"));
let tls = block_on(make_rust_tls(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
))
.map(|tls| tls.expect("tls config failed"));

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

Expand Down

0 comments on commit fc191f7

Please sign in to comment.