krabka-client-admin 0.6.0

Operator-side admin client for Krabka clusters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//! KIP-48 delegation tokens, as Kafka's `Admin.createDelegationToken`,
//! `renewDelegationToken`, `expireDelegationToken`, and
//! `describeDelegationToken`.
//!
//! Each call takes an options struct that mirrors Kafka's
//! `CreateDelegationTokenOptions`, `RenewDelegationTokenOptions`,
//! `ExpireDelegationTokenOptions`, or `DescribeDelegationTokenOptions`, and
//! sends one request on the admin connection, as `KafkaAdminClient` sends it
//! to the least-loaded node. A non-zero top-level error code becomes
//! [`AdminError::Broker`], as Kafka completes the call with
//! `response.error().exception()`.

use bytes::Bytes;
use krabka_protocol::owned::{
    create_delegation_token_request::{CreatableRenewers, CreateDelegationTokenRequest},
    create_delegation_token_response::CreateDelegationTokenResponse,
    describe_delegation_token_request::{
        DescribeDelegationTokenOwner, DescribeDelegationTokenRequest,
    },
    describe_delegation_token_response::{
        DescribeDelegationTokenResponse, DescribedDelegationToken,
    },
    expire_delegation_token_request::ExpireDelegationTokenRequest,
    renew_delegation_token_request::RenewDelegationTokenRequest,
};
use krabka_security::KafkaPrincipal;
use krabka_units::{Time, convert::wire::opt_time_to_millis_i64};

use crate::{AdminClient, AdminError, kafka_error_name};

/// The first `CreateDelegationToken` version with the owner fields
/// (KIP-373).
const CREATE_WITH_OWNER_MIN_VERSION: i16 = 3;

/// What [`AdminClient::create_delegation_token`] asks for, as Kafka's
/// `CreateDelegationTokenOptions`. The default mints a token that the caller
/// owns, with no renewers and the broker's maximum lifetime.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CreateDelegationTokenOptions {
    /// The owner of the token, of any principal type. `None` makes the
    /// caller the owner. Another owner needs the `CreateTokens` operation on
    /// the owner's `User` resource, and `CreateDelegationToken` v3 (KIP-373).
    pub owner: Option<KafkaPrincipal>,
    /// The principals that may renew the token.
    pub renewers: Vec<KafkaPrincipal>,
    /// The maximum lifetime of the token. `None` sends Kafka's `-1`, and the
    /// broker then uses `delegation.token.max.lifetime.ms`. The broker also
    /// uses that ceiling for a lifetime of zero or less, and caps a longer
    /// one at it.
    pub max_lifetime: Option<Time>,
}

/// What [`AdminClient::renew_delegation_token`] asks for, as Kafka's
/// `RenewDelegationTokenOptions`.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct RenewDelegationTokenOptions {
    /// How far past now to move the expiry. `None` sends Kafka's `-1`, and
    /// the broker then uses `delegation.token.expiry.time.ms`. The broker
    /// also uses that default for a period of zero or less, caps a longer
    /// one at it, and never moves the expiry past the token's maximum
    /// timestamp.
    pub renew_time_period: Option<Time>,
}

/// What [`AdminClient::expire_delegation_token`] asks for, as Kafka's
/// `ExpireDelegationTokenOptions`.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ExpireDelegationTokenOptions {
    /// How far past now the token expires. `None` sends Kafka's `-1`, which
    /// expires the token now, as does any negative period. A period of zero
    /// or more sets the expiry to that time, capped at the token's maximum
    /// timestamp.
    pub expiry_time_period: Option<Time>,
}

/// What [`AdminClient::describe_delegation_token`] asks for, as Kafka's
/// `DescribeDelegationTokenOptions`. The default describes every token that
/// the caller may describe.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DescribeDelegationTokenOptions {
    /// The owners whose tokens to describe. `None` sends a null owner list,
    /// and the broker returns every token that the caller owns or may
    /// describe. An empty list sends an empty owner list, and the broker
    /// returns no tokens.
    pub owners: Option<Vec<KafkaPrincipal>>,
}

