Thực hiện Rotating AWS KMS CMK

Rotating CMK là một việc rất quan trọng trong quản lý khóa và là một phương pháp bảo mật tốt nhất. Trong AWS KMS, có nhiều cách khác nhau để rotate các khóa tùy theo cách chúng được tạo.

Rotate khóa CMK được tạo từ AWS Key Material

Bật tính năng tự động rotate Key Material hàng năm với AWS KMS

Với AWS Key Material, bạn có thể bật tính năng tự động rotate Key Material hàng năm với AWS KMS. Dữ liệu cryptographic material cũ cũng sẽ được KMS lưu lại để sử dụng cho việc Giải mã dữ liệu được Mã hóa trước đó.
CMK sau khi rotate vẫn giữ nguyên các thuộc tính như Key ID, Key ARN, Region, Policies, Permision. Do vậy mà bạn không cần phải trỏ lại CMK Alias tới CMK được khởi tạo mới.

  1. Cấp quyền EnableKeyRotation để Rotating AWS KMS CMK
  • Truy cập vào giao diện quản trị dịch vụ IAM.
  • Click Policies.
  • Nhập KMS-Workshop-ImportMaterialPermissions vào ô tìm kiếm, nhấn Enter
  • Click KMS-Workshop-ImportMaterialPermissions Rotating CMK
  1. Tại trang Summary
  • Click Edit policy Rotating CMK
  1. Tại trang Edit KMS-Workshop-ImportMaterialPermissions
  • Click KMS
  • Click Actions
  • Nhập EnableKeyRotation vào ô tìm kiếm
  • Chọn EnableKeyRotation
  • Click Review policy Rotating CMK
  1. Tại trang Review policy
  • Click Save changes Rotating CMK
  1. Chạy lệnh dưới đây để tự động Rotating AWS KMS CMK
aws kms enable-key-rotation --key-id <key ID of FirstCMK>

Rotating CMK

Một cách khác để Rotating CMK đó là thực hiện tạo CMK Key mới hoàn toàn, rồi cập nhật lại Alias cho CMK.
  1. Cấp quyền UpdateAlias để cập nhật lại Alias cho CMK.Cấp quyền DeleteImportedKeyMaterial, DisableKey, EnableKeyScheduleKeyDeletion để sử dụng trong phần tiếp theo.
  1. Tại trang Create policy
  • Click Service.
  • Nhập kms vào ô tìm kiếm.
  • Click KMS Rotating CMK
  1. Trong phần Actions
  • Thêm quyền UpdateAlias
    • Nhập UpdateAlias vào ô tìm kiếm
    • Chọn UpdateAlias Rotating CMK
  • Làm tương tự với các quyền DeleteImportedKeyMaterial, DisableKey, EnableKeyScheduleKeyDeletion
  1. Trong phần Resources
  • Click Resources
  • Chọn Specific
  • Tại mục alias Chọn Any in this account
  • Tại mục key Chọn Any in this account
  • Click Next:Tags Rotating CMK
  1. Tại trang Add tags
  • Click Next:Review Rotating CMK
  1. Tại trang Review policy
  • Tại mục Name, nhập KMSWorkshop-RotationDisableOps
  • Click Create Policy Rotating CMK
  1. Thực hiện attach policy mới tạo KMSWorkshop-RotationDisableOps vào role KMSWorkshop-InstanceInitRole
  1. Trong phần Permissions policies
  • Click Add permissions.
  • Click Attach policies. Rotating CMK
  1. Trong phần Other permissions policies
  • Nhập KMSWorkshop-RotationDisableOps vào ô tìm kiếm, nhấn Enter
  • Chọn KMSWorkshop-RotationDisableOps.
  • Click Attach policies. Rotating CMK
  1. Chạy lệnh dưới đây để tạo CMK Key mới
aws kms create-key

Rotating CMK 11. Chạy lệnh dưới đây để cập nhật CMK Alias

aws kms update-alias --alias alias/FirstCMK --target-key-id <key ID of CMK we created in step 10>

Rotating CMK 12. Để kiểm tra chúng ta truy cập KMS Console

  • Click Customer managed keys Rotating CMK

Rotating khóa CMK được tạo từ External Key Material

Với loại CMK được tạo từ External Key Material bạn sẽ không thể bật tính năng Rotation tự động mà bạn sẽ tạo một CMK mới rồi update lại alias.

  1. Chạy lệnh dưới đây để tạo External CMK mới
aws kms create-key --origin=EXTERNAL

Rotating CMK 2. Chạy lệnh

aws kms get-parameters-for-import --key-id <key ID of the External CMK we created in step 1>  --wrapping-algorithm RSAES_OAEP_SHA_1 --wrapping-key-spec RSA_2048

Rotating CMK 3. Đưa giá trị của PublicKey vào file pkey.b64.

  • Sao chép giá trị của PublicKey.
  • Chạy lệnh vi pkey.b64. Rotating CMK
  • Nhấn phím i để chuyển sang chế độ Insert. Dán giá trị PublicKey chúng ta đã sao chép.
  • Nhấn Esc.Nhập :wq.Nhấn Enter để lưu file và thoát ra. Rotating CMK
  1. Làm tương tự PublicKey để đưa giá trị của ImportToken vào file token.b64.
  2. Sử dụng OpenSSL để giải mã các file định dạng b64 vừa lưu ở trên thành các file tương ứng định dạng nhị phân pkey.bintoken.bin
  • Chạy lệnh openssl enc -d -base64 -A -in pkey.b64 -out pkey.bin.
  • Chạy lệnh openssl enc -d -base64 -A -in token.b64 -out token.bin. Rotating CMK
  1. Chạy bên dưới sẽ tạo ra mã khóa đối xứng 256bit lưu vào file genkey.bin. Đây chính là Key material sử dụng cho việc tạo CMK
openssl rand -out genkey.bin 32

Rotating CMK

  1. Thực hiện encrypt Key material. Key material sau khi encrypt sẽ được lưu vào file WrappedKeyMaterial.bin.
  • Chạy lệnh:
openssl rsautl -encrypt -in genkey.bin -oaep -inkey pkey.bin -keyform DER -pubin -out WrappedKeyMaterial.bin

Rotating CMK 8. Chạy lệnh

aws kms import-key-material --key-id <key ID of the External CMK we created in step 1> --encrypted-key-material fileb://WrappedKeyMaterial.bin --import-token fileb://token.bin --expiration-model KEY_MATERIAL_EXPIRES --valid-to 2022-06-02T12:00:00-08:00

Rotating CMK 9. Chạy lệnh dưới đây để update lại alias

aws kms update-alias --alias alias/ImportedCMK --target-key-id <key ID of the External CMK we created in step 1>

Rotating CMK 10. Để kiểm tra chúng ta truy cập KMS Console

  • Click Customer managed keys Rotating CMK