Skip to content

Commit

Permalink
Merge pull request kubevirt#13497 from tiraboschi/vmim_rbac
Browse files Browse the repository at this point in the history
[vmim][rbac] Limit VMIMs to cluster admins
  • Loading branch information
kubevirt-bot authored Jan 28, 2025
2 parents 09c2ae3 + 7021b0f commit 75620e5
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 43 deletions.
39 changes: 35 additions & 4 deletions manifests/generated/operator-csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ spec:
- virtualmachines/restart
- virtualmachines/addvolume
- virtualmachines/removevolume
- virtualmachines/migrate
- virtualmachines/memorydump
verbs:
- update
Expand All @@ -891,7 +890,6 @@ spec:
- virtualmachineinstances
- virtualmachineinstancepresets
- virtualmachineinstancereplicasets
- virtualmachineinstancemigrations
verbs:
- get
- delete
Expand All @@ -901,6 +899,14 @@ spec:
- list
- watch
- deletecollection
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- list
- watch
- apiGroups:
- snapshot.kubevirt.io
resources:
Expand Down Expand Up @@ -1023,7 +1029,6 @@ spec:
- virtualmachines/restart
- virtualmachines/addvolume
- virtualmachines/removevolume
- virtualmachines/migrate
- virtualmachines/memorydump
verbs:
- update
Expand All @@ -1040,7 +1045,6 @@ spec:
- virtualmachineinstances
- virtualmachineinstancepresets
- virtualmachineinstancereplicasets
- virtualmachineinstancemigrations
verbs:
- get
- delete
Expand All @@ -1049,6 +1053,14 @@ spec:
- patch
- list
- watch
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- list
- watch
- apiGroups:
- snapshot.kubevirt.io
resources:
Expand Down Expand Up @@ -1227,6 +1239,25 @@ spec:
- get
- list
- watch
- apiGroups:
- subresources.kubevirt.io
resources:
- virtualmachines/migrate
verbs:
- update
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- delete
- create
- update
- patch
- list
- watch
- deletecollection
- apiGroups:
- authentication.k8s.io
resources:
Expand Down
39 changes: 35 additions & 4 deletions manifests/generated/rbac-operator.authorization.k8s.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,6 @@ rules:
- virtualmachines/restart
- virtualmachines/addvolume
- virtualmachines/removevolume
- virtualmachines/migrate
- virtualmachines/memorydump
verbs:
- update
Expand All @@ -893,7 +892,6 @@ rules:
- virtualmachineinstances
- virtualmachineinstancepresets
- virtualmachineinstancereplicasets
- virtualmachineinstancemigrations
verbs:
- get
- delete
Expand All @@ -903,6 +901,14 @@ rules:
- list
- watch
- deletecollection
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- list
- watch
- apiGroups:
- snapshot.kubevirt.io
resources:
Expand Down Expand Up @@ -1025,7 +1031,6 @@ rules:
- virtualmachines/restart
- virtualmachines/addvolume
- virtualmachines/removevolume
- virtualmachines/migrate
- virtualmachines/memorydump
verbs:
- update
Expand All @@ -1042,7 +1047,6 @@ rules:
- virtualmachineinstances
- virtualmachineinstancepresets
- virtualmachineinstancereplicasets
- virtualmachineinstancemigrations
verbs:
- get
- delete
Expand All @@ -1051,6 +1055,14 @@ rules:
- patch
- list
- watch
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- list
- watch
- apiGroups:
- snapshot.kubevirt.io
resources:
Expand Down Expand Up @@ -1229,6 +1241,25 @@ rules:
- get
- list
- watch
- apiGroups:
- subresources.kubevirt.io
resources:
- virtualmachines/migrate
verbs:
- update
- apiGroups:
- kubevirt.io
resources:
- virtualmachineinstancemigrations
verbs:
- get
- delete
- create
- update
- patch
- list
- watch
- deletecollection
- apiGroups:
- authentication.k8s.io
resources:
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-operator/kubevirt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ const (

NAMESPACE = "kubevirt-test"

resourceCount = 77
resourceCount = 78
patchCount = 50
updateCount = 28
updateCount = 29
)