/// One delegation token, as Kafka's `DelegationToken` with its
/// `TokenInformation`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DelegationToken {
    /// The token ID, which a client presents as its SCRAM user name.
    pub token_id: String,
    /// The HMAC of the token, which a client presents as its SCRAM password.
    pub hmac: Vec<u8>,
    /// The principal that owns the token.
    pub owner: KafkaPrincipal,
    /// The principal that asked for the token. It differs from
    /// [`owner`](Self::owner) when a caller created a token for another
    /// owner.
    pub token_requester: KafkaPrincipal,
    /// The principals that may renew the token.
    pub renewers: Vec<KafkaPrincipal>,
    /// When the broker issued the token, in epoch milliseconds.
    pub issue_timestamp_ms: i64,
    /// When the token expires unless someone renews it, in epoch
    /// milliseconds.
    pub expiry_timestamp_ms: i64,
    /// The latest expiry that a renewal can set, in epoch milliseconds.
    pub max_timestamp_ms: i64,
}

impl AdminClient {
    /// Creates a delegation token, as Kafka's `createDelegationToken`
    /// operation does.
    ///
    /// The broker does not return the renewers, so the result has the
    /// renewers of `options`, as Kafka's result does. With an
    /// [`owner`](CreateDelegationTokenOptions::owner) the client negotiates
    /// `CreateDelegationToken` v3 or higher, as Kafka's request refuses to
    /// write the owner fields below v3.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Broker`] for a non-zero error code, such as
    /// `DELEGATION_TOKEN_AUTH_DISABLED` (61),
    /// `DELEGATION_TOKEN_REQUEST_NOT_ALLOWED` (64),
    /// `DELEGATION_TOKEN_AUTHORIZATION_FAILED` (65), or
    /// `INVALID_PRINCIPAL_TYPE` (67). Returns [`AdminError::Transport`] when
    /// the connection fails, or when an owner is set and the broker does not
    /// support v3.
    pub async fn create_delegation_token(
        &self,
        options: &CreateDelegationTokenOptions,
    ) -> Result<DelegationToken, AdminError> {
        let request = create_request(options);
        let response = if options.owner.is_some() {
            self.conn
                .send_at_least(request, CREATE_WITH_OWNER_MIN_VERSION)
                .await?
        } else {
            self.conn.send(request).await?
        };
        created_token(response, &options.renewers)
    }

    /// Renews the delegation token with `hmac`, as Kafka's
    /// `renewDelegationToken` operation does, and returns its new expiry in
    /// epoch milliseconds.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Broker`] for a non-zero error code, such as
    /// `DELEGATION_TOKEN_NOT_FOUND` (62), `DELEGATION_TOKEN_OWNER_MISMATCH`
    /// (63), or `DELEGATION_TOKEN_EXPIRED` (66). Returns
    /// [`AdminError::Transport`] when the connection fails.
    pub async fn renew_delegation_token(
        &self,
        hmac: &[u8],
        options: RenewDelegationTokenOptions,
    ) -> Result<i64, AdminError> {
        let response = self.conn.send(renew_request(hmac, options)).await?;
        check("RenewDelegationToken", response.error_code)?;
        Ok(response.expiry_timestamp_ms)
    }

    /// Expires the delegation token with `hmac`, as Kafka's
    /// `expireDelegationToken` operation does, and returns the expiry that
    /// the broker set, in epoch milliseconds. A token that expires now gets
    /// the broker's clock at the time of the request.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Broker`] for a non-zero error code, such as
    /// `DELEGATION_TOKEN_NOT_FOUND` (62), `DELEGATION_TOKEN_OWNER_MISMATCH`
    /// (63), or `DELEGATION_TOKEN_EXPIRED` (66). Returns
    /// [`AdminError::Transport`] when the connection fails.
    pub async fn expire_delegation_token(
        &self,
        hmac: &[u8],
        options: ExpireDelegationTokenOptions,
    ) -> Result<i64, AdminError> {
        let response = self.conn.send(expire_request(hmac, options)).await?;
        check("ExpireDelegationToken", response.error_code)?;
        Ok(response.expiry_timestamp_ms)
    }

