认证考题 | 使用 AWS KMS key 加密小型文件


  题目

A company runs a cron job on an Amazon EC2 instance on a predefined schedule. The cron job calls a bash script that encrypts a 2 KB file. A security engineer creates an AWS Key Management Service (AWS KMS) customer managed key with a key policy. The key policy and the EC2 instance role have the necessary configuration for this job.
Which process should the bash script use to encrypt the file?
A. Use the aws kms encrypt command to encrypt the file by using the existing KMS key.
B. Use the aws kms create-grant command to generate a grant for the existing KMS key.
C. Use the aws kms encrypt command to generate a data key. Use the plaintext data key to encrypt the file.
D. Use the aws kms generate-data-key command to generate a data key. Use the encrypted data key to encrypt the file.

  参考答案

A

  需求概括

一家公司在 Amazon EC2实例上按预定义计划运行 cron 作业。该作业调用 bash 脚本加密一个2KB 的文件。安全工程师创建了一个带有密钥策略的 AWS Key Management Service(AWS KMS)客户托管密钥,且密钥策略和 EC2实例角色已为该作业配置必要权限。
bash 脚本应使用哪种 AWS KMS 命令以最小操作步骤加密文件?

  参考解析

技巧:排除明显错误选项,在没有明显错误的选项中选择最合理的选项。

A. 正确。使用 aws kms encrypt 命令通过现有 KMS 密钥加密文件。直接加密简单高效,适合小文件,且脚本无需管理数据密钥。
B. 不正确。使用 aws kms create-grant 命令为现有 KMS 密钥生成授权。create-grant 用于临时授权其他 AWS 主体使用密钥,与加密文件无关。
C. 不正确。使用 aws kms encrypt 命令生成数据密钥。使用明文数据密钥加密文件。encrypt 不生成数据密钥,此选项混淆了命令功能。
D. 不正确。使用 aws kms generate-data-key 命令生成数据密钥。使用加密的数据密钥加密文件。加密数据密钥仅用于存储,解密时需用 KMS 解密数据密钥,无法直接用于文件加密。

  技术总结

KMS 直接加密:使用 aws kms encrypt 直接加密文件数据,适用于小文件(不大于4KB),但需处理加密结果(如 Base64编码)。
KMS 数据密钥加密:使用 aws kms generate-data-key 生成明文和密文数据密钥,用明文密钥加密文件,密文密钥存储供解密使用。
权限要求:EC2实例角色需具备 kms:Encrypt 或 kms:GenerateDataKey 权限,具体取决于加密方式。