kafka_client_engine/config/
security.rs1use std::{fmt, sync::Arc};
4
5use zeroize::Zeroize;
6
7#[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 pub const fn system_roots() -> Self {
23 Self {
24 trust: EngineTlsTrust::SystemRoots,
25 }
26 }
27
28 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#[derive(Clone, Copy, Debug, Eq, PartialEq)]
62pub enum EngineSaslMechanism {
63 Plain,
65 ScramSha256,
67 ScramSha512,
69}
70
71#[derive(Clone, Eq, PartialEq)]
73pub struct EngineSasl {
74 mechanism: EngineSaslMechanism,
75 username: Arc<str>,
76 password: Arc<SecretText>,
77}
78
79impl EngineSasl {
80 pub fn plain(username: impl Into<String>, password: impl Into<String>) -> Self {
82 Self::new(EngineSaslMechanism::Plain, username, password)
83 }
84
85 pub fn scram_sha_256(username: impl Into<String>, password: impl Into<String>) -> Self {
87 Self::new(EngineSaslMechanism::ScramSha256, username, password)
88 }
89
90 pub fn scram_sha_512(username: impl Into<String>, password: impl Into<String>) -> Self {
92 Self::new(EngineSaslMechanism::ScramSha512, username, password)
93 }
94
95 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#[derive(Clone, Debug, Eq, PartialEq)]
137pub enum EngineSecurity {
138 Plaintext,
140 Tls(EngineTls),
142 SaslPlaintext(EngineSasl),
144 SaslTls {
146 tls: EngineTls,
148 sasl: EngineSasl,
150 },
151}
152
153impl EngineSecurity {
154 pub const fn plaintext() -> Self {
156 Self::Plaintext
157 }
158
159 pub const fn tls(tls: EngineTls) -> Self {
161 Self::Tls(tls)
162 }
163
164 pub const fn sasl_plaintext(sasl: EngineSasl) -> Self {
166 Self::SaslPlaintext(sasl)
167 }
168
169 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}