type KubeVirtTestData struct {
Expand Down Expand Up @@ -2397,7 +2397,7 @@ var _ = Describe("KubeVirt Operator", func() {
Expect(kvTestData.totalAdds).To(Equal(resourceCount - expectedUncreatedResources + expectedTemporaryResources))

Expect(kvTestData.controller.stores.ServiceAccountCache.List()).To(HaveLen(4))
Expect(kvTestData.controller.stores.ClusterRoleCache.List()).To(HaveLen(9))
Expect(kvTestData.controller.stores.ClusterRoleCache.List()).To(HaveLen(10))
Expect(kvTestData.controller.stores.ClusterRoleBindingCache.List()).To(HaveLen(7))
Expect(kvTestData.controller.stores.RoleCache.List()).To(HaveLen(5))
Expect(kvTestData.controller.stores.RoleBindingCache.List()).To(HaveLen(5))
Expand Down
66 changes: 62 additions & 4 deletions pkg/virt-operator/resource/generate/rbac/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func GetAllCluster() []runtime.Object {
newViewClusterRole(),
newInstancetypeViewClusterRole(),
newInstancetypeViewClusterRoleBinding(),
newMigrateClusterRole(),
}
}

Expand Down Expand Up @@ -247,7 +248,6 @@ func newAdminClusterRole() *rbacv1.ClusterRole {
apiVMRestart,
apiVMAddVolume,
apiVMRemoveVolume,
apiVMMigrate,
apiVMMemoryDump,
},
Verbs: []string{
Expand All @@ -274,12 +274,22 @@ func newAdminClusterRole() *rbacv1.ClusterRole {
apiVMInstances,
apiVMIPresets,
apiVMIReplicasets,
apiVMIMigrations,
},
Verbs: []string{
"get", "delete", "create", "update", "patch", "list", "watch", "deletecollection",
},
},
{
APIGroups: []string{
GroupName,
},
Resources: []string{
apiVMIMigrations,
},
Verbs: []string{
"get", "list", "watch",
},
},
{
APIGroups: []string{
snapshot.GroupName,
Expand Down Expand Up @@ -430,7 +440,6 @@ func newEditClusterRole() *rbacv1.ClusterRole {
apiVMRestart,
apiVMAddVolume,
apiVMRemoveVolume,
apiVMMigrate,
apiVMMemoryDump,
},
Verbs: []string{
Expand All @@ -457,12 +466,22 @@ func newEditClusterRole() *rbacv1.ClusterRole {
apiVMInstances,
apiVMIPresets,
apiVMIReplicasets,
apiVMIMigrations,
},
Verbs: []string{
"get", "delete", "create", "update", "patch", "list", "watch",
},
},
{
APIGroups: []string{
GroupName,
},
Resources: []string{
apiVMIMigrations,
},
Verbs: []string{
"get", "list", "watch",
},
},
{
APIGroups: []string{
snapshot.GroupName,
Expand Down Expand Up @@ -549,6 +568,45 @@ func newEditClusterRole() *rbacv1.ClusterRole {
}
}

func newMigrateClusterRole() *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
APIVersion: VersionNamev1,
Kind: "ClusterRole",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kubevirt.io:migrate",
Labels: map[string]string{
virtv1.AppLabel: "",
},
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{
virtv1.SubresourceGroupName,
},
Resources: []string{
apiVMMigrate,
},
Verbs: []string{
"update",
},
},
{
APIGroups: []string{
GroupName,
},
Resources: []string{
apiVMIMigrations,
},
Verbs: []string{
"get", "delete", "create", "update", "patch", "list", "watch", "deletecollection",
},
},
},
}
}

func newViewClusterRole() *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
Expand Down
18 changes: 14 additions & 4 deletions pkg/virt-operator/resource/generate/rbac/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMRestart), virtv1.SubresourceGroupName, apiVMStop, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMAddVolume), virtv1.SubresourceGroupName, apiVMRestart, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMRemoveVolume), virtv1.SubresourceGroupName, apiVMAddVolume, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMMigrate), virtv1.SubresourceGroupName, apiVMMigrate, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMMemoryDump), virtv1.SubresourceGroupName, apiVMMemoryDump, "update"),

Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiExpandVmSpec), virtv1.SubresourceGroupName, apiExpandVmSpec, "update"),
Expand All @@ -122,7 +121,6 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("do all operations to %s/%s", GroupName, apiVMInstances), GroupName, apiVMInstances, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
Entry(fmt.Sprintf("do all operations to %s/%s", GroupName, apiVMIPresets), GroupName, apiVMIPresets, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
Entry(fmt.Sprintf("do all operations to %s/%s", GroupName, apiVMIReplicasets), GroupName, apiVMIReplicasets, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
Entry(fmt.Sprintf("do all operations to %s/%s", GroupName, apiVMIMigrations), GroupName, apiVMIMigrations, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),

Entry(fmt.Sprintf("do all operations to %s/%s", snapshot.GroupName, apiVMSnapshots), snapshot.GroupName, apiVMSnapshots, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
Entry(fmt.Sprintf("do all operations to %s/%s", snapshot.GroupName, apiVMSnapshotContents), snapshot.GroupName, apiVMSnapshotContents, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
Expand All @@ -140,6 +138,7 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("do all operations to %s/%s", pool.GroupName, apiVMPools), pool.GroupName, apiVMPools, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),

Entry(fmt.Sprintf("get, list, watch %s/%s", migrations.GroupName, migrations.ResourceMigrationPolicies), migrations.GroupName, migrations.ResourceMigrationPolicies, "get", "list", "watch"),
Entry(fmt.Sprintf("get, list, watch %s/%s", GroupName, apiVMIMigrations), GroupName, apiVMIMigrations, "get", "list", "watch"),
)
})

