Skip to content

Commit 3ccda07

Browse files
committed
Plumb subresource through subjectaccessreview
1 parent 41f9ee6 commit 3ccda07

5 files changed

Lines changed: 300 additions & 0 deletions

File tree

‎pkg/registry/authorization/subjectaccessreview/BUILD‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,16 @@ go_library(
2424
"//pkg/runtime:go_default_library",
2525
],
2626
)
27+
28+
go_test(
29+
name = "go_default_test",
30+
srcs = ["rest_test.go"],
31+
library = ":go_default_library",
32+
tags = ["automanaged"],
33+
deps = [
34+
"//pkg/api:go_default_library",
35+
"//pkg/apis/authorization:go_default_library",
36+
"//pkg/auth/authorizer:go_default_library",
37+
"//pkg/auth/user:go_default_library",
38+
],
39+
)
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}

‎pkg/registry/authorization/util/BUILD‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ go_library(
2020
"//pkg/auth/user:go_default_library",
2121
],
2222
)
23+
24+
go_test(
25+
name = "go_default_test",
26+
srcs = ["helpers_test.go"],
27+
library = ":go_default_library",
28+
tags = ["automanaged"],
29+
deps = [
30+
"//pkg/apis/authorization:go_default_library",
31+
"//pkg/auth/authorizer:go_default_library",
32+
"//pkg/util/sets:go_default_library",
33+
],
34+
)

‎pkg/registry/authorization/util/helpers.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func ResourceAttributesFrom(user user.Info, in authorizationapi.ResourceAttribut
2929
Verb: in.Verb,
3030
Namespace: in.Namespace,
3131
APIGroup: in.Group,
32+
APIVersion: in.Version,
3233
Resource: in.Resource,
34+
Subresource: in.Subresource,
35+
Name: in.Name,
3336
ResourceRequest: true,
3437
}
3538
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 util
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
24+
"k8s.io/kubernetes/pkg/auth/authorizer"
25+
"k8s.io/kubernetes/pkg/util/sets"
26+
)
27+
28+
func TestResourceAttributesFrom(t *testing.T) {
29+
knownResourceAttributesNames := sets.NewString(
30+
// Fields we copy in ResourceAttributesFrom
31+
"Verb",
32+
"Namespace",
33+
"Group",
34+
"Version",
35+
"Resource",
36+
"Subresource",
37+
"Name",
38+
39+
// Fields we copy in NonResourceAttributesFrom
40+
"Path",
41+
"Verb",
42+
)
43+
reflect.TypeOf(authorizationapi.ResourceAttributes{}).FieldByNameFunc(func(name string) bool {
44+
if !knownResourceAttributesNames.Has(name) {
45+
t.Errorf("authorizationapi.ResourceAttributes has a new field: %q. Add to ResourceAttributesFrom/NonResourceAttributesFrom as appropriate, then add to knownResourceAttributesNames", name)
46+
}
47+
return false
48+
})
49+
50+
knownAttributesRecordFieldNames := sets.NewString(
51+
// Fields we set in ResourceAttributesFrom
52+
"User",
53+
"Verb",
54+
"Namespace",
55+
"APIGroup",
56+
"APIVersion",
57+
"Resource",
58+
"Subresource",
59+
"Name",
60+
"ResourceRequest",
61+
62+
// Fields we set in NonResourceAttributesFrom
63+
"User",
64+
"ResourceRequest",
65+
"Path",
66+
"Verb",
67+
)
68+
reflect.TypeOf(authorizer.AttributesRecord{}).FieldByNameFunc(func(name string) bool {
69+
if !knownAttributesRecordFieldNames.Has(name) {
70+
t.Errorf("authorizer.AttributesRecord has a new field: %q. Add to ResourceAttributesFrom/NonResourceAttributesFrom as appropriate, then add to knownAttributesRecordFieldNames", name)
71+
}
72+
return false
73+
})
74+
}

0 commit comments

Comments
 (0)