-
Notifications
You must be signed in to change notification settings - Fork 0
565 lines (485 loc) · 20.7 KB
/
security.yml
File metadata and controls
565 lines (485 loc) · 20.7 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
name: Security Scan
on:
push:
branches: ["main"]
pull_request:
permissions:
contents: read
issues: write
pull-requests: write
jobs:
security:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Prepare artifacts directory
run: mkdir -p artifacts/security
- name: Extract exclude paths from config
id: exclude-paths
run: |
python3 << 'EOF'
import yaml
import json
import os
exclude_paths = []
try:
if os.path.exists('security-config.yml'):
with open('security-config.yml') as f:
config = yaml.safe_load(f)
if config and 'exclude_paths' in config:
exclude_paths = config['exclude_paths'] or []
print(f"Loaded exclude_paths: {exclude_paths}")
else:
print("No security-config.yml found, no exclusions")
except Exception as e:
print(f"Error reading config: {e}")
# Build exclusion flags for different tools
semgrep_excludes = ' '.join([f'--exclude {p}' for p in exclude_paths])
gitleaks_excludes = ','.join(exclude_paths) if exclude_paths else ''
# Write to GitHub Actions output
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"semgrep_excludes={semgrep_excludes}\n")
f.write(f"gitleaks_excludes={gitleaks_excludes}\n")
f.write(f"exclude_paths={json.dumps(exclude_paths)}\n")
EOF
pip install pyyaml
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
- name: Install dependencies
run: go mod download
- name: Set up Python for Semgrep
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Run Semgrep (CLI JSON)
run: |
pip install semgrep
semgrep --config p/ci ${{ steps.exclude-paths.outputs.semgrep_excludes }} --json > artifacts/security/semgrep-report.json || true
- name: Create Gitleaks config with exclusions
if: steps.exclude-paths.outputs.gitleaks_excludes != ''
run: |
cat > .gitleaks.toml << 'EOF'
[allowlist]
paths = [
'''${{ steps.exclude-paths.outputs.gitleaks_excludes }}'''
]
EOF
sed -i "s/'''/\"/g" .gitleaks.toml
sed -i 's/,/",\n "/g' .gitleaks.toml
echo "Generated .gitleaks.toml:"
cat .gitleaks.toml
- name: Install Gitleaks
run: |
curl -sSfL https://github.com/gitleaks/gitleaks/releases/download/v8.18.1/gitleaks_8.18.1_linux_x64.tar.gz | tar -xz
sudo mv gitleaks /usr/local/bin/
- name: Run Gitleaks
run: |
if [ -f .gitleaks.toml ]; then
gitleaks detect --report-format json --report-path artifacts/security/gitleaks-report.json --config .gitleaks.toml --exit-code 0 || true
else
gitleaks detect --report-format json --report-path artifacts/security/gitleaks-report.json --exit-code 0 || true
fi
- name: Run Trivy FS Scan
uses: aquasecurity/trivy-action@master
with:
scan-type: fs
format: json
output: artifacts/security/trivy-fs.json
severity: HIGH,CRITICAL
skip-dirs: ${{ steps.exclude-paths.outputs.gitleaks_excludes }}
- name: Check for Dockerfile
id: docker-check
run: |
if [ -f "Dockerfile" ]; then
echo "has_docker=true" >> $GITHUB_OUTPUT
echo "✓ Dockerfile detected, will scan Docker image"
else
echo "has_docker=false" >> $GITHUB_OUTPUT
echo "No Dockerfile found, skipping image scan"
fi
- name: Build Docker image for scanning
if: steps.docker-check.outputs.has_docker == 'true'
run: |
# Build image with a temporary tag for scanning
docker build -t devsecops-scan-temp:latest .
echo "Built Docker image: devsecops-scan-temp:latest"
- name: Run Trivy Image Scan
if: steps.docker-check.outputs.has_docker == 'true'
uses: aquasecurity/trivy-action@master
with:
scan-type: image
image-ref: devsecops-scan-temp:latest
format: json
output: artifacts/security/trivy-image.json
severity: HIGH,CRITICAL
- name: Extract fail_on thresholds from config
if: always()
run: |
pip install pyyaml
python3 << 'EOF'
import yaml
import json
import os
# Default thresholds
fail_on = {
'gitleaks': 0,
'semgrep': 10,
'trivy_critical': 0,
'trivy_high': 5,
'trivy_medium': -1,
'trivy_low': -1
}
# Try to read from security-config.yml
try:
if os.path.exists('security-config.yml'):
with open('security-config.yml') as f:
config = yaml.safe_load(f)
if config and 'fail_on' in config:
fail_on.update(config['fail_on'])
print(f"Loaded fail_on config: {fail_on}")
else:
print("No security-config.yml found, using defaults")
except Exception as e:
print(f"Error reading config, using defaults: {e}")
# Write to JSON for GitHub script to read
os.makedirs('artifacts/security', exist_ok=True)
with open('artifacts/security/fail-on.json', 'w') as out:
json.dump(fail_on, out)
EOF
- name: Generate security summary (JSON)
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
const dir = 'artifacts/security';
const summaryPath = path.join(dir, 'summary.json');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
function readJson(file) {
if (!fs.existsSync(file)) return null;
const raw = fs.readFileSync(file, 'utf8');
try { return JSON.parse(raw); }
catch { return null; }
}
// Read fail_on thresholds from extracted config
const failOnPath = path.join(dir, 'fail-on.json');
const failOn = readJson(failOnPath) || {
gitleaks: 0,
semgrep: 10,
trivy_critical: 0,
trivy_high: -1,
trivy_medium: -1,
trivy_low: -1
};
core.info(`Using fail_on thresholds: ${JSON.stringify(failOn)}`);
const result = {
version: "0.3.0",
status: "PASS",
blocking_count: 0,
summary: {},
findings: []
};
//
// GITLEAKS
//
const gitleaksPath = path.join(dir, 'gitleaks-report.json');
const gitleaks = readJson(gitleaksPath);
if (gitleaks) {
let count = 0;
if (Array.isArray(gitleaks)) count = gitleaks.length;
else if (Array.isArray(gitleaks.findings)) count = gitleaks.findings.length;
result.summary.gitleaks = { total: count };
// Check fail gate
if (failOn.gitleaks >= 0 && count > failOn.gitleaks) {
result.blocking_count += count - failOn.gitleaks;
core.warning(`Gitleaks: ${count} secrets found (threshold: ${failOn.gitleaks})`);
}
}
//
// TRIVY FS
//
const trivyFsPath = path.join(dir, 'trivy-fs.json');
const trivy = readJson(trivyFsPath);
if (trivy?.Results) {
const counts = { critical: 0, high: 0, medium: 0, low: 0 };
for (const r of trivy.Results) {
for (const v of (r.Vulnerabilities || [])) {
const sev = (v.Severity || "").toLowerCase();
if (counts[sev] !== undefined) counts[sev]++;
}
}
result.summary.trivy_fs = counts;
// Check fail gates for each severity
const severities = {
critical: 'trivy_critical',
high: 'trivy_high',
medium: 'trivy_medium',
low: 'trivy_low'
};
for (const [sev, configKey] of Object.entries(severities)) {
const threshold = failOn[configKey];
const count = counts[sev] || 0;
if (threshold >= 0 && count > threshold) {
result.blocking_count += count - threshold;
core.warning(`Trivy ${sev}: ${count} vulnerabilities (threshold: ${threshold})`);
}
}
}
//
// TRIVY IMAGE
//
const trivyImagePath = path.join(dir, 'trivy-image.json');
const trivyImage = readJson(trivyImagePath);
if (trivyImage?.Results) {
const counts = { critical: 0, high: 0, medium: 0, low: 0 };
for (const r of trivyImage.Results) {
for (const v of (r.Vulnerabilities || [])) {
const sev = (v.Severity || "").toLowerCase();
if (counts[sev] !== undefined) counts[sev]++;
}
}
result.summary.trivy_image = counts;
core.info(`Trivy Image scan: ${JSON.stringify(counts)}`);
// Check fail gates for image scan (use same thresholds as FS)
const severities = {
critical: 'trivy_critical',
high: 'trivy_high',
medium: 'trivy_medium',
low: 'trivy_low'
};
for (const [sev, configKey] of Object.entries(severities)) {
const threshold = failOn[configKey];
const count = counts[sev] || 0;
if (threshold >= 0 && count > threshold) {
result.blocking_count += count - threshold;
core.warning(`Trivy Image ${sev}: ${count} vulnerabilities (threshold: ${threshold})`);
}
}
}
//
// SEMGREP
//
const semgrepPath = path.join(dir, 'semgrep-report.json');
const semgrep = readJson(semgrepPath);
if (semgrep) {
let results = [];
if (Array.isArray(semgrep)) {
results = semgrep;
} else if (Array.isArray(semgrep.results)) {
results = semgrep.results;
}
const count = results.length;
result.summary.semgrep = { total: count };
core.info(`Semgrep findings: ${count}`);
// Check fail gate
if (failOn.semgrep >= 0 && count > failOn.semgrep) {
result.blocking_count += count - failOn.semgrep;
core.warning(`Semgrep: ${count} findings (threshold: ${failOn.semgrep})`);
}
} else {
core.info("No semgrep-report.json found, skipping Semgrep summary.");
}
// Set final status
result.status = result.blocking_count > 0 ? "FAIL" : "PASS";
if (result.blocking_count > 0) {
core.warning(`Security scan FAILED: ${result.blocking_count} issue(s) exceed thresholds`);
} else {
core.info("Security scan PASSED: All checks within thresholds");
}
fs.writeFileSync(summaryPath, JSON.stringify(result, null, 2));
core.info("Wrote summary.json");
- name: Post PR Security Summary
if: always() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const marker = '<!-- devsecops-kit-security-summary -->';
const summaryPath = 'artifacts/security/summary.json';
let summary = null;
try { summary = JSON.parse(fs.readFileSync(summaryPath, 'utf8')); }
catch { core.warning("No summary.json available."); }
let body = `${marker}\n### 🔐 DevSecOps Kit Security Summary\n\n`;
if (!summary) {
body += "_No summary available._\n";
} else {
const leaks = summary.summary?.gitleaks?.total ?? 0;
const trivyFs = summary.summary?.trivy_fs ?? {};
const trivyImage = summary.summary?.trivy_image ?? {};
const semgrep = summary.summary?.semgrep ?? null;
body += `- **Gitleaks:** ${leaks} leak(s)\n`;
if (Object.keys(trivyFs).length > 0) {
body += `- **Trivy FS:**\n`;
for (const sev of Object.keys(trivyFs)) {
body += ` - ${sev.toUpperCase()}: ${trivyFs[sev]}\n`;
}
}
if (Object.keys(trivyImage).length > 0) {
body += `- **Trivy Image:**\n`;
for (const sev of Object.keys(trivyImage)) {
body += ` - ${sev.toUpperCase()}: ${trivyImage[sev]}\n`;
}
}
if (semgrep) {
body += `- **Semgrep:** ${semgrep.total} finding(s)\n`;
}
// Use status from summary.json
const status = summary.status || "UNKNOWN";
const blockingCount = summary.blocking_count || 0;
body += `\n**Status:** ${status === "FAIL" ? '🚨 **FAIL**' : '✅ **PASS**'}\n`;
if (blockingCount > 0) {
body += `_${blockingCount} issue(s) exceed configured thresholds_\n`;
}
}
const { owner, repo } = context.repo;
const pr = context.issue.number;
const comments = await github.rest.issues.listComments({ owner, repo, issue_number: pr });
const existing = comments.data.find(c => c.body?.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner, repo,
comment_id: existing.id,
body
});
} else {
await github.rest.issues.createComment({
owner, repo,
issue_number: pr,
body
});
}
- name: Post detailed fix-it comments
if: always() && github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const dir = 'artifacts/security';
const { owner, repo } = context.repo;
const pr = context.issue.number;
function readJson(file) {
if (!fs.existsSync(file)) return null;
try { return JSON.parse(fs.readFileSync(file, 'utf8')); }
catch { return null; }
}
// Get PR files to validate comments are on changed files
const { data: prFiles } = await github.rest.pulls.listFiles({ owner, repo, pull_number: pr });
const changedFiles = new Set(prFiles.map(f => f.filename));
// Get the commit SHA for review comments
const { data: prData } = await github.rest.pulls.get({ owner, repo, pull_number: pr });
const commitId = prData.head.sha;
const comments = [];
//
// SEMGREP FINDINGS
//
const semgrepPath = path.join(dir, 'semgrep-report.json');
const semgrep = readJson(semgrepPath);
if (semgrep) {
let results = [];
if (Array.isArray(semgrep)) results = semgrep;
else if (Array.isArray(semgrep.results)) results = semgrep.results;
for (const finding of results.slice(0, 10)) { // Limit to 10 comments
const filePath = finding.path;
const line = finding.start?.line || finding.line || 1;
const message = finding.extra?.message || finding.check_id || 'Security issue detected';
const severity = finding.extra?.severity || 'WARNING';
const fixRegex = finding.extra?.fix_regex;
if (!changedFiles.has(filePath)) continue; // Only comment on changed files
let body = `**🔍 Semgrep [${severity}]**\n\n`;
body += `${message}\n\n`;
body += `**Rule:** \`${finding.check_id}\`\n`;
if (fixRegex) {
body += `\n**Suggested fix:** Apply the regex replacement suggested by Semgrep.\n`;
}
if (finding.extra?.metadata?.references) {
body += `\n**References:**\n`;
for (const ref of finding.extra.metadata.references.slice(0, 3)) {
body += `- ${ref}\n`;
}
}
comments.push({ path: filePath, line, body });
}
}
//
// GITLEAKS FINDINGS
//
const gitleaksPath = path.join(dir, 'gitleaks-report.json');
const gitleaks = readJson(gitleaksPath);
if (gitleaks) {
let leaks = [];
if (Array.isArray(gitleaks)) leaks = gitleaks;
else if (Array.isArray(gitleaks.findings)) leaks = gitleaks.findings;
for (const leak of leaks.slice(0, 5)) { // Limit to 5 secrets
const filePath = leak.File || leak.file;
const line = leak.StartLine || leak.line || 1;
const secret = leak.Secret || leak.match || '';
const rule = leak.RuleID || leak.rule || 'Secret detected';
if (!changedFiles.has(filePath)) continue;
let body = `**🚨 Secret Detected**\n\n`;
body += `**Rule:** \`${rule}\`\n`;
body += `**Match:** \`${secret.substring(0, 20)}...\`\n\n`;
body += `⚠️ **Action Required:** Remove this secret immediately and:\n`;
body += `1. Rotate the compromised credential\n`;
body += `2. Use environment variables or secret management\n`;
body += `3. Never commit secrets to version control\n`;
comments.push({ path: filePath, line, body });
}
}
// Post review comments (batch API)
if (comments.length > 0) {
try {
await github.rest.pulls.createReview({
owner,
repo,
pull_number: pr,
commit_id: commitId,
event: 'COMMENT',
comments: comments.map(c => ({
path: c.path,
line: c.line,
body: c.body
}))
});
core.info(`Posted ${comments.length} fix-it comment(s)`);
} catch (error) {
core.warning(`Failed to post review comments: ${error.message}`);
}
} else {
core.info("No findings in changed files, skipping fix-it comments");
}
- name: Upload security artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: security-reports
path: artifacts/security/
- name: Check fail gates
if: always()
run: |
if [ ! -f artifacts/security/summary.json ]; then
echo "No summary.json found, skipping fail gate check"
exit 0
fi
STATUS=$(cat artifacts/security/summary.json | python3 -c "import sys, json; print(json.load(sys.stdin).get('status', 'UNKNOWN'))")
BLOCKING_COUNT=$(cat artifacts/security/summary.json | python3 -c "import sys, json; print(json.load(sys.stdin).get('blocking_count', 0))")
echo "Security scan status: $STATUS"
echo "Blocking issues: $BLOCKING_COUNT"
if [ "$STATUS" = "FAIL" ]; then
echo "❌ Security scan FAILED: $BLOCKING_COUNT issue(s) exceed configured thresholds"
echo "Review the security summary above and artifacts for details"
exit 1
else
echo "✅ Security scan PASSED: All findings within acceptable thresholds"
exit 0
fi