@@ -12,8 +12,10 @@ import (
12
12
"github.com/onsi/ginkgo/extensions/table"
13
13
. "github.com/onsi/gomega"
14
14
templatev1 "github.com/openshift/api/template/v1"
15
+ authv1 "k8s.io/api/authorization/v1"
15
16
core "k8s.io/api/core/v1"
16
17
rbac "k8s.io/api/rbac/v1"
18
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
19
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
20
"sigs.k8s.io/controller-runtime/pkg/client"
19
21
@@ -332,4 +334,79 @@ var _ = Describe("Common templates", func() {
332
334
}
333
335
})
334
336
})
337
+
338
+ Context ("rbac" , func () {
339
+ Context ("os-images" , func () {
340
+ var (
341
+ regularSA * core.ServiceAccount
342
+ )
343
+
344
+ BeforeEach (func () {
345
+ regularSA = & core.ServiceAccount {
346
+ ObjectMeta : metav1.ObjectMeta {
347
+ Name : "regular-sa" ,
348
+ Namespace : strategy .GetNamespace (),
349
+ },
350
+ }
351
+
352
+ Expect (apiClient .Create (ctx , regularSA )).ToNot (HaveOccurred (), "creation of regular service account failed" )
353
+ Expect (apiClient .Get (ctx , getResourceKey (regularSA ), regularSA )).ToNot (HaveOccurred ())
354
+ })
355
+
356
+ AfterEach (func () {
357
+ Expect (apiClient .Delete (ctx , regularSA )).NotTo (HaveOccurred ())
358
+ })
359
+
360
+ It ("regular service account should be able to 'get' os-images namespace" , func () {
361
+ sar , err := coreClient .AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , & authv1.SubjectAccessReview {
362
+ Spec : authv1.SubjectAccessReviewSpec {
363
+ User : fmt .Sprintf ("system:serviceaccount:%s:%s" , strategy .GetNamespace (), regularSA .GetName ()),
364
+ Groups : []string {"system:serviceaccounts" },
365
+ ResourceAttributes : & authv1.ResourceAttributes {
366
+ Namespace : commonTemplates .GoldenImagesNSname ,
367
+ Verb : "get" ,
368
+ Version : "v1" ,
369
+ Resource : "namespaces" ,
370
+ },
371
+ },
372
+ }, metav1.CreateOptions {})
373
+ Expect (err ).ToNot (HaveOccurred ())
374
+ Expect (sar .Status .Allowed ).To (BeTrue (), "regular service account cannot 'get' the os images namespace" )
375
+ })
376
+
377
+ It ("regular service account should be able to 'list' os-images namespace" , func () {
378
+ sar , err := coreClient .AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , & authv1.SubjectAccessReview {
379
+ Spec : authv1.SubjectAccessReviewSpec {
380
+ User : fmt .Sprintf ("system:serviceaccount:%s:%s" , strategy .GetNamespace (), regularSA .GetName ()),
381
+ Groups : []string {"system:serviceaccounts" },
382
+ ResourceAttributes : & authv1.ResourceAttributes {
383
+ Namespace : commonTemplates .GoldenImagesNSname ,
384
+ Verb : "list" ,
385
+ Version : "v1" ,
386
+ Resource : "namespaces" ,
387
+ },
388
+ },
389
+ }, metav1.CreateOptions {})
390
+ Expect (err ).ToNot (HaveOccurred ())
391
+ Expect (sar .Status .Allowed ).To (BeTrue (), "regular service account cannot 'list' the os images namespace" )
392
+ })
393
+
394
+ It ("regular service account should be able to 'watch' os-images namespace" , func () {
395
+ sar , err := coreClient .AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , & authv1.SubjectAccessReview {
396
+ Spec : authv1.SubjectAccessReviewSpec {
397
+ User : fmt .Sprintf ("system:serviceaccount:%s:%s" , strategy .GetNamespace (), regularSA .GetName ()),
398
+ Groups : []string {"system:serviceaccounts" },
399
+ ResourceAttributes : & authv1.ResourceAttributes {
400
+ Namespace : commonTemplates .GoldenImagesNSname ,
401
+ Verb : "watch" ,
402
+ Version : "v1" ,
403
+ Resource : "namespaces" ,
404
+ },
405
+ },
406
+ }, metav1.CreateOptions {})
407
+ Expect (err ).ToNot (HaveOccurred ())
408
+ Expect (sar .Status .Allowed ).To (BeTrue (), "regular service account cannot 'watch' the os images namespace" )
409
+ })
410
+ })
411
+ })
335
412
})
0 commit comments