Skip to content

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

fix(sesv2): return Easy DKIM tokens for a domain email identity (#1899)

Jeff Heller00e2c6f82283
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 091e504c..88ef8497 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+- **SESv2 — `CreateEmailIdentity`/`GetEmailIdentity` return Easy DKIM tokens for a DOMAIN identity** — a domain identity created without `DkimSigningAttributes` answered an empty `Tokens` list with `Status: NOT_STARTED`. AWS provides a set of DKIM tokens for its CNAME records in that case (Easy DKIM), so the Terraform `aws_sesv2_email_identity` resource's `dkim_signing_attributes[0].tokens` indexing failed. A DOMAIN identity now gets three tokens, `SigningAttributesOrigin: AWS_SES` and `Status: PENDING`, unless `DkimSigningAttributes` brings its own key (BYODKIM); EMAIL_ADDRESS identities are unchanged.
- **API Gateway v2 (HTTP API) — a missing identity source is a 401 without caching too** — a REQUEST authorizer's short circuit for a declared identity source missing from the request only fired with `authorizerResultTtlInSeconds` caching on; with caching off the authorizer was invoked. AWS answers `401 {"message":"Unauthorized"}` (compact JSON) without invoking it either way, as observed on a deployed HTTP API with a TTL of 0. `$context.*` identity sources, which MiniStack does not model, do not count as missing without caching.
### Added
diff --git a/ministack/services/ses_v2.py b/ministack/services/ses_v2.py
index 7fbf1885..305e290a 100644
--- a/ministack/services/ses_v2.py
+++ b/ministack/services/ses_v2.py
@@ -15,6 +15,7 @@ Email templates live in the v1 store, so either API version sees the other's.
import base64
import copy
+import hashlib
import json
import logging
import os
@@ -113,6 +114,25 @@ def _resource_arn(kind, name):
return f"arn:aws:ses:{get_region()}:{get_account_id()}:{kind}/{name}"
+def _easy_dkim_attributes(identity, identity_type, signing_attributes):
+ """A DOMAIN identity uses Easy DKIM unless DkimSigningAttributes brings its
+ own key (BYODKIM): three tokens for its CNAME records, verification
+ pending. An EMAIL_ADDRESS identity has no DKIM tokens."""
+ byodkim = any(signing_attributes.get(k) for k in ("DomainSigningPrivateKey", "DomainSigningSelector"))
+ if identity_type != "DOMAIN" or byodkim:
+ return {"SigningEnabled": False, "Status": "NOT_STARTED", "Tokens": []}
+ tokens = [
+ hashlib.md5(f"{identity}-dkim-{i}".encode()).hexdigest()[:32]
+ for i in range(3)
+ ]
+ return {
+ "SigningEnabled": False,
+ "SigningAttributesOrigin": "AWS_SES",
+ "Status": "PENDING",
+ "Tokens": tokens,
+ }
+
+
def _invalid_resource_arn(arn):
return _json_err("BadRequestException", f"Invalid ResourceArn: {arn}")
@@ -451,11 +471,13 @@ async def handle_request(method, path, headers, body, query_params):
if not identity:
return _json_err("BadRequestException", "EmailIdentity is required")
identity_type = "DOMAIN" if "." in identity and "@" not in identity else "EMAIL_ADDRESS"
+ dkim_attributes = _easy_dkim_attributes(
+ identity, identity_type, data.get("DkimSigningAttributes") or {})
_identities[identity] = {
"EmailIdentity": identity,
"IdentityType": identity_type,
"VerifiedForSendingStatus": True,
- "DkimAttributes": {"SigningEnabled": False, "Status": "NOT_STARTED", "Tokens": []},
+ "DkimAttributes": dkim_attributes,
"MailFromAttributes": {"BehaviorOnMxFailure": "USE_DEFAULT_VALUE"},
"Tags": data.get("Tags", []),
"CreatedTimestamp": now_iso(),
@@ -464,7 +486,7 @@ async def handle_request(method, path, headers, body, query_params):
return json_response({
"IdentityType": identity_type,
"VerifiedForSendingStatus": True,
- "DkimAttributes": {"SigningEnabled": False, "Status": "NOT_STARTED", "Tokens": []},
+ "DkimAttributes": dkim_attributes,
})
# GET /v2/email/identities (ListEmailIdentities)
diff --git a/tests/test_ses_v2.py b/tests/test_ses_v2.py
index 2e24182c..83e8d512 100644
--- a/tests/test_ses_v2.py
+++ b/tests/test_ses_v2.py
@@ -172,6 +172,49 @@ def test_ses_v2_identity_tag_resource_uses_parser_backed_resource_arn(ses_v2):
assert body["Tags"] == [{"Key": "team", "Value": "platform"}]
+def test_ses_v2_create_domain_identity_returns_easy_dkim_tokens(ses_v2):
+ """A domain created without DkimSigningAttributes gets Easy DKIM tokens (CreateEmailIdentity API reference)."""
+ status, body = _call(
+ ses_v2,
+ "POST",
+ "/v2/email/identities",
+ body={"EmailIdentity": "dkim.example.com"},
+ )
+ assert status == 200
+ dkim = body["DkimAttributes"]
+ assert len(set(dkim["Tokens"])) == 3
+ assert (dkim["SigningAttributesOrigin"], dkim["Status"]) == ("AWS_SES", "PENDING")
+
+ status, body = _call(ses_v2, "GET", "/v2/email/identities/dkim.example.com")
+ assert status == 200
+ assert body["DkimAttributes"]["Tokens"] == dkim["Tokens"]
+
+
+def test_ses_v2_create_byodkim_domain_identity_gets_no_easy_dkim_tokens(ses_v2):
+ """DkimSigningAttributes with a key and selector is BYODKIM, not Easy DKIM."""
+ status, body = _call(
+ ses_v2,
+ "POST",
+ "/v2/email/identities",
+ body={"EmailIdentity": "byodkim.example.com", "DkimSigningAttributes": {
+ "DomainSigningSelector": "sel1", "DomainSigningPrivateKey": "cHJpdmF0ZQ=="}},
+ )
+ assert status == 200
+ assert body["DkimAttributes"].get("SigningAttributesOrigin") != "AWS_SES"
+
+
+def test_ses_v2_create_email_address_identity_has_no_dkim_tokens(ses_v2):
+ status, body = _call(
+ ses_v2,
+ "POST",
+ "/v2/email/identities",
+ body={"EmailIdentity": "[email protected]"},
+ )
+ assert status == 200
+ assert body["DkimAttributes"]["SigningEnabled"] is False
+ assert body["DkimAttributes"]["Tokens"] == []
+
+
def test_ses_v2_configuration_set_tag_resource_uses_parser_backed_resource_arn(ses_v2):
config_set_name = "parser-config"
resource_arn = _arn("configuration-set", config_set_name)