Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(kms): include KeyMaterialId in GenerateDataKey responses (#1854)

Alberto Ureñaf7efa688c014
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 270cabd1..10274004 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+
+- **KMS — `GenerateDataKey` returns `KeyMaterialId`** — requests without `Recipient` now include the identifier of the wrapping key material. The identifier remains stable across requests and persistence reloads, and changes when the wrapping material changes. Fixes #1853.
### Added
- **KMS — grants** — `CreateGrant`, `RevokeGrant` and `RetireGrant`, and `ListGrants` returns the grants they create instead of always an empty list. `CreateGrant` returns a `GrantId` and a `GrantToken`, follows the key state (`DisabledException` for a disabled key, `KMSInvalidStateException` pending deletion, replica deletion or import), rejects operations the key cannot perform (`Sign` / `Verify` / `GenerateMac` / `VerifyMac` on a symmetric encryption key, data key operations on an asymmetric key, anything but `GenerateMac` / `VerifyMac` / `DescribeKey` / `CreateGrant` / `RetireGrant` on an HMAC key) with `ValidationException`, and with a `Name` a retry with identical parameters returns the original `GrantId` with a new token. `ListGrants` filters by `GrantId` and `GranteePrincipal` and pages with `Limit` (1 to 100, default 50) and `Marker`. `RetireGrant` takes a `GrantToken`, or a `KeyId` and `GrantId`. All three honour `DryRun`, and grants persist with the key. Grants are recorded but not evaluated when authorizing other calls, as with key policies. Contributed by @DaviReisVieira.
diff --git a/ministack/services/kms.py b/ministack/services/kms.py
index 8fe35c7f..d337193e 100644
--- a/ministack/services/kms.py
+++ b/ministack/services/kms.py
@@ -1231,11 +1231,22 @@ def _generate_data_key(data):
if rec is None:
# result is an error response tuple
return result
- return json_response({
+
+ response = {
"KeyId": rec["Arn"],
"Plaintext": base64.b64encode(data_key).decode(),
"CiphertextBlob": base64.b64encode(result).decode(),
- })
+ }
+ if "Recipient" not in data:
+ # Derive an emulator identifier from the wrapping material and key ID.
+ # Both survive persistence and are shared by multi-Region replicas.
+ response["KeyMaterialId"] = hmac.new(
+ rec["_symmetric_key"],
+ b"ministack:kms:key-material-id:" + rec["KeyId"].encode("utf-8"),
+ hashlib.sha256,
+ ).hexdigest()
+
+ return json_response(response)
def _generate_data_key_pair_common(data, action):
diff --git a/tests/test_kms.py b/tests/test_kms.py
index c90fc5c3..dd79454b 100644
--- a/tests/test_kms.py
+++ b/tests/test_kms.py
@@ -2,6 +2,7 @@ import base64
import datetime as _dt
import json
import os
+import re
import time
import uuid as _uuid_mod
@@ -305,6 +306,73 @@ def test_kms_encrypt_decrypt_with_explicit_key(kms_client):
)
assert dec_resp["Plaintext"] == plaintext
[email protected]("key_spec", ["AES_128", "AES_256"])
+def test_kms_generate_data_key_material_id(kms_client, key_spec):
+ """The material identifier belongs to the wrapping key material."""
+ key_id = kms_client.create_key(
+ KeySpec="SYMMETRIC_DEFAULT",
+ KeyUsage="ENCRYPT_DECRYPT",
+ )["KeyMetadata"]["KeyId"]
+
+ try:
+ first = kms_client.generate_data_key(
+ KeyId=key_id,
+ KeySpec=key_spec,
+ EncryptionContext={"record": "first"},
+ )
+ second = kms_client.generate_data_key(
+ KeyId=key_id,
+ KeySpec=key_spec,
+ EncryptionContext={"record": "second"},
+ )
+
+ material_id = first.get("KeyMaterialId")
+ assert isinstance(material_id, str)
+ assert re.fullmatch(r"[a-f0-9]{64}", material_id)
+ assert second.get("KeyMaterialId") == material_id
+ finally:
+ kms_client.schedule_key_deletion(
+ KeyId=key_id,
+ PendingWindowInDays=7,
+ )
+
+def test_kms_generate_data_key_material_id_survives_state_roundtrip():
+ from ministack.services import kms as _kms
+
+ _kms.reset()
+ try:
+ status, _headers, body = _kms._create_key({
+ "KeySpec": "SYMMETRIC_DEFAULT",
+ "KeyUsage": "ENCRYPT_DECRYPT",
+ })
+ assert status == 200
+ key_id = json.loads(body)["KeyMetadata"]["KeyId"]
+
+ def material_id():
+ status, _headers, body = _kms._generate_data_key({
+ "KeyId": key_id,
+ "KeySpec": "AES_256",
+ })
+ assert status == 200
+ return json.loads(body)["KeyMaterialId"]
+
+ original_id = material_id()
+
+ state = _kms.get_state()
+ _kms.reset()
+ _kms.load_persisted_state(state)
+
+ assert material_id() == original_id
+
+ # Keep the key ID, but replace its material.
+ rec = _kms._keys[key_id]
+ rec["_symmetric_key"] = bytes(
+ byte ^ 0xFF for byte in rec["_symmetric_key"]
+ )
+ assert material_id() != original_id
+ finally:
+ _kms.reset()
+
def test_kms_generate_data_key_aes_256(kms_client):
key = kms_client.create_key(
KeySpec="SYMMETRIC_DEFAULT", KeyUsage="ENCRYPT_DECRYPT"