Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ func parseImpersonationChain(chain string) (string, []string) {

const iamLoginScope = "https://www.googleapis.com/auth/sqlservice.login"

// iamAuthNEnabled returns true if IAM authentication is enabled globally
// or for any instance in the configuration.
func (c *Config) iamAuthNEnabled() bool {
if c.IAMAuthN {
return true
}
for _, inst := range c.Instances {
if inst.IAMAuthN != nil && *inst.IAMAuthN {
return true
}
}
return false
}

func credentialsOpt(c Config, l cloudsql.Logger) (cloudsqlconn.Option, error) {
// If service account impersonation is configured, set up an impersonated
// credentials token source.
Expand Down Expand Up @@ -363,7 +377,8 @@ func credentialsOpt(c Config, l cloudsql.Logger) (cloudsqlconn.Option, error) {
if err != nil {
return nil, err
}
if c.IAMAuthN {

if c.iamAuthNEnabled() {
iamLoginTS, err := impersonate.CredentialsTokenSource(
context.Background(),
impersonate.CredentialsConfig{
Expand Down Expand Up @@ -439,7 +454,7 @@ func (c *Config) DialerOptions(l cloudsql.Logger) ([]cloudsqlconn.Option, error)
opts = append(opts, cloudsqlconn.WithUniverseDomain(c.UniverseDomain))
}

if c.IAMAuthN {
if c.iamAuthNEnabled() {
opts = append(opts, cloudsqlconn.WithIAMAuthN())
}

Expand Down
8 changes: 8 additions & 0 deletions tests/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ func TestPostgresIAMDBAuthn(t *testing.T) {
dsn: fmt.Sprintf("host=localhost user=%s database=%s sslmode=disable",
impersonatedIAMUser, *postgresDB),
},
{
desc: "using impersonation with query param",
args: []string{
"--impersonate-service-account", *impersonatedUser,
fmt.Sprintf("%s?auto-iam-authn=true", *postgresConnName)},
dsn: fmt.Sprintf("host=localhost user=%s password=password database=%s sslmode=disable",
impersonatedIAMUser, *postgresDB),
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
Expand Down
Loading