|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# --------------------------------------------------------- |
| 4 | +# Pre-flight Check: Verify jq is installed |
| 5 | +# --------------------------------------------------------- |
| 6 | +if ! command -v jq &> /dev/null; then |
| 7 | + printf "Error: 'jq' is not installed.\n" |
| 8 | + printf "This script requires jq to parse Kubernetes JSON output.\n" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +# --------------------------------------------------------- |
| 13 | +# CONFIGURATION |
| 14 | +# --------------------------------------------------------- |
| 15 | +TARGET_API="argoproj.io" |
| 16 | +TARGET_RESOURCE="applications" |
| 17 | +TARGET_LABEL_KEY="app.kubernetes.io/part-of" |
| 18 | +TARGET_LABEL_VAL="argocd" |
| 19 | + |
| 20 | +printf "=========================================================\n" |
| 21 | +printf "SEARCH CRITERIA (Must match ALL):\n" |
| 22 | +printf " 1. API/Resource: %s / %s\n" "$TARGET_API" "$TARGET_RESOURCE" |
| 23 | +printf " 2. Label: %s=%s\n" "$TARGET_LABEL_KEY" "$TARGET_LABEL_VAL" |
| 24 | +printf " 3. Scope: Cross-namespace only\n" |
| 25 | +printf "=========================================================\n" |
| 26 | + |
| 27 | +printf "\nScanning Cluster (this may take a moment)...\n" |
| 28 | + |
| 29 | +# --------------------------------------------------------- |
| 30 | +# STEP 1: FIND CANDIDATE ROLES |
| 31 | +# --------------------------------------------------------- |
| 32 | +CANDIDATE_ROLES_JSON=$(oc get roles -A -o json | jq -r --arg API "$TARGET_API" \ |
| 33 | + --arg RES "$TARGET_RESOURCE" \ |
| 34 | + --arg L_KEY "$TARGET_LABEL_KEY" \ |
| 35 | + --arg L_VAL "$TARGET_LABEL_VAL" ' |
| 36 | + [ |
| 37 | + .items[] | |
| 38 | + select( |
| 39 | + (.metadata.labels?[$L_KEY] == $L_VAL) |
| 40 | + and |
| 41 | + ( |
| 42 | + .rules[]? | |
| 43 | + ( (.apiGroups[]? == $API) or (.apiGroups[]? == "*") ) and |
| 44 | + ( (.resources[]? == $RES) or (.resources[]? == "*") ) |
| 45 | + ) |
| 46 | + ) | |
| 47 | + "\(.metadata.namespace)/\(.metadata.name)" |
| 48 | + ] | unique |
| 49 | +') |
| 50 | + |
| 51 | +# If no candidate roles exist, we can exit early |
| 52 | +if [ "$CANDIDATE_ROLES_JSON" == "[]" ]; then |
| 53 | + printf " • No Roles found matching label/rule criteria.\n" |
| 54 | + exit 0 |
| 55 | +fi |
| 56 | + |
| 57 | +# --------------------------------------------------------- |
| 58 | +# FIND BINDINGS |
| 59 | +# --------------------------------------------------------- |
| 60 | +# We process ALL bindings, but filter down to only those that: |
| 61 | +# a) Point to a "Candidate Role" found in Step 1 |
| 62 | +# b) Have at least one Subject in a DIFFERENT namespace |
| 63 | +# We save this filtered JSON array to a variable. |
| 64 | +TARGET_BINDINGS_JSON=$(oc get rolebindings -A -o json | jq --argjson TARGET_ROLES "$CANDIDATE_ROLES_JSON" ' |
| 65 | + [ |
| 66 | + .items[] | |
| 67 | + (.metadata.namespace + "/" + .roleRef.name) as $localRef | |
| 68 | + .metadata.namespace as $binding_ns | |
| 69 | +
|
| 70 | + # Filter A: Must reference one of our Candidate Roles |
| 71 | + select( |
| 72 | + .roleRef.kind == "Role" and |
| 73 | + ($localRef as $ref | $TARGET_ROLES | index($ref)) |
| 74 | + ) | |
| 75 | +
|
| 76 | + # Filter B: Must have at least one cross-namespace ServiceAccount |
| 77 | + select( |
| 78 | + [ |
| 79 | + .subjects[]? | |
| 80 | + select(.kind == "ServiceAccount" and .namespace != $binding_ns) |
| 81 | + ] | length > 0 |
| 82 | + ) |
| 83 | + ] |
| 84 | +') |
| 85 | + |
| 86 | +# --------------------------------------------------------- |
| 87 | +# OUTPUT ROLES |
| 88 | +# --------------------------------------------------------- |
| 89 | +printf "\nRoles with cross-namespace access:\n" |
| 90 | + |
| 91 | +# We extract the unique list of roles strictly from the OFFENDING bindings. |
| 92 | +VERIFIED_ROLES=$(echo "$TARGET_BINDINGS_JSON" | jq -r ' |
| 93 | + [ .[] | "\(.metadata.namespace)/\(.roleRef.name)" ] | unique |
| 94 | +') |
| 95 | + |
| 96 | +if [ "$VERIFIED_ROLES" == "[]" ]; then |
| 97 | + printf " • No cross-namespace bindings found for the candidate roles.\n" |
| 98 | + printf "Scan Complete.\n" |
| 99 | + exit 0 |
| 100 | +else |
| 101 | + echo "$VERIFIED_ROLES" | jq -r '.[] | " • Role: \((.))"' |
| 102 | +fi |
| 103 | + |
| 104 | +# --------------------------------------------------------- |
| 105 | +# OUTPUT BINDINGS |
| 106 | +# --------------------------------------------------------- |
| 107 | +printf "\nCross-namespace bindings detail:\n" |
| 108 | + |
| 109 | +echo "$TARGET_BINDINGS_JSON" | jq -r ' |
| 110 | + .[] | |
| 111 | + .metadata.namespace as $binding_ns | |
| 112 | +
|
| 113 | + # Calculate aggregate list of external namespaces for summary |
| 114 | + ( |
| 115 | + [ |
| 116 | + .subjects[]? | |
| 117 | + select(.kind == "ServiceAccount" and .namespace != $binding_ns) | |
| 118 | + .namespace |
| 119 | + ] |
| 120 | + | unique |
| 121 | + | join(", ") |
| 122 | + ) as $external_namespaces | |
| 123 | +
|
| 124 | + "--------------------------------------------------", |
| 125 | + "BINDING: \(.metadata.namespace) / \(.metadata.name)", |
| 126 | + "ROLE REF: \(.roleRef.name)", |
| 127 | + "SUBJECTS (cross-namespace only):", |
| 128 | + ( |
| 129 | + .subjects[]? | |
| 130 | + # Print only external service accounts |
| 131 | + if (.kind == "ServiceAccount" and .namespace != $binding_ns) then |
| 132 | + " • \(.kind): \(.name) (ns: \(.namespace))" |
| 133 | + else |
| 134 | + empty |
| 135 | + end |
| 136 | + ), |
| 137 | + "", |
| 138 | + "• Namespace \($external_namespaces) has access to \(.metadata.namespace)", |
| 139 | + "" |
| 140 | +' |
0 commit comments