Repository navigation
Expand file tree
/
Copy pathsecurity-scan.sh
More file actions
350 lines (276 loc) · 13.5 KB
/
Copy pathsecurity-scan.sh
File metadata and controls
350 lines (276 loc) · 13.5 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
#!/bin/bash
# DevSecOps Security Scanning Script
# This script runs comprehensive security scans for the DevOps pipeline
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DOCKER_IMAGE="${DOCKER_IMAGE:-eknathdj/devops-sample-app}"
DOCKER_TAG="${DOCKER_TAG:-latest}"
SECURITY_REPORTS_DIR="security-reports"
TRIVY_CACHE_DIR=".trivy"
# Security thresholds
CRITICAL_THRESHOLD=0
HIGH_THRESHOLD=5
MEDIUM_THRESHOLD=10
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_tool() {
local tool=$1
local install_cmd=$2
if ! command -v "$tool" &> /dev/null; then
log_warning "$tool not found. Installing..."
eval "$install_cmd"
else
log_info "$tool is available"
fi
}
install_security_tools() {
log_info "Installing security scanning tools..."
# Create reports directory
mkdir -p "$SECURITY_REPORTS_DIR"
mkdir -p "$TRIVY_CACHE_DIR"
# Install gitleaks
check_tool "gitleaks" "curl -sSfL https://github.com/zricethezav/gitleaks/releases/download/v8.18.0/gitleaks_8.18.0_linux_x64.tar.gz | tar -xz && sudo mv gitleaks /usr/local/bin/"
# Install trivy
check_tool "trivy" "curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.46.0"
# Install hadolint
check_tool "hadolint" "wget -O hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64 && chmod +x hadolint && sudo mv hadolint /usr/local/bin/"
# Install Python tools
pip3 install --user detect-secrets semgrep checkov bandit safety || log_warning "Some Python tools failed to install"
log_success "Security tools installation completed"
}
scan_secrets() {
log_info "🔐 Running secret detection scans..."
# Gitleaks scan
log_info "Running gitleaks scan..."
if gitleaks detect --source . --report-format json --report-path "$SECURITY_REPORTS_DIR/gitleaks-report.json" --exit-code 1; then
log_success "No secrets detected by gitleaks"
else
log_error "Secrets detected by gitleaks! Check the report."
return 1
fi
# detect-secrets scan
log_info "Running detect-secrets scan..."
if detect-secrets scan --baseline .secrets.baseline --force-use-all-plugins; then
log_success "detect-secrets scan passed"
else
log_error "New secrets detected by detect-secrets!"
return 1
fi
log_success "Secret detection completed"
}
scan_sast() {
log_info "📋 Running Static Application Security Testing (SAST)..."
# Semgrep scan
log_info "Running Semgrep SAST scan..."
semgrep --config=auto --json --output="$SECURITY_REPORTS_DIR/semgrep-report.json" . || log_warning "Semgrep found issues"
# Count issues
if [ -f "$SECURITY_REPORTS_DIR/semgrep-report.json" ]; then
CRITICAL_COUNT=$(jq '[.results[] | select(.extra.severity == "ERROR")] | length' "$SECURITY_REPORTS_DIR/semgrep-report.json" 2>/dev/null || echo "0")
HIGH_COUNT=$(jq '[.results[] | select(.extra.severity == "WARNING")] | length' "$SECURITY_REPORTS_DIR/semgrep-report.json" 2>/dev/null || echo "0")
log_info "SAST Results: Critical: $CRITICAL_COUNT, High: $HIGH_COUNT"
if [ "$CRITICAL_COUNT" -gt "$CRITICAL_THRESHOLD" ]; then
log_error "Critical SAST issues exceed threshold ($CRITICAL_COUNT > $CRITICAL_THRESHOLD)"
return 1
fi
fi
# Bandit scan for Python (if applicable)
if find . -name "*.py" -type f | head -1 | grep -q .; then
log_info "Running Bandit Python security scan..."
bandit -r . -f json -o "$SECURITY_REPORTS_DIR/bandit-report.json" || log_warning "Bandit found issues"
fi
log_success "SAST scanning completed"
}
scan_dockerfile() {
log_info "🐳 Scanning Dockerfile security..."
# Hadolint scan
log_info "Running Hadolint Dockerfile scan..."
hadolint Dockerfile --format json > "$SECURITY_REPORTS_DIR/hadolint-report.json" || log_warning "Dockerfile issues found"
log_success "Dockerfile scanning completed"
}
scan_dependencies() {
log_info "📦 Scanning dependencies for vulnerabilities..."
# Safety scan for Python
if [ -f "requirements.txt" ]; then
log_info "Running Safety scan for Python dependencies..."
safety check --json --output "$SECURITY_REPORTS_DIR/safety-report.json" || log_warning "Python dependency vulnerabilities found"
fi
# npm audit for Node.js
if [ -f "package.json" ]; then
log_info "Running npm audit for Node.js dependencies..."
npm audit --json > "$SECURITY_REPORTS_DIR/npm-audit-report.json" || log_warning "Node.js dependency vulnerabilities found"
fi
# Go mod scan
if [ -f "go.mod" ]; then
log_info "Running Go module vulnerability scan..."
go list -json -deps ./... | nancy sleuth > "$SECURITY_REPORTS_DIR/nancy-report.json" || log_warning "Go dependency vulnerabilities found"
fi
log_success "Dependency scanning completed"
}
scan_infrastructure() {
log_info "🛡️ Scanning Infrastructure as Code..."
# Checkov scan
log_info "Running Checkov IaC scan..."
checkov -d k8s/ --framework kubernetes --output json --output-file "$SECURITY_REPORTS_DIR/checkov-k8s-report.json" || log_warning "Kubernetes IaC issues found"
checkov -f Dockerfile --framework dockerfile --output json --output-file "$SECURITY_REPORTS_DIR/checkov-docker-report.json" || log_warning "Dockerfile IaC issues found"
# kube-score scan
if command -v kube-score &> /dev/null; then
log_info "Running kube-score analysis..."
find k8s/ -name "*.yaml" -exec kube-score score {} \; > "$SECURITY_REPORTS_DIR/kube-score-report.txt" || log_warning "Kubernetes best practice issues found"
fi
log_success "Infrastructure scanning completed"
}
scan_container() {
log_info "🔍 Scanning container image for vulnerabilities..."
if [ -z "$DOCKER_IMAGE" ]; then
log_error "DOCKER_IMAGE not set"
return 1
fi
# Trivy vulnerability scan
log_info "Running Trivy vulnerability scan on $DOCKER_IMAGE:$DOCKER_TAG..."
trivy image \
--cache-dir "$TRIVY_CACHE_DIR" \
--format json \
--output "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" \
--severity CRITICAL,HIGH,MEDIUM \
"$DOCKER_IMAGE:$DOCKER_TAG"
# Trivy configuration scan
log_info "Running Trivy configuration scan..."
trivy config \
--cache-dir "$TRIVY_CACHE_DIR" \
--format json \
--output "$SECURITY_REPORTS_DIR/trivy-config-report.json" \
--severity CRITICAL,HIGH \
.
# Check vulnerability counts
if [ -f "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" ]; then
CRITICAL_VULNS=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "CRITICAL")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0")
HIGH_VULNS=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "HIGH")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0")
MEDIUM_VULNS=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "MEDIUM")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0")
log_info "Vulnerability Summary: Critical: $CRITICAL_VULNS, High: $HIGH_VULNS, Medium: $MEDIUM_VULNS"
# Security gate checks
if [ "$CRITICAL_VULNS" -gt "$CRITICAL_THRESHOLD" ]; then
log_error "Critical vulnerabilities exceed threshold ($CRITICAL_VULNS > $CRITICAL_THRESHOLD)"
return 1
fi
if [ "$HIGH_VULNS" -gt "$HIGH_THRESHOLD" ]; then
log_error "High vulnerabilities exceed threshold ($HIGH_VULNS > $HIGH_THRESHOLD)"
return 1
fi
fi
log_success "Container scanning completed"
}
generate_report() {
log_info "📊 Generating comprehensive security report..."
cat > "$SECURITY_REPORTS_DIR/security-summary.html" << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>DevSecOps Security Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.header { background: #f4f4f4; padding: 20px; border-radius: 5px; }
.section { margin: 20px 0; padding: 15px; border-left: 4px solid #007cba; }
.success { border-left-color: #28a745; }
.warning { border-left-color: #ffc107; }
.error { border-left-color: #dc3545; }
.metric { display: inline-block; margin: 10px; padding: 10px; background: #f8f9fa; border-radius: 3px; }
table { width: 100%; border-collapse: collapse; margin: 10px 0; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<div class="header">
<h1>🛡️ DevSecOps Security Scan Report</h1>
<p><strong>Generated:</strong> $(date)</p>
<p><strong>Image:</strong> ${DOCKER_IMAGE}:${DOCKER_TAG}</p>
</div>
EOF
# Add scan results to HTML report
echo '<div class="section success"><h2>✅ Completed Scans</h2>' >> "$SECURITY_REPORTS_DIR/security-summary.html"
# Check which scans completed successfully
for report in gitleaks semgrep trivy-vuln hadolint checkov-k8s; do
if [ -f "$SECURITY_REPORTS_DIR/${report}-report.json" ]; then
echo "<p>✅ $report scan completed</p>" >> "$SECURITY_REPORTS_DIR/security-summary.html"
else
echo "<p>❌ $report scan missing</p>" >> "$SECURITY_REPORTS_DIR/security-summary.html"
fi
done
echo '</div>' >> "$SECURITY_REPORTS_DIR/security-summary.html"
echo '</body></html>' >> "$SECURITY_REPORTS_DIR/security-summary.html"
# Generate markdown summary
cat > "$SECURITY_REPORTS_DIR/SECURITY_SUMMARY.md" << EOF
# 🛡️ DevSecOps Security Report
**Date:** $(date)
**Image:** ${DOCKER_IMAGE}:${DOCKER_TAG}
**Scan Status:** $([ $? -eq 0 ] && echo "✅ PASSED" || echo "❌ FAILED")
## 📊 Security Metrics
| Scan Type | Status | Critical | High | Medium |
|-----------|--------|----------|------|--------|
| Secrets | $([ -f "$SECURITY_REPORTS_DIR/gitleaks-report.json" ] && echo "✅" || echo "❌") | - | - | - |
| SAST | $([ -f "$SECURITY_REPORTS_DIR/semgrep-report.json" ] && echo "✅" || echo "❌") | $([ -f "$SECURITY_REPORTS_DIR/semgrep-report.json" ] && jq '[.results[] | select(.extra.severity == "ERROR")] | length' "$SECURITY_REPORTS_DIR/semgrep-report.json" 2>/dev/null || echo "0") | $([ -f "$SECURITY_REPORTS_DIR/semgrep-report.json" ] && jq '[.results[] | select(.extra.severity == "WARNING")] | length' "$SECURITY_REPORTS_DIR/semgrep-report.json" 2>/dev/null || echo "0") | - |
| Container | $([ -f "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" ] && echo "✅" || echo "❌") | $([ -f "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" ] && jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "CRITICAL")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0") | $([ -f "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" ] && jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "HIGH")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0") | $([ -f "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" ] && jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "MEDIUM")] | length' "$SECURITY_REPORTS_DIR/trivy-vuln-report.json" 2>/dev/null || echo "0") |
| IaC | $([ -f "$SECURITY_REPORTS_DIR/checkov-k8s-report.json" ] && echo "✅" || echo "❌") | - | - | - |
## 🎯 Security Gates
- **Critical Vulnerabilities:** ≤ $CRITICAL_THRESHOLD
- **High Vulnerabilities:** ≤ $HIGH_THRESHOLD
- **Medium Vulnerabilities:** ≤ $MEDIUM_THRESHOLD
## 📋 Recommendations
1. **Keep Dependencies Updated:** Regularly update base images and application dependencies
2. **Implement Secret Management:** Use external secret managers (HashiCorp Vault, AWS Secrets Manager)
3. **Enable Runtime Security:** Deploy runtime security monitoring tools
4. **Network Segmentation:** Implement Kubernetes network policies
5. **Access Controls:** Apply RBAC and least-privilege principles
---
*Generated by DevSecOps Security Scanner*
EOF
log_success "Security report generated at $SECURITY_REPORTS_DIR/"
}
# Main execution
main() {
log_info "🚀 Starting DevSecOps security scanning..."
local exit_code=0
# Install tools
install_security_tools
# Run scans
scan_secrets || exit_code=1
scan_sast || exit_code=1
scan_dockerfile || exit_code=1
scan_dependencies || exit_code=1
scan_infrastructure || exit_code=1
# Container scan only if image exists
if docker image inspect "$DOCKER_IMAGE:$DOCKER_TAG" &> /dev/null; then
scan_container || exit_code=1
else
log_warning "Docker image $DOCKER_IMAGE:$DOCKER_TAG not found, skipping container scan"
fi
# Generate report
generate_report
if [ $exit_code -eq 0 ]; then
log_success "🎉 All security scans completed successfully!"
else
log_error "💥 Some security scans failed. Check the reports for details."
fi
return $exit_code
}
# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi