diff --git a/google/config.go b/google/config.go index e4693787531..d43f0d15527 100644 --- a/google/config.go +++ b/google/config.go @@ -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 { @@ -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 diff --git a/google/header_transport.go b/google/header_transport.go index c372bedaa4e..3fbe23182f0 100644 --- a/google/header_transport.go +++ b/google/header_transport.go @@ -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} }