cpa-exec 0.4.1

Part of cliproxy: upstream provider executors
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
975
976
977
978
979
//! Vertex service-account credentials (internal/auth/vertex, internal/cmd/vertex_import.go)
//! and the Google JWT token source the Vertex executor uses (golang.org/x/oauth2/google
//! `CredentialsFromJSON` with the cloud-platform scope, v0.30.0).
//!
//! Go creates a fresh token source for every request, so every Vertex request with a
//! service account first exchanges a signed JWT at the key's `token_uri`. RS256 with
//! PKCS#1 v1.5 is deterministic, so the exchange is byte-identical to Go's for the same
//! clock second.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use base64::Engine;
use bytes::Bytes;
use cpa_common::gostr::trim_space;
use cpa_common::json::{self as gj, GoValue};
use serde_json::{Map, Value};

use crate::meta_wire::{self, Slot};
use crate::proxy::{self, GoHeaders};

/// `https://www.googleapis.com/auth/cloud-platform`, the only scope Vertex asks for.
pub const SCOPE: &str = "https://www.googleapis.com/auth/cloud-platform";
/// `google.JWTTokenURL`, used when a key has no `token_uri`.
pub const DEFAULT_TOKEN_URL: &str = "https://oauth2.googleapis.com/token";
const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer";

/// Go's `base64.StdEncoding.DecodeString`: padded, line breaks ignored, non-zero
/// trailing bits accepted. The error is the `CorruptInputError` offset Go reports
/// ("illegal base64 data at input byte N"), from the same quantum-by-quantum scan
/// (encoding/base64 decodeQuantum).
fn std_decode(src: &[u8]) -> Result<Vec<u8>, usize> {
    let value = |c: u8| match c {
        b'A'..=b'Z' => Some(c - b'A'),
        b'a'..=b'z' => Some(c - b'a' + 26),
        b'0'..=b'9' => Some(c - b'0' + 52),
        b'+' => Some(62),
        b'/' => Some(63),
        _ => None,
    };
    let newline = |c: u8| c == b'\n' || c == b'\r';
    let mut out = Vec::with_capacity(src.len() / 4 * 3);
    let mut si = 0;
    while si < src.len() {
        let mut quantum = [0u8; 4];
        let mut len = 4;
        let mut trailing = None;
        let mut j = 0;
        while j < 4 {
            if si == src.len() {
                if j == 0 {
                    return Ok(out);
                }
                return Err(si - j);
            }
            let c = src[si];
            si += 1;
            if let Some(v) = value(c) {
                quantum[j] = v;
                j += 1;
                continue;
            }
            if newline(c) {
                continue;
            }
            if c != b'=' || j < 2 {
                return Err(si - 1);
            }
            if j == 2 {
                // "==" is expected; the first "=" is consumed.
                while si < src.len() && newline(src[si]) {
                    si += 1;
                }
                if si == src.len() {
                    return Err(src.len());
                }
                if src[si] != b'=' {
                    return Err(si - 1);
                }
                si += 1;
            }
            while si < src.len() && newline(src[si]) {
                si += 1;
            }
            if si < src.len() {
                trailing = Some(si);
            }
            len = j;
            break;
        }
        let v = u32::from(quantum[0]) << 18
            | u32::from(quantum[1]) << 12
            | u32::from(quantum[2]) << 6
            | u32::from(quantum[3]);
        out.extend_from_slice(&[(v >> 16) as u8, (v >> 8) as u8, v as u8][..len - 1]);
        if let Some(at) = trailing {
            return Err(at);
        }
    }
    Ok(out)
}

/// One PEM block (`encoding/pem.Block`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PemBlock {
    pub kind: String,
    pub headers: BTreeMap<String, String>,
    pub bytes: Vec<u8>,
}

/// `pem.getLine`: the line without its newline, a trailing `\r` and trailing spaces or
/// tabs; the rest; and the bytes consumed.
fn get_line(data: &[u8]) -> (&[u8], &[u8], usize) {
    let (mut i, j) = match data.iter().position(|b| *b == b'\n') {
        None => (data.len(), data.len()),
        Some(i) => (i, i + 1),
    };
    if j > i && i > 0 && data[i - 1] == b'\r' {
        i -= 1;
    }
    let mut line = &data[..i];
    while let [rest @ .., b' ' | b'\t'] = line {
        line = rest;
    }
    (line, &data[j..], j)
}

fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).position(|w| w == needle)
}

fn rfind(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).rposition(|w| w == needle)
}

