Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set ClientOption for quota project when possible #9636

Closed
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
14 changes: 11 additions & 3 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,19 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {

cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())

// 1. MTLS TRANSPORT/CLIENT - sets up proper auth headers
client, _, err := transport.NewHTTPClient(cleanCtx, option.WithTokenSource(tokenSource))
// 0. ClientOption setup for HTTP client
clientOpts := []option.ClientOption{
// Set up authorization headers
option.WithTokenSource(tokenSource),
}

// 1. MTLS TRANSPORT/CLIENT
client, _, err := transport.NewHTTPClient(cleanCtx, clientOpts...)

if err != nil {
return err
}

// Userinfo is fetched before request logging is enabled to reduce additional noise.
err = c.logGoogleIdentities()
if err != nil {
Expand All @@ -381,7 +389,7 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {

// 4. Header Transport - outer wrapper to inject additional headers we want to apply
// before making requests
headerTransport := newTransportWithHeaders(retryTransport)
headerTransport := newTransportWithHeaders(retryTransport, c)

// Set final transport value.
client.Transport = headerTransport
Expand Down
9 changes: 8 additions & 1 deletion google/header_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ type headerTransportLayer struct {
baseTransit http.RoundTripper
}

func newTransportWithHeaders(baseTransit http.RoundTripper) headerTransportLayer {
func newTransportWithHeaders(baseTransit http.RoundTripper, c *Config) headerTransportLayer {
if baseTransit == nil {
baseTransit = http.DefaultTransport
}

headers := make(http.Header)

if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" {
headers.Set("X-Goog-Request-Reason", requestReason)
}

// Ensure $userProject is set for all HTTP requests using the client if specified by the provider config
// See https://cloud.google.com/apis/docs/system-parameters
if c.UserProjectOverride && c.BillingProject != "" {
headers.Set("X-Goog-User-Project", c.BillingProject)
}

return headerTransportLayer{Header: headers, baseTransit: baseTransit}
}

Expand Down