2015-11-20 06:14:49 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-11-20 06:14:49 +00:00
|
|
|
|
|
|
|
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 v0_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-04 14:31:53 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2015-12-21 05:21:26 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/abac"
|
2015-11-20 06:14:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/abac/v0"
|
|
|
|
)
|
|
|
|
|
2016-12-19 14:25:51 +00:00
|
|
|
func TestV0Conversion(t *testing.T) {
|
2015-11-20 06:14:49 +00:00
|
|
|
testcases := map[string]struct {
|
|
|
|
old *v0.Policy
|
|
|
|
expected *api.Policy
|
|
|
|
}{
|
|
|
|
// a completely empty policy rule allows everything to all users
|
|
|
|
"empty": {
|
|
|
|
old: &v0.Policy{},
|
2016-12-19 14:25:51 +00:00
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
|
2015-11-20 06:14:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// specifying a user is preserved
|
|
|
|
"user": {
|
|
|
|
old: &v0.Policy{User: "bob"},
|
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{User: "bob", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
// specifying a group is preserved (and no longer matches all users)
|
|
|
|
"group": {
|
|
|
|
old: &v0.Policy{Group: "mygroup"},
|
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: "mygroup", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
|
|
|
|
},
|
|
|
|
|
2016-12-19 14:25:51 +00:00
|
|
|
// specifying * for user or group maps to all authenticated subjects
|
|
|
|
"* user": {
|
|
|
|
old: &v0.Policy{User: "*"},
|
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
|
|
|
|
},
|
|
|
|
"* group": {
|
|
|
|
old: &v0.Policy{Group: "*"},
|
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
|
|
|
|
},
|
|
|
|
|
2015-11-20 06:14:49 +00:00
|
|
|
// specifying a namespace removes the * match on non-resource path
|
|
|
|
"namespace": {
|
|
|
|
old: &v0.Policy{Namespace: "myns"},
|
2016-12-19 14:25:51 +00:00
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "*", APIGroup: "*"}},
|
2015-11-20 06:14:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// specifying a resource removes the * match on non-resource path
|
|
|
|
"resource": {
|
|
|
|
old: &v0.Policy{Resource: "myresource"},
|
2016-12-19 14:25:51 +00:00
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "*", Resource: "myresource", APIGroup: "*"}},
|
2015-11-20 06:14:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// specifying a namespace+resource removes the * match on non-resource path
|
|
|
|
"namespace+resource": {
|
|
|
|
old: &v0.Policy{Namespace: "myns", Resource: "myresource"},
|
2016-12-19 14:25:51 +00:00
|
|
|
expected: &api.Policy{Spec: api.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "myresource", APIGroup: "*"}},
|
2015-11-20 06:14:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, tc := range testcases {
|
|
|
|
internal := &api.Policy{}
|
2016-07-04 20:13:27 +00:00
|
|
|
if err := api.Scheme.Convert(tc.old, internal, nil); err != nil {
|
2015-11-20 06:14:49 +00:00
|
|
|
t.Errorf("%s: unexpected error: %v", k, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(internal, tc.expected) {
|
|
|
|
t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, internal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|