From 600b5e633d160a330056a84d33fc5d31ba830a56 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 30 Oct 2015 00:05:50 -0400 Subject: [PATCH] Fix GetRequestInfo subresource parsing for proxy/redirect verbs --- pkg/apiserver/handlers.go | 21 +++++++++++---------- pkg/apiserver/handlers_test.go | 3 +++ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/apiserver/handlers.go b/pkg/apiserver/handlers.go index 61e0da5dab1..2f2ced3372c 100644 --- a/pkg/apiserver/handlers.go +++ b/pkg/apiserver/handlers.go @@ -40,11 +40,10 @@ import ( // CRUDdy GET/POST/PUT/DELETE actions on REST objects. // TODO: find a way to keep this up to date automatically. Maybe dynamically populate list as handlers added to // master's Mux. -var specialVerbs = map[string]bool{ - "proxy": true, - "redirect": true, - "watch": true, -} +var specialVerbs = sets.NewString("proxy", "redirect", "watch") + +// specialVerbsNoSubresources contains root verbs which do not allow subresources +var specialVerbsNoSubresources = sets.NewString("proxy", "redirect") // Constant for the retry-after interval on rate limiting. // TODO: maybe make this dynamic? or user-adjustable? @@ -436,11 +435,13 @@ type RequestInfoResolver struct { // /api/{version}/{resource} // /api/{version}/{resource}/{resourceName} // -// Special verbs: +// Special verbs without subresources: // /api/{version}/proxy/{resource}/{resourceName} // /api/{version}/proxy/namespaces/{namespace}/{resource}/{resourceName} // /api/{version}/redirect/namespaces/{namespace}/{resource}/{resourceName} // /api/{version}/redirect/{resource}/{resourceName} +// +// Special verbs with subresources: // /api/{version}/watch/{resource} // /api/{version}/watch/namespaces/{namespace}/{resource} // @@ -489,7 +490,7 @@ func (r *RequestInfoResolver) GetRequestInfo(req *http.Request) (RequestInfo, er currentParts = currentParts[1:] // handle input of form /{specialVerb}/* - if _, ok := specialVerbs[currentParts[0]]; ok { + if specialVerbs.Has(currentParts[0]) { if len(currentParts) < 2 { return requestInfo, fmt.Errorf("unable to determine kind and namespace from url, %v", req.URL) } @@ -534,13 +535,13 @@ func (r *RequestInfoResolver) GetRequestInfo(req *http.Request) (RequestInfo, er // parts look like: resource/resourceName/subresource/other/stuff/we/don't/interpret switch { - case len(requestInfo.Parts) >= 3: + case len(requestInfo.Parts) >= 3 && !specialVerbsNoSubresources.Has(requestInfo.Verb): requestInfo.Subresource = requestInfo.Parts[2] fallthrough - case len(requestInfo.Parts) == 2: + case len(requestInfo.Parts) >= 2: requestInfo.Name = requestInfo.Parts[1] fallthrough - case len(requestInfo.Parts) == 1: + case len(requestInfo.Parts) >= 1: requestInfo.Resource = requestInfo.Parts[0] } diff --git a/pkg/apiserver/handlers_test.go b/pkg/apiserver/handlers_test.go index fcfed364fe7..112692c6bf9 100644 --- a/pkg/apiserver/handlers_test.go +++ b/pkg/apiserver/handlers_test.go @@ -228,12 +228,15 @@ func TestGetAPIRequestInfo(t *testing.T) { // special verbs {"GET", "/api/v1/proxy/namespaces/other/pods/foo", "proxy", "api", "", "v1", "other", "pods", "", "foo", []string{"pods", "foo"}}, + {"GET", "/api/v1/proxy/namespaces/other/pods/foo/subpath/not/a/subresource", "proxy", "api", "", "v1", "other", "pods", "", "foo", []string{"pods", "foo", "subpath", "not", "a", "subresource"}}, {"GET", "/api/v1/redirect/namespaces/other/pods/foo", "redirect", "api", "", "v1", "other", "pods", "", "foo", []string{"pods", "foo"}}, + {"GET", "/api/v1/redirect/namespaces/other/pods/foo/subpath/not/a/subresource", "redirect", "api", "", "v1", "other", "pods", "", "foo", []string{"pods", "foo", "subpath", "not", "a", "subresource"}}, {"GET", "/api/v1/watch/pods", "watch", "api", "", "v1", api.NamespaceAll, "pods", "", "", []string{"pods"}}, {"GET", "/api/v1/watch/namespaces/other/pods", "watch", "api", "", "v1", "other", "pods", "", "", []string{"pods"}}, // subresource identification {"GET", "/api/v1/namespaces/other/pods/foo/status", "get", "api", "", "v1", "other", "pods", "status", "foo", []string{"pods", "foo", "status"}}, + {"GET", "/api/v1/namespaces/other/pods/foo/proxy/subpath", "get", "api", "", "v1", "other", "pods", "proxy", "foo", []string{"pods", "foo", "proxy", "subpath"}}, {"PUT", "/api/v1/namespaces/other/finalize", "update", "api", "", "v1", "other", "finalize", "", "", []string{"finalize"}}, // verb identification