Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: support Workload Identity Federation credentials for BigQuery and Spanner (#19564)

Vincent Huang49c5770e6e1f
diff --git a/backend/plugin/db/bigquery/bigquery.go b/backend/plugin/db/bigquery/bigquery.go
index 98b1d04362..ff10cb060f 100644
--- a/backend/plugin/db/bigquery/bigquery.go
+++ b/backend/plugin/db/bigquery/bigquery.go
@@ -57,7 +57,11 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
var o []option.ClientOption
if gcpCredential := config.DataSource.GetGcpCredential(); gcpCredential != nil {
- o = append(o, option.WithAuthCredentialsJSON(option.ServiceAccount, []byte(gcpCredential.Content)))
+ credOption, err := util.GCPCredentialOption([]byte(gcpCredential.Content))
+ if err != nil {
+ return nil, err
+ }
+ o = append(o, credOption)
}
client, err := bigquery.NewClient(ctx, config.DataSource.Host, o...)
if err != nil {
diff --git a/backend/plugin/db/spanner/spanner.go b/backend/plugin/db/spanner/spanner.go
index 9b24e36b80..595ac0e955 100644
--- a/backend/plugin/db/spanner/spanner.go
+++ b/backend/plugin/db/spanner/spanner.go
@@ -71,7 +71,11 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
var o []option.ClientOption
if gcpCredential := config.DataSource.GetGcpCredential(); gcpCredential != nil {
- o = append(o, option.WithAuthCredentialsJSON(option.ServiceAccount, []byte(gcpCredential.Content)))
+ credOption, err := util.GCPCredentialOption([]byte(gcpCredential.Content))
+ if err != nil {
+ return nil, err
+ }
+ o = append(o, credOption)
}
if config.ConnectionContext.DatabaseName != "" {
d.databaseName = config.ConnectionContext.DatabaseName
diff --git a/backend/plugin/db/util/connection.go b/backend/plugin/db/util/connection.go
index 96227277a8..8c8092aec6 100644
--- a/backend/plugin/db/util/connection.go
+++ b/backend/plugin/db/util/connection.go
@@ -3,6 +3,7 @@ package util
import (
"context"
+ "encoding/json"
"cloud.google.com/go/cloudsqlconn"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
@@ -11,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/pkg/errors"
+ "google.golang.org/api/option"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -47,6 +49,36 @@ func GetGCPConnectionConfig(ctx context.Context, connCfg db.ConnectionConfig) (*
return cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithIAMAuthN())
}
+// 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 the credential configuration. Malicious URLs in the config could
+// pose a security risk. See https://cloud.google.com/docs/authentication/external/externally-sourced-credentials.
+func GCPCredentialOption(credJSON []byte) (option.ClientOption, error) {
+ var cred struct {
+ Type string `json:"type"`
+ }
+ if err := json.Unmarshal(credJSON, &cred); err != nil {
+ return nil, errors.Wrap(err, "failed to parse GCP credential JSON")
+ }
+ switch cred.Type {
+ case "service_account":
+ return option.WithAuthCredentialsJSON(option.ServiceAccount, credJSON), nil
+ case "external_account":
+ return option.WithAuthCredentialsJSON(option.ExternalAccount, credJSON), nil
+ case "impersonated_service_account":
+ return option.WithAuthCredentialsJSON(option.ImpersonatedServiceAccount, credJSON), nil
+ case "authorized_user":
+ return option.WithAuthCredentialsJSON(option.AuthorizedUser, credJSON), nil
+ case "":
+ return nil, errors.Errorf("GCP credential JSON missing \"type\" field")
+ default:
+ return nil, errors.Errorf("unsupported GCP credential type: %q", cred.Type)
+ }
+}
+
func GetAzureConnectionConfig(connCfg db.ConnectionConfig) (azcore.TokenCredential, error) {
if azureCredential := connCfg.DataSource.GetAzureCredential(); azureCredential != nil {
c, err := azidentity.NewClientSecretCredential(azureCredential.TenantId, azureCredential.ClientId, azureCredential.ClientSecret, nil)
diff --git a/backend/plugin/db/util/connection_test.go b/backend/plugin/db/util/connection_test.go
new file mode 100644
index 0000000000..44e9553bfa
--- /dev/null
+++ b/backend/plugin/db/util/connection_test.go
@@ -0,0 +1,68 @@
+//nolint:revive
+package util
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestGCPCredentialOption(t *testing.T) {
+ tests := []struct {
+ name string
+ json string
+ wantErr string
+ }{
+ {
+ name: "service_account",
+ json: `{"type": "service_account", "project_id": "test"}`,
+ },
+ {
+ name: "external_account",
+ json: `{"type": "external_account", "audience": "test"}`,
+ },
+ {
+ name: "impersonated_service_account",
+ json: `{"type": "impersonated_service_account"}`,
+ },
+ {
+ name: "authorized_user",
+ json: `{"type": "authorized_user"}`,
+ },
+ {
+ name: "invalid JSON",
+ json: `not json`,
+ wantErr: "failed to parse GCP credential JSON",
+ },
+ {
+ name: "missing type field",
+ json: `{"project_id": "test"}`,
+ wantErr: `missing "type" field`,
+ },
+ {
+ name: "unsupported type",
+ json: `{"type": "unknown_type"}`,
+ wantErr: `unsupported GCP credential type: "unknown_type"`,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := GCPCredentialOption([]byte(tc.json))
+ if tc.wantErr != "" {
+ if err == nil {
+ t.Fatalf("expected error containing %q, got nil", tc.wantErr)
+ }
+ if !strings.Contains(err.Error(), tc.wantErr) {
+ t.Fatalf("expected error containing %q, got %q", tc.wantErr, err.Error())
+ }
+ return
+ }
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if got == nil {
+ t.Fatal("expected non-nil ClientOption")
+ }
+ })
+ }
+}