diff --git a/scripts/validate/scan-platform-sensitive-source.sh b/scripts/validate/scan-platform-sensitive-source.sh index ca433fd..63e65d4 100644 --- a/scripts/validate/scan-platform-sensitive-source.sh +++ b/scripts/validate/scan-platform-sensitive-source.sh @@ -2,9 +2,13 @@ set -o pipefail -readonly PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT=/home/donghyeon/workspace/platform -readonly PLATFORM_SENSITIVE_SOURCE_EXACT_DOCS_ROOT=/home/donghyeon/workspace/docs/platform -readonly PLATFORM_SENSITIVE_SOURCE_EXACT_SCANNER="$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT/scripts/validate/scan-platform-sensitive-source.sh" +# The platform root is the checkout this scanner lives in, so a temporary copy +# or a CI runner scans itself. The central documentation root stays on the +# desktop host unless --docs-root or --platform-only says otherwise. +PLATFORM_SENSITIVE_SOURCE_SELF_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd -P)" +readonly PLATFORM_SENSITIVE_SOURCE_SELF_ROOT +readonly PLATFORM_SENSITIVE_SOURCE_SELF="$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT/scripts/validate/scan-platform-sensitive-source.sh" +readonly PLATFORM_SENSITIVE_SOURCE_DEFAULT_DOCS_ROOT=/home/donghyeon/workspace/docs/platform _sensitive_source_fail() { printf 'ERROR: %s\n' "$*" >&2 @@ -14,12 +18,15 @@ _sensitive_source_fail() { _sensitive_source_usage() { cat <<'USAGE' Usage: - bash scripts/validate/scan-platform-sensitive-source.sh + bash scripts/validate/scan-platform-sensitive-source.sh [--docs-root DIR | --platform-only] -Scans the complete platform source and central platform documentation trees. -Only .git and .helm directory contents, plus the exact platform-root -.superpowers/sdd scratch subtree, are excluded. Diagnostics contain file names -and finding classes only; matched values and lines are never printed. +Scans the platform checkout that contains this script and the central platform +documentation tree (default /home/donghyeon/workspace/docs/platform, or +--docs-root DIR). --platform-only skips the documentation tree, for a +temporary copy or a CI runner that has none. Only .git and .helm directory +contents, plus the platform-root .superpowers/sdd scratch subtree, are +excluded. Diagnostics contain file names and finding classes only; matched +values and lines are never printed. The production run invokes all five renderer entrypoints in private temporary contexts. Four output-producing renderers publish private handoffs. The access @@ -43,7 +50,7 @@ _sensitive_source_report_path() { local path=$1 platform_root=$2 docs_root=$3 relative if [[ "$path" == "$platform_root"/* ]]; then relative="platform/${path#"$platform_root"/}" - elif [[ "$path" == "$docs_root"/* ]]; then + elif [[ -n "$docs_root" && "$path" == "$docs_root"/* ]]; then relative="docs/${path#"$docs_root"/}" else relative="$(basename -- "$path")" @@ -82,13 +89,19 @@ structured_source = suffix in (".yaml", ".yml", ".json") python_source = suffix == ".py" shell_source = suffix in (".sh", ".bash", ".zsh") or not suffix prose_source = suffix in (".md", ".markdown", ".txt", ".rst") -embedded_python_source = path in ( - "/home/donghyeon/workspace/platform/scripts/validate/observability-smoke.sh", - "/home/donghyeon/workspace/platform/scripts/validate/test-scan-platform-sensitive-source.sh", - "/home/donghyeon/workspace/platform/scripts/validate/test-configure-keycloak-grafana-oidc.sh", - "/home/donghyeon/workspace/platform/scripts/bootstrap/create-observability-secrets.sh", - "/home/donghyeon/workspace/platform/scripts/bootstrap/configure-keycloak-grafana-oidc.sh", -) +# Shell files with embedded Python, relative to the checkout that holds this +# scanner (scanner_source is /scripts/validate/). +scanner_root = Path(scanner_source).parents[2] +embedded_python_source = path in { + str(scanner_root / relative) + for relative in ( + "scripts/validate/observability-smoke.sh", + "scripts/validate/test-scan-platform-sensitive-source.sh", + "scripts/validate/test-configure-keycloak-grafana-oidc.sh", + "scripts/bootstrap/create-observability-secrets.sh", + "scripts/bootstrap/configure-keycloak-grafana-oidc.sh", + ) +} try: raw = open(path, "rb").read() @@ -1382,9 +1395,13 @@ scan_platform_sensitive_roots() ( return 1 } done - [[ "$platform_root" == /* && "$docs_root" == /* && "$platform_root" != "$docs_root" ]] || + # An empty docs root means a platform-only scan. + local -a scan_roots=("$platform_root") + [[ -z "$docs_root" ]] || scan_roots+=("$docs_root") + [[ "$platform_root" == /* ]] && { [[ -z "$docs_root" ]] || + [[ "$docs_root" == /* && "$platform_root" != "$docs_root" ]]; } || _sensitive_source_fail 'scan roots must be distinct absolute paths' || return - for argument in "$platform_root" "$docs_root"; do + for argument in "${scan_roots[@]}"; do [[ -d "$argument" && ! -L "$argument" && "$(cd -- "$argument" && pwd -P)" == "$argument" ]] || _sensitive_source_fail "scan root is not a physical directory: $argument" || return done @@ -1397,7 +1414,7 @@ scan_platform_sensitive_roots() ( printf 'SCANNER_ERROR: file inventory hardening failed\n' >&2 return 1 } - if ! find "$platform_root" "$docs_root" \ + if ! find "${scan_roots[@]}" \ \( -type d -path "$platform_root/.superpowers/sdd" -prune \) -o \ \( -type d \( -name .git -o -name .helm \) -prune \) -o \ \( -type f -o -type l \) -print0 >"$list_file"; then @@ -1432,7 +1449,7 @@ scan_platform_sensitive_roots() ( fi finding="$(_sensitive_source_python_scan \ - "$file" "$test_file" "$PLATFORM_SENSITIVE_SOURCE_EXACT_SCANNER")" || { + "$file" "$test_file" "$PLATFORM_SENSITIVE_SOURCE_SELF")" || { printf 'SCANNER_ERROR: content scan failed for FILE=%s\n' "$report" >&2 return 1 } @@ -1706,14 +1723,23 @@ scan_platform_renderer_secrets() ( ) scan_platform_sensitive_source_main() { - (( $# == 0 )) || { + local docs_root=$PLATFORM_SENSITIVE_SOURCE_DEFAULT_DOCS_ROOT + if (( $# == 1 )) && [[ "$1" == --platform-only ]]; then + docs_root='' + elif (( $# == 2 )) && [[ "$1" == --docs-root && "$2" == /* ]]; then + docs_root=$2 + elif (( $# != 0 )); then _sensitive_source_usage >&2 return 2 - } + fi + if [[ -n "$docs_root" && ! -d "$docs_root" ]]; then + printf 'ERROR: documentation root %s does not exist; pass --docs-root DIR or --platform-only\n' \ + "$docs_root" >&2 + return 2 + fi scan_platform_sensitive_roots \ - "$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT" \ - "$PLATFORM_SENSITIVE_SOURCE_EXACT_DOCS_ROOT" >/dev/null || return - scan_platform_renderer_secrets "$PLATFORM_SENSITIVE_SOURCE_EXACT_PLATFORM_ROOT" || return + "$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" "$docs_root" >/dev/null || return + scan_platform_renderer_secrets "$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" || return printf 'PLATFORM SENSITIVE SOURCE SCAN PASS\n' } diff --git a/scripts/validate/test-scan-platform-sensitive-source.sh b/scripts/validate/test-scan-platform-sensitive-source.sh index 6a61395..0f5ad6c 100644 --- a/scripts/validate/test-scan-platform-sensitive-source.sh +++ b/scripts/validate/test-scan-platform-sensitive-source.sh @@ -1468,6 +1468,39 @@ output="$(run_scanner 2>&1)" || scanner_error_rc=$? fail 'scanner error did not fail closed' } pass 'rg errors fail closed' +PLATFORM_SENSITIVE_SOURCE_TEST_PATH='' + +[[ "$PLATFORM_SENSITIVE_SOURCE_SELF_ROOT" == "$ROOT" ]] || \ + fail 'the scanner does not scan the checkout it lives in' +pass 'the scanner scans the checkout it lives in, not a fixed path' +main_rc=0 +main_output="$(scan_platform_sensitive_source_main --unknown 2>&1)" || main_rc=$? +(( main_rc == 2 )) || fail 'an unknown entrypoint argument was accepted' +main_rc=0 +main_output="$(scan_platform_sensitive_source_main --docs-root relative/docs 2>&1)" || main_rc=$? +(( main_rc == 2 )) || fail 'a relative docs root was accepted' +new_fixture +main_rc=0 +main_output="$(scan_platform_sensitive_source_main --docs-root "$WORK/missing-docs" 2>&1)" || main_rc=$? +(( main_rc == 2 )) || fail 'a missing docs root was accepted' +[[ "$main_output" == *'--platform-only'* ]] || \ + fail 'a missing docs root did not name --platform-only' +pass 'the entrypoint rejects unknown, relative and missing docs roots before scanning' +printf 'ordinary input\n' >"$WORK/platform/bootstrap/ordinary.md" +platform_only_output="$(scan_platform_sensitive_roots "$WORK/platform" '' 2>&1)" || { + printf '%s\n' "$platform_only_output" >&2 + fail 'a platform-only root scan failed' +} +[[ "$platform_only_output" == *'PLATFORM SENSITIVE SOURCE SCAN PASS'* ]] || \ + fail 'a platform-only root scan did not emit the PASS marker' +printf 'Slack: https://hooks.%s/services/T1/B2/C3\n' 'slack.com' \ + >"$WORK/platform/bootstrap/live.env" +platform_only_rc=0 +platform_only_output="$(scan_platform_sensitive_roots "$WORK/platform" '' 2>&1)" || platform_only_rc=$? +(( platform_only_rc != 0 )) || fail 'a platform-only root scan accepted a Slack webhook' +[[ "$platform_only_output" == *'platform/bootstrap/live.env'* ]] || \ + fail 'a platform-only root scan did not report the filename' +pass 'a platform-only root scan still scans the platform root' cleanup_before="$( find /tmp -maxdepth 1 -type d -name 'platform-sensitive-source-test.??????' \