首页 > 解决方案 > 如何将 KeyManager 添加到使用 moto 模拟的 kms 密钥中

问题描述

我想创建一个由 AWS 管理的密钥。到目前为止,这就是我所拥有的

@mock_kms
def test_mocking_getting_keys(self):
    session = boto3.Session(profile_name=profile)
    client = session.client('kms', 'us-east-2')
    key = client.create_key(
        Policy='string',
        Description='string',
        KeyUsage='SIGN_VERIFY',
        CustomerMasterKeySpec='RSA_2048',
        Origin='AWS_KMS',
        CustomKeyStoreId='string',
        BypassPolicyLockoutSafetyCheck=True,
        Tags=[
            {
                'TagKey': 'string',
                'TagValue': 'string'
            },
        ]
    )
    print(key)

但密钥似乎没有 KeyManager 字段:

 {'KeyMetadata': {'AWSAccountId': '012345678912', 'KeyId': '7fc3e676-0d1c-4526-9161-41b27a776033', 'Arn': 'arn:aws:kms:us-east-2:012345678912:key/7fc3e676-0d1c-4526-9161-41b27a776033', 'CreationDate': datetime.datetime(2020, 1, 3, 13, 31, 17, tzinfo=tzutc()), 'Enabled': True, 'Description': 'string', 'KeyUsage': 'SIGN_VERIFY', 'KeyState': 'Enabled'}, 'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'amazon.com'}, 'RetryAttempts': 0}}

我尝试在 create_key 调用期间将 KeyManager 添加为参数,但这也不起作用。

好像 moto 不返回 KeyManager 字段。有没有办法专门模拟该返回值但不改变其余参数的 dictionary.get 方法的行为?

IE

key['KeyMetadata']['AWSAccountId']将返回模拟值,然后 key['KeyMetadata']['KeyManager']返回另一个我可以指定的模拟值。

标签: pythonamazon-web-servicesboto3moto

解决方案


Moto 当前不返回KeyManager属性,您可以在 Moto GitHub 上打开一个问题,或者自己添加它(本地或 PR'ed 到上游)


推荐阅读