-
Notifications
You must be signed in to change notification settings - Fork 0
320 lines (281 loc) · 11.2 KB
/
security-scan.yml
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
name: Security Scan
# Trigger the workflow on pull requests, pushes to main/develop, and weekly
on:
pull_request:
branches: [main, develop]
push:
branches: [main, develop]
schedule:
- cron: '0 0 * * 0' # Run weekly on Sundays at midnight
jobs:
static_analysis:
name: Static Code Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all history for SonarQube analysis
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Bandit security scanner
run: |
python -m pip install --upgrade pip
pip install bandit
- name: Run Bandit scan
run: |
# Find all Python files and scan them, excluding tests
bandit -r . --exclude ./tests,./venv -f sarif -o bandit-results.sarif
continue-on-error: true
- name: SonarQube Analysis
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.projectKey=brik-refunds-service
-Dsonar.organization=brik
-Dsonar.sources=.
-Dsonar.exclusions=**/tests/**,**/node_modules/**
-Dsonar.python.coverage.reportPaths=coverage.xml
-Dsonar.python.bandit.reportPaths=bandit-results.sarif
-Dsonar.qualitygate.wait=true
- name: Upload SARIF results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: bandit-results.sarif
category: bandit
if: always() # Upload results even if previous steps failed
dependency_check:
name: Dependency Vulnerability Scan
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
# Check if requirements.txt exists in common locations
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
elif [ -f "backend/requirements.txt" ]; then
pip install -r backend/requirements.txt
else
echo "No requirements.txt found"
fi
pip install safety
continue-on-error: true
- name: Install Node.js dependencies
run: |
# Check if package.json exists in different possible locations
if [ -f "package.json" ]; then
npm ci
elif [ -f "frontend/package.json" ]; then
cd frontend && npm ci
else
echo "No package.json found"
fi
continue-on-error: true
- name: Run Safety check on Python dependencies
run: |
safety check --json > safety-results.json || echo "Safety check completed with issues"
continue-on-error: true
- name: Run npm audit on Node.js dependencies
run: |
if [ -f "package.json" ]; then
npm audit --json > npm-audit-results.json || echo "Vulnerabilities found"
elif [ -f "frontend/package.json" ]; then
cd frontend && npm audit --json > npm-audit-results.json || echo "Vulnerabilities found"
else
echo "No package.json found"
fi
continue-on-error: true
- name: Snyk scan for Python
uses: snyk/actions/python@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high --sarif-file-output=snyk-python.sarif
continue-on-error: true
- name: Snyk scan for Node.js
uses: snyk/actions/node@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high --sarif-file-output=snyk-node.sarif
continue-on-error: true
- name: Upload Snyk Python results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: snyk-python.sarif
category: snyk-python
if: always()
- name: Upload Snyk Node.js results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: snyk-node.sarif
category: snyk-node
if: always()
secret_detection:
name: Secret Detection
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Full history for secret detection
- name: GitLeaks scan
uses: zricethezav/gitleaks-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}
continue-on-error: true
- name: Check for high-severity secrets
run: |
if [ -f "gitleaks-report.sarif" ]; then
# Count high severity findings
HIGH_COUNT=$(grep -c "high" gitleaks-report.sarif || echo "0")
if [ "$HIGH_COUNT" -gt "0" ]; then
echo "::error ::$HIGH_COUNT high-severity secrets detected in the codebase"
exit 1
fi
fi
continue-on-error: true
- name: Upload Gitleaks report
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: gitleaks-report.sarif
category: secrets
if: always()
container_scan:
name: Container Vulnerability Scan
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build backend Docker image
run: |
# Check if Dockerfile exists in different possible locations
if [ -f "Dockerfile" ]; then
docker build -t brik/refunds-service:${{ github.sha }} .
elif [ -f "backend/Dockerfile" ]; then
docker build -t brik/refunds-backend:${{ github.sha }} ./backend
else
echo "No backend Dockerfile found, skipping"
exit 0
fi
continue-on-error: true
- name: Build frontend Docker image
run: |
if [ -f "frontend/Dockerfile" ]; then
docker build -t brik/refunds-frontend:${{ github.sha }} ./frontend
else
echo "No frontend Dockerfile found, skipping"
exit 0
fi
continue-on-error: true
- name: Run Trivy vulnerability scanner on backend
uses: aquasecurity/trivy-action@master
with:
image-ref: 'brik/refunds-backend:${{ github.sha }}'
format: 'sarif'
output: 'trivy-backend-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '0' # Don't fail on vulnerabilities for now
continue-on-error: true
- name: Run Trivy vulnerability scanner on frontend
uses: aquasecurity/trivy-action@master
with:
image-ref: 'brik/refunds-frontend:${{ github.sha }}'
format: 'sarif'
output: 'trivy-frontend-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '0' # Don't fail on vulnerabilities for now
continue-on-error: true
- name: Upload Trivy backend scan results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: trivy-backend-results.sarif
category: container-backend
if: always()
- name: Upload Trivy frontend scan results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: trivy-frontend-results.sarif
category: container-frontend
if: always()
- name: Check for critical vulnerabilities
run: |
# Count critical vulnerabilities
CRITICAL_COUNT_BACKEND=0
CRITICAL_COUNT_FRONTEND=0
if [ -f "trivy-backend-results.sarif" ]; then
CRITICAL_COUNT_BACKEND=$(grep -c '"level":"error"' trivy-backend-results.sarif || echo "0")
fi
if [ -f "trivy-frontend-results.sarif" ]; then
CRITICAL_COUNT_FRONTEND=$(grep -c '"level":"error"' trivy-frontend-results.sarif || echo "0")
fi
TOTAL=$((CRITICAL_COUNT_BACKEND + CRITICAL_COUNT_FRONTEND))
if [ "$TOTAL" -gt "0" ]; then
echo "::error ::$TOTAL critical vulnerabilities detected in container images"
echo "Backend: $CRITICAL_COUNT_BACKEND, Frontend: $CRITICAL_COUNT_FRONTEND"
exit 1
fi
continue-on-error: true
security_report:
name: Generate Security Report
needs: [static_analysis, dependency_check, secret_detection, container_scan]
runs-on: ubuntu-latest
if: always()
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Determine overall security status
id: security_status
run: |
# Check the status of previous jobs
if [[ "${{ needs.static_analysis.result }}" == "failure" || \
"${{ needs.dependency_check.result }}" == "failure" || \
"${{ needs.secret_detection.result }}" == "failure" || \
"${{ needs.container_scan.result }}" == "failure" ]]; then
echo "status=failure" >> $GITHUB_OUTPUT
echo "::error ::Security scan detected issues that need to be addressed."
else
echo "status=success" >> $GITHUB_OUTPUT
echo "::notice ::Security scan completed successfully."
fi
- name: Create security report summary
run: |
mkdir -p security-reports
echo "# Security Scan Report" > security-reports/summary.md
echo "## Scan Results" >> security-reports/summary.md
echo "- Static Analysis: ${{ needs.static_analysis.result }}" >> security-reports/summary.md
echo "- Dependency Check: ${{ needs.dependency_check.result }}" >> security-reports/summary.md
echo "- Secret Detection: ${{ needs.secret_detection.result }}" >> security-reports/summary.md
echo "- Container Scan: ${{ needs.container_scan.result }}" >> security-reports/summary.md
echo "## Overall Status: ${{ steps.security_status.outputs.status }}" >> security-reports/summary.md
echo "" >> security-reports/summary.md
echo "For detailed information, please check the security scan logs and SARIF results in the GitHub Security tab." >> security-reports/summary.md
- name: Upload security report artifact
uses: actions/upload-artifact@v3
with:
name: security-reports
path: security-reports/
retention-days: 14
- name: Fail workflow if security issues found
if: steps.security_status.outputs.status == 'failure'
run: |
echo "Security scan detected issues. Please address them before merging."
exit 1