Skip to main content

confluent_kafka/admin/
user_scram_credential_alteration.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//! A request to alter a user's SASL/SCRAM credentials.
16
17use super::{UserScramCredentialDeletion, UserScramCredentialUpsertion};
18
19/// A request to alter a user's SASL/SCRAM credentials.
20///
21/// 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).
22// Java models this as an `abstract` base with the two concrete subclasses
23// `UserScramCredentialUpsertion` and `UserScramCredentialDeletion`. Rust models
24// the closed subclass hierarchy as an enum: the Java `instanceof` dispatch in
25// `KafkaAdminClient.alterUserScramCredentials` becomes a `match` here, and the
26// shared `user()` accessor of the abstract base becomes a method delegating to
27// the active variant.
28#[derive(Debug, Clone)]
29#[non_exhaustive]
30#[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialAlteration")]
31pub enum UserScramCredentialAlteration {
32    /// An update/insertion of a credential ([`UserScramCredentialUpsertion`]).
33    Upsertion(UserScramCredentialUpsertion),
34    /// A deletion of a credential ([`UserScramCredentialDeletion`]).
35    Deletion(UserScramCredentialDeletion),
36}
37
38impl UserScramCredentialAlteration {
39    /// Returns the always non-null user.
40    #[doc(alias = "org.apache.kafka.clients.admin.UserScramCredentialAlteration#user")]
41    pub fn user(&self) -> &str {
42        match self {
43            UserScramCredentialAlteration::Upsertion(u) => u.user(),
44            UserScramCredentialAlteration::Deletion(d) => d.user(),
45        }
46    }
47}
48
49impl From<UserScramCredentialUpsertion> for UserScramCredentialAlteration {
50    fn from(upsertion: UserScramCredentialUpsertion) -> Self {
51        UserScramCredentialAlteration::Upsertion(upsertion)
52    }
53}
54
55impl From<UserScramCredentialDeletion> for UserScramCredentialAlteration {
56    fn from(deletion: UserScramCredentialDeletion) -> Self {
57        UserScramCredentialAlteration::Deletion(deletion)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use crate::admin::{ScramCredentialInfo, ScramMechanism};
65
66    #[test]
67    fn user_accessor_delegates_to_variant() {
68        let upsertion: UserScramCredentialAlteration = UserScramCredentialUpsertion::with_str(
69            "u1",
70            ScramCredentialInfo::new(ScramMechanism::ScramSha256, 4096),
71            "pw",
72        )
73        .into();
74        assert_eq!(upsertion.user(), "u1");
75
76        let deletion: UserScramCredentialAlteration =
77            UserScramCredentialDeletion::new("u2", ScramMechanism::ScramSha512).into();
78        assert_eq!(deletion.user(), "u2");
79    }
80}