Skip to content

Commit b2e0625

Browse files
committed
fix(gcp): detect credential type before requesting token source
Switch from CredentialsFromJSONWithParams to CredentialsFromJSONWithType to properly handle different GCP credential formats. Add credential type detection helper to parse the JSON and extract the credential type field.
1 parent 2be1448 commit b2e0625

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

connection/gcp.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package connection
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67
"time"
@@ -45,14 +46,32 @@ func (t *GCPConnection) FromModel(connection models.Connection) {
4546
}
4647

4748
func (g *GCPConnection) TokenSource(ctx context.Context, scopes ...string) (oauth2.TokenSource, error) {
48-
creds, err := google.CredentialsFromJSONWithParams(ctx, []byte(g.Credentials.ValueStatic), google.CredentialsParams{Scopes: scopes})
49+
credType, err := detectCredentialType([]byte(g.Credentials.ValueStatic))
50+
if err != nil {
51+
return nil, fmt.Errorf("detecting credential type: %w", err)
52+
}
53+
54+
creds, err := google.CredentialsFromJSONWithType(ctx, []byte(g.Credentials.ValueStatic), credType, scopes...)
4955
if err != nil {
5056
return nil, err
5157
}
5258

5359
return creds.TokenSource, nil
5460
}
5561

62+
func detectCredentialType(jsonData []byte) (google.CredentialsType, error) {
63+
var f struct {
64+
Type string `json:"type"`
65+
}
66+
if err := json.Unmarshal(jsonData, &f); err != nil {
67+
return "", fmt.Errorf("parsing credentials JSON: %w", err)
68+
}
69+
if f.Type == "" {
70+
return google.ServiceAccount, nil
71+
}
72+
return google.CredentialsType(f.Type), nil
73+
}
74+
5675
func (g *GCPConnection) Validate() *GCPConnection {
5776
if g == nil {
5877
return &GCPConnection{}

0 commit comments

Comments
 (0)