k8s.io/kubernetes@v1.29.3/pkg/apis/abac/v0/conversion_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v0_test
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/apiserver/pkg/authentication/user"
    24  	"k8s.io/kubernetes/pkg/apis/abac"
    25  	"k8s.io/kubernetes/pkg/apis/abac/v0"
    26  )
    27  
    28  func TestV0Conversion(t *testing.T) {
    29  	testcases := map[string]struct {
    30  		old      *v0.Policy
    31  		expected *abac.Policy
    32  	}{
    33  		// a completely empty policy rule allows everything to all users
    34  		"empty": {
    35  			old:      &v0.Policy{},
    36  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
    37  		},
    38  
    39  		// specifying a user is preserved
    40  		"user": {
    41  			old:      &v0.Policy{User: "bob"},
    42  			expected: &abac.Policy{Spec: abac.PolicySpec{User: "bob", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
    43  		},
    44  
    45  		// specifying a group is preserved (and no longer matches all users)
    46  		"group": {
    47  			old:      &v0.Policy{Group: "mygroup"},
    48  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: "mygroup", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
    49  		},
    50  
    51  		// specifying * for user or group maps to all authenticated subjects
    52  		"* user": {
    53  			old:      &v0.Policy{User: "*"},
    54  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
    55  		},
    56  		"* group": {
    57  			old:      &v0.Policy{Group: "*"},
    58  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
    59  		},
    60  
    61  		// specifying a namespace removes the * match on non-resource path
    62  		"namespace": {
    63  			old:      &v0.Policy{Namespace: "myns"},
    64  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "*", APIGroup: "*"}},
    65  		},
    66  
    67  		// specifying a resource removes the * match on non-resource path
    68  		"resource": {
    69  			old:      &v0.Policy{Resource: "myresource"},
    70  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "*", Resource: "myresource", APIGroup: "*"}},
    71  		},
    72  
    73  		// specifying a namespace+resource removes the * match on non-resource path
    74  		"namespace+resource": {
    75  			old:      &v0.Policy{Namespace: "myns", Resource: "myresource"},
    76  			expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "myresource", APIGroup: "*"}},
    77  		},
    78  	}
    79  	for k, tc := range testcases {
    80  		internal := &abac.Policy{}
    81  		if err := abac.Scheme.Convert(tc.old, internal, nil); err != nil {
    82  			t.Errorf("%s: unexpected error: %v", k, err)
    83  		}
    84  		if !reflect.DeepEqual(internal, tc.expected) {
    85  			t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, internal)
    86  		}
    87  	}
    88  }