Make create-key optionally deterministic#9884
Conversation
This is a port of the behavior from localstack/localstack#10379
bblommers
left a comment
There was a problem hiding this comment.
Hi @mdaniel! Thanks for the PR.
I'm not necessarily a fan of this feature as-is, to be honest, as it could lead to an explosion of custom logic to make names or IDs for every service deterministic.
Have you looked into seeding Moto instead? That should result in the same outcome, i.e. a static ID:
https://docs.getmoto.org/en/latest/docs/configuration/recorder/index.html#deterministic-identifiers
|
Hey @bblommers , thanks for taking a look! I admit currently there are two conflated concerns in this change:
What I'm hearing is that the preferred way is to instead have I tried it with diff --git a/moto/kms/models.py b/moto/kms/models.py
index d2853d585..465a46198 100644
--- a/moto/kms/models.py
+++ b/moto/kms/models.py
@@ -1,5 +1,4 @@
import json
-import os
from collections.abc import Iterable
from copy import copy
from datetime import datetime, timedelta
@@ -650,7 +649,7 @@ class KmsBackend(BaseBackend):
else:
plaintext_len = number_of_bytes
- plaintext = os.urandom(plaintext_len)
+ plaintext = mock_random.randbytes(plaintext_len)
ciphertext_blob, arn = self.encrypt(
key_id=key_id, plaintext=plaintext, encryption_context=encryption_context
diff --git a/moto/kms/responses.py b/moto/kms/responses.py
index 8ec091b29..e7e712a0c 100644
--- a/moto/kms/responses.py
+++ b/moto/kms/responses.py
@@ -1,11 +1,11 @@
import base64
import json
-import os
import re
from typing import Any
from moto.core.responses import BaseResponse
from moto.kms.utils import RESERVED_ALIASE_TARGET_KEY_IDS, RESERVED_ALIASES
+from moto.moto_api._internal import mock_random
from moto.utilities.utils import get_partition
from .exceptions import (
@@ -632,7 +632,7 @@ class KmsResponse(BaseResponse):
"equal to 1024"
)
- entropy = os.urandom(number_of_bytes)
+ entropy = mock_random.randbytes(number_of_bytes)
response_entropy = base64.b64encode(entropy).decode("utf-8")
and it did work better:
that cycle did what I expected, although I am going to have to file a followup bug because That last item kind of relates to why just having the seed behavior won't help for the way that we currently use localstack to exercise our code, since in the real AWS account the key-id is essentially fixed, and our ability to load a known key-id with known key material into moto means there is one less deviation between real and local Let me take this new behavior back to the team and see if it's a compromise they're willing to make to get off of localstack In the interim, I'll open a separate PR for |
|
I still owe you the PR about Are there alternatives that you would accept for allowing one to actually set the key material, aside from using a seed to PRNG-it into place? |
This is a follow-on change from getmoto#9884 to allow more deterministic behavior
What?
Allow using Tag keys on
kms:CreateKeyrequests so that the backing Key can optionally be deterministic in both its master key material as well as optionally have a knownKeyIdThis is a port of localstack/localstack#10379
Why?
When testing KMS it can often be helpful to compare expected results, or to be able to use a fixed
KeyIdin setup scripts. Since there was only one function call to generate the key material, being able to supply it out of band is convenient