Skip to content

Commit

Permalink
Minor refactoring, turned write_certificate into function
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed May 17, 2024
1 parent 41ab1e6 commit 20d553f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ impl ServeArgs {
// create a self-signed certificate if needed and store it for later use
let (cert, key) = agama_server::cert::create_certificate()?;

agama_server::cert::write_certificate(cert.clone(), key.clone());
// tries to write generated self-signed certificate. Nobody cares if it fails
let _ = agama_server::cert::write_certificate(cert.clone(), key.clone());

tls_builder.set_private_key(&key)?;
tls_builder.set_certificate(&cert)?;
Expand Down
8 changes: 5 additions & 3 deletions rust/agama-server/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ const DEFAULT_CERT_FILE: &str = "/run/agama/cert.pem";
const DEFAULT_KEY_FILE: &str = "/run/agama/key.pem";

/// Writes the certificate and the key to the well known location
pub fn write_certificate(cert: X509, key: PKey<Private>) {
pub fn write_certificate(cert: X509, key: PKey<Private>) -> anyhow::Result<()> {
if let Ok(bytes) = cert.to_pem() {
fs::write(Path::new(DEFAULT_CERT_FILE), bytes);
fs::write(Path::new(DEFAULT_CERT_FILE), bytes)?;
}
if let Ok(bytes) = key.public_key_to_pem() {
fs::write(Path::new(DEFAULT_KEY_FILE), bytes);
fs::write(Path::new(DEFAULT_KEY_FILE), bytes)?;
}

Ok(())
}

/// Generates a self-signed SSL certificate
Expand Down

0 comments on commit 20d553f

Please sign in to comment.