|
| 1 | +/* |
| 2 | +Copyright 2017 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 subjectaccessreview |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "reflect" |
| 25 | + |
| 26 | + genericapirequest "k8s.io/kubernetes/pkg/api" |
| 27 | + authorizationapi "k8s.io/kubernetes/pkg/apis/authorization" |
| 28 | + "k8s.io/kubernetes/pkg/auth/authorizer" |
| 29 | + "k8s.io/kubernetes/pkg/auth/user" |
| 30 | +) |
| 31 | + |
| 32 | +type fakeAuthorizer struct { |
| 33 | + attrs authorizer.Attributes |
| 34 | + |
| 35 | + ok bool |
| 36 | + reason string |
| 37 | + err error |
| 38 | +} |
| 39 | + |
| 40 | +func (f *fakeAuthorizer) Authorize(attrs authorizer.Attributes) (bool, string, error) { |
| 41 | + f.attrs = attrs |
| 42 | + return f.ok, f.reason, f.err |
| 43 | +} |
| 44 | + |
| 45 | +func TestCreate(t *testing.T) { |
| 46 | + testcases := map[string]struct { |
| 47 | + spec authorizationapi.SubjectAccessReviewSpec |
| 48 | + ok bool |
| 49 | + reason string |
| 50 | + err error |
| 51 | + |
| 52 | + expectedErr string |
| 53 | + expectedAttrs authorizer.Attributes |
| 54 | + expectedStatus authorizationapi.SubjectAccessReviewStatus |
| 55 | + }{ |
| 56 | + "empty": { |
| 57 | + expectedErr: "nonResourceAttributes or resourceAttributes", |
| 58 | + }, |
| 59 | + |
| 60 | + "nonresource rejected": { |
| 61 | + spec: authorizationapi.SubjectAccessReviewSpec{ |
| 62 | + User: "bob", |
| 63 | + NonResourceAttributes: &authorizationapi.NonResourceAttributes{Verb: "get", Path: "/mypath"}, |
| 64 | + }, |
| 65 | + ok: false, |
| 66 | + reason: "myreason", |
| 67 | + err: errors.New("myerror"), |
| 68 | + expectedAttrs: authorizer.AttributesRecord{ |
| 69 | + User: &user.DefaultInfo{Name: "bob"}, |
| 70 | + Verb: "get", |
| 71 | + Path: "/mypath", |
| 72 | + ResourceRequest: false, |
| 73 | + }, |
| 74 | + expectedStatus: authorizationapi.SubjectAccessReviewStatus{ |
| 75 | + Allowed: false, |
| 76 | + Reason: "myreason", |
| 77 | + EvaluationError: "myerror", |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + "nonresource allowed": { |
| 82 | + spec: authorizationapi.SubjectAccessReviewSpec{ |
| 83 | + User: "bob", |
| 84 | + NonResourceAttributes: &authorizationapi.NonResourceAttributes{Verb: "get", Path: "/mypath"}, |
| 85 | + }, |
| 86 | + ok: true, |
| 87 | + reason: "allowed", |
| 88 | + err: nil, |
| 89 | + expectedAttrs: authorizer.AttributesRecord{ |
| 90 | + User: &user.DefaultInfo{Name: "bob"}, |
| 91 | + Verb: "get", |
| 92 | + Path: "/mypath", |
| 93 | + ResourceRequest: false, |
| 94 | + }, |
| 95 | + expectedStatus: authorizationapi.SubjectAccessReviewStatus{ |
| 96 | + Allowed: true, |
| 97 | + Reason: "allowed", |
| 98 | + EvaluationError: "", |
| 99 | + }, |
| 100 | + }, |
| 101 | + |
| 102 | + "resource rejected": { |
| 103 | + spec: authorizationapi.SubjectAccessReviewSpec{ |
| 104 | + User: "bob", |
| 105 | + ResourceAttributes: &authorizationapi.ResourceAttributes{ |
| 106 | + Namespace: "myns", |
| 107 | + Verb: "create", |
| 108 | + Group: "extensions", |
| 109 | + Version: "v1beta1", |
| 110 | + Resource: "deployments", |
| 111 | + Subresource: "scale", |
| 112 | + Name: "mydeployment", |
| 113 | + }, |
| 114 | + }, |
| 115 | + ok: false, |
| 116 | + reason: "myreason", |
| 117 | + err: errors.New("myerror"), |
| 118 | + expectedAttrs: authorizer.AttributesRecord{ |
| 119 | + User: &user.DefaultInfo{Name: "bob"}, |
| 120 | + Namespace: "myns", |
| 121 | + Verb: "create", |
| 122 | + APIGroup: "extensions", |
| 123 | + APIVersion: "v1beta1", |
| 124 | + Resource: "deployments", |
| 125 | + Subresource: "scale", |
| 126 | + Name: "mydeployment", |
| 127 | + ResourceRequest: true, |
| 128 | + }, |
| 129 | + expectedStatus: authorizationapi.SubjectAccessReviewStatus{ |
| 130 | + Allowed: false, |
| 131 | + Reason: "myreason", |
| 132 | + EvaluationError: "myerror", |
| 133 | + }, |
| 134 | + }, |
| 135 | + |
| 136 | + "resource allowed": { |
| 137 | + spec: authorizationapi.SubjectAccessReviewSpec{ |
| 138 | + User: "bob", |
| 139 | + ResourceAttributes: &authorizationapi.ResourceAttributes{ |
| 140 | + Namespace: "myns", |
| 141 | + Verb: "create", |
| 142 | + Group: "extensions", |
| 143 | + Version: "v1beta1", |
| 144 | + Resource: "deployments", |
| 145 | + Subresource: "scale", |
| 146 | + Name: "mydeployment", |
| 147 | + }, |
| 148 | + }, |
| 149 | + ok: true, |
| 150 | + reason: "allowed", |
| 151 | + err: nil, |
| 152 | + expectedAttrs: authorizer.AttributesRecord{ |
| 153 | + User: &user.DefaultInfo{Name: "bob"}, |
| 154 | + Namespace: "myns", |
| 155 | + Verb: "create", |
| 156 | + APIGroup: "extensions", |
| 157 | + APIVersion: "v1beta1", |
| 158 | + Resource: "deployments", |
| 159 | + Subresource: "scale", |
| 160 | + Name: "mydeployment", |
| 161 | + ResourceRequest: true, |
| 162 | + }, |
| 163 | + expectedStatus: authorizationapi.SubjectAccessReviewStatus{ |
| 164 | + Allowed: true, |
| 165 | + Reason: "allowed", |
| 166 | + EvaluationError: "", |
| 167 | + }, |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + for k, tc := range testcases { |
| 172 | + auth := &fakeAuthorizer{ |
| 173 | + ok: tc.ok, |
| 174 | + reason: tc.reason, |
| 175 | + err: tc.err, |
| 176 | + } |
| 177 | + rest := NewREST(auth) |
| 178 | + |
| 179 | + result, err := rest.Create(genericapirequest.NewContext(), &authorizationapi.SubjectAccessReview{Spec: tc.spec}) |
| 180 | + if err != nil { |
| 181 | + if tc.expectedErr != "" { |
| 182 | + if !strings.Contains(err.Error(), tc.expectedErr) { |
| 183 | + t.Errorf("%s: expected %s to contain %q", k, err, tc.expectedErr) |
| 184 | + } |
| 185 | + } else { |
| 186 | + t.Errorf("%s: %v", k, err) |
| 187 | + } |
| 188 | + continue |
| 189 | + } |
| 190 | + if !reflect.DeepEqual(auth.attrs, tc.expectedAttrs) { |
| 191 | + t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.expectedAttrs, auth.attrs) |
| 192 | + } |
| 193 | + status := result.(*authorizationapi.SubjectAccessReview).Status |
| 194 | + if !reflect.DeepEqual(status, tc.expectedStatus) { |
| 195 | + t.Errorf("%s: expected\n%#v\ngot\n%#v", k, tc.expectedStatus, status) |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments