Skip to content

Commit 7a4b8cf

Browse files
committed
anyhow -> snafu
1 parent 9de0643 commit 7a4b8cf

File tree

6 files changed

+46
-41
lines changed

6 files changed

+46
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cert-tools/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ publish = false
1111
[dependencies]
1212
stackable-secret-operator-utils = { path = "../utils" }
1313

14-
anyhow.workspace = true
1514
clap = { workspace = true, features = ["derive"] }
1615
hex.workspace = true
1716
openssl.workspace = true
17+
snafu.workspace = true
1818
tracing.workspace = true
1919
tracing-subscriber = { workspace = true, features = ["env-filter"] }

rust/cert-tools/src/cert_ext.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
use anyhow::Context;
21
use openssl::{
32
hash::{DigestBytes, MessageDigest},
43
string::OpensslString,
54
x509::X509,
65
};
6+
use snafu::ResultExt;
77

88
pub trait CertExt {
9-
fn serial_as_hex(&self) -> anyhow::Result<OpensslString>;
10-
fn sha256_digest(&self) -> anyhow::Result<DigestBytes>;
9+
fn serial_as_hex(&self) -> Result<OpensslString, snafu::Whatever>;
10+
fn sha256_digest(&self) -> Result<DigestBytes, snafu::Whatever>;
1111
}
1212

