Skip to content

Commit 594e083

Browse files
authored
feat: Configure the connector with Google Auth Credentials. (#997)
1 parent 08fbbb7 commit 594e083

File tree

6 files changed

+196
-70
lines changed

6 files changed

+196
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# IDEs
22
.vscode/
33
.idea/
4+
*.iml
45

56
# direnv
67
.envrc

dialer_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import (
2929
"testing"
3030
"time"
3131

32+
"cloud.google.com/go/auth"
3233
"cloud.google.com/go/cloudsqlconn/errtype"
3334
"cloud.google.com/go/cloudsqlconn/instance"
3435
"cloud.google.com/go/cloudsqlconn/internal/cloudsql"
3536
"cloud.google.com/go/cloudsqlconn/internal/mock"
37+
3638
"golang.org/x/oauth2"
3739
)
3840

@@ -674,6 +676,40 @@ func TestTokenSourceWithIAMAuthN(t *testing.T) {
674676
}
675677
}
676678

679+
func TestCredentialsWithIAMAuthN(t *testing.T) {
680+
ts := &auth.Credentials{}
681+
tcs := []struct {
682+
desc string
683+
opts []Option
684+
wantErr bool
685+
}{
686+
{
687+
desc: "when token source is set with IAM AuthN",
688+
opts: []Option{WithCredentials(ts), WithIAMAuthN()},
689+
wantErr: true,
690+
},
691+
{
692+
desc: "when IAM AuthN token source is set without IAM AuthN",
693+
opts: []Option{WithIAMAuthNCredentials(ts, ts)},
694+
wantErr: true,
695+
},
696+
{
697+
desc: "when IAM AuthN token source is set with IAM AuthN",
698+
opts: []Option{WithIAMAuthNCredentials(ts, ts), WithIAMAuthN()},
699+
wantErr: false,
700+
},
701+
}
702+
for _, tc := range tcs {
703+
t.Run(tc.desc, func(t *testing.T) {
704+
_, err := NewDialer(context.Background(), tc.opts...)
705+
gotErr := err != nil
706+
if tc.wantErr != gotErr {
707+
t.Fatalf("err: want = %v, got = %v", tc.wantErr, gotErr)
708+
}
709+
})
710+
}
711+
}
712+
677713
func TestDialerRemovesInvalidInstancesFromCache(t *testing.T) {
678714
// When a dialer attempts to retrieve connection info for a
679715
// non-existent instance, it should delete the instance from

e2e_postgres_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"testing"
2828
"time"
2929

30+
"cloud.google.com/go/auth"
31+
"cloud.google.com/go/auth/credentials"
3032
"cloud.google.com/go/cloudsqlconn"
3133
"github.com/jackc/pgx/v5/pgxpool"
3234
"golang.org/x/oauth2"
@@ -749,19 +751,25 @@ func TestPostgresV4Hook(t *testing.T) {
749751
// removeAuthEnvVar retrieves an OAuth2 token and a path to a service account key
750752
// and then unsets GOOGLE_APPLICATION_CREDENTIALS. It returns a cleanup function
751753
// that restores the original setup.
752-
func removeAuthEnvVar(t *testing.T) (*oauth2.Token, string, func()) {
754+
func removeAuthEnvVar(t *testing.T) (*oauth2.Token, *auth.Credentials, string, func()) {
753755
ts, err := google.DefaultTokenSource(context.Background(),
754756
"https://www.googleapis.com/auth/cloud-platform",
755757
)
756758
if err != nil {
757759
t.Errorf("failed to resolve token source: %v", err)
758760
}
761+
762+
creds, err := credentials.DetectDefault(&credentials.DetectOptions{Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"}})
763+
if err != nil {
764+
t.Errorf("failed to resolve auth credentials: %v", err)
765+
}
766+
759767
tok, err := ts.Token()
760768
if err != nil {
761769
t.Errorf("failed to get token: %v", err)
762770
}
763771
if ipType == "private" {
764-
return tok, "", func() {}
772+
return tok, creds, "", func() {}
765773
}
766774
path, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
767775
if !ok {
@@ -770,7 +778,7 @@ func removeAuthEnvVar(t *testing.T) (*oauth2.Token, string, func()) {
770778
if err := os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS"); err != nil {
771779
t.Fatalf("failed to unset GOOGLE_APPLICATION_CREDENTIALS")
772780
}
773-
return tok, path, func() {
781+
return tok, creds, path, func() {
774782
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", path)
775783
}
776784
}
@@ -798,7 +806,7 @@ func TestPostgresAuthentication(t *testing.T) {
798806
creds = keyfile(t)
799807
}
800808
opts = addIPTypeOptions(opts)
801-
tok, path, cleanup := removeAuthEnvVar(t)
809+
tok, authCreds, path, cleanup := removeAuthEnvVar(t)
802810
defer cleanup()
803811

804812
tcs := []struct {
@@ -811,6 +819,10 @@ func TestPostgresAuthentication(t *testing.T) {
811819
oauth2.StaticTokenSource(tok),
812820
)),
813821
},
822+
{
823+
desc: "with auth credentials",
824+
opts: append(opts, cloudsqlconn.WithCredentials(authCreds)),
825+
},
814826
}
815827

816828
if ipType != "private" {

go.mod

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,51 @@ module cloud.google.com/go/cloudsqlconn
33
go 1.23.0
44

55
require (
6-
cloud.google.com/go/auth v0.16.2
6+
cloud.google.com/go/auth v0.16.4
77
cloud.google.com/go/auth/oauth2adapt v0.2.8
88
github.com/go-sql-driver/mysql v1.9.3
99
github.com/google/uuid v1.6.0
1010
github.com/jackc/pgx/v4 v4.18.3
1111
github.com/jackc/pgx/v5 v5.7.5
1212
github.com/microsoft/go-mssqldb v1.9.2
1313
go.opencensus.io v0.24.0
14-
golang.org/x/net v0.42.0
14+
golang.org/x/net v0.43.0
1515
golang.org/x/oauth2 v0.30.0
1616
golang.org/x/time v0.12.0
17-
google.golang.org/api v0.241.0
18-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7
19-
google.golang.org/grpc v1.73.0
17+
google.golang.org/api v0.246.0
18+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250804133106-a7a43d27e69b
19+
google.golang.org/grpc v1.74.2
2020
)
2121

2222
require (
23-
cloud.google.com/go/compute/metadata v0.7.0 // indirect
23+
cloud.google.com/go/compute/metadata v0.8.0 // indirect
2424
filippo.io/edwards25519 v1.1.0 // indirect
2525
github.com/felixge/httpsnoop v1.0.4 // indirect
26-
github.com/go-logr/logr v1.4.2 // indirect
26+
github.com/go-logr/logr v1.4.3 // indirect
2727
github.com/go-logr/stdr v1.2.2 // indirect
2828
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
2929
github.com/golang-sql/sqlexp v0.1.0 // indirect
30-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
30+
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
3131
github.com/google/s2a-go v0.1.9 // indirect
3232
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
33-
github.com/googleapis/gax-go/v2 v2.14.2 // indirect
33+
github.com/googleapis/gax-go/v2 v2.15.0 // indirect
3434
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
3535
github.com/jackc/pgconn v1.14.3 // indirect
3636
github.com/jackc/pgio v1.0.0 // indirect
3737
github.com/jackc/pgpassfile v1.0.0 // indirect
3838
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
3939
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
40-
github.com/jackc/pgtype v1.14.0 // indirect
40+
github.com/jackc/pgtype v1.14.4 // indirect
4141
github.com/jackc/puddle/v2 v2.2.2 // indirect
4242
github.com/pkg/errors v0.9.1 // indirect
4343
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
44-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
45-
go.opentelemetry.io/otel v1.36.0 // indirect
46-
go.opentelemetry.io/otel/metric v1.36.0 // indirect
47-
go.opentelemetry.io/otel/trace v1.36.0 // indirect
48-
golang.org/x/crypto v0.40.0 // indirect
44+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0 // indirect
45+
go.opentelemetry.io/otel v1.37.0 // indirect
46+
go.opentelemetry.io/otel/metric v1.37.0 // indirect
47+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
48+
golang.org/x/crypto v0.41.0 // indirect
4949
golang.org/x/sync v0.16.0 // indirect
50-
golang.org/x/sys v0.34.0 // indirect
51-
golang.org/x/text v0.27.0 // indirect
52-
google.golang.org/protobuf v1.36.6 // indirect
50+
golang.org/x/sys v0.35.0 // indirect
51+
golang.org/x/text v0.28.0 // indirect
52+
google.golang.org/protobuf v1.36.7 // indirect
5353
)

0 commit comments

Comments
 (0)