@@ -12,7 +12,7 @@ use rustls::{
12
12
NoClientAuth ,
13
13
ServerConfig ,
14
14
} ;
15
- use std:: io:: BufReader ;
15
+ use std:: { fs :: File , io:: BufReader } ;
16
16
use structopt:: StructOpt ;
17
17
18
18
#[ derive( Debug , StructOpt ) ]
@@ -29,6 +29,17 @@ pub(crate) struct CliArgs {
29
29
#[ structopt( long, short, default_value = "nats://0.0.0.0:4222" ) ]
30
30
nats : String ,
31
31
32
+ /// Path to the certificate file
33
+ #[ structopt( long, short, required_unless = "dummy-certificates" ) ]
34
+ cert_file : Option < String > ,
35
+ /// Path to the key file
36
+ #[ structopt( long, short, required_unless = "dummy-certificates" ) ]
37
+ key_file : Option < String > ,
38
+
39
+ /// Use dummy HTTPS certificates (for testing)
40
+ #[ structopt( long, short, required_unless = "cert-file" ) ]
41
+ dummy_certificates : bool ,
42
+
32
43
/// Trace rest requests to the Jaeger endpoint agent
33
44
#[ structopt( long, short) ]
34
45
jaeger : Option < String > ,
@@ -90,37 +101,75 @@ where
90
101
}
91
102
}
92
103
93
- #[ actix_web:: main]
94
- async fn main ( ) -> std:: io:: Result < ( ) > {
95
- // need to keep the jaeger pipeline tracer alive, if enabled
96
- let _tracer = init_tracing ( ) ;
97
-
98
- mbus_api:: message_bus_init ( CliArgs :: from_args ( ) . nats ) . await ;
104
+ fn get_certificates ( ) -> anyhow:: Result < ServerConfig > {
105
+ if CliArgs :: from_args ( ) . dummy_certificates {
106
+ get_dummy_certificates ( )
107
+ } else {
108
+ // guaranteed to be `Some` by the require_unless attribute
109
+ let cert_file = CliArgs :: from_args ( )
110
+ . cert_file
111
+ . expect ( "cert_file is required" ) ;
112
+ let key_file =
113
+ CliArgs :: from_args ( ) . key_file . expect ( "key_file is required" ) ;
114
+ let cert_file = & mut BufReader :: new ( File :: open ( cert_file) ?) ;
115
+ let key_file = & mut BufReader :: new ( File :: open ( key_file) ?) ;
116
+ load_certificates ( cert_file, key_file)
117
+ }
118
+ }
99
119
100
- // dummy certificates
101
- let mut config = ServerConfig :: new ( NoClientAuth :: new ( ) ) ;
120
+ fn get_dummy_certificates ( ) -> anyhow:: Result < ServerConfig > {
102
121
let cert_file = & mut BufReader :: new (
103
122
& std:: include_bytes!( "../../certs/rsa/user.chain" ) [ ..] ,
104
123
) ;
105
124
let key_file = & mut BufReader :: new (
106
125
& std:: include_bytes!( "../../certs/rsa/user.rsa" ) [ ..] ,
107
126
) ;
108
- let cert_chain = certs ( cert_file) . unwrap ( ) ;
109
- let mut keys = rsa_private_keys ( key_file) . unwrap ( ) ;
110
- config. set_single_cert ( cert_chain, keys. remove ( 0 ) ) . unwrap ( ) ;
127
+
128
+ load_certificates ( cert_file, key_file)
129
+ }
130
+
131
+ fn load_certificates < R : std:: io:: Read > (
132
+ cert_file : & mut BufReader < R > ,
133
+ key_file : & mut BufReader < R > ,
134
+ ) -> anyhow:: Result < ServerConfig > {
135
+ let mut config = ServerConfig :: new ( NoClientAuth :: new ( ) ) ;
136
+ let cert_chain = certs ( cert_file) . map_err ( |_| {
137
+ anyhow:: anyhow!(
138
+ "Failed to retrieve certificates from the certificate file" ,
139
+ )
140
+ } ) ?;
141
+ let mut keys = rsa_private_keys ( key_file) . map_err ( |_| {
142
+ anyhow:: anyhow!(
143
+ "Failed to retrieve the rsa private keys from the key file" ,
144
+ )
145
+ } ) ?;
146
+ if keys. is_empty ( ) {
147
+ anyhow:: bail!( "No keys found in the keys file" ) ;
148
+ }
149
+ config. set_single_cert ( cert_chain, keys. remove ( 0 ) ) ?;
150
+ Ok ( config)
151
+ }
152
+
153
+ #[ actix_web:: main]
154
+ async fn main ( ) -> anyhow:: Result < ( ) > {
155
+ // need to keep the jaeger pipeline tracer alive, if enabled
156
+ let _tracer = init_tracing ( ) ;
157
+
158
+ mbus_api:: message_bus_init ( CliArgs :: from_args ( ) . nats ) . await ;
111
159
112
160
let server = HttpServer :: new ( move || {
113
161
App :: new ( )
114
162
. wrap ( RequestTracing :: new ( ) )
115
163
. wrap ( middleware:: Logger :: default ( ) )
116
164
. configure_api ( & v0:: configure_api)
117
165
} )
118
- . bind_rustls ( CliArgs :: from_args ( ) . https , config ) ?;
166
+ . bind_rustls ( CliArgs :: from_args ( ) . https , get_certificates ( ) ? ) ?;
119
167
if let Some ( http) = CliArgs :: from_args ( ) . http {
120
- server. bind ( http) ?
168
+ server. bind ( http) . map_err ( anyhow :: Error :: from ) ?
121
169
} else {
122
170
server
123
171
}
124
172
. run ( )
125
173
. await
174
+ . map_err ( |e| e. into ( ) )
126
175
}
0 commit comments