Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implement PartialEq for most Snafu Error enums #757

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ futures-util = "0.3.30"
hyper = { version = "1.0.0", features = ["full"] }
hyper-util = "0.1.3"
json-patch = "1.0.0"
k8s-openapi = { version = "0.21.0", default-features = false, features = ["schemars","v1_28"] }
k8s-openapi = { version = "0.21.0", default-features = false, features = ["schemars", "v1_28"] }
# We use rustls instead of openssl for easier portablitly, e.g. so that we can build stackablectl without the need to vendor (build from source) openssl
kube = { version = "0.88.1", default-features = false, features = ["client","jsonpatch","runtime","derive","rustls-tls"] }
kube = { version = "0.88.1", default-features = false, features = ["client", "jsonpatch", "runtime", "derive", "rustls-tls"] }
lazy_static = "1.4.0"
opentelemetry = "0.22.0"
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
Expand All @@ -51,7 +51,7 @@ sha2 = { version = "0.10.8", features = ["oid"] }
signature = "2.2.0"
snafu = "0.8.0"
stackable-operator-derive = { path = "stackable-operator-derive" }
strum = { version = "0.26.1", features = ["derive"] }
strum = { version = "0.26.2", features = ["derive"] }
syn = "2.0.28"
tempfile = "3.7.1"
thiserror = "1.0.44"
Expand Down
42 changes: 42 additions & 0 deletions crates/stackable-certs/src/ca/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ pub enum Error {
ParseAuthorityKeyIdentifier { source: x509_cert::der::Error },
}

/// Custom implementation of [`std::cmp::PartialEq`] because some inner types
/// don't implement it.
///
/// Note that this implementation is restritced to testing because there is a
/// variant that is impossible to compare, and will cause a panic if it is
/// attemped.
#[cfg(test)]
impl PartialEq for Error {
NickLarsenNZ marked this conversation as resolved.
Show resolved Hide resolved
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::CreateCertificateBuilder { source: lhs_source },
Self::CreateCertificateBuilder { source: rhs_source },
)
| (
Self::AddCertificateExtension { source: lhs_source },
Self::AddCertificateExtension { source: rhs_source },
)
| (
Self::BuildCertificate { source: lhs_source },
Self::BuildCertificate { source: rhs_source },
) => match (lhs_source, rhs_source) {
(x509_cert::builder::Error::Asn1(lhs), x509_cert::builder::Error::Asn1(rhs)) => {
lhs == rhs
}
(
x509_cert::builder::Error::PublicKey(lhs),
x509_cert::builder::Error::PublicKey(rhs),
) => lhs == rhs,
(
x509_cert::builder::Error::Signature(_),
x509_cert::builder::Error::Signature(_),
) => panic!(
"it is impossible to compare the opaque Error contained witin signature::error::Error"
),
_ => false,
},
(lhs, rhs) => lhs == rhs,
}
}
}

/// Defines all error variants which can occur when loading a CA from a
/// Kubernetes [`Secret`].
#[derive(Debug, Snafu)]
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-certs/src/keys/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::keys::CertificateKeypair;

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
#[snafu(context(false))]
SerializeKeyToPem { source: x509_cert::spki::Error },
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-certs/src/keys/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const KEY_SIZE: usize = 4096;

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
#[snafu(display("failed to create RSA key"))]
CreateKey { source: rsa::Error },
Expand Down
17 changes: 16 additions & 1 deletion crates/stackable-certs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ where
ReadFile { source: std::io::Error },
}

/// Custom implementation of [`std::cmp::PartialEq`] because [`std::io::Error`] doesn't implement it, but [`std::io::ErrorKind`] does.
impl<E: snafu::Error + std::cmp::PartialEq> PartialEq for CertificatePairError<E> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::WriteFile { source: lhs_source }, Self::WriteFile { source: rhs_source }) => {
lhs_source.kind() == rhs_source.kind()
}
(Self::ReadFile { source: lhs_source }, Self::ReadFile { source: rhs_source }) => {
lhs_source.kind() == rhs_source.kind()
}
(lhs, rhs) => lhs == rhs,
}
}
}

/// Contains the certificate and the signing / embedded key pair.
///
/// A [`CertificateAuthority`](crate::ca::CertificateAuthority) uses this struct
Expand Down Expand Up @@ -155,7 +170,7 @@ pub enum PrivateKeyType {
}

/// Private and public key encoding, either DER or PEM.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum KeyEncoding {
Pem,
Der,
Expand Down
10 changes: 8 additions & 2 deletions crates/stackable-operator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Changed

