Skip to content

Commit 3997cfa

Browse files
committed
refactor: [torrust#852] eenrich field types in TslConfig config struct
1 parent a2e718b commit 3997cfa

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

packages/configuration/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::collections::HashMap;
1010
use std::sync::Arc;
1111
use std::{env, fs};
1212

13+
use camino::Utf8PathBuf;
1314
use derive_more::Constructor;
1415
use serde::{Deserialize, Serialize};
1516
use serde_with::{serde_as, NoneAsEmptyString};
@@ -165,8 +166,8 @@ impl From<figment::Error> for Error {
165166
pub struct TslConfig {
166167
/// Path to the SSL certificate file.
167168
#[serde_as(as = "NoneAsEmptyString")]
168-
pub ssl_cert_path: Option<String>,
169+
pub ssl_cert_path: Option<Utf8PathBuf>,
169170
/// Path to the SSL key file.
170171
#[serde_as(as = "NoneAsEmptyString")]
171-
pub ssl_key_path: Option<String>,
172+
pub ssl_key_path: Option<Utf8PathBuf>,
172173
}

src/bootstrap/jobs/http_tracker.rs

+2-2
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;
21+
use super::make_rust_tls_from_path_buf;
2222
use crate::core;
2323
use crate::servers::http::server::{HttpServer, Launcher};
2424
use crate::servers::http::Version;
@@ -42,7 +42,7 @@ pub async fn start_job(
4242
if config.enabled {
4343
let socket = config.bind_address;
4444

45-
let tls = make_rust_tls(
45+
let tls = make_rust_tls_from_path_buf(
4646
config.ssl_enabled,
4747
&config.tsl_config.ssl_cert_path,
4848
&config.tsl_config.ssl_key_path,

src/bootstrap/jobs/mod.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,35 @@ pub async fn make_rust_tls(enabled: bool, cert: &Option<String>, key: &Option<St
2828

2929
if let (Some(cert), Some(key)) = (cert, key) {
3030
info!("Using https: cert path: {cert}.");
31-
info!("Using https: key 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) {
58+
info!("Using https: cert path: {cert}.");
59+
info!("Using https: key path: {key}.");
3260

3361
Some(
3462
RustlsConfig::from_pem_file(cert, key)
@@ -77,6 +105,7 @@ use std::panic::Location;
77105
use std::sync::Arc;
78106

79107
use axum_server::tls_rustls::RustlsConfig;
108+
use camino::Utf8PathBuf;
80109
use log::info;
81110
use thiserror::Error;
82111
use torrust_tracker_located_error::{DynError, LocatedError};

src/servers/http/server.rs

+2-2
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;
227+
use crate::bootstrap::jobs::make_rust_tls_from_path_buf;
228228
use crate::servers::http::server::{HttpServer, Launcher};
229229
use crate::servers::registar::Registar;
230230

@@ -236,7 +236,7 @@ mod tests {
236236

237237
let bind_to = config.bind_address;
238238

239-
let tls = make_rust_tls(
239+
let tls = make_rust_tls_from_path_buf(
240240
config.ssl_enabled,
241241
&config.tsl_config.ssl_cert_path,
242242
&config.tsl_config.ssl_key_path,

tests/servers/http/environment.rs

+2-2
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;
5+
use torrust_tracker::bootstrap::jobs::make_rust_tls_from_path_buf;
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,7 +33,7 @@ impl Environment<Stopped> {
3333

3434
let bind_to = config.bind_address;
3535

36-
let tls = block_on(make_rust_tls(
36+
let tls = block_on(make_rust_tls_from_path_buf(
3737
config.ssl_enabled,
3838
&config.tsl_config.ssl_cert_path,
3939
&config.tsl_config.ssl_key_path,

0 commit comments

Comments
 (0)