Skip to main content

confluent_kafka/admin/options/
create_delegation_token_options.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//! Options for `Admin::create_delegation_token`.
16
17use crate::common::security::auth::KafkaPrincipal;
18
19/// Options for `Admin::create_delegation_token`.
20#[derive(Clone, Debug)]
21#[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions")]
22pub struct CreateDelegationTokenOptions {
23    max_lifetime_ms: i64,
24    renewers: Vec<KafkaPrincipal>,
25    owner: Option<KafkaPrincipal>,
26    timeout_ms: Option<i32>,
27}
28
29impl Default for CreateDelegationTokenOptions {
30    fn default() -> Self {
31        Self { max_lifetime_ms: -1, renewers: Vec::new(), owner: None, timeout_ms: None }
32    }
33}
34
35impl CreateDelegationTokenOptions {
36    /// Creates default options (default API timeout, server-default lifetime,
37    /// no renewers, owner defaults to the request principal).
38    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#CreateDelegationTokenOptions")]
39    pub fn new() -> Self {
40        Self::default()
41    }
42
43    /// Sets the principals allowed to renew the token.
44    ///
45    /// # Arguments
46    ///
47    /// - `renewers` - the principals allowed to renew the token.
48    ///
49    /// # Returns
50    ///
51    /// This `CreateDelegationTokenOptions` instance.
52    #[must_use]
53    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#renewers")]
54    pub fn set_renewers(mut self, renewers: Vec<KafkaPrincipal>) -> Self {
55        self.renewers = renewers;
56        self
57    }
58
59    /// Returns the principals allowed to renew the token.
60    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#renewers")]
61    pub fn renewers(&self) -> &[KafkaPrincipal] {
62        &self.renewers
63    }
64
65    /// Sets the owner of the token, or `None` for none, in which case the
66    /// broker makes the request principal the owner.
67    ///
68    /// # Arguments
69    ///
70    /// - `owner` - the owner of the token, or `None` to clear an owner set
71    ///   earlier, so the request principal owns the token.
72    ///
73    /// # Returns
74    ///
75    /// This `CreateDelegationTokenOptions` instance.
76    #[must_use]
77    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#owner")]
78    pub fn set_owner(mut self, owner: Option<KafkaPrincipal>) -> Self {
79        self.owner = owner;
80        self
81    }
82
83    /// Returns the owner of the token, or `None` if not set.
84    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#owner")]
85    pub fn owner(&self) -> Option<&KafkaPrincipal> {
86        self.owner.as_ref()
87    }
88
89    /// Sets the maximum lifetime of the token in milliseconds, or `-1` to use the server-side
90    /// default.
91    ///
92    /// # Arguments
93    ///
94    /// - `max_lifetime_ms` - the maximum lifetime in milliseconds.
95    ///
96    /// # Returns
97    ///
98    /// This `CreateDelegationTokenOptions` instance.
99    #[must_use]
100    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#maxLifetimeMs")]
101    pub fn set_max_lifetime_ms(mut self, max_lifetime_ms: i64) -> Self {
102        self.max_lifetime_ms = max_lifetime_ms;
103        self
104    }
105
106    /// Returns the maximum lifetime of the token in milliseconds.
107    #[doc(alias = "org.apache.kafka.clients.admin.CreateDelegationTokenOptions#maxLifetimeMs")]
108    pub fn max_lifetime_ms(&self) -> i64 {
109        self.max_lifetime_ms
110    }
111
112    /// Set the timeout in milliseconds for this operation, or `None` if the default
113    /// API timeout for the `AdminClient` should be used.
114    ///
115    /// # Arguments
116    ///
117    /// - `timeout_ms` - the timeout in milliseconds, or `None` for the default API timeout.
118    ///
119    /// # Returns
120    ///
121    /// This `CreateDelegationTokenOptions` instance.
122    #[must_use]
123    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
124    pub fn set_timeout_ms(mut self, timeout_ms: Option<i32>) -> Self {
125        self.timeout_ms = timeout_ms;
126        self
127    }
128
129    /// Returns the timeout in milliseconds for this operation, or `None` if the
130    /// default API timeout for the `AdminClient` should be used.
131    #[doc(alias = "org.apache.kafka.clients.admin.AbstractOptions#timeoutMs")]
132    pub fn timeout_ms(&self) -> Option<i32> {
133        self.timeout_ms
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140
141    #[test]
142    fn defaults() {
143        let options = CreateDelegationTokenOptions::new();
144        assert_eq!(options.max_lifetime_ms(), -1);
145        assert!(options.renewers().is_empty());
146        assert!(options.owner().is_none());
147        assert_eq!(options.timeout_ms(), None);
148    }
149
150    #[test]
151    fn setters() {
152        let owner = KafkaPrincipal::new(KafkaPrincipal::USER_TYPE, "alice");
153        let renewer = KafkaPrincipal::new(KafkaPrincipal::USER_TYPE, "bob");
154        let options = CreateDelegationTokenOptions::new()
155            .set_owner(Some(owner.clone()))
156            .set_renewers(vec![renewer.clone()])
157            .set_max_lifetime_ms(1000)
158            .set_timeout_ms(Some(5000));
159        assert_eq!(options.owner(), Some(&owner));
160        assert_eq!(options.renewers(), &[renewer]);
161        assert_eq!(options.max_lifetime_ms(), 1000);
162        assert_eq!(options.timeout_ms(), Some(5000));
163    }
164
165    /// New test, no Java original: Java's `owner(null)` stores `null`, so it
166    /// clears an owner set earlier and `owner()` is `Optional.empty()` again
167    /// (`CreateDelegationTokenOptions#owner(KafkaPrincipal)`).
168    #[test]
169    fn set_owner_none_clears_the_owner() {
170        let owner = KafkaPrincipal::new(KafkaPrincipal::USER_TYPE, "alice");
171        let options = CreateDelegationTokenOptions::new().set_owner(Some(owner)).set_owner(None);
172        assert!(options.owner().is_none());
173    }
174}