use std::{fmt, sync::Arc};
use zeroize::Zeroize;
#[derive(Clone, Default, Eq, PartialEq)]
pub struct EngineTls {
trust: EngineTlsTrust,
}
#[derive(Clone, Default, Eq, PartialEq)]
enum EngineTlsTrust {
#[default]
SystemRoots,
CustomRootsPem(Arc<[u8]>),
}
impl EngineTls {
pub const fn system_roots() -> Self {
Self {
trust: EngineTlsTrust::SystemRoots,
}
}
pub fn custom_roots_pem(pem: impl Into<Vec<u8>>) -> Self {
Self {
trust: EngineTlsTrust::CustomRootsPem(Arc::from(pem.into())),
}
}
pub(crate) fn custom_roots_pem_bytes(&self) -> Option<&[u8]> {
match &self.trust {
EngineTlsTrust::SystemRoots => None,
EngineTlsTrust::CustomRootsPem(pem) => Some(pem),
}
}
}
impl fmt::Debug for EngineTls {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut diagnostic = formatter.debug_struct("EngineTls");
match &self.trust {
EngineTlsTrust::SystemRoots => {
diagnostic.field("trust", &"SystemRoots");
}
EngineTlsTrust::CustomRootsPem(pem) => {
diagnostic
.field("trust", &"CustomRootsPem")
.field("pem_bytes", &pem.len());
}
}
diagnostic.finish()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EngineSaslMechanism {
Plain,
ScramSha256,
ScramSha512,
}
#[derive(Clone, Eq, PartialEq)]
pub struct EngineSasl {
mechanism: EngineSaslMechanism,
username: Arc<str>,
password: Arc<SecretText>,
}
impl EngineSasl {
pub fn plain(username: impl Into<String>, password: impl Into<String>) -> Self {
Self::new(EngineSaslMechanism::Plain, username, password)
}
pub fn scram_sha_256(username: impl Into<String>, password: impl Into<String>) -> Self {
Self::new(EngineSaslMechanism::ScramSha256, username, password)
}
pub fn scram_sha_512(username: impl Into<String>, password: impl Into<String>) -> Self {
Self::new(EngineSaslMechanism::ScramSha512, username, password)
}
pub const fn mechanism(&self) -> EngineSaslMechanism {
self.mechanism
}
fn new(
mechanism: EngineSaslMechanism,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
Self {
mechanism,
username: Arc::from(username.into()),
password: Arc::new(SecretText(password.into())),
}
}
pub(crate) fn credentials(&self) -> (&str, &str) {
(&self.username, &self.password.0)
}
}
impl fmt::Debug for EngineSasl {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("EngineSasl")
.field("mechanism", &self.mechanism)
.finish_non_exhaustive()
}
}
#[derive(Eq, PartialEq)]
struct SecretText(String);
impl Drop for SecretText {
fn drop(&mut self) {
self.0.zeroize();
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EngineSecurity {
Plaintext,
Tls(EngineTls),
SaslPlaintext(EngineSasl),
SaslTls {
tls: EngineTls,
sasl: EngineSasl,
},
}
impl EngineSecurity {
pub const fn plaintext() -> Self {
Self::Plaintext
}
pub const fn tls(tls: EngineTls) -> Self {
Self::Tls(tls)
}
pub const fn sasl_plaintext(sasl: EngineSasl) -> Self {
Self::SaslPlaintext(sasl)
}
pub const fn sasl_tls(tls: EngineTls, sasl: EngineSasl) -> Self {
Self::SaslTls { tls, sasl }
}
}
impl Default for EngineSecurity {
fn default() -> Self {
Self::Plaintext
}
}