-
Notifications
You must be signed in to change notification settings - Fork 0
ci(codeql): Guardrail gegen Default-Setup Drift #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
af7655d
ci(codeql): Guardrail gegen Default-Setup Drift
github-actions[bot] 91611aa
ci(codeql): GH token permissions fuer default-setup API
github-actions[bot] b18aafe
ci(codeql): Optional PAT fuer default-setup endpoint
github-actions[bot] 4c58134
ci(codeql): 403 detection ohne rg
github-actions[bot] 6df7834
docs(security): PAT requirement fuer CodeQL default-setup guardrail
github-actions[bot] bdd942e
ci(codeql): Review-Fixes (Idempotenz/Robustheit/Evidence)
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| name: codeql-default-setup-guardrail | ||
|
|
||
| on: | ||
| schedule: | ||
| # Daily drift detection for security settings that cannot be protected by branch rules. | ||
| - cron: "17 4 * * *" | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| issues: write | ||
|
|
||
| concurrency: | ||
| group: codeql-default-setup-guardrail | ||
| cancel-in-progress: false | ||
|
|
||
| defaults: | ||
| run: | ||
| shell: bash | ||
|
|
||
| jobs: | ||
| guardrail: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 | ||
|
|
||
| - name: Read CodeQL Default Setup State | ||
| id: state | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN: ${{ secrets.CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN }} | ||
| run: bash tools/ci/codeql-default-setup-guardrail/get_state.sh | ||
|
|
||
| - name: Open Issue On Drift (idempotent) | ||
| if: steps.state.outputs.drift == 'true' | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| GITHUB_REPOSITORY: ${{ github.repository }} | ||
| CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN: ${{ secrets.CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN }} | ||
| run: bash tools/ci/codeql-default-setup-guardrail/ensure_issue.sh | ||
|
|
||
| - name: Fail Run On Drift (fail-closed) | ||
| if: steps.state.outputs.drift == 'true' | ||
| run: exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # CodeQL Default Setup Guardrail | ||
|
|
||
| ## Ziel | ||
| Dieses Repository nutzt ein **CodeQL Advanced Setup** (`.github/workflows/codeql.yml`) fuer C# (build-mode `manual`). | ||
| GitHub **CodeQL Default Setup** muss deshalb **deaktiviert** sein (`state=not-configured`), sonst werden Advanced-SARIF Uploads abgelehnt. | ||
|
|
||
| ## Verifikation | ||
| ```bash | ||
| gh api repos/{owner}/{repo}/code-scanning/default-setup --jq '{state,updated_at,schedule}' | ||
| ``` | ||
|
|
||
| Erwartung: | ||
| - `state: "not-configured"` | ||
|
|
||
| ## Guardrails | ||
| - Merge-Gate (fail-closed): `tools/ci/check-codeql-default-setup.sh` ist Teil von `preflight`. | ||
| - Drift Detection: `.github/workflows/codeql-default-setup-guardrail.yml` laeuft taeglich und erstellt bei Drift ein Issue. | ||
|
|
||
| ## Token / Permissions (wichtig) | ||
| Der GitHub API Endpoint `GET /repos/{owner}/{repo}/code-scanning/default-setup` ist in GitHub Actions mit dem standardmaessigen `GITHUB_TOKEN` in diesem Repo nicht erreichbar (typisch: `HTTP 403 Resource not accessible by integration`). | ||
|
|
||
| Damit der Guardrail in PR-CI und im scheduled Workflow deterministisch funktioniert, ist ein Fine-Grained PAT als Repo-Secret erforderlich: | ||
| - Secret-Name: `CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN` | ||
| - Minimalrechte (Repository permissions): | ||
| - Administration: Read-only | ||
| - Security events: Read-only | ||
|
|
||
| Wiring: | ||
| - PR-CI: `.github/workflows/ci.yml` setzt `CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN` als env fuer `preflight`. | ||
| - Scheduled: `.github/workflows/codeql-default-setup-guardrail.yml` nutzt `CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN`, falls vorhanden. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
| export LC_ALL=C | ||
|
|
||
| ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)" | ||
| OUT_DIR="${ROOT_DIR}/artifacts/ci/preflight/codeql-default-setup-guardrail" | ||
| RAW_LOG="${OUT_DIR}/raw.log" | ||
| SUMMARY_MD="${OUT_DIR}/summary.md" | ||
| RESULT_JSON="${OUT_DIR}/result.json" | ||
| DEFAULT_SETUP_JSON="${OUT_DIR}/default-setup.json" | ||
|
|
||
| mkdir -p "${OUT_DIR}" | ||
| : > "${RAW_LOG}" | ||
|
|
||
| BASE_GH_TOKEN="${GH_TOKEN:-}" | ||
| CODEQL_TOKEN="${CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN:-}" | ||
|
|
||
| log() { | ||
| printf '%s\n' "$*" | tee -a "${RAW_LOG}" >/dev/null | ||
| } | ||
|
|
||
| fail() { | ||
| local reason="$1" | ||
| log "FAIL: ${reason}" | ||
| { | ||
| echo "# CodeQL Default Setup Guardrail" | ||
| echo | ||
| echo "- status: fail" | ||
| echo "- reason: ${reason}" | ||
| } > "${SUMMARY_MD}" | ||
| if [[ -f "${DEFAULT_SETUP_JSON}" ]]; then | ||
| jq -n --arg reason "${reason}" \ | ||
| '{schema_version:1,check_id:"codeql-default-setup-guardrail",status:"fail",reason:$reason,evidence_paths:["artifacts/ci/preflight/codeql-default-setup-guardrail/raw.log","artifacts/ci/preflight/codeql-default-setup-guardrail/default-setup.json","artifacts/ci/preflight/codeql-default-setup-guardrail/summary.md"]}' \ | ||
| > "${RESULT_JSON}" | ||
| else | ||
| jq -n --arg reason "${reason}" \ | ||
| '{schema_version:1,check_id:"codeql-default-setup-guardrail",status:"fail",reason:$reason,evidence_paths:["artifacts/ci/preflight/codeql-default-setup-guardrail/raw.log","artifacts/ci/preflight/codeql-default-setup-guardrail/summary.md"]}' \ | ||
| > "${RESULT_JSON}" | ||
| fi | ||
| exit 1 | ||
| } | ||
|
|
||
| if ! command -v gh >/dev/null 2>&1; then | ||
| fail "gh fehlt" | ||
| fi | ||
| if ! command -v jq >/dev/null 2>&1; then | ||
| fail "jq fehlt" | ||
| fi | ||
|
|
||
| REPO="${GITHUB_REPOSITORY:-}" | ||
| if [[ -z "${REPO}" ]]; then | ||
| origin_url="$(git -C "${ROOT_DIR}" remote get-url origin 2>/dev/null || true)" | ||
| if [[ "${origin_url}" =~ github.com[:/]([^/]+/[^/.]+)(\.git)?$ ]]; then | ||
| REPO="${BASH_REMATCH[1]}" | ||
| fi | ||
| fi | ||
| if [[ -z "${REPO}" ]]; then | ||
| fail "Repository-Slug konnte nicht bestimmt werden" | ||
| fi | ||
| if [[ ! "${REPO}" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then | ||
| fail "Ungueltiger Repository-Slug: '${REPO}' (erwartet owner/name)" | ||
| fi | ||
|
|
||
| gh_api() { | ||
| local -r endpoint="$1"; shift | ||
| if [[ -n "${CODEQL_TOKEN}" ]]; then | ||
| GH_TOKEN="${CODEQL_TOKEN}" GH_REPO="${REPO}" gh api "${endpoint}" "$@" | ||
| else | ||
| GH_REPO="${REPO}" gh api "${endpoint}" "$@" | ||
| fi | ||
| } | ||
|
|
||
| attempt=1 | ||
| delay=2 | ||
| max_attempts=3 | ||
| while true; do | ||
| if gh_api "repos/{owner}/{repo}/code-scanning/default-setup" > "${DEFAULT_SETUP_JSON}" 2>> "${RAW_LOG}"; then | ||
| break | ||
| fi | ||
| if (( attempt >= max_attempts )); then | ||
| if grep -qF "Resource not accessible by integration (HTTP 403)" "${RAW_LOG}"; then | ||
| fail "GitHub API 403 fuer CodeQL Default Setup. GITHUB_TOKEN reicht hier nicht aus; setze Secret CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN (Fine-Grained PAT, Repo: Administration Read, Security Events Read)." | ||
| fi | ||
| fail "GitHub API fuer CodeQL Default Setup fehlgeschlagen" | ||
| fi | ||
| log "WARN: API-Fehler, retry ${attempt}/${max_attempts}" | ||
| sleep "${delay}" | ||
| attempt=$((attempt + 1)) | ||
| delay=$((delay * 2)) | ||
| done | ||
|
|
||
| state="$(jq -r '.state // empty' "${DEFAULT_SETUP_JSON}")" | ||
| if [[ -z "${state}" ]]; then | ||
| fail "Ungueltige API-Antwort: state fehlt" | ||
| fi | ||
|
|
||
| if [[ "${state}" != "not-configured" ]]; then | ||
| fail "CodeQL Default Setup ist aktiv (state=${state}). Advanced-Setup erfordert state=not-configured." | ||
| fi | ||
|
|
||
| { | ||
| echo "# CodeQL Default Setup Guardrail" | ||
| echo | ||
| echo "- status: pass" | ||
| echo "- repo: ${REPO}" | ||
| echo "- state: ${state}" | ||
| } > "${SUMMARY_MD}" | ||
|
|
||
| jq -n \ | ||
| '{schema_version:1,check_id:"codeql-default-setup-guardrail",status:"pass",state:"not-configured",evidence_paths:["artifacts/ci/preflight/codeql-default-setup-guardrail/raw.log","artifacts/ci/preflight/codeql-default-setup-guardrail/default-setup.json","artifacts/ci/preflight/codeql-default-setup-guardrail/summary.md"]}' \ | ||
| > "${RESULT_JSON}" | ||
|
|
||
| log "PASS: CodeQL Default Setup ist not-configured." | ||
108 changes: 108 additions & 0 deletions
108
tools/ci/codeql-default-setup-guardrail/ensure_issue.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
tomtastisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| IFS=$'\n\t' | ||
| export LC_ALL=C | ||
|
|
||
| repo="${GITHUB_REPOSITORY:-${REPO:-}}" | ||
| if [[ -z "${repo}" ]]; then | ||
| echo "ERROR: missing GITHUB_REPOSITORY/REPO env (expected owner/name)." >&2 | ||
| exit 2 | ||
| fi | ||
| if [[ ! "${repo}" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then | ||
| echo "ERROR: invalid repo slug: '${repo}' (expected owner/name)." >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| CODEQL_TOKEN="${CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN:-}" | ||
|
|
||
| retry() { | ||
| local -r max="${1}"; shift | ||
| local -r base_sleep="${1}"; shift | ||
| local attempt=1 | ||
| local sleep_s="${base_sleep}" | ||
| while true; do | ||
| if "$@"; then | ||
| return 0 | ||
| fi | ||
| if [[ "${attempt}" -ge "${max}" ]]; then | ||
| return 1 | ||
| fi | ||
| echo "WARN: command failed (attempt ${attempt}/${max}); retrying in ${sleep_s}s..." >&2 | ||
| sleep "${sleep_s}" | ||
| attempt="$((attempt + 1))" | ||
| sleep_s="$((sleep_s * 2))" | ||
| done | ||
| } | ||
|
|
||
| gh_api() { | ||
| local -r endpoint="$1"; shift | ||
| if [[ -n "${CODEQL_TOKEN}" ]]; then | ||
| GH_TOKEN="${CODEQL_TOKEN}" GH_REPO="${repo}" gh api "${endpoint}" "$@" | ||
| else | ||
| GH_REPO="${repo}" gh api "${endpoint}" "$@" | ||
| fi | ||
| } | ||
|
|
||
| state="" | ||
| if ! state="$(retry 4 1 gh_api "repos/{owner}/{repo}/code-scanning/default-setup" --jq .state)"; then | ||
| echo "ERROR: failed to query CodeQL default-setup state via GitHub API." >&2 | ||
| exit 3 | ||
| fi | ||
| if [[ -z "${state}" ]]; then | ||
| echo "ERROR: invalid API response (missing state)." >&2 | ||
| exit 3 | ||
| fi | ||
|
|
||
| if [[ "${state}" == "not-configured" ]]; then | ||
| echo "INFO: state is not-configured; no drift issue required." | ||
| exit 0 | ||
| fi | ||
|
|
||
| title="SECURITY: CodeQL Default Setup ist aktiviert (Guardrail)" | ||
| marker="<!-- codeql-default-setup-guardrail -->" | ||
|
|
||
| body=$'Der CI-Guardrail hat festgestellt, dass **GitHub CodeQL Default Setup** aktiv ist.\n\n' | ||
| body+=$'Impact:\n- Advanced CodeQL Workflow (`.github/workflows/codeql.yml`) kann dadurch keine SARIF-Ergebnisse wie erwartet verarbeiten.\n\n' | ||
| body+=$'Fix:\n- In GitHub UI: Settings -> Code security and analysis -> CodeQL -> Default setup deaktivieren\n- Oder per API: `PATCH /repos/{owner}/{repo}/code-scanning/default-setup` mit `state=not-configured`\n\n' | ||
| body+=$'Evidence:\n- Siehe Workflow-Logs und `artifacts/ci/preflight/codeql-default-setup-guardrail/`.\n\n' | ||
| if [[ ! "${state}" =~ ^[a-z-]+$ ]]; then | ||
| state="unknown" | ||
| fi | ||
| body+="Observed state: \`${state}\`"$'\n\n' | ||
| body+="${marker}"$'\n' | ||
|
|
||
| existing="$(gh_api "repos/{owner}/{repo}/issues?state=open&per_page=100" --paginate --slurp | jq -r --arg t "${title}" '[.[] | .[] | select(.pull_request? | not) | select(.title == $t) | .number][0] // empty')" | ||
| if [[ -n "${existing}" ]]; then | ||
| echo "INFO: drift issue already open (#${existing}); nothing to do." | ||
| exit 0 | ||
| fi | ||
|
|
||
| issue_number="" | ||
| if ! issue_number="$(gh_api "repos/{owner}/{repo}/issues" -X POST -f title="${title}" -f body="${body}" --jq .number)"; then | ||
| # Benign race: another run may have created the issue after our initial check. | ||
| existing_after_create="$(gh_api "repos/{owner}/{repo}/issues?state=open&per_page=100" --paginate --slurp | jq -r --arg t "${title}" '[.[] | .[] | select(.pull_request? | not) | select(.title == $t) | .number][0] // empty')" | ||
| if [[ -n "${existing_after_create}" ]]; then | ||
| issue_number="${existing_after_create}" | ||
| echo "INFO: drift issue was created concurrently (#${issue_number}); proceeding." | ||
| else | ||
| echo "ERROR: failed to create drift issue in ${repo}." >&2 | ||
| exit 4 | ||
| fi | ||
| fi | ||
|
|
||
| echo "INFO: created drift issue #${issue_number}." | ||
|
|
||
| # Best-effort labels: only add labels that already exist, otherwise the API call would hard-fail. | ||
| desired_labels=("security" "area:pipeline") | ||
| labels_available="$(gh label list -R "${repo}" -L 200 --json name 2>/dev/null || echo '[]')" | ||
| for label in "${desired_labels[@]}"; do | ||
| if jq -er --arg label "${label}" '.[] | select(.name == $label) | true' <<<"${labels_available}" >/dev/null 2>&1; then | ||
| if gh issue edit "${issue_number}" -R "${repo}" --add-label "${label}" >/dev/null 2>&1; then | ||
| echo "INFO: added label '${label}'." | ||
| else | ||
| echo "WARN: failed to add label '${label}' to issue #${issue_number}." >&2 | ||
| fi | ||
| else | ||
| echo "INFO: label '${label}' not present in repo; skipping." | ||
| fi | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
| export LC_ALL=C | ||
|
|
||
| repo="${GITHUB_REPOSITORY:-${REPO:-}}" | ||
| if [[ -z "${repo}" ]]; then | ||
| echo "ERROR: missing GITHUB_REPOSITORY/REPO env (expected owner/name)." >&2 | ||
| exit 2 | ||
| fi | ||
tomtastisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if [[ ! "${repo}" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then | ||
| echo "ERROR: invalid repo slug: '${repo}' (expected owner/name)." >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| CODEQL_TOKEN="${CODEQL_DEFAULT_SETUP_GUARDRAIL_TOKEN:-}" | ||
|
|
||
| retry() { | ||
| local -r max="${1}"; shift | ||
| local -r base_sleep="${1}"; shift | ||
| local attempt=1 | ||
| local sleep_s="${base_sleep}" | ||
| while true; do | ||
| if "$@"; then | ||
| return 0 | ||
| fi | ||
| if [[ "${attempt}" -ge "${max}" ]]; then | ||
| return 1 | ||
| fi | ||
| echo "WARN: command failed (attempt ${attempt}/${max}); retrying in ${sleep_s}s..." >&2 | ||
| sleep "${sleep_s}" | ||
| attempt="$((attempt + 1))" | ||
| sleep_s="$((sleep_s * 2))" | ||
tomtastisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| done | ||
| } | ||
tomtastisch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| gh_api() { | ||
| local -r endpoint="$1"; shift | ||
| if [[ -n "${CODEQL_TOKEN}" ]]; then | ||
| GH_TOKEN="${CODEQL_TOKEN}" GH_REPO="${repo}" gh api "${endpoint}" "$@" | ||
| else | ||
| GH_REPO="${repo}" gh api "${endpoint}" "$@" | ||
| fi | ||
| } | ||
|
|
||
| state="" | ||
| # Backoff schedule for max=4, base=1s: 1s, 2s, 4s. | ||
| if ! state="$(retry 4 1 gh_api "repos/{owner}/{repo}/code-scanning/default-setup" --jq .state)"; then | ||
| echo "ERROR: failed to query CodeQL default-setup state via GitHub API." >&2 | ||
| exit 3 | ||
| fi | ||
| if [[ -z "${state}" ]]; then | ||
| echo "ERROR: invalid API response (missing state)." >&2 | ||
| exit 3 | ||
| fi | ||
|
|
||
| drift="false" | ||
| if [[ "${state}" != "not-configured" ]]; then | ||
| drift="true" | ||
| fi | ||
|
|
||
| echo "INFO: default-setup state='${state}', drift='${drift}'." | ||
|
|
||
| if [[ -n "${GITHUB_OUTPUT:-}" ]]; then | ||
| { | ||
| echo "state=${state}" | ||
| echo "drift=${drift}" | ||
| } >> "${GITHUB_OUTPUT}" | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.