mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
parent
fbd4722094
commit
8c900187b6
@ -169,6 +169,11 @@ type proxyTransport struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
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)
|
||||||
|
|
||||||
resp, err := http.DefaultTransport.RoundTrip(req)
|
resp, err := http.DefaultTransport.RoundTrip(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,12 +56,12 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
testTransport := &proxyTransport{
|
testTransport := &proxyTransport{
|
||||||
proxyScheme: "http",
|
proxyScheme: "http",
|
||||||
proxyHost: "foo.com",
|
proxyHost: "foo.com",
|
||||||
proxyPathPrepend: "/proxy/minion/minion1:10250/",
|
proxyPathPrepend: "/proxy/minion/minion1:10250",
|
||||||
}
|
}
|
||||||
testTransport2 := &proxyTransport{
|
testTransport2 := &proxyTransport{
|
||||||
proxyScheme: "https",
|
proxyScheme: "https",
|
||||||
proxyHost: "foo.com",
|
proxyHost: "foo.com",
|
||||||
proxyPathPrepend: "/proxy/minion/minion1:8080/",
|
proxyPathPrepend: "/proxy/minion/minion1:8080",
|
||||||
}
|
}
|
||||||
|
|
||||||
table := map[string]struct {
|
table := map[string]struct {
|
||||||
@ -70,6 +70,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
transport *proxyTransport
|
transport *proxyTransport
|
||||||
output string
|
output string
|
||||||
contentType string
|
contentType string
|
||||||
|
forwardedURI string
|
||||||
}{
|
}{
|
||||||
"normal": {
|
"normal": {
|
||||||
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>`,
|
||||||
@ -77,6 +78,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
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/logs/google.log">google.log</a></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
"content-type charset": {
|
"content-type charset": {
|
||||||
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>`,
|
||||||
@ -84,6 +86,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
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/logs/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",
|
||||||
},
|
},
|
||||||
"content-type passthrough": {
|
"content-type passthrough": {
|
||||||
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>`,
|
||||||
@ -91,6 +94,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
output: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
contentType: "text/plain",
|
contentType: "text/plain",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
"subdir": {
|
"subdir": {
|
||||||
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>`,
|
||||||
@ -98,6 +102,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
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/whatever/apt/google.log">google.log</a>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
|
||||||
},
|
},
|
||||||
"image": {
|
"image": {
|
||||||
input: `<pre><img src="kubernetes.jpg"/></pre>`,
|
input: `<pre><img src="kubernetes.jpg"/></pre>`,
|
||||||
@ -105,6 +110,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes.jpg"/></pre>`,
|
output: `<pre><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes.jpg"/></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/",
|
||||||
},
|
},
|
||||||
"abs": {
|
"abs": {
|
||||||
input: `<script src="http://google.com/kubernetes.js"/>`,
|
input: `<script src="http://google.com/kubernetes.js"/>`,
|
||||||
@ -112,6 +118,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<script src="http://google.com/kubernetes.js"/>`,
|
output: `<script src="http://google.com/kubernetes.js"/>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/any/path/",
|
||||||
},
|
},
|
||||||
"abs but same host": {
|
"abs but same host": {
|
||||||
input: `<script src="http://myminion.com/kubernetes.js"/>`,
|
input: `<script src="http://myminion.com/kubernetes.js"/>`,
|
||||||
@ -119,6 +126,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<script src="http://foo.com/proxy/minion/minion1:10250/kubernetes.js"/>`,
|
output: `<script src="http://foo.com/proxy/minion/minion1:10250/kubernetes.js"/>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
|
forwardedURI: "/proxy/minion/minion1:10250/any/path/",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +135,19 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
item.input = fmtHTML(item.input)
|
item.input = fmtHTML(item.input)
|
||||||
item.output = fmtHTML(item.output)
|
item.output = fmtHTML(item.output)
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Check request headers.
|
||||||
|
if got, want := r.Header.Get("X-Forwarded-Uri"), item.forwardedURI; got != want {
|
||||||
|
t.Errorf("%v: X-Forwarded-Uri = %q, want %q", name, got, want)
|
||||||
|
}
|
||||||
|
if got, want := r.Header.Get("X-Forwarded-Host"), item.transport.proxyHost; got != want {
|
||||||
|
t.Errorf("%v: X-Forwarded-Host = %q, want %q", name, got, want)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send response.
|
||||||
w.Header().Set("Content-Type", item.contentType)
|
w.Header().Set("Content-Type", item.contentType)
|
||||||
fmt.Fprint(w, item.input)
|
fmt.Fprint(w, item.input)
|
||||||
}))
|
}))
|
||||||
|
Loading…
Reference in New Issue
Block a user