#!/bin/bash # Installs a gitleaks pre-receive hook on every Gitea repo (current and future). # Gitea has no global hooks dir, so a systemd timer symlinks the hook into each # repo's hooks/pre-receive.d every 5 minutes. Pull-mirror syncs never run it. set -euo pipefail [ "$(id -u)" = 0 ] || { echo "Run with sudo." >&2; exit 1; } BASE="https://softeryx.xyz/joseph/gitea-hygiene/raw/branch/main" VERSION=8.30.1 APP_INI="${APP_INI:-/etc/gitea/app.ini}" case "$(uname -m)" in x86_64) ARCH=x64 ;; aarch64|arm64) ARCH=arm64 ;; *) echo "Unsupported arch $(uname -m)" >&2; exit 1 ;; esac # 1. gitleaks binary, checksum-verified TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT ASSET="gitleaks_${VERSION}_linux_${ARCH}.tar.gz" curl -fsSL -o "$TMP/$ASSET" "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/${ASSET}" curl -fsSL -o "$TMP/sums.txt" "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/gitleaks_${VERSION}_checksums.txt" ( cd "$TMP" && grep " ${ASSET}\$" sums.txt | sha256sum -c - ) tar -xzf "$TMP/$ASSET" -C "$TMP" gitleaks install -o root -g root -m 755 "$TMP/gitleaks" /usr/local/bin/gitleaks echo "Installed $(/usr/local/bin/gitleaks version)" # 2. The hook itself, in one shared place install -d -o root -g root -m 755 /usr/local/lib/gitea-hooks/pre-receive.d curl -fsSL -o "$TMP/hook" "$BASE/pre-receive.d/gitleaks" install -o root -g git -m 750 "$TMP/hook" /usr/local/lib/gitea-hooks/pre-receive.d/gitleaks # 3. Find the repository root from app.ini ROOT="$(awk -F= '/^\[/{s=$0} s=="[repository]" && $1~/^[ \t]*ROOT[ \t]*$/{gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2}' "$APP_INI" 2>/dev/null || true)" ROOT="${ROOT:-/var/lib/gitea/data/gitea-repositories}" [ -d "$ROOT" ] || { echo "Repo root $ROOT not found; set APP_INI or edit /usr/local/sbin/gitea-hooks-sync" >&2; exit 1; } # 4. Sync script + timer cat > /usr/local/sbin/gitea-hooks-sync < /etc/systemd/system/gitea-hooks-sync.service <<'UNIT' [Unit] Description=Link shared pre-receive hooks into Gitea repos [Service] Type=oneshot ExecStart=/usr/local/sbin/gitea-hooks-sync UNIT cat > /etc/systemd/system/gitea-hooks-sync.timer <<'UNIT' [Unit] Description=Keep Gitea shared hooks linked [Timer] OnBootSec=1min OnUnitActiveSec=5min [Install] WantedBy=timers.target UNIT systemctl daemon-reload systemctl enable --now gitea-hooks-sync.timer >/dev/null /usr/local/sbin/gitea-hooks-sync echo "Done. To turn it off: sudo systemctl disable --now gitea-hooks-sync.timer && sudo rm /usr/local/lib/gitea-hooks/pre-receive.d/gitleaks"