Mengimplementasikan security testing as code: definition-as-code, test data management, reproducibility, dan infrastructure as code security

Setelah di episode 23 kita menguji AI/LLM applications, pada episode ini kita membahas pendekatan modern: security testing as code. Daripada manual testing yang tidak reproducible, security tests ditulis dalam code yang bisa di-version, di-review, dan di-run secara konsisten.
Security Testing as Code:
├── Test definitions dalam code (bukan manual docs)
├── Automated execution di CI/CD
├── Version controlled (git)
├── Reproducible results
├── Reviewable (PR process)
└── Reportable (automated reporting)# .semgrep/custom-rules.yml
rules:
- id: hardcoded-secret
pattern: |
password = "..."
message: "Hardcoded password detected"
languages: [python]
severity: ERROR
- id: sql-injection
pattern: |
$QUERY = "..." + $INPUT
message: "Potential SQL injection"
languages: [python]
severity: WARNING#!/bin/bash
# security-tests.sh - Security test suite
set -e
echo "=== SAST Scan ==="
semgrep --config p/owasp-top-ten --json .
echo "=== SCA Audit ==="
npm audit --audit-level=high
echo "=== Secret Scan ==="
trufflehog filesystem .
echo "=== Container Scan ==="
trivy image --severity HIGH,CRITICAL $IMAGE_NAME
echo "=== All security tests passed ==="# Generate test data yang aman
# 1. Fake data (bukan real PII)
faker-cli --locale=en generate email 100
# 2. Anonymized production data
anonymize-db --input prod-dump.sql --output test-dump.sql
# 3. Synthetic data
python generate-test-data.py --count 1000 --output test-data.json# docker-compose.test.yml
version: '3.8'
services:
app:
build: .
environment:
- DATABASE_URL=postgresql://test:test@db:5432/test
- REDIS_URL=redis://redis:6379
db:
image: postgres:16
environment:
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
redis:
image: redis:alpine# Pin tool versions
semgrep --version # Pastikan version konsisten
trivy --version
# Pin config versions
# .semgrep/config.yml → commit ke git
# .trivyignore → commit ke git# Simpan baseline
semgrep --config p/owasp-top-ten --json . > baseline/semgrep.json
npm audit --json > baseline/npm-audit.json
# Di CI, bandingkan
semgrep --config p/owasp-top-ten --json . > current/semgrep.json
diff baseline/semgrep.json current/semgrep.json# Generate report dari scan results
python generate-report.py \
--semgrep current/semgrep.json \
--npm-audit current/npm-audit.json \
--trivy current/trivy.json \
--output security-report.html
# Upload ke dashboard
curl -X POST https://security-dashboard.internal/api/reports \
-F "report=@security-report.html" \
-F "branch=$(git branch --show-current)"Tip
Mulai dengan security-tests.sh yang menjalankan semua scan. Setelah itu, refactor menjadi pipeline CI/CD yang terpisah per scan type.
# 1. Buat security test script
cat > security-tests.sh << 'EOF'
#!/bin/bash
set -e
echo "Running security tests..."
semgrep --config p/owasp-top-ten --json .
npm audit --audit-level=high
EOF
chmod +x security-tests.sh
# 2. Jalankan
./security-tests.sh
# 3. Commit ke git
git add security-tests.sh
git commit -m "chore: add security test suite"Di episode 25 selanjutnya kita akan membahas security test automation framework — membangun reusable framework, coverage metrics, dan shared libraries. Sampai jumpa di episode 25!