mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 14:23:37 +00:00
Merge pull request #4038 from enisoc/apiserver-proxy-rewrite
Fix apiserver proxy path rewriting.
This commit is contained in:
commit
449e4b2cab
@ -216,16 +216,26 @@ func (t *proxyTransport) updateURLs(n *html.Node, sourceURL *url.URL) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Is this URL relative?
|
|
||||||
if url.Host == "" {
|
// Is this URL referring to the same host as sourceURL?
|
||||||
url.Scheme = t.proxyScheme
|
if url.Host == "" || url.Host == sourceURL.Host {
|
||||||
url.Host = t.proxyHost
|
|
||||||
url.Path = path.Join(t.proxyPathPrepend, path.Dir(sourceURL.Path), url.Path, "/")
|
|
||||||
n.Attr[i].Val = url.String()
|
|
||||||
} else if url.Host == sourceURL.Host {
|
|
||||||
url.Scheme = t.proxyScheme
|
url.Scheme = t.proxyScheme
|
||||||
url.Host = t.proxyHost
|
url.Host = t.proxyHost
|
||||||
|
origPath := url.Path
|
||||||
|
|
||||||
|
if strings.HasPrefix(url.Path, "/") {
|
||||||
|
// The path is rooted at the host. Just add proxy prepend.
|
||||||
url.Path = path.Join(t.proxyPathPrepend, url.Path)
|
url.Path = path.Join(t.proxyPathPrepend, url.Path)
|
||||||
|
} else {
|
||||||
|
// The path is relative to sourceURL.
|
||||||
|
url.Path = path.Join(t.proxyPathPrepend, path.Dir(sourceURL.Path), url.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(origPath, "/") {
|
||||||
|
// Add back the trailing slash, which was stripped by path.Join().
|
||||||
|
url.Path += "/"
|
||||||
|
}
|
||||||
|
|
||||||
n.Attr[i].Val = url.String()
|
n.Attr[i].Val = url.String()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,15 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
sourceURL: "http://myminion.com/logs/log.log",
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
|
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
|
||||||
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
|
},
|
||||||
|
"trailing slash": {
|
||||||
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log/">google.log</a></pre>`,
|
||||||
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
|
transport: testTransport,
|
||||||
|
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log/">google.log</a></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
@ -84,7 +92,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
sourceURL: "http://myminion.com/logs/log.log",
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
|
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
|
||||||
contentType: "text/html; charset=utf-8",
|
contentType: "text/html; charset=utf-8",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
@ -100,7 +108,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
||||||
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
||||||
transport: testTransport2,
|
transport: testTransport2,
|
||||||
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/google.log">google.log</a>`,
|
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/google.log">google.log</a>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
|
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user