1313
impl CertExt for X509 {
14-
fn serial_as_hex(&self) -> anyhow::Result<OpensslString> {
14+
fn serial_as_hex(&self) -> Result<OpensslString, snafu::Whatever> {
1515
self.serial_number()
1616
.to_bn()
17-
.context("failed to get certificate serial number as BigNumber")?
17+
.whatever_context("failed to get certificate serial number as BigNumber")?
1818
.to_hex_str()
19-
.context("failed to convert certificate serial number to hex string")
19+
.whatever_context("failed to convert certificate serial number to hex string")
2020
}
2121

22-
fn sha256_digest(&self) -> anyhow::Result<DigestBytes> {
22+
fn sha256_digest(&self) -> Result<DigestBytes, snafu::Whatever> {
2323
self.digest(MessageDigest::sha256())
24-
.context("failed to get certificate digest")
24+
.whatever_context("failed to get certificate digest")
2525
}
2626
}

rust/cert-tools/src/cli_args.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{fs, path::PathBuf};
22

3-
use anyhow::{Context, ensure};
43
use clap::Parser;
54
use openssl::x509::X509;
5+
use snafu::{ResultExt, ensure_whatever};
66

77
use crate::parsers::{parse_pem_contents, parse_pkcs12_file_workaround};
88

@@ -69,22 +69,22 @@ impl GeneratePkcs12 {
6969
}
7070

7171
impl CertInput {
72-
pub fn read(&self) -> anyhow::Result<Vec<X509>> {
73-
let file_contents =
74-
fs::read(self.path()).with_context(|| format!("failed to read file from {self:?}"))?;
72+
pub fn read(&self) -> Result<Vec<X509>, snafu::Whatever> {
73+
let file_contents = fs::read(self.path())
74+
.with_whatever_context(|_| format!("failed to read file from {self:?}"))?;
7575

7676
match self {
7777
CertInput::Pem(_) => {
78-
let certs = parse_pem_contents(&file_contents).with_context(|| {
78+
let certs = parse_pem_contents(&file_contents).with_whatever_context(|_| {
7979
format!(
8080
"failed to parse PEM contents from {path:?}",
8181
path = self.path()
8282
)
8383
})?;
84-
ensure!(
84+
let path = self.path();
85+
ensure_whatever!(
8586
!certs.is_empty(),
86-
"The PEM file {path:?} contained no certificates",
87-
path = self.path()
87+
"The PEM file at {path:?} contained no certificates",
8888
);
8989

9090
Ok(certs)

rust/cert-tools/src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
use std::{collections::HashMap, fs};
22

3-
use anyhow::{Context, ensure};
43
use cert_ext::CertExt;
54
use clap::Parser;
65
use cli_args::{Cli, GeneratePkcs12};
76
use openssl::x509::X509;
7+
use snafu::{ResultExt, ensure_whatever};
88
use stackable_secret_operator_utils::pkcs12::pkcs12_truststore;
99
use tracing::{info, level_filters::LevelFilter, warn};
1010

1111
mod cert_ext;
1212
mod cli_args;
1313
mod parsers;
1414

15-
pub fn main() -> anyhow::Result<()> {
15+
#[snafu::report]
16+
pub fn main() -> Result<(), snafu::Whatever> {
1617
let filter = tracing_subscriber::EnvFilter::builder()
1718
.with_default_directive(LevelFilter::INFO.into())
18-
.from_env()?;
19+
.from_env()
20+
.whatever_context("failed to create tracing subscriber EnvFilter")?;
1921
tracing_subscriber::fmt()
2022
// Short running tool does not need any complex output
2123
.with_target(false)
@@ -32,24 +34,24 @@ pub fn main() -> anyhow::Result<()> {
3234
Ok(())
3335
}
3436

35-
fn generate_pkcs12_truststore(cli_args: GeneratePkcs12) -> anyhow::Result<()> {
37+
fn generate_pkcs12_truststore(cli_args: GeneratePkcs12) -> Result<(), snafu::Whatever> {
3638
let certificate_sources = cli_args.certificate_sources();
37-
ensure!(
39+
ensure_whatever!(
3840
!certificate_sources.is_empty(),
3941
"The list of certificate sources can not be empty. Please provide at least on --pem or --pkcs12."
4042
);
4143
let certificate_sources = certificate_sources
4244
.iter()
4345
.map(|source| {
44-
let certificate = source.read().with_context(|| {
46+
let certificate = source.read().with_whatever_context(|_| {
4547
format!(
4648
"failed to read certificate source {path:?}",
4749
path = source.path()
4850
)
4951
})?;
5052
Ok((source, certificate))
5153
})
52-
.collect::<anyhow::Result<Vec<_>>>()?;
54+
.collect::<Result<Vec<_>, _>>()?;
5355

5456
let mut certificates = HashMap::<Vec<u8>, X509>::new();
5557
for (source, certificates_list) in certificate_sources.into_iter() {
@@ -88,8 +90,8 @@ fn generate_pkcs12_truststore(cli_args: GeneratePkcs12) -> anyhow::Result<()> {
8890

8991
let pkcs12_truststore_bytes =
9092
pkcs12_truststore(certificates.values().map(|c| &**c), &cli_args.out_password)
91-
.context("failed to create PKCS12 truststore from certificates")?;
92-
fs::write(&cli_args.out, &pkcs12_truststore_bytes).with_context(|| {
93+
.whatever_context("failed to create PKCS12 truststore from certificates")?;
94+
fs::write(&cli_args.out, &pkcs12_truststore_bytes).with_whatever_context(|_| {
9395
format!(
9496
"failed to write to output PKCS12 truststore at {:?}",
9597
cli_args.out

rust/cert-tools/src/parsers.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use std::{
33
process::{Command, Stdio},
44
};
55

6-
use anyhow::{Context, bail};
76
use openssl::{pkcs12::Pkcs12, x509::X509};
7+
use snafu::{OptionExt, ResultExt, whatever};
88
use stackable_secret_operator_utils::pem::split_pem_certificates;
99

10-
pub fn parse_pem_contents(pem_bytes: &[u8]) -> anyhow::Result<Vec<X509>> {
10+
pub fn parse_pem_contents(pem_bytes: &[u8]) -> Result<Vec<X509>, snafu::Whatever> {
1111
let pems = split_pem_certificates(pem_bytes);
1212
pems.into_iter()
13-
.map(|pem| X509::from_pem(pem).context("failed to parse PEM encoded certificate"))
13+
.map(|pem| X509::from_pem(pem).whatever_context("failed to parse PEM encoded certificate"))
1414
.collect()
1515
}
1616

@@ -44,15 +44,18 @@ pub fn parse_pem_contents(pem_bytes: &[u8]) -> anyhow::Result<Vec<X509>> {
4444
/// The proper solution would be that secret-operator writes PKCS12 truststores using modern algorithms.
4545
/// For that we probably(?) drop the p12 crate?
4646
#[allow(unused)]
47-
pub fn parse_pkcs12_file(file_contents: &[u8], password: &str) -> anyhow::Result<Vec<X509>> {
47+
pub fn parse_pkcs12_file(
48+
file_contents: &[u8],
49+
password: &str,
50+
) -> Result<Vec<X509>, snafu::Whatever> {
4851
let parsed = Pkcs12::from_der(file_contents)
49-
.context("failed to parse PKCS12 DER encoded file")?
52+
.whatever_context("failed to parse PKCS12 DER encoded file")?
5053
.parse2(password)
51-
.context("Failed to parse PKCS12 using the provided password")?;
54+
.whatever_context("Failed to parse PKCS12 using the provided password")?;
5255

5356
parsed
5457
.ca
55-
.context("pkcs12 truststore did not contain a CA")?
58+
.whatever_context("pkcs12 truststore did not contain a CA")?
5659
.into_iter()
5760
.map(Ok)
5861
.collect()
@@ -64,7 +67,7 @@ pub fn parse_pkcs12_file(file_contents: &[u8], password: &str) -> anyhow::Result
6467
pub fn parse_pkcs12_file_workaround(
6568
file_contents: &[u8],
6669
password: &str,
67-
) -> anyhow::Result<Vec<X509>> {
70+
) -> Result<Vec<X509>, snafu::Whatever> {
6871
let mut child = Command::new("openssl")
6972
.args(&[
7073
"pkcs12",
@@ -78,27 +81,27 @@ pub fn parse_pkcs12_file_workaround(
7881
.stdout(Stdio::piped())
7982
.stderr(Stdio::piped())
8083
.spawn()
81-
.context("Failed to spawn openssl process")?;
84+
.whatever_context("Failed to spawn openssl process")?;
8285

8386
{
8487
let stdin = child
8588
.stdin
8689
.as_mut()
87-
.context("Failed to open openssl process stdin")?;
90+
.whatever_context("Failed to open openssl process stdin")?;
8891
stdin
8992
.write_all(file_contents)
90-
.context("Failed to write PKCS12 data to openssl process stdin")?;
93+
.whatever_context("Failed to write PKCS12 data to openssl process stdin")?;
9194
}
9295

9396
let output = child
9497
.wait_with_output()
95-
.context("Failed to read openssl process output")?;
98+
.whatever_context("Failed to read openssl process output")?;
9699
if !output.status.success() {
97100
let stderr = String::from_utf8_lossy(&output.stderr);
98-
bail!("openssl process failed with STDERR: {stderr:?}");
101+
whatever!("openssl process failed with STDERR: {stderr:?}");
99102
}
100103

101-
parse_pem_contents(&output.stdout).with_context(|| {
104+
parse_pem_contents(&output.stdout).with_whatever_context(|_| {
102105
format!(
103106
"failed to parse openssl process output, which should be PEM. STDOUT: {stdout}?",
104107
stdout = String::from_utf8_lossy(&output.stdout)

0 commit comments

Comments
 (0)