DoVertexImport imports a Google Cloud service account key JSON and persists it as a "vertex" provider credential. The file content is embedded in the auth file to allow portable deployment across stores.
(cfg *config.Config, keyPath string, prefix string)
| 21 | // it as a "vertex" provider credential. The file content is embedded in the auth |
| 22 | // file to allow portable deployment across stores. |
| 23 | func DoVertexImport(cfg *config.Config, keyPath string, prefix string) { |
| 24 | if cfg == nil { |
| 25 | cfg = &config.Config{} |
| 26 | } |
| 27 | if resolved, errResolve := util.ResolveAuthDir(cfg.AuthDir); errResolve == nil { |
| 28 | cfg.AuthDir = resolved |
| 29 | } |
| 30 | rawPath := strings.TrimSpace(keyPath) |
| 31 | if rawPath == "" { |
| 32 | log.Errorf("vertex-import: missing service account key path") |
| 33 | return |
| 34 | } |
| 35 | data, errRead := os.ReadFile(rawPath) |
| 36 | if errRead != nil { |
| 37 | log.Errorf("vertex-import: read file failed: %v", errRead) |
| 38 | return |
| 39 | } |
| 40 | var sa map[string]any |
| 41 | if errUnmarshal := json.Unmarshal(data, &sa); errUnmarshal != nil { |
| 42 | log.Errorf("vertex-import: invalid service account json: %v", errUnmarshal) |
| 43 | return |
| 44 | } |
| 45 | // Validate and normalize private_key before saving |
| 46 | normalizedSA, errFix := vertex.NormalizeServiceAccountMap(sa) |
| 47 | if errFix != nil { |
| 48 | log.Errorf("vertex-import: %v", errFix) |
| 49 | return |
| 50 | } |
| 51 | sa = normalizedSA |
| 52 | email, _ := sa["client_email"].(string) |
| 53 | projectID, _ := sa["project_id"].(string) |
| 54 | if strings.TrimSpace(projectID) == "" { |
| 55 | log.Errorf("vertex-import: project_id missing in service account json") |
| 56 | return |
| 57 | } |
| 58 | if strings.TrimSpace(email) == "" { |
| 59 | // Keep empty email but warn |
| 60 | log.Warn("vertex-import: client_email missing in service account json") |
| 61 | } |
| 62 | // Default location if not provided by user. Can be edited in the saved file later. |
| 63 | location := "us-central1" |
| 64 | |
| 65 | // Normalize and validate prefix: must be a single segment (no "/" allowed). |
| 66 | prefix = strings.TrimSpace(prefix) |
| 67 | prefix = strings.Trim(prefix, "/") |
| 68 | if prefix != "" && strings.Contains(prefix, "/") { |
| 69 | log.Errorf("vertex-import: prefix must be a single segment (no '/' allowed): %q", prefix) |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | // Include prefix in filename so importing the same project with different |
| 74 | // prefixes creates separate credential files instead of overwriting. |
| 75 | baseName := sanitizeFilePart(projectID) |
| 76 | if prefix != "" { |
| 77 | baseName = sanitizeFilePart(prefix) + "-" + baseName |
| 78 | } |
| 79 | fileName := fmt.Sprintf("vertex-%s.json", baseName) |
| 80 | // Build auth record |
no test coverage detected