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