Skip to main content

kafka_client_engine/config/
security.rs

1//! Client-owned security policy validated into private driver configuration.
2
3use std::{fmt, sync::Arc};
4
5use zeroize::Zeroize;
6
7/// Engine-owned TLS trust selection.
8#[derive(Clone, Default, Eq, PartialEq)]
9pub struct EngineTls {
10    trust: EngineTlsTrust,
11}
12
13#[derive(Clone, Default, Eq, PartialEq)]
14enum EngineTlsTrust {
15    #[default]
16    SystemRoots,
17    CustomRootsPem(Arc<[u8]>),
18}
19
20impl EngineTls {
21    /// Uses a startup snapshot of the platform-native certificate roots.
22    pub const fn system_roots() -> Self {
23        Self {
24            trust: EngineTlsTrust::SystemRoots,
25        }
26    }
27
28    /// Retains one caller-owned PEM certificate bundle for startup validation.
29    pub fn custom_roots_pem(pem: impl Into<Vec<u8>>) -> Self {
30        Self {
31            trust: EngineTlsTrust::CustomRootsPem(Arc::from(pem.into())),
32        }
33    }
34
35    pub(crate) fn custom_roots_pem_bytes(&self) -> Option<&[u8]> {
36        match &self.trust {
37            EngineTlsTrust::SystemRoots => None,
38            EngineTlsTrust::CustomRootsPem(pem) => Some(pem),
39        }
40    }
41}
42
43impl fmt::Debug for EngineTls {
44    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
45        let mut diagnostic = formatter.debug_struct("EngineTls");
46        match &self.trust {
47            EngineTlsTrust::SystemRoots => {
48                diagnostic.field("trust", &"SystemRoots");
49            }
50            EngineTlsTrust::CustomRootsPem(pem) => {
51                diagnostic
52                    .field("trust", &"CustomRootsPem")
53                    .field("pem_bytes", &pem.len());
54            }
55        }
56        diagnostic.finish()
57    }
58}
59
60/// Engine-owned SASL mechanism vocabulary.
61#[derive(Clone, Copy, Debug, Eq, PartialEq)]
62pub enum EngineSaslMechanism {
63    /// SASL/PLAIN.
64    Plain,
65    /// SCRAM-SHA-256.
66    ScramSha256,
67    /// SCRAM-SHA-512.
68    ScramSha512,
69}
70
71/// Engine-owned credentials with redacted diagnostics and zeroized final release.
72#[derive(Clone, Eq, PartialEq)]
73pub struct EngineSasl {
74    mechanism: EngineSaslMechanism,
75    username: Arc<str>,
76    password: Arc<SecretText>,
77}
78
79impl EngineSasl {
80    /// Creates unvalidated SASL/PLAIN credentials for engine startup validation.
81    pub fn plain(username: impl Into<String>, password: impl Into<String>) -> Self {
82        Self::new(EngineSaslMechanism::Plain, username, password)
83    }
84
85    /// Creates unvalidated SCRAM-SHA-256 credentials for engine startup validation.
86    pub fn scram_sha_256(username: impl Into<String>, password: impl Into<String>) -> Self {
87        Self::new(EngineSaslMechanism::ScramSha256, username, password)
88    }
89
90    /// Creates unvalidated SCRAM-SHA-512 credentials for engine startup validation.
91    pub fn scram_sha_512(username: impl Into<String>, password: impl Into<String>) -> Self {
92        Self::new(EngineSaslMechanism::ScramSha512, username, password)
93    }
94
95    /// Returns the configured mechanism without exposing credentials.
96    pub const fn mechanism(&self) -> EngineSaslMechanism {
97        self.mechanism
98    }
99
100    fn new(
101        mechanism: EngineSaslMechanism,
102        username: impl Into<String>,
103        password: impl Into<String>,
104    ) -> Self {
105        Self {
106            mechanism,
107            username: Arc::from(username.into()),
108            password: Arc::new(SecretText(password.into())),
109        }
110    }
111
112    pub(crate) fn credentials(&self) -> (&str, &str) {
113        (&self.username, &self.password.0)
114    }
115}
116
117impl fmt::Debug for EngineSasl {
118    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
119        formatter
120            .debug_struct("EngineSasl")
121            .field("mechanism", &self.mechanism)
122            .finish_non_exhaustive()
123    }
124}
125
126#[derive(Eq, PartialEq)]
127struct SecretText(String);
128
129impl Drop for SecretText {
130    fn drop(&mut self) {
131        self.0.zeroize();
132    }
133}
134
135/// Complete engine-owned transport and authentication policy.
136#[derive(Clone, Debug, Eq, PartialEq)]
137pub enum EngineSecurity {
138    /// Plain TCP without SASL.
139    Plaintext,
140    /// TLS without SASL.
141    Tls(EngineTls),
142    /// SASL over plain TCP.
143    SaslPlaintext(EngineSasl),
144    /// SASL over TLS.
145    SaslTls {
146        /// TLS trust used by each logical broker identity.
147        tls: EngineTls,
148        /// Broker authentication mechanism and credentials.
149        sasl: EngineSasl,
150    },
151}
152
153impl EngineSecurity {
154    /// Selects plain TCP without broker authentication.
155    pub const fn plaintext() -> Self {
156        Self::Plaintext
157    }
158
159    /// Selects TLS transport without SASL.
160    pub const fn tls(tls: EngineTls) -> Self {
161        Self::Tls(tls)
162    }
163
164    /// Selects SASL over plain TCP.
165    pub const fn sasl_plaintext(sasl: EngineSasl) -> Self {
166        Self::SaslPlaintext(sasl)
167    }
168
169    /// Selects SASL over TLS.
170    pub const fn sasl_tls(tls: EngineTls, sasl: EngineSasl) -> Self {
171        Self::SaslTls { tls, sasl }
172    }
173}
174
175impl Default for EngineSecurity {
176    fn default() -> Self {
177        Self::Plaintext
178    }
179}