diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index a3a9dce..30b0414 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -4,7 +4,7 @@ on: branches: [ main ] jobs: - security-gate: + security-gate-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout Code @@ -12,19 +12,23 @@ jobs: with: fetch-depth: 0 - # 1. SECRET SCANNING (Ativo: Ignora o commit antigo graças ao .gitleaksignore, mas bloqueia novas fugas) + # ========================================== + # ETAPA 1: TESTES ESTÁTICOS DE SEGURANÇA + # ========================================== + + # 1.1. SECRET SCANNING (Protege contra novas fugas de chaves) - name: Gitleaks Scan run: | curl -sL https://github.com/gitleaks/gitleaks/releases/download/v8.18.2/gitleaks_8.18.2_linux_x64.tar.gz | tar -xz -C /tmp /tmp/gitleaks protect --source . --verbose --redact --staged --exit-code 1 - # 2. SCA - Verifica vulnerabilidades no Nginx + # 1.2. SCA - Verifica vulnerabilidades conhecidas no Nginx - name: Scan Docker Image Vulnerabilities (Trivy) run: | curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin trivy image --severity HIGH,CRITICAL nginx:alpine - # 3. SAST - Análise de Código com SonarQube + # 1.3. SAST - Análise de Código Fonte - name: SonarQube Analysis run: | curl -sL https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip -o sonar-scanner.zip @@ -36,26 +40,76 @@ jobs: -Dsonar.token=${{ secrets.SONAR_TOKEN }} \ -Dsonar.qualitygate.wait=true - deploy: - needs: security-gate - runs-on: ubuntu-latest - steps: - - name: Checkout Code - uses: actions/checkout@v3 + # ========================================== + # ETAPA 2: SANDBOX (AMBIENTE DE TESTE DINÂMICO) + # ========================================== - # 4. DEPLOY ATÓMICO E HARDENING - - name: Hardened Deploy + - name: Criar Sandbox Temporária run: | + # Remover qualquer resíduo de sandbox anterior + docker rm -f website-test-sandbox || true + + # Levantar a Sandbox na porta temporária 8080 + docker run -d --name website-test-sandbox -p 8080:80 nginx:alpine + + # Copiar o index.html atual para a Sandbox para poder ser testado + docker cp index.html website-test-sandbox:/usr/share/nginx/html/index.html + + # Aguardar 5 segundos para o servidor Nginx iniciar + sleep 5 + + # ========================================== + # ETAPA 3: DAST - TESTE DINÂMICO (OWASP ZAP) + # ========================================== + + # Ataca a Sandbox (8080) e não a produção. Se encontrar falhas graves, a pipeline para aqui. + - name: OWASP ZAP Baseline Scan + run: | + mkdir -p qatests + chmod 777 qatests + docker run --user root -v $(pwd)/qatests:/zap/wrk/:rw -t ghcr.io/zaproxy/zaproxy:stable zap-baseline.py \ + -t http://51.89.40.2:8080 \ + -r report.html + + # Garante que a Sandbox é desmantelada mesmo que o passo do ZAP falhe + - name: Destruir Sandbox + if: always() + run: | + docker rm -f website-test-sandbox || true + + # ========================================== + # ETAPA 4: DEPLOY EM PRODUÇÃO (SÓ SE TUDO PASSAR) + # ========================================== + + - name: Hardened Deploy (Produção - Porta 3000) + run: | + # Backup do ficheiro anterior em Produção docker exec website-test-backend tar -czf /tmp/index_backup.tar.gz -C /usr/share/nginx/html index.html || true + + # Limpar a pasta de produção e copiar o novo código aprovado docker exec website-test-backend sh -c "rm -rf /usr/share/nginx/html/*" docker cp index.html website-test-backend:/usr/share/nginx/html/index.html + + # Aplicar Hardening de permissões docker exec website-test-backend chown root:root /usr/share/nginx/html/index.html docker exec website-test-backend chmod 444 /usr/share/nginx/html/index.html - # Testar o acesso local por dentro do próprio container Nginx + + # Testar se o site responde localmente em produção docker exec website-test-backend curl --silent --show-error --fail http://localhost:80 || exit 1 - # 5. AUDITORIA DE DEPLOY + # ========================================== + # ETAPA 5: ARTEFACTOS E NOTIFICAÇÕES + # ========================================== + + # Guarda o relatório interativo gerado pelo OWASP ZAP para poderes descarregar no Gitea + - name: Guardar Relatorio ZAP + if: always() + uses: actions/upload-artifact@v3 + with: + name: owasp-zap-report + path: qatests/report.html + - name: Slack/Discord Notification if: always() run: | - echo "Deploy finalizado com status: ${{ job.status }}" \ No newline at end of file + echo "Pipeline finalizada com status: ${{ job.status }}" \ No newline at end of file