From 332e0c91fbc7ad42af5af4ef88848aad82e3f699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbyn=C4=9Bk=20Dr=C3=A1pela?= Date: Sat, 24 Oct 2026 12:18:25 +0200 Subject: [PATCH] fix: grant the bundle RBAC to the OLM v1 installer ServiceAccount prepare-restricted-environment.sh renders an installer ClusterRole for the OLM v1 path. OLM installs the bundle with that ServiceAccount, and the API server refuses to create RBAC that grants permissions the installer does not hold itself ("attempting to grant RBAC permissions not currently held"). The role was not updated when the operator gained access to pods, apiservers, ingresses and the DevHubPluginCatalog CRD, so OLM v1 installs fail with: failed to create resource: clusterroles.rbac.authorization.k8s.io "rhdh-operator.v2.2-..." is forbidden: ... attempting to grant RBAC permissions not currently held: {pods}, {apiservers}, {ingresses}, {devhubplugincatalogs}, {devhubplugincatalogs/status} The Helm release fails halfway: the CRD, the Deployment and the ClusterRoleBinding are created, so the operator pod runs without any permissions and reports "forbidden" for every resource it watches. Add the missing rules, and a check (make check-installer-rbac) that compares the installer role with the bundle CSV and the ClusterRoles shipped in the bundle so they cannot drift again. Assisted-by: OpenCode --- .github/workflows/installer-rbac-check.yaml | 43 +++++++ .../scripts/prepare-restricted-environment.sh | 11 +- Makefile | 4 + hack/check-installer-rbac.sh | 116 ++++++++++++++++++ 4 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/installer-rbac-check.yaml create mode 100755 hack/check-installer-rbac.sh diff --git a/.github/workflows/installer-rbac-check.yaml b/.github/workflows/installer-rbac-check.yaml new file mode 100644 index 000000000..08bf10fe6 --- /dev/null +++ b/.github/workflows/installer-rbac-check.yaml @@ -0,0 +1,43 @@ +name: Check Installer RBAC + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + branches: + - main + - 'release-[0-9]+.[0-9]+' + +jobs: + check-installer-rbac: + name: Check Installer RBAC + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6 + with: + files: | + .github/workflows/installer-rbac-check.yaml + Makefile + hack/check-installer-rbac.sh + .rhdh/scripts/prepare-restricted-environment.sh + bundle/**/*.clusterserviceversion.yaml + config/rbac/** + + - name: Install yq + if: steps.changed-files.outputs.any_changed == 'true' + run: | + YQ_VERSION=v4.54.1 + curl -fsSL --proto '=https' --tlsv1.2 -o "${RUNNER_TEMP}/yq" "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64" + chmod +x "${RUNNER_TEMP}/yq" + echo "${RUNNER_TEMP}" >> "${GITHUB_PATH}" + + - name: Run installer RBAC check + if: steps.changed-files.outputs.any_changed == 'true' + run: make check-installer-rbac diff --git a/.rhdh/scripts/prepare-restricted-environment.sh b/.rhdh/scripts/prepare-restricted-environment.sh index 107b7bf00..8bb6a2121 100755 --- a/.rhdh/scripts/prepare-restricted-environment.sh +++ b/.rhdh/scripts/prepare-restricted-environment.sh @@ -2010,7 +2010,7 @@ rules: resources: ["configmaps", "persistentvolumeclaims", "secrets", "services"] verbs: ["create", "delete", "get", "list", "patch", "update", "watch"] - apiGroups: [""] - resources: ["persistentvolumes"] + resources: ["persistentvolumes", "pods"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["events"] @@ -2024,8 +2024,8 @@ rules: verbs: ["create", "delete", "patch", "update"] # Operator runtime: OpenShift - apiGroups: ["config.openshift.io"] - resources: ["ingresses"] - verbs: ["get"] + resources: ["apiservers", "ingresses"] + verbs: ["get", "list", "watch"] - apiGroups: ["route.openshift.io"] resources: ["routes", "routes/custom-host"] verbs: ["create", "delete", "get", "list", "patch", "update", "watch"] @@ -2041,8 +2041,11 @@ rules: resources: ["backstages/finalizers"] verbs: ["update"] - apiGroups: ["rhdh.redhat.com"] - resources: ["backstages/status"] + resources: ["backstages/status", "devhubplugincatalogs/status"] verbs: ["get", "patch", "update"] +- apiGroups: ["rhdh.redhat.com"] + resources: ["devhubplugincatalogs"] + verbs: ["get", "list", "watch"] # Operator runtime: plugin integrations - apiGroups: ["sonataflow.org"] resources: ["sonataflowplatforms", "sonataflows"] diff --git a/Makefile b/Makefile index c95632b38..d7c576e91 100644 --- a/Makefile +++ b/Makefile @@ -228,6 +228,10 @@ validate-image-digests: ## Validate that Dockerfile digests are manifest lists ( @echo @hack/validate-image-digests.sh "$(HERMETO_IMAGE)" +.PHONY: check-installer-rbac +check-installer-rbac: ## Verify the OLM v1 installer ClusterRole can grant the bundle RBAC + @hack/check-installer-rbac.sh + CRD_BASELINE_REF ?= main .PHONY: crd-upgrade-check diff --git a/hack/check-installer-rbac.sh b/hack/check-installer-rbac.sh new file mode 100755 index 000000000..5219613dd --- /dev/null +++ b/hack/check-installer-rbac.sh @@ -0,0 +1,116 @@ +#!/bin/bash +# +# Verify that the OLM v1 installer ClusterRole rendered by +# .rhdh/scripts/prepare-restricted-environment.sh can grant every permission +# the bundle asks for. +# +# OLM v1 installs a bundle with a dedicated installer ServiceAccount, and the +# API server refuses to create RBAC that grants permissions the installer does +# not hold itself ("attempting to grant RBAC permissions not currently held"). +# The installer role is written out by hand in the prepare script, so it drifts +# whenever the operator's RBAC grows. This check keeps the two in sync. +# +# Usage: +# ./hack/check-installer-rbac.sh +# +# Requirements: +# - yq (https://github.com/mikefarah/yq) +# +# Exit codes: +# 0 - The installer role covers every bundle permission +# 1 - Permissions are missing, or the role/CSVs cannot be parsed +# + +# The yq expressions below use $ variables that yq expands. The shell must not. +# shellcheck disable=SC2016 + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" +PREPARE_SCRIPT="${REPO_ROOT}/.rhdh/scripts/prepare-restricted-environment.sh" +BUNDLE_CSVS=( + "${REPO_ROOT}/bundle/rhdh/manifests/backstage-operator.clusterserviceversion.yaml" + "${REPO_ROOT}/bundle/backstage.io/manifests/backstage-operator.clusterserviceversion.yaml" +) +# ClusterRoles shipped in the bundle (editor/viewer/metrics): the installer +# creates them too, so it needs to hold their rules as well. +BUNDLE_CLUSTER_ROLES=("${REPO_ROOT}"/bundle/*/manifests/*clusterrole*.yaml) + +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + +if ! command -v yq &> /dev/null; then + echo -e "${RED}ERROR:${NC} yq is required (https://github.com/mikefarah/yq)" + exit 1 +fi + +tmp_dir="$(mktemp -d)" +trap 'rm -rf "${tmp_dir}"' EXIT + +installer_role="${tmp_dir}/installer-role.yaml" +required="${tmp_dir}/required.txt" +granted="${tmp_dir}/granted.txt" + +# Print one "group|resource|verb" line per rule selected by $2 in file $1. +rules_to_triples() { + local file=$1 + local selector=$2 + { + yq -r "(${selector})"' | (.apiGroups // [""])[] as $g | (.resources // [])[] as $r | .verbs[] as $v | "\($g)|\($r)|\($v)"' "${file}" + yq -r "(${selector})"' | (.nonResourceURLs // [])[] as $u | .verbs[] as $v | "nonresource|\($u)|\($v)"' "${file}" + } | sort -u +} + +# The role is written by a heredoc in the prepare script. Take the rules from +# there instead of duplicating them in this check. +awk ' + /cat < "${installer_role}" + +if [[ ! -s "${installer_role}" ]]; then + echo -e "${RED}ERROR:${NC} could not find the installer ClusterRole in ${PREPARE_SCRIPT}" + exit 1 +fi + +# The bundle CSV is the contract OLM applies: clusterPermissions become a +# ClusterRole and permissions a namespaced Role, both created by the installer. +: > "${required}" +for csv in "${BUNDLE_CSVS[@]}"; do + [[ -f "${csv}" ]] || continue + rules_to_triples "${csv}" \ + '(.spec.install.spec.clusterPermissions // [])[].rules[], (.spec.install.spec.permissions // [])[].rules[]' \ + >> "${required}" +done + +for role in "${BUNDLE_CLUSTER_ROLES[@]}"; do + [[ -f "${role}" ]] || continue + rules_to_triples "${role}" '.rules[]' >> "${required}" +done +sort -u -o "${required}" "${required}" + +if [[ ! -s "${required}" ]]; then + echo -e "${RED}ERROR:${NC} no bundle permissions found in ${BUNDLE_CSVS[*]}" + exit 1 +fi + +rules_to_triples "${installer_role}" '.rules[]' > "${granted}" + +missing="$(comm -23 "${required}" "${granted}")" +if [[ -n "${missing}" ]]; then + echo -e "${RED}The OLM v1 installer ClusterRole cannot grant:${NC}" + while IFS='|' read -r group resource verb; do + printf ' %s %s [%s]\n' "${group:-core}" "${resource}" "${verb}" + done <<< "${missing}" + echo + echo "Add the missing rules to the installer ClusterRole in" + echo " ${PREPARE_SCRIPT#"${REPO_ROOT}/"}" + echo "so OLM v1 can install the bundle. Without them the operator ClusterRole" + echo "creation is refused and the operator runs with no permissions." + exit 1 +fi + +echo -e "${GREEN}Installer ClusterRole covers every bundle permission.${NC}"