Skip to main content

confluent_kafka/admin/
user_scram_credentials_description.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//! All SASL/SCRAM credentials associated with a user.
16
17use super::ScramCredentialInfo;
18
19/// Representation of all SASL/SCRAM credentials associated with a user that can
20/// be retrieved.
21///
22/// See [KIP-554: Add Broker-side SCRAM Config API](https://cwiki.apache.org/confluence/display/KAFKA/KIP-554%3A+Add+Broker-side+SCRAM+Config+API).
23#[derive(Debug, Clone, PartialEq, Eq, Hash)]
24#[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialsDescription")]
25pub struct UserScramCredentialsDescription {
26    name: String,
27    credential_infos: Vec<ScramCredentialInfo>,
28}
29
30impl UserScramCredentialsDescription {
31    /// Creates a new description.
32    ///
33    /// # Arguments
34    ///
35    /// - `name` - the required user name
36    /// - `credential_infos` - the required SASL/SCRAM credential representations for the user
37    #[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialsDescription#UserScramCredentialsDescription")]
38    pub fn new(name: impl Into<String>, credential_infos: Vec<ScramCredentialInfo>) -> Self {
39        Self { name: name.into(), credential_infos }
40    }
41
42    /// Returns the user name.
43    #[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialsDescription#name")]
44    pub fn name(&self) -> &str {
45        &self.name
46    }
47
48    /// Returns the always non-null list of SASL/SCRAM credential representations
49    /// for the user.
50    #[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialsDescription#credentialInfos")]
51    pub fn credential_infos(&self) -> &[ScramCredentialInfo] {
52        &self.credential_infos
53    }
54}
55
56impl std::fmt::Display for UserScramCredentialsDescription {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        write!(
59            f,
60            "UserScramCredentialsDescription{{name='{}', credentialInfos={:?}}}",
61            self.name, self.credential_infos
62        )
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::admin::ScramMechanism;
70
71    #[test]
72    fn accessors_and_equality() {
73        let d = UserScramCredentialsDescription::new(
74            "u",
75            vec![ScramCredentialInfo::new(ScramMechanism::ScramSha256, 4096)],
76        );
77        assert_eq!(d.name(), "u");
78        assert_eq!(d.credential_infos().len(), 1);
79        assert_eq!(
80            d,
81            UserScramCredentialsDescription::new(
82                "u",
83                vec![ScramCredentialInfo::new(ScramMechanism::ScramSha256, 4096)]
84            )
85        );
86    }
87}