我可以通过 Java SDK 添加客户端加密文件,也可以获取该文件。我现在正在尝试使用 boto3 访问它。(我知道 boto3 不支持此功能,但 s3-wrapper 支持。不过,这涉及 boto3)。
我正在获取 s3 元数据,然后像这样调用 kms.decrypt:
object_info = s3.head_object(Bucket=bucket_name, Key=key_name)
metadata = object_info['Metadata'] # metadata is a dict with lots of x-amz-key, x-amz-iv, etc
ekey = kms.decrypt(CiphertextBlob=metadata,EncryptionContext=metadata)
# fails with:
# ParamValidationError:
# Parameter validation failed: Invalid type for parameter CiphertextBlob, value: .. type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object`
那么,如果我应该将密钥作为 CiphertextBlob 传递怎么办?
# this key looks like 'CiAlgoyM4k...
ekey = kms.decrypt(CiphertextBlob=metadata['x-amz-key-v2'],EncryptionContext=metadata)
# fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None
然后我尝试传入一个base64的密钥:
# this key looks like b'\n %\x82\x8c\x8c\xe2ML...
cblob = base64.b64decode(metadata['x-amz-key-v2'])
ekey = kms.decrypt(CiphertextBlob=cblob,EncryptionContext=metadata)
# (still) fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None
然后我尝试将 s3 内容作为 blob 传递。
full_object = s3.get_object(Bucket=bucket_name, Key=key_name)
ekey = kms.decrypt(CiphertextBlob=full_object['Body'].read(),EncryptionContext=metadata)
# Still fails with:
# botocore.exceptions.ClientError: An error occurred (InvalidCiphertextException) when calling the Decrypt operation: None
所以,我想我的问题是,什么是 S3 元数据中正确的 CiphertextBlob?由于我将它与 Java SDK 的EncryptedPutObjectRequest和AmazonS3EncryptionClient 放在一起,将它抽象出来,我不知道那个 blob 应该是什么样子。
相关链接
- stackoverflow:ruby 中的客户端解密;不使用 KMS
- boto3 github问题:在boto3 for s3中kms解密的开始;不起作用,也许是因为这是“v2”键?