NormalizeServiceAccountMap returns a copy of the given service account map with a sanitized private_key field that is guaranteed to contain a valid RSA PRIVATE KEY PEM block.
(sa map[string]any)
| 35 | // NormalizeServiceAccountMap returns a copy of the given service account map with |
| 36 | // a sanitized private_key field that is guaranteed to contain a valid RSA PRIVATE KEY PEM block. |
| 37 | func NormalizeServiceAccountMap(sa map[string]any) (map[string]any, error) { |
| 38 | if sa == nil { |
| 39 | return nil, fmt.Errorf("service account payload is empty") |
| 40 | } |
| 41 | pk, _ := sa["private_key"].(string) |
| 42 | if strings.TrimSpace(pk) == "" { |
| 43 | return nil, fmt.Errorf("service account missing private_key") |
| 44 | } |
| 45 | normalized, err := sanitizePrivateKey(pk) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | clone := make(map[string]any, len(sa)) |
| 50 | for k, v := range sa { |
| 51 | clone[k] = v |
| 52 | } |
| 53 | clone["private_key"] = normalized |
| 54 | return clone, nil |
| 55 | } |
| 56 | |
| 57 | func sanitizePrivateKey(raw string) (string, error) { |
| 58 | pk := strings.ReplaceAll(raw, "\r\n", "\n") |
no test coverage detected