implement authorizer filter
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -26,20 +26,22 @@ var kubernetesAPIPrefixes = sets.NewString("api", "apis")
|
||||
type RequestInfo struct {
|
||||
*k8srequest.RequestInfo
|
||||
|
||||
// IsKubeSphereRequest indicates whether or not the request should be handled by kubernetes or kubesphere
|
||||
// IsKubernetesRequest indicates whether or not the request should be handled by kubernetes or kubesphere
|
||||
IsKubernetesRequest bool
|
||||
|
||||
// Workspace of requested namespace, for non-workspaced resources, this may be empty
|
||||
// Workspace of requested resource, for non-workspaced resources, this may be empty
|
||||
Workspace string
|
||||
|
||||
// Cluster of requested resource, this is empty in single-cluster environment
|
||||
Cluster string
|
||||
|
||||
// Devops project of requested resource, this may be empty
|
||||
DevopsProject string
|
||||
}
|
||||
|
||||
type RequestInfoFactory struct {
|
||||
APIPrefixes sets.String
|
||||
GrouplessAPIPrefixes sets.String
|
||||
k8sRequestInfoFactory *k8srequest.RequestInfoFactory
|
||||
APIPrefixes sets.String
|
||||
GrouplessAPIPrefixes sets.String
|
||||
}
|
||||
|
||||
// NewRequestInfo returns the information from the http request. If error is not nil, RequestInfo holds the information as best it is known before the failure
|
||||
|
||||
94
pkg/apiserver/request/requestinfo_test.go
Normal file
94
pkg/apiserver/request/requestinfo_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 The KubeSphere Authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* /
|
||||
*/
|
||||
|
||||
package request
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newTestRequestInfoResolver() RequestInfoResolver {
|
||||
requestInfoResolver := &RequestInfoFactory{
|
||||
APIPrefixes: sets.NewString("api", "apis", "kapis", "kapi"),
|
||||
GrouplessAPIPrefixes: sets.NewString("api", "kapi"),
|
||||
}
|
||||
|
||||
return requestInfoResolver
|
||||
}
|
||||
|
||||
func TestRequestInfoFactory_NewRequestInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
method string
|
||||
expectedErr error
|
||||
expectedVerb string
|
||||
expectedResource string
|
||||
expectedIsResourceRequest bool
|
||||
expectedCluster string
|
||||
}{
|
||||
{
|
||||
name: "login",
|
||||
url: "/oauth/authorize?client_id=ks-console&response_type=token",
|
||||
method: http.MethodPost,
|
||||
expectedErr: nil,
|
||||
expectedVerb: "POST",
|
||||
expectedResource: "",
|
||||
expectedIsResourceRequest: false,
|
||||
expectedCluster: "",
|
||||
},
|
||||
{
|
||||
name: "list namespaces",
|
||||
url: "/kapis/resources.kubesphere.io/v1alpha2/namespaces",
|
||||
method: http.MethodGet,
|
||||
expectedErr: nil,
|
||||
expectedVerb: "list",
|
||||
expectedResource: "namespaces",
|
||||
expectedIsResourceRequest: true,
|
||||
expectedCluster: "",
|
||||
},
|
||||
}
|
||||
|
||||
requestInfoResolver := newTestRequestInfoResolver()
|
||||
|
||||
for _, test := range tests {
|
||||
req, err := http.NewRequest(test.method, test.url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
requestInfo, err := requestInfoResolver.NewRequestInfo(req)
|
||||
|
||||
if err != nil {
|
||||
if test.expectedErr != err {
|
||||
t.Errorf("%s: expected error %v, actual %v", test.name, test.expectedErr, err)
|
||||
}
|
||||
} else {
|
||||
if test.expectedVerb != requestInfo.Verb {
|
||||
t.Errorf("%s: expected verb %v, actual %+v", test.name, test.expectedVerb, requestInfo.Verb)
|
||||
}
|
||||
if test.expectedResource != requestInfo.Resource {
|
||||
t.Errorf("%s: expected resource %v, actual %+v", test.name, test.expectedResource, requestInfo.Resource)
|
||||
}
|
||||
if test.expectedIsResourceRequest != requestInfo.IsResourceRequest {
|
||||
t.Errorf("%s: expected is resource request %v, actual %+v", test.name, test.expectedIsResourceRequest, requestInfo.IsResourceRequest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user