diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go index e6e30bbd44b..ee3433212c6 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go @@ -142,6 +142,7 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { newReq := req.WithContext(context.Background()) newReq.Header = utilnet.CloneHeader(req.Header) newReq.URL = location + newReq.Host = location.Host if handlingInfo.proxyRoundTripper == nil { proxyError(w, req, "", http.StatusNotFound) diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go index 354c8d8b94a..6133eb64dc5 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go @@ -42,18 +42,21 @@ type targetHTTPHandler struct { called bool headers map[string][]string path string + host string } func (d *targetHTTPHandler) Reset() { d.path = "" d.called = false d.headers = nil + d.host = "" } func (d *targetHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { d.path = r.URL.Path d.called = true d.headers = r.Header + d.host = r.Host w.WriteHeader(http.StatusOK) } @@ -258,6 +261,38 @@ func TestProxyHandler(t *testing.T) { }, expectedStatusCode: http.StatusServiceUnavailable, }, + "change aggregator http request host": { + user: &user.DefaultInfo{ + Name: "username", + Groups: []string{"one", "two"}, + }, + path: "/request/path", + apiService: &apiregistration.APIService{ + ObjectMeta: metav1.ObjectMeta{Name: "v1.foo"}, + Spec: apiregistration.APIServiceSpec{ + Service: &apiregistration.ServiceReference{}, + Group: "foo", + Version: "v1", + InsecureSkipTLSVerify: true, + }, + Status: apiregistration.APIServiceStatus{ + Conditions: []apiregistration.APIServiceCondition{ + {Type: apiregistration.Available, Status: apiregistration.ConditionTrue}, + }, + }, + }, + expectedStatusCode: http.StatusOK, + expectedCalled: true, + expectedHeaders: map[string][]string{ + "X-Forwarded-Proto": {"https"}, + "X-Forwarded-Uri": {"/request/path"}, + "X-Forwarded-For": {"127.0.0.1"}, + "X-Remote-User": {"username"}, + "User-Agent": {"Go-http-client/1.1"}, + "Accept-Encoding": {"gzip"}, + "X-Remote-Group": {"one", "two"}, + }, + }, } for name, tc := range tests { @@ -313,6 +348,10 @@ func TestProxyHandler(t *testing.T) { t.Errorf("%s: expected %v, got %v", name, e, a) return } + if e, a := targetServer.Listener.Addr().String(), target.host; tc.expectedCalled && !reflect.DeepEqual(e, a) { + t.Errorf("%s: expected %v, got %v", name, e, a) + return + } }() } }