Skip to content

Commit ff866bc

Browse files
refactor(rest): add cmdline certificate arg
Adds cmdline arguments to use https certificates. Adds argument to use dummy certificates (used by test).
1 parent 2302520 commit ff866bc

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

rest/service/src/main.rs

+63-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustls::{
1212
NoClientAuth,
1313
ServerConfig,
1414
};
15-
use std::io::BufReader;
15+
use std::{fs::File, io::BufReader};
1616
use structopt::StructOpt;
1717

1818
#[derive(Debug, StructOpt)]
@@ -29,6 +29,17 @@ pub(crate) struct CliArgs {
2929
#[structopt(long, short, default_value = "nats://0.0.0.0:4222")]
3030
nats: String,
3131

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+
3243
/// Trace rest requests to the Jaeger endpoint agent
3344
#[structopt(long, short)]
3445
jaeger: Option<String>,
@@ -90,37 +101,75 @@ where
90101
}
91102
}
92103

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+
}
99119

100-
// dummy certificates
101-
let mut config = ServerConfig::new(NoClientAuth::new());
120+
fn get_dummy_certificates() -> anyhow::Result<ServerConfig> {
102121
let cert_file = &mut BufReader::new(
103122
&std::include_bytes!("../../certs/rsa/user.chain")[..],
104123
);
105124
let key_file = &mut BufReader::new(
106125
&std::include_bytes!("../../certs/rsa/user.rsa")[..],
107126
);
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;
111159

112160
let server = HttpServer::new(move || {
113161
App::new()
114162
.wrap(RequestTracing::new())
115163
.wrap(middleware::Logger::default())
116164
.configure_api(&v0::configure_api)
117165
})
118-
.bind_rustls(CliArgs::from_args().https, config)?;
166+
.bind_rustls(CliArgs::from_args().https, get_certificates()?)?;
119167
if let Some(http) = CliArgs::from_args().http {
120-
server.bind(http)?
168+
server.bind(http).map_err(anyhow::Error::from)?
121169
} else {
122170
server
123171
}
124172
.run()
125173
.await
174+
.map_err(|e| e.into())
126175
}

rest/tests/v0_test.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ async fn client() {
5555
.add_container_spec(
5656
ContainerSpec::from_binary(
5757
"rest",
58-
Binary::from_dbg("rest")
59-
.with_nats("-n")
60-
.with_args(vec!["-j", "10.1.0.8:6831"]),
58+
Binary::from_dbg("rest").with_nats("-n").with_args(vec![
59+
"-j",
60+
"10.1.0.8:6831",
61+
"--dummy-certificates",
62+
]),
6163
)
6264
.with_portmap("8080", "8080")
6365
.with_portmap("8081", "8081"),

services/deployer/src/infra/rest.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl ComponentAction for Rest {
2121
"rest",
2222
Binary::from_dbg("rest")
2323
.with_nats("-n")
24+
.with_arg("--dummy-certificates")
2425
.with_args(vec!["--https", "rest:8080"])
2526
.with_args(vec!["--http", "rest:8081"]),
2627
)
@@ -34,6 +35,7 @@ impl ComponentAction for Rest {
3435
"rest",
3536
Binary::from_dbg("rest")
3637
.with_nats("-n")
38+
.with_arg("--dummy-certificates")
3739
.with_args(vec!["-j", &jaeger_config])
3840
.with_args(vec!["--https", "rest:8080"])
3941
.with_args(vec!["--http", "rest:8081"]),

0 commit comments

Comments
 (0)