1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use serde::{Deserialize, Serialize};
use crate::AuthError;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
Serialize,
Deserialize,
strum::IntoStaticStr,
strum::EnumString,
)]
pub enum SaslMechanism {
#[strum(serialize = "PLAIN")]
Plain,
#[strum(serialize = "SCRAM-SHA-256")]
ScramSha256,
#[strum(serialize = "SCRAM-SHA-512")]
ScramSha512,
/// SASL/OAUTHBEARER (KIP-255 / RFC 7628). The broker's configured token
/// validator validates the bearer token.
#[strum(serialize = "OAUTHBEARER")]
OAuthBearer,
/// SASL/GSSAPI (Kerberos, RFC 4752). The GSS provider drives context
/// establishment and the RFC 4752 security-layer negotiation from the
/// broker's configured service keytab.
///
/// The variant exists in every build, because metadata records persist it.
/// The provider needs the `gssapi` Cargo feature; see
/// [`SaslMechanism::is_available`].
#[strum(serialize = "GSSAPI")]
Gssapi,
}
impl SaslMechanism {
#[must_use]
pub fn wire_name(self) -> &'static str {
self.into()
}
#[must_use]
pub fn from_wire(s: &str) -> Option<Self> {
s.parse().ok()
}
/// `true` for the SCRAM mechanisms, SHA-256 and SHA-512.
///
/// The handshake and authenticate code uses this where it treats both the
/// same way at the dispatch level.
#[must_use]
pub fn is_scram(self) -> bool {
matches!(self, Self::ScramSha256 | Self::ScramSha512)
}
/// Every mechanism, in declaration order.
pub const ALL: [Self; 5] = [
Self::Plain,
Self::ScramSha256,
Self::ScramSha512,
Self::OAuthBearer,
Self::Gssapi,
];
/// `true` when this build can run the mechanism.
///
/// GSSAPI needs the `gssapi` Cargo feature on a native target. Every other
/// mechanism is always available.
#[must_use]
pub const fn is_available(self) -> bool {
match self {
Self::Gssapi => cfg!(all(feature = "gssapi", not(target_family = "wasm"))),
Self::Plain | Self::ScramSha256 | Self::ScramSha512 | Self::OAuthBearer => true,
}
}
/// The mechanisms this build can run, in declaration order.
pub fn available() -> impl Iterator<Item = Self> {
Self::ALL.into_iter().filter(|m| m.is_available())
}
/// Returns `self` when this build can run the mechanism.
///
/// # Errors
///
/// [`AuthError::UnsupportedMechanism`] when [`SaslMechanism::is_available`]
/// is `false`. A broker answers that with Kafka's
/// `UNSUPPORTED_SASL_MECHANISM` (error code 33), as it does for a mechanism
/// the listener has not enabled.
pub const fn require_available(self) -> Result<Self, AuthError> {
if self.is_available() {
Ok(self)
} else {
Err(AuthError::UnsupportedMechanism)
}
}
}
#[cfg(test)]
mod tests {
use assert2::check;
use super::*;
#[test]
fn wire_name_round_trip() {
for m in [
SaslMechanism::Plain,
SaslMechanism::ScramSha256,
SaslMechanism::ScramSha512,
SaslMechanism::OAuthBearer,
] {
assert2::assert!(SaslMechanism::from_wire(m.wire_name()) == Some(m));
}
}
#[test]
fn from_wire_unknown_returns_none() {
for wire in ["SCRAM-SHA-128", "OAUTH", ""] {
assert2::assert!(SaslMechanism::from_wire(wire) == None);
}
}
/// Wire-exactness guard: only the canonical Kafka mechanism strings parse.
/// The Rust variant names and other casings must NOT parse, so the
/// `strum(serialize = ...)` attributes can never silently loosen the SASL
/// handshake's accepted mechanism set.
#[test]
fn from_wire_rejects_variant_names_and_casing() {
for wire in [
"Plain",
"plain",
"ScramSha256",
"scram-sha-256",
"OAuthBearer",
] {
assert2::assert!(SaslMechanism::from_wire(wire) == None);
}
}
#[test]
fn oauthbearer_wire_round_trip() {
check!(
(
SaslMechanism::from_wire("OAUTHBEARER"),
SaslMechanism::OAuthBearer.wire_name(),
SaslMechanism::OAuthBearer.is_scram(),
) == (Some(SaslMechanism::OAuthBearer), "OAUTHBEARER", false)
);
}
#[test]
fn gssapi_mechanism_roundtrips_wire_name() {
use std::str::FromStr;
assert2::assert!(
(
SaslMechanism::from_str("GSSAPI").unwrap(),
SaslMechanism::Gssapi.wire_name(),
) == (SaslMechanism::Gssapi, "GSSAPI")
);
}
/// Whether this test build has the GSSAPI provider.
const GSSAPI: bool = cfg!(all(feature = "gssapi", not(target_family = "wasm")));
#[test]
fn availability_follows_the_gssapi_feature() {
let available = SaslMechanism::ALL.map(|m| (m, m.is_available()));
let listed: Vec<_> = SaslMechanism::available().collect();
let want_listed: Vec<_> = SaslMechanism::ALL
.into_iter()
.filter(|m| *m != SaslMechanism::Gssapi || GSSAPI)
.collect();
assert2::assert!(
(available, listed)
== (
[
(SaslMechanism::Plain, true),
(SaslMechanism::ScramSha256, true),
(SaslMechanism::ScramSha512, true),
(SaslMechanism::OAuthBearer, true),
(SaslMechanism::Gssapi, GSSAPI),
],
want_listed,
)
);
}
#[test]
fn require_available_rejects_gssapi_without_the_feature() {
let want_gssapi = GSSAPI
.then_some(SaslMechanism::Gssapi)
.ok_or(AuthError::UnsupportedMechanism);
assert2::assert!(
(
SaslMechanism::Gssapi.require_available(),
SaslMechanism::Plain.require_available(),
) == (want_gssapi, Ok(SaslMechanism::Plain))
);
}
#[test]
fn is_scram_predicate() {
for (_case, mechanism, want) in [
("PLAIN", SaslMechanism::Plain, false),
("SCRAM SHA-256", SaslMechanism::ScramSha256, true),
("SCRAM SHA-512", SaslMechanism::ScramSha512, true),
] {
assert2::assert!(mechanism.is_scram() == want);
}
}
}