1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 array::{array_len_prefix_len, get_array_len, put_array_len},
9 fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64},
10 string_bytes::{
11 bytes_len, compact_bytes_len, compact_string_len, get_bytes_owned,
12 get_compact_bytes_owned, get_compact_string_owned, get_string_owned, put_bytes,
13 put_compact_bytes, put_compact_string, put_string, string_len,
14 },
15 },
16 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
17};
18pub const API_KEY: i16 = 41;
19pub const MIN_VERSION: i16 = 1;
20pub const MAX_VERSION: i16 = 3;
21pub const FLEXIBLE_MIN: i16 = 2;
22#[inline]
23#[must_use]
24pub fn is_flexible(version: i16) -> bool {
25 version >= FLEXIBLE_MIN
26}
27#[derive(Debug, Clone, PartialEq, Eq, Default)]
28pub struct DescribeDelegationTokenResponse {
29 pub error_code: i16,
30 pub tokens: Vec<DescribedDelegationToken>,
31 pub throttle_time_ms: i32,
32 pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Encode for DescribeDelegationTokenResponse {
35 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37 return Err(ProtocolError::UnsupportedVersion {
38 api_key: API_KEY,
39 version,
40 });
41 }
42 let flex = is_flexible(version);
43 if version >= 0 {
44 put_i16(buf, self.error_code);
45 }
46 if version >= 0 {
47 {
48 put_array_len(buf, (self.tokens).len(), flex)?;
49 for it in &self.tokens {
50 it.encode(buf, version)?;
51 }
52 }
53 }
54 if version >= 0 {
55 put_i32(buf, self.throttle_time_ms);
56 }
57 if flex {
58 let tagged = WriteTaggedFields::new();
59 tagged.write(buf, &self.unknown_tagged_fields)?;
60 }
61 Ok(())
62 }
63 fn encoded_len(&self, version: i16) -> usize {
64 let flex = is_flexible(version);
65 let mut n: usize = 0;
66 if version >= 0 {
67 n += 2;
68 }
69 if version >= 0 {
70 n += {
71 let prefix = array_len_prefix_len((self.tokens).len(), flex);
72 let body: usize = (self.tokens).iter().map(|it| it.encoded_len(version)).sum();
73 prefix + body
74 };
75 }
76 if version >= 0 {
77 n += 4;
78 }
79 if flex {
80 let known_pairs: Vec<(u32, usize)> = Vec::new();
81 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
82 }
83 n
84 }
85}
86impl Decode<'_> for DescribeDelegationTokenResponse {
87 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
88 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
89 return Err(ProtocolError::UnsupportedVersion {
90 api_key: API_KEY,
91 version,
92 });
93 }
94 let flex = is_flexible(version);
95 let mut out = Self::default();
96 if version >= 0 {
97 out.error_code = get_i16(buf)?;
98 }
99 if version >= 0 {
100 out.tokens = {
101 let n = get_array_len(buf, flex)?;
102 let mut v = Vec::with_capacity(n);
103 for _ in 0..n {
104 v.push(DescribedDelegationToken::decode(buf, version)?);
105 }
106 v
107 };
108 }
109 if version >= 0 {
110 out.throttle_time_ms = get_i32(buf)?;
111 }
112 if flex {
113 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
114 }
115 Ok(out)
116 }
117}
118#[cfg(test)]
119impl DescribeDelegationTokenResponse {
120 #[must_use]
121 pub fn populated(version: i16) -> Self {
122 let mut m = Self::default();
123 if version >= 0 {
124 m.error_code = 1i16;
125 }
126 if version >= 0 {
127 m.tokens = vec![DescribedDelegationToken::populated(version)];
128 }
129 if version >= 0 {
130 m.throttle_time_ms = 1i32;
131 }
132 m
133 }
134}
135#[derive(Debug, Clone, PartialEq, Eq, Default)]
136pub struct DescribedDelegationToken {
137 pub principal_type: String,
138 pub principal_name: String,
139 pub token_requester_principal_type: String,
140 pub token_requester_principal_name: String,
141 pub issue_timestamp: i64,
142 pub expiry_timestamp: i64,
143 pub max_timestamp: i64,
144 pub token_id: String,
145 pub hmac: ::bytes::Bytes,
146 pub renewers: Vec<DescribedDelegationTokenRenewer>,
147 pub unknown_tagged_fields: UnknownTaggedFields,
148}
149impl DescribedDelegationToken {
150 fn encode_field_0<B: BufMut>(
151 &self,
152 buf: &mut B,
153 version: i16,
154 flex: bool,
155 ) -> Result<(), ProtocolError> {
156 if version >= 0 {
157 if flex {
158 put_compact_string(buf, &self.principal_type)?;
159 } else {
160 put_string(buf, &self.principal_type)?;
161 }
162 }
163 Ok(())
164 }
165 fn encode_field_1<B: BufMut>(
166 &self,
167 buf: &mut B,
168 version: i16,
169 flex: bool,
170 ) -> Result<(), ProtocolError> {
171 if version >= 0 {
172 if flex {
173 put_compact_string(buf, &self.principal_name)?;
174 } else {
175 put_string(buf, &self.principal_name)?;
176 }
177 }
178 Ok(())
179 }
180 fn encode_field_2<B: BufMut>(
181 &self,
182 buf: &mut B,
183 version: i16,
184 flex: bool,
185 ) -> Result<(), ProtocolError> {
186 if version >= 3 {
187 if flex {
188 put_compact_string(buf, &self.token_requester_principal_type)?;
189 } else {
190 put_string(buf, &self.token_requester_principal_type)?;
191 }
192 }
193 Ok(())
194 }
195 fn encode_field_3<B: BufMut>(
196 &self,
197 buf: &mut B,
198 version: i16,
199 flex: bool,
200 ) -> Result<(), ProtocolError> {
201 if version >= 3 {
202 if flex {
203 put_compact_string(buf, &self.token_requester_principal_name)?;
204 } else {
205 put_string(buf, &self.token_requester_principal_name)?;
206 }
207 }
208 Ok(())
209 }
210 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
211 if version >= 0 {
212 put_i64(buf, self.issue_timestamp);
213 }
214 }
215 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
216 if version >= 0 {
217 put_i64(buf, self.expiry_timestamp);
218 }
219 }
220 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
221 if version >= 0 {
222 put_i64(buf, self.max_timestamp);
223 }
224 }
225 fn encode_field_7<B: BufMut>(
226 &self,
227 buf: &mut B,
228 version: i16,
229 flex: bool,
230 ) -> Result<(), ProtocolError> {
231 if version >= 0 {
232 if flex {
233 put_compact_string(buf, &self.token_id)?;
234 } else {
235 put_string(buf, &self.token_id)?;
236 }
237 }
238 Ok(())
239 }
240 fn encode_field_8<B: BufMut>(
241 &self,
242 buf: &mut B,
243 version: i16,
244 flex: bool,
245 ) -> Result<(), ProtocolError> {
246 if version >= 0 {
247 if flex {
248 put_compact_bytes(buf, &self.hmac)?;
249 } else {
250 put_bytes(buf, &self.hmac)?;
251 }
252 }
253 Ok(())
254 }
255 fn encode_field_9<B: BufMut>(
256 &self,
257 buf: &mut B,
258 version: i16,
259 flex: bool,
260 ) -> Result<(), ProtocolError> {
261 if version >= 0 {
262 {
263 put_array_len(buf, (self.renewers).len(), flex)?;
264 for it in &self.renewers {
265 it.encode(buf, version)?;
266 }
267 }
268 }
269 Ok(())
270 }
271 fn encode_tagged_fields<B: BufMut>(
272 &self,
273 buf: &mut B,
274 _version: i16,
275 flex: bool,
276 ) -> Result<(), ProtocolError> {
277 if flex {
278 let tagged = WriteTaggedFields::new();
279 tagged.write(buf, &self.unknown_tagged_fields)?;
280 }
281 Ok(())
282 }
283 fn decode_field_0<B: Buf>(
284 out: &mut Self,
285 buf: &mut B,
286 version: i16,
287 flex: bool,
288 ) -> Result<(), ProtocolError> {
289 if version >= 0 {
290 out.principal_type = if flex {
291 get_compact_string_owned(buf)?
292 } else {
293 get_string_owned(buf)?
294 };
295 }
296 Ok(())
297 }
298 fn decode_field_1<B: Buf>(
299 out: &mut Self,
300 buf: &mut B,
301 version: i16,
302 flex: bool,
303 ) -> Result<(), ProtocolError> {
304 if version >= 0 {
305 out.principal_name = if flex {
306 get_compact_string_owned(buf)?
307 } else {
308 get_string_owned(buf)?
309 };
310 }
311 Ok(())
312 }
313 fn decode_field_2<B: Buf>(
314 out: &mut Self,
315 buf: &mut B,
316 version: i16,
317 flex: bool,
318 ) -> Result<(), ProtocolError> {
319 if version >= 3 {
320 out.token_requester_principal_type = if flex {
321 get_compact_string_owned(buf)?
322 } else {
323 get_string_owned(buf)?
324 };
325 }
326 Ok(())
327 }
328 fn decode_field_3<B: Buf>(
329 out: &mut Self,
330 buf: &mut B,
331 version: i16,
332 flex: bool,
333 ) -> Result<(), ProtocolError> {
334 if version >= 3 {
335 out.token_requester_principal_name = if flex {
336 get_compact_string_owned(buf)?
337 } else {
338 get_string_owned(buf)?
339 };
340 }
341 Ok(())
342 }
343 fn decode_field_4<B: Buf>(
344 out: &mut Self,
345 buf: &mut B,
346 version: i16,
347 _flex: bool,
348 ) -> Result<(), ProtocolError> {
349 if version >= 0 {
350 out.issue_timestamp = get_i64(buf)?;
351 }
352 Ok(())
353 }
354 fn decode_field_5<B: Buf>(
355 out: &mut Self,
356 buf: &mut B,
357 version: i16,
358 _flex: bool,
359 ) -> Result<(), ProtocolError> {
360 if version >= 0 {
361 out.expiry_timestamp = get_i64(buf)?;
362 }
363 Ok(())
364 }
365 fn decode_field_6<B: Buf>(
366 out: &mut Self,
367 buf: &mut B,
368 version: i16,
369 _flex: bool,
370 ) -> Result<(), ProtocolError> {
371 if version >= 0 {
372 out.max_timestamp = get_i64(buf)?;
373 }
374 Ok(())
375 }
376 fn decode_field_7<B: Buf>(
377 out: &mut Self,
378 buf: &mut B,
379 version: i16,
380 flex: bool,
381 ) -> Result<(), ProtocolError> {
382 if version >= 0 {
383 out.token_id = if flex {
384 get_compact_string_owned(buf)?
385 } else {
386 get_string_owned(buf)?
387 };
388 }
389 Ok(())
390 }
391 fn decode_field_8<B: Buf>(
392 out: &mut Self,
393 buf: &mut B,
394 version: i16,
395 flex: bool,
396 ) -> Result<(), ProtocolError> {
397 if version >= 0 {
398 out.hmac = if flex {
399 get_compact_bytes_owned(buf)?
400 } else {
401 get_bytes_owned(buf)?
402 };
403 }
404 Ok(())
405 }
406 fn decode_field_9<B: Buf>(
407 out: &mut Self,
408 buf: &mut B,
409 version: i16,
410 flex: bool,
411 ) -> Result<(), ProtocolError> {
412 if version >= 0 {
413 out.renewers = {
414 let n = get_array_len(buf, flex)?;
415 let mut v = Vec::with_capacity(n);
416 for _ in 0..n {
417 v.push(DescribedDelegationTokenRenewer::decode(buf, version)?);
418 }
419 v
420 };
421 }
422 Ok(())
423 }
424 fn decode_tagged_fields<B: Buf>(
425 out: &mut Self,
426 buf: &mut B,
427 _version: i16,
428 flex: bool,
429 ) -> Result<(), ProtocolError> {
430 if flex {
431 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
432 }
433 Ok(())
434 }
435}
436impl Encode for DescribedDelegationToken {
437 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
438 let flex = version >= 2;
439 self.encode_field_0(buf, version, flex)?;
440 self.encode_field_1(buf, version, flex)?;
441 self.encode_field_2(buf, version, flex)?;
442 self.encode_field_3(buf, version, flex)?;
443 self.encode_field_4(buf, version, flex);
444 self.encode_field_5(buf, version, flex);
445 self.encode_field_6(buf, version, flex);
446 self.encode_field_7(buf, version, flex)?;
447 self.encode_field_8(buf, version, flex)?;
448 self.encode_field_9(buf, version, flex)?;
449 self.encode_tagged_fields(buf, version, flex)?;
450 Ok(())
451 }
452 fn encoded_len(&self, version: i16) -> usize {
453 let flex = version >= 2;
454 let mut n: usize = 0;
455 if version >= 0 {
456 n += if flex {
457 compact_string_len(&self.principal_type)
458 } else {
459 string_len(&self.principal_type)
460 };
461 }
462 if version >= 0 {
463 n += if flex {
464 compact_string_len(&self.principal_name)
465 } else {
466 string_len(&self.principal_name)
467 };
468 }
469 if version >= 3 {
470 n += if flex {
471 compact_string_len(&self.token_requester_principal_type)
472 } else {
473 string_len(&self.token_requester_principal_type)
474 };
475 }
476 if version >= 3 {
477 n += if flex {
478 compact_string_len(&self.token_requester_principal_name)
479 } else {
480 string_len(&self.token_requester_principal_name)
481 };
482 }
483 if version >= 0 {
484 n += 8;
485 }
486 if version >= 0 {
487 n += 8;
488 }
489 if version >= 0 {
490 n += 8;
491 }
492 if version >= 0 {
493 n += if flex {
494 compact_string_len(&self.token_id)
495 } else {
496 string_len(&self.token_id)
497 };
498 }
499 if version >= 0 {
500 n += if flex {
501 compact_bytes_len(&self.hmac)
502 } else {
503 bytes_len(&self.hmac)
504 };
505 }
506 if version >= 0 {
507 n += {
508 let prefix = array_len_prefix_len((self.renewers).len(), flex);
509 let body: usize = (self.renewers)
510 .iter()
511 .map(|it| it.encoded_len(version))
512 .sum();
513 prefix + body
514 };
515 }
516 if flex {
517 let known_pairs: Vec<(u32, usize)> = Vec::new();
518 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
519 }
520 n
521 }
522}
523impl Decode<'_> for DescribedDelegationToken {
524 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
525 let flex = version >= 2;
526 let mut out = Self::default();
527 Self::decode_field_0(&mut out, buf, version, flex)?;
528 Self::decode_field_1(&mut out, buf, version, flex)?;
529 Self::decode_field_2(&mut out, buf, version, flex)?;
530 Self::decode_field_3(&mut out, buf, version, flex)?;
531 Self::decode_field_4(&mut out, buf, version, flex)?;
532 Self::decode_field_5(&mut out, buf, version, flex)?;
533 Self::decode_field_6(&mut out, buf, version, flex)?;
534 Self::decode_field_7(&mut out, buf, version, flex)?;
535 Self::decode_field_8(&mut out, buf, version, flex)?;
536 Self::decode_field_9(&mut out, buf, version, flex)?;
537 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
538 Ok(out)
539 }
540}
541#[cfg(test)]
542impl DescribedDelegationToken {
543 #[must_use]
544 pub fn populated(version: i16) -> Self {
545 let mut m = Self::default();
546 if version >= 0 {
547 m.principal_type = "x".to_owned();
548 }
549 if version >= 0 {
550 m.principal_name = "x".to_owned();
551 }
552 if version >= 3 {
553 m.token_requester_principal_type = "x".to_owned();
554 }
555 if version >= 3 {
556 m.token_requester_principal_name = "x".to_owned();
557 }
558 if version >= 0 {
559 m.issue_timestamp = 1i64;
560 }
561 if version >= 0 {
562 m.expiry_timestamp = 1i64;
563 }
564 if version >= 0 {
565 m.max_timestamp = 1i64;
566 }
567 if version >= 0 {
568 m.token_id = "x".to_owned();
569 }
570 if version >= 0 {
571 m.hmac = ::bytes::Bytes::from_static(b"x");
572 }
573 if version >= 0 {
574 m.renewers = vec![DescribedDelegationTokenRenewer::populated(version)];
575 }
576 m
577 }
578}
579#[derive(Debug, Clone, PartialEq, Eq, Default)]
580pub struct DescribedDelegationTokenRenewer {
581 pub principal_type: String,
582 pub principal_name: String,
583 pub unknown_tagged_fields: UnknownTaggedFields,
584}
585impl Encode for DescribedDelegationTokenRenewer {
586 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
587 let flex = version >= 2;
588 if version >= 0 {
589 if flex {
590 put_compact_string(buf, &self.principal_type)?;
591 } else {
592 put_string(buf, &self.principal_type)?;
593 }
594 }
595 if version >= 0 {
596 if flex {
597 put_compact_string(buf, &self.principal_name)?;
598 } else {
599 put_string(buf, &self.principal_name)?;
600 }
601 }
602 if flex {
603 let tagged = WriteTaggedFields::new();
604 tagged.write(buf, &self.unknown_tagged_fields)?;
605 }
606 Ok(())
607 }
608 fn encoded_len(&self, version: i16) -> usize {
609 let flex = version >= 2;
610 let mut n: usize = 0;
611 if version >= 0 {
612 n += if flex {
613 compact_string_len(&self.principal_type)
614 } else {
615 string_len(&self.principal_type)
616 };
617 }
618 if version >= 0 {
619 n += if flex {
620 compact_string_len(&self.principal_name)
621 } else {
622 string_len(&self.principal_name)
623 };
624 }
625 if flex {
626 let known_pairs: Vec<(u32, usize)> = Vec::new();
627 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
628 }
629 n
630 }
631}
632impl Decode<'_> for DescribedDelegationTokenRenewer {
633 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
634 let flex = version >= 2;
635 let mut out = Self::default();
636 if version >= 0 {
637 out.principal_type = if flex {
638 get_compact_string_owned(buf)?
639 } else {
640 get_string_owned(buf)?
641 };
642 }
643 if version >= 0 {
644 out.principal_name = if flex {
645 get_compact_string_owned(buf)?
646 } else {
647 get_string_owned(buf)?
648 };
649 }
650 if flex {
651 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
652 }
653 Ok(out)
654 }
655}
656#[cfg(test)]
657impl DescribedDelegationTokenRenewer {
658 #[must_use]
659 pub fn populated(version: i16) -> Self {
660 let mut m = Self::default();
661 if version >= 0 {
662 m.principal_type = "x".to_owned();
663 }
664 if version >= 0 {
665 m.principal_name = "x".to_owned();
666 }
667 m
668 }
669}
670#[must_use]
673#[allow(unused_comparisons)]
674pub fn default_json(_version: i16) -> ::serde_json::Value {
675 let mut obj = ::serde_json::Map::new();
676 obj.insert("errorCode".to_owned(), ::serde_json::json!(0));
677 obj.insert("tokens".to_owned(), ::serde_json::Value::Array(vec![]));
678 obj.insert("throttleTimeMs".to_owned(), ::serde_json::json!(0));
679 ::serde_json::Value::Object(obj)
680}