-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup-piepline
599 lines (521 loc) · 18.5 KB
/
backup-piepline
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
name: Security & Build Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }}
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
MONGO_URL: ${{ secrets.MONGO_URL }}
SEMGREP_TOKEN: ${{ secrets.SEMGREP_TOKEN }}
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
GH_PAT: ${{ secrets.GH_PAT }}
CLUSTER_NAME: cluster-1
CLUSTER_ZONE: asia-south1-a
APP_DOMAIN: "http://myapp.awsaparna123.xyz"
FRONTEND_IMAGE: ${{ secrets.DOCKER_USERNAME }}/imdb-clone-frontend
BACKEND_IMAGE: ${{ secrets.DOCKER_USERNAME }}/imdb-clone-backend
permissions:
contents: read
security-events: write
actions: write
jobs:
# Frontend Security Scans
frontend-trufflehog:
name: 🐷 Frontend TruffleHog
runs-on: ubuntu-latest
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
fetch-depth: 0
- name: TruffleHog Scan
uses: trufflesecurity/[email protected]
continue-on-error: true
with:
path: .
base: ${{ github.event.before }}
head: ${{ github.sha }}
extra_args: --json --output trufflehog-frontend-report.json
- name: Upload TruffleHog Results
uses: actions/upload-artifact@v4
with:
name: frontend-trufflehog-report
path: trufflehog-frontend-report.json
retention-days: 30
frontend-semgrep:
name: 🔍 Frontend Semgrep
runs-on: ubuntu-latest
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
fetch-depth: 0
- name: Semgrep Scan
uses: semgrep/semgrep-action@v1
continue-on-error: true
with:
config: p/javascript
output: semgrep-frontend-results.json
json: true
- name: Upload Semgrep Results
uses: actions/upload-artifact@v4
with:
name: frontend-semgrep-report
path: semgrep-frontend-results.json
retention-days: 30
frontend-snyk:
name: 🛡️ Frontend Snyk
runs-on: ubuntu-latest
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
- name: Snyk Scan
uses: snyk/actions/node@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
command: test --json > snyk-frontend-report.json
- name: Upload Snyk Results
uses: actions/upload-artifact@v4
with:
name: frontend-snyk-report
path: snyk-frontend-report.json
retention-days: 30
frontend-checkov:
name: 🏗️ Frontend Checkov
runs-on: ubuntu-latest
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
- name: Checkov Scan
uses: bridgecrewio/checkov-action@v12
continue-on-error: true
with:
directory: .
framework: dockerfile,kubernetes,secrets
output: json
output-file-path: checkov-frontend-report.json
- name: Upload Checkov Results
uses: actions/upload-artifact@v4
with:
name: frontend-checkov-report
path: checkov-frontend-report.json
retention-days: 30
frontend-trivy:
name: 🔒 Frontend Trivy
runs-on: ubuntu-latest
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
continue-on-error: true
with:
scan-type: 'fs'
format: 'json'
output: 'trivy-frontend-report.json'
severity: 'CRITICAL,HIGH,MEDIUM'
- name: Upload Trivy Results
uses: actions/upload-artifact@v4
with:
name: frontend-trivy-report
path: trivy-frontend-report.json
retention-days: 30
# Backend Security Scans
backend-trufflehog:
name: 🐷 Backend TruffleHog
runs-on: ubuntu-latest
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
fetch-depth: 0
- name: TruffleHog Scan
uses: trufflesecurity/[email protected]
continue-on-error: true
with:
path: .
base: ${{ github.event.before }}
head: ${{ github.sha }}
extra_args: --json --output trufflehog-backend-report.json
- name: Upload TruffleHog Results
uses: actions/upload-artifact@v4
with:
name: backend-trufflehog-report
path: trufflehog-backend-report.json
retention-days: 30
backend-semgrep:
name: 🔍 Backend Semgrep
runs-on: ubuntu-latest
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
fetch-depth: 0
- name: Semgrep Scan
uses: semgrep/semgrep-action@v1
continue-on-error: true
with:
config: p/javascript
output: semgrep-backend-results.json
json: true
- name: Upload Semgrep Results
uses: actions/upload-artifact@v4
with:
name: backend-semgrep-report
path: semgrep-backend-results.json
retention-days: 30
backend-snyk:
name: 🛡️ Backend Snyk
runs-on: ubuntu-latest
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
- name: Snyk Scan
uses: snyk/actions/node@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
command: test --json > snyk-backend-report.json
- name: Upload Snyk Results
uses: actions/upload-artifact@v4
with:
name: backend-snyk-report
path: snyk-backend-report.json
retention-days: 30
backend-checkov:
name: 🏗️ Backend Checkov
runs-on: ubuntu-latest
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
- name: Checkov Scan
uses: bridgecrewio/checkov-action@v12
continue-on-error: true
with:
directory: .
framework: dockerfile,kubernetes,secrets
output: json
output-file-path: checkov-backend-report.json
- name: Upload Checkov Results
uses: actions/upload-artifact@v4
with:
name: backend-checkov-report
path: checkov-backend-report.json
retention-days: 30
backend-trivy:
name: 🔒 Backend Trivy
runs-on: ubuntu-latest
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
continue-on-error: true
with:
scan-type: 'fs'
format: 'json'
output: 'trivy-backend-report.json'
severity: 'CRITICAL,HIGH,MEDIUM'
- name: Upload Trivy Results
uses: actions/upload-artifact@v4
with:
name: backend-trivy-report
path: trivy-backend-report.json
retention-days: 30
# Build Jobs
frontend-build:
needs: [frontend-trufflehog, frontend-semgrep, frontend-snyk, frontend-checkov, frontend-trivy]
runs-on: ubuntu-latest
outputs:
image_tag: ${{ github.sha }}
steps:
- name: Checkout Frontend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-frontend
token: ${{ secrets.GH_PAT }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
cache: 'npm'
- name: Install Dependencies
run: npm ci
- name: Run Tests
run: npm test
continue-on-error: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Frontend Image
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: ${{ env.FRONTEND_IMAGE }}:${{ github.sha }}
cache-from: type=registry,ref=${{ env.FRONTEND_IMAGE }}:latest
cache-to: type=inline
build-args: |
NODE_ENV=production
- name: Scan Frontend Image with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.FRONTEND_IMAGE }}:${{ github.sha }}
format: 'json'
output: 'trivy-frontend-image-report.json'
severity: 'CRITICAL,HIGH,MEDIUM'
- name: Upload Trivy Image Scan Results
uses: actions/upload-artifact@v4
with:
name: frontend-trivy-image-report
path: trivy-frontend-image-report.json
retention-days: 30
- name: Push Frontend Image
run: |
docker push ${{ env.FRONTEND_IMAGE }}:${{ github.sha }}
docker tag ${{ env.FRONTEND_IMAGE }}:${{ github.sha }} ${{ env.FRONTEND_IMAGE }}:latest
docker push ${{ env.FRONTEND_IMAGE }}:latest
backend-build:
needs: [backend-trufflehog, backend-semgrep, backend-snyk, backend-checkov, backend-trivy]
runs-on: ubuntu-latest
outputs:
image_tag: ${{ github.sha }}
steps:
- name: Checkout Backend Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
cache: 'npm'
- name: Install Dependencies
run: npm ci
- name: Run Tests
run: npm test
continue-on-error: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Backend Image
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: ${{ env.BACKEND_IMAGE }}:${{ github.sha }}
cache-from: type=registry,ref=${{ env.BACKEND_IMAGE }}:latest
cache-to: type=inline
build-args: |
NODE_ENV=production
- name: Scan Backend Image with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.BACKEND_IMAGE }}:${{ github.sha }}
format: 'json'
output: 'trivy-backend-image-report.json'
severity: 'CRITICAL,HIGH,MEDIUM'
- name: Upload Trivy Image Scan Results
uses: actions/upload-artifact@v4
with:
name: backend-trivy-image-report
path: trivy-backend-image-report.json
retention-days: 30
- name: Push Backend Image
run: |
docker push ${{ env.BACKEND_IMAGE }}:${{ github.sha }}
docker tag ${{ env.BACKEND_IMAGE }}:${{ github.sha }} ${{ env.BACKEND_IMAGE }}:latest
docker push ${{ env.BACKEND_IMAGE }}:latest
deploy:
needs: [frontend-build, backend-build]
runs-on: ubuntu-latest
steps:
- name: Checkout Infrastructure Code
uses: actions/checkout@v4
with:
repository: jai3747/imdb-clone-backend
token: ${{ secrets.GH_PAT }}
path: backend
- name: Google Auth
id: auth
uses: google-github-actions/auth@v1
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
- name: Install kubectl and gke-auth
run: |
gcloud components install kubectl gke-gcloud-auth-plugin
gcloud --quiet auth configure-docker
- name: Get GKE Credentials
run: |
gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} \
--zone ${{ env.CLUSTER_ZONE }} \
--project ${{ secrets.GCP_PROJECT_ID }}
- name: Create Kubernetes Namespaces
run: |
kubectl create namespace mongodb --dry-run=client -o yaml | kubectl apply -f -
kubectl create namespace backend --dry-run=client -o yaml | kubectl apply -f -
kubectl create namespace frontend --dry-run=client -o yaml | kubectl apply -f -
- name: Create Secrets
run: |
kubectl create secret generic mongodb-secret \
--namespace mongodb \
--from-literal=MONGO_URL="${{ secrets.MONGO_URL }}" \
--dry-run=client -o yaml | kubectl apply -f -
kubectl create secret generic backend-secret \
--namespace backend \
--from-literal=MONGO_URL="${{ secrets.MONGO_URL }}" \
--dry-run=client -o yaml | kubectl apply -f -
- name: Update Image Tags
run: |
cd backend
sed -i "s|image: .*imdb-clone-backend.*|image: ${{ env.BACKEND_IMAGE }}:${{ needs.backend-build.outputs.image_tag }}|g" k8.yaml
sed -i "s|image: .*imdb-clone-frontend.*|image: ${{ env.FRONTEND_IMAGE }}:${{ needs.frontend-build.outputs.image_tag }}|g" k8.yaml
- name: Deploy Applications
run: |
cd backend
kubectl apply -f k8.yaml --namespace backend
- name: Wait for Deployments
run: |
kubectl rollout status statefulset/mongodb --namespace mongodb --timeout=5m
kubectl wait --for=condition=ready pod -l app=mongodb --namespace backend --timeout=5m
kubectl rollout status deployment/backend-deployment --namespace backend --timeout=5m
kubectl wait --for=condition=ready pod -l app=backend --namespace backend --timeout=5m
kubectl rollout status deployment/frontend-deployment --namespace backend --timeout=5m
kubectl wait --for=condition=ready pod -l app=frontend --namespace backend --timeout=5m
- name: Verify Deployment
run: |
# echo "=== MongoDB Status ==="
# kubectl get all -n mongodb
echo "=== Backend Status ==="
kubectl get all -n backend
# echo "=== Frontend Status ==="
# kubectl get all -n frontend
# DAST Scanning
dast-scan:
needs: [deploy]
runs-on: ubuntu-latest
steps:
- name: Health Check
run: |
timeout=300
start_time=$(date +%s)
while true; do
current_time=$(date +%s)
elapsed=$((current_time - start_time))
if [ $elapsed -gt $timeout ]; then
echo "Timeout waiting for deployment"
exit 1
fi
if curl -s -f "${{ env.APP_DOMAIN }}/health" >/dev/null; then
echo "Service is up!"
break
fi
echo "Waiting for service to be available..."
sleep 10
done
- name: ZAP Baseline Scan
uses: zaproxy/[email protected]
continue-on-error: true
with:
target: ${{ env.APP_DOMAIN }}
rules_file_name: '.zap/rules.tsv'
cmd_options: '-a -j -I'
allow_issue_writing: true
- name: Run Nuclei Scan
uses: projectdiscovery/nuclei-action@main
continue-on-error: true
with:
target: ${{ env.APP_DOMAIN }}
templates: "cves,vulnerabilities,exposures,misconfiguration"
severity: "critical,high,medium"
output: "nuclei-output.txt"
rate-limit: 150
bulk-size: 25
retry-on-failure: true
# Report Aggregation
security-report:
needs: [dast-scan]
runs-on: ubuntu-latest
if: always()
steps:
- name: Download All Artifacts
uses: actions/download-artifact@v4
with:
path: security-reports
pattern: '*-report*'
merge-multiple: true
- name: Generate Security Dashboard
run: |
echo "# Security Scan Results" > security-dashboard.md
echo "## Summary of Findings" >> security-dashboard.md
# Process ZAP results
if [ -f "security-reports/zap-report.json" ]; then
echo "### ZAP Scan Results" >> security-dashboard.md
jq -r '.site[0].alerts[] | "- " + .name + " (Risk: " + .risk + ")"' security-reports/zap-report.json >> security-dashboard.md
fi
# Process Nuclei results
if [ -f "security-reports/nuclei-output.txt" ]; then
echo "### Nuclei Scan Results" >> security-dashboard.md
echo "\`\`\`" >> security-dashboard.md
cat security-reports/nuclei-output.txt >> security-dashboard.md
echo "\`\`\`" >> security-dashboard.md
fi
# Process Trivy results
if [ -f "security-reports/trivy-frontend-image-report.json" ]; then
echo "### Trivy Frontend Image Scan Results" >> security-dashboard.md
jq -r '.Results[] | select(.Vulnerabilities) | .Vulnerabilities[] | select(.Severity == "CRITICAL" or .Severity == "HIGH") | "- " + .VulnerabilityID + " (" + .Severity + "): " + .Title' security-reports/trivy-frontend-image-report.json >> security-dashboard.md
fi
- name: Upload Security Dashboard
uses: actions/upload-artifact@v4
with:
name: security-dashboard
path: security-dashboard.md
retention-days: 30
- name: Notify on Critical Findings
if: failure()
run: |
echo "Critical security issues found in the scan!"
echo "Please review the security dashboard for details."
exit 1