Skip to content

Commit

Permalink
Add X-Goog-Request-Reason header if environment variable `CLOUDSDK_…
Browse files Browse the repository at this point in the history
…CORE_REQUEST_REASON` is set (#4356) (#8220)

* add header transport layer

* some changes

* set transport to be headertransport

* export file for tpgc

* update

* revert ordering change to parameter initialization... default scopes need to be retained for token initialization on vcr tests

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 14, 2021
1 parent 0a20c2c commit 07ae6ae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/4356.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
10 changes: 6 additions & 4 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
if err != nil {
return err
}

c.tokenSource = tokenSource

cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())
Expand All @@ -256,20 +257,21 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
// See ClientWithAdditionalRetries
retryTransport := NewTransportWithDefaultRetries(loggingTransport)

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

// Set final transport value.
client.Transport = retryTransport
client.Transport = headerTransport

// This timeout is a timeout per HTTP request, not per logical operation.
client.Timeout = c.synchronousTimeout()

c.client = client
c.context = ctx

c.Region = GetRegionFromRegionSelfLink(c.Region)

c.requestBatcherServiceUsage = NewRequestBatcher("Service Usage", ctx, c.BatchingConfig)
c.requestBatcherIam = NewRequestBatcher("IAM", ctx, c.BatchingConfig)

c.PollInterval = 10 * time.Second

return nil
Expand Down
35 changes: 35 additions & 0 deletions google/header_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package google

import (
"net/http"
"os"
)

// adapted from https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go
type headerTransportLayer struct {
http.Header
baseTransit http.RoundTripper
}

func newTransportWithHeaders(baseTransit http.RoundTripper) 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)
}

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

func (h headerTransportLayer) RoundTrip(req *http.Request) (*http.Response, error) {
for key, value := range h.Header {
// only set headers that are not previously defined
if _, ok := req.Header[key]; !ok {
req.Header[key] = value
}
}
return h.baseTransit.RoundTrip(req)
}

0 comments on commit 07ae6ae

Please sign in to comment.