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 b8c45cfb4..9824e788f 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 +.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}"