一个 lambda 的 InvalidChiperException,但不是另一个

InvalidChiperException for one lambda, but not the other

db_password = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['DB_PASSWORD']))['Plaintext']

services_region = 'us-east-1'    
db_password = boto3.client('kms', region_name=services_region).decrypt(CiphertextBlob=b64decode(os.environ['DB_PASSWORD']))['Plaintext']

我已经尝试了上面的两个代码,我已验证两个 lambda 函数都使用相同的 VPC、私有子网,并且两者的安全组都已添加到 kms 端点。他们都使用完全相同的角色。他们都使用相同的 S3 压缩包,并且他们都在代码中使用相同的行来进行 .decrypt(我复制并粘贴了代码,它是上面的第二个)。

其中一个可以正常工作,另一个则抛出以下内容:

An error occurred (InvalidCiphertextException) when calling the Decrypt operation: : InvalidCiphertextException
Traceback (most recent call last):
  File "/var/task/awsfinbi_workdocs_api_pull/bin/api_pull.py", line 143, in lambda_handler
    get_data()
  File "/var/task/awsfinbi_workdocs_api_pull/bin/api_pull.py", line 100, in get_data
    db_load(df, s)
  File "/var/task/awsfinbi_workdocs_api_pull/bin/api_pull.py", line 122, in db_load
    raise e
  File "/var/task/awsfinbi_workdocs_api_pull/bin/api_pull.py", line 112, in db_load
    db_password = boto3.client('kms',region_name=services_region).decrypt(CiphertextBlob=b64decode(os.environ['DB_PASSWORD']))['Plaintext']
  File "/var/runtime/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 635, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidCiphertextException: An error occurred (InvalidCiphertextException) when calling the Decrypt operation:

两者唯一的区别是密码不同。但是,我使用与程序用户不同的密码对其进行了测试,似乎没有任何区别。

lambda 上的权限选项卡显示 kms:* Allow: All resources

我试过创建一个密钥并使用它,我也试过使用所有其他密钥。都抛出这个错误。

我在解决这个问题时可能遗漏了什么?

好吧,我偶然发现了一个答案。

1 关于我的代码的事情,没有发布,是我有一个多次调用解密的 for 循环。显然,永远不应该这样做。因此,我将解密代码移出 for 循环。

即使将它从 for 循环中删除也没有解决它,但是当我将它设为如下两行时它起作用了:

ENCRYPTED = os.environ['DB_PASSWORD']
db_password = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED),
    EncryptionContext={'LambdaFunctionName': os.environ['AWS_LAMBDA_FUNCTION_NAME']}
)['Plaintext'].decode('utf-8')

我之前曾尝试过上下文,但当时它并没有帮助全部一行。我不确定为什么它不喜欢将它放在一行中,但上面的方法有效。