Skip to content

Commit 1ee22c1

Browse files
authored
Allow users to see the kubevirt-os-images namespace with oc project (#74)
to make base images visible in the UI Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1893278 Signed-off-by: Omer Yahud <[email protected]>
1 parent 366ec84 commit 1ee22c1

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

internal/operands/common-templates/resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func newViewRole(namespace string) *rbac.Role {
6868
Resources: []string{"datavolumes/source"},
6969
Verbs: []string{"create"},
7070
},
71+
{
72+
APIGroups: []string{""},
73+
Resources: []string{"namespaces"},
74+
Verbs: []string{"get", "list", "watch"},
75+
},
7176
},
7277
}
7378
}

tests/commonTemplates_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
"github.com/onsi/ginkgo/extensions/table"
1313
. "github.com/onsi/gomega"
1414
templatev1 "github.com/openshift/api/template/v1"
15+
authv1 "k8s.io/api/authorization/v1"
1516
core "k8s.io/api/core/v1"
1617
rbac "k8s.io/api/rbac/v1"
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1719
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
"sigs.k8s.io/controller-runtime/pkg/client"
1921

@@ -332,4 +334,79 @@ var _ = Describe("Common templates", func() {
332334
}
333335
})
334336
})
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+
})
335412
})

tests/tests_common_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,10 @@ func isStatusDeployed(obj *v1beta1.SSP) bool {
197197
progressing.Status == core.ConditionFalse &&
198198
degraded.Status == core.ConditionFalse
199199
}
200+
201+
func getResourceKey(obj controllerutil.Object) client.ObjectKey {
202+
return client.ObjectKey{
203+
Namespace: obj.GetNamespace(),
204+
Name: obj.GetName(),
205+
}
206+
}

tests/tests_suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/apimachinery/pkg/fields"
2121
"k8s.io/apimachinery/pkg/runtime"
2222
"k8s.io/apimachinery/pkg/runtime/serializer"
23+
"k8s.io/client-go/kubernetes"
2324
"k8s.io/client-go/kubernetes/scheme"
2425
"k8s.io/client-go/rest"
2526
"k8s.io/client-go/tools/cache"
@@ -230,6 +231,7 @@ func (s *existingSspStrategy) sspModificationDisabled() bool {
230231

231232
var (
232233
apiClient client.Client
234+
coreClient *kubernetes.Clientset
233235
ctx context.Context
234236
strategy TestSuiteStrategy
235237
sspListerWatcher cache.ListerWatcher
@@ -285,6 +287,8 @@ func setupApiClient() {
285287
Expect(err).ToNot(HaveOccurred())
286288
apiClient, err = client.New(cfg, client.Options{})
287289
Expect(err).ToNot(HaveOccurred())
290+
coreClient, err = kubernetes.NewForConfig(cfg)
291+
Expect(err).ToNot(HaveOccurred())
288292

289293
ctx = context.Background()
290294
sspListerWatcher = createSspListerWatcher(cfg)

0 commit comments

Comments
 (0)