Skip to content

Commit 0566c2a

Browse files
committed
add test cases for PowerUser, Manual and Emergency groups
1 parent a53f942 commit 0566c2a

File tree

2 files changed

+114
-21
lines changed

2 files changed

+114
-21
lines changed

test/e2e/authorization.go

Lines changed: 109 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111

1212
var (
1313
allGroups = [][]string{
14-
[]string{"FooBar"},
15-
[]string{"ReadOnly"},
16-
[]string{"PowerUser"},
17-
[]string{"Emergency"},
18-
[]string{"Manual"},
19-
[]string{"system:serviceaccounts:kube-system"},
20-
[]string{"CollaboratorEmergency"},
21-
[]string{"CollaboratorManual"},
22-
[]string{"Collaborator24x7"},
23-
[]string{"CollaboratorPowerUser"},
24-
[]string{"Administrator"},
14+
{"FooBar"},
15+
{"ReadOnly"},
16+
{"PowerUser"},
17+
{"Emergency"},
18+
{"Manual"},
19+
{"system:serviceaccounts:kube-system"},
20+
{"CollaboratorEmergency"},
21+
{"CollaboratorManual"},
22+
{"Collaborator24x7"},
23+
{"CollaboratorPowerUser"},
24+
{"Administrator"},
2525
}
2626
)
2727

