1use std::collections::BTreeMap;
11use std::path::{Path, PathBuf};
12
13use base64::Engine;
14use bytes::Bytes;
15use cpa_common::gostr::trim_space;
16use cpa_common::json::{self as gj, GoValue};
17use serde_json::{Map, Value};
18
19use crate::meta_wire::{self, Slot};
20use crate::proxy::{self, GoHeaders};
21
22pub const SCOPE: &str = "https://www.googleapis.com/auth/cloud-platform";
24pub const DEFAULT_TOKEN_URL: &str = "https://oauth2.googleapis.com/token";
26const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:jwt-bearer";
27
28fn std_decode(src: &[u8]) -> Result<Vec<u8>, usize> {
33 let value = |c: u8| match c {
34 b'A'..=b'Z' => Some(c - b'A'),
35 b'a'..=b'z' => Some(c - b'a' + 26),
36 b'0'..=b'9' => Some(c - b'0' + 52),
37 b'+' => Some(62),
38 b'/' => Some(63),
39 _ => None,
40 };
41 let newline = |c: u8| c == b'\n' || c == b'\r';
42 let mut out = Vec::with_capacity(src.len() / 4 * 3);
43 let mut si = 0;
44 while si < src.len() {
45 let mut quantum = [0u8; 4];
46 let mut len = 4;
47 let mut trailing = None;
48 let mut j = 0;
49 while j < 4 {
50 if si == src.len() {
51 if j == 0 {
52 return Ok(out);
53 }
54 return Err(si - j);
55 }
56 let c = src[si];
57 si += 1;
58 if let Some(v) = value(c) {
59 quantum[j] = v;
60 j += 1;
61 continue;
62 }
63 if newline(c) {
64 continue;
65 }
66 if c != b'=' || j < 2 {
67 return Err(si - 1);
68 }
69 if j == 2 {
70 while si < src.len() && newline(src[si]) {
72 si += 1;
73 }
74 if si == src.len() {
75 return Err(src.len());
76 }
77 if src[si] != b'=' {
78 return Err(si - 1);
79 }
80 si += 1;
81 }
82 while si < src.len() && newline(src[si]) {
83 si += 1;
84 }
85 if si < src.len() {
86 trailing = Some(si);
87 }
88 len = j;
89 break;
90 }
91 let v = u32::from(quantum[0]) << 18
92 | u32::from(quantum[1]) << 12
93 | u32::from(quantum[2]) << 6
94 | u32::from(quantum[3]);
95 out.extend_from_slice(&[(v >> 16) as u8, (v >> 8) as u8, v as u8][..len - 1]);
96 if let Some(at) = trailing {
97 return Err(at);
98 }
99 }
100 Ok(out)
101}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct PemBlock {
106 pub kind: String,
107 pub headers: BTreeMap<String, String>,
108 pub bytes: Vec<u8>,
109}
110
111fn get_line(data: &[u8]) -> (&[u8], &[u8], usize) {
114 let (mut i, j) = match data.iter().position(|b| *b == b'\n') {
115 None => (data.len(), data.len()),
116 Some(i) => (i, i + 1),
117 };
118 if j > i && i > 0 && data[i - 1] == b'\r' {
119 i -= 1;
120 }
121 let mut line = &data[..i];
122 while let [rest @ .., b' ' | b'\t'] = line {
123 line = rest;
124 }
125 (line, &data[j..], j)
126}
127
128fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
129 haystack.windows(needle.len()).position(|w| w == needle)
130}
131
132fn rfind(haystack: &[u8], needle: &[u8]) -> Option<usize> {
133 haystack.windows(needle.len()).rposition(|w| w == needle)
134}
135
136pub fn pem_decode(data: &[u8]) -> Option<PemBlock> {
139 const START: &[u8] = b"\n-----BEGIN ";
140 const END: &[u8] = b"\n-----END ";
141 const EOL: &[u8] = b"-----";
142 let mut rest = data;
143 let mut end_trailer_index: isize = 0;
144 loop {
145 if end_trailer_index < 0 || end_trailer_index as usize > rest.len() {
146 return None;
147 }
148 rest = &rest[end_trailer_index as usize..];
149 let mut end_index = find(rest, END)? as isize;
150 end_trailer_index = end_index + END.len() as isize;
151 let Some(begin) = rfind(&rest[..end_index as usize], &START[1..]) else {
152 continue;
153 };
154 if begin > 0 && rest[begin - 1] != b'\n' {
155 continue;
156 }
157 let skip = (begin + START.len() - 1) as isize;
158 rest = &rest[skip as usize..];
159 end_index -= skip;
160 end_trailer_index -= skip;
161 let (type_line, next, consumed) = get_line(rest);
162 rest = next;
163 end_index -= consumed as isize;
164 end_trailer_index -= consumed as isize;
165 let Some(kind) = type_line.strip_suffix(EOL) else {
166 continue;
167 };
168 let mut headers = BTreeMap::new();
169 loop {
170 if rest.is_empty() {
171 return None;
172 }
173 let (line, next, consumed) = get_line(rest);
174 let Some(colon) = line.iter().position(|b| *b == b':') else {
175 break;
176 };
177 let key = String::from_utf8_lossy(trim_space(&line[..colon])).into_owned();
178 let value = String::from_utf8_lossy(trim_space(&line[colon + 1..])).into_owned();
179 headers.insert(key, value);
180 rest = next;
181 end_index -= consumed as isize;
182 end_trailer_index -= consumed as isize;
183 }
184 if !headers.is_empty() && end_index < 0 {
185 continue;
186 }
187 if end_trailer_index < 0 || end_trailer_index as usize > rest.len() {
188 continue;
189 }
190 let trailer = &rest[end_trailer_index as usize..];
191 let trailer_len = kind.len() + EOL.len();
192 if trailer.len() < trailer_len {
193 continue;
194 }
195 let (end_line, rest_of_end_line) = trailer.split_at(trailer_len);
196 if !end_line.starts_with(kind) || !end_line.ends_with(EOL) {
197 continue;
198 }
199 if !get_line(rest_of_end_line).0.is_empty() {
200 continue;
201 }
202 let mut bytes = Vec::new();
203 if end_index > 0 {
204 let body: Vec<u8> = rest[..end_index as usize]
205 .iter()
206 .copied()
207 .filter(|b| *b != b' ' && *b != b'\t')
208 .collect();
209 match std_decode(&body) {
210 Ok(decoded) => bytes = decoded,
211 Err(_) => continue,
212 }
213 }
214 return Some(PemBlock {
215 kind: String::from_utf8_lossy(kind).into_owned(),
216 headers,
217 bytes,
218 });
219 }
220}
221
222pub fn pem_encode(block: &PemBlock) -> String {
225 let mut out = format!("-----BEGIN {}-----\n", block.kind);
226 if !block.headers.is_empty() {
227 if let Some(value) = block.headers.get("Proc-Type") {
228 out.push_str(&format!("Proc-Type: {value}\n"));
229 }
230 for (key, value) in &block.headers {
231 if key != "Proc-Type" {
232 out.push_str(&format!("{key}: {value}\n"));
233 }
234 }
235 out.push('\n');
236 }
237 let encoded = base64::engine::general_purpose::STANDARD.encode(&block.bytes);
238 for line in encoded.as_bytes().chunks(64) {
239 out.push_str(std::str::from_utf8(line).unwrap_or_default());
240 out.push('\n');
241 }
242 out.push_str(&format!("-----END {}-----\n", block.kind));
243 out
244}
245
246fn strip_ansi(s: &str) -> String {
249 let input: Vec<char> = s.chars().collect();
250 let mut out = String::with_capacity(s.len());
251 let mut i = 0;
252 while i < input.len() {
253 let c = input[i];
254 if c != '\u{1b}' {
255 out.push(c);
256 i += 1;
257 continue;
258 }
259 if i + 1 >= input.len() {
260 i += 1;
261 continue;
262 }
263 match input[i + 1] {
264 ']' => {
265 i += 2;
266 while i < input.len() {
267 if input[i] == '\u{7}' {
268 break;
269 }
270 if input[i] == '\u{1b}' && i + 1 < input.len() && input[i + 1] == '\\' {
271 i += 1;
272 break;
273 }
274 i += 1;
275 }
276 }
277 '[' => {
278 i += 2;
279 while i < input.len() {
280 if input[i].is_ascii_alphabetic() {
281 break;
282 }
283 i += 1;
284 }
285 }
286 _ => {}
287 }
288 i += 1;
289 }
290 out
291}
292
293fn rebuild_pem(raw: &str) -> Result<String, String> {
296 let kind = if raw.contains("RSA PRIVATE KEY") {
297 "RSA PRIVATE KEY"
298 } else {
299 "PRIVATE KEY"
300 };
301 let header = format!("-----BEGIN {kind}-----");
302 let footer = format!("-----END {kind}-----");
303 let (Some(start), Some(end)) = (raw.find(&header), raw.find(&footer)) else {
304 return Err("missing pem markers".into());
305 };
306 if end <= start {
307 return Err("missing pem markers".into());
308 }
309 let payload: String = raw[start + header.len()..end]
310 .chars()
311 .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '/' | '='))
312 .collect();
313 if payload.is_empty() {
314 return Err("private_key base64 payload empty".into());
315 }
316 let der = std_decode(payload.as_bytes())
317 .map_err(|at| format!("private_key base64 decode failed: illegal base64 data at input byte {at}"))?;
318 Ok(pem_encode(&PemBlock {
319 kind: kind.into(),
320 headers: BTreeMap::new(),
321 bytes: der,
322 }))
323}
324
325fn pkcs1_der(rsa: &btls::rsa::Rsa<btls::pkey::Private>) -> Result<Vec<u8>, String> {
327 rsa.private_key_to_der().map_err(|e| e.to_string())
328}
329
330fn parse_pkcs1(der: &[u8]) -> Result<btls::rsa::Rsa<btls::pkey::Private>, String> {
335 const FAILED: &str = "x509: failed to parse private key";
336 let key = btls::rsa::Rsa::private_key_from_der(der).map_err(|_| FAILED)?;
337 if !single_sequence(der) {
340 return Err("asn1: syntax error: trailing data".into());
341 }
342 if !key.check_key().unwrap_or(false) {
343 return Err(FAILED.into());
344 }
345 Ok(key)
346}
347
348fn single_sequence(der: &[u8]) -> bool {
350 let [0x30, first, rest @ ..] = der else {
351 return false;
352 };
353 let (len, header) = match *first {
354 n if n < 0x80 => (usize::from(n), 2),
355 n @ 0x81..=0x84 => {
356 let k = usize::from(n & 0x7f);
357 let Some(bytes) = rest.get(..k) else {
358 return false;
359 };
360 (bytes.iter().fold(0usize, |a, b| a << 8 | usize::from(*b)), 2 + k)
361 }
362 _ => return false,
363 };
364 header.checked_add(len) == Some(der.len())
365}
366
367fn parse_pkcs8_rsa(der: &[u8]) -> Result<btls::rsa::Rsa<btls::pkey::Private>, bool> {
369 let key = btls::pkey::PKey::private_key_from_pkcs8(der).map_err(|_| false)?;
370 key.rsa().map_err(|_| true)
371}
372
373fn ensure_rsa(block: PemBlock) -> Result<PemBlock, String> {
377 let pkcs1 = |bytes: Vec<u8>| PemBlock {
378 kind: "RSA PRIVATE KEY".into(),
379 headers: BTreeMap::new(),
380 bytes,
381 };
382 match block.kind.as_str() {
383 "RSA PRIVATE KEY" => match parse_pkcs1(&block.bytes) {
384 Ok(_) => Ok(block),
385 Err(e) => Err(format!("private_key invalid rsa: {e}")),
386 },
387 "PRIVATE KEY" => match parse_pkcs8_rsa(&block.bytes) {
388 Ok(rsa) => Ok(pkcs1(pkcs1_der(&rsa)?)),
389 Err(true) => Err("private_key is not an RSA key".into()),
390 Err(false) => Err("private_key invalid pkcs8: x509: failed to parse private key".into()),
391 },
392 _ => {
393 if let Ok(rsa) = parse_pkcs1(&block.bytes) {
394 return Ok(pkcs1(pkcs1_der(&rsa)?));
395 }
396 if let Ok(rsa) = parse_pkcs8_rsa(&block.bytes) {
397 return Ok(pkcs1(pkcs1_der(&rsa)?));
398 }
399 Err("private_key uses unsupported format".into())
400 }
401 }
402}
403
404pub fn sanitize_private_key(raw: &str) -> Result<String, String> {
406 let pk = raw.replace("\r\n", "\n").replace('\r', "\n");
407 let pk = strip_ansi(&pk);
408 let pk = String::from_utf8_lossy(trim_space(pk.as_bytes())).into_owned();
409 let normalized = if pem_decode(pk.as_bytes()).is_some() {
410 pk
411 } else {
412 rebuild_pem(&pk).map_err(|e| format!("private_key is not valid pem: {e}"))?
413 };
414 let block = pem_decode(normalized.as_bytes()).ok_or("private_key pem decode failed")?;
415 Ok(pem_encode(&ensure_rsa(block)?))
416}
417
418pub fn normalize_service_account(sa: &Map<String, Value>) -> Result<Map<String, Value>, String> {
420 let pk = sa.get("private_key").and_then(Value::as_str).unwrap_or_default();
421 if pk.trim().is_empty() {
422 return Err("service account missing private_key".into());
423 }
424 let mut out = sa.clone();
425 out.insert("private_key".into(), Value::String(sanitize_private_key(pk)?));
426 Ok(out)
427}
428
429#[derive(Default)]
431struct KeyFile {
432 kind: String,
433 client_email: String,
434 private_key: String,
435 private_key_id: String,
436 token_uri: String,
437 audience: String,
438}
439
440impl KeyFile {
441 fn parse(sa: &Map<String, Value>) -> Result<Self, String> {
446 let sorted: BTreeMap<&String, &Value> = sa.iter().collect();
447 let data = serde_json::to_vec(&sorted).map_err(|e| e.to_string())?;
448 let mut f = Self::default();
449 let mut unused: [String; 13] = Default::default();
450 let mut unused = unused.iter_mut();
451 let mut other = || Slot::Str(unused.next().expect("one string per unread field"));
452 let mut fields = [
453 ("type", Slot::Str(&mut f.kind)),
454 ("client_email", Slot::Str(&mut f.client_email)),
455 ("private_key_id", Slot::Str(&mut f.private_key_id)),
456 ("private_key", Slot::Str(&mut f.private_key)),
457 ("auth_uri", other()),
458 ("token_uri", Slot::Str(&mut f.token_uri)),
459 ("project_id", other()),
460 ("universe_domain", other()),
461 ("client_secret", other()),
462 ("client_id", other()),
463 ("refresh_token", other()),
464 ("audience", Slot::Str(&mut f.audience)),
465 ("subject_token_type", other()),
466 ("token_url", other()),
467 ("token_info_url", other()),
468 ("service_account_impersonation_url", other()),
469 ("service_account_impersonation", Slot::Object),
470 ("delegates", Slot::Strs),
471 ("credential_source", Slot::Object),
472 ("quota_project_id", other()),
473 ("workforce_pool_user_project", other()),
474 ("revoke_url", other()),
475 ("source_credentials", Slot::Object),
476 ];
477 meta_wire::unmarshal(&data, "google", "credentialsFile", &mut fields)?;
478 Ok(f)
479 }
480}
481
482fn signing_key(pem: &str) -> Result<btls::pkey::PKey<btls::pkey::Private>, String> {
484 let der = pem_decode(pem.as_bytes()).map_or_else(|| pem.as_bytes().to_vec(), |b| b.bytes);
485 let rsa = match parse_pkcs8_rsa(&der) {
486 Ok(rsa) => rsa,
487 Err(true) => return Err("private key is invalid".into()),
488 Err(false) => parse_pkcs1(&der)
489 .map_err(|e| format!("private key should be a PEM or plain PKCS1 or PKCS8; parse error: {e}"))?,
490 };
491 btls::pkey::PKey::from_rsa(rsa).map_err(|e| e.to_string())
492}
493
494fn b64url(data: &[u8]) -> String {
495 base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(data)
496}
497
498fn quote(s: &str) -> String {
499 let mut out = Vec::new();
500 gj::marshal_str(&mut out, s.as_bytes(), true);
501 String::from_utf8(out).unwrap_or_default()
502}
503
504pub fn signed_assertion(key: &KeyFileView<'_>, now: i64) -> Result<String, String> {
507 let pkey = signing_key(key.private_key)?;
508 let mut header = String::from(r#"{"alg":"RS256","typ":"JWT""#);
509 if !key.private_key_id.is_empty() {
510 header.push_str(&format!(r#","kid":{}"#, quote(key.private_key_id)));
511 }
512 header.push('}');
513 let iat = now - 10;
514 let mut claims = format!(r#"{{"iss":{}"#, quote(key.client_email));
515 claims.push_str(&format!(r#","scope":{}"#, quote(SCOPE)));
516 claims.push_str(&format!(
517 r#","aud":{},"exp":{},"iat":{iat}}}"#,
518 quote(key.audience),
519 iat + 3600
520 ));
521 let signing_input = format!("{}.{}", b64url(header.as_bytes()), b64url(claims.as_bytes()));
522 let mut signer = btls::sign::Signer::new(btls::hash::MessageDigest::sha256(), &pkey).map_err(|e| e.to_string())?;
523 let signature = signer
524 .sign_oneshot_to_vec(signing_input.as_bytes())
525 .map_err(|e| e.to_string())?;
526 Ok(format!("{signing_input}.{}", b64url(&signature)))
527}
528
529pub struct KeyFileView<'a> {
531 pub client_email: &'a str,
532 pub private_key: &'a str,
533 pub private_key_id: &'a str,
534 pub audience: &'a str,
536}
537
538pub fn token_request(sa: &Map<String, Value>, now: i64) -> Result<(String, String), String> {
541 let file = KeyFile::parse(sa)?;
545 match file.kind.as_str() {
546 "service_account" => {}
547 "authorized_user" => return Err("oauth2: token expired and refresh token is not set".into()),
552 "" => return Err("missing 'type' field in credentials".into()),
553 other => return Err(format!("unknown credential type: {}", gostr_quote(other))),
554 }
555 let token_url = if file.token_uri.is_empty() {
556 DEFAULT_TOKEN_URL
557 } else {
558 file.token_uri.as_str()
559 };
560 let audience = if file.audience.is_empty() {
561 token_url
562 } else {
563 file.audience.as_str()
564 };
565 let assertion = signed_assertion(
566 &KeyFileView {
567 client_email: &file.client_email,
568 private_key: &file.private_key,
569 private_key_id: &file.private_key_id,
570 audience,
571 },
572 now,
573 )?;
574 let body = format!(
575 "assertion={}&grant_type={}",
576 cpa_common::gostr::query_escape(&assertion),
577 cpa_common::gostr::query_escape(GRANT_TYPE)
578 );
579 Ok((token_url.to_owned(), body))
580}
581
582pub async fn access_token(client: &wreq::Client, sa: &Map<String, Value>, now: i64) -> Result<String, String> {
585 let (token_url, body) = token_request(sa, now)?;
586 let token_url = token_url.as_str();
587 let mut headers = GoHeaders::new();
588 headers.set("Content-Type", "application/x-www-form-urlencoded");
589 let upstream = proxy::send(client, token_url, headers, Bytes::from(body), None)
590 .await
591 .map_err(|e| format!("oauth2: cannot fetch token: {}", String::from_utf8_lossy(&e.body)))?;
592 let status = upstream.status;
593 let data = proxy::read_all(upstream.body, 1 << 20, false)
594 .await
595 .map_err(|e| format!("oauth2: cannot fetch token: {}", String::from_utf8_lossy(&e.body)))?;
596 if !(200..=299).contains(&status) {
597 let reason = http::StatusCode::from_u16(status)
599 .ok()
600 .and_then(|s| s.canonical_reason())
601 .unwrap_or_default();
602 return Err(format!(
603 "oauth2: cannot fetch token: {status} {reason}\nResponse: {}",
604 String::from_utf8_lossy(&data)
605 ));
606 }
607 parse_token(&data)
608}
609
610fn gostr_quote(s: &str) -> String {
611 cpa_common::gostr::quote(s)
612}
613
614fn parse_token(data: &[u8]) -> Result<String, String> {
618 let (mut access, mut token_type, mut refresh, mut id_token, mut expires_in) = Default::default();
619 let mut fields = [
620 ("access_token", Slot::Str(&mut access)),
621 ("token_type", Slot::Str(&mut token_type)),
622 ("refresh_token", Slot::Str(&mut refresh)),
623 ("expiry", Slot::Time),
624 ("expires_in", Slot::Int(&mut expires_in, "int64")),
625 ("id_token", Slot::Str(&mut id_token)),
626 ];
627 meta_wire::unmarshal(data, "oauth2", "Token", &mut fields)
628 .map_err(|e| format!("oauth2: cannot fetch token: {e}"))?;
629 if !id_token.is_empty() {
630 decode_id_token(&id_token).map_err(|e| format!("oauth2: error decoding JWT token: {e}"))?;
631 }
632 Ok(access)
633}
634
635fn decode_id_token(token: &str) -> Result<(), String> {
639 let segments: Vec<&str> = token.split('.').collect();
640 if segments.len() != 3 {
641 return Err("jws: invalid token received".into());
642 }
643 let engine = base64::engine::GeneralPurpose::new(
644 &base64::alphabet::URL_SAFE,
645 base64::engine::GeneralPurposeConfig::new()
646 .with_decode_allow_trailing_bits(true)
647 .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone),
648 );
649 let claims: Vec<u8> = segments[1].bytes().filter(|b| *b != b'\r' && *b != b'\n').collect();
650 let claims = engine.decode(claims).map_err(|e| format!("illegal base64 data: {e}"))?;
651 let Some(start) = claims.iter().position(|c| !matches!(c, b' ' | b'\t' | b'\r' | b'\n')) else {
652 return Err("EOF".into());
653 };
654 let value = &claims[start..];
655 if value.starts_with(b"null") {
656 return Ok(());
657 }
658 if !value.starts_with(b"{") {
659 return Err("json: cannot unmarshal into Go value of type jws.ClaimSet".into());
661 }
662 let end = meta_wire::object_end(value).ok_or("unexpected EOF")?;
663 let (mut iss, mut scope, mut aud, mut typ, mut sub, mut prn) = Default::default();
664 let (mut exp, mut iat) = (0, 0);
665 let mut fields = [
666 ("iss", Slot::Str(&mut iss)),
667 ("scope", Slot::Str(&mut scope)),
668 ("aud", Slot::Str(&mut aud)),
669 ("exp", Slot::Int(&mut exp, "int64")),
670 ("iat", Slot::Int(&mut iat, "int64")),
671 ("typ", Slot::Str(&mut typ)),
672 ("sub", Slot::Str(&mut sub)),
673 ("prn", Slot::Str(&mut prn)),
674 ];
675 meta_wire::unmarshal(&value[..end], "jws", "ClaimSet", &mut fields)
676}
677
678fn file_part(s: &str) -> String {
680 s.trim().replace(['/', '\\', ':'], "_").replace(' ', "-")
681}
682
683fn label(project: &str, email: &str) -> String {
685 match (project.trim(), email.trim()) {
686 ("", "") => "vertex".into(),
687 (p, "") => p.into(),
688 ("", e) => e.into(),
689 (p, e) => format!("{p} ({e})"),
690 }
691}
692
693pub fn import(auth_dir: &Path, key_path: &str, prefix: &str) -> Result<PathBuf, String> {
696 let path = key_path.trim();
697 if path.is_empty() {
698 return Err("vertex-import: missing service account key path".into());
699 }
700 let data = std::fs::read(path).map_err(|e| {
701 format!(
702 "vertex-import: read file failed: {}",
703 path_error("open", Path::new(path), &e)
704 )
705 })?;
706 let sa = decode_service_account(&data)
707 .map_err(|e| format!("vertex-import: invalid service account json: {e}"))
708 .and_then(|sa| normalize_decoded(sa).map_err(|e| format!("vertex-import: {e}")))?;
709 let text = |sa: &BTreeMap<String, GoValue>, key: &str| match sa.get(key) {
710 Some(GoValue::String(s)) => s.clone(),
711 _ => String::new(),
712 };
713 let (email, project) = (text(&sa, "client_email"), text(&sa, "project_id"));
714 if project.trim().is_empty() {
715 return Err("vertex-import: project_id missing in service account json".into());
716 }
717 if email.trim().is_empty() {
718 tracing::warn!("vertex-import: client_email missing in service account json");
719 }
720 let prefix = prefix.trim().trim_matches('/');
721 if prefix.contains('/') {
722 return Err(format!(
723 "vertex-import: prefix must be a single segment (no '/' allowed): {}",
724 gostr_quote(prefix)
725 ));
726 }
727 let mut base = file_part(&project);
728 if !prefix.is_empty() {
729 base = format!("{}-{base}", file_part(prefix));
730 }
731 let mut doc = BTreeMap::new();
732 doc.insert("disabled".to_owned(), GoValue::Bool(false));
733 doc.insert("email".to_owned(), GoValue::String(email.clone()));
734 doc.insert("label".to_owned(), GoValue::String(label(&project, &email)));
735 doc.insert("location".to_owned(), GoValue::String("us-central1".into()));
736 doc.insert("prefix".to_owned(), GoValue::String(prefix.to_owned()));
737 doc.insert("project_id".to_owned(), GoValue::String(project));
738 doc.insert("service_account".to_owned(), GoValue::Object(sa));
739 doc.insert("type".to_owned(), GoValue::String("vertex".into()));
740 let out = go_clean(&auth_dir.join(format!("vertex-{base}.json")));
743 let save_failed = |e: String| format!("vertex-import: save credential failed: {e}");
744 let dir = out.parent().unwrap_or(Path::new("."));
745 go_mkdir_all(dir).map_err(|e| save_failed(format!("auth filestore: create dir failed: {e}")))?;
746 println!("Saving credentials to {}", out.display());
748 crate::kimi_auth::write_private(&out, &GoValue::Object(doc).encode_indented()).map_err(|e| {
750 save_failed(format!(
751 "vertex credential: create file failed: {}",
752 path_error("open", &out, &e)
753 ))
754 })?;
755 Ok(out)
756}
757
758fn decode_service_account(data: &[u8]) -> Result<Option<BTreeMap<String, GoValue>>, String> {
760 match GoValue::parse_f64(data).map(GoValue::into_object) {
762 Some(Ok(sa)) => Ok(Some(sa)),
763 Some(Err(GoValue::Null)) => Ok(None),
765 Some(Err(other)) => {
766 let kind = match other {
767 GoValue::Array(_) => "array",
768 GoValue::String(_) => "string",
769 GoValue::Bool(_) => "bool",
770 _ => "number",
771 };
772 Err(format!(
773 "json: cannot unmarshal {kind} into Go value of type map[string]interface {{}}"
774 ))
775 }
776 None => Err(meta_wire::check_valid(data).err().unwrap_or_else(|| {
778 let number = first_overflow(&gj::parse(data)).unwrap_or_default();
779 format!("json: cannot unmarshal number {number} into Go value of type float64")
780 })),
781 }
782}
783
784fn normalize_decoded(sa: Option<BTreeMap<String, GoValue>>) -> Result<BTreeMap<String, GoValue>, String> {
786 let mut sa = sa.ok_or("service account payload is empty")?;
787 let private_key = match sa.get("private_key") {
788 Some(GoValue::String(s)) => s.clone(),
789 _ => String::new(),
790 };
791 if private_key.trim().is_empty() {
792 return Err("service account missing private_key".into());
793 }
794 sa.insert(
795 "private_key".into(),
796 GoValue::String(sanitize_private_key(&private_key)?),
797 );
798 Ok(sa)
799}
800
801#[derive(Debug)]
804pub struct Upload {
805 pub path: PathBuf,
806 pub project: String,
807 pub email: String,
808 pub location: String,
809 contents: Vec<u8>,
810}
811
812impl Upload {
813 pub fn write(&self) -> Result<(), UploadError> {
815 let dir = self.path.parent().unwrap_or(Path::new("."));
816 go_mkdir_all(dir).map_err(|e| UploadError::Save(format!("auth filestore: create dir failed: {e}")))?;
817 crate::kimi_auth::write_private(&self.path, &self.contents).map_err(|e| {
818 UploadError::Save(format!(
819 "vertex credential: create file failed: {}",
820 path_error("open", &self.path, &e)
821 ))
822 })
823 }
824}
825
826#[derive(Debug)]
828pub enum UploadError {
829 InvalidJson(String),
830 InvalidServiceAccount(String),
831 ProjectMissing,
832 Save(String),
833}
834
835pub fn import_upload(auth_dir: &Path, data: &[u8], location: &str) -> Result<Upload, UploadError> {
841 let sa = decode_service_account(data).map_err(UploadError::InvalidJson)?;
842 let sa = normalize_decoded(sa).map_err(UploadError::InvalidServiceAccount)?;
843 let text = |key: &str| match sa.get(key) {
846 None | Some(GoValue::Null) => String::new(),
847 Some(value) => go_sprint(value).trim().to_owned(),
848 };
849 let (project, email) = (text("project_id"), text("client_email"));
850 if project.is_empty() {
851 return Err(UploadError::ProjectMissing);
852 }
853 let location = match location.trim() {
854 "" => "us-central1".to_owned(),
855 l => l.to_owned(),
856 };
857 let mut doc = BTreeMap::new();
858 doc.insert("email".to_owned(), GoValue::String(email.clone()));
859 doc.insert("label".to_owned(), GoValue::String(label(&project, &email)));
860 doc.insert("location".to_owned(), GoValue::String(location.clone()));
861 doc.insert("project_id".to_owned(), GoValue::String(project.clone()));
862 doc.insert("service_account".to_owned(), GoValue::Object(sa));
863 doc.insert("type".to_owned(), GoValue::String("vertex".into()));
864 let path = go_clean(&auth_dir.join(format!("vertex-{}.json", file_part(&project))));
865 let disabled = std::fs::read(&path)
866 .ok()
867 .and_then(|raw| crate::meta_auth::go_map_from_json(&raw))
868 .and_then(|(existing, _)| crate::meta_auth::merge_existing(&mut doc, &existing, "vertex"))
869 .unwrap_or(false);
870 crate::meta_auth::normalize_metadata(&mut doc);
871 if let Some(weight) = doc.get("weight") {
872 let json: Value = serde_json::from_slice(&weight.marshal()).unwrap_or(Value::Null);
873 cpa_core::config::credentials::parse_weight(&json)
874 .map_err(|e| UploadError::Save(format!("auth filestore: invalid metadata weight: {e}")))?;
875 }
876 doc.insert("disabled".to_owned(), GoValue::Bool(disabled));
877 Ok(Upload {
878 path,
879 project,
880 email,
881 location,
882 contents: GoValue::Object(doc).encode_indented(),
883 })
884}
885
886fn go_sprint(value: &GoValue) -> String {
889 match value {
890 GoValue::Null => "<nil>".into(),
891 GoValue::Bool(b) => b.to_string(),
892 GoValue::String(s) => s.clone(),
893 GoValue::Number(n) => crate::xai_auth::go_float_v(cpa_common::json::parse_float(n.as_bytes())),
894 GoValue::Array(items) => format!("[{}]", items.iter().map(go_sprint).collect::<Vec<_>>().join(" ")),
895 GoValue::Object(map) => {
896 let items: Vec<String> = map.iter().map(|(k, v)| format!("{k}:{}", go_sprint(v))).collect();
897 format!("map[{}]", items.join(" "))
898 }
899 }
900}
901
902fn first_overflow(value: &gj::Res<'_>) -> Option<String> {
904 if value.kind == gj::Kind::Number {
905 let raw = String::from_utf8_lossy(value.raw()).into_owned();
906 return gj::go_parse_float(value.raw()).is_err().then_some(raw);
907 }
908 let mut found = None;
909 if value.kind == gj::Kind::Json {
910 value.each(|_, item| {
911 found = first_overflow(&item);
912 found.is_none()
913 });
914 }
915 found
916}
917
918fn path_error(op: &str, path: &Path, e: &std::io::Error) -> String {
920 let text = e.to_string();
922 let text = text.split(" (os error").next().unwrap_or_default();
923 let mut chars = text.chars();
924 let errno: String = chars
925 .next()
926 .map_or_else(String::new, |c| c.to_lowercase().chain(chars).collect());
927 format!("{op} {}: {errno}", path.display())
928}
929
930fn go_clean(path: &Path) -> PathBuf {
933 use std::path::Component;
934 let mut out: Vec<Component<'_>> = Vec::new();
935 for component in path.components() {
936 match component {
937 Component::CurDir => {}
938 Component::ParentDir => match out.last() {
939 Some(Component::Normal(_)) => {
940 out.pop();
941 }
942 Some(Component::RootDir | Component::Prefix(_)) => {}
943 _ => out.push(component),
944 },
945 other => out.push(other),
946 }
947 }
948 if out.is_empty() {
949 return PathBuf::from(".");
950 }
951 out.iter().collect()
952}
953
954fn go_mkdir_all(dir: &Path) -> Result<(), String> {
958 match std::fs::metadata(dir) {
959 Ok(meta) if meta.is_dir() => return Ok(()),
960 Ok(_) => return Err(format!("mkdir {}: not a directory", dir.display())),
961 Err(_) => {}
962 }
963 if let Some(parent) = dir.parent().filter(|p| !p.as_os_str().is_empty()) {
964 go_mkdir_all(parent)?;
965 }
966 #[cfg(unix)]
967 let builder = {
968 let mut builder = std::fs::DirBuilder::new();
969 std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
970 builder
971 };
972 #[cfg(not(unix))]
973 let builder = std::fs::DirBuilder::new();
974 match builder.create(dir) {
975 Ok(()) => Ok(()),
976 Err(_) if std::fs::symlink_metadata(dir).is_ok_and(|m| m.is_dir()) => Ok(()),
977 Err(e) => Err(path_error("mkdir", dir, &e)),
978 }
979}