diff --git a/.forgejo/workflows/security.yml b/.forgejo/workflows/security.yml index 0d68f2e..90c5088 100644 --- a/.forgejo/workflows/security.yml +++ b/.forgejo/workflows/security.yml @@ -1,113 +1,80 @@ -name: Validacion +name: Seguridad de main on: - pull_request: - branches: [develop] push: - branches: [develop] - workflow_dispatch: + branches: [ main ] + pull_request: + branches: [ main ] jobs: - gitflow: - name: gitflow + security-audit: + name: Escaneo de Secretos (Gitleaks) y Gobernanza runs-on: ubuntu-latest + steps: - - uses: https://github.com/actions/checkout@v4 + - name: Checkout Code + uses: actions/checkout@v4 with: fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha || github.sha }} - persist-credentials: false - github-server-url: https://git.bryan-bano.com - - uses: https://github.com/astral-sh/setup-uv@v6 - with: - github-token: '' - enable-cache: false - - name: Validar rama, issue, milestone, etiquetas y commits - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GITHUB_SERVER_URL: https://git.bryan-bano.com - run: uv run --no-project --python 3.12 python scripts/check_gitflow.py - ansible: - name: ansible - runs-on: ubuntu-latest - steps: - - uses: https://github.com/actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha || github.sha }} - persist-credentials: false - github-server-url: https://git.bryan-bano.com - - uses: https://github.com/astral-sh/setup-uv@v6 - with: - github-token: '' - enable-cache: false - - name: Instalar dependencias y coleccion AWS + - name: Instalar y Ejecutar Gitleaks run: | - uv venv --python 3.12 .venv - uv pip install --python .venv/bin/python -r ansible/requirements.txt - echo "$PWD/.venv/bin" >> "$GITHUB_PATH" - export PATH="$PWD/.venv/bin:$PATH" - ansible-galaxy collection install -r ansible/requirements.yml - - name: Validar sintaxis y buenas practicas - working-directory: ansible - run: | - ansible-playbook -i inventory/hosts.example.yml playbook.yml --syntax-check - ansible-lint playbook.yml - - name: Probar autenticacion, eventos, inventario y reglas de integracion - run: python -m unittest discover -s tests -v - - terraform: - name: terraform - runs-on: ubuntu-latest - steps: - - uses: https://github.com/actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha || github.sha }} - persist-credentials: false - github-server-url: https://git.bryan-bano.com - - uses: https://github.com/hashicorp/setup-terraform@v3 - with: - terraform_version: '1.15.9' - terraform_wrapper: false - - name: Validar infraestructura sin credenciales AWS - working-directory: terraform-tesis-ir-secure-portal - run: | - terraform fmt -check -diff - terraform init -backend=false -input=false - terraform validate -no-color - - secretos: - name: secretos - runs-on: ubuntu-latest - steps: - - uses: https://github.com/actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha || github.sha }} - persist-credentials: false - github-server-url: https://git.bryan-bano.com - - name: Descargar Gitleaks y verificar checksum - run: | - set -eu - mkdir -p /tmp/tesis-gitleaks - cd /tmp/tesis-gitleaks - base=https://github.com/gitleaks/gitleaks/releases/download/v8.24.3 - case "$(uname -m)" in - x86_64) archive=gitleaks_8.24.3_linux_x64.tar.gz ;; - aarch64|arm64) archive=gitleaks_8.24.3_linux_arm64.tar.gz ;; - *) echo "Arquitectura no soportada" >&2; exit 1 ;; + ARCH=$(uname -m) + case "$ARCH" in + x86_64) GITLEAKS_ARCH="x64" ;; + aarch64|arm64) GITLEAKS_ARCH="arm64" ;; + *) GITLEAKS_ARCH="x64" ;; esac - curl -fsSLO "$base/$archive" - curl -fsSLO "$base/gitleaks_8.24.3_checksums.txt" - sha256sum --check --ignore-missing gitleaks_8.24.3_checksums.txt - tar -xzf "$archive" gitleaks - - name: Buscar secretos en los commits del cambio - env: - BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before }} - run: | - set -eu - if [ -n "$BASE_SHA" ] && git cat-file -e "$BASE_SHA^{commit}" 2>/dev/null; then - /tmp/tesis-gitleaks/gitleaks git --redact --no-banner --log-opts="$BASE_SHA..HEAD" . - else - /tmp/tesis-gitleaks/gitleaks git --redact --no-banner --log-opts=-1 . + + mkdir -p /tmp/bin + if ! command -v gitleaks &> /dev/null; then + echo "Instalando Gitleaks v8.18.4 para arquitectura $ARCH ($GITLEAKS_ARCH)..." + curl -sSL "https://github.com/gitleaks/gitleaks/releases/download/v8.18.4/gitleaks_8.18.4_linux_${GITLEAKS_ARCH}.tar.gz" | tar -xz -C /tmp/bin gitleaks + export PATH="/tmp/bin:$PATH" fi + + echo "Ejecutando escaneo profundo de secretos en código e historial..." + gitleaks detect --source . --redact --config .gitleaks.toml + + - name: Validar Metadata de Pull Request + if: github.event_name == 'pull_request' + env: + PR_HEAD_REF: ${{ github.head_ref }} + PR_BASE_REF: ${{ github.base_ref }} + PR_TITLE: ${{ github.event.pull_request.title }} + PR_BODY: ${{ github.event.pull_request.body }} + run: | + echo "==> Validando Pull Request: '$PR_TITLE'" + echo " Origen: $PR_HEAD_REF -> Destino: $PR_BASE_REF" + + # 1. Reglas de ramas (GitFlow) + if [[ "$PR_BASE_REF" == "develop" ]]; then + if ! [[ "$PR_HEAD_REF" =~ ^(feature|feat|chore|fix|bugfix|refactor|docs|test|perf)/ ]]; then + echo "❌ Error de Rama: PR hacia 'develop' debe originarse en feature/*, chore/*, fix/*, etc." + echo " Rama recibida: '$PR_HEAD_REF'" + exit 1 + fi + elif [[ "$PR_BASE_REF" == "main" || "$PR_BASE_REF" == "master" ]]; then + if ! [[ "$PR_HEAD_REF" =~ ^(release|hotfix)/ || "$PR_HEAD_REF" == "develop" ]]; then + echo "❌ Error de Rama: PR hacia producción ('main') debe provenir de 'develop', 'release/*' o 'hotfix/*'." + echo " Rama recibida: '$PR_HEAD_REF'" + exit 1 + fi + fi + + # 2. Convención de Títulos (Conventional Commits) + TITLE_REGEX="^(feat|fix|chore|docs|style|refactor|perf|test|build|ci|revert)(\([a-zA-Z0-9_\-\./]+\))?!?: .+" + if ! [[ "$PR_TITLE" =~ $TITLE_REGEX ]]; then + echo "❌ Error de Título: Debe seguir Conventional Commits (ej. 'feat(auth): login', 'chore: update deps')." + echo " Título recibido: '$PR_TITLE'" + exit 1 + fi + + # 3. Validación de Descripción + CLEAN_BODY=$(echo "$PR_BODY" | tr -d '[:space:]') + if [[ ${#CLEAN_BODY} -lt 15 ]]; then + echo "❌ Error de Descripción: La descripción del PR no puede estar vacía." + exit 1 + fi + + echo "✅ Validaciones de gobernanza y seguridad aprobadas." diff --git a/.forgejo/workflows/validate.yml b/.forgejo/workflows/validate.yml new file mode 100644 index 0000000..0d68f2e --- /dev/null +++ b/.forgejo/workflows/validate.yml @@ -0,0 +1,113 @@ +name: Validacion + +on: + pull_request: + branches: [develop] + push: + branches: [develop] + workflow_dispatch: + +jobs: + gitflow: + name: gitflow + runs-on: ubuntu-latest + steps: + - uses: https://github.com/actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + github-server-url: https://git.bryan-bano.com + - uses: https://github.com/astral-sh/setup-uv@v6 + with: + github-token: '' + enable-cache: false + - name: Validar rama, issue, milestone, etiquetas y commits + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_SERVER_URL: https://git.bryan-bano.com + run: uv run --no-project --python 3.12 python scripts/check_gitflow.py + + ansible: + name: ansible + runs-on: ubuntu-latest + steps: + - uses: https://github.com/actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + github-server-url: https://git.bryan-bano.com + - uses: https://github.com/astral-sh/setup-uv@v6 + with: + github-token: '' + enable-cache: false + - name: Instalar dependencias y coleccion AWS + run: | + uv venv --python 3.12 .venv + uv pip install --python .venv/bin/python -r ansible/requirements.txt + echo "$PWD/.venv/bin" >> "$GITHUB_PATH" + export PATH="$PWD/.venv/bin:$PATH" + ansible-galaxy collection install -r ansible/requirements.yml + - name: Validar sintaxis y buenas practicas + working-directory: ansible + run: | + ansible-playbook -i inventory/hosts.example.yml playbook.yml --syntax-check + ansible-lint playbook.yml + - name: Probar autenticacion, eventos, inventario y reglas de integracion + run: python -m unittest discover -s tests -v + + terraform: + name: terraform + runs-on: ubuntu-latest + steps: + - uses: https://github.com/actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + github-server-url: https://git.bryan-bano.com + - uses: https://github.com/hashicorp/setup-terraform@v3 + with: + terraform_version: '1.15.9' + terraform_wrapper: false + - name: Validar infraestructura sin credenciales AWS + working-directory: terraform-tesis-ir-secure-portal + run: | + terraform fmt -check -diff + terraform init -backend=false -input=false + terraform validate -no-color + + secretos: + name: secretos + runs-on: ubuntu-latest + steps: + - uses: https://github.com/actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + github-server-url: https://git.bryan-bano.com + - name: Descargar Gitleaks y verificar checksum + run: | + set -eu + mkdir -p /tmp/tesis-gitleaks + cd /tmp/tesis-gitleaks + base=https://github.com/gitleaks/gitleaks/releases/download/v8.24.3 + case "$(uname -m)" in + x86_64) archive=gitleaks_8.24.3_linux_x64.tar.gz ;; + aarch64|arm64) archive=gitleaks_8.24.3_linux_arm64.tar.gz ;; + *) echo "Arquitectura no soportada" >&2; exit 1 ;; + esac + curl -fsSLO "$base/$archive" + curl -fsSLO "$base/gitleaks_8.24.3_checksums.txt" + sha256sum --check --ignore-missing gitleaks_8.24.3_checksums.txt + tar -xzf "$archive" gitleaks + - name: Buscar secretos en los commits del cambio + env: + BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before }} + run: | + set -eu + if [ -n "$BASE_SHA" ] && git cat-file -e "$BASE_SHA^{commit}" 2>/dev/null; then + /tmp/tesis-gitleaks/gitleaks git --redact --no-banner --log-opts="$BASE_SHA..HEAD" . + else + /tmp/tesis-gitleaks/gitleaks git --redact --no-banner --log-opts=-1 . + fi diff --git a/README.md b/README.md index c33497c..196eb10 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ El sistema implementa 5 playbooks de detección y respuesta automatizada estanda ```text localstack-tesis/ ├── ansible/ ← Portal, Nginx y CloudWatch Agent por SSM/SSH -├── .forgejo/workflows/security.yml ← Validacion de PR hacia develop +├── .forgejo/workflows/validate.yml ← Validacion de PR hacia develop ├── docs/ ← Documentación técnica integral de tesis │ ├── README.md ← Índice general y mapa de navegación │ ├── 01-contexto-tesis-y-roles.md ← Marco académico, roles A/B/C y contratos diff --git a/docs/11-forgejo-actions-y-proteccion-develop.md b/docs/11-forgejo-actions-y-proteccion-develop.md index 938b805..d47e532 100644 --- a/docs/11-forgejo-actions-y-proteccion-develop.md +++ b/docs/11-forgejo-actions-y-proteccion-develop.md @@ -2,7 +2,7 @@ Ansible se entrega desde `feature/1-ansible-portal-cloudwatch`, asociada al Issue #1 en Forgejo, hacia `develop`. No se hace push directo a `develop` o `main`. -El workflow [security.yml](../.forgejo/workflows/security.yml) usa un runner `ubuntu-latest`, Linux x86_64 o ARM64 con Node compatible con Actions y acceso HTTPS a GitHub, PyPI, Galaxy y los registros Terraform: +El workflow [validate.yml](../.forgejo/workflows/validate.yml) usa un runner `ubuntu-latest`, Linux x86_64 o ARM64 con Node compatible con Actions y acceso HTTPS a GitHub, PyPI, Galaxy y los registros Terraform: | Job | Comprobacion | |---|---| @@ -40,3 +40,5 @@ En Forgejo 1.21, editar etiquetas o milestone puede no iniciar otra ejecucion. C El runner anuncia internamente `http://forgejo:3000`, que no resuelve desde los contenedores de jobs. El checkout y las consultas de metadatos usan explicitamente `https://git.bryan-bano.com`; el runner debe poder alcanzar esa URL. No se desactiva la verificacion TLS. El runner disponible es ARM64 con imagen `node:22-bookworm`. Se usa `setup-uv` y un entorno Python 3.12 aislado para no depender de las distribuciones Ubuntu admitidas por `setup-python`. Gitleaks selecciona el archivo x64/ARM64 segun la arquitectura real. + +El workflow `security.yml` anterior se conserva para pushes/PR de `main`, con salida redactada. `validate.yml` atiende el circuito de `develop`, evitando duplicar el escaneo en el mismo PR.