Repository Public
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : QYVORA8# File : security/audit/ci/post-receive-audit.sh9# Module : Security10# Repository : https://git.spboucher.ai/qyvora11# Project Website : Not specified12# Created : 2026-12-0413# Last Modified : 2026-12-0414#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: contact@spboucher.ai23# Development : dev@spboucher.ai24# Support : support@spboucher.ai25# Security/Admin : admin@spboucher.ai26#27# Copyright (c) 202628# Simon-Pierre Boucher29# All rights reserved where applicable. This file is governed by the30# applicable project license, copyright notices and relevant law.31# Existing third-party rights and notices remain unaffected.32#33# License : None specified (no license file in this repository)34#35# Do not embed credentials, private keys, access tokens,36# certificates or confidential personal information.37#38# =============================================================================39# END OF SP BOUCHER SOURCE HEADER40# =============================================================================4142# QYVORA — server-side security audit, meant to be called from the `post-receive` hook of the bare43# repository on the forge (spbgit, gateway M1M32). NOT INSTALLED BY DEFAULT: see security/README.md44# ("Server-side integration") for the one-line installation the owner must approve.45#46# For every pushed branch listed in QY_CI_BRANCHES, the pushed revision is cloned into a scratch47# directory on a build node, `Scripts/security-audit.sh --quick` runs there, and the outcome is written48# to a status file on the forge. The push itself is never blocked or slowed: the audit runs detached.49#50# stdin: "<old-sha> <new-sha> <ref>" lines, as Git gives them to post-receive51# env: QY_CI_CLONE_URL what the build node clones (default: gitsrv:srv/git/qyvora.git)52# QY_CI_BUILD_NODE SSH alias of the build node, or "local" (default: M3U96b)53# QY_CI_WORK work directory on the build node (default: qyvora-build/ci)54# QY_CI_STATUS_DIR where status files are written here (default: ~/qyvora-audit-status)55# QY_CI_BRANCHES refs that trigger the audit (default: "refs/heads/main")56# QY_CI_AUDIT_ARGS arguments of security-audit.sh (default: "--quick")57# QY_CI_FOREGROUND 1 = wait for the result (tests) (default: 0, detached)58#59# Status files: <status-dir>/<sha>.json and <status-dir>/latest-<branch>.json60# {"commit", "ref", "node", "started", "finished", "exit_status", "gate_passed", "blockers", "summary", "log"}61# The build node keeps a persistent tool directory (<work>/tools, linked as security/.tools) so that the62# pinned tools are installed once (security/audit/install-tools.sh), and removes each scratch clone after use.63# Nothing is written outside <work> on the build node and outside <status-dir> on the forge.6465set -euo pipefail6667CLONE_URL="${QY_CI_CLONE_URL:-gitsrv:srv/git/qyvora.git}"68NODE="${QY_CI_BUILD_NODE:-M3U96b}"69WORK="${QY_CI_WORK:-qyvora-build/ci}"70STATUS_DIR="${QY_CI_STATUS_DIR:-$HOME/qyvora-audit-status}"71BRANCHES="${QY_CI_BRANCHES:-refs/heads/main}"72AUDIT_ARGS="${QY_CI_AUDIT_ARGS:---quick}"73FOREGROUND="${QY_CI_FOREGROUND:-0}"74ZERO="0000000000000000000000000000000000000000"7576mkdir -p "$STATUS_DIR"7778# The job executed on the build node (or locally): prints one JSON status object on its last line.79remote_job() {80 cat <<'JOB'81set -u82sha="$1"; ref="$2"; url="$3"; work="$4"; shift 483started="$(date -u +%Y-%m-%dT%H:%M:%SZ)"84mkdir -p "$work/tools" "$work/runs"85dir="$work/runs/$sha"86rm -rf "$dir"87log="$work/runs/$sha.log"88status=12589{90 git clone --quiet "$url" "$dir" && git -C "$dir" checkout --quiet --detach "$sha" || exit 12591 rm -rf "$dir/security/.tools"92 ln -s "$(cd "$work/tools" && pwd)" "$dir/security/.tools"93 cd "$dir"94 [ -d security/.tools/venv ] || bash security/audit/install-tools.sh || true95 QY_AUDIT_SCRATCH="$(cd .. && pwd)/build-cache" QY_AUDIT_REPORTS="$dir/ci-reports" bash Scripts/security-audit.sh "$@"96} > "$log" 2>&197status=$?98finished="$(date -u +%Y-%m-%dT%H:%M:%SZ)"99python3 - "$dir/ci-reports/SECURITY_AUDIT_RESULTS.json" "$sha" "$ref" "$started" "$finished" "$status" "$log" <<'PY'100import json, socket, sys101path, sha, ref, started, finished, status, log = sys.argv[1:8]102out = {"commit": sha, "ref": ref, "node": socket.gethostname(), "started": started, "finished": finished,103 "exit_status": int(status), "gate_passed": None, "blockers": [], "summary": None, "log": log}104try:105 doc = json.load(open(path))106 out["gate_passed"] = doc["gate"]["passed"]107 out["blockers"] = doc["gate"]["blockers"]108 s = doc["summary"]109 out["summary"] = {"tools_run": len(s["tools_run"]), "tools_not_run": [t["tool"] for t in s["tools_not_run"]],110 "tools_failed": [t["tool"] for t in s["tools_failed"]], "test_cases": s["tests"]["cases"],111 "findings_open": s["findings"]["open"], "audited_commit": doc["audit"]["git_commit"], "mode": doc["audit"]["mode"]}112except Exception as error: # no results document: the log says why113 out["error"] = "no results document: " + str(error)114print(json.dumps(out))115PY116rm -rf "$dir"117JOB118}119120run_one() {121 local sha="$1" ref="$2" branch out122 branch="$(basename "$ref")"123 if [ "$NODE" = "local" ]; then124 out="$(remote_job | bash -s -- "$sha" "$ref" "$CLONE_URL" "$WORK" $AUDIT_ARGS | tail -n 1)"125 else126 out="$(remote_job | ssh -o BatchMode=yes -o ConnectTimeout=20 "$NODE" "zsh -lc 'bash -s -- $sha $ref $CLONE_URL $WORK $AUDIT_ARGS'" | tail -n 1)"127 fi128 if [ -z "$out" ]; then129 out="{\"commit\": \"$sha\", \"ref\": \"$ref\", \"node\": \"$NODE\", \"exit_status\": 255, \"gate_passed\": null, \"error\": \"the build node returned nothing (unreachable?)\"}"130 fi131 printf '%s\n' "$out" > "$STATUS_DIR/$sha.json"132 cp "$STATUS_DIR/$sha.json" "$STATUS_DIR/latest-$branch.json"133}134135while read -r old new ref; do136 [ -z "${new:-}" ] && continue137 [ "$new" = "$ZERO" ] && continue # branch deletion138 case " $BRANCHES " in *" $ref "*) ;; *) continue ;; esac139 printf '{"commit": "%s", "ref": "%s", "node": "%s", "state": "running", "started": "%s"}\n' \140 "$new" "$ref" "$NODE" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$STATUS_DIR/$new.json"141 if [ "$FOREGROUND" = "1" ]; then142 run_one "$new" "$ref"143 else144 ( run_one "$new" "$ref" ) </dev/null >/dev/null 2>&1 &145 disown || true146 echo "security audit of ${new:0:12} started on $NODE — status: $STATUS_DIR/$new.json"147 fi148done149