API key bị lộ = thảm họa
API key bị lộ có thể dẫn đến: chi phí phát sinh hàng nghìn dollar, dữ liệu bị truy cập trái phép, và service bị lạm dụng. Dưới đây là 5 bước bảo mật API key.
1. Biến môi trường — không hardcode
Text
# ❌ KHÔNG BAO GIỜ
api_key = "izzi-xxx-real-key-here"
# ✅ ĐÚNG CÁCH
import os
api_key = os.environ["IZZI_API_KEY"]2. File .env + .gitignore
Text
# .env (local development)
IZZI_API_KEY=izzi-xxx-your-key
# .gitignore — BẮT BUỘC
.env
.env.local
.env.production3. Secret Manager cho production
Dùng AWS Secrets Manager, GCP Secret Manager, hoặc Doppler:
Text
# AWS Secrets Manager
aws secretsmanager get-secret-value \
--secret-id izzi-api-key \
--query SecretString \
--output text4. Key rotation (xoay vòng key)
- Tạo key mới trên Izzi API Dashboard
- Cập nhật secret manager
- Deploy lại service
- Xóa key cũ
- Lặp lại mỗi 90 ngày
5. Tách key theo môi trường
izzi-dev-xxx— cho developmentizzi-staging-xxx— cho stagingizzi-prod-xxx— cho production
Checklist bảo mật
- ☐
.envnằm trong.gitignore - ☐ Không có API key trong source code
- ☐ Production dùng secret manager
- ☐ Key rotate mỗi 90 ngày
- ☐ Tách key theo môi trường
- ☐ Đã cấu hình usage alert
