forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_test.go
324 lines (269 loc) · 11.4 KB
/
access_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package tests_test
import (
"fmt"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
authv1 "k8s.io/api/authorization/v1"
authClientV1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
)
var _ = Describe("[rfe_id:500][crit:high][vendor:[email protected]][level:component]User Access", func() {
tests.FlagParse()
view := tests.ViewServiceAccountName
edit := tests.EditServiceAccountName
admin := tests.AdminServiceAccountName
var k8sClient string
BeforeEach(func() {
k8sClient = tests.GetK8sCmdClient()
tests.SkipIfNoCmd(k8sClient)
tests.BeforeTestCleanup()
})
Describe("With default kubevirt service accounts", func() {
table.DescribeTable("should verify permissions on resources are correct for view, edit, and admin", func(resource string) {
viewVerbs := make(map[string]string)
editVerbs := make(map[string]string)
adminVerbs := make(map[string]string)
// GET
viewVerbs["get"] = "yes"
editVerbs["get"] = "yes"
adminVerbs["get"] = "yes"
// List
viewVerbs["list"] = "yes"
editVerbs["list"] = "yes"
adminVerbs["list"] = "yes"
// WATCH
viewVerbs["watch"] = "yes"
editVerbs["watch"] = "yes"
adminVerbs["watch"] = "yes"
// DELETE
viewVerbs["delete"] = "no"
editVerbs["delete"] = "yes"
adminVerbs["delete"] = "yes"
// CREATE
viewVerbs["create"] = "no"
editVerbs["create"] = "yes"
adminVerbs["create"] = "yes"
// UPDATE
viewVerbs["update"] = "no"
editVerbs["update"] = "yes"
adminVerbs["update"] = "yes"
// PATCH
viewVerbs["patch"] = "no"
editVerbs["patch"] = "yes"
adminVerbs["patch"] = "yes"
// DELETE COllECTION
viewVerbs["deleteCollection"] = "no"
editVerbs["deleteCollection"] = "no"
adminVerbs["deleteCollection"] = "yes"
namespace := tests.NamespaceTestDefault
verbs := []string{"get", "list", "watch", "delete", "create", "update", "patch", "deletecollection"}
for _, verb := range verbs {
// VIEW
By(fmt.Sprintf("verifying VIEW sa for verb %s", verb))
expectedRes, _ := viewVerbs[verb]
as := fmt.Sprintf("system:serviceaccount:%s:%s", namespace, view)
result, _, _ := tests.RunCommand(k8sClient, "auth", "can-i", "--as", as, verb, resource)
Expect(result).To(ContainSubstring(expectedRes))
// EDIT
By(fmt.Sprintf("verifying EDIT sa for verb %s", verb))
expectedRes, _ = editVerbs[verb]
as = fmt.Sprintf("system:serviceaccount:%s:%s", namespace, edit)
result, _, _ = tests.RunCommand(k8sClient, "auth", "can-i", "--as", as, verb, resource)
Expect(result).To(ContainSubstring(expectedRes))
// ADMIN
By(fmt.Sprintf("verifying ADMIN sa for verb %s", verb))
expectedRes, _ = adminVerbs[verb]
as = fmt.Sprintf("system:serviceaccount:%s:%s", namespace, admin)
result, _, _ = tests.RunCommand(k8sClient, "auth", "can-i", "--as", as, verb, resource)
Expect(result).To(ContainSubstring(expectedRes))
// DEFAULT - the default should always return 'no' for ever verb.
// This is primarily a sanity check.
By(fmt.Sprintf("verifying DEFAULT sa for verb %s", verb))
expectedRes = "no"
as = fmt.Sprintf("system:serviceaccount:%s:default", namespace)
result, _, _ = tests.RunCommand(k8sClient, "auth", "can-i", "--as", as, verb, resource)
Expect(result).To(ContainSubstring(expectedRes))
}
},
table.Entry("[test_id:526]given a vmi", "virtualmachineinstances"),
table.Entry("[test_id:527]given a vm", "virtualmachines"),
table.Entry("[test_id:528]given a vmi preset", "virtualmachineinstancepresets"),
table.Entry("[test_id:529][crit:low]given a vmi replica set", "virtualmachineinstancereplicasets"),
table.Entry("[test_id:3230]given a vmi migration", "virtualmachineinstancemigrations"),
)
var authClient *authClientV1.AuthorizationV1Client
It("[test_id:3231]Prepare auth client", func() {
virtClient, err := kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())
authClient, err = authClientV1.NewForConfig(virtClient.Config())
Expect(err).ToNot(HaveOccurred())
})
table.DescribeTable("should verify permissions on subresources are correct for view, edit, and admin", func(resource string, subresource string) {
viewVerbs := make(map[string]bool)
editVerbs := make(map[string]bool)
adminVerbs := make(map[string]bool)
// GET
viewVerbs["get"] = false
editVerbs["get"] = false
adminVerbs["get"] = false
// List
viewVerbs["list"] = false
editVerbs["list"] = false
adminVerbs["list"] = false
// WATCH
viewVerbs["watch"] = false
editVerbs["watch"] = false
adminVerbs["watch"] = false
// DELETE
viewVerbs["delete"] = false
editVerbs["delete"] = false
adminVerbs["delete"] = false
// CREATE
viewVerbs["create"] = false
editVerbs["create"] = false
adminVerbs["create"] = false
// UPDATE
viewVerbs["update"] = false
editVerbs["update"] = true
adminVerbs["update"] = true
// PATCH
viewVerbs["patch"] = false
editVerbs["patch"] = false
adminVerbs["patch"] = false
// DELETE COllECTION
viewVerbs["deleteCollection"] = false
editVerbs["deleteCollection"] = false
adminVerbs["deleteCollection"] = false
namespace := tests.NamespaceTestDefault
verbs := []string{"get", "list", "watch", "delete", "create", "update", "patch", "deletecollection"}
doSarRequest := func(resource string, subresource string, user string, verb string, expected bool) {
sar := &authv1.SubjectAccessReview{}
sar.Spec = authv1.SubjectAccessReviewSpec{
User: user,
ResourceAttributes: &authv1.ResourceAttributes{
Namespace: namespace,
Verb: verb,
Group: v1.SubresourceGroupName,
Version: v1.GroupVersion.Version,
Resource: resource,
Subresource: subresource,
},
}
result, err := authClient.SubjectAccessReviews().Create(sar)
Expect(err).ToNot(HaveOccurred())
Expect(result.Status.Allowed).To(Equal(expected))
}
for _, verb := range verbs {
// VIEW
By(fmt.Sprintf("verifying VIEW sa for verb %s", verb))
expectedRes, _ := viewVerbs[verb]
user := fmt.Sprintf("system:serviceaccount:%s:%s", namespace, view)
doSarRequest(resource, subresource, user, verb, expectedRes)
// EDIT
By(fmt.Sprintf("verifying EDIT sa for verb %s", verb))
expectedRes, _ = editVerbs[verb]
user = fmt.Sprintf("system:serviceaccount:%s:%s", namespace, edit)
doSarRequest(resource, subresource, user, verb, expectedRes)
// ADMIN
By(fmt.Sprintf("verifying ADMIN sa for verb %s", verb))
expectedRes, _ = adminVerbs[verb]
user = fmt.Sprintf("system:serviceaccount:%s:%s", namespace, admin)
doSarRequest(resource, subresource, user, verb, expectedRes)
// DEFAULT - the default should always return 'no' for ever verb.
// This is primarily a sanity check.
By(fmt.Sprintf("verifying DEFAULT sa for verb %s", verb))
expectedRes = false
user = fmt.Sprintf("system:serviceaccount:%s:default", namespace)
doSarRequest(resource, subresource, user, verb, expectedRes)
}
},
table.Entry("[test_id:3232]on vm start", "virtualmachines", "start"),
table.Entry("[test_id:3233]on vm stop", "virtualmachines", "stop"),
table.Entry("[test_id:3234]on vm restart", "virtualmachines", "restart"),
)
})
Describe("[rfe_id:2919][crit:high][vendor:[email protected]][level:component] With regular OpenShift user", func() {
BeforeEach(func() {
tests.SkipIfNoCmd("oc")
})
const testUser = "testuser"
testRights := func(resource, right string) {
verbsList := []string{"get", "list", "watch", "delete", "create", "update", "patch", "deletecollection"}
for _, verb := range verbsList {
// AS A TEST USER
By(fmt.Sprintf("verifying user rights for verb %s", verb))
result, _, _ := tests.RunCommand(k8sClient, "auth", "can-i", "--as", testUser, verb, resource)
Expect(result).To(ContainSubstring(right))
}
}
Context("should fail without admin rights for the project", func() {
BeforeEach(func() {
By("Ensuring the cluster has new test user")
stdOut, stdErr, err := tests.RunCommandWithNS("", k8sClient, "create", "user", testUser)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
stdOut, stdErr, err = tests.RunCommandWithNS("", k8sClient, "project", tests.NamespaceTestDefault)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
})
AfterEach(func() {
stdOut, stdErr, err := tests.RunCommandWithNS("", k8sClient, "delete", "user", testUser)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
})
table.DescribeTable("should verify permissions on resources are correct for view, edit, and admin", func(resource string) {
testRights(resource, "no")
},
table.Entry("[test_id:2921]given a vmi", "virtualmachineinstances"),
table.Entry("[test_id:2915]given a vm", "virtualmachines"),
table.Entry("[test_id:2917]given a vmi preset", "virtualmachineinstancepresets"),
table.Entry("[test_id:2919]given a vmi replica set", "virtualmachineinstancereplicasets"),
table.Entry("[test_id:3235]given a vmi migration", "virtualmachineinstancemigrations"),
)
})
Context("should succeed with admin rights for the project", func() {
BeforeEach(func() {
By("Ensuring the cluster has new test user")
stdOut, stdErr, err := tests.RunCommandWithNS("", k8sClient, "create", "user", testUser)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
By("Ensuring user has the admin rights for the test namespace project")
// This is ussually done in backgroung when creating new user with login and by creating new project by that user
stdOut, stdErr, err = tests.RunCommandWithNS("", k8sClient, "adm", "policy", "add-role-to-user", "-n", tests.NamespaceTestDefault, "admin", testUser)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
stdOut, stdErr, err = tests.RunCommandWithNS("", k8sClient, "project", tests.NamespaceTestDefault)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
})
AfterEach(func() {
stdOut, stdErr, err := tests.RunCommandWithNS("", k8sClient, "delete", "user", testUser)
Expect(err).ToNot(HaveOccurred(), "ERR: %s", stdOut+stdErr)
})
table.DescribeTable("should verify permissions on resources are correct the test user", func(resource string) {
testRights(resource, "yes")
},
table.Entry("[test_id:2920]given a vmi", "virtualmachineinstances"),
table.Entry("[test_id:2831]given a vm", "virtualmachines"),
table.Entry("[test_id:2916]given a vmi preset", "virtualmachineinstancepresets"),
table.Entry("[test_id:2918][crit:low]given a vmi replica set", "virtualmachineinstancereplicasets"),
table.Entry("[test_id:2837]given a vmi migration", "virtualmachineinstancemigrations"),
)
})
})
})