Skip to content

Commit

Permalink
auto-cert: Protect private key file with UNIX mode 600
Browse files Browse the repository at this point in the history
  • Loading branch information
Icelk committed Mar 3, 2024
1 parent ea4888c commit 5705895
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions extensions/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ pub async fn mount<'a, F: Future + Send + 'a>(
{
warn!("Failed to write ACME account credentials to {account_path:?}: {err}");
}
if let Err(err) =
tokio::fs::write(&account_path, new_account_serialized.0.as_bytes()).await
if let Err(err) = write(&account_path, new_account_serialized.0.as_bytes(), true).await
{
warn!("Failed to write ACME account credentials to {account_path:?}: {err}");
}
Expand Down Expand Up @@ -242,7 +241,7 @@ pub async fn mount<'a, F: Future + Send + 'a>(
{
error!("Failed to write new TLS certificate (chain): {err}");
}
if let Err(err) = tokio::fs::write(&cert_path, certs_pem).await {
if let Err(err) = write(&cert_path, certs_pem, false).await {
error!("Failed to write new TLS certificate (chain): {err}");
}
if let Err(err) =
Expand All @@ -251,7 +250,7 @@ pub async fn mount<'a, F: Future + Send + 'a>(
{
error!("Failed to write new TLS private key: {err}");
}
if let Err(err) = tokio::fs::write(&pk_path, pk_pem).await {
if let Err(err) = write(&pk_path, pk_pem, true).await {
error!("Failed to write new TLS private key: {err}");
}
}
Expand Down Expand Up @@ -487,3 +486,15 @@ fn get_expiration(cert: &[u8]) -> Option<(chrono::OffsetDateTime, bool)> {
.any(|n| n.as_str().map_or(false, |s| s.contains("rcgen")));
Some((cert.validity().not_after.to_datetime(), self_signed))
}

async fn write(path: &Path, bytes: impl AsRef<[u8]>, _protected: bool) -> io::Result<()> {
let mut open = tokio::fs::OpenOptions::new();
open.write(true).create(true).truncate(true);
#[cfg(unix)]
if _protected {
open.mode(0o600);
}
let mut file = open.open(path).await?;
file.write_all(bytes.as_ref()).await?;
Ok(())
}

0 comments on commit 5705895

Please sign in to comment.