use super::providers::{KafkaProvider, KnownProvider, validate};
pub const DEFAULT_ALLOWED: &[&str] = &[
"strimzi",
"redpanda",
"msk",
"redpanda-cloud",
"confluent-cloud",
"plaintext",
];
pub fn require(provider: &str, allowed: &[&str]) -> Result<(&'static str, &'static str), String> {
if !allowed.contains(&provider) {
return Err(format!(
"provider {provider:?} is not in the allowed set {allowed:?}"
));
}
Ok(KnownProvider::parse(provider)?.auth())
}
pub fn require_blessed(provider: &str) -> Result<(&'static str, &'static str), String> {
require(provider, DEFAULT_ALLOWED)
}
pub fn assert_not_weakened(
provider: &str,
security_protocol: &str,
sasl_mechanism: &str,
) -> Result<(), String> {
validate(security_protocol, sasl_mechanism)?;
let (want_proto, want_mech) = KnownProvider::parse(provider)?.auth();
if !security_protocol.eq_ignore_ascii_case(want_proto) || sasl_mechanism != want_mech {
return Err(format!(
"config for provider {provider:?} weakens its auth: got \
({security_protocol}, {sasl_mechanism}); the provider's strongest is \
({want_proto}, {want_mech})"
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn require_blessed_returns_derived_auth() {
assert_eq!(
require_blessed("confluent-cloud").unwrap(),
("SASL_SSL", "PLAIN")
);
assert_eq!(
require_blessed("strimzi").unwrap(),
("SASL_SSL", "SCRAM-SHA-512")
);
}
#[test]
fn require_refuses_quarantined_and_unknown() {
assert!(require_blessed("msk_iam").is_err());
assert!(require_blessed("kinesis").is_err());
}
#[test]
fn require_honours_a_custom_allow_list() {
assert!(require("redpanda", &["redpanda"]).is_ok());
assert!(require("confluent-cloud", &["redpanda"]).is_err());
}
#[test]
fn assert_not_weakened_accepts_the_strongest() {
assert!(assert_not_weakened("strimzi", "SASL_SSL", "SCRAM-SHA-512").is_ok());
assert!(assert_not_weakened("strimzi", "sasl_ssl", "SCRAM-SHA-512").is_ok());
assert!(assert_not_weakened("confluent-cloud", "SASL_SSL", "PLAIN").is_ok());
}
#[test]
fn assert_not_weakened_refuses_a_downgrade() {
assert!(assert_not_weakened("strimzi", "SASL_SSL", "PLAIN").is_err());
}
}