forked from ferchosur/Practica-DevSecOps-HackLab
-
Notifications
You must be signed in to change notification settings - Fork 0
200 lines (185 loc) · 8.17 KB
/
Copy pathmain.yml
File metadata and controls
200 lines (185 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
name: devsecops-pipeline
on:
pull_request:
push:
branches: ["main"]
workflow_dispatch:
permissions:
contents: read
security-events: write
packages: write # útil si luego publicas en GHCR
discussions: write
pull-requests: write
concurrency:
group: devsecops-${{ github.ref }}
cancel-in-progress: true
env:
# 🔁 Cambia esto para probar cada escenario (apps/10-secrets-leak, 20-sast-bugs, etc.)
APP_DIR: apps/10-secrets-leak
# Parámetros de despliegue local
IMAGE_NAME: demo-app
IMAGE_TAG: local
KIND_CLUSTER: devsecops
SERVICE_RELEASE_NAME: demo
jobs:
# ──────────────────────────────────────────────────────────────────────────────
# Secrets + SAST
# ──────────────────────────────────────────────────────────────────────────────
secrets:
name: Secrets scanning (Gitleaks)
runs-on: ubuntu-latest
steps:
- name: Clean gitleaks
run: rm -f /tmp/gitleaks.tmp
- uses: actions/checkout@v4
with:
fetch-depth: 0 # para análisis que miran historial
- name: Gitleaks
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Si usas un gitleaks.toml propio, añade inputs/vars según la acción
sast:
name: SAST (Semgrep)
runs-on: ubuntu-latest
needs: [secrets]
steps:
- uses: actions/checkout@v4
- name: self-hosted (no bloqueante)
run: |
docker run --rm -v "$PWD:/src" returntocorp/semgrep:latest \
semgrep scan --config p/ci --config .semgrep
- name: Export SARIF (para pestaña Security)
run: |
docker run --rm -v "$PWD:/src" returntocorp/semgrep:latest \
semgrep scan --config p/ci --config .semgrep --sarif -o semgrep.sarif || true
- uses: github/codeql-action/upload-sarif@v3
with: { sarif_file: semgrep.sarif }
# ──────────────────────────────────────────────────────────────────────────────
# IaC + Build + SBOM
# ──────────────────────────────────────────────────────────────────────────────
# iac:
# name: IaC scan (Checkov)
# runs-on: ubuntu-latest
# needs: [sast]
# steps:
# - uses: actions/checkout@v4
# - name: Checkov (K8s/Terraform)
# uses: bridgecrewio/checkov-action@master
# with:
# directory: .
# quiet: true
# soft_fail: false # pon true si no quieres bloquear (no recomendado)
build:
name: Build + SBOM
runs-on: ubuntu-latest
needs: [sast]
steps:
- uses: actions/checkout@v4
- name: Build imagen de la app objetivo
run: |
cd "${APP_DIR}"
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
- name: SBOM (Syft)
uses: anchore/sbom-action@v0
with:
image: demo-app:local
artifact-name: sbom.spdx.json # queda como artefacto del job
# ──────────────────────────────────────────────────────────────────────────────
# Trivy + Firma/Verify (cosign)
# ──────────────────────────────────────────────────────────────────────────────
container_scan:
name: Container & deps scan (Trivy)
runs-on: self-hosted
needs: [build]
steps:
# - name: Trivy image (CRITICAL,HIGH)
# uses: aquasecurity/trivy-action@0.28.0
# with:
# scan-type: fs
# image-ref: demo-app:local
# format: sarif
# output: trivy-image.sarif
# ignore-unfixed: true
# severity: CRITICAL,HIGH
# - uses: github/codeql-action/upload-sarif@v3
# with: { sarif_file: trivy-image.sarif }
- name: Trivy fs (SCA sobre el repo)
uses: aquasecurity/trivy-action@0.28.0
with:
scan-type: fs
scan-ref: .
format: sarif
output: trivy-fs.sarif
ignore-unfixed: true
severity: CRITICAL,HIGH
- uses: github/codeql-action/upload-sarif@v3
with: { sarif_file: trivy-fs.sarif }
sign:
name: Supply chain gate (cosign sobre SBOM)
runs-on: self-hosted
needs: [container_scan]
env:
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
steps:
- uses: actions/checkout@v4
- name: Instalar cosign
run: |
COSIGN_URL="https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64"
curl -sSLf "$COSIGN_URL" -o /usr/local/bin/cosign
chmod +x /usr/local/bin/cosign
- name: Generar claves (si no existen)
run: |
test -f cosign.key || cosign generate-key-pair
- name: Descargar SBOM del job anterior
uses: actions/download-artifact@v4
with:
name: sbom.spdx.json
path: .
- name: Firmar SBOM (sign-blob)
run: cosign sign-blob --yes --key cosign.key sbom.spdx.json <<< "$COSIGN_PASSWORD"
- name: Verificar firma del SBOM (gate)
run: cosign verify-blob --key cosign.pub --signature sbom.spdx.json.sig sbom.spdx.json
# ── Alternativa (comentada) si publicas la imagen en GHCR y quieres firmar la imagen:
# - name: Login GHCR
# run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# - name: Push a GHCR
# run: |
# export IMG="ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}"
# docker tag ${IMAGE_NAME}:${IMAGE_TAG} "$IMG"
# docker push "$IMG"
# - name: Sign imagen en GHCR
# run: cosign sign --yes --key cosign.key "ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}" <<< "$COSIGN_PASSWORD"
# - name: Verify imagen (gate)
# run: cosign verify --key cosign.pub "ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.sha }}"
# ──────────────────────────────────────────────────────────────────────────────
# Deploy a K8s local + DAST (ZAP)
# ──────────────────────────────────────────────────────────────────────────────
deploy:
name: Deploy a Kubernetes local (kind/minikube)
runs-on: self-hosted
needs: [container_scan]
steps:
- uses: actions/checkout@v4
- name: Cargar imagen local al clúster kind
run: kind load docker-image demo-app:local --name devsecops
- name: Helm upgrade/install
run: |
helm upgrade --install demo-app charts/demo-app \
--set image.repository=${IMAGE_NAME} \
--set image.tag=${IMAGE_TAG} \
--set service.type=NodePort
- name: Esperar readiness
run: kubectl rollout status deploy/demo-app --timeout=180s
dast:
name: DAST (OWASP ZAP baseline)
runs-on: self-hosted
needs: [deploy]
steps:
- name: Port-forward al Service y ejecutar ZAP
run: |
kubectl port-forward svc/demo 8080:80 & echo $! > pf.pid
sleep 3
docker run --rm -t owasp/zap2docker-stable \
zap-baseline.py -t http://localhost:8080 -x zap.xml
kill $(cat pf.pid) || true