Expand Down Expand Up @@ -178,7 +177,6 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMRestart), virtv1.SubresourceGroupName, apiVMStop, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMAddVolume), virtv1.SubresourceGroupName, apiVMRestart, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMRemoveVolume), virtv1.SubresourceGroupName, apiVMAddVolume, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMMigrate), virtv1.SubresourceGroupName, apiVMMigrate, "update"),
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMMemoryDump), virtv1.SubresourceGroupName, apiVMMemoryDump, "update"),

Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiExpandVmSpec), virtv1.SubresourceGroupName, apiExpandVmSpec, "update"),
Expand All @@ -187,7 +185,6 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", GroupName, apiVMInstances), GroupName, apiVMInstances, "get", "delete", "create", "update", "patch", "list", "watch"),
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", GroupName, apiVMIPresets), GroupName, apiVMIPresets, "get", "delete", "create", "update", "patch", "list", "watch"),
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", GroupName, apiVMIReplicasets), GroupName, apiVMIReplicasets, "get", "delete", "create", "update", "patch", "list", "watch"),
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", GroupName, apiVMIMigrations), GroupName, apiVMIMigrations, "get", "delete", "create", "update", "patch", "list", "watch"),

Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", snapshot.GroupName, apiVMSnapshots), snapshot.GroupName, apiVMSnapshots, "get", "delete", "create", "update", "patch", "list", "watch"),
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", snapshot.GroupName, apiVMSnapshotContents), snapshot.GroupName, apiVMSnapshotContents, "get", "delete", "create", "update", "patch", "list", "watch"),
Expand All @@ -207,6 +204,19 @@ var _ = Describe("Cluster role and cluster role bindings", func() {
Entry(fmt.Sprintf("get, list %s/%s", GroupName, apiKubevirts), GroupName, apiKubevirts, "get", "list"),

Entry(fmt.Sprintf("get, list, watch %s/%s", migrations.GroupName, migrations.ResourceMigrationPolicies), migrations.GroupName, migrations.ResourceMigrationPolicies, "get", "list", "watch"),
Entry(fmt.Sprintf("get, list, watch %s/%s", GroupName, apiVMIMigrations), GroupName, apiVMIMigrations, "get", "list", "watch"),
)
})

Context("migrate cluster role", func() {

DescribeTable("should contain rule to", func(apiGroup, resource string, verbs ...string) {
clusterRole := getObject(clusterObjects, reflect.TypeOf(&rbacv1.ClusterRole{}), "kubevirt.io:migrate").(*rbacv1.ClusterRole)
Expect(clusterRole).ToNot(BeNil())
expectExactRuleExists(clusterRole.Rules, apiGroup, resource, verbs...)
},
Entry(fmt.Sprintf("update %s/%s", virtv1.SubresourceGroupName, apiVMMigrate), virtv1.SubresourceGroupName, apiVMMigrate, "update"),
Entry(fmt.Sprintf("get, delete, create, update, patch, list, watch %s/%s", GroupName, apiVMIMigrations), GroupName, apiVMIMigrations, "get", "delete", "create", "update", "patch", "list", "watch", "deletecollection"),
)
})

Expand Down
Loading

0 comments on commit 75620e5

Please sign in to comment.