@@ -20,41 +20,13 @@ pub struct Started {
20
20
pub address : std:: net:: SocketAddr ,
21
21
}
22
22
23
- pub async fn make_rust_tls ( enabled : bool , cert : & Option < String > , key : & Option < String > ) -> Option < Result < RustlsConfig , Error > > {
23
+ pub async fn make_rust_tls ( enabled : bool , tsl_config : & TslConfig ) -> Option < Result < RustlsConfig , Error > > {
24
24
if !enabled {
25
25
info ! ( "TLS not enabled" ) ;
26
26
return None ;
27
27
}
28
28
29
- if let ( Some ( cert) , Some ( key) ) = ( cert, key) {
30
- info ! ( "Using https: cert 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) {
29
+ if let ( Some ( cert) , Some ( key) ) = ( tsl_config. ssl_cert_path . clone ( ) , tsl_config. ssl_key_path . clone ( ) ) {
58
30
info ! ( "Using https: cert path: {cert}." ) ;
59
31
info ! ( "Using https: key path: {key}." ) ;
60
32
@@ -75,27 +47,41 @@ pub async fn make_rust_tls_from_path_buf(
75
47
#[ cfg( test) ]
76
48
mod tests {
77
49
50
+ use camino:: Utf8PathBuf ;
51
+ use torrust_tracker_configuration:: TslConfig ;
52
+
78
53
use super :: make_rust_tls;
79
54
80
55
#[ tokio:: test]
81
56
async fn it_should_error_on_bad_tls_config ( ) {
82
- let ( bad_cert_path, bad_key_path) = ( Some ( "bad cert path" . to_string ( ) ) , Some ( "bad key path" . to_string ( ) ) ) ;
83
- let err = make_rust_tls ( true , & bad_cert_path, & bad_key_path)
84
- . await
85
- . expect ( "tls_was_enabled" )
86
- . expect_err ( "bad_cert_and_key_files" ) ;
57
+ let err = make_rust_tls (
58
+ true ,
59
+ & TslConfig {
60
+ ssl_cert_path : Some ( Utf8PathBuf :: from ( "bad cert path" ) ) ,
61
+ ssl_key_path : Some ( Utf8PathBuf :: from ( "bad key path" ) ) ,
62
+ } ,
63
+ )
64
+ . await
65
+ . expect ( "tls_was_enabled" )
66
+ . expect_err ( "bad_cert_and_key_files" ) ;
87
67
88
68
assert ! ( err
89
69
. to_string( )
90
70
. contains( "bad tls config: No such file or directory (os error 2)" ) ) ;
91
71
}
92
72
93
73
#[ tokio:: test]
94
- async fn it_should_error_on_missing_tls_config ( ) {
95
- let err = make_rust_tls ( true , & None , & None )
96
- . await
97
- . expect ( "tls_was_enabled" )
98
- . expect_err ( "missing_config" ) ;
74
+ async fn it_should_error_on_missing_cert_or_key_paths ( ) {
75
+ let err = make_rust_tls (
76
+ true ,
77
+ & TslConfig {
78
+ ssl_cert_path : None ,
79
+ ssl_key_path : None ,
80
+ } ,
81
+ )
82
+ . await
83
+ . expect ( "tls_was_enabled" )
84
+ . expect_err ( "missing_config" ) ;
99
85
100
86
assert_eq ! ( err. to_string( ) , "tls config missing" ) ;
101
87
}
@@ -105,9 +91,9 @@ use std::panic::Location;
105
91
use std:: sync:: Arc ;
106
92
107
93
use axum_server:: tls_rustls:: RustlsConfig ;
108
- use camino:: Utf8PathBuf ;
109
94
use log:: info;
110
95
use thiserror:: Error ;
96
+ use torrust_tracker_configuration:: TslConfig ;
111
97
use torrust_tracker_located_error:: { DynError , LocatedError } ;
112
98
113
99
/// Error returned by the Bootstrap Process.
0 commit comments