Skip to content

Commit cbaf52d

Browse files
Merge pull request #52933 from liggitt/proxy-subpath-slash
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.. Preserve leading and trailing slashes on proxy subpaths subresource parsing was not populating path parameters correctly (leading and trailing slashes were being stripped) this caused bad locations to be sent to the proxy, causing kubernetes/kubernetes#52022. the first attempt to fix that (#52065) unconditionally prefixed '/', which broke the redirect case (#52813 #52729) fixes #52813, fixes #52729 needs to be picked to 1.7 and 1.8 ```release-note Restores redirect behavior for proxy subresources ``` Kubernetes-commit: e0f75338b5720fbce0aa02b0cf79965950089dbc
2 parents 757692d + 22f2201 commit cbaf52d

File tree

3 files changed

+88
-65
lines changed

3 files changed

+88
-65
lines changed

Diff for: Godeps/Godeps.json

+58-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/endpoints/apiserver_test.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -2135,15 +2135,25 @@ func TestGetWithOptions(t *testing.T) {
21352135
requestURL: "/namespaces/default/simple/id?param1=test1&param2=test2",
21362136
expectedPath: "",
21372137
},
2138+
{
2139+
name: "with root slash",
2140+
requestURL: "/namespaces/default/simple/id/?param1=test1&param2=test2",
2141+
expectedPath: "/",
2142+
},
21382143
{
21392144
name: "with path",
21402145
requestURL: "/namespaces/default/simple/id/a/different/path?param1=test1&param2=test2",
2141-
expectedPath: "a/different/path",
2146+
expectedPath: "/a/different/path",
2147+
},
2148+
{
2149+
name: "with path with trailing slash",
2150+
requestURL: "/namespaces/default/simple/id/a/different/path/?param1=test1&param2=test2",
2151+
expectedPath: "/a/different/path/",
21422152
},
21432153
{
21442154
name: "as subresource",
21452155
requestURL: "/namespaces/default/simple/id/subresource/another/different/path?param1=test1&param2=test2",
2146-
expectedPath: "another/different/path",
2156+
expectedPath: "/another/different/path",
21472157
},
21482158
{
21492159
name: "cluster-scoped basic",
@@ -2155,13 +2165,13 @@ func TestGetWithOptions(t *testing.T) {
21552165
name: "cluster-scoped basic with path",
21562166
rootScoped: true,
21572167
requestURL: "/simple/id/a/cluster/path?param1=test1&param2=test2",
2158-
expectedPath: "a/cluster/path",
2168+
expectedPath: "/a/cluster/path",
21592169
},
21602170
{
21612171
name: "cluster-scoped basic as subresource",
21622172
rootScoped: true,
21632173
requestURL: "/simple/id/subresource/another/cluster/path?param1=test1&param2=test2",
2164-
expectedPath: "another/cluster/path",
2174+
expectedPath: "/another/cluster/path",
21652175
},
21662176
}
21672177

@@ -2556,7 +2566,7 @@ func TestConnectWithOptions(t *testing.T) {
25562566
func TestConnectWithOptionsAndPath(t *testing.T) {
25572567
responseText := "Hello World"
25582568
itemID := "theID"
2559-
testPath := "a/b/c/def"
2569+
testPath := "/a/b/c/def"
25602570
connectStorage := &ConnecterRESTStorage{
25612571
connectHandler: &OutputConnect{
25622572
response: responseText,
@@ -2572,7 +2582,7 @@ func TestConnectWithOptionsAndPath(t *testing.T) {
25722582
server := httptest.NewServer(handler)
25732583
defer server.Close()
25742584

2575-
resp, err := http.Get(server.URL + "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/default/simple/" + itemID + "/connect/" + testPath + "?param1=value1&param2=value2")
2585+
resp, err := http.Get(server.URL + "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/default/simple/" + itemID + "/connect" + testPath + "?param1=value1&param2=value2")
25762586

25772587
if err != nil {
25782588
t.Errorf("unexpected error: %v", err)

Diff for: pkg/endpoints/handlers/rest.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,20 @@ func getRequestOptions(req *http.Request, scope RequestScope, into runtime.Objec
212212
if isSubresource {
213213
startingIndex = 3
214214
}
215-
newQuery[subpathKey] = []string{strings.Join(requestInfo.Parts[startingIndex:], "/")}
215+
216+
p := strings.Join(requestInfo.Parts[startingIndex:], "/")
217+
218+
// ensure non-empty subpaths correctly reflect a leading slash
219+
if len(p) > 0 && !strings.HasPrefix(p, "/") {
220+
p = "/" + p
221+
}
222+
223+
// ensure subpaths correctly reflect the presence of a trailing slash on the original request
224+
if strings.HasSuffix(requestInfo.Path, "/") && !strings.HasSuffix(p, "/") {
225+
p += "/"
226+
}
227+
228+
newQuery[subpathKey] = []string{p}
216229
query = newQuery
217230
}
218231
return scope.ParameterCodec.DecodeParameters(query, scope.Kind.GroupVersion(), into)

0 commit comments

Comments
 (0)