/// Go 1.26 `pem.Decode`: the first END line, the last BEGIN line before it, headers,
/// and the base64 body; `None` when no block parses.
pub fn pem_decode(data: &[u8]) -> Option<PemBlock> {
    const START: &[u8] = b"\n-----BEGIN ";
    const END: &[u8] = b"\n-----END ";
    const EOL: &[u8] = b"-----";
    let mut rest = data;
    let mut end_trailer_index: isize = 0;
    loop {
        if end_trailer_index < 0 || end_trailer_index as usize > rest.len() {
            return None;
        }
        rest = &rest[end_trailer_index as usize..];
        let mut end_index = find(rest, END)? as isize;
        end_trailer_index = end_index + END.len() as isize;
        let Some(begin) = rfind(&rest[..end_index as usize], &START[1..]) else {
            continue;
        };
        if begin > 0 && rest[begin - 1] != b'\n' {
            continue;
        }
        let skip = (begin + START.len() - 1) as isize;
        rest = &rest[skip as usize..];
        end_index -= skip;
        end_trailer_index -= skip;
        let (type_line, next, consumed) = get_line(rest);
        rest = next;
        end_index -= consumed as isize;
        end_trailer_index -= consumed as isize;
        let Some(kind) = type_line.strip_suffix(EOL) else {
            continue;
        };
        let mut headers = BTreeMap::new();
        loop {
            if rest.is_empty() {
                return None;
            }
            let (line, next, consumed) = get_line(rest);
            let Some(colon) = line.iter().position(|b| *b == b':') else {
                break;
            };
            let key = String::from_utf8_lossy(trim_space(&line[..colon])).into_owned();
            let value = String::from_utf8_lossy(trim_space(&line[colon + 1..])).into_owned();
            headers.insert(key, value);
            rest = next;
            end_index -= consumed as isize;
            end_trailer_index -= consumed as isize;
        }
        if !headers.is_empty() && end_index < 0 {
            continue;
        }
        if end_trailer_index < 0 || end_trailer_index as usize > rest.len() {
            continue;
        }
        let trailer = &rest[end_trailer_index as usize..];
        let trailer_len = kind.len() + EOL.len();
        if trailer.len() < trailer_len {
            continue;
        }
        let (end_line, rest_of_end_line) = trailer.split_at(trailer_len);
        if !end_line.starts_with(kind) || !end_line.ends_with(EOL) {
            continue;
        }
        if !get_line(rest_of_end_line).0.is_empty() {
            continue;
        }
        let mut bytes = Vec::new();
        if end_index > 0 {
            let body: Vec<u8> = rest[..end_index as usize]
                .iter()
                .copied()
                .filter(|b| *b != b' ' && *b != b'\t')
                .collect();
            match std_decode(&body) {
                Ok(decoded) => bytes = decoded,
                Err(_) => continue,
            }
        }
        return Some(PemBlock {
            kind: String::from_utf8_lossy(kind).into_owned(),
            headers,
            bytes,
        });
    }
}

/// `pem.EncodeToMemory`: `Proc-Type` first, other headers sorted, then the body in
/// 64-column base64 lines.
pub fn pem_encode(block: &PemBlock) -> String {
    let mut out = format!("-----BEGIN {}-----\n", block.kind);
    if !block.headers.is_empty() {
        if let Some(value) = block.headers.get("Proc-Type") {
            out.push_str(&format!("Proc-Type: {value}\n"));
        }
        for (key, value) in &block.headers {
            if key != "Proc-Type" {
                out.push_str(&format!("{key}: {value}\n"));
            }
        }
        out.push('\n');
    }
    let encoded = base64::engine::general_purpose::STANDARD.encode(&block.bytes);
    for line in encoded.as_bytes().chunks(64) {
        out.push_str(std::str::from_utf8(line).unwrap_or_default());
        out.push('\n');
    }
    out.push_str(&format!("-----END {}-----\n", block.kind));
    out
}

/// `stripANSIEscape`: OSC sequences up to BEL or ST, CSI sequences up to their final
/// letter, and lone ESC characters.
fn strip_ansi(s: &str) -> String {
    let input: Vec<char> = s.chars().collect();
    let mut out = String::with_capacity(s.len());
    let mut i = 0;
    while i < input.len() {
        let c = input[i];
        if c != '\u{1b}' {
            out.push(c);
            i += 1;
            continue;
        }
        if i + 1 >= input.len() {
            i += 1;
            continue;
        }
        match input[i + 1] {
            ']' => {
                i += 2;
                while i < input.len() {
                    if input[i] == '\u{7}' {
                        break;
                    }
                    if input[i] == '\u{1b}' && i + 1 < input.len() && input[i + 1] == '\\' {
                        i += 1;
                        break;
                    }
                    i += 1;
                }
            }
            '[' => {
                i += 2;
                while i < input.len() {
                    if input[i].is_ascii_alphabetic() {
                        break;
                    }
                    i += 1;
                }
            }
            _ => {}
        }
        i += 1;
    }
    out
}