@@ -141,7 +141,6 @@ var _ = g.Describe("Authorization [RBAC] [Zalando]", func() {
141141
"nodes",
142142
"rbac.authorization.k8s.io/clusterroles",
143143
"storage.k8s.io/storageclasses",
144-
"policy/podsecuritypolicies",
145144
"apiextensions.k8s.io/customresourcedefinitions",
146145
}
147146
g.It("should allow read access", func() {
@@ -162,20 +161,109 @@ var _ = g.Describe("Authorization [RBAC] [Zalando]", func() {
162161
})
163162

164163
g.Context("For PowerUser, Manual and Emergency groups", func() {
164+
var tc testCase
165+
g.BeforeEach(func() {
166+
tc.data.groups = [][]string{
167+
{"PowerUser"},
168+
{"Manual"},
169+
{"Emergency"},
170+
}
171+
tc.data.users = []string{"test-user"}
172+
})
165173

166-
g.It("should deny read access to Secrets in kube-system and visibility namespaces", func() {})
167-
g.It("should deny write access to Nodes", func() {})
168-
g.It("should deny write access to DaemonSets", func() {})
169-
g.It("should deny deleting CRDs", func() {})
170-
g.It("should deny deleting kube-system or visibility namespaces", func() {})
174+
g.It("should deny read access to Secrets in kube-system and visibility namespaces", func() {
175+
tc.data.resources = []string{"secrets"}
176+
tc.data.namespaces = []string{"kube-system", "visibility"}
177+
tc.data.verbs = []string{"get", "list", "watch"}
178+
tc.run(context.TODO(), cs)
179+
output := tc.output
180+
gomega.Expect(output.denied).To(gomega.BeTrue())
181+
})
182+
183+
g.It("should deny write access to Nodes", func() {
184+
tc.data.resources = []string{"nodes"}
185+
tc.data.verbs = []string{"create", "update", "delete", "patch"}
186+
tc.run(context.TODO(), cs)
187+
output := tc.output
188+
gomega.Expect(output.denied).To(gomega.BeTrue())
189+
})
190+
191+
g.It("should deny write access to DaemonSets", func() {
192+
tc.data.resources = []string{"apps/daemonsets"}
193+
tc.data.verbs = []string{"create", "update", "delete", "patch"}
194+
tc.run(context.TODO(), cs)
195+
output := tc.output
196+
gomega.Expect(output.denied).To(gomega.BeTrue())
197+
})
198+
199+
// TODO: Double check if the original test case is correct
200+
g.It("should allow deleting CRDs", func() {
201+
tc.data.resources = []string{"apiextensions.k8s.io/customresourcedefinitions"}
202+
tc.data.verbs = []string{"delete"}
203+
tc.run(context.TODO(), cs)
204+
output := tc.output
205+
gomega.Expect(output.allowed).To(gomega.BeTrue())
206+
})
207+
208+
g.It("should deny deleting kube-system or visibility namespaces", func() {
209+
tc.data.resources = []string{"namespaces"}
210+
tc.data.namespaces = []string{"kube-system", "visibility"}
211+
tc.data.verbs = []string{"delete"}
212+
tc.run(context.TODO(), cs)
213+
output := tc.output
214+
gomega.Expect(output.denied).To(gomega.BeTrue())
215+
})
171216

172217
g.When("the resource is a namespaced resource", func() {
173-
g.It("should deny write access in kube-system and visibility namespaces", func() {})
174-
g.It("should allow write access in namespaces other than kube-system and visibility", func() {})
218+
g.BeforeEach(func() {
219+
tc.data.resources = []string{
220+
"pods",
221+
"apps/deployments",
222+
"apps/statefulsets",
223+
"apps/deployments/scale",
224+
"apps/statefulsets/scale",
225+
"services",
226+
"persistentvolumes",
227+
"persistentvolumeclaims",
228+
"configmaps",
229+
}
230+
tc.data.verbs = []string{"create", "update", "delete", "patch"}
231+
})
232+
g.It("should deny write access in kube-system and visibility namespaces", func() {
233+
tc.data.namespaces = []string{"kube-system", "visibility"}
234+
tc.run(context.TODO(), cs)
235+
output := tc.output
236+
gomega.Expect(output.denied).To(gomega.BeTrue())
237+
})
238+
g.It("should allow write access in namespaces other than kube-system and visibility", func() {
239+
tc.data.namespaces = []string{"", "teapot"}
240+
tc.run(context.TODO(), cs)
241+
output := tc.output
242+
gomega.Expect(output.allowed).To(gomega.BeTrue(),
243+
"Reason: %v", output.reason)
244+
})
175245
})
176246
g.When("the resource is a global resource", func() {
177-
g.It("should deny access to Nodes", func() {})
178-
g.It("should allow access to resources other than Nodes", func() {})
247+
g.BeforeEach(func() {
248+
tc.data.verbs = []string{"create", "update", "delete", "patch"}
249+
})
250+
g.It("should deny write access to Nodes", func() {
251+
tc.data.resources = []string{"nodes"}
252+
tc.run(context.TODO(), cs)
253+
output := tc.output
254+
gomega.Expect(output.denied).To(gomega.BeTrue())
255+
})
256+
g.It("should allow write access to resources other than Nodes", func() {
257+
tc.data.resources = []string{
258+
"namespaces",
259+
"storage.k8s.io/storageclasses",
260+
"apiextensions.k8s.io/customresourcedefinitions",
261+
}
262+
tc.run(context.TODO(), cs)
263+
output := tc.output
264+
gomega.Expect(output.allowed).To(gomega.BeTrue(),
265+
"Reason: %v", output.reason)
266+
})
179267
})
180268
})
181269

test/e2e/authorization_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ func createSubjectAccessReview(ctx context.Context, cs kubernetes.Interface, sar
311311
// evaluateOutput evaluates the output based on the created SubjectAccessReview objects
312312
func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview) {
313313
tcOutput := testcaseOutput{}
314+
// TODO: Test should only pass if all SubjectAccessReviews have expected
315+
// value. Need to rethink this composition logic.
316+
// For example if we have 3 SubjectAccessReviews and the expecataion is 'deny',
317+
// then ALL 3 of them should have a 'denied: true' in response. In this implementation
318+
// even if 1 of them was denied, the test would pass even if the other 2 were allowed.
314319

315320
// Iterate over all the SubjectAccessReviews created and determine the final result
316321
// We don't break the loop if we have denied access from one SubjectAccessReview,

0 commit comments

Comments
 (0)