|
| 1 | +# Security-Enhanced CI/CD Pipeline Template |
| 2 | +# |
| 3 | +# This template demonstrates security best practices for CI/CD pipelines. |
| 4 | +# Adapt this template to your specific security tool and workflow needs. |
| 5 | +# |
| 6 | +# Key Security Features: |
| 7 | +# - SAST (Static Application Security Testing) |
| 8 | +# - Dependency vulnerability scanning |
| 9 | +# - Secrets detection |
| 10 | +# - Infrastructure-as-Code security scanning |
| 11 | +# - Container image scanning |
| 12 | +# - Security artifact uploading for compliance |
| 13 | + |
| 14 | +name: Security Scan Pipeline |
| 15 | + |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: [main, develop] |
| 19 | + pull_request: |
| 20 | + branches: [main, develop] |
| 21 | + schedule: |
| 22 | + # Run weekly security scans on Sunday at 2 AM UTC |
| 23 | + - cron: '0 2 * * 0' |
| 24 | + workflow_dispatch: # Allow manual trigger |
| 25 | + |
| 26 | +# Security: Restrict permissions to minimum required |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | + security-events: write # For uploading SARIF results |
| 30 | + pull-requests: write # For commenting on PRs |
| 31 | + |
| 32 | +env: |
| 33 | + # Configuration |
| 34 | + SECURITY_SCAN_FAIL_ON: 'critical,high' # Fail build on these severities |
| 35 | + REPORT_DIR: 'security-reports' |
| 36 | + |
| 37 | +jobs: |
| 38 | + # Job 1: Static Application Security Testing (SAST) |
| 39 | + sast-scan: |
| 40 | + name: SAST Security Scan |
| 41 | + runs-on: ubuntu-latest |
| 42 | + |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + fetch-depth: 0 # Full history for better analysis |
| 48 | + |
| 49 | + - name: Set up Python |
| 50 | + uses: actions/setup-python@v5 |
| 51 | + with: |
| 52 | + python-version: '3.11' |
| 53 | + |
| 54 | + - name: Run SAST Scanner |
| 55 | + run: | |
| 56 | + # Example: Using Semgrep for SAST |
| 57 | + pip install semgrep |
| 58 | + semgrep --config=auto \ |
| 59 | + --json \ |
| 60 | + --output ${{ env.REPORT_DIR }}/sast-results.json \ |
| 61 | + . || true |
| 62 | +
|
| 63 | + # Alternative: Bandit for Python projects |
| 64 | + # pip install bandit |
| 65 | + # bandit -r . -f json -o ${{ env.REPORT_DIR }}/bandit-results.json |
| 66 | +
|
| 67 | + - name: Process SAST Results |
| 68 | + run: | |
| 69 | + # Parse results and fail on critical/high severity |
| 70 | + python3 -c " |
| 71 | + import json |
| 72 | + import sys |
| 73 | +
|
| 74 | + with open('${{ env.REPORT_DIR }}/sast-results.json') as f: |
| 75 | + results = json.load(f) |
| 76 | +
|
| 77 | + critical = len([r for r in results.get('results', []) if r.get('extra', {}).get('severity') == 'ERROR']) |
| 78 | + high = len([r for r in results.get('results', []) if r.get('extra', {}).get('severity') == 'WARNING']) |
| 79 | +
|
| 80 | + print(f'Critical findings: {critical}') |
| 81 | + print(f'High findings: {high}') |
| 82 | +
|
| 83 | + if critical > 0: |
| 84 | + print('❌ Build failed: Critical security issues found') |
| 85 | + sys.exit(1) |
| 86 | + elif high > 0: |
| 87 | + print('⚠️ Warning: High severity issues found') |
| 88 | + # Optionally fail on high severity |
| 89 | + # sys.exit(1) |
| 90 | + else: |
| 91 | + print('✅ No critical security issues found') |
| 92 | + " |
| 93 | +
|
| 94 | + - name: Upload SAST Results |
| 95 | + if: always() |
| 96 | + uses: actions/upload-artifact@v4 |
| 97 | + with: |
| 98 | + name: sast-results |
| 99 | + path: ${{ env.REPORT_DIR }}/sast-results.json |
| 100 | + retention-days: 30 |
| 101 | + |
| 102 | + # Job 2: Dependency Vulnerability Scanning |
| 103 | + dependency-scan: |
| 104 | + name: Dependency Vulnerability Scan |
| 105 | + runs-on: ubuntu-latest |
| 106 | + |
| 107 | + steps: |
| 108 | + - name: Checkout code |
| 109 | + uses: actions/checkout@v4 |
| 110 | + |
| 111 | + - name: Set up Python |
| 112 | + uses: actions/setup-python@v5 |
| 113 | + with: |
| 114 | + python-version: '3.11' |
| 115 | + |
| 116 | + - name: Scan Python Dependencies |
| 117 | + if: hashFiles('requirements.txt') != '' |
| 118 | + run: | |
| 119 | + pip install safety |
| 120 | + safety check \ |
| 121 | + --json \ |
| 122 | + --output ${{ env.REPORT_DIR }}/safety-results.json \ |
| 123 | + || true |
| 124 | +
|
| 125 | + - name: Scan Node Dependencies |
| 126 | + if: hashFiles('package.json') != '' |
| 127 | + run: | |
| 128 | + npm audit --json > ${{ env.REPORT_DIR }}/npm-audit.json || true |
| 129 | +
|
| 130 | + - name: Process Dependency Results |
| 131 | + run: | |
| 132 | + # Check for critical vulnerabilities |
| 133 | + if [ -f "${{ env.REPORT_DIR }}/safety-results.json" ]; then |
| 134 | + critical_count=$(python3 -c "import json; data=json.load(open('${{ env.REPORT_DIR }}/safety-results.json')); print(len([v for v in data.get('vulnerabilities', []) if v.get('severity', '').lower() == 'critical']))") |
| 135 | + echo "Critical vulnerabilities: $critical_count" |
| 136 | + if [ "$critical_count" -gt "0" ]; then |
| 137 | + echo "❌ Build failed: Critical vulnerabilities in dependencies" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + fi |
| 141 | +
|
| 142 | + - name: Upload Dependency Scan Results |
| 143 | + if: always() |
| 144 | + uses: actions/upload-artifact@v4 |
| 145 | + with: |
| 146 | + name: dependency-scan-results |
| 147 | + path: ${{ env.REPORT_DIR }}/ |
| 148 | + retention-days: 30 |
| 149 | + |
| 150 | + # Job 3: Secrets Detection |
| 151 | + secrets-scan: |
| 152 | + name: Secrets Detection |
| 153 | + runs-on: ubuntu-latest |
| 154 | + |
| 155 | + steps: |
| 156 | + - name: Checkout code |
| 157 | + uses: actions/checkout@v4 |
| 158 | + with: |
| 159 | + fetch-depth: 0 # Full history to scan all commits |
| 160 | + |
| 161 | + - name: Run Gitleaks |
| 162 | + uses: gitleaks/gitleaks-action@v2 |
| 163 | + env: |
| 164 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 165 | + GITLEAKS_ENABLE_SUMMARY: true |
| 166 | + |
| 167 | + - name: Alternative - TruffleHog Scan |
| 168 | + if: false # Set to true to enable |
| 169 | + run: | |
| 170 | + pip install truffleHog |
| 171 | + trufflehog --json --regex --entropy=True . \ |
| 172 | + > ${{ env.REPORT_DIR }}/trufflehog-results.json || true |
| 173 | +
|
| 174 | + - name: Upload Secrets Scan Results |
| 175 | + if: always() |
| 176 | + uses: actions/upload-artifact@v4 |
| 177 | + with: |
| 178 | + name: secrets-scan-results |
| 179 | + path: ${{ env.REPORT_DIR }}/ |
| 180 | + retention-days: 30 |
| 181 | + |
| 182 | + # Job 4: Container Image Scanning |
| 183 | + container-scan: |
| 184 | + name: Container Image Security Scan |
| 185 | + runs-on: ubuntu-latest |
| 186 | + if: hashFiles('Dockerfile') != '' |
| 187 | + |
| 188 | + steps: |
| 189 | + - name: Checkout code |
| 190 | + uses: actions/checkout@v4 |
| 191 | + |
| 192 | + - name: Build Docker Image |
| 193 | + run: | |
| 194 | + docker build -t app:${{ github.sha }} . |
| 195 | +
|
| 196 | + - name: Run Trivy Scanner |
| 197 | + uses: aquasecurity/trivy-action@master |
| 198 | + with: |
| 199 | + image-ref: app:${{ github.sha }} |
| 200 | + format: 'sarif' |
| 201 | + output: '${{ env.REPORT_DIR }}/trivy-results.sarif' |
| 202 | + severity: 'CRITICAL,HIGH' |
| 203 | + |
| 204 | + - name: Upload Trivy Results to GitHub Security |
| 205 | + if: always() |
| 206 | + uses: github/codeql-action/upload-sarif@v3 |
| 207 | + with: |
| 208 | + sarif_file: '${{ env.REPORT_DIR }}/trivy-results.sarif' |
| 209 | + |
| 210 | + - name: Upload Container Scan Results |
| 211 | + if: always() |
| 212 | + uses: actions/upload-artifact@v4 |
| 213 | + with: |
| 214 | + name: container-scan-results |
| 215 | + path: ${{ env.REPORT_DIR }}/ |
| 216 | + retention-days: 30 |
| 217 | + |
| 218 | + # Job 5: Infrastructure-as-Code Security Scanning |
| 219 | + iac-scan: |
| 220 | + name: IaC Security Scan |
| 221 | + runs-on: ubuntu-latest |
| 222 | + if: hashFiles('**/*.tf', '**/*.yaml', '**/*.yml') != '' |
| 223 | + |
| 224 | + steps: |
| 225 | + - name: Checkout code |
| 226 | + uses: actions/checkout@v4 |
| 227 | + |
| 228 | + - name: Run Checkov |
| 229 | + run: | |
| 230 | + pip install checkov |
| 231 | + checkov -d . \ |
| 232 | + --output json \ |
| 233 | + --output-file ${{ env.REPORT_DIR }}/checkov-results.json \ |
| 234 | + --quiet \ |
| 235 | + || true |
| 236 | +
|
| 237 | + - name: Run tfsec (for Terraform) |
| 238 | + if: hashFiles('**/*.tf') != '' |
| 239 | + run: | |
| 240 | + curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bash |
| 241 | + tfsec . \ |
| 242 | + --format json \ |
| 243 | + --out ${{ env.REPORT_DIR }}/tfsec-results.json \ |
| 244 | + || true |
| 245 | +
|
| 246 | + - name: Process IaC Results |
| 247 | + run: | |
| 248 | + # Fail on critical findings |
| 249 | + if [ -f "${{ env.REPORT_DIR }}/checkov-results.json" ]; then |
| 250 | + critical_count=$(python3 -c "import json; data=json.load(open('${{ env.REPORT_DIR }}/checkov-results.json')); print(data.get('summary', {}).get('failed', 0))") |
| 251 | + echo "Failed checks: $critical_count" |
| 252 | + if [ "$critical_count" -gt "0" ]; then |
| 253 | + echo "⚠️ Warning: IaC security issues found" |
| 254 | + # Optionally fail the build |
| 255 | + # exit 1 |
| 256 | + fi |
| 257 | + fi |
| 258 | +
|
| 259 | + - name: Upload IaC Scan Results |
| 260 | + if: always() |
| 261 | + uses: actions/upload-artifact@v4 |
| 262 | + with: |
| 263 | + name: iac-scan-results |
| 264 | + path: ${{ env.REPORT_DIR }}/ |
| 265 | + retention-days: 30 |
| 266 | + |
| 267 | + # Job 6: Security Report Generation and Notification |
| 268 | + security-report: |
| 269 | + name: Generate Security Report |
| 270 | + runs-on: ubuntu-latest |
| 271 | + needs: [sast-scan, dependency-scan, secrets-scan] |
| 272 | + if: always() |
| 273 | + |
| 274 | + steps: |
| 275 | + - name: Checkout code |
| 276 | + uses: actions/checkout@v4 |
| 277 | + |
| 278 | + - name: Download All Scan Results |
| 279 | + uses: actions/download-artifact@v4 |
| 280 | + with: |
| 281 | + path: all-results/ |
| 282 | + |
| 283 | + - name: Generate Consolidated Report |
| 284 | + run: | |
| 285 | + # Consolidate all security scan results |
| 286 | + mkdir -p consolidated-report |
| 287 | +
|
| 288 | + cat > consolidated-report/security-summary.md << 'EOF' |
| 289 | + # Security Scan Summary |
| 290 | +
|
| 291 | + **Scan Date**: $(date -u +"%Y-%m-%d %H:%M:%S UTC") |
| 292 | + **Commit**: ${{ github.sha }} |
| 293 | + **Branch**: ${{ github.ref_name }} |
| 294 | +
|
| 295 | + ## Scan Results |
| 296 | +
|
| 297 | + ### SAST Scan |
| 298 | + See artifacts: `sast-results` |
| 299 | +
|
| 300 | + ### Dependency Scan |
| 301 | + See artifacts: `dependency-scan-results` |
| 302 | +
|
| 303 | + ### Secrets Scan |
| 304 | + See artifacts: `secrets-scan-results` |
| 305 | +
|
| 306 | + ### Container Scan |
| 307 | + See artifacts: `container-scan-results` |
| 308 | +
|
| 309 | + ### IaC Scan |
| 310 | + See artifacts: `iac-scan-results` |
| 311 | +
|
| 312 | + --- |
| 313 | +
|
| 314 | + For detailed results, download scan artifacts from this workflow run. |
| 315 | + EOF |
| 316 | +
|
| 317 | + - name: Comment on PR (if applicable) |
| 318 | + if: github.event_name == 'pull_request' |
| 319 | + uses: actions/github-script@v7 |
| 320 | + with: |
| 321 | + script: | |
| 322 | + const fs = require('fs'); |
| 323 | + const report = fs.readFileSync('consolidated-report/security-summary.md', 'utf8'); |
| 324 | +
|
| 325 | + github.rest.issues.createComment({ |
| 326 | + issue_number: context.issue.number, |
| 327 | + owner: context.repo.owner, |
| 328 | + repo: context.repo.repo, |
| 329 | + body: report |
| 330 | + }); |
| 331 | +
|
| 332 | + - name: Upload Consolidated Report |
| 333 | + if: always() |
| 334 | + uses: actions/upload-artifact@v4 |
| 335 | + with: |
| 336 | + name: consolidated-security-report |
| 337 | + path: consolidated-report/ |
| 338 | + retention-days: 90 |
| 339 | + |
| 340 | +# Security Best Practices Demonstrated: |
| 341 | +# |
| 342 | +# 1. ✅ Minimal permissions (principle of least privilege) |
| 343 | +# 2. ✅ Multiple security scan types (defense in depth) |
| 344 | +# 3. ✅ Fail-fast on critical findings |
| 345 | +# 4. ✅ Secrets detection across full git history |
| 346 | +# 5. ✅ Container image scanning before deployment |
| 347 | +# 6. ✅ IaC scanning for misconfigurations |
| 348 | +# 7. ✅ Artifact retention for compliance audit trail |
| 349 | +# 8. ✅ SARIF format for GitHub Security integration |
| 350 | +# 9. ✅ Scheduled scans for continuous monitoring |
| 351 | +# 10. ✅ PR comments for developer feedback |
| 352 | +# |
| 353 | +# Compliance Mappings: |
| 354 | +# - SOC 2: CC6.1, CC6.6, CC7.2 (Security monitoring and logging) |
| 355 | +# - PCI-DSS: 6.2, 6.5 (Secure development practices) |
| 356 | +# - NIST: SA-11 (Developer Security Testing) |
| 357 | +# - OWASP: Integrated security testing throughout SDLC |
0 commit comments