/// `rebuildPEM`: the base64 alphabet between the markers of a block that lost its line
/// structure.
fn rebuild_pem(raw: &str) -> Result<String, String> {
    let kind = if raw.contains("RSA PRIVATE KEY") {
        "RSA PRIVATE KEY"
    } else {
        "PRIVATE KEY"
    };
    let header = format!("-----BEGIN {kind}-----");
    let footer = format!("-----END {kind}-----");
    let (Some(start), Some(end)) = (raw.find(&header), raw.find(&footer)) else {
        return Err("missing pem markers".into());
    };
    if end <= start {
        return Err("missing pem markers".into());
    }
    let payload: String = raw[start + header.len()..end]
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '/' | '='))
        .collect();
    if payload.is_empty() {
        return Err("private_key base64 payload empty".into());
    }
    let der = std_decode(payload.as_bytes())
        .map_err(|at| format!("private_key base64 decode failed: illegal base64 data at input byte {at}"))?;
    Ok(pem_encode(&PemBlock {
        kind: kind.into(),
        headers: BTreeMap::new(),
        bytes: der,
    }))
}

/// A parsed RSA private key and its PKCS#1 DER (`x509.MarshalPKCS1PrivateKey`).
fn pkcs1_der(rsa: &btls::rsa::Rsa<btls::pkey::Private>) -> Result<Vec<u8>, String> {
    rsa.private_key_to_der().map_err(|e| e.to_string())
}

/// `x509.ParsePKCS1PrivateKey`. The error is Go's for trailing data and a generic
/// x509 message otherwise.
// ponytail: Go also accepts keys that omit the CRT values and multi-prime keys, which
// BoringSSL rejects; Google issues neither. Other parse errors carry no asn1 detail.
fn parse_pkcs1(der: &[u8]) -> Result<btls::rsa::Rsa<btls::pkey::Private>, String> {
    const FAILED: &str = "x509: failed to parse private key";
    let key = btls::rsa::Rsa::private_key_from_der(der).map_err(|_| FAILED)?;
    // BoringSSL's d2i parser stops after the key; asn1.Unmarshal rejects what follows,
    // before any key validation.
    if !single_sequence(der) {
        return Err("asn1: syntax error: trailing data".into());
    }
    if !key.check_key().unwrap_or(false) {
        return Err(FAILED.into());
    }
    Ok(key)
}

/// Whether `der` is exactly one DER SEQUENCE, nothing after it.
fn single_sequence(der: &[u8]) -> bool {
    let [0x30, first, rest @ ..] = der else {
        return false;
    };
    let (len, header) = match *first {
        n if n < 0x80 => (usize::from(n), 2),
        n @ 0x81..=0x84 => {
            let k = usize::from(n & 0x7f);
            let Some(bytes) = rest.get(..k) else {
                return false;
            };
            (bytes.iter().fold(0usize, |a, b| a << 8 | usize::from(*b)), 2 + k)
        }
        _ => return false,
    };
    header.checked_add(len) == Some(der.len())
}

/// PKCS#8: `Err(true)` for a parsed non-RSA key, `Err(false)` for unparseable input.
fn parse_pkcs8_rsa(der: &[u8]) -> Result<btls::rsa::Rsa<btls::pkey::Private>, bool> {
    let key = btls::pkey::PKey::private_key_from_pkcs8(der).map_err(|_| false)?;
    key.rsa().map_err(|_| true)
}

/// `ensureRSAPrivateKey`.
// ponytail: x509 parse errors carry BoringSSL's reason rather than Go's encoding/asn1
// text; which inputs fail matches.
fn ensure_rsa(block: PemBlock) -> Result<PemBlock, String> {
    let pkcs1 = |bytes: Vec<u8>| PemBlock {
        kind: "RSA PRIVATE KEY".into(),
        headers: BTreeMap::new(),
        bytes,
    };
    match block.kind.as_str() {
        "RSA PRIVATE KEY" => match parse_pkcs1(&block.bytes) {
            Ok(_) => Ok(block),
            Err(e) => Err(format!("private_key invalid rsa: {e}")),
        },
        "PRIVATE KEY" => match parse_pkcs8_rsa(&block.bytes) {
            Ok(rsa) => Ok(pkcs1(pkcs1_der(&rsa)?)),
            Err(true) => Err("private_key is not an RSA key".into()),
            Err(false) => Err("private_key invalid pkcs8: x509: failed to parse private key".into()),
        },
        _ => {
            if let Ok(rsa) = parse_pkcs1(&block.bytes) {
                return Ok(pkcs1(pkcs1_der(&rsa)?));
            }
            if let Ok(rsa) = parse_pkcs8_rsa(&block.bytes) {
                return Ok(pkcs1(pkcs1_der(&rsa)?));
            }
            Err("private_key uses unsupported format".into())
        }
    }
}

/// `sanitizePrivateKey`: a valid `RSA PRIVATE KEY` PEM block for a pasted key.
pub fn sanitize_private_key(raw: &str) -> Result<String, String> {
    let pk = raw.replace("\r\n", "\n").replace('\r', "\n");
    let pk = strip_ansi(&pk);
    let pk = String::from_utf8_lossy(trim_space(pk.as_bytes())).into_owned();
    let normalized = if pem_decode(pk.as_bytes()).is_some() {
        pk
    } else {
        rebuild_pem(&pk).map_err(|e| format!("private_key is not valid pem: {e}"))?
    };
    let block = pem_decode(normalized.as_bytes()).ok_or("private_key pem decode failed")?;
    Ok(pem_encode(&ensure_rsa(block)?))
}

