Skip to main content

confluent_kafka/common/acl/
acl_operation.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//! ACL operations.
16//!
17//! Corresponds to `org.apache.kafka.common.acl.AclOperation`.
18
19/// Represents an operation which an ACL grants or denies permission to perform.
20///
21/// Some operations imply other operations:
22/// - `ALLOW ALL` implies `ALLOW` everything
23/// - `DENY ALL` implies `DENY` everything
24/// - `ALLOW READ` implies `ALLOW DESCRIBE`
25/// - `ALLOW WRITE` implies `ALLOW DESCRIBE`
26/// - `ALLOW DELETE` implies `ALLOW DESCRIBE`
27/// - `ALLOW ALTER` implies `ALLOW DESCRIBE`
28/// - `ALLOW ALTER_CONFIGS` implies `ALLOW DESCRIBE_CONFIGS`
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
30#[non_exhaustive]
31#[doc(alias = "org.apache.kafka.common.acl.AclOperation")]
32pub enum AclOperation {
33    /// Represents any `AclOperation` which this client cannot understand,
34    /// perhaps because this client is too old.
35    Unknown,
36    /// In a filter, matches any `AclOperation`.
37    Any,
38    /// `ALL` operation.
39    All,
40    /// `READ` operation.
41    Read,
42    /// `WRITE` operation.
43    Write,
44    /// `CREATE` operation.
45    Create,
46    /// `DELETE` operation.
47    Delete,
48    /// `ALTER` operation.
49    Alter,
50    /// `DESCRIBE` operation.
51    Describe,
52    /// `CLUSTER_ACTION` operation.
53    ClusterAction,
54    /// `DESCRIBE_CONFIGS` operation.
55    DescribeConfigs,
56    /// `ALTER_CONFIGS` operation.
57    AlterConfigs,
58    /// `IDEMPOTENT_WRITE` operation.
59    IdempotentWrite,
60    /// `CREATE_TOKENS` operation.
61    CreateTokens,
62    /// `DESCRIBE_TOKENS` operation.
63    DescribeTokens,
64    /// `TWO_PHASE_COMMIT` operation.
65    TwoPhaseCommit,
66}
67
68// Note: we cannot have more than 30 ACL operations without modifying the format
69// used to describe ACL operations in MetadataResponse.
70
71impl AclOperation {
72    /// Returns all operations, in declaration (code) order.
73    #[doc(alias = "org.apache.kafka.common.acl.AclOperation#values")]
74    pub fn values() -> &'static [Self] {
75        &[
76            AclOperation::Unknown,
77            AclOperation::Any,
78            AclOperation::All,
79            AclOperation::Read,
80            AclOperation::Write,
81            AclOperation::Create,
82            AclOperation::Delete,
83            AclOperation::Alter,
84            AclOperation::Describe,
85            AclOperation::ClusterAction,
86            AclOperation::DescribeConfigs,
87            AclOperation::AlterConfigs,
88            AclOperation::IdempotentWrite,
89            AclOperation::CreateTokens,
90            AclOperation::DescribeTokens,
91            AclOperation::TwoPhaseCommit,
92        ]
93    }
94
95    /// Returns the code of this operation.
96    #[doc(alias = "org.apache.kafka.common.acl.AclOperation#code")]
97    pub fn code(&self) -> i8 {
98        match self {
99            AclOperation::Unknown => 0,
100            AclOperation::Any => 1,
101            AclOperation::All => 2,
102            AclOperation::Read => 3,
103            AclOperation::Write => 4,
104            AclOperation::Create => 5,
105            AclOperation::Delete => 6,
106            AclOperation::Alter => 7,
107            AclOperation::Describe => 8,
108            AclOperation::ClusterAction => 9,
109            AclOperation::DescribeConfigs => 10,
110            AclOperation::AlterConfigs => 11,
111            AclOperation::IdempotentWrite => 12,
112            AclOperation::CreateTokens => 13,
113            AclOperation::DescribeTokens => 14,
114            AclOperation::TwoPhaseCommit => 15,
115        }
116    }
117
118    /// Returns the `AclOperation` with the provided code or [`AclOperation::Unknown`]
119    /// if one cannot be found.
120    ///
121    /// # Arguments
122    ///
123    /// - `code` - the code to look up
124    #[doc(alias = "org.apache.kafka.common.acl.AclOperation#fromCode")]
125    pub fn from_code(code: i8) -> AclOperation {
126        match code {
127            0 => AclOperation::Unknown,
128            1 => AclOperation::Any,
129            2 => AclOperation::All,
130            3 => AclOperation::Read,
131            4 => AclOperation::Write,
132            5 => AclOperation::Create,
133            6 => AclOperation::Delete,
134            7 => AclOperation::Alter,
135            8 => AclOperation::Describe,
136            9 => AclOperation::ClusterAction,
137            10 => AclOperation::DescribeConfigs,
138            11 => AclOperation::AlterConfigs,
139            12 => AclOperation::IdempotentWrite,
140            13 => AclOperation::CreateTokens,
141            14 => AclOperation::DescribeTokens,
142            15 => AclOperation::TwoPhaseCommit,
143            _ => AclOperation::Unknown,
144        }
145    }
146
147    /// Parse the given string as an ACL operation.
148    ///
149    /// # Arguments
150    ///
151    /// - `str` - The string to parse.
152    ///
153    /// # Returns
154    ///
155    /// The `AclOperation`, or `UNKNOWN` if the string could not be matched.
156    #[doc(alias = "org.apache.kafka.common.acl.AclOperation#fromString")]
157    pub fn from_string(str: &str) -> AclOperation {
158        match str.to_uppercase().as_str() {
159            "UNKNOWN" => AclOperation::Unknown,
160            "ANY" => AclOperation::Any,
161            "ALL" => AclOperation::All,
162            "READ" => AclOperation::Read,
163            "WRITE" => AclOperation::Write,
164            "CREATE" => AclOperation::Create,
165            "DELETE" => AclOperation::Delete,
166            "ALTER" => AclOperation::Alter,
167            "DESCRIBE" => AclOperation::Describe,
168            "CLUSTER_ACTION" => AclOperation::ClusterAction,
169            "DESCRIBE_CONFIGS" => AclOperation::DescribeConfigs,
170            "ALTER_CONFIGS" => AclOperation::AlterConfigs,
171            "IDEMPOTENT_WRITE" => AclOperation::IdempotentWrite,
172            "CREATE_TOKENS" => AclOperation::CreateTokens,
173            "DESCRIBE_TOKENS" => AclOperation::DescribeTokens,
174            "TWO_PHASE_COMMIT" => AclOperation::TwoPhaseCommit,
175            _ => AclOperation::Unknown,
176        }
177    }
178
179    /// Returns true if this operation is [`AclOperation::Unknown`].
180    #[doc(alias = "org.apache.kafka.common.acl.AclOperation#isUnknown")]
181    pub fn is_unknown(&self) -> bool {
182        *self == AclOperation::Unknown
183    }
184}
185
186impl std::fmt::Display for AclOperation {
187    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188        let name = match self {
189            AclOperation::Unknown => "UNKNOWN",
190            AclOperation::Any => "ANY",
191            AclOperation::All => "ALL",
192            AclOperation::Read => "READ",
193            AclOperation::Write => "WRITE",
194            AclOperation::Create => "CREATE",
195            AclOperation::Delete => "DELETE",
196            AclOperation::Alter => "ALTER",
197            AclOperation::Describe => "DESCRIBE",
198            AclOperation::ClusterAction => "CLUSTER_ACTION",
199            AclOperation::DescribeConfigs => "DESCRIBE_CONFIGS",
200            AclOperation::AlterConfigs => "ALTER_CONFIGS",
201            AclOperation::IdempotentWrite => "IDEMPOTENT_WRITE",
202            AclOperation::CreateTokens => "CREATE_TOKENS",
203            AclOperation::DescribeTokens => "DESCRIBE_TOKENS",
204            AclOperation::TwoPhaseCommit => "TWO_PHASE_COMMIT",
205        };
206        write!(f, "{name}")
207    }
208}
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213
214    // Mirrors `AclOperationTest.AclOperationTestInfo` in the Java client.
215    struct Info {
216        operation: AclOperation,
217        code: i8,
218        name: &'static str,
219        unknown: bool,
220    }
221
222    // Mirrors `AclOperationTest.INFOS` in the Java client, in declaration order.
223    const INFOS: [Info; 16] = [
224        Info { operation: AclOperation::Unknown, code: 0, name: "unknown", unknown: true },
225        Info { operation: AclOperation::Any, code: 1, name: "any", unknown: false },
226        Info { operation: AclOperation::All, code: 2, name: "all", unknown: false },
227        Info { operation: AclOperation::Read, code: 3, name: "read", unknown: false },
228        Info { operation: AclOperation::Write, code: 4, name: "write", unknown: false },
229        Info { operation: AclOperation::Create, code: 5, name: "create", unknown: false },
230        Info { operation: AclOperation::Delete, code: 6, name: "delete", unknown: false },
231        Info { operation: AclOperation::Alter, code: 7, name: "alter", unknown: false },
232        Info { operation: AclOperation::Describe, code: 8, name: "describe", unknown: false },
233        Info {
234            operation: AclOperation::ClusterAction,
235            code: 9,
236            name: "cluster_action",
237            unknown: false,
238        },
239        Info {
240            operation: AclOperation::DescribeConfigs,
241            code: 10,
242            name: "describe_configs",
243            unknown: false,
244        },
245        Info {
246            operation: AclOperation::AlterConfigs,
247            code: 11,
248            name: "alter_configs",
249            unknown: false,
250        },
251        Info {
252            operation: AclOperation::IdempotentWrite,
253            code: 12,
254            name: "idempotent_write",
255            unknown: false,
256        },
257        Info {
258            operation: AclOperation::CreateTokens,
259            code: 13,
260            name: "create_tokens",
261            unknown: false,
262        },
263        Info {
264            operation: AclOperation::DescribeTokens,
265            code: 14,
266            name: "describe_tokens",
267            unknown: false,
268        },
269        Info {
270            operation: AclOperation::TwoPhaseCommit,
271            code: 15,
272            name: "two_phase_commit",
273            unknown: false,
274        },
275    ];
276
277    /// Mirrors `AclOperationTest.testIsUnknown`.
278    #[test]
279    #[doc(alias = "org.apache.kafka.common.acl.AclOperationTest#testIsUnknown")]
280    fn test_is_unknown() {
281        for info in &INFOS {
282            assert_eq!(
283                info.unknown,
284                info.operation.is_unknown(),
285                "{} was supposed to have unknown == {}",
286                info.operation,
287                info.unknown
288            );
289        }
290    }
291
292    /// Mirrors `AclOperationTest.testCode`.
293    #[test]
294    #[doc(alias = "org.apache.kafka.common.acl.AclOperationTest#testCode")]
295    fn test_code() {
296        assert_eq!(AclOperation::values().len(), INFOS.len());
297        for info in &INFOS {
298            assert_eq!(
299                info.code,
300                info.operation.code(),
301                "{} was supposed to have code == {}",
302                info.operation,
303                info.code
304            );
305            assert_eq!(
306                info.operation,
307                AclOperation::from_code(info.code),
308                "AclOperation::from_code({}) was supposed to be {}",
309                info.code,
310                info.operation
311            );
312        }
313        assert_eq!(AclOperation::Unknown, AclOperation::from_code(120));
314    }
315
316    /// Mirrors `AclOperationTest.testName`.
317    #[test]
318    #[doc(alias = "org.apache.kafka.common.acl.AclOperationTest#testName")]
319    fn test_name() {
320        for info in &INFOS {
321            assert_eq!(
322                info.operation,
323                AclOperation::from_string(info.name),
324                "AclOperation::from_string({}) was supposed to be {}",
325                info.name,
326                info.operation
327            );
328        }
329        assert_eq!(AclOperation::Unknown, AclOperation::from_string("something"));
330    }
331
332    /// Mirrors `AclOperationTest.testExhaustive`.
333    #[test]
334    #[doc(alias = "org.apache.kafka.common.acl.AclOperationTest#testExhaustive")]
335    fn test_exhaustive() {
336        assert_eq!(INFOS.len(), AclOperation::values().len());
337        for (i, info) in INFOS.iter().enumerate() {
338            assert_eq!(info.operation, AclOperation::values()[i]);
339        }
340    }
341}