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

Add Kubernetes-specific headers in apiserver proxy. #4440

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/apiserver/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
proxy.Transport = &proxyTransport{
proxyScheme: req.URL.Scheme,
proxyHost: req.URL.Host,
proxyPrefix: r.prefix,
proxyPathPrepend: path.Join(r.prefix, "ns", namespace, resource, id),
apiRequestInfo: requestInfo,
}
proxy.FlushInterval = 200 * time.Millisecond
proxy.ServeHTTP(w, newReq)
Expand All @@ -189,14 +191,20 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
type proxyTransport struct {
proxyScheme string
proxyHost string
proxyPrefix string
proxyPathPrepend string
apiRequestInfo APIRequestInfo
}

func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Add reverse proxy headers.
req.Header.Set("X-Forwarded-Uri", t.proxyPathPrepend+req.URL.Path)
req.Header.Set("X-Forwarded-Host", t.proxyHost)
req.Header.Set("X-Forwarded-Proto", t.proxyScheme)
// Add Kubernetes-specific headers.
req.Header.Set("X-Kubernetes-Proxy-Prefix", t.proxyPrefix)
req.Header.Set("X-Kubernetes-Proxy-Namespace", t.apiRequestInfo.Namespace)
req.Header.Set("X-Kubernetes-Proxy-Api-Version", t.apiRequestInfo.APIVersion)

resp, err := http.DefaultTransport.RoundTrip(req)

Expand Down Expand Up @@ -227,6 +235,11 @@ func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// to the same host as sourceURL, which is the page on which the target URL
// occurred. If any error occurs (e.g. parsing), it returns targetURL.
func (t *proxyTransport) rewriteURL(targetURL string, sourceURL *url.URL) string {
if t.proxyPrefix != "" && strings.HasPrefix(targetURL, t.proxyPrefix) {
// The destination server used our X-Kubernetes-Proxy-Prefix header to write
// a URL that already goes through the proxy, perhaps to another resource.
return targetURL
}
url, err := url.Parse(targetURL)
if err != nil {
return targetURL
Expand Down
25 changes: 25 additions & 0 deletions pkg/apiserver/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ func fmtHTML(in string) string {
}

func TestProxyTransport(t *testing.T) {
apiRequestInfo := APIRequestInfo{
APIVersion: "v1beta1",
Namespace: "default",
}
testTransport := &proxyTransport{
proxyScheme: "http",
proxyHost: "foo.com",
proxyPrefix: "/proxy/",
proxyPathPrepend: "/proxy/minion/minion1:10250",
apiRequestInfo: apiRequestInfo,
}
testTransport2 := &proxyTransport{
proxyScheme: "https",
proxyHost: "foo.com",
proxyPrefix: "/proxy/",
proxyPathPrepend: "/proxy/minion/minion1:8080",
apiRequestInfo: apiRequestInfo,
}
type Item struct {
input string
Expand Down Expand Up @@ -160,6 +168,14 @@ func TestProxyTransport(t *testing.T) {
redirectWant: "http://example.com/redirected/target/",
forwardedURI: "/proxy/minion/minion1:10250/redirect",
},
"proxy prefix": {
input: `<pre><a href="/proxy/services/xyz/kubelet.log">kubelet.log</a></pre>`,
sourceURL: "http://myminion.com/logs/log.log",
transport: testTransport,
output: `<pre><a href="/proxy/services/xyz/kubelet.log">kubelet.log</a></pre>`,
contentType: "text/html",
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
},
}

testItem := func(name string, item *Item) {
Expand All @@ -178,6 +194,15 @@ func TestProxyTransport(t *testing.T) {
if got, want := r.Header.Get("X-Forwarded-Proto"), item.transport.proxyScheme; got != want {
t.Errorf("%v: X-Forwarded-Proto = %q, want %q", name, got, want)
}
if got, want := r.Header.Get("X-Kubernetes-Proxy-Prefix"), "/proxy/"; got != want {
t.Errorf("%v: X-Kubernetes-Proxy-Prefix = %q, want %q", name, got, want)
}
if got, want := r.Header.Get("X-Kubernetes-Proxy-Namespace"), apiRequestInfo.Namespace; got != want {
t.Errorf("%v: X-Kubernetes-Proxy-Namespace = %q, want %q", name, got, want)
}
if got, want := r.Header.Get("X-Kubernetes-Proxy-Api-Version"), apiRequestInfo.APIVersion; got != want {
t.Errorf("%v: X-Kubernetes-Proxy-Api-Version = %q, want %q", name, got, want)
}

// Send response.
if item.redirect != "" {
Expand Down