Skip to content

Commit

Permalink
refactor: [#852] eenrich field types in TslConfig config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 10, 2024
1 parent a2e718b commit 3997cfa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::{env, fs};

use camino::Utf8PathBuf;
use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
Expand Down Expand Up @@ -165,8 +166,8 @@ impl From<figment::Error> for Error {
pub struct TslConfig {
/// Path to the SSL certificate file.
#[serde_as(as = "NoneAsEmptyString")]
pub ssl_cert_path: Option<String>,
pub ssl_cert_path: Option<Utf8PathBuf>,
/// Path to the SSL key file.
#[serde_as(as = "NoneAsEmptyString")]
pub ssl_key_path: Option<String>,
pub ssl_key_path: Option<Utf8PathBuf>,
}
4 changes: 2 additions & 2 deletions src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use log::info;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::HttpTracker;

use super::make_rust_tls;
use super::make_rust_tls_from_path_buf;
use crate::core;
use crate::servers::http::server::{HttpServer, Launcher};
use crate::servers::http::Version;
Expand All @@ -42,7 +42,7 @@ pub async fn start_job(
if config.enabled {
let socket = config.bind_address;

let tls = make_rust_tls(
let tls = make_rust_tls_from_path_buf(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
Expand Down
31 changes: 30 additions & 1 deletion src/bootstrap/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,35 @@ pub async fn make_rust_tls(enabled: bool, cert: &Option<String>, key: &Option<St

if let (Some(cert), Some(key)) = (cert, key) {
info!("Using https: cert path: {cert}.");
info!("Using https: key path: {cert}.");
info!("Using https: key path: {key}.");

Some(
RustlsConfig::from_pem_file(cert, key)
.await
.map_err(|err| Error::BadTlsConfig {
source: (Arc::new(err) as DynError).into(),
}),
)
} else {
Some(Err(Error::MissingTlsConfig {
location: Location::caller(),
}))
}
}

pub async fn make_rust_tls_from_path_buf(
enabled: bool,
cert: &Option<Utf8PathBuf>,
key: &Option<Utf8PathBuf>,
) -> Option<Result<RustlsConfig, Error>> {
if !enabled {
info!("TLS not enabled");
return None;
}

if let (Some(cert), Some(key)) = (cert, key) {
info!("Using https: cert path: {cert}.");
info!("Using https: key path: {key}.");

Some(
RustlsConfig::from_pem_file(cert, key)
Expand Down Expand Up @@ -77,6 +105,7 @@ use std::panic::Location;
use std::sync::Arc;

use axum_server::tls_rustls::RustlsConfig;
use camino::Utf8PathBuf;
use log::info;
use thiserror::Error;
use torrust_tracker_located_error::{DynError, LocatedError};
Expand Down
4 changes: 2 additions & 2 deletions src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ mod tests {
use torrust_tracker_test_helpers::configuration::ephemeral_mode_public;

use crate::bootstrap::app::initialize_with_configuration;
use crate::bootstrap::jobs::make_rust_tls;
use crate::bootstrap::jobs::make_rust_tls_from_path_buf;
use crate::servers::http::server::{HttpServer, Launcher};
use crate::servers::registar::Registar;

Expand All @@ -236,7 +236,7 @@ mod tests {

let bind_to = config.bind_address;

let tls = make_rust_tls(
let tls = make_rust_tls_from_path_buf(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
Expand Down
4 changes: 2 additions & 2 deletions tests/servers/http/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use futures::executor::block_on;
use torrust_tracker::bootstrap::app::initialize_with_configuration;
use torrust_tracker::bootstrap::jobs::make_rust_tls;
use torrust_tracker::bootstrap::jobs::make_rust_tls_from_path_buf;
use torrust_tracker::core::Tracker;
use torrust_tracker::servers::http::server::{HttpServer, Launcher, Running, Stopped};
use torrust_tracker::servers::registar::Registar;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Environment<Stopped> {

let bind_to = config.bind_address;

let tls = block_on(make_rust_tls(
let tls = block_on(make_rust_tls_from_path_buf(
config.ssl_enabled,
&config.tsl_config.ssl_cert_path,
&config.tsl_config.ssl_key_path,
Expand Down

0 comments on commit 3997cfa

Please sign in to comment.