This is an automated email from the ASF dual-hosted git repository. squakez pushed a commit to branch release-2.11.x in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 99d9381a63166d35c2831f17c8158093e32335de Author: SnehaNprog <[email protected]> AuthorDate: Tue Nov 10 21:17:04 2026 +0530 Fix #6952: Move StorageClass RBAC to a ClusterRole for all install modes --- helm/camel-k/templates/rbacs-common.yaml | 30 +++++++++++++ helm/camel-k/templates/rbacs-descoped.yaml | 7 --- helm/camel-k/templates/rbacs-namespaced.yaml | 7 --- .../rbac/descoped/operator-cluster-role.yaml | 7 --- ...> get-storageclasses-cluster-role-binding.yaml} | 26 +++++------ ...n.yaml => get-storageclasses-cluster-role.yaml} | 27 ++++++------ pkg/resources/config/rbac/kustomization.yaml | 2 + .../config/rbac/namespaced/operator-role.yaml | 7 --- pkg/resources/resources_test.go | 51 ++++++++++++++++++++++ 9 files changed, 110 insertions(+), 54 deletions(-) diff --git a/helm/camel-k/templates/rbacs-common.yaml b/helm/camel-k/templates/rbacs-common.yaml index 41b426201..5d27ca764 100644 --- a/helm/camel-k/templates/rbacs-common.yaml +++ b/helm/camel-k/templates/rbacs-common.yaml @@ -116,6 +116,21 @@ rules: - watch --- apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app: camel-k + name: {{ include "camel-k.fullname" . }}-operator-storageclasses +rules: +- apiGroups: + - storage.k8s.io + resources: + - storageclasses + verbs: + - get + - list +--- +apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: labels: @@ -157,3 +172,18 @@ subjects: - apiGroup: rbac.authorization.k8s.io kind: Group name: system:authenticated +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + labels: + app: camel-k + name: {{ include "camel-k.fullname" . }}-operator-storageclasses +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: {{ include "camel-k.fullname" . }}-operator-storageclasses +subjects: +- kind: ServiceAccount + name: {{ include "camel-k.fullname" . }}-operator + namespace: '{{ .Release.Namespace }}' diff --git a/helm/camel-k/templates/rbacs-descoped.yaml b/helm/camel-k/templates/rbacs-descoped.yaml index 8cdd8aaf6..a2748c8ce 100644 --- a/helm/camel-k/templates/rbacs-descoped.yaml +++ b/helm/camel-k/templates/rbacs-descoped.yaml @@ -141,13 +141,6 @@ rules: - list - patch - watch -- apiGroups: - - storage.k8s.io - resources: - - storageclasses - verbs: - - get - - list - apiGroups: - "" resources: diff --git a/helm/camel-k/templates/rbacs-namespaced.yaml b/helm/camel-k/templates/rbacs-namespaced.yaml index 530539539..3f28fe9a0 100644 --- a/helm/camel-k/templates/rbacs-namespaced.yaml +++ b/helm/camel-k/templates/rbacs-namespaced.yaml @@ -180,13 +180,6 @@ rules: - get - list - watch -- apiGroups: - - storage.k8s.io - resources: - - storageclasses - verbs: - - get - - list - apiGroups: - "" resources: diff --git a/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml b/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml index f5db9280e..f117ba9d4 100644 --- a/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml +++ b/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml @@ -145,13 +145,6 @@ rules: - patch - watch # Required by mount trait -- apiGroups: - - storage.k8s.io - resources: - - storageclasses - verbs: - - get - - list - apiGroups: - "" resources: diff --git a/pkg/resources/config/rbac/kustomization.yaml b/pkg/resources/config/rbac/get-storageclasses-cluster-role-binding.yaml similarity index 74% copy from pkg/resources/config/rbac/kustomization.yaml copy to pkg/resources/config/rbac/get-storageclasses-cluster-role-binding.yaml index d6a983f2c..6cd3db353 100644 --- a/pkg/resources/config/rbac/kustomization.yaml +++ b/pkg/resources/config/rbac/get-storageclasses-cluster-role-binding.yaml @@ -15,16 +15,16 @@ # limitations under the License. # --------------------------------------------------------------------------- -# -# rbac resources applicable for all kubernetes platforms -# -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - -resources: -- builder-role.yaml -- builder-role-openshift.yaml -- kamelets-viewer-role.yaml -- builder-role-binding.yaml -- builder-role-binding-openshift.yaml -- kamelets-viewer-role-binding.yaml +kind: ClusterRoleBinding +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: camel-k-operator-storageclasses + labels: + app: "camel-k" +subjects: +- kind: ServiceAccount + name: camel-k-operator +roleRef: + kind: ClusterRole + name: camel-k-operator-storageclasses + apiGroup: rbac.authorization.k8s.io diff --git a/pkg/resources/config/rbac/kustomization.yaml b/pkg/resources/config/rbac/get-storageclasses-cluster-role.yaml similarity index 74% copy from pkg/resources/config/rbac/kustomization.yaml copy to pkg/resources/config/rbac/get-storageclasses-cluster-role.yaml index d6a983f2c..ec219ffa9 100644 --- a/pkg/resources/config/rbac/kustomization.yaml +++ b/pkg/resources/config/rbac/get-storageclasses-cluster-role.yaml @@ -15,16 +15,17 @@ # limitations under the License. # --------------------------------------------------------------------------- -# -# rbac resources applicable for all kubernetes platforms -# -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - -resources: -- builder-role.yaml -- builder-role-openshift.yaml -- kamelets-viewer-role.yaml -- builder-role-binding.yaml -- builder-role-binding-openshift.yaml -- kamelets-viewer-role-binding.yaml +kind: ClusterRole +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: camel-k-operator-storageclasses + labels: + app: "camel-k" +rules: +- apiGroups: + - storage.k8s.io + resources: + - storageclasses + verbs: + - get + - list diff --git a/pkg/resources/config/rbac/kustomization.yaml b/pkg/resources/config/rbac/kustomization.yaml index d6a983f2c..bf908303f 100644 --- a/pkg/resources/config/rbac/kustomization.yaml +++ b/pkg/resources/config/rbac/kustomization.yaml @@ -28,3 +28,5 @@ resources: - builder-role-binding.yaml - builder-role-binding-openshift.yaml - kamelets-viewer-role-binding.yaml +- get-storageclasses-cluster-role.yaml +- get-storageclasses-cluster-role-binding.yaml diff --git a/pkg/resources/config/rbac/namespaced/operator-role.yaml b/pkg/resources/config/rbac/namespaced/operator-role.yaml index 9071708f7..8d61a414d 100644 --- a/pkg/resources/config/rbac/namespaced/operator-role.yaml +++ b/pkg/resources/config/rbac/namespaced/operator-role.yaml @@ -185,13 +185,6 @@ rules: - get - list - watch -- apiGroups: - - storage.k8s.io - resources: - - storageclasses - verbs: - - get - - list # Service Accounts (dynamic SA creation) - apiGroups: - "" diff --git a/pkg/resources/resources_test.go b/pkg/resources/resources_test.go index aff93f741..08bd30529 100644 --- a/pkg/resources/resources_test.go +++ b/pkg/resources/resources_test.go @@ -19,6 +19,7 @@ package resources import ( "fmt" + "slices" "testing" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -26,6 +27,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + rbacv1 "k8s.io/api/rbac/v1" ) func NoErrorAndNotEmptyBytes(t *testing.T, path string, callable func(path string) ([]byte, error)) { @@ -161,3 +163,52 @@ func TestCRDResources(t *testing.T) { NoErrorAndNotEmptyBytes(t, "/config/crd/bases/camel.apache.org_kamelets.yaml", Resource) NoErrorAndNotEmptyBytes(t, "/config/crd/bases/camel.apache.org_pipes.yaml", Resource) } + +// StorageClass is a cluster scoped resource: a namespaced Role can never grant access to it, +// so the permission must be granted through a dedicated ClusterRole in every installation mode. +func TestStorageClassesRBACIsClusterScoped(t *testing.T) { + hasStorageClasses := func(rules []rbacv1.PolicyRule) bool { + for _, rule := range rules { + if slices.Contains(rule.Resources, "storageclasses") { + return true + } + } + + return false + } + + var role rbacv1.Role + data, err := Resource("config/rbac/namespaced/operator-role.yaml") + require.NoError(t, err) + require.NoError(t, yaml.Unmarshal(data, &role)) + assert.False(t, hasStorageClasses(role.Rules), "namespaced operator Role cannot grant access to cluster scoped storageclasses") + + var descopedRole rbacv1.ClusterRole + data, err = Resource("config/rbac/descoped/operator-cluster-role.yaml") + require.NoError(t, err) + require.NoError(t, yaml.Unmarshal(data, &descopedRole)) + assert.False(t, hasStorageClasses(descopedRole.Rules), "storageclasses must be granted by the common ClusterRole only") + + var clusterRole rbacv1.ClusterRole + data, err = Resource("config/rbac/get-storageclasses-cluster-role.yaml") + require.NoError(t, err) + require.NoError(t, yaml.Unmarshal(data, &clusterRole)) + require.True(t, hasStorageClasses(clusterRole.Rules)) + assert.Equal(t, []string{"storage.k8s.io"}, clusterRole.Rules[0].APIGroups) + assert.ElementsMatch(t, []string{"get", "list"}, clusterRole.Rules[0].Verbs) + + var binding rbacv1.ClusterRoleBinding + data, err = Resource("config/rbac/get-storageclasses-cluster-role-binding.yaml") + require.NoError(t, err) + require.NoError(t, yaml.Unmarshal(data, &binding)) + assert.Equal(t, clusterRole.Name, binding.RoleRef.Name) + assert.Equal(t, "ClusterRole", binding.RoleRef.Kind) + require.Len(t, binding.Subjects, 1) + assert.Equal(t, "camel-k-operator", binding.Subjects[0].Name) + + // the common RBAC kustomization must ship them, so they apply to every installation mode + kustomization, err := ResourceAsString("config/rbac/kustomization.yaml") + require.NoError(t, err) + assert.Contains(t, kustomization, "get-storageclasses-cluster-role.yaml") + assert.Contains(t, kustomization, "get-storageclasses-cluster-role-binding.yaml") +}
