Skip to content

Commit

Permalink
Adjust priority of quota project sources
Browse files Browse the repository at this point in the history
'GOOGLE_CLOUD_QUOTA_PROJECT' env variable should override quota project
setting from credentials file. This is a behavior of previous auth code:
https://github.com/googleapis/google-api-go-client/blob/main/internal/creds.go#L242-L261
  • Loading branch information
michalskalski committed Sep 2, 2024
1 parent 633dc86 commit e4f3cfc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions auth/credentials/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ type DetectOptions struct {
// The default value is "googleapis.com". This option is ignored for
// authentication flows that do not support universe domain. Optional.
UniverseDomain string
// QuotaProjectID allow overriding the project ID used for quota management.
// Optional.
QuotaProjectID string
}

func (o *DetectOptions) validate() error {
Expand Down
21 changes: 18 additions & 3 deletions auth/credentials/filetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package credentials
import (
"errors"
"fmt"
"os"

"cloud.google.com/go/auth"
"cloud.google.com/go/auth/credentials/internal/externalaccount"
Expand Down Expand Up @@ -56,7 +57,7 @@ func fileCredentials(b []byte, opts *DetectOptions) (*auth.Credentials, error) {
if err != nil {
return nil, err
}
quotaProjectID = f.QuotaProjectID
quotaProjectID = resolveQuotaProjectID(opts.QuotaProjectID, f.QuotaProjectID)
universeDomain = f.UniverseDomain
case credsfile.ExternalAccountKey:
f, err := credsfile.ParseExternalAccount(b)
Expand All @@ -67,7 +68,7 @@ func fileCredentials(b []byte, opts *DetectOptions) (*auth.Credentials, error) {
if err != nil {
return nil, err
}
quotaProjectID = f.QuotaProjectID
quotaProjectID = resolveQuotaProjectID(opts.QuotaProjectID, f.QuotaProjectID)
universeDomain = resolveUniverseDomain(opts.UniverseDomain, f.UniverseDomain)
case credsfile.ExternalAccountAuthorizedUserKey:
f, err := credsfile.ParseExternalAccountAuthorizedUser(b)
Expand All @@ -78,7 +79,7 @@ func fileCredentials(b []byte, opts *DetectOptions) (*auth.Credentials, error) {
if err != nil {
return nil, err
}
quotaProjectID = f.QuotaProjectID
quotaProjectID = resolveQuotaProjectID(opts.QuotaProjectID, f.QuotaProjectID)
universeDomain = f.UniverseDomain
case credsfile.ImpersonatedServiceAccountKey:
f, err := credsfile.ParseImpersonatedServiceAccount(b)
Expand Down Expand Up @@ -126,6 +127,20 @@ func resolveUniverseDomain(optsUniverseDomain, fileUniverseDomain string) string
return fileUniverseDomain
}

// resolveQuotaProjectID retrieves quota project with precedence being:
// client option, environment variable, creds file.
func resolveQuotaProjectID(optsQuotaProjectID, fileQuotaProjectID string) string {
if optsQuotaProjectID != "" {
return optsQuotaProjectID
}

if qp := os.Getenv(internalauth.QuotaProjectEnvVar); qp != "" {
return qp
}

return fileQuotaProjectID
}

func handleServiceAccount(f *credsfile.ServiceAccountFile, opts *DetectOptions) (auth.TokenProvider, error) {
if opts.UseSelfSignedJWT {
return configureSelfSignedJWT(f, opts)
Expand Down
1 change: 1 addition & 0 deletions auth/internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func CloneDetectOptions(oldDo *credentials.DetectOptions) *credentials.DetectOpt
CredentialsFile: oldDo.CredentialsFile,
UseSelfSignedJWT: oldDo.UseSelfSignedJWT,
UniverseDomain: oldDo.UniverseDomain,
QuotaProjectID: oldDo.QuotaProjectID,

// These fields are are pointer types that we just want to use exactly
// as the user set, copy the ref
Expand Down

0 comments on commit e4f3cfc

Please sign in to comment.