1. Vì sao DevSecOps?
Truyền thống: Security là phase cuối — Pen-tester scan trước release, log bug, dev fix sau. Vấn đề:
- Bug security phát hiện muộn → cost fix cao 100×.
- Pentest 2 tuần/release → bottleneck.
- Dev không học từ bug security.
- Compliance audit = rush 2 tuần trước.
DevSecOps: tích hợp security vào mọi giai đoạn DevOps lifecycle.
1.1. Cost of late security
| Phase | Cost to fix bug |
|---|---|
| Design | 1× |
| Code | 10× |
| Test | 30× |
| Production | 100-500× |
Source: NIST. Bug security có production = patch + incident response + customer notification + audit.
1.2. Recent breaches
- SolarWinds 2020 — supply chain attack, 18,000 organizations affected.
- Log4Shell 2021 — RCE qua dependency, internet meltdown.
- Codecov 2021 — bash uploader compromise → leak credentials of 100s companies.
- 3CX 2023 — supply chain double-tier attack.
- XZ Utils 2024 — multi-year backdoor in critical OSS lib (caught early).
Common pattern: supply chain. Bạn không phải target chính — attacker compromise dependency bạn dùng.
1.3. DevSecOps principles
- Shift-left — phát hiện sớm trong pipeline.
- Automate — security check là code, không manual.
- Defense in depth — nhiều layer (network + app + data).
- Least privilege — IAM, RBAC, network policy.
- Zero trust — không trust gì mặc định.
- Continuous — security không phải project, là practice.
- Shared responsibility — dev + security cùng own.
2. Shift-Left Security — Pipeline Integration
| Stage | Security check |
|---|---|
| IDE | Linter security plugin (ESLint security, Bandit), Snyk CLI |
| Pre-commit | gitleaks (secret scan), pre-commit-terraform |
| PR / CI | SAST, SCA, IaC scan, license check |
| Build | Container scan (Trivy), SBOM generation, sign artifact |
| Deploy | Admission controller (OPA, Kyverno), policy check, signature verify |
| Runtime | Falco, RASP, threat detection (GuardDuty, Cilium Tetragon) |
2.1. CI security pipeline example
# .github/workflows/security.yml
name: Security
on: [pull_request, push]
jobs:
secret-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- uses: gitleaks/gitleaks-action@v2
sca:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 20 }
- run: npm audit --audit-level=high
- uses: snyk/actions/node@master
env: { SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} }
sast:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: returntocorp/semgrep-action@v1
with:
config: p/owasp-top-ten
iac-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aquasecurity/trivy-action@master
with:
scan-type: config
severity: HIGH,CRITICAL
exit-code: 1
container-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build -t myapp:${{ github.sha }} .
- uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
severity: CRITICAL
exit-code: 1
3. SAST — Static Application Security Testing
Analyze source code (không run) để tìm vulnerability pattern.
3.1. Tools
| Tool | License | Languages |
|---|---|---|
| Semgrep | OSS + paid | 30+ languages |
| SonarQube | OSS + paid | 30+ languages |
| Snyk Code | Paid | Most |
| Checkmarx | Paid (enterprise) | Most |
| GitHub CodeQL | Free public, paid private | 10+ languages |
| Bandit | OSS | Python |
| gosec | OSS | Go |
| brakeman | OSS | Ruby |
3.2. Semgrep example
# Run với rules có sẵn
semgrep --config p/security-audit .
semgrep --config p/owasp-top-ten .
semgrep --config p/javascript .
# Custom rule
cat > rules/sql-injection.yml <<EOF
rules:
- id: sql-injection
pattern: |
query = "SELECT * FROM users WHERE id = " + \$X
message: "SQL injection risk: don't concatenate user input"
severity: ERROR
languages: [python, javascript]
EOF
semgrep --config rules/ .
3.3. Vulnerability classes
- Injection — SQL, command, LDAP injection.
- XSS — Cross-site scripting.
- Insecure crypto — MD5, SHA1, ECB, hardcoded key.
- Path traversal —
../file access. - SSRF — Server-Side Request Forgery.
- XXE — XML External Entity.
- Insecure deserialization.
Reference: OWASP Top 10.
3.4. Suppress false positive
// Semgrep nosemgrep
const html = userInput; // nosemgrep: javascript.lang.security.audit.unsafe-html
// Justification COMMENT cần thiết — không bypass blindly
Track suppression — quarterly review.
4. DAST — Dynamic Application Security Testing
Test running app (black box) — gửi malicious request, observe response.
4.1. Tools
- OWASP ZAP — open source, GUI + CLI.
- Burp Suite — paid, popular pentest tool.
- Nuclei — template-based scanner.
- nikto — web server scanner.
- sqlmap — SQL injection specialist.
4.2. ZAP trong CI
- name: ZAP baseline scan
uses: zaproxy/action-baseline@v0.10.0
with:
target: 'https://staging.example.com'
rules_file_name: '.zap/rules.tsv'
cmd_options: '-a'
4.3. SAST vs DAST so sánh
SAST
- Source code analysis
- Tìm sớm (commit time)
- Cao false positive
- Không thấy runtime issue (config, infra)
- Cover mọi path code
DAST
- Black box, no source
- Test running app
- Low false positive
- Chỉ thấy path được test
- Catch runtime issue (auth, session)
Use both — complement.
4.4. IAST — middle ground
Interactive Application Security Testing: agent trong runtime, observe code execution + input. Best of both. Tools: Contrast, Veracode IAST.
5. SCA — Software Composition Analysis
Modern app = 90%+ third-party dependency. SCA scan dependency cho known vulnerabilities (CVE).
5.1. Tools
- npm audit, yarn audit, pnpm audit (Node).
- pip-audit, safety (Python).
- bundle audit (Ruby).
- govulncheck (Go).
- Snyk — multi-language, IDE + CLI + CI.
- Dependabot (GitHub) — auto PR upgrade.
- Renovate — alternative, customizable.
- OWASP Dependency-Check.
- Trivy — multi-purpose (container + SCA + IaC).
5.2. Dependabot config
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: npm
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 10
versioning-strategy: increase
groups:
eslint:
patterns: ["eslint*"]
typescript:
patterns: ["typescript", "@types/*"]
- package-ecosystem: docker
directory: /
schedule:
interval: weekly
- package-ecosystem: terraform
directory: /infra
schedule:
interval: weekly
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
5.3. Vulnerability severity
| Score (CVSS) | Severity | SLA fix |
|---|---|---|
| 9.0-10.0 | Critical | 24-48h |
| 7.0-8.9 | High | 1 tuần |
| 4.0-6.9 | Medium | 1 tháng |
| 0.1-3.9 | Low | Quarterly review |
5.4. Reachability analysis
1 dependency có CVE không = bạn vulnerable. Cần check: code có thực sự call vulnerable function không?
Tools: Snyk Reachability, Endor Labs — reduce noise hiệu quả 70-90%.
5.5. License compliance
Một số license (GPL, AGPL) yêu cầu open source code dùng nó. Vi phạm = legal issue.
Tools: FOSSA, BlackDuck, Snyk License, license-checker (npm).
Whitelist phổ biến cho proprietary: MIT, Apache-2.0, BSD-3-Clause, ISC.
6. Secret Scanning
6.1. Tools
- gitleaks — fast, popular.
- trufflehog — entropy + verifier.
- detect-secrets (Yelp).
- GitHub Secret Scanning — built-in.
- GitGuardian — SaaS, real-time scan.
6.2. gitleaks pre-commit
# .pre-commit-config.yaml
repos:
- repo: https://github.com/zricethezav/gitleaks
rev: v8.18.0
hooks:
- id: gitleaks
# CLI
gitleaks detect --source . --verbose
gitleaks detect --source . --report-format json --report-path leaks.json
# Scan git history
gitleaks detect --source . --no-banner --no-color
6.3. GitHub Secret Scanning
Free for public repos, paid Advanced Security cho private. Pattern detection cho 100+ provider (AWS, Stripe, Slack, ...). Push protection: block commit chứa secret known pattern.
6.4. Khi secret leak
- Rotate ngay — invalidate secret, generate new.
- Audit log — check secret used từ đâu, có suspicious?
- Rewrite history (optional, drastic) — BFG Repo-Cleaner / git-filter-repo. Lưu ý: break clones, force push.
- Postmortem.
- Improve process — add pre-commit hook, training.
7. Container Security
7.1. Image scanning — Trivy
trivy image myapp:1.0
trivy image --severity HIGH,CRITICAL myapp:1.0
# Fail CI if critical
trivy image --exit-code 1 --severity CRITICAL myapp:1.0
# Scan k8s YAML
trivy config k8s/
# Scan Dockerfile
trivy config Dockerfile
# Multi-purpose
trivy fs .
trivy repo https://github.com/...
7.2. Reduce attack surface
- Minimal base image — distroless, alpine.
- Multi-stage build — runtime image không có compiler/devDep.
- Non-root user.
- Read-only filesystem.
- Drop all capabilities, add chỉ cần thiết.
- No SSH trong container.
- Pin base image SHA.
7.3. Pod Security Standards (K8s)
K8s 1.25+ deprecate PodSecurityPolicy, replace bằng PSS:
| Profile | Restrictions |
|---|---|
| Privileged | None — toàn quyền |
| Baseline | Prevent known privilege escalations |
| Restricted | Hardened: non-root, drop caps, seccomp, no host network/PID |
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
7.4. Admission controllers
- Pod Security Admission — built-in.
- OPA Gatekeeper — generic policy.
- Kyverno — Kubernetes-native, no Rego.
7.5. Network policy zero-trust
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: production
spec:
podSelector: {}
policyTypes: [Ingress, Egress]
# Sau đó allow specific
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow
spec:
podSelector:
matchLabels: { app: api }
ingress:
- from:
- podSelector:
matchLabels: { app: web }
ports:
- port: 3000
egress:
- to:
- podSelector:
matchLabels: { app: db }
ports:
- port: 5432
- to:
- namespaceSelector:
matchLabels: { name: kube-system }
ports:
- port: 53 # DNS
Cần CNI hỗ trợ (Calico, Cilium). Default K8s allow-all.
8. Supply Chain Security
Sau SolarWinds, Log4Shell, XZ — supply chain trở thành priority security.
8.1. SBOM — Software Bill of Materials
SBOM = list mọi component (lib, version, license) trong artifact. Format: SPDX hoặc CycloneDX.
# Generate SBOM với syft
syft myapp:1.0 -o cyclonedx-json > sbom.json
syft myapp:1.0 -o spdx-json > sbom.spdx.json
# Check SBOM cho known vuln với grype
grype sbom:./sbom.json
US Executive Order 14028 (2021) require SBOM cho federal contracts.
8.2. Sign artifact — Sigstore / cosign
# Keyless signing với OIDC (no key management!)
cosign sign --yes ghcr.io/me/myapp:1.0
# Verify
cosign verify --certificate-identity me@example.com \
--certificate-oidc-issuer https://github.com/login/oauth \
ghcr.io/me/myapp:1.0
Sigstore (CNCF) là free signing infrastructure. Identity-based, không cần PKI.
8.3. Verify trong K8s
# Kyverno policy verify image signature
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: verify-image-signature
spec:
validationFailureAction: Enforce
background: false
rules:
- name: verify-signature
match:
any:
- resources:
kinds: [Pod]
verifyImages:
- imageReferences:
- "ghcr.io/me/*"
attestors:
- entries:
- keyless:
subject: "https://github.com/me/myrepo/.github/workflows/*"
issuer: "https://token.actions.githubusercontent.com"
8.4. SLSA — Supply chain Levels for Software Artifacts
Framework Google define 4 levels supply chain integrity:
| Level | Requirements |
|---|---|
| SLSA 1 | Build is automated + provenance documented |
| SLSA 2 | + Version control + hosted build (GHA, etc.) |
| SLSA 3 | + Build platform integrity, hardened build |
| SLSA 4 | + 2-person review, hermetic build |
8.5. Provenance attestation
# Sigstore attest provenance
cosign attest --predicate provenance.json --type slsaprovenance \
ghcr.io/me/myapp:1.0
Provenance = "ai build, từ commit nào, với build steps gì". Verify trước deploy.
9. Policy as Code
9.1. OPA (Open Policy Agent)
Generic policy engine. Policy viết bằng Rego:
package kubernetes.admission
deny[msg] {
input.request.kind.kind == "Pod"
input.request.object.spec.containers[_].securityContext.runAsUser == 0
msg := "Pod must not run as root"
}
deny[msg] {
input.request.kind.kind == "Pod"
not input.request.object.spec.containers[_].resources.limits.memory
msg := "Memory limit required"
}
deny[msg] {
input.request.kind.kind == "Service"
input.request.object.spec.type == "LoadBalancer"
not contains(input.request.object.metadata.annotations["service.beta.kubernetes.io/aws-load-balancer-internal"], "true")
msg := "Public LoadBalancer not allowed; use ingress"
}
Use cases: K8s admission, Terraform validation, API authorization.
9.2. Conftest — OPA cho IaC
# Test Terraform plan
terraform plan -out=plan.binary
terraform show -json plan.binary > plan.json
conftest test plan.json --policy ./policy/
# Test K8s YAML
conftest test deployment.yaml --policy ./policy/
9.3. Kyverno — K8s native
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-resources
spec:
validationFailureAction: Enforce
rules:
- name: validate-resources
match:
any:
- resources:
kinds: [Pod]
validate:
message: "Both resources requests and limits required"
pattern:
spec:
containers:
- name: "*"
resources:
limits:
memory: "?*"
cpu: "?*"
requests:
memory: "?*"
cpu: "?*"
Kyverno đơn giản hơn OPA (YAML pattern matching, no Rego). Đa số K8s policy use cases.
10. Runtime Security
SAST/DAST/SCA detect static. Runtime security detect attack đang xảy ra.
10.1. Falco
Falco (CNCF) detect anomalous syscall/file/network activity:
# Falco rule
- rule: Suspicious shell in container
desc: Detect shell spawned in production container
condition: spawned_process and container and shell_procs
output: Shell spawned (user=%user.name container=%container.id command=%proc.cmdline)
priority: WARNING
- rule: Write to /etc
desc: Process writing to /etc directory
condition: open_write and fd.directory startswith /etc
output: Write to /etc (user=%user.name path=%fd.name)
priority: ERROR
Sử dụng eBPF kernel hooks — minimal overhead, see syscalls.
10.2. Cilium Tetragon
Modern alternative — eBPF-based, Kubernetes-native. Detect + enforce (block syscall).
10.3. AWS GuardDuty / GCP Security Command Center / Azure Defender
Cloud-native threat detection: VPC flow log analysis, malicious IP detection, instance behavior anomaly. Easy enable, recommend cho cloud workload.
10.4. RASP — Runtime Application Self-Protection
Agent trong app process, detect + block attack tại app layer (SQL injection ngay trước query, ...). Tools: Contrast, Imperva.
11. Compliance
11.1. Common standards
| Standard | Scope | Audited by |
|---|---|---|
| SOC 2 | SaaS controls (security, availability) | External CPA firm yearly |
| ISO 27001 | Info security mgmt system | Accredited body |
| PCI-DSS | Payment card data | QSA + Self-assessment |
| HIPAA | US health data | HHS audit on demand |
| GDPR | EU personal data | EU DPA |
| FedRAMP | US federal cloud | 3PAO |
| CIS Benchmarks | Best practice baselines | Self-audit |
11.2. Compliance as Code
Audit chỉ ngắn gọn nếu đã có evidence trong toolchain:
- Access control → IAM policy + audit log.
- Encryption → Terraform code force encryption.
- Backup → automated với retention.
- Vulnerability mgmt → CVE scan trong CI.
- Incident response → runbook + postmortem.
- Change management → Git PR + approval.
Tools auto-collect evidence: Drata, Vanta, SecureFrame.
11.3. Audit log
Cloud-native audit:
- AWS CloudTrail — all API calls.
- GCP Cloud Audit Logs.
- Azure Activity Log.
- K8s audit policy → log API server requests.
- App-level audit (login, action) → structured log.
Retention: 90 days hot, 1+ year cold (per compliance requirement).
12. Threat Modeling
12.1. STRIDE
Microsoft STRIDE — categorize threats:
| Letter | Threat | Mitigation |
|---|---|---|
| S | Spoofing identity | Auth, MFA |
| T | Tampering data | Integrity check, signing |
| R | Repudiation | Audit log |
| I | Information disclosure | Encryption, access control |
| D | DoS | Rate limit, autoscale, CDN |
| E | Elevation of privilege | Least privilege, sandboxing |
12.2. Process
- Decompose application — components, data flow, trust boundary.
- Identify threats — apply STRIDE per component.
- Rate threats — DREAD (Damage, Reproducibility, Exploitability, Affected, Discoverability).
- Mitigate — design controls.
- Document + review.
12.3. Khi nào threat model?
- New service / major architecture change.
- Handle sensitive data (PII, payment).
- External-facing API.
- Compliance requirement.
Tools: Microsoft Threat Modeling Tool, OWASP Threat Dragon, Threagile.
13. Bài tập
- Pre-commit security: setup gitleaks + detect-secrets trong pre-commit. Cố tình commit AWS key, verify hook block.
- SAST trong CI: add Semgrep với p/owasp-top-ten. Fix top 5 findings.
- SCA + Dependabot: setup Dependabot config cho npm + docker + terraform. Review PRs hàng tuần.
- Container scan: Trivy scan image. Identify CRITICAL CVE. Fix bằng upgrade base image.
- SBOM: generate SBOM với syft cho 1 image. Inspect format CycloneDX. Run grype trên SBOM.
- cosign sign + verify: sign image với cosign keyless (OIDC GitHub). Verify identity. Setup Kyverno policy require signature.
- Pod Security Standards: label namespace với
pod-security.kubernetes.io/enforce: restricted. Try deploy Pod chạy as root, verify rejected. - Network Policy: implement zero-trust trong namespace: default-deny + explicit allow. Verify Pod không reach Pod khác trừ khi allow.
- OPA / Kyverno: viết policy require resource.limits cho mọi Pod. Test với deployment thiếu limit, verify rejected.
- Falco: cài Falco trên cluster. Trigger rule "Suspicious shell" bằng
kubectl exec -it pod -- bash. Verify alert. - Threat model: cho 1 service bạn maintain. Decompose. Apply STRIDE. List 5 threats + mitigations.
- Compliance audit: cho organization, audit theo CIS Benchmarks K8s. Identify top 5 gaps. Plan remediation.
- Secret rotation drill: simulate AWS key leak. Practice: rotate key, audit CloudTrail, postmortem.
- SLSA assessment: đánh giá build pipeline hiện tại theo SLSA levels. Plan upgrade từ L1 lên L3.
14. Quiz
Quiz cuối Chương 12
"Shift-left security" có nghĩa:
SAST vs DAST khác nhau:
SCA (Software Composition Analysis) tập trung vào:
Khi secret leak vào public Git:
SBOM (Software Bill of Materials) là:
Sigstore / cosign keyless signing dùng:
SLSA (Supply chain Levels for Software Artifacts) định nghĩa:
Pod Security Standard "Restricted" profile yêu cầu:
runAsNonRoot: true, capabilities.drop: [ALL], no hostNetwork/hostPID/hostIPC, readOnlyRootFilesystem: true (recommended), allowed seccomp profiles. Apply qua namespace label pod-security.kubernetes.io/enforce: restricted. K8s 1.25+ replace deprecated PodSecurityPolicy.OPA (Open Policy Agent) / Kyverno dùng để:
Threat modeling STRIDE 6 categories:
Hoàn thành Chương 12. Tiếp theo: Chương 13 — Performance & Scaling →