Skip to content

Commit

Permalink
chore(cert-watch): better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Feb 6, 2024
1 parent 183c924 commit 7f20870
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion https-tls/cert-watch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ $ touch cert.pem

### Client

- [HTTPie]: `http --verify=no :8443`
- cURL: `curl -v --insecure https://127.0.0.1:8443`
- Browser: go to <https://127.0.0.1:8443>
- Browser: navigate to <https://127.0.0.1:8443>

[`mkcert`]: https://github.com/FiloSottile/mkcert
[httpie]: https://httpie.io/cli
19 changes: 7 additions & 12 deletions https-tls/cert-watch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn main() -> eyre::Result<()> {
// loop reloads on TLS changes and exits on normal ctrl-c (etc.) signals
loop {
// load TLS cert/key files and
let config = load_rustls_config();
let config = load_rustls_config()?;

log::info!("starting HTTPS server at https://localhost:8443");

Expand Down Expand Up @@ -97,24 +97,19 @@ async fn main() -> eyre::Result<()> {
Ok(())
}

fn load_rustls_config() -> rustls::ServerConfig {
fn load_rustls_config() -> eyre::Result<rustls::ServerConfig> {
// init server config builder with safe defaults
let config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth();

// load TLS key/cert files
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
let cert_file = &mut BufReader::new(File::open("cert.pem")?);
let key_file = &mut BufReader::new(File::open("key.pem")?);

// convert files to key/cert objects
let cert_chain = certs(cert_file)
.unwrap()
.into_iter()
.map(Certificate)
.collect();
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)
.unwrap()
let cert_chain = certs(cert_file)?.into_iter().map(Certificate).collect();
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)?
.into_iter()
.map(PrivateKey)
.collect();
Expand All @@ -125,5 +120,5 @@ fn load_rustls_config() -> rustls::ServerConfig {
std::process::exit(1);
}

config.with_single_cert(cert_chain, keys.remove(0)).unwrap()
Ok(config.with_single_cert(cert_chain, keys.remove(0))?)
}

0 comments on commit 7f20870

Please sign in to comment.