Better errors when TPM PIN file not found (#2330)

This commit is contained in:
James Hodgkinson 2023-11-24 09:16:20 +10:00 committed by GitHub
parent bb8914c70d
commit 24c4f15b5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,6 +17,7 @@ use std::io::{Error as IoError, ErrorKind};
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
@ -437,6 +438,15 @@ async fn process_etc_passwd_group(
}
async fn read_hsm_pin(hsm_pin_path: &str) -> Result<Vec<u8>, Box<dyn Error>> {
if !PathBuf::from_str(hsm_pin_path)?.exists() {
// TODO generate the file by default
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("HSM PIN file '{}' not found", hsm_pin_path),
)
.into());
}
let mut file = File::open(hsm_pin_path).await?;
let mut contents = vec![];
file.read_to_end(&mut contents).await?;
@ -736,7 +746,7 @@ async fn main() -> ExitCode {
let hsm_pin = match read_hsm_pin(cfg.hsm_pin_path.as_str()).await {
Ok(hp) => hp,
Err(err) => {
error!(?err, "Failed to read hsm pin");
error!(?err, "Failed to read HSM PIN from {}", cfg.hsm_pin_path.as_str());
return ExitCode::FAILURE
}
};