Files
shared-actions/.gitea/workflows/python-dependency-check.yml
T
LucaZanni 8b63ed6820 🐛 fix(ci): extra-index-gitea letto come env, non nel campo if dello step
- nel workflow_call di act_runner il with: del chiamante non arriva a inputs nel campo if: lo step "Registry PyPI di Gitea" veniva saltato (run 67345 di python-chatterbox-api)
- lo step gira sempre, legge EXTRA_INDEX_GITEA dall'env e termina se non e' true (confronto case-insensitive)
- il secret BuildKit passa per l'output secret_files dello step, la pulizia del netrc gira con if: always()

Fixes #18 @30m
2026-10-02 02:16:53 +02:00

159 lines
6.4 KiB
YAML

name: Python Dependency Audit
on:
workflow_call:
inputs:
python-version:
type: string
default: '3.11'
install-extras:
type: string
default: 'dev'
working-directory:
type: string
default: '.'
extra-index-gitea:
description: >-
Aggiunge il registry PyPI privato di Gitea (PZeta_Touch) come indice extra di pip,
autenticato con i secrets G_USER / NPM_TOKEN (serve 'secrets: inherit').
type: boolean
default: false
workflow_dispatch:
jobs:
python-dependency-audit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- name: Checkout
uses: https://gitea.com/actions/checkout@v6
- name: Setup Python
uses: https://github.com/actions/setup-python@v5.2.0
with:
python-version: ${{ inputs.python-version || '3.11' }}
# Registry PyPI privato di Gitea (#17). Le credenziali vanno in un file netrc e non
# nell'URL: PIP_EXTRA_INDEX_URL resta senza segreti, pip legge utente e token dal file
# indicato da NETRC, e nulla di sensibile passa per l'env esportato o per i log.
# L'input si legge come variabile d'ambiente e non nel campo `if:` dello step: nel
# workflow_call di act_runner i valori `with:` del chiamante non arrivano al contesto
# `inputs` in quella posizione (stesso problema di owns-auth-roles in quality-gates.yml).
- name: Registry PyPI di Gitea
id: pypi-gitea
env:
EXTRA_INDEX_GITEA: ${{ inputs.extra-index-gitea }}
G_USER: ${{ secrets.G_USER }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
if ! printf "%s" "$EXTRA_INDEX_GITEA" | grep -qix "true"; then
echo "extra-index-gitea non attivo: solo PyPI pubblico."
exit 0
fi
if [ -z "$G_USER" ] || [ -z "$NPM_TOKEN" ]; then
echo "::error::extra-index-gitea richiede i secrets G_USER e NPM_TOKEN: serve 'secrets: inherit' nel workflow chiamante."
exit 1
fi
NETRC_FILE="$RUNNER_TEMP/pip-gitea.netrc"
( umask 077; printf 'machine gitea.pzetatouch.it\nlogin %s\npassword %s\n' "$G_USER" "$NPM_TOKEN" > "$NETRC_FILE" )
echo "NETRC=$NETRC_FILE" >> "$GITHUB_ENV"
echo "PIP_EXTRA_INDEX_URL=https://gitea.pzetatouch.it/api/packages/PZeta_Touch/pypi/simple/" >> "$GITHUB_ENV"
echo "secret_files=pip_netrc=$NETRC_FILE" >> "$GITHUB_OUTPUT"
echo "Registry PyPI di Gitea aggiunto come indice extra (credenziali da netrc)."
- name: Cache pip
uses: https://github.com/actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ inputs.python-version }}-${{ hashFiles('**/pyproject.toml', '**/requirements*.txt') }}
restore-keys: |
${{ runner.os }}-pip-${{ inputs.python-version }}-
${{ runner.os }}-pip-
- name: Install dependencies (isolated venv)
run: |
# venv pulita: audita SOLO le dipendenze dichiarate dal repo,
# non l'ambiente globale del runner (che su runner ML/GPU è contaminato)
python -m venv .audit-venv
.audit-venv/bin/pip install --upgrade pip pip-audit
if [ -n "${{ inputs.install-extras }}" ]; then
.audit-venv/bin/pip install -e ".[${{ inputs.install-extras }}]"
elif [ -f requirements.txt ]; then
.audit-venv/bin/pip install -r requirements.txt
else
.audit-venv/bin/pip install -e "."
fi
- name: Security audit (pip-audit)
id: audit
run: |
.audit-venv/bin/pip-audit --format=json --output=audit.json 2>/dev/null || true
VULNS=$(python3 -c "
import json, sys
try:
data = json.load(open('audit.json'))
deps = data.get('dependencies', [])
count = sum(len(d.get('vulns', [])) for d in deps)
print(count)
except Exception:
print(0)
")
echo "vulnerabilities=$VULNS" >> $GITHUB_OUTPUT
- name: Open or update issue if vulnerabilities found
if: steps.audit.outputs.vulnerabilities != '0'
env:
GITEA_TOKEN: ${{ gitea.token }}
SERVER_URL: ${{ gitea.server_url }}
REPOSITORY: ${{ gitea.repository }}
run: |
VULNS="${{ steps.audit.outputs.vulnerabilities }}"
DATE=$(date '+%Y-%m-%d')
TITLE_PREFIX="Security Python:"
VULN_LIST=$(python3 -c "
import json
try:
data = json.load(open('audit.json'))
lines = []
for dep in data.get('dependencies', []):
for v in dep.get('vulns', []):
lines.append(f\"- {dep['name']} {dep.get('version','?')}: {v.get('id','?')} ({v.get('description','')[:80]})\")
print('\n'.join(lines[:20]))
except Exception:
print('N/A')
")
printf '## Security Audit Python — %s\n\n### Vulnerabilità trovate: %s\n\n```\n%s\n```\n' \
"$DATE" "$VULNS" "$VULN_LIST" > /tmp/body.md
# Deduplica: se esiste già una issue aperta con lo stesso prefisso,
# aggiungi un commento invece di aprirne una nuova
EXISTING=$(curl -s \
-H "Authorization: token $GITEA_TOKEN" \
"$SERVER_URL/api/v1/repos/$REPOSITORY/issues?state=open&type=issues&limit=50" \
| jq -r --arg p "$TITLE_PREFIX" '[.[] | select(.title | contains($p))] | (first // {}) | .number // empty')
if [ -n "$EXISTING" ]; then
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $GITEA_TOKEN" \
"$SERVER_URL/api/v1/repos/$REPOSITORY/issues/$EXISTING/comments" \
-d "{ \"body\": $(jq -Rs . /tmp/body.md) }"
else
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: token $GITEA_TOKEN" \
"$SERVER_URL/api/v1/repos/$REPOSITORY/issues" \
-d "{
\"title\": \"[$DATE] Security Python: $VULNS vulnerabilità rilevate\",
\"body\": $(jq -Rs . /tmp/body.md)
}"
fi
- name: Rimozione credenziali registry PyPI
if: always()
run: rm -f "$RUNNER_TEMP/pip-gitea.netrc"