/// `NormalizeServiceAccountMap`: a copy whose `private_key` is a valid RSA PEM block.
pub fn normalize_service_account(sa: &Map<String, Value>) -> Result<Map<String, Value>, String> {
    let pk = sa.get("private_key").and_then(Value::as_str).unwrap_or_default();
    if pk.trim().is_empty() {
        return Err("service account missing private_key".into());
    }
    let mut out = sa.clone();
    out.insert("private_key".into(), Value::String(sanitize_private_key(pk)?));
    Ok(out)
}

/// The key-file fields the JWT token source reads.
#[derive(Default)]
struct KeyFile {
    kind: String,
    client_email: String,
    private_key: String,
    private_key_id: String,
    token_uri: String,
    audience: String,
}

impl KeyFile {
    /// `google.CredentialsFromJSON`: Go marshals the normalized map with its keys sorted
    /// and unmarshals it into `credentialsFile` (oauth2 v0.30.0), so a wrongly typed
    /// member of any field fails and, of keys that fold together, the last sorted wins.
    /// The workspace map keeps file order, so the keys are sorted here.
    fn parse(sa: &Map<String, Value>) -> Result<Self, String> {
        let sorted: BTreeMap<&String, &Value> = sa.iter().collect();
        let data = serde_json::to_vec(&sorted).map_err(|e| e.to_string())?;
        let mut f = Self::default();
        let mut unused: [String; 13] = Default::default();
        let mut unused = unused.iter_mut();
        let mut other = || Slot::Str(unused.next().expect("one string per unread field"));
        let mut fields = [
            ("type", Slot::Str(&mut f.kind)),
            ("client_email", Slot::Str(&mut f.client_email)),
            ("private_key_id", Slot::Str(&mut f.private_key_id)),
            ("private_key", Slot::Str(&mut f.private_key)),
            ("auth_uri", other()),
            ("token_uri", Slot::Str(&mut f.token_uri)),
            ("project_id", other()),
            ("universe_domain", other()),
            ("client_secret", other()),
            ("client_id", other()),
            ("refresh_token", other()),
            ("audience", Slot::Str(&mut f.audience)),
            ("subject_token_type", other()),
            ("token_url", other()),
            ("token_info_url", other()),
            ("service_account_impersonation_url", other()),
            ("service_account_impersonation", Slot::Object),
            ("delegates", Slot::Strs),
            ("credential_source", Slot::Object),
            ("quota_project_id", other()),
            ("workforce_pool_user_project", other()),
            ("revoke_url", other()),
            ("source_credentials", Slot::Object),
        ];
        meta_wire::unmarshal(&data, "google", "credentialsFile", &mut fields)?;
        Ok(f)
    }
}

/// `oauth2/internal.ParseKey`: a PEM block or bare DER, PKCS#8 then PKCS#1, RSA only.
fn signing_key(pem: &str) -> Result<btls::pkey::PKey<btls::pkey::Private>, String> {
    let der = pem_decode(pem.as_bytes()).map_or_else(|| pem.as_bytes().to_vec(), |b| b.bytes);
    let rsa = match parse_pkcs8_rsa(&der) {
        Ok(rsa) => rsa,
        Err(true) => return Err("private key is invalid".into()),
        Err(false) => parse_pkcs1(&der)
            .map_err(|e| format!("private key should be a PEM or plain PKCS1 or PKCS8; parse error: {e}"))?,
    };
    btls::pkey::PKey::from_rsa(rsa).map_err(|e| e.to_string())
}

fn b64url(data: &[u8]) -> String {
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(data)
}

fn quote(s: &str) -> String {
    let mut out = Vec::new();
    gj::marshal_str(&mut out, s.as_bytes(), true);
    String::from_utf8(out).unwrap_or_default()
}

