torrust_tracker/bootstrap/jobs/
mod.rs

1//! Application jobs launchers.
2//!
3//! The main application setup has only two main stages:
4//!
5//! 1. Setup the domain layer: the core tracker.
6//! 2. Launch all the application services as concurrent jobs.
7//!
8//! This modules contains all the functions needed to start those jobs.
9pub mod health_check_api;
10pub mod http_tracker;
11pub mod torrent_cleanup;
12pub mod tracker_apis;
13pub mod udp_tracker;
14
15/// This is the message that the "launcher" spawned task sends to the main
16/// application process to notify the service was successfully started.
17///
18#[derive(Debug)]
19pub struct Started {
20    pub address: std::net::SocketAddr,
21}
22
23#[instrument(skip(opt_tsl_config))]
24pub async fn make_rust_tls(opt_tsl_config: &Option<TslConfig>) -> Option<Result<RustlsConfig, Error>> {
25    match opt_tsl_config {
26        Some(tsl_config) => {
27            let cert = tsl_config.ssl_cert_path.clone();
28            let key = tsl_config.ssl_key_path.clone();
29
30            if !cert.exists() || !key.exists() {
31                return Some(Err(Error::MissingTlsConfig {
32                    location: Location::caller(),
33                }));
34            }
35
36            tracing::info!("Using https: cert path: {cert}.");
37            tracing::info!("Using https: key path: {key}.");
38
39            Some(
40                RustlsConfig::from_pem_file(cert, key)
41                    .await
42                    .map_err(|err| Error::BadTlsConfig {
43                        source: (Arc::new(err) as DynError).into(),
44                    }),
45            )
46        }
47        None => None,
48    }
49}
50
51#[cfg(test)]
52mod tests {
53
54    use camino::Utf8PathBuf;
55    use torrust_tracker_configuration::TslConfig;
56
57    use super::{make_rust_tls, Error};
58
59    #[tokio::test]
60    async fn it_should_error_on_bad_tls_config() {
61        let err = make_rust_tls(&Some(TslConfig {
62            ssl_cert_path: Utf8PathBuf::from("bad cert path"),
63            ssl_key_path: Utf8PathBuf::from("bad key path"),
64        }))
65        .await
66        .expect("tls_was_enabled")
67        .expect_err("bad_cert_and_key_files");
68
69        assert!(matches!(err, Error::MissingTlsConfig { location: _ }));
70    }
71
72    #[tokio::test]
73    async fn it_should_error_on_missing_cert_or_key_paths() {
74        let err = make_rust_tls(&Some(TslConfig {
75            ssl_cert_path: Utf8PathBuf::from(""),
76            ssl_key_path: Utf8PathBuf::from(""),
77        }))
78        .await
79        .expect("tls_was_enabled")
80        .expect_err("missing_config");
81
82        assert!(matches!(err, Error::MissingTlsConfig { location: _ }));
83    }
84}
85
86use std::panic::Location;
87use std::sync::Arc;
88
89use axum_server::tls_rustls::RustlsConfig;
90use thiserror::Error;
91use torrust_tracker_configuration::TslConfig;
92use torrust_tracker_located_error::{DynError, LocatedError};
93use tracing::instrument;
94
95/// Error returned by the Bootstrap Process.
96#[derive(Error, Debug)]
97pub enum Error {
98    /// Enabled tls but missing config.
99    #[error("tls config missing")]
100    MissingTlsConfig { location: &'static Location<'static> },
101
102    /// Unable to parse tls Config.
103    #[error("bad tls config: {source}")]
104    BadTlsConfig {
105        source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
106    },
107}