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(compute/metadata): remove leading slash for Get suffix #2760

Merged
merged 3 commits into from Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions compute/metadata/metadata.go
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
codyoss marked this conversation as resolved.
Show resolved Hide resolved
req, err := http.NewRequest("GET", u, nil)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions compute/metadata/metadata_test.go
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