Lesson 2Security

S3 Security and Lifecycle

S3 security ต้องคิดทั้ง identity policy, bucket policy, Block Public Access, Object Ownership, encryption, access logging และ lifecycle ที่ควบคุม cost/retention

Private default Block Public Access และ ACLs disabled ควบคุม access ด้วย IAM/bucket policy เป็นหลัก ไม่เปิด public เพื่อแก้ปัญหาระยะสั้น
Encryption SSE-S3 ง่าย, SSE-KMS คุม key/audit ได้มากกว่า ถ้าใช้ KMS ต้องให้ role/application มี permission กับ key ด้วย
Retention Lifecycle ต้องจับ prefix/version ให้ถูก Transition/expire ช่วยลด cost แต่ rule ผิดอาจลบข้อมูลที่ยังต้องใช้

Block Public Access และ Object Ownership

S3 buckets และ objects เป็น private โดย default และ AWS แนะนำให้เปิด Block Public Access ไว้ เว้นแต่มี use case ชัดเจนที่ต้อง public Object Ownership ช่วยจัดการ ownership และ ACL โดย bucket ใหม่มักใช้ ACLs disabled เป็น default เพื่อให้ควบคุม access ผ่าน policy แทน ACL

Bucket Policy

Bucket Policy เป็น resource-based IAM policy ที่ attach กับ bucket ใช้ allow/deny access ตาม Principal, Action, Resource และ Condition เช่นบังคับ TLS ด้วย aws:SecureTransport หรือจำกัด access ผ่าน VPC Endpoint

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyInsecureTransport",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::example-bucket",
        "arn:aws:s3:::example-bucket/*"
      ],
      "Condition": { "Bool": { "aws:SecureTransport": "false" } }
    }
  ]
}

Server-side Encryption

  • SSE-S3: Amazon S3 จัดการ encryption keys ให้ เหมาะกับ default encryption ทั่วไป
  • SSE-KMS: ใช้ AWS KMS keys เหมาะเมื่อต้องการ key policy, audit และ control มากขึ้น
  • SSE-C: ลูกค้าจัดการ key เอง ใช้น้อยกว่าและต้องระวัง operational burden

Lifecycle Policy

Lifecycle Policy ใช้ transition objects ไป Storage Class อื่น หรือ expire objects เมื่อครบอายุ เช่น ย้าย log ไป Glacier หลัง 90 วัน และลบ noncurrent versions หลัง 365 วัน เพื่อลดค่าใช้จ่ายของ Versioning

Pre-signed URL

Pre-signed URL ให้ temporary access ไปยัง object โดยใช้ permission ของผู้สร้าง URL และมี expiration เหมาะกับให้ download/upload ชั่วคราวโดยไม่ต้องเปิด bucket public

Common mistakes

  • ปิด Block Public Access เพื่อให้ upload/download ง่าย แล้วลืมเปิดกลับ
  • ใช้ ACL ผสม policy โดยไม่จำเป็น ทำให้ debug access ยาก
  • เปิด SSE-KMS แต่ role/application ไม่มี KMS permission
  • ตั้ง Lifecycle expire ผิด prefix แล้วลบข้อมูลที่ยังต้องใช้
Before next lesson
  1. อธิบาย private bucket baseline ได้: Block Public Access, ACL disabled, encryption, least privilege policy
  2. เลือก SSE-S3 หรือ SSE-KMS โดยรู้ permission/cost tradeoff ได้
  3. ใช้ pre-signed URL สำหรับ temporary sharing โดยไม่เปิด bucket public ได้

Review questions

  1. Block Public Access ควรปิดเมื่อไร?
  2. SSE-S3 ต่างจาก SSE-KMS อย่างไร?
  3. Pre-signed URL ดีกว่าการเปิด bucket public อย่างไร?