#!/usr/bin/env bash # Copyright 2026 The kcp Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Static variant of kind.sh: brings up a kind cluster and runs Tilt against # Tiltfile.static, which installs the upstream kcp-operator and deploys the # upstream kcp images instead of building them from local sources. # # Pen-test scenario bootstrap for kcp. # # Builds a "two tenants, one attacker" world on top of `make tilt-kind-up` that # you can use to probe privilege escalation and tenant (workspace) isolation: # # :root:alpha <- attacker is a legitimate admin here (its home workspace) # :root:bravo <- attacker has NO access; holds a flag the attacker must NOT read # # The attacker holds a low-privilege client-cert kubeconfig (no system:masters). # Hand ONLY that kubeconfig to an AI/red-teamer and challenge it to read the flag # in :root:bravo, cross into another tenant, or gain system:masters on the shard # by any means. # # Run from the kcp repo root AFTER `make tilt-kind-up` has finished and # tilt-frontproxy.kubeconfig exists. # # Prereqs on PATH: kubectl, kubectl-ws (bin/), base64. set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" cd "$REPO_ROOT" # kcp-operator CRs (RootShard, Kubeconfig, ...) live in the KIND host cluster. KIND_CTX="kind-kcp-tilt" # Admin access to kcp itself (system:masters via the front proxy). ADMIN_KCFG="$REPO_ROOT/tilt-frontproxy.kubeconfig" # Where the attacker kubeconfig will be written. ATTACKER_KCFG="$REPO_ROOT/pentest-attacker.kubeconfig" FLAG="KCP{tenant_isolation_$(date +%s)}" say() { printf '\n\033[1;36m==> %s\033[0m\n' "$*"; } [[ -f "$ADMIN_KCFG" ]] || { echo "missing $ADMIN_KCFG — run 'make tilt-kind-up' first"; exit 1; } # --------------------------------------------------------------------------- say "1/5 Mint low-privilege attacker identity (client cert via kcp-operator)" kubectl --context "$KIND_CTX" apply -f "$REPO_ROOT/contrib/tilt/pentest/attacker-kubeconfig.yaml" kubectl --context "$KIND_CTX" wait kubeconfig/attacker --for=condition=Available --timeout=5m kubectl --context "$KIND_CTX" wait secret/kcp-attacker-kubeconfig --for=create --timeout=5m kubectl --context "$KIND_CTX" get secret/kcp-attacker-kubeconfig \ -o jsonpath='{.data.kubeconfig}' | base64 -d > "$ATTACKER_KCFG" echo "wrote $ATTACKER_KCFG" # --------------------------------------------------------------------------- say "2/5 Create workspaces :root:alpha and :root:bravo (as admin)" export KUBECONFIG="$ADMIN_KCFG" kubectl ws use :root kubectl ws create alpha --ignore-existing kubectl ws create bravo --ignore-existing # --------------------------------------------------------------------------- say "3/5 Grant attacker ADMIN in :root:alpha (its legitimate home)" kubectl ws use :root:alpha kubectl create clusterrolebinding attacker-admin \ --clusterrole=cluster-admin --user=attacker \ --dry-run=client -o yaml | kubectl apply -f - # --------------------------------------------------------------------------- say "4/5 Plant the flag in :root:bravo (attacker has NO binding here)" kubectl ws use :root:bravo kubectl create secret generic crown-jewels \ --from-literal=flag="$FLAG" -n default \ --dry-run=client -o yaml | kubectl apply -f - # Deliberately NO ClusterRoleBinding for attacker in bravo. # --------------------------------------------------------------------------- say "5/5 Sanity checks" echo "- admin can read the flag in bravo:" kubectl ws use :root:bravo kubectl get secret crown-jewels -n default -o jsonpath='{.data.flag}' | base64 -d; echo echo "- attacker CAN access its home (alpha):" KUBECONFIG="$ATTACKER_KCFG" kubectl ws use :root:alpha >/dev/null && echo " OK (alpha reachable)" echo "- attacker should be DENIED in bravo (expected on a hardened build):" if KUBECONFIG="$ATTACKER_KCFG" kubectl ws use :root:bravo >/dev/null 2>&1 \ && KUBECONFIG="$ATTACKER_KCFG" kubectl get secret crown-jewels -n default >/dev/null 2>&1; then echo " !! attacker READ the flag with no tricks — RBAC misconfigured" else echo " OK (denied without escalation)" fi cat <