Skip to content

Commit

Permalink
Modified and improved default way how to store self generated cert
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed May 21, 2024
1 parent 20d553f commit 979b751
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rust/agama-server/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ use openssl::x509::{X509NameBuilder, X509};
// pub write(...)
// }

const DEFAULT_CERT_FILE: &str = "/run/agama/cert.pem";
const DEFAULT_KEY_FILE: &str = "/run/agama/key.pem";
const DEFAULT_CERT_DIR: &str = "/run/agama/ssl";

/// Writes the certificate and the key to the well known location
pub fn write_certificate(cert: X509, key: PKey<Private>) -> anyhow::Result<()> {
// check and create default dir if needed
if ! Path::new(DEFAULT_CERT_DIR).is_dir() {
std::fs::create_dir_all(DEFAULT_CERT_DIR)?;
}

if let Ok(bytes) = cert.to_pem() {
fs::write(Path::new(DEFAULT_CERT_FILE), bytes)?;
fs::write(Path::new(DEFAULT_CERT_DIR).join("cert.pem"), bytes)?;
}
if let Ok(bytes) = key.public_key_to_pem() {
fs::write(Path::new(DEFAULT_KEY_FILE), bytes)?;
fs::write(Path::new(DEFAULT_CERT_DIR).join("key.pem"), bytes)?;
}

Ok(())
Expand Down

0 comments on commit 979b751

Please sign in to comment.