Không commit secrets vào code (kể cả .env); lưu trong native secret store của CI (GitHub Secrets, GitLab Variables) hoặc vault tập trung (HashiCorp Vault, AWS Secrets Manager); rotate định kỳ; dùng OIDC để cấp short-lived token cho cloud thay vì long-lived access key. Best practices:
- Native CI secrets: GitHub
Settings → Secrets and variables → Actions; truy cập qua${{ secrets.MY_KEY }}— auto-masked trong logs. - Environment secrets: secrets gắn với environment (
production,staging) → cần approval reviewer trước khi job dùng được. - OIDC (recommended cho cloud): GitHub Actions issue OIDC token, AWS/GCP/Azure trust GitHub OIDC → cấp short-lived credentials (15min) — không cần lưu AWS access key static.
- Secret scanning: GitHub Secret Scanning + push protection (free cho public repo, paid cho private) chặn commit chứa AWS key/Stripe key trước khi push tới remote.
- gitleaks trong pre-commit hook: scan locally.
- HashiCorp Vault / AWS Secrets Manager: cho secrets nhạy cảm hơn (DB password production), CI fetch tại runtime với short TTL. Rotation: API keys quan trọng rotate 90 ngày; nếu nghi ngờ leak → revoke ngay, rotate, audit log để xác định blast radius. Sai lầm phổ biến: log toàn bộ env vào CI logs (
set -xlộ secret); commit.envrồi delete (vẫn trong Git history — phải BFG repo-cleaner / git filter-repo + force push + rotate); echo secret vào artifact upload công khai