Skip to main content

confluent_kafka/common/config/
ssl_client_auth.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! SSL client authentication policy.
16//!
17//! Translated from `org.apache.kafka.common.config.SslClientAuth`.
18
19use std::fmt;
20use std::str::FromStr;
21
22/// Describes whether the server should require or request client authentication.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
24#[non_exhaustive]
25#[doc(alias = "org.apache.kafka.common.config.SslClientAuth")]
26pub enum SslClientAuth {
27    /// Server requires client certificate.
28    Required,
29    /// Server requests but does not require client certificate.
30    Requested,
31    /// Server does not request client certificate.
32    #[default]
33    None,
34}
35
36impl SslClientAuth {
37    /// All variants in declaration order.
38    const ALL: [SslClientAuth; 3] = [SslClientAuth::Required, SslClientAuth::Requested, SslClientAuth::None];
39
40    /// Returns the variant name in uppercase, matching the Java enum name.
41    fn name(self) -> &'static str {
42        match self {
43            SslClientAuth::Required => "REQUIRED",
44            SslClientAuth::Requested => "REQUESTED",
45            SslClientAuth::None => "NONE",
46        }
47    }
48
49    /// Returns the constant whose name matches `key` ignoring case, or
50    /// [`SslClientAuth::None`] when `key` is `None`.
51    ///
52    /// # Arguments
53    ///
54    /// - `key` - the config value to look up
55    ///
56    /// # Returns
57    ///
58    /// The matching constant, or `None` when `key` is set but matches no constant.
59    #[doc(alias = "org.apache.kafka.common.config.SslClientAuth#forConfig")]
60    pub fn for_config(key: Option<&str>) -> Option<Self> {
61        match key {
62            Option::None => Some(SslClientAuth::None),
63            Some(k) => {
64                let upper = k.to_uppercase();
65                Self::ALL.iter().find(|a| a.name() == upper).copied()
66            },
67        }
68    }
69}
70
71impl fmt::Display for SslClientAuth {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        // Java's toString() returns lowercase
74        write!(f, "{}", self.name().to_lowercase())
75    }
76}
77
78impl FromStr for SslClientAuth {
79    type Err = String;
80
81    // Parses an `SslClientAuth` from its name (case-insensitive).
82    fn from_str(s: &str) -> Result<Self, Self::Err> {
83        let upper = s.to_uppercase();
84        SslClientAuth::ALL
85            .iter()
86            .find(|a| a.name() == upper)
87            .copied()
88            .ok_or_else(|| format!("No enum constant SslClientAuth.{}", s))
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_default() {
98        assert_eq!(SslClientAuth::default(), SslClientAuth::None);
99    }
100
101    #[test]
102    fn test_for_config_none_key() {
103        assert_eq!(SslClientAuth::for_config(Option::None), Some(SslClientAuth::None));
104    }
105
106    #[test]
107    fn test_for_config_required() {
108        assert_eq!(SslClientAuth::for_config(Some("REQUIRED")), Some(SslClientAuth::Required));
109    }
110
111    #[test]
112    fn test_for_config_requested() {
113        assert_eq!(SslClientAuth::for_config(Some("REQUESTED")), Some(SslClientAuth::Requested));
114    }
115
116    #[test]
117    fn test_for_config_none_variant() {
118        assert_eq!(SslClientAuth::for_config(Some("NONE")), Some(SslClientAuth::None));
119    }
120
121    #[test]
122    fn test_for_config_case_insensitive() {
123        assert_eq!(SslClientAuth::for_config(Some("required")), Some(SslClientAuth::Required));
124        assert_eq!(SslClientAuth::for_config(Some("Requested")), Some(SslClientAuth::Requested));
125        assert_eq!(SslClientAuth::for_config(Some("none")), Some(SslClientAuth::None));
126    }
127
128    #[test]
129    fn test_for_config_invalid() {
130        assert_eq!(SslClientAuth::for_config(Some("INVALID")), Option::None);
131        assert_eq!(SslClientAuth::for_config(Some("")), Option::None);
132    }
133
134    #[test]
135    fn test_display() {
136        assert_eq!(format!("{}", SslClientAuth::Required), "required");
137        assert_eq!(format!("{}", SslClientAuth::Requested), "requested");
138        assert_eq!(format!("{}", SslClientAuth::None), "none");
139    }
140
141    #[test]
142    fn test_from_str_roundtrip() {
143        for variant in &SslClientAuth::ALL {
144            let name = variant.name();
145            let parsed: SslClientAuth = name.parse().unwrap();
146            assert_eq!(*variant, parsed);
147        }
148    }
149
150    #[test]
151    fn test_from_str_case_insensitive() {
152        assert_eq!("required".parse::<SslClientAuth>().unwrap(), SslClientAuth::Required);
153        assert_eq!("None".parse::<SslClientAuth>().unwrap(), SslClientAuth::None);
154    }
155
156    #[test]
157    fn test_from_str_invalid() {
158        assert!("INVALID".parse::<SslClientAuth>().is_err());
159        assert!("".parse::<SslClientAuth>().is_err());
160    }
161
162    #[test]
163    fn test_clone_and_copy() {
164        let a = SslClientAuth::Required;
165        let b = a;
166        assert_eq!(a, b);
167    }
168}