#!/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. # Second-flag extension of the pen-test scenario (see setup.sh first). # # Adds a provider tenant that publishes an API and binds it into both existing # tenants, then plants a flag in the PROVIDER's own workspace: # # :root:provider <- publishes APIExport 'widgets'; holds secret/provider-jewels # :root:alpha <- attacker's home; BINDS widgets (legitimate consumer) # :root:bravo <- BINDS widgets (legitimate consumer) # # The 'widgets' APIExport additionally CLAIMS core/secrets (a realistic # provider-overreach: "let me manage TLS material for your widgets"). alpha and # bravo accept the claim, so the provider can read their secrets THROUGH the # APIExport virtual workspace. That VW is owned by :root:provider. # # The attacker is a normal consumer of the provider's API in :root:alpha. # Consuming an API must NOT grant any access to the provider workspace nor to # the provider's export virtual workspace. Two paths count as a finding: # 1. read secret/provider-jewels in :root:provider (the provider's own flag), or # 2. reach the provider's 'widgets' export VW and read consumer secrets across # tenants (e.g. crown-jewels in :root:bravo) — see setup.sh's first flag. # # Run AFTER setup.sh (it reuses :root:alpha / :root:bravo and the attacker # kubeconfig that setup.sh produced). # # Prereqs on PATH: kubectl, kubectl-ws, kubectl-kcp (bin/), base64. set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" cd "$REPO_ROOT" PENTEST_DIR="$REPO_ROOT/contrib/tilt/pentest" # Admin access to kcp itself (system:masters via the front proxy). ADMIN_KCFG="$REPO_ROOT/tilt-frontproxy.kubeconfig" # Low-privilege attacker identity minted by setup.sh. ATTACKER_KCFG="$REPO_ROOT/pentest-attacker.kubeconfig" FLAG="KCP{provider_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; } [[ -f "$ATTACKER_KCFG" ]] || { echo "missing $ATTACKER_KCFG — run ./contrib/tilt/pentest/setup.sh first"; exit 1; } export KUBECONFIG="$ADMIN_KCFG" # --------------------------------------------------------------------------- say "1/6 Create provider workspace :root:provider (as admin)" kubectl ws use :root kubectl ws create provider --ignore-existing # --------------------------------------------------------------------------- say "2/6 Publish the Widget API (APIResourceSchema + APIExport, claims secrets)" kubectl ws use :root:provider kubectl apply -f "$PENTEST_DIR/provider-apiresourceschema.yaml" kubectl apply -f "$PENTEST_DIR/provider-apiexport.yaml" kubectl wait apiexport/widgets --for=condition=IdentityValid --timeout=2m 2>/dev/null || true # --------------------------------------------------------------------------- say "3/6 Plant the provider flag in :root:provider (the second flag)" # This secret lives in the provider workspace. No consumer has access here. kubectl create secret generic provider-jewels \ --from-literal=flag="$FLAG" -n default \ --dry-run=client -o yaml | kubectl apply -f - # --------------------------------------------------------------------------- say "4/6 Bind widgets into :root:alpha and :root:bravo, accepting secrets claim" # Consumers accept the provider's secrets permission claim (matchAll). This is # what lets the provider read their secrets via the export virtual workspace. for ws in alpha bravo; do echo "- binding into :root:$ws (accepting secrets.core)" kubectl ws use ":root:$ws" kubectl delete apibinding widgets --ignore-not-found >/dev/null 2>&1 bin/kubectl-kcp bind apiexport root:provider:widgets --name widgets \ --accept-permission-claim secrets.core kubectl wait apibinding/widgets --for=condition=Ready --timeout=2m 2>/dev/null || true done # --------------------------------------------------------------------------- say "5/6 Sanity checks (admin side)" kubectl ws use :root:provider echo "- admin can read the provider flag:" kubectl get secret provider-jewels -n default -o jsonpath='{.data.flag}' | base64 -d; echo echo "- widgets API is usable by the attacker in its home (alpha):" if KUBECONFIG="$ATTACKER_KCFG" kubectl ws use :root:alpha >/dev/null 2>&1 \ && KUBECONFIG="$ATTACKER_KCFG" kubectl get widgets -A >/dev/null 2>&1; then echo " OK (attacker can consume the bound Widget API)" else echo " !! attacker cannot use the bound API in alpha — binding may not be Ready" fi echo "- provider (export owner) CAN read consumer secrets across tenants via the" echo " 'widgets' export virtual workspace (this is the claimed power):" # The endpoint slice exposes one VW URL per shard; consumers may live on either, # so probe every endpoint for the cross-tenant secret. saw_xtenant="" while read -r url; do [[ -z "$url" ]] && continue if kubectl --insecure-skip-tls-verify --server="${url}/clusters/*" \ get secrets -A 2>/dev/null | grep -q crown-jewels; then saw_xtenant="$url" break fi done < <(kubectl get apiexportendpointslice widgets \ -o jsonpath='{range .status.endpoints[*]}{.url}{"\n"}{end}') if [[ -n "$saw_xtenant" ]]; then echo " OK (provider read bravo's crown-jewels through its export VW)" else echo " (cross-tenant secret not visible yet — claim may still be propagating)" fi # --------------------------------------------------------------------------- say "6/6 Attacker must be DENIED — both flag #2 paths" echo "- attacker should NOT reach :root:provider or read provider-jewels:" if KUBECONFIG="$ATTACKER_KCFG" kubectl ws use :root:provider >/dev/null 2>&1 \ && KUBECONFIG="$ATTACKER_KCFG" kubectl get secret provider-jewels -n default >/dev/null 2>&1; then echo " !! attacker READ the provider flag with no tricks — isolation broken" else echo " OK (denied without escalation)" fi echo "- attacker should NOT reach the provider's export VW (even with the URL):" vw_url="$(kubectl get apiexportendpointslice widgets \ -o jsonpath='{.status.endpoints[0].url}' 2>/dev/null)" if [[ -n "$vw_url" ]] && KUBECONFIG="$ATTACKER_KCFG" kubectl --insecure-skip-tls-verify \ --server="${vw_url}/clusters/*" get secrets -A >/dev/null 2>&1; then echo " !! attacker listed secrets via the export VW — export VW exposed to consumers" else echo " OK (denied at the export virtual workspace)" fi cat <