    /// Describes the delegation tokens that `options` selects, as Kafka's
    /// `describeDelegationToken` operation does.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Broker`] for a non-zero error code, such as
    /// `DELEGATION_TOKEN_AUTH_DISABLED` (61) or
    /// `DELEGATION_TOKEN_REQUEST_NOT_ALLOWED` (64). Returns
    /// [`AdminError::Transport`] when the connection fails.
    pub async fn describe_delegation_token(
        &self,
        options: &DescribeDelegationTokenOptions,
    ) -> Result<Vec<DelegationToken>, AdminError> {
        let response = self.conn.send(describe_request(options)).await?;
        described_tokens(response)
    }
}

/// The `CreateDelegationToken` request of `options`, as
/// `KafkaAdminClient.createDelegationToken` builds it. With no owner the
/// owner fields keep their empty-string defaults, as Kafka's
/// `CreateDelegationTokenRequestData` does, and the broker makes the caller
/// the owner.
fn create_request(options: &CreateDelegationTokenOptions) -> CreateDelegationTokenRequest {
    let mut request = CreateDelegationTokenRequest {
        renewers: options
            .renewers
            .iter()
            .map(|renewer| CreatableRenewers {
                principal_type: renewer.principal_type.clone(),
                principal_name: renewer.name.clone(),
                ..Default::default()
            })
            .collect(),
        max_lifetime_ms: opt_time_to_millis_i64(options.max_lifetime),
        ..Default::default()
    };
    if let Some(owner) = &options.owner {
        request.owner_principal_type = Some(owner.principal_type.clone());
        request.owner_principal_name = Some(owner.name.clone());
    }
    request
}

fn renew_request(hmac: &[u8], options: RenewDelegationTokenOptions) -> RenewDelegationTokenRequest {
    RenewDelegationTokenRequest {
        hmac: Bytes::copy_from_slice(hmac),
        renew_period_ms: opt_time_to_millis_i64(options.renew_time_period),
        ..Default::default()
    }
}

fn expire_request(
    hmac: &[u8],
    options: ExpireDelegationTokenOptions,
) -> ExpireDelegationTokenRequest {
    ExpireDelegationTokenRequest {
        hmac: Bytes::copy_from_slice(hmac),
        expiry_time_period_ms: opt_time_to_millis_i64(options.expiry_time_period),
        ..Default::default()
    }
}

/// The `DescribeDelegationToken` request of `options`, as Kafka's
/// `DescribeDelegationTokenRequest.Builder` builds it: a null owner list for
/// no owners, and the listed owners otherwise.
fn describe_request(options: &DescribeDelegationTokenOptions) -> DescribeDelegationTokenRequest {
    DescribeDelegationTokenRequest {
        owners: options.owners.as_ref().map(|owners| {
            owners
                .iter()
                .map(|owner| DescribeDelegationTokenOwner {
                    principal_type: owner.principal_type.clone(),
                    principal_name: owner.name.clone(),
                    ..Default::default()
                })
                .collect()
        }),
        ..Default::default()
    }
}

/// The token of a `CreateDelegationToken` answer, with the renewers that the
/// request named.
fn created_token(
    response: CreateDelegationTokenResponse,
    renewers: &[KafkaPrincipal],
) -> Result<DelegationToken, AdminError> {
    check("CreateDelegationToken", response.error_code)?;
    let owner = principal(response.principal_type, response.principal_name);
    Ok(DelegationToken {
        token_id: response.token_id,
        hmac: response.hmac.to_vec(),
        token_requester: requester(
            &owner,
            response.token_requester_principal_type,
            response.token_requester_principal_name,
        ),
        owner,
        renewers: renewers.to_vec(),
        issue_timestamp_ms: response.issue_timestamp_ms,
        expiry_timestamp_ms: response.expiry_timestamp_ms,
        max_timestamp_ms: response.max_timestamp_ms,
    })
}

fn described_tokens(
    response: DescribeDelegationTokenResponse,
) -> Result<Vec<DelegationToken>, AdminError> {
    check("DescribeDelegationToken", response.error_code)?;
    Ok(response.tokens.into_iter().map(described_token).collect())
}

