4
import os, sys
AWS_DIRECTORY = '/home/jenkins/.aws'
certificates_folder = 'my_folder'

SUCCESS = 'success'

class AmazonKMS(object):


def __init__(self):
    # making sure boto3 has the certificates and region files
    result = os.system('mkdir -p ' + AWS_DIRECTORY)
    self._check_os_result(result)
    result = os.system('cp ' + certificates_folder + 'kms_config ' + AWS_DIRECTORY + '/config')
    self._check_os_result(result)
    result = os.system('cp ' + certificates_folder + 'kms_credentials ' + AWS_DIRECTORY + '/credentials')
    self._check_os_result(result)


    # boto3 is the amazon client package
    import boto3
    self.kms_client = boto3.client('kms', region_name='us-east-1')
    self.global_key_alias = 'alias/global'
    self.global_key_id = None

def _check_os_result(self, result):
    if result != 0 and raise_on_copy_error:
        raise FAILED_COPY


def decrypt_text(self, encrypted_text):
    response = self.kms_client.decrypt(
        CiphertextBlob = encrypted_text
    )

    return response['Plaintext']

使用时 amazon_kms = AmazonKMS() amazon_kms.decrypt_text(blob_password)

得到

E   ClientError: An error occurred (AccessDeniedException) when calling the Decrypt operation: The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access.

堆栈跟踪是

../keys_management/amazon_kms.py:77: in decrypt_text
    CiphertextBlob = encrypted_text
/home/jenkins/.virtualenvs/global_tests/local/lib/python2.7/site-packages/botocore/client.py:253: in _api_call
    return self._make_api_call(operation_name, kwargs)
/home/jenkins/.virtualenvs/global_tests/local/lib/python2.7/site-packages/botocore/client.py:557: in _make_api_call
    raise error_class(parsed_response, operation_name)

这发生在每小时运行一次的脚本中。

它每天只失败 2 -3 次。

重试后成功。

试图从boto3升级1.2.31.4.4

这种行为的可能原因是什么?

4

1 回答 1

0

我的猜测是问题不在于您在此处描述的任何内容。很可能登录令牌超时或类似的东西。为了进一步研究这一点,仔细查看此处登录的工作方式可能会有所帮助。这段代码如何运行?它是否像在 Lambda 或 EC2 上一样在 AWS 内部运行?您是否从自己的服务器上运行它(看起来像在 jenkins 上运行)?登录访问是如何建立的?这些 kms_credentials 是做什么用的,它们的外观如何?您是否做过类似担任角色的事情(这可能会通过访问令牌起作用,但一段时间后将不再起作用)?

于 2020-05-06T09:22:34.540 回答