Skip to content

Commit

Permalink
Merge pull request #3223 from didier-wenzek/fix/io-error-with-missing…
Browse files Browse the repository at this point in the history
…-file-path

fix: tedge-agent error does not show which file does not exist
  • Loading branch information
didier-wenzek authored Nov 6, 2024
2 parents 73b343e + 04dc838 commit ab45404
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
13 changes: 10 additions & 3 deletions crates/common/certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ pub struct PemCertificate {

impl PemCertificate {
pub fn from_pem_file(path: impl AsRef<Path>) -> Result<PemCertificate, CertificateError> {
let file = std::fs::File::open(path)?;
let path = path.as_ref();
let file = std::fs::File::open(path).map_err(|error| CertificateError::IoError {
error,
path: path.to_owned(),
})?;
let (pem, _) = x509_parser::pem::Pem::read(std::io::BufReader::new(file))?;
Ok(PemCertificate { pem })
}
Expand Down Expand Up @@ -240,8 +244,11 @@ pub fn translate_rustls_error(err: &(dyn std::error::Error + 'static)) -> Option

#[derive(thiserror::Error, Debug)]
pub enum CertificateError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("Could not access {path}: {error}")]
IoError {
path: PathBuf,
error: std::io::Error,
},

#[error("Cryptography related error")]
CryptographyError(#[from] rcgen::Error),
Expand Down
33 changes: 27 additions & 6 deletions crates/common/certificate/src/parse_root_certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ pub fn add_certs_from_directory(
root_store: &mut RootCertStore,
cert_dir: impl AsRef<Path>,
) -> Result<(), CertificateError> {
let files = fs::read_dir(cert_dir)?;
let cert_dir = cert_dir.as_ref();
let files = fs::read_dir(cert_dir).map_err(|error| CertificateError::IoError {
error,
path: cert_dir.to_owned(),
})?;
let certs = files.filter_map(|f| f.ok()).filter(|file| {
file.path()
.extension()
Expand Down Expand Up @@ -133,8 +137,16 @@ fn try_rec_add_root_cert(
root_store: &mut RootCertStore,
cert_path: &Path,
) -> Result<(), CertificateError> {
if fs::metadata(cert_path)?.is_dir() {
for file_entry in fs::read_dir(cert_path)?.flatten() {
let metadata = fs::metadata(cert_path).map_err(|error| CertificateError::IoError {
error,
path: cert_path.to_owned(),
})?;
if metadata.is_dir() {
let entries = fs::read_dir(cert_path).map_err(|error| CertificateError::IoError {
error,
path: cert_path.to_owned(),
})?;
for file_entry in entries.flatten() {
rec_add_root_cert(root_store, &file_entry.path());
}
} else if let Err(err) = add_root_cert(root_store, cert_path) {
Expand Down Expand Up @@ -165,7 +177,10 @@ pub fn read_pvt_key(key_file: impl AsRef<Path>) -> Result<PrivateKey, Certificat
}

fn parse_pkcs8_key(key_file: &Path) -> Result<PrivateKey, CertificateError> {
let f = File::open(key_file)?;
let f = File::open(key_file).map_err(|error| CertificateError::IoError {
error,
path: key_file.to_owned(),
})?;
let mut key_reader = BufReader::new(f);
match pkcs8_private_keys(&mut key_reader) {
Ok(key) if !key.is_empty() => Ok(PrivateKey(key[0].clone())),
Expand All @@ -174,7 +189,10 @@ fn parse_pkcs8_key(key_file: &Path) -> Result<PrivateKey, CertificateError> {
}

fn parse_rsa_key(key_file: &Path) -> Result<PrivateKey, CertificateError> {
let f = File::open(key_file)?;
let f = File::open(key_file).map_err(|error| CertificateError::IoError {
error,
path: key_file.to_owned(),
})?;
let mut key_reader = BufReader::new(f);
match rsa_private_keys(&mut key_reader) {
Ok(key) if !key.is_empty() => Ok(PrivateKey(key[0].clone())),
Expand All @@ -183,7 +201,10 @@ fn parse_rsa_key(key_file: &Path) -> Result<PrivateKey, CertificateError> {
}

pub fn read_cert_chain(cert_file: impl AsRef<Path>) -> Result<Vec<Certificate>, CertificateError> {
let f = File::open(&cert_file)?;
let f = File::open(&cert_file).map_err(|error| CertificateError::IoError {
error,
path: cert_file.as_ref().to_owned(),
})?;
let mut cert_reader = BufReader::new(f);
certs(&mut cert_reader)
.map(|der_chain| der_chain.into_iter().map(Certificate).collect())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ fn device_id(reader: &TEdgeConfigReader) -> Result<String, ReadError> {

fn cert_error_into_config_error(key: Cow<'static, str>, err: CertificateError) -> ReadError {
match &err {
CertificateError::IoError(io_err) => match io_err.kind() {
CertificateError::IoError { error, .. } => match error.kind() {
std::io::ErrorKind::NotFound => ReadError::ReadOnlyNotFound {
key,
message: concat!(
Expand Down
4 changes: 2 additions & 2 deletions crates/core/tedge/src/cli/certificate/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ fn create_new_file(path: impl AsRef<Path>, user: &str, group: &str) -> Result<Fi

pub fn cn_of_self_signed_certificate(cert_path: &Utf8PathBuf) -> Result<String, CertError> {
let pem = PemCertificate::from_pem_file(cert_path).map_err(|err| match err {
certificate::CertificateError::IoError(from) => {
CertError::IoError(from).cert_context(cert_path.clone())
certificate::CertificateError::IoError { error, .. } => {
CertError::IoError(error).cert_context(cert_path.clone())
}
from => CertError::CertificateError(from),
})?;
Expand Down
4 changes: 2 additions & 2 deletions crates/core/tedge/src/cli/certificate/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl Command for ShowCertCmd {
impl ShowCertCmd {
fn show_certificate(&self) -> Result<(), CertError> {
let pem = PemCertificate::from_pem_file(&self.cert_path).map_err(|err| match err {
certificate::CertificateError::IoError(from) => {
CertError::IoError(from).cert_context(self.cert_path.clone())
certificate::CertificateError::IoError { error, .. } => {
CertError::IoError(error).cert_context(self.cert_path.clone())
}
from => CertError::CertificateError(from),
})?;
Expand Down

0 comments on commit ab45404

Please sign in to comment.