fn described_token(token: DescribedDelegationToken) -> DelegationToken {
    let owner = principal(token.principal_type, token.principal_name);
    DelegationToken {
        token_id: token.token_id,
        hmac: token.hmac.to_vec(),
        token_requester: requester(
            &owner,
            token.token_requester_principal_type,
            token.token_requester_principal_name,
        ),
        owner,
        renewers: token
            .renewers
            .into_iter()
            .map(|renewer| principal(renewer.principal_type, renewer.principal_name))
            .collect(),
        issue_timestamp_ms: token.issue_timestamp,
        expiry_timestamp_ms: token.expiry_timestamp,
        max_timestamp_ms: token.max_timestamp,
    }
}

/// The requester of a token. The requester fields start at v3; below it they
/// decode empty, and a token could then only be made by its own owner, so the
/// owner is the requester.
fn requester(owner: &KafkaPrincipal, principal_type: String, name: String) -> KafkaPrincipal {
    if principal_type.is_empty() && name.is_empty() {
        owner.clone()
    } else {
        principal(principal_type, name)
    }
}

fn principal(principal_type: String, name: String) -> KafkaPrincipal {
    KafkaPrincipal {
        principal_type,
        name,
    }
}

/// `Ok` for a zero error code, and [`AdminError::Broker`] for any other.
fn check(api: &'static str, code: i16) -> Result<(), AdminError> {
    if code == 0 {
        return Ok(());
    }
    Err(AdminError::Broker {
        api,
        code,
        name: kafka_error_name(code),
        message: None,
    })
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use krabka_client_core::{ClientError, MockBroker};
    use krabka_protocol::{
        Decode, Encode,
        owned::{
            api_versions_request, create_delegation_token_request,
            describe_delegation_token_request,
            describe_delegation_token_response::DescribedDelegationTokenRenewer,
            expire_delegation_token_request,
            expire_delegation_token_response::ExpireDelegationTokenResponse,
            renew_delegation_token_request,
            renew_delegation_token_response::RenewDelegationTokenResponse,
        },
    };

    use super::*;
    use crate::partition_leaders::test_support::{
        api_versions, decode_request, encode_response, fast_admin,
    };

    fn user(name: &str) -> KafkaPrincipal {
        principal("User".to_owned(), name.to_owned())
    }

    fn group(name: &str) -> KafkaPrincipal {
        principal("Group".to_owned(), name.to_owned())
    }

    /// Every delegation-token API up to its latest version.
    const TOKEN_APIS: [(i16, i16, i16); 4] = [
        (create_delegation_token_request::API_KEY, 1, 3),
        (renew_delegation_token_request::API_KEY, 1, 2),
        (expire_delegation_token_request::API_KEY, 1, 2),
        (describe_delegation_token_request::API_KEY, 1, 3),
    ];

    /// A mock broker that advertises `apis`, answers each `api_key` request
    /// with `answer` encoded at the negotiated version, and records the
    /// requests it decodes. Every token API is flexible from v2.
    async fn token_broker<Req, Resp>(
        apis: Vec<(i16, i16, i16)>,
        api_key: i16,
        answer: Resp,
    ) -> (MockBroker, Arc<Mutex<Vec<Req>>>)
    where
        Req: for<'a> Decode<'a> + Send + 'static,
        Resp: Encode + Send + 'static,
    {
        let requests = Arc::new(Mutex::new(Vec::new()));
        let seen = Arc::clone(&requests);
        let broker = MockBroker::start(move |key, version, _, body| match key {
            api_versions_request::API_KEY => Some(api_versions(&apis)),
            key if key == api_key => {
                let flexible = version >= 2;
                seen.lock()
                    .expect("requests lock")
                    .push(decode_request::<Req>(body, version, flexible));
                Some(encode_response(&answer, version, flexible))
            }
            _ => None,
        })
        .await;
        (broker, requests)
    }

    type Outcome<T> = Result<T, (&'static str, i16, &'static str)>;

    fn outcome<T>(result: Result<T, AdminError>) -> Outcome<T> {
        result.map_err(|error| match error {
            AdminError::Broker {
                api,
                code,
                name,
                message: None,
            } => (api, code, name),
            other => panic!("unexpected error {other:?}"),
        })
    }

    fn created_response(error_code: i16) -> CreateDelegationTokenResponse {
        CreateDelegationTokenResponse {
            error_code,
            principal_type: "Group".to_owned(),
            principal_name: "ops".to_owned(),
            token_requester_principal_type: "User".to_owned(),
            token_requester_principal_name: "admin".to_owned(),
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms: 2_000,
            max_timestamp_ms: 9_000,
            token_id: "tok-1".to_owned(),
            hmac: Bytes::from_static(b"\xde\xad"),
            ..Default::default()
        }
    }

    /// Kafka's `createDelegationToken` sends the renewers and the maximum
    /// lifetime, sets the owner fields only for an owner, and builds the
    /// token from the answer and the requested renewers.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn create_delegation_token_sends_each_option_as_kafka_does() {
        let token = |renewers| DelegationToken {
            token_id: "tok-1".to_owned(),
            hmac: vec![0xde, 0xad],
            owner: group("ops"),
            token_requester: user("admin"),
            renewers,
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms: 2_000,
            max_timestamp_ms: 9_000,
        };
        let renewers = vec![user("bob"), group("ops")];
        let wire_renewers = vec![
            CreatableRenewers {
                principal_type: "User".to_owned(),
                principal_name: "bob".to_owned(),
                ..Default::default()
            },
            CreatableRenewers {
                principal_type: "Group".to_owned(),
                principal_name: "ops".to_owned(),
                ..Default::default()
            },
        ];
        for (name, options, request, error_code, expected) in [
            (
                "defaults",
                CreateDelegationTokenOptions::default(),
                CreateDelegationTokenRequest {
                    owner_principal_type: Some(String::new()),
                    owner_principal_name: Some(String::new()),
                    renewers: Vec::new(),
                    max_lifetime_ms: -1,
                    ..Default::default()
                },
                0,
                Ok(token(Vec::new())),
            ),
            (
                "owner of another principal type, renewers, lifetime",
                CreateDelegationTokenOptions {
                    owner: Some(group("ops")),
                    renewers: renewers.clone(),
                    max_lifetime: Some(krabka_units::secs(60)),
                },
                CreateDelegationTokenRequest {
                    owner_principal_type: Some("Group".to_owned()),
                    owner_principal_name: Some("ops".to_owned()),
                    renewers: wire_renewers.clone(),
                    max_lifetime_ms: 60_000,
                    ..Default::default()
                },
                0,
                Ok(token(renewers.clone())),
            ),
            (
                "zero lifetime reaches the broker as zero",
                CreateDelegationTokenOptions {
                    max_lifetime: Some(krabka_units::millis(0)),
                    ..CreateDelegationTokenOptions::default()
                },
                CreateDelegationTokenRequest {
                    max_lifetime_ms: 0,
                    ..Default::default()
                },
                0,
                Ok(token(Vec::new())),
            ),
            (
                "not authorized to create for the owner",
                CreateDelegationTokenOptions {
                    owner: Some(user("alice")),
                    ..CreateDelegationTokenOptions::default()
                },
                CreateDelegationTokenRequest {
                    owner_principal_type: Some("User".to_owned()),
                    owner_principal_name: Some("alice".to_owned()),
                    max_lifetime_ms: -1,
                    ..Default::default()
                },
                65,
                Err((
                    "CreateDelegationToken",
                    65,
                    "DELEGATION_TOKEN_AUTHORIZATION_FAILED",
                )),
            ),
            (
                "renewer of an invalid principal type",
                CreateDelegationTokenOptions {
                    renewers: renewers.clone(),
                    ..CreateDelegationTokenOptions::default()
                },
                CreateDelegationTokenRequest {
                    renewers: wire_renewers.clone(),
                    max_lifetime_ms: -1,
                    ..Default::default()
                },
                67,
                Err(("CreateDelegationToken", 67, "INVALID_PRINCIPAL_TYPE")),
            ),
        ] {
            let (broker, requests) = token_broker::<CreateDelegationTokenRequest, _>(
                TOKEN_APIS.to_vec(),
                create_delegation_token_request::API_KEY,
                created_response(error_code),
            )
            .await;
            let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = outcome(admin.create_delegation_token(&options).await);

            broker.stop();
            let requests = requests.lock().expect("requests lock").clone();
            assert2::assert!(
                (result, requests) == (expected, vec![request]),
                "case {name}"
            );
        }
    }

    /// The `AdminClientLike` methods forward to the inherent calls: each one
    /// reaches the broker and returns the inherent call's result.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn admin_client_like_forwards_each_token_call() {
        use crate::AdminClientLike;

        let (create, _) = token_broker::<CreateDelegationTokenRequest, _>(
            TOKEN_APIS.to_vec(),
            create_delegation_token_request::API_KEY,
            created_response(0),
        )
        .await;
        let mut admin = fast_admin(create.addr, krabka_units::secs(5)).await;
        let created = AdminClientLike::create_delegation_token(
            &mut admin,
            &CreateDelegationTokenOptions::default(),
        )
        .await
        .map(|token| token.token_id);
        create.stop();

        let (renew, _) = token_broker::<RenewDelegationTokenRequest, _>(
            TOKEN_APIS.to_vec(),
            renew_delegation_token_request::API_KEY,
            RenewDelegationTokenResponse {
                expiry_timestamp_ms: 7_000,
                ..Default::default()
            },
        )
        .await;
        let mut admin = fast_admin(renew.addr, krabka_units::secs(5)).await;
        let renewed = AdminClientLike::renew_delegation_token(
            &mut admin,
            b"hmac",
            RenewDelegationTokenOptions::default(),
        )
        .await;
        renew.stop();

        let (expire, _) = token_broker::<ExpireDelegationTokenRequest, _>(
            TOKEN_APIS.to_vec(),
            expire_delegation_token_request::API_KEY,
            ExpireDelegationTokenResponse {
                expiry_timestamp_ms: 8_000,
                ..Default::default()
            },
        )
        .await;
        let mut admin = fast_admin(expire.addr, krabka_units::secs(5)).await;
        let expired = AdminClientLike::expire_delegation_token(
            &mut admin,
            b"hmac",
            ExpireDelegationTokenOptions::default(),
        )
        .await;
        expire.stop();

        let (describe, _) = token_broker::<DescribeDelegationTokenRequest, _>(
            TOKEN_APIS.to_vec(),
            describe_delegation_token_request::API_KEY,
            DescribeDelegationTokenResponse::default(),
        )
        .await;
        let mut admin = fast_admin(describe.addr, krabka_units::secs(5)).await;
        let described = AdminClientLike::describe_delegation_token(
            &mut admin,
            &DescribeDelegationTokenOptions::default(),
        )
        .await
        .map(|tokens| tokens.len());
        describe.stop();

        assert2::assert!(
            (
                outcome(created),
                outcome(renewed),
                outcome(expired),
                outcome(described),
            ) == (Ok("tok-1".to_owned()), Ok(7_000), Ok(8_000), Ok(0))
        );
    }

    /// Below v3 the requester fields are absent and decode empty, and a
    /// token could then only be made by its owner, so the requester of a
    /// created or described token falls back to the owner. From v3 the answer
    /// names the requester.
    #[test]
    fn a_token_without_requester_fields_names_its_owner_as_requester() {
        for (name, requester_type, requester_name, expected) in [
            ("v1 or v2 answer", "", "", user("alice")),
            ("v3 answer", "User", "admin", user("admin")),
        ] {
            let created = created_token(
                CreateDelegationTokenResponse {
                    principal_type: "User".to_owned(),
                    principal_name: "alice".to_owned(),
                    token_requester_principal_type: requester_type.to_owned(),
                    token_requester_principal_name: requester_name.to_owned(),
                    ..Default::default()
                },
                &[],
            )
            .expect("no error code");
            let described = described_token(DescribedDelegationToken {
                principal_type: "User".to_owned(),
                principal_name: "alice".to_owned(),
                token_requester_principal_type: requester_type.to_owned(),
                token_requester_principal_name: requester_name.to_owned(),
                ..Default::default()
            });
            assert2::assert!(
                (created.token_requester, described.token_requester)
                    == (expected.clone(), expected),
                "case {name}"
            );
        }
    }

    /// Kafka's request refuses to write the owner fields below v3, so an
    /// owner needs v3. Without an owner an older version serves.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn create_delegation_token_needs_v3_only_for_an_owner() {
        for (name, owner, expected) in [
            ("no owner", None, Ok(1)),
            (
                "owner",
                Some(user("alice")),
                Err((create_delegation_token_request::API_KEY, 1, 2, 3, 3)),
            ),
        ] {
            let (broker, requests) = token_broker::<CreateDelegationTokenRequest, _>(
                vec![(create_delegation_token_request::API_KEY, 1, 2)],
                create_delegation_token_request::API_KEY,
                created_response(0),
            )
            .await;
            let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = admin
                .create_delegation_token(&CreateDelegationTokenOptions {
                    owner,
                    ..CreateDelegationTokenOptions::default()
                })
                .await
                .map(|_| requests.lock().expect("requests lock").len())
                .map_err(|error| match error {
                    AdminError::Transport(ClientError::IncompatibleVersion {
                        api_key,
                        broker_min,
                        broker_max,
                        client_min,
                        client_max,
                    }) => (api_key, broker_min, broker_max, client_min, client_max),
                    other => panic!("case {name}: unexpected error {other:?}"),
                });

            broker.stop();
            assert2::assert!(result == expected, "case {name}");
        }
    }

    /// Kafka's `renewDelegationToken` sends the renew period, `-1` by
    /// default, and returns the expiry of the answer.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn renew_delegation_token_sends_the_period_and_returns_the_expiry() {
        for (name, renew_time_period, renew_period_ms, error_code, expected) in [
            ("broker default", None, -1, 0, Ok(5_000)),
            (
                "one hour",
                Some(krabka_units::secs(3_600)),
                3_600_000,
                0,
                Ok(5_000),
            ),
            (
                "unknown token",
                None,
                -1,
                62,
                Err(("RenewDelegationToken", 62, "DELEGATION_TOKEN_NOT_FOUND")),
            ),
            (
                "expired token",
                None,
                -1,
                66,
                Err(("RenewDelegationToken", 66, "DELEGATION_TOKEN_EXPIRED")),
            ),
        ] {
            let (broker, requests) = token_broker::<RenewDelegationTokenRequest, _>(
                TOKEN_APIS.to_vec(),
                renew_delegation_token_request::API_KEY,
                RenewDelegationTokenResponse {
                    error_code,
                    expiry_timestamp_ms: 5_000,
                    ..Default::default()
                },
            )
            .await;
            let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = outcome(
                admin
                    .renew_delegation_token(
                        b"\x01\x02",
                        RenewDelegationTokenOptions { renew_time_period },
                    )
                    .await,
            );

            broker.stop();
            let requests = requests.lock().expect("requests lock").clone();
            assert2::assert!(
                (result, requests)
                    == (
                        expected,
                        vec![RenewDelegationTokenRequest {
                            hmac: Bytes::from_static(b"\x01\x02"),
                            renew_period_ms,
                            ..Default::default()
                        }]
                    ),
                "case {name}"
            );
        }
    }

    /// Kafka's `expireDelegationToken` sends the expiry period, `-1` (now)
    /// by default, and returns the expiry of the answer, which
    /// `kafka-delegation-tokens` prints as the new expiry date.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn expire_delegation_token_sends_the_period_and_returns_the_expiry() {
        for (name, expiry_time_period, expiry_time_period_ms, error_code, expected) in [
            ("now", None, -1, 0, Ok(7_000)),
            ("in zero ms", Some(krabka_units::millis(0)), 0, 0, Ok(7_000)),
            (
                "in one minute",
                Some(krabka_units::secs(60)),
                60_000,
                0,
                Ok(7_000),
            ),
            (
                "not a renewer",
                None,
                -1,
                63,
                Err((
                    "ExpireDelegationToken",
                    63,
                    "DELEGATION_TOKEN_OWNER_MISMATCH",
                )),
            ),
        ] {
            let (broker, requests) = token_broker::<ExpireDelegationTokenRequest, _>(
                TOKEN_APIS.to_vec(),
                expire_delegation_token_request::API_KEY,
                ExpireDelegationTokenResponse {
                    error_code,
                    expiry_timestamp_ms: 7_000,
                    ..Default::default()
                },
            )
            .await;
            let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = outcome(
                admin
                    .expire_delegation_token(
                        b"\xaa",
                        ExpireDelegationTokenOptions { expiry_time_period },
                    )
                    .await,
            );

            broker.stop();
            let requests = requests.lock().expect("requests lock").clone();
            assert2::assert!(
                (result, requests)
                    == (
                        expected,
                        vec![ExpireDelegationTokenRequest {
                            hmac: Bytes::from_static(b"\xaa"),
                            expiry_time_period_ms,
                            ..Default::default()
                        }]
                    ),
                "case {name}"
            );
        }
    }

    /// Kafka's `describeDelegationToken` sends a null owner list for no
    /// owners, which asks for every token that the caller may describe, and
    /// an empty list for an empty list, which asks for none. It maps each
    /// described token with its requester and renewers.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn describe_delegation_token_sends_the_owner_filter_as_kafka_does() {
        let described = DescribedDelegationToken {
            principal_type: "User".to_owned(),
            principal_name: "alice".to_owned(),
            token_requester_principal_type: "User".to_owned(),
            token_requester_principal_name: "operator".to_owned(),
            issue_timestamp: 1_000,
            expiry_timestamp: 2_000,
            max_timestamp: 9_000,
            token_id: "tok-1".to_owned(),
            hmac: Bytes::from_static(b"\xbe\xef"),
            renewers: vec![DescribedDelegationTokenRenewer {
                principal_type: "Group".to_owned(),
                principal_name: "ops".to_owned(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let token = DelegationToken {
            token_id: "tok-1".to_owned(),
            hmac: vec![0xbe, 0xef],
            owner: user("alice"),
            token_requester: user("operator"),
            renewers: vec![group("ops")],
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms: 2_000,
            max_timestamp_ms: 9_000,
        };
        let owner = |principal_type: &str, principal_name: &str| DescribeDelegationTokenOwner {
            principal_type: principal_type.to_owned(),
            principal_name: principal_name.to_owned(),
            ..Default::default()
        };
        for (name, owners, wire_owners, (error_code, tokens), expected) in [
            (
                "every token",
                None,
                None,
                (0, vec![described.clone()]),
                Ok(vec![token.clone()]),
            ),
            (
                "no owner",
                Some(Vec::new()),
                Some(Vec::new()),
                (0, Vec::new()),
                Ok(Vec::new()),
            ),
            (
                "owners of two principal types",
                Some(vec![user("alice"), group("ops")]),
                Some(vec![owner("User", "alice"), owner("Group", "ops")]),
                (0, vec![described.clone()]),
                Ok(vec![token.clone()]),
            ),
            (
                "tokens disabled",
                None,
                None,
                (61, Vec::new()),
                Err((
                    "DescribeDelegationToken",
                    61,
                    "DELEGATION_TOKEN_AUTH_DISABLED",
                )),
            ),
        ] {
            let (broker, requests) = token_broker::<DescribeDelegationTokenRequest, _>(
                TOKEN_APIS.to_vec(),
                describe_delegation_token_request::API_KEY,
                DescribeDelegationTokenResponse {
                    error_code,
                    tokens,
                    ..Default::default()
                },
            )
            .await;
            let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = outcome(
                admin
                    .describe_delegation_token(&DescribeDelegationTokenOptions { owners })
                    .await,
            );

            broker.stop();
            let requests = requests.lock().expect("requests lock").clone();
            assert2::assert!(
                (result, requests)
                    == (
                        expected,
                        vec![DescribeDelegationTokenRequest {
                            owners: wire_owners,
                            ..Default::default()
                        }]
                    ),
                "case {name}"
            );
        }
    }
}