Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/installer-rbac-check.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions .rhdh/scripts/prepare-restricted-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]
Expand All @@ -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"]
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
116 changes: 116 additions & 0 deletions hack/check-installer-rbac.sh
Original file line number Diff line number Diff line change
@@ -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)"

Check warning on line 45 in hack/check-installer-rbac.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Redirect this error message to stderr (>&2).

See more on https://sonarcloud.io/project/issues?id=redhat-developer_rhdh-operator&issues=AaFta-M6Lhxv2LRQDfBG&open=AaFta-M6Lhxv2LRQDfBG&pullRequest=3923
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 <</ && /clusterRole\.yaml/ { in_role = 1; next }
in_role && /^EOF$/ { exit }
in_role { print }
' "${PREPARE_SCRIPT}" > "${installer_role}"

if [[ ! -s "${installer_role}" ]]; then
echo -e "${RED}ERROR:${NC} could not find the installer ClusterRole in ${PREPARE_SCRIPT}"

Check warning on line 75 in hack/check-installer-rbac.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Redirect this error message to stderr (>&2).

See more on https://sonarcloud.io/project/issues?id=redhat-developer_rhdh-operator&issues=AaFta-M6Lhxv2LRQDfBH&open=AaFta-M6Lhxv2LRQDfBH&pullRequest=3923
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[*]}"

Check warning on line 96 in hack/check-installer-rbac.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Redirect this error message to stderr (>&2).

See more on https://sonarcloud.io/project/issues?id=redhat-developer_rhdh-operator&issues=AaFta-M6Lhxv2LRQDfBK&open=AaFta-M6Lhxv2LRQDfBK&pullRequest=3923
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}"
Loading