- Implement `PartialEq` for most _Snafu_ Error enums ([#757]).

[#757]: https://github.com/stackabletech/operator-rs/pull/757

## [0.65.0] - 2024-03-25

## Added
### Added

- Add `stackable_webhook` crate which provides utilities to create webhooks with TLS termination ([#730]).
- Add `ConversionReview` re-export in `stackable_webhook` crate ([#749]).

[#730]: https://github.com/stackabletech/operator-rs/pull/730
[#749]: https://github.com/stackabletech/operator-rs/pull/749

## Changed
### Changed

- Remove `resources` key from `DynamicValues` struct ([#734]).
- Bump `opentelemetry`, `opentelemetry_sdk`, `opentelemetry-jaeger`, and `tracing-opentelemetry` Rust dependencies
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/builder/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

// NOTE (Techassi): Think about that name
#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum ObjectMetaBuilderError {
#[snafu(display("failed to set recommended labels"))]
RecommendedLabels { source: LabelError },
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/builder/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod resources;
pub mod security;
pub mod volume;

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
#[snafu(display("termination grace period is too long (got {duration}, maximum allowed is {max})", max = Duration::from_secs(i64::MAX as u64)))]
TerminationGracePeriodTooLong {
Expand Down
4 changes: 2 additions & 2 deletions crates/stackable-operator/src/builder/pod/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl VolumeMountBuilder {
}
}

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum SecretOperatorVolumeSourceBuilderError {
#[snafu(display("failed to parse secret operator volume annotation"))]
ParseAnnotation { source: AnnotationError },
Expand Down Expand Up @@ -414,7 +414,7 @@ impl ListenerReference {

// NOTE (Techassi): We might want to think about these names and how long they
// are getting.
#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum ListenerOperatorVolumeSourceBuilderError {
#[snafu(display("failed to convert listener reference into Kubernetes annotation"))]
ListenerReferenceAnnotation { source: AnnotationError },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
#[snafu(display(
"failed to convert bind credentials (secret class volume) into named Kubernetes volume"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const DEFAULT_OIDC_WELLKNOWN_PATH: &str = ".well-known/openid-configuration"
pub const CLIENT_ID_SECRET_KEY: &str = "clientId";
pub const CLIENT_SECRET_SECRET_KEY: &str = "clientSecret";

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
#[snafu(display("failed to parse OIDC endpoint url"))]
ParseOidcEndpointUrl { source: ParseError },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct AuthenticationProvider {
pub client_cert_secret_class: Option<String>,
}

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum TlsClientDetailsError {
#[snafu(display("failed to convert secret class volume into named Kubernetes volume"))]
SecretClassVolume { source: SecretClassVolumeError },
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/commons/secret_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::builder::{
SecretOperatorVolumeSourceBuilder, SecretOperatorVolumeSourceBuilderError, VolumeBuilder,
};

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum SecretClassVolumeError {
#[snafu(display("failed to build secret operator volume"))]
SecretOperatorVolume {
Expand Down
6 changes: 3 additions & 3 deletions crates/stackable-operator/src/config/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> Validator<'a> {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
struct FieldPath {
idents: Vec<String>,
}
Expand All @@ -74,7 +74,7 @@ impl Display for FieldPath {
}
}

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
#[snafu(display("failed to validate {path}"))]
/// An error that occurred when validating an object.
///
Expand All @@ -85,7 +85,7 @@ pub struct ValidationError {
problem: ValidationProblem,
}
/// A problem that was discovered during validation, with no additional context.
#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
enum ValidationProblem {
#[snafu(display("field is required"))]
FieldRequired,
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/kvp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ where
}
}

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum KeyValuePairsError {
#[snafu(display("key/value pair already exists"))]
PairAlreadyExists,
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/product_config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
role_utils::{CommonConfiguration, Role},
};

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum ConfigError {
#[snafu(display("Invalid configuration found: {reason}"))]
InvalidConfiguration { reason: String },
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-operator/src/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use schemars::JsonSchema;
use snafu::{OptionExt, ResultExt, Snafu};
use strum::IntoEnumIterator;

#[derive(Debug, Snafu, PartialEq)]
#[derive(Debug, PartialEq, Snafu)]
#[snafu(module)]
pub enum DurationParseError {
#[snafu(display("empty input"))]
Expand Down
25 changes: 25 additions & 0 deletions crates/stackable-webhook/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ pub enum Error {
},
}

/// Custom implementation of [`std::cmp::PartialEq`] because some inner types
/// don't implement it.
///
/// Note that this implementation is restritced to testing because there are
/// variants that use [`stackable_certs::ca::Error`] which only implements
/// [`PartialEq`] for tests.
#[cfg(test)]
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
Self::BindTcpListener {
source: lhs_source,
socket_addr: lhs_socket_addr,
},
Self::BindTcpListener {
source: rhs_source,
socket_addr: rhs_socket_addr,
},
) => lhs_socket_addr == rhs_socket_addr && lhs_source.kind() == rhs_source.kind(),
(lhs, rhs) => lhs == rhs,
}
}
}

/// A server which terminates TLS connections and allows clients to commnunicate
/// via HTTPS with the underlying HTTP router.
pub struct TlsServer {
Expand Down
Loading