Skip to content

Commit

Permalink
fix(compute/metadata): remove leading slash for Get suffix (#2760)
Browse files Browse the repository at this point in the history
Updates #2750
  • Loading branch information
codyoss authored Aug 21, 2020
1 parent 03d78b5 commit f0d605c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions compute/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
// being stable anyway.
host = metadataIP
}
suffix = strings.TrimLeft(suffix, "/")
u := "http://" + host + "/computeMetadata/v1/" + suffix
req, err := http.NewRequest("GET", u, nil)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions compute/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ func TestGetFailsOnBadURL(t *testing.T) {
}
}

func TestGet_LeadingSlash(t *testing.T) {
want := "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/identity?audience=http://example.com"
tests := []struct {
name string
suffix string
}{
{
name: "without leading slash",
suffix: "instance/service-accounts/default/identity?audience=http://example.com",
},
{
name: "with leading slash",
suffix: "/instance/service-accounts/default/identity?audience=http://example.com",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ct := &captureTransport{}
c := NewClient(&http.Client{Transport: ct})
c.Get(tc.suffix)
if ct.url != want {
t.Fatalf("got %v, want %v", ct.url, want)
}
})
}
}

type captureTransport struct {
url string
}

func (ct *captureTransport) RoundTrip(req *http.Request) (*http.Response, error) {
ct.url = req.URL.String()
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
}

type userAgentTransport struct {
userAgent string
base http.RoundTripper
Expand Down

0 comments on commit f0d605c

Please sign in to comment.