Tuần 6 - Ngày 2: KMS và Secrets Manager
Mục tiêu học tập
- Hiểu KMS: keys, encryption, envelope encryption
- Phân biệt CMK types: AWS-managed, Customer-managed, AWS-owned
- Nắm Secrets Manager vs Parameter Store
- Áp dụng rotation, cross-account sharing
1. AWS KMS Overview
AWS KMS (Key Management Service) = managed service tạo và quản lý cryptographic keys.
Đặc điểm
- Multi-tenant AES-256 encryption keys
- Tích hợp với 100+ AWS services
- FIPS 140-2 Level 2 (HSM level 3 với CloudHSM)
- Audit logs trong CloudTrail
- Regional (key chỉ dùng được trong region tạo)
Pricing
- $1/key/month (Customer-managed CMK)
- $0.03 per 10,000 API calls
- AWS-managed CMK: free
- Free tier: 20,000 API calls/month
2. KMS Key Types
Customer-Managed Key (CMK)
- Bạn tạo, manage, rotate
- Full control over key policy, alias, deletion
- Pricing: $1/month + API calls
- Best for: granular control, audit, compliance
AWS-Managed Key
- AWS tạo + manage cho service (e.g.,
aws/s3,aws/rds) - Free
- View key trong console (read-only)
- Default cho hầu hết AWS services
- Use case: Default encryption, không cần custom control
AWS-Owned Key
- AWS tạo, không hiển thị trong account
- Used internally by services (DynamoDB encryption by default)
- Free
- Không thể audit, không thể disable
Symmetric vs Asymmetric
| Symmetric (AES-256) | Asymmetric (RSA/ECC) | |
|---|---|---|
| Use case | Encrypt/decrypt data | Sign/verify, encrypt with public key |
| Encryption | Same key for both | Public encrypt, Private decrypt |
| Operations | Encrypt, Decrypt, GenerateDataKey | Sign, Verify, Encrypt, Decrypt |
| Most common | Default cho hầu hết use cases | Code signing, JWT, certificates |
3. Envelope Encryption
Vấn đề
- AES encryption fast cho data nhỏ
- Network call to KMS slow cho data lớn (1 KB limit per call)
Giải pháp: Envelope Encryption
- App calls KMS
GenerateDataKey→ returns:- Plaintext data key (for encryption)
- Encrypted data key (wrapped by CMK)
- App encrypts data locally with plaintext data key (fast)
- App stores encrypted data + encrypted data key
- App discards plaintext data key from memory
Decryption
- App reads encrypted data + encrypted data key
- App calls KMS
Decrypton encrypted data key → plaintext data key - App decrypts data locally
Benefit
- Limit KMS API calls (only encrypt/decrypt data key)
- Encrypt large data fast (locally)
- Most AWS services (S3 SSE-KMS, EBS) use envelope encryption
4. KMS Key Rotation
Automatic Rotation (Customer-managed CMK)
- Enable trong KMS console
- Rotation period: 90 ngày → 7 năm, configurable (default 1 năm) — update April 2024
- Old key version kept (decrypt old ciphertext)
- New key version used for new encryption
- Free
- AWS-managed CMK: rotated every 1 year automatically (KHÔNG configurable)
Manual Rotation
- Create new CMK
- Update alias to point to new key
- Old key stays for decrypting old data
5. KMS Multi-Region Keys
Định nghĩa
Multi-Region Keys = create replica of CMK in another Region, same key material.
Use case
- Cross-region applications (decrypt in any region)
- DR (failover to another region)
- Global Tables (DynamoDB), CRR (S3)
vs Single-Region
- Single-region: key bound to 1 region (must re-encrypt to use in another region)
- Multi-region: key works seamlessly across regions
6. KMS Key Policy
Định nghĩa
Key Policy = resource-based policy attached to KMS key (control WHO can use the key).
Default policy
- Allows root user full access
- Allows IAM policies attached to users/roles to grant access
Custom policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableAdminAccess",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "AllowUseOfKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222222222222:role/AppRole" },
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey"
],
"Resource": "*"
}
]
}
7. KMS Key Deletion
Process
- Initiate "Schedule deletion"
- Waiting period: 7-30 days (default 30)
- During waiting period: key disabled (cannot encrypt/decrypt)
- After waiting period: permanently deleted
- Cancel deletion during waiting period possible
Best practice
- Long waiting period (30 days) for safety
- Disable key first, see if anything breaks
- KHÔNG delete key đã encrypt production data (lose access permanently)
8. KMS Grants
Định nghĩa
Grant = temporary permission to use KMS key, programmatically created.
Use case
- AWS services on behalf of user (EBS create snapshot)
- Lambda function temporarily access KMS
- More granular than key policy
Properties
- Created/revoked via API
- Specifies operations, resources, conditions
- Up to 50,000 grants per key
9. AWS Secrets Manager
Định nghĩa
Secrets Manager = managed service store, retrieve, rotate secrets (DB passwords, API keys).
Đặc điểm
- Automatic rotation (built-in for RDS, custom Lambda for others)
- KMS encryption (cần CMK)
- Audit logs in CloudTrail
- Resource-based policy (cross-account access)
- Pricing: $0.40/secret/month + $0.05/10K API calls
Native rotation supported
- RDS: MySQL, PostgreSQL, MariaDB, Oracle, SQL Server
- Aurora
- DocumentDB, Redshift
- Custom Lambda function for other secrets
Rotation flow (RDS example)
- Lambda function rotates DB password
- Lambda updates secret in Secrets Manager
- Lambda updates DB user password
- App fetches new password from Secrets Manager
Use case
- Application secrets (API keys, OAuth tokens)
- Database credentials (with automated rotation)
- 3rd-party service credentials
10. Systems Manager Parameter Store
Định nghĩa
Parameter Store = store configuration data and secrets, integrate với SSM.
Đặc điểm
- Free (Standard tier, < 4 KB value, < 10K parameters)
- Advanced tier: $0.05/parameter/month, > 4 KB values
- Encryption via KMS (SecureString type)
- Hierarchical structure (
/app/dev/db/password) - No built-in rotation (manual or custom)
Types
- String: plain text
- StringList: comma-separated
- SecureString: encrypted với KMS
Use case
- App configuration (non-secret)
- Feature flags
- Database connection strings
- API endpoints
11. Secrets Manager vs Parameter Store
| Secrets Manager | Parameter Store | |
|---|---|---|
| Cost | $0.40/secret/month | Free (Standard), $0.05/param Advanced |
| Rotation | Built-in (RDS automatic) | Manual / custom Lambda |
| Cross-account | Yes (resource policy) | Via shared KMS key |
| Encryption | Always (KMS) | Optional (SecureString) |
| Max size | 64 KB | 4 KB (Standard), 8 KB (Advanced) |
| Versioning | Yes (5 versions kept) | Yes (Advanced) |
| Use case | Secrets needing rotation | Config + simple secrets |
Decision
- Secrets needing rotation: Secrets Manager
- Configuration data: Parameter Store (cheaper)
- Simple secrets, no rotation: Parameter Store SecureString (free)
12. AWS CloudHSM
Định nghĩa
CloudHSM = dedicated Hardware Security Module (HSM) in cloud, single-tenant.
So với KMS
| KMS | CloudHSM | |
|---|---|---|
| Tenancy | Multi-tenant | Single-tenant |
| Compliance | FIPS 140-2 L2 | FIPS 140-2 L3 |
| Control | AWS manages | Customer manages |
| Cost | $1/key/month | $1.45/hour/HSM (~$1000/month) |
| Use case | General encryption | Strict compliance, custom crypto |
Use case CloudHSM
- Regulatory requires single-tenant HSM (some financial, government)
- Custom cryptographic algorithms
- BYOK (Bring Your Own Key) into KMS
- SSL/TLS offload for high volume
13. Common Patterns
Pattern 1: S3 with SSE-KMS + Bucket Key
- Encrypt objects with SSE-KMS
- Enable Bucket Key (reduce KMS API calls 99%)
Pattern 2: RDS with Secrets Manager + auto-rotation
- RDS uses Secrets Manager for password
- Rotate every 30 days automatic
- App fetches latest from Secrets Manager
Pattern 3: Application config in Parameter Store
- Lambda reads
/app/prod/api-endpointat cold start - Cache value in memory (avoid repeated calls)
Pattern 4: Cross-account secret sharing
- Resource policy on secret in Account A
- IAM policy in Account B allows GetSecretValue
- Shared via KMS key access
Câu hỏi ôn tập
-
Envelope Encryption giải quyết vấn đề gì?
Xem đáp án
Giải quyết vấn đề performance và data size limits của KMS. KMS chỉ có thể encrypt dữ liệu đến 4 KB trực tiếp. Envelope Encryption: (1) KMS generate Data Encryption Key (DEK), (2) DEK encrypt actual data (locally, nhanh, không giới hạn size), (3) Encrypt DEK bằng KMS CMK và lưu cùng data. Khi decrypt: gọi KMS decrypt DEK, dùng DEK decrypt data. Chỉ DEK đi qua network đến KMS — data lớn xử lý locally.
-
KMS Key auto-rotation rotate mỗi bao lâu?
Xem đáp án
1 năm (365 ngày) cho AWS-managed keys và customer-managed keys (khi bật auto-rotation). Customer-managed keys có thể cấu hình rotation từ 90 ngày đến 2,560 ngày (7 năm). Auto-rotation chỉ rotate key material — Key ID không đổi, nên không cần update references. Data encrypted với key cũ vẫn decrypt được (KMS giữ old key material).
-
KMS Key deletion có waiting period bao lâu?
Xem đáp án
7 đến 30 ngày (configurable). Không thể xóa key ngay lập tức — waiting period để phát hiện và cancel nếu key vẫn đang được dùng. Trong thời gian này key bị disabled (không thể dùng encrypt/decrypt). Nếu cancel trước khi hết waiting period, key hoạt động lại bình thường. Sau khi xóa, data encrypted với key đó không thể decrypt — mất vĩnh viễn.
-
Khác biệt cốt lõi giữa Secrets Manager và Parameter Store?
Xem đáp án
Secrets Manager: tự động rotation của secrets (RDS, Redshift, DocumentDB, custom), có chi phí ($0.40/secret/month + $0.05/10K API calls). Dùng cho database credentials, API keys cần rotation. Parameter Store: lưu configuration và secrets (SecureString = encrypted), không có auto-rotation tích hợp, rẻ hơn (Standard: free; Advanced: $0.05/10K requests). Dùng cho application config, non-rotating secrets. Lambda/ECS tích hợp tốt với cả hai.
-
Khi nào dùng CloudHSM thay vì KMS?
Xem đáp án
Dùng CloudHSM khi: (1) Cần FIPS 140-2 Level 3 compliance (KMS là Level 2), (2) Cần sole tenancy của hardware HSM (không shared với AWS hay customers khác), (3) Cần quản lý key material hoàn toàn — KMS manages key material, bạn không thể export, (4) Compliance regulations yêu cầu HSM riêng (PCI DSS, certain government). CloudHSM đắt hơn (~$1.60/giờ/HSM) và cần quản lý nhiều hơn.
Bài tập thực hành
- Tạo Customer-Managed CMK, enable auto-rotation
- Encrypt S3 bucket với SSE-KMS, observe envelope encryption
- Tạo secret trong Secrets Manager cho RDS password, enable auto-rotation 30 days
- Tạo Parameter Store SecureString cho API key, retrieve via CLI
- Tạo Multi-Region KMS key, replicate sang region khác
Tài liệu tham khảo chính thức
Tiếp theo: ACM và CloudFront Security