Set file mode on TLS key to 0600. (#1376)

This commit is contained in:
Koen van Hove
2026-07-10 11:46:24 +02:00
committed by GitHub
parent 5c92027dbb
commit d4696a417b
3 changed files with 45 additions and 3 deletions
+4
View File
@@ -14,6 +14,9 @@ New
Improvements Improvements
* TLS keys are now stored with the mode set to 0600 on Unix systems.
([#1376])
Bug fixes Bug fixes
Other changes Other changes
@@ -22,6 +25,7 @@ Other changes
This primarily adds a new ASPA UI. ([#1373]) This primarily adds a new ASPA UI. ([#1373])
[#1373]: https://github.com/NLnetLabs/krill/pull/1373 [#1373]: https://github.com/NLnetLabs/krill/pull/1373
[#1376]: https://github.com/NLnetLabs/krill/pull/1376
[#1378]: https://github.com/NLnetLabs/krill/pull/1378 [#1378]: https://github.com/NLnetLabs/krill/pull/1378
[#1383]: https://github.com/NLnetLabs/krill/pull/1383 [#1383]: https://github.com/NLnetLabs/krill/pull/1383
[krill-ui 0.10.0]: https://github.com/NLnetLabs/krill-ui/releases/tag/v0.10.0 [krill-ui 0.10.0]: https://github.com/NLnetLabs/krill-ui/releases/tag/v0.10.0
+40 -2
View File
@@ -4,8 +4,10 @@
use std::{fmt, fs, io}; use std::{fmt, fs, io};
use std::borrow::Cow; use std::borrow::Cow;
use std::fs::File; use std::fs::{File, OpenOptions};
use std::io::{Read, Write}; use std::io::{Read, Write};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use bytes::Bytes; use bytes::Bytes;
@@ -55,6 +57,19 @@ pub fn remove_dir_all(dir: &Path) -> Result<(), KrillIoError> {
/// Creates a new File or opens an exiting one. If the file did not exist, the /// Creates a new File or opens an exiting one. If the file did not exist, the
/// path will be created if it did not exist yet. /// path will be created if it did not exist yet.
pub fn create_file_with_path(path: &Path) -> Result<File, KrillIoError> { pub fn create_file_with_path(path: &Path) -> Result<File, KrillIoError> {
create_file(path, false)
}
/// Creates a new File or opens an exiting one. If the file did not exist, the
/// path will be created if it did not exist yet.
pub fn create_private_file_with_path(path: &Path) -> Result<File, KrillIoError> {
create_file(path, true)
}
/// Creates a file.
///
/// If `private` is `true`, will set the file mode to `0o600` on Unix.
fn create_file(path: &Path, private: bool) -> Result<File, KrillIoError> {
if if
!path.exists() !path.exists()
&& let Some(parent) = path.parent() && let Some(parent) = path.parent()
@@ -70,7 +85,15 @@ pub fn create_file_with_path(path: &Path) -> Result<File, KrillIoError> {
) )
})?; })?;
} }
File::create(path).map_err(|e| { let mut options = OpenOptions::new();
options.create(true);
options.read(true);
options.write(true);
#[cfg(unix)]
if private {
options.mode(0o600);
}
options.open(path).map_err(|e| {
KrillIoError::new( KrillIoError::new(
format!("Could not create file: {}", path.to_string_lossy()), format!("Could not create file: {}", path.to_string_lossy()),
e, e,
@@ -99,6 +122,21 @@ pub fn save(content: &[u8], full_path: &Path) -> Result<(), KrillIoError> {
Ok(()) Ok(())
} }
/// Saves a file, creating parent dirs as needed
pub fn save_private(content: &[u8], full_path: &Path) -> Result<(), KrillIoError> {
let mut f = create_private_file_with_path(full_path)?;
f.write_all(content).map_err(|e| {
KrillIoError::new(
format!("Could not write to: {}", full_path.to_string_lossy()),
e,
)
})?;
trace!("Saved file: {}", full_path.to_string_lossy());
Ok(())
}
/// Saves an object to json - unwraps any json errors! /// Saves an object to json - unwraps any json errors!
pub fn save_json<O: Serialize>( pub fn save_json<O: Serialize>(
object: &O, object: &O,
+1 -1
View File
@@ -128,7 +128,7 @@ impl HttpsSigner {
fn save_private_key(&self, tls_keys_dir: &Path) -> Result<(), Error> { fn save_private_key(&self, tls_keys_dir: &Path) -> Result<(), Error> {
let key_file_path = key_file_path(tls_keys_dir); let key_file_path = key_file_path(tls_keys_dir);
let bytes = Bytes::from(self.private.private_key_to_pem_pkcs8()?); let bytes = Bytes::from(self.private.private_key_to_pem_pkcs8()?);
file::save(&bytes, &key_file_path)?; file::save_private(&bytes, &key_file_path)?;
Ok(()) Ok(())
} }