Skip to content

Commit bc43323

Browse files
committed
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
1 parent 422fcc5 commit bc43323

4 files changed

Lines changed: 170 additions & 4 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Check Installer RBAC
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened, ready_for_review]
6+
branches:
7+
- main
8+
- 'release-[0-9]+.[0-9]+'
9+
10+
jobs:
11+
check-installer-rbac:
12+
name: Check Installer RBAC
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Get changed files
22+
id: changed-files
23+
uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
24+
with:
25+
files: |
26+
.github/workflows/installer-rbac-check.yaml
27+
Makefile
28+
hack/check-installer-rbac.sh
29+
.rhdh/scripts/prepare-restricted-environment.sh
30+
bundle/**/*.clusterserviceversion.yaml
31+
config/rbac/**
32+
33+
- name: Install yq
34+
if: steps.changed-files.outputs.any_changed == 'true'
35+
run: |
36+
YQ_VERSION=v4.54.1
37+
curl -fsSL -o "${RUNNER_TEMP}/yq" "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64"
38+
chmod +x "${RUNNER_TEMP}/yq"
39+
echo "${RUNNER_TEMP}" >> "${GITHUB_PATH}"
40+
41+
- name: Run installer RBAC check
42+
if: steps.changed-files.outputs.any_changed == 'true'
43+
run: make check-installer-rbac

‎.rhdh/scripts/prepare-restricted-environment.sh‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ rules:
20102010
resources: ["configmaps", "persistentvolumeclaims", "secrets", "services"]
20112011
verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
20122012
- apiGroups: [""]
2013-
resources: ["persistentvolumes"]
2013+
resources: ["persistentvolumes", "pods"]
20142014
verbs: ["get", "list", "watch"]
20152015
- apiGroups: [""]
20162016
resources: ["events"]
@@ -2024,8 +2024,8 @@ rules:
20242024
verbs: ["create", "delete", "patch", "update"]
20252025
# Operator runtime: OpenShift
20262026
- apiGroups: ["config.openshift.io"]
2027-
resources: ["ingresses"]
2028-
verbs: ["get"]
2027+
resources: ["apiservers", "ingresses"]
2028+
verbs: ["get", "list", "watch"]
20292029
- apiGroups: ["route.openshift.io"]
20302030
resources: ["routes", "routes/custom-host"]
20312031
verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
@@ -2041,8 +2041,11 @@ rules:
20412041
resources: ["backstages/finalizers"]
20422042
verbs: ["update"]
20432043
- apiGroups: ["rhdh.redhat.com"]
2044-
resources: ["backstages/status"]
2044+
resources: ["backstages/status", "devhubplugincatalogs/status"]
20452045
verbs: ["get", "patch", "update"]
2046+
- apiGroups: ["rhdh.redhat.com"]
2047+
resources: ["devhubplugincatalogs"]
2048+
verbs: ["get", "list", "watch"]
20462049
# Operator runtime: plugin integrations
20472050
- apiGroups: ["sonataflow.org"]
20482051
resources: ["sonataflowplatforms", "sonataflows"]

‎Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ validate-image-digests: ## Validate that Dockerfile digests are manifest lists (
228228
@echo
229229
@hack/validate-image-digests.sh "$(HERMETO_IMAGE)"
230230

231+
.PHONY: check-installer-rbac
232+
check-installer-rbac: ## Verify the OLM v1 installer ClusterRole can grant the bundle RBAC
233+
@hack/check-installer-rbac.sh
234+
231235
CRD_BASELINE_REF ?= main
232236

233237
.PHONY: crd-upgrade-check

‎hack/check-installer-rbac.sh‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/bin/bash
2+
#
3+
# Verify that the OLM v1 installer ClusterRole rendered by
4+
# .rhdh/scripts/prepare-restricted-environment.sh can grant every permission
5+
# the bundle asks for.
6+
#
7+
# OLM v1 installs a bundle with a dedicated installer ServiceAccount, and the
8+
# API server refuses to create RBAC that grants permissions the installer does
9+
# not hold itself ("attempting to grant RBAC permissions not currently held").
10+
# The installer role is written out by hand in the prepare script, so it drifts
11+
# whenever the operator's RBAC grows. This check keeps the two in sync.
12+
#
13+
# Usage:
14+
# ./hack/check-installer-rbac.sh
15+
#
16+
# Requirements:
17+
# - yq (https://github.com/mikefarah/yq)
18+
#
19+
# Exit codes:
20+
# 0 - The installer role covers every bundle permission
21+
# 1 - Permissions are missing, or the role/CSVs cannot be parsed
22+
#
23+
24+
# The yq expressions below use $ variables that yq expands. The shell must not.
25+
# shellcheck disable=SC2016
26+
27+
set -euo pipefail
28+
29+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
30+
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
31+
PREPARE_SCRIPT="${REPO_ROOT}/.rhdh/scripts/prepare-restricted-environment.sh"
32+
BUNDLE_CSVS=(
33+
"${REPO_ROOT}/bundle/rhdh/manifests/backstage-operator.clusterserviceversion.yaml"
34+
"${REPO_ROOT}/bundle/backstage.io/manifests/backstage-operator.clusterserviceversion.yaml"
35+
)
36+
# ClusterRoles shipped in the bundle (editor/viewer/metrics): the installer
37+
# creates them too, so it needs to hold their rules as well.
38+
BUNDLE_CLUSTER_ROLES=("${REPO_ROOT}"/bundle/*/manifests/*clusterrole*.yaml)
39+
40+
RED='\033[0;31m'
41+
GREEN='\033[0;32m'
42+
NC='\033[0m'
43+
44+
if ! command -v yq &> /dev/null; then
45+
echo -e "${RED}ERROR:${NC} yq is required (https://github.com/mikefarah/yq)"
46+
exit 1
47+
fi
48+
49+
tmp_dir="$(mktemp -d)"
50+
trap 'rm -rf "${tmp_dir}"' EXIT
51+
52+
installer_role="${tmp_dir}/installer-role.yaml"
53+
required="${tmp_dir}/required.txt"
54+
granted="${tmp_dir}/granted.txt"
55+
56+
# Print one "group|resource|verb" line per rule selected by $2 in file $1.
57+
rules_to_triples() {
58+
local file=$1
59+
local selector=$2
60+
{
61+
yq -r "(${selector})"' | (.apiGroups // [""])[] as $g | (.resources // [])[] as $r | .verbs[] as $v | "\($g)|\($r)|\($v)"' "${file}"
62+
yq -r "(${selector})"' | (.nonResourceURLs // [])[] as $u | .verbs[] as $v | "nonresource|\($u)|\($v)"' "${file}"
63+
} | sort -u
64+
}
65+
66+
# The role is written by a heredoc in the prepare script. Take the rules from
67+
# there instead of duplicating them in this check.
68+
awk '
69+
/cat <</ && /clusterRole\.yaml/ { in_role = 1; next }
70+
in_role && /^EOF$/ { exit }
71+
in_role { print }
72+
' "${PREPARE_SCRIPT}" > "${installer_role}"
73+
74+
if [[ ! -s "${installer_role}" ]]; then
75+
echo -e "${RED}ERROR:${NC} could not find the installer ClusterRole in ${PREPARE_SCRIPT}"
76+
exit 1
77+
fi
78+
79+
# The bundle CSV is the contract OLM applies: clusterPermissions become a
80+
# ClusterRole and permissions a namespaced Role, both created by the installer.
81+
: > "${required}"
82+
for csv in "${BUNDLE_CSVS[@]}"; do
83+
[[ -f "${csv}" ]] || continue
84+
rules_to_triples "${csv}" \
85+
'(.spec.install.spec.clusterPermissions // [])[].rules[], (.spec.install.spec.permissions // [])[].rules[]' \
86+
>> "${required}"
87+
done
88+
89+
for role in "${BUNDLE_CLUSTER_ROLES[@]}"; do
90+
[[ -f "${role}" ]] || continue
91+
rules_to_triples "${role}" '.rules[]' >> "${required}"
92+
done
93+
sort -u -o "${required}" "${required}"
94+
95+
if [[ ! -s "${required}" ]]; then
96+
echo -e "${RED}ERROR:${NC} no bundle permissions found in ${BUNDLE_CSVS[*]}"
97+
exit 1
98+
fi
99+
100+
rules_to_triples "${installer_role}" '.rules[]' > "${granted}"
101+
102+
missing="$(comm -23 "${required}" "${granted}")"
103+
if [[ -n "${missing}" ]]; then
104+
echo -e "${RED}The OLM v1 installer ClusterRole cannot grant:${NC}"
105+
while IFS='|' read -r group resource verb; do
106+
printf ' %s %s [%s]\n' "${group:-core}" "${resource}" "${verb}"
107+
done <<< "${missing}"
108+
echo
109+
echo "Add the missing rules to the installer ClusterRole in"
110+
echo " ${PREPARE_SCRIPT#"${REPO_ROOT}/"}"
111+
echo "so OLM v1 can install the bundle. Without them the operator ClusterRole"
112+
echo "creation is refused and the operator runs with no permissions."
113+
exit 1
114+
fi
115+
116+
echo -e "${GREEN}Installer ClusterRole covers every bundle permission.${NC}"

0 commit comments

Comments
 (0)