Skip to content

Commit 315f6e6

Browse files
[util] Ignore proxies when calling Nova Metadata (#2218)
Prior to this change, the call to the Nova Metadata service used HTTP proxies as directed by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof). However, the call to the Metadata well-known IP must originate from the requesting server directly to be correctly served. With this change, calls to the Nova Metadata service ignore proxy settings. Fixes #2217
1 parent a6c7061 commit 315f6e6

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

pkg/util/metadata/metadata.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,17 @@ func getFromConfigDrive(metadataVersion string) (*Metadata, error) {
189189
return parseMetadata(f)
190190
}
191191

192+
func noProxyHTTPClient() *http.Client {
193+
noProxyTransport := http.DefaultTransport.(*http.Transport).Clone()
194+
noProxyTransport.Proxy = nil
195+
return &http.Client{Transport: noProxyTransport}
196+
}
197+
192198
func getFromMetadataService(metadataVersion string) (*Metadata, error) {
193199
// Try to get JSON from metadata server.
194200
metadataURL := getMetadataURL(metadataVersion)
195-
klog.V(4).Infof("Attempting to fetch metadata from %s", metadataURL)
196-
resp, err := http.Get(metadataURL)
201+
klog.V(4).Infof("Attempting to fetch metadata from %s, ignoring proxy settings", metadataURL)
202+
resp, err := noProxyHTTPClient().Get(metadataURL)
197203
if err != nil {
198204
return nil, fmt.Errorf("error fetching %s: %v", metadataURL, err)
199205
}

pkg/util/metadata/metadata_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package metadata
1818

1919
import (
2020
"fmt"
21+
"net/http"
22+
"net/http/httptest"
23+
"os"
2124
"strings"
2225
"testing"
2326
)
@@ -148,3 +151,27 @@ func TestCheckMetaDataOpts(t *testing.T) {
148151
}
149152
}
150153
}
154+
155+
func TestGetFromMetadataService(t *testing.T) {
156+
t.Run("ignores HTTP_PROXY", func(t *testing.T) {
157+
// Here I spin up an HTTP server, set it as HTTP_PROXY, and
158+
// assert that the request to the Metadata server doesn't hit
159+
// it.
160+
fakeProxy := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
161+
t.Errorf("the call to Metadata hit the proxy server")
162+
}))
163+
defer fakeProxy.Close()
164+
165+
// defer resetting HTTP_PROXY to whatever it was before this test
166+
defer func(originalValue string, wasSet bool) {
167+
if wasSet {
168+
os.Setenv("HTTP_PROXY", originalValue)
169+
} else {
170+
os.Unsetenv("HTTP_PROXY")
171+
}
172+
}(os.LookupEnv("HTTP_PROXY"))
173+
174+
os.Setenv("HTTP_PROXY", fakeProxy.URL)
175+
_, _ = getFromMetadataService("")
176+
})
177+
}

0 commit comments

Comments
 (0)