MCPcopy Create free account
hub / github.com/bytebase/bytebase / GCPCredentialOption

Function GCPCredentialOption

backend/plugin/db/util/connection.go:74–95  ·  view source on GitHub ↗

GCPCredentialOption returns the appropriate option.ClientOption for the given GCP credential JSON, supporting both service account keys and external account (Workload Identity Federation) configurations. SECURITY NOTE: ExternalAccount and ImpersonatedServiceAccount credential types do not validate

(credJSON []byte)

Source from the content-addressed store, hash-verified

72// do not validate the credential configuration. Malicious URLs in the config could
73// pose a security risk. See https://cloud.google.com/docs/authentication/external/externally-sourced-credentials.
74func GCPCredentialOption(credJSON []byte) (option.ClientOption, error) {
75 var cred struct {
76 Type string `json:"type"`
77 }
78 if err := json.Unmarshal(credJSON, &cred); err != nil {
79 return nil, errors.Wrap(err, "failed to parse GCP credential JSON")
80 }
81 switch cred.Type {
82 case "service_account":
83 return option.WithAuthCredentialsJSON(option.ServiceAccount, credJSON), nil
84 case "external_account":
85 return option.WithAuthCredentialsJSON(option.ExternalAccount, credJSON), nil
86 case "impersonated_service_account":
87 return option.WithAuthCredentialsJSON(option.ImpersonatedServiceAccount, credJSON), nil
88 case "authorized_user":
89 return option.WithAuthCredentialsJSON(option.AuthorizedUser, credJSON), nil
90 case "":
91 return nil, errors.Errorf("GCP credential JSON missing \"type\" field")
92 default:
93 return nil, errors.Errorf("unsupported GCP credential type: %q", cred.Type)
94 }
95}
96
97func GetAzureConnectionConfig(connCfg db.ConnectionConfig) (azcore.TokenCredential, error) {
98 if azureCredential := connCfg.DataSource.GetAzureCredential(); azureCredential != nil {

Callers 3

OpenMethod · 0.92
OpenMethod · 0.92
TestGCPCredentialOptionFunction · 0.85

Calls 2

UnmarshalMethod · 0.80
ErrorfMethod · 0.80

Tested by 1

TestGCPCredentialOptionFunction · 0.68