/// `jws.Encode` of the service account's claim set at `now` (Unix seconds): issued ten
/// seconds early, valid for an hour.
pub fn signed_assertion(key: &KeyFileView<'_>, now: i64) -> Result<String, String> {
    let pkey = signing_key(key.private_key)?;
    let mut header = String::from(r#"{"alg":"RS256","typ":"JWT""#);
    if !key.private_key_id.is_empty() {
        header.push_str(&format!(r#","kid":{}"#, quote(key.private_key_id)));
    }
    header.push('}');
    let iat = now - 10;
    let mut claims = format!(r#"{{"iss":{}"#, quote(key.client_email));
    claims.push_str(&format!(r#","scope":{}"#, quote(SCOPE)));
    claims.push_str(&format!(
        r#","aud":{},"exp":{},"iat":{iat}}}"#,
        quote(key.audience),
        iat + 3600
    ));
    let signing_input = format!("{}.{}", b64url(header.as_bytes()), b64url(claims.as_bytes()));
    let mut signer = btls::sign::Signer::new(btls::hash::MessageDigest::sha256(), &pkey).map_err(|e| e.to_string())?;
    let signature = signer
        .sign_oneshot_to_vec(signing_input.as_bytes())
        .map_err(|e| e.to_string())?;
    Ok(format!("{signing_input}.{}", b64url(&signature)))
}

/// The service-account fields the JWT needs, with Go's defaults applied.
pub struct KeyFileView<'a> {
    pub client_email: &'a str,
    pub private_key: &'a str,
    pub private_key_id: &'a str,
    /// `aud`: the key's `audience`, else the token URL.
    pub audience: &'a str,
}

/// The token request for a key file: Go's `jwtSource` URL and form body, signed at
/// `now` (Unix seconds), after `CredentialsFromJSON` decoded the key.
pub fn token_request(sa: &Map<String, Value>, now: i64) -> Result<(String, String), String> {
    // ponytail: Go first tries the key as a `web`/`installed` OAuth client file
    // (ConfigFromJSON), whose token source then needs an interactive handler; a Go
    // service-account file never has those members.
    let file = KeyFile::parse(sa)?;
    match file.kind.as_str() {
        "service_account" => {}
        // ponytail: with a refresh token Go refreshes an `authorized_user` key at its token
        // URL, and it runs the external-account, impersonation and GDCH flows for those
        // types. Vertex service-account files carry none of them; without a refresh token
        // Go fails like this.
        "authorized_user" => return Err("oauth2: token expired and refresh token is not set".into()),
        "" => return Err("missing 'type' field in credentials".into()),
        other => return Err(format!("unknown credential type: {}", gostr_quote(other))),
    }
    let token_url = if file.token_uri.is_empty() {
        DEFAULT_TOKEN_URL
    } else {
        file.token_uri.as_str()
    };
    let audience = if file.audience.is_empty() {
        token_url
    } else {
        file.audience.as_str()
    };
    let assertion = signed_assertion(
        &KeyFileView {
            client_email: &file.client_email,
            private_key: &file.private_key,
            private_key_id: &file.private_key_id,
            audience,
        },
        now,
    )?;
    let body = format!(
        "assertion={}&grant_type={}",
        cpa_common::gostr::query_escape(&assertion),
        cpa_common::gostr::query_escape(GRANT_TYPE)
    );
    Ok((token_url.to_owned(), body))
}

/// One token from the key file: `google.CredentialsFromJSON(...).TokenSource.Token()`.
/// Returns the access token, which may be empty (Go then sends no `Authorization`).
pub async fn access_token(client: &wreq::Client, sa: &Map<String, Value>, now: i64) -> Result<String, String> {
    let (token_url, body) = token_request(sa, now)?;
    let token_url = token_url.as_str();
    let mut headers = GoHeaders::new();
    headers.set("Content-Type", "application/x-www-form-urlencoded");
    let upstream = proxy::send(client, token_url, headers, Bytes::from(body), None)
        .await
        .map_err(|e| format!("oauth2: cannot fetch token: {}", String::from_utf8_lossy(&e.body)))?;
    let status = upstream.status;
    let data = proxy::read_all(upstream.body, 1 << 20, false)
        .await
        .map_err(|e| format!("oauth2: cannot fetch token: {}", String::from_utf8_lossy(&e.body)))?;
    if !(200..=299).contains(&status) {
        // oauth2.RetrieveError: the response status line, then the body.
        let reason = http::StatusCode::from_u16(status)
            .ok()
            .and_then(|s| s.canonical_reason())
            .unwrap_or_default();
        return Err(format!(
            "oauth2: cannot fetch token: {status} {reason}\nResponse: {}",
            String::from_utf8_lossy(&data)
        ));
    }
    parse_token(&data)
}

fn gostr_quote(s: &str) -> String {
    cpa_common::gostr::quote(s)
}

/// The access token from a token response, decoded as Go's `jwtSource.Token` does into
/// `struct { oauth2.Token; IDToken string }`: a wrongly typed member fails, and an
/// `id_token` must decode as a JWT claim set.
fn parse_token(data: &[u8]) -> Result<String, String> {
    let (mut access, mut token_type, mut refresh, mut id_token, mut expires_in) = Default::default();
    let mut fields = [
        ("access_token", Slot::Str(&mut access)),
        ("token_type", Slot::Str(&mut token_type)),
        ("refresh_token", Slot::Str(&mut refresh)),
        ("expiry", Slot::Time),
        ("expires_in", Slot::Int(&mut expires_in, "int64")),
        ("id_token", Slot::Str(&mut id_token)),
    ];
    meta_wire::unmarshal(data, "oauth2", "Token", &mut fields)
        .map_err(|e| format!("oauth2: cannot fetch token: {e}"))?;
    if !id_token.is_empty() {
        decode_id_token(&id_token).map_err(|e| format!("oauth2: error decoding JWT token: {e}"))?;
    }
    Ok(access)
}

/// `jws.Decode`: exactly three dot-separated segments; the second is unpadded base64url
/// (line breaks ignored) whose first JSON value decodes into a `jws.ClaimSet`. What
/// follows that value is never read.
fn decode_id_token(token: &str) -> Result<(), String> {
    let segments: Vec<&str> = token.split('.').collect();
    if segments.len() != 3 {
        return Err("jws: invalid token received".into());
    }
    let engine = base64::engine::GeneralPurpose::new(
        &base64::alphabet::URL_SAFE,
        base64::engine::GeneralPurposeConfig::new()
            .with_decode_allow_trailing_bits(true)
            .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone),
    );
    let claims: Vec<u8> = segments[1].bytes().filter(|b| *b != b'\r' && *b != b'\n').collect();
    let claims = engine.decode(claims).map_err(|e| format!("illegal base64 data: {e}"))?;
    let Some(start) = claims.iter().position(|c| !matches!(c, b' ' | b'\t' | b'\r' | b'\n')) else {
        return Err("EOF".into());
    };
    let value = &claims[start..];
    if value.starts_with(b"null") {
        return Ok(());
    }
    if !value.starts_with(b"{") {
        // Any other first value fails: a non-object cannot fill the struct.
        return Err("json: cannot unmarshal into Go value of type jws.ClaimSet".into());
    }
    let end = meta_wire::object_end(value).ok_or("unexpected EOF")?;
    let (mut iss, mut scope, mut aud, mut typ, mut sub, mut prn) = Default::default();
    let (mut exp, mut iat) = (0, 0);
    let mut fields = [
        ("iss", Slot::Str(&mut iss)),
        ("scope", Slot::Str(&mut scope)),
        ("aud", Slot::Str(&mut aud)),
        ("exp", Slot::Int(&mut exp, "int64")),
        ("iat", Slot::Int(&mut iat, "int64")),
        ("typ", Slot::Str(&mut typ)),
        ("sub", Slot::Str(&mut sub)),
        ("prn", Slot::Str(&mut prn)),
    ];
    meta_wire::unmarshal(&value[..end], "jws", "ClaimSet", &mut fields)
}

/// `sanitizeFilePart`.
fn file_part(s: &str) -> String {
    s.trim().replace(['/', '\\', ':'], "_").replace(' ', "-")
}

/// `labelForVertex`.
fn label(project: &str, email: &str) -> String {
    match (project.trim(), email.trim()) {
        ("", "") => "vertex".into(),
        (p, "") => p.into(),
        ("", e) => e.into(),
        (p, e) => format!("{p} ({e})"),
    }
}

/// `-vertex-import`: reads a service-account key, normalizes its private key and writes
/// `vertex-[<prefix>-]<project>.json` in `auth_dir`. Errors are Go's log messages.
pub fn import(auth_dir: &Path, key_path: &str, prefix: &str) -> Result<PathBuf, String> {
    let path = key_path.trim();
    if path.is_empty() {
        return Err("vertex-import: missing service account key path".into());
    }
    let data = std::fs::read(path).map_err(|e| {
        format!(
            "vertex-import: read file failed: {}",
            path_error("open", Path::new(path), &e)
        )
    })?;
    let sa = decode_service_account(&data)
        .map_err(|e| format!("vertex-import: invalid service account json: {e}"))
        .and_then(|sa| normalize_decoded(sa).map_err(|e| format!("vertex-import: {e}")))?;
    let text = |sa: &BTreeMap<String, GoValue>, key: &str| match sa.get(key) {
        Some(GoValue::String(s)) => s.clone(),
        _ => String::new(),
    };
    let (email, project) = (text(&sa, "client_email"), text(&sa, "project_id"));
    if project.trim().is_empty() {
        return Err("vertex-import: project_id missing in service account json".into());
    }
    if email.trim().is_empty() {
        tracing::warn!("vertex-import: client_email missing in service account json");
    }
    let prefix = prefix.trim().trim_matches('/');
    if prefix.contains('/') {
        return Err(format!(
            "vertex-import: prefix must be a single segment (no '/' allowed): {}",
            gostr_quote(prefix)
        ));
    }
    let mut base = file_part(&project);
    if !prefix.is_empty() {
        base = format!("{}-{base}", file_part(prefix));
    }
    let mut doc = BTreeMap::new();
    doc.insert("disabled".to_owned(), GoValue::Bool(false));
    doc.insert("email".to_owned(), GoValue::String(email.clone()));
    doc.insert("label".to_owned(), GoValue::String(label(&project, &email)));
    doc.insert("location".to_owned(), GoValue::String("us-central1".into()));
    doc.insert("prefix".to_owned(), GoValue::String(prefix.to_owned()));
    doc.insert("project_id".to_owned(), GoValue::String(project));
    doc.insert("service_account".to_owned(), GoValue::Object(sa));
    doc.insert("type".to_owned(), GoValue::String("vertex".into()));
    // FileTokenStore.Save: filepath.Join (cleaned), MkdirAll of its directory, then
    // VertexCredentialStorage.SaveTokenToFile.
    let out = go_clean(&auth_dir.join(format!("vertex-{base}.json")));
    let save_failed = |e: String| format!("vertex-import: save credential failed: {e}");
    let dir = out.parent().unwrap_or(Path::new("."));
    go_mkdir_all(dir).map_err(|e| save_failed(format!("auth filestore: create dir failed: {e}")))?;
    // misc.LogSavingCredentials.
    println!("Saving credentials to {}", out.display());
    // Private (0600) and atomic, like every other credential writer; Go uses os.Create.
    crate::kimi_auth::write_private(&out, &GoValue::Object(doc).encode_indented()).map_err(|e| {
        save_failed(format!(
            "vertex credential: create file failed: {}",
            path_error("open", &out, &e)
        ))
    })?;
    Ok(out)
}

/// `json.Unmarshal` into `map[string]any` with Go's error text; `None` for `null`.
fn decode_service_account(data: &[u8]) -> Result<Option<BTreeMap<String, GoValue>>, String> {
    // Numbers become float64.
    match GoValue::parse_f64(data).map(GoValue::into_object) {
        Some(Ok(sa)) => Ok(Some(sa)),
        // `null` leaves the map nil.
        Some(Err(GoValue::Null)) => Ok(None),
        Some(Err(other)) => {
            let kind = match other {
                GoValue::Array(_) => "array",
                GoValue::String(_) => "string",
                GoValue::Bool(_) => "bool",
                _ => "number",
            };
            Err(format!(
                "json: cannot unmarshal {kind} into Go value of type map[string]interface {{}}"
            ))
        }
        // Go's syntax error, else the first number beyond float64.
        None => Err(meta_wire::check_valid(data).err().unwrap_or_else(|| {
            let number = first_overflow(&gj::parse(data)).unwrap_or_default();
            format!("json: cannot unmarshal number {number} into Go value of type float64")
        })),
    }
}

/// [`normalize_service_account`] for a decoded Go map; `None` is a nil map.
fn normalize_decoded(sa: Option<BTreeMap<String, GoValue>>) -> Result<BTreeMap<String, GoValue>, String> {
    let mut sa = sa.ok_or("service account payload is empty")?;
    let private_key = match sa.get("private_key") {
        Some(GoValue::String(s)) => s.clone(),
        _ => String::new(),
    };
    if private_key.trim().is_empty() {
        return Err("service account missing private_key".into());
    }
    sa.insert(
        "private_key".into(),
        GoValue::String(sanitize_private_key(&private_key)?),
    );
    Ok(sa)
}

/// A service account imported through the Management API, checked and merged with an
/// existing file but not yet written ([`Upload::write`]).
#[derive(Debug)]
pub struct Upload {
    pub path: PathBuf,
    pub project: String,
    pub email: String,
    pub location: String,
    contents: Vec<u8>,
}

impl Upload {
    /// Go `VertexCredentialStorage.SaveTokenToFile`, called by the token store's `Save`.
    pub fn write(&self) -> Result<(), UploadError> {
        let dir = self.path.parent().unwrap_or(Path::new("."));
        go_mkdir_all(dir).map_err(|e| UploadError::Save(format!("auth filestore: create dir failed: {e}")))?;
        crate::kimi_auth::write_private(&self.path, &self.contents).map_err(|e| {
            UploadError::Save(format!(
                "vertex credential: create file failed: {}",
                path_error("open", &self.path, &e)
            ))
        })
    }
}

/// Why a Management API import failed, with Go's message for each answer.
#[derive(Debug)]
pub enum UploadError {
    InvalidJson(String),
    InvalidServiceAccount(String),
    ProjectMissing,
    Save(String),
}

/// Management `ImportVertexCredential`: like `-vertex-import`, but without a prefix,
/// with the given location (`us-central1` when empty), and saved as the management
/// token store saves: settings and `disabled` of an existing file are kept
/// (`mergeExistingAuthFileMetadata`). Nothing is written yet: the caller saves the
/// result through its token store.
pub fn import_upload(auth_dir: &Path, data: &[u8], location: &str) -> Result<Upload, UploadError> {
    let sa = decode_service_account(data).map_err(UploadError::InvalidJson)?;
    let sa = normalize_decoded(sa).map_err(UploadError::InvalidServiceAccount)?;
    // `valueAsString`: `fmt.Sprint` for a value that is not a string, so a numeric
    // project ID 1000000 names `vertex-1e+06.json`, as in Go.
    let text = |key: &str| match sa.get(key) {
        None | Some(GoValue::Null) => String::new(),
        Some(value) => go_sprint(value).trim().to_owned(),
    };
    let (project, email) = (text("project_id"), text("client_email"));
    if project.is_empty() {
        return Err(UploadError::ProjectMissing);
    }
    let location = match location.trim() {
        "" => "us-central1".to_owned(),
        l => l.to_owned(),
    };
    let mut doc = BTreeMap::new();
    doc.insert("email".to_owned(), GoValue::String(email.clone()));
    doc.insert("label".to_owned(), GoValue::String(label(&project, &email)));
    doc.insert("location".to_owned(), GoValue::String(location.clone()));
    doc.insert("project_id".to_owned(), GoValue::String(project.clone()));
    doc.insert("service_account".to_owned(), GoValue::Object(sa));
    doc.insert("type".to_owned(), GoValue::String("vertex".into()));
    let path = go_clean(&auth_dir.join(format!("vertex-{}.json", file_part(&project))));
    let disabled = std::fs::read(&path)
        .ok()
        .and_then(|raw| crate::meta_auth::go_map_from_json(&raw))
        .and_then(|(existing, _)| crate::meta_auth::merge_existing(&mut doc, &existing, "vertex"))
        .unwrap_or(false);
    crate::meta_auth::normalize_metadata(&mut doc);
    if let Some(weight) = doc.get("weight") {
        let json: Value = serde_json::from_slice(&weight.marshal()).unwrap_or(Value::Null);
        cpa_core::config::credentials::parse_weight(&json)
            .map_err(|e| UploadError::Save(format!("auth filestore: invalid metadata weight: {e}")))?;
    }
    doc.insert("disabled".to_owned(), GoValue::Bool(disabled));
    Ok(Upload {
        path,
        project,
        email,
        location,
        contents: GoValue::Object(doc).encode_indented(),
    })
}

/// `fmt.Sprint` of a value `json.Unmarshal` decoded into `any`: numbers are float64
/// printed as `%v`, maps print their keys sorted.
fn go_sprint(value: &GoValue) -> String {
    match value {
        GoValue::Null => "<nil>".into(),
        GoValue::Bool(b) => b.to_string(),
        GoValue::String(s) => s.clone(),
        GoValue::Number(n) => crate::xai_auth::go_float_v(cpa_common::json::parse_float(n.as_bytes())),
        GoValue::Array(items) => format!("[{}]", items.iter().map(go_sprint).collect::<Vec<_>>().join(" ")),
        GoValue::Object(map) => {
            let items: Vec<String> = map.iter().map(|(k, v)| format!("{k}:{}", go_sprint(v))).collect();
            format!("map[{}]", items.join(" "))
        }
    }
}

/// The first number, in document order, that does not fit a float64.
fn first_overflow(value: &gj::Res<'_>) -> Option<String> {
    if value.kind == gj::Kind::Number {
        let raw = String::from_utf8_lossy(value.raw()).into_owned();
        return gj::go_parse_float(value.raw()).is_err().then_some(raw);
    }
    let mut found = None;
    if value.kind == gj::Kind::Json {
        value.each(|_, item| {
            found = first_overflow(&item);
            found.is_none()
        });
    }
    found
}

/// Go's `*fs.PathError` text: `<op> <path>: <errno text>`.
fn path_error(op: &str, path: &Path, e: &std::io::Error) -> String {
    // syscall.Errno strings are strerror in lower case.
    let text = e.to_string();
    let text = text.split(" (os error").next().unwrap_or_default();
    let mut chars = text.chars();
    let errno: String = chars
        .next()
        .map_or_else(String::new, |c| c.to_lowercase().chain(chars).collect());
    format!("{op} {}: {errno}", path.display())
}

/// `filepath.Clean`, lexically: no `.` elements, `..` folded into its parent, no
/// trailing separator; `.` for an empty result.
fn go_clean(path: &Path) -> PathBuf {
    use std::path::Component;
    let mut out: Vec<Component<'_>> = Vec::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => match out.last() {
                Some(Component::Normal(_)) => {
                    out.pop();
                }
                Some(Component::RootDir | Component::Prefix(_)) => {}
                _ => out.push(component),
            },
            other => out.push(other),
        }
    }
    if out.is_empty() {
        return PathBuf::from(".");
    }
    out.iter().collect()
}

/// `os.MkdirAll(dir, 0o700)` with Go's error: an existing non-directory fails as
/// `mkdir <path>: not a directory`, parents are created first, and a failed `mkdir`
/// names the directory it could not create.
fn go_mkdir_all(dir: &Path) -> Result<(), String> {
    match std::fs::metadata(dir) {
        Ok(meta) if meta.is_dir() => return Ok(()),
        Ok(_) => return Err(format!("mkdir {}: not a directory", dir.display())),
        Err(_) => {}
    }
    if let Some(parent) = dir.parent().filter(|p| !p.as_os_str().is_empty()) {
        go_mkdir_all(parent)?;
    }
    #[cfg(unix)]
    let builder = {
        let mut builder = std::fs::DirBuilder::new();
        std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
        builder
    };
    #[cfg(not(unix))]
    let builder = std::fs::DirBuilder::new();
    match builder.create(dir) {
        Ok(()) => Ok(()),
        Err(_) if std::fs::symlink_metadata(dir).is_ok_and(|m| m.is_dir()) => Ok(()),
        Err(e) => Err(path_error("mkdir", dir, &e)),
    }
}