Appearance
Amazon S3 — Security (Bản gốc slide / Original slide)
1. Amazon S3 — Object Encryption
Có 4 phương pháp để mã hoá object trong S3:
| Phương pháp | Ai quản lý key | Ghi chú |
|---|---|---|
| SSE-S3 | AWS quản lý hoàn toàn | Mặc định bật cho bucket/object mới; mã hoá AES-256; header x-amz-server-side-encryption: AES256 |
| SSE-KMS | AWS KMS (bạn kiểm soát key) | Có audit trail qua CloudTrail; header x-amz-server-side-encryption: aws:kms; chịu giới hạn quota của KMS |
| SSE-C | Khách hàng tự cung cấp key | S3 không lưu key bạn cung cấp; bắt buộc dùng HTTPS; key phải gửi kèm mỗi HTTP request |
| Client-Side Encryption | Khách hàng tự mã hoá/giải mã | Dùng thư viện như Amazon S3 Client-Side Encryption Library; client tự mã hoá trước khi gửi và tự giải mã sau khi tải về |
⚠️ Cần hiểu rõ phương pháp nào dùng cho tình huống nào — đây là câu hỏi thi rất phổ biến.
Giới hạn của SSE-KMS:
- Upload gọi KMS API
GenerateDataKey; download gọi KMS APIDecrypt - Cả hai đều tính vào KMS quota mỗi giây (5,500 / 10,000 / 30,000 request/giây tuỳ region)
- Có thể xin tăng quota qua Service Quotas Console
There are 4 methods to encrypt objects in S3:
| Method | Who manages the key | Notes |
|---|---|---|
| SSE-S3 | Fully managed by AWS | Enabled by default for new buckets/objects; AES-256 encryption; header x-amz-server-side-encryption: AES256 |
| SSE-KMS | AWS KMS (you control the key) | Audit trail via CloudTrail; header x-amz-server-side-encryption: aws:kms; subject to KMS quota limits |
| SSE-C | Customer-provided key | S3 does NOT store the key you provide; HTTPS mandatory; key must be provided in every HTTP request |
| Client-Side Encryption | Customer encrypts/decrypts | Use libraries like the Amazon S3 Client-Side Encryption Library; client encrypts before sending and decrypts after retrieving |
⚠️ Know which method fits which situation — a very common exam question.
SSE-KMS Limitation:
- Upload calls the KMS API
GenerateDataKey; download calls the KMS APIDecrypt - Both count towards the KMS quota per second (5,500 / 10,000 / 30,000 req/s depending on region)
- You can request a quota increase via the Service Quotas Console
2. S3 — Encryption in Transit (SSL/TLS)
- Mã hoá khi truyền (in-flight) còn gọi là SSL/TLS
- S3 expose 2 endpoint:
- HTTP Endpoint — không mã hoá
- HTTPS Endpoint — mã hoá khi truyền
- Khuyến nghị dùng HTTPS; bắt buộc với SSE-C
- Hầu hết client mặc định đã dùng HTTPS endpoint
Bắt buộc mã hoá khi truyền:
- Dùng điều kiện
aws:SecureTransporttrong Bucket Policy để từ chối mọi request HTTP (không mã hoá), chỉ cho phép HTTPS
Default Encryption vs. Bucket Policy:
- SSE-S3 được tự động áp dụng cho object mới trong bucket
- Có thể "ép mã hoá" (force encryption) bằng bucket policy, từ chối mọi API call PUT object nếu thiếu header mã hoá (SSE-KMS hoặc SSE-C)
- Lưu ý: Bucket Policy được đánh giá TRƯỚC "Default Encryption"
- Encryption in flight is also called SSL/TLS
- S3 exposes 2 endpoints:
- HTTP Endpoint — non-encrypted
- HTTPS Endpoint — encryption in flight
- HTTPS is recommended; mandatory for SSE-C
- Most clients use the HTTPS endpoint by default
Force Encryption in Transit:
- Use the
aws:SecureTransportcondition in a Bucket Policy to deny any HTTP request (non-encrypted), allowing only HTTPS
Default Encryption vs. Bucket Policies:
- SSE-S3 encryption is automatically applied to new objects stored in the bucket
- Optionally, "force encryption" using a bucket policy and refuse any PUT API call missing encryption headers (SSE-KMS or SSE-C)
- Note: Bucket Policies are evaluated BEFORE "Default Encryption"
3. CORS là gì? (What is CORS?)
- CORS = Cross-Origin Resource Sharing
- Origin = scheme (protocol) + host (domain) + port — ví dụ
https://www.example.com(port ngầm định 443 cho HTTPS, 80 cho HTTP) - Là cơ chế của trình duyệt (web browser) cho phép request sang origin khác trong khi đang truy cập origin chính
- Cùng origin:
http://example.com/app1&http://example.com/app2 - Khác origin:
http://www.example.com&http://other.example.com - Request sẽ KHÔNG được thực hiện trừ khi origin kia cho phép, thông qua CORS Header (ví dụ
Access-Control-Allow-Origin)
S3 & CORS:
- Nếu client gửi cross-origin request tới S3 bucket, phải bật đúng CORS header trên bucket
- Có thể cho phép một origin cụ thể hoặc
*(mọi origin) - Đây là câu hỏi thi rất hay gặp — ví dụ: một static website ở bucket A load ảnh từ bucket B (khác origin) → bucket B cần cấu hình CORS cho phép origin của bucket A
- CORS = Cross-Origin Resource Sharing
- Origin = scheme (protocol) + host (domain) + port — e.g.,
https://www.example.com(implied port 443 for HTTPS, 80 for HTTP) - A web browser mechanism allowing requests to other origins while visiting the main origin
- Same origin:
http://example.com/app1&http://example.com/app2 - Different origins:
http://www.example.com&http://other.example.com - Requests won't be fulfilled unless the other origin allows them, using CORS Headers (e.g.,
Access-Control-Allow-Origin)
S3 & CORS:
- If a client makes a cross-origin request on our S3 bucket, we need to enable the correct CORS headers
- Popular exam question
- You can allow a specific origin or
*(all origins) - Common example: a static website in bucket A loads images from bucket B (a different origin) → bucket B needs CORS configured to allow bucket A's origin
4. Amazon S3 — MFA Delete
- MFA (Multi-Factor Authentication) — buộc user tạo mã trên thiết bị (điện thoại hoặc hardware) trước khi thực hiện thao tác quan trọng trên S3
- Cần MFA để:
- Xoá vĩnh viễn một object version
- Tạm dừng (suspend) Versioning trên bucket
- KHÔNG cần MFA để:
- Bật Versioning
- Liệt kê (list) các version đã xoá
- Để dùng MFA Delete, Versioning phải được bật trên bucket
- Chỉ chủ bucket (root account) mới bật/tắt được MFA Delete
- MFA (Multi-Factor Authentication) — force users to generate a code on a device (mobile or hardware) before doing important S3 operations
- MFA will be required to:
- Permanently delete an object version
- Suspend Versioning on the bucket
- MFA won't be required to:
- Enable Versioning
- List deleted versions
- To use MFA Delete, Versioning must be enabled on the bucket
- Only the bucket owner (root account) can enable/disable MFA Delete
5. S3 Access Logs
- Vì mục đích audit, có thể muốn log mọi truy cập tới S3 bucket
- Mọi request tới S3 (từ account nào, được phép hay bị từ chối) đều được log vào một S3 bucket khác
- Dữ liệu log có thể phân tích bằng công cụ data analysis
- Logging bucket phải nằm cùng AWS region với bucket được theo dõi
⚠️ Cảnh báo quan trọng:
- KHÔNG đặt logging bucket trùng với bucket đang được theo dõi
- Sẽ tạo ra vòng lặp logging (logging loop) — bucket sẽ tăng trưởng theo cấp số nhân (log về việc ghi log, cứ thế lặp lại)
- For audit purposes, you may want to log all access to S3 buckets
- Any request made to S3 (from any account, authorized or denied) will be logged into another S3 bucket
- That data can be analyzed using data analysis tools
- The target logging bucket must be in the same AWS region
⚠️ Important warning:
- Do NOT set your logging bucket to be the monitored bucket
- It will create a logging loop, and your bucket will grow exponentially
6. Amazon S3 — Pre-Signed URLs
- Tạo pre-signed URL bằng S3 Console, AWS CLI, hoặc SDK
- Thời hạn URL:
- S3 Console — từ 1 phút tới 720 phút (12 giờ)
- AWS CLI — cấu hình bằng tham số
--expires-in(đơn vị giây, mặc định 3600 giây, tối đa 604800 giây ≈ 168 giờ)
- User được cấp pre-signed URL sẽ thừa hưởng quyền của người tạo URL (cho GET/PUT)
- Ví dụ:
- Chỉ cho phép user đã đăng nhập tải video premium từ bucket
- Cho phép danh sách user thay đổi liên tục tải file bằng cách sinh URL động
- Cho phép user tạm thời upload file vào một vị trí cụ thể trong bucket
- Generate pre-signed URLs using the S3 Console, AWS CLI, or SDK
- URL Expiration:
- S3 Console — 1 min up to 720 mins (12 hours)
- AWS CLI — configure with the
--expires-inparameter in seconds (default 3600 secs, max 604800 secs ~ 168 hours)
- Users given a pre-signed URL inherit the permissions of the user who generated it (for GET/PUT)
- Examples:
- Allow only logged-in users to download a premium video from your bucket
- Allow an ever-changing list of users to download files by generating URLs dynamically
- Allow a user to temporarily upload a file to a precise location in your bucket
7. S3 Glacier Vault Lock
- Áp dụng mô hình WORM (Write Once Read Many)
- Tạo một Vault Lock Policy
- Khoá (lock) policy để không thể chỉnh sửa trong tương lai — không thể thay đổi hoặc xoá
- Hữu ích cho compliance và data retention
- Adopt a WORM (Write Once Read Many) model
- Create a Vault Lock Policy
- Lock the policy for future edits — it can no longer be changed or deleted
- Helpful for compliance and data retention
8. S3 Object Lock
(Yêu cầu bật Versioning)
- Áp dụng mô hình WORM (Write Once Read Many)
- Chặn việc xoá một object version trong một khoảng thời gian xác định
- Retention mode — Compliance:
- Object version không thể bị ghi đè hoặc xoá bởi bất kỳ ai, kể cả root user
- Retention mode không thể thay đổi, và retention period không thể rút ngắn
- Retention mode — Governance:
- Hầu hết user không thể ghi đè/xoá object version hoặc thay đổi lock setting
- Một số user có quyền đặc biệt để thay đổi retention hoặc xoá object
- Retention Period: bảo vệ object trong một khoảng thời gian cố định, có thể gia hạn thêm
- Legal Hold:
- Bảo vệ object vô thời hạn, độc lập với retention period
- Có thể gắn/gỡ tự do bằng quyền IAM
s3:PutObjectLegalHold
(Versioning must be enabled)
- Adopt a WORM (Write Once Read Many) model
- Block an object version deletion for a specified amount of time
- Retention mode – Compliance:
- Object versions can't be overwritten or deleted by any user, including the root user
- Retention modes can't be changed, and retention periods can't be shortened
- Retention mode – Governance:
- Most users can't overwrite or delete an object version or alter its lock settings
- Some users have special permissions to change the retention or delete the object
- Retention Period: protects the object for a fixed period, can be extended
- Legal Hold:
- Protects the object indefinitely, independent from the retention period
- Can be freely placed and removed using the
s3:PutObjectLegalHoldIAM permission
9. S3 — Access Points
- Access Point giúp đơn giản hoá việc quản lý bảo mật cho S3 bucket
- Mỗi Access Point có:
- DNS name riêng (Internet Origin hoặc VPC Origin)
- Access point policy (tương tự bucket policy) — quản lý bảo mật ở quy mô lớn
- Ví dụ trên sơ đồ:
- Finance Access Point — cấp quyền đọc/ghi cho prefix
/finance - Sales Access Point — cấp quyền đọc/ghi cho prefix
/sales - Analytics Access Point — cấp quyền chỉ đọc cho toàn bộ bucket
- Finance Access Point — cấp quyền đọc/ghi cho prefix
Access Point — VPC Origin:
- Có thể giới hạn Access Point chỉ truy cập được từ bên trong VPC
- Phải tạo VPC Endpoint (Gateway hoặc Interface Endpoint) để truy cập Access Point
- VPC Endpoint Policy phải cho phép truy cập tới bucket đích và Access Point tương ứng
- Access Points simplify security management for S3 Buckets
- Each Access Point has:
- Its own DNS name (Internet Origin or VPC Origin)
- An access point policy (similar to a bucket policy) — manage security at scale
- Example in the diagram:
- Finance Access Point — grants R/W to the
/financeprefix - Sales Access Point — grants R/W to the
/salesprefix - Analytics Access Point — grants read-only to the entire bucket
- Finance Access Point — grants R/W to the
Access Points — VPC Origin:
- You can define the access point to be accessible only from within the VPC
- Must create a VPC Endpoint (Gateway or Interface Endpoint) to access the Access Point
- The VPC Endpoint Policy must allow access to the target bucket and Access Point
10. S3 Object Lambda
- Dùng AWS Lambda function để biến đổi object trước khi trả về cho ứng dụng gọi
- Chỉ cần một S3 bucket duy nhất — bên trên tạo S3 Access Point và các S3 Object Lambda Access Point
- Use cases:
- Ẩn (redact) thông tin định danh cá nhân (PII) cho môi trường analytics hoặc non-production
- Chuyển đổi định dạng dữ liệu, ví dụ XML → JSON
- Resize / watermark ảnh ngay khi trả về, dựa theo thông tin của người gọi (ví dụ user nào đang request)
- Use AWS Lambda functions to transform the object before it's returned to the calling application
- Only one S3 bucket is needed — on top of it, create an S3 Access Point and S3 Object Lambda Access Points
- Use cases:
- Redacting personally identifiable information (PII) for analytics or non-production environments
- Converting data formats, e.g., XML to JSON
- Resizing and watermarking images on the fly, using caller-specific details (e.g., the user who requested the object)