feat(ses): honor ConfigurationSetName on CreateEmailIdentity (v2) (#1812)

* feat(ses): honor ConfigurationSetName on CreateEmailIdentity (v2)

CreateEmailIdentity now reads the optional ConfigurationSetName and sets it as
the identity's default configuration set, surfaced by GetEmailIdentity. Verified
against AWS: a non-existent configuration set fails with NotFoundException before
the identity is created (atomic), matching the deleted-default send behavior.

* fix(ses): treat only an empty ConfigurationSetName as absent on CreateEmailIdentity

A whitespace-only ConfigurationSetName was silently ignored (isBlank),
diverging from the PutEmailIdentityConfigurationSetAttributes path, which
routes such a name through name validation. Switch to isEmpty so both paths
reject a whitespace-only name as invalid input (400), and add coverage for
the empty (no default) and whitespace (400) cases.

* test(ses): clarify that empty-string ConfigurationSetName handling deviates from AWS

The comment on the empty-string CreateEmailIdentity test mentioned that AWS stores ""
verbatim without stating that Floci deliberately diverges (treating "" as "no default"),
which could read as if the assertion matched AWS. Reword to make the deviation and its
rationale explicit.

* docs(ses): correct the non-string ConfigurationSetName comment to match the actual response

The comment claimed AWS returns a 400 with an empty body, but the code throws a
SerializationException that AwsExceptionMapper renders as a JSON error body. Reword the
comment to describe the actual response shape.

* fix(ses): reject a non-string EmailIdentity on CreateEmailIdentity

CreateEmailIdentity read EmailIdentity via asText, which coerces a non-string JSON value
(e.g. 123 -> "123") and would silently create an identity named "123". Reject a non-string
EmailIdentity with a 400 SerializationException, matching the ConfigurationSetName handling
in the same method.

* fix(ses): validate Tags before creating the identity on CreateEmailIdentity

parseTagsArray ran after verifyEmailIdentity/verifyDomainIdentity, so a non-array Tags
value (e.g. "Tags":"oops") returned 400 but left the identity created — a half-created
state. parseTagsArray is pure, so hoist it above the create; the identity is now
persisted only after both the ConfigurationSetName and the Tags shape have been
validated, matching AWS.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-08-12 00:12:16 -04:00
1 parent b6765b3056
commit cd3588b517
3 files changed
+159 -2

No files matched your search

@@ -104,4 +104,19 @@ class SesEmailIdentityConfigurationSetTest {
.build()))
.isInstanceOf(NotFoundException.class);
}
@Test
@Order(4)
void createEmailIdentity_withConfigurationSet_readsBack() {
String id = "sdk-cs-oncreate-" + TestFixtures.uniqueName() + "@example.com";
sesV2.createEmailIdentity(CreateEmailIdentityRequest.builder()
.emailIdentity(id).configurationSetName(configSet).build());
try {
assertThat(sesV2.getEmailIdentity(GetEmailIdentityRequest.builder()
.emailIdentity(id).build()).configurationSetName())
.isEqualTo(configSet);
} finally {
sesV2.deleteEmailIdentity(DeleteEmailIdentityRequest.builder().emailIdentity(id).build());
}
}
}
@@ -89,21 +89,58 @@ public class SesController {
String region = regionResolver.resolveRegion(headers);
try {
JsonNode request = objectMapper.readTree(body);
String emailIdentity = request.path("EmailIdentity").asText(null);
JsonNode emailIdentityNode = request.path("EmailIdentity");
if (!emailIdentityNode.isMissingNode() && !emailIdentityNode.isNull()
&& !emailIdentityNode.isTextual()) {
// A non-string EmailIdentity is rejected rather than coerced (asText would turn 123
// into "123"), the same as ConfigurationSetName below.
throw new AwsException("SerializationException", null, 400);
}
String emailIdentity = emailIdentityNode.asText(null);
if (emailIdentity == null || emailIdentity.isBlank()) {
throw new AwsException("BadRequestException", "EmailIdentity is required.", 400);
}
// ConfigurationSetName must be a String; AWS rejects a non-string, so reject it here too
// rather than coercing via asText (which would turn 123 into "123"). Floci surfaces this
// as a 400 SerializationException, rendered as a JSON error body by AwsExceptionMapper.
JsonNode configSetNode = request.path("ConfigurationSetName");
String configurationSetName = null;
if (!configSetNode.isMissingNode() && !configSetNode.isNull()) {
if (!configSetNode.isTextual()) {
throw new AwsException("SerializationException", null, 400);
}
configurationSetName = configSetNode.textValue();
}
if (sesService.getIdentityVerificationAttributes(emailIdentity, region) != null) {
throw new AwsException("AlreadyExistsException",
"Email identity " + emailIdentity + " already exist.", 400);
}
// Parse Tags up front. parseTagsArray is pure (it only reads the node), so validating the
// shape before creating the identity keeps the call atomic — a malformed Tags value fails
// without leaving a half-created identity behind, matching AWS and the ConfigurationSetName
// pre-check below.
List<Tag> parsedTags = parseTagsArray(request.path("Tags"));
// Verified against AWS: a non-existent ConfigurationSetName fails the whole call
// (NotFoundException) without creating the identity, so validate it before creating.
// Only the empty string means "no default configuration set" (consistent with the
// PutEmailIdentityConfigurationSetAttributes path); a whitespace-only name flows through
// name validation and is rejected as invalid input, rather than being silently ignored.
boolean hasConfigSet = configurationSetName != null && !configurationSetName.isEmpty();
if (hasConfigSet) {
sesService.getConfigurationSet(configurationSetName, region);
}
Identity identity = emailIdentity.contains("@")
? sesService.verifyEmailIdentity(emailIdentity, region)
: sesService.verifyDomainIdentity(emailIdentity, region);
List<Tag> parsedTags = parseTagsArray(request.path("Tags"));
if (hasConfigSet) {
sesService.setEmailIdentityConfigurationSet(emailIdentity, configurationSetName, region);
}
if (parsedTags != null) {
sesService.setIdentityTags(emailIdentity, region, parsedTags);
}
@@ -301,6 +301,111 @@ class SesEmailIdentityConfigurationSetV2IntegrationTest {
.body("message", equalTo("Configuration set <" + cs + "> does not exist."));
}
@Test
@Order(11)
void createEmailIdentity_withConfigurationSet_surfacesInGet() {
String cs = "cs-attr-oncreate-cs";
String id = "cs-attr-oncreate@floci.test";
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"ConfigurationSetName\":\"" + cs + "\"}")
.when().post("/v2/email/configuration-sets").then().statusCode(200);
// CreateEmailIdentity accepts a ConfigurationSetName and sets it as the identity default.
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"" + id + "\",\"ConfigurationSetName\":\"" + cs + "\"}")
.when().post("/v2/email/identities").then().statusCode(200);
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/" + id)
.then().statusCode(200).body("ConfigurationSetName", equalTo(cs));
}
@Test
@Order(12)
void createEmailIdentity_withMissingConfigurationSet_returns404AndDoesNotCreate() {
String id = "cs-attr-oncreate-missing@floci.test";
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"" + id + "\",\"ConfigurationSetName\":\"cs-attr-ghost-oncreate\"}")
.when().post("/v2/email/identities").then().statusCode(404)
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("Configuration set <cs-attr-ghost-oncreate> does not exist."));
// The identity must not have been created (AWS is atomic on the config-set validation).
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/" + id).then().statusCode(404);
}
@Test
@Order(17)
void createEmailIdentity_withNonArrayTags_returns400AndDoesNotCreate() {
String id = "cs-attr-oncreate-badtags@floci.test";
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"" + id + "\",\"Tags\":\"oops\"}")
.when().post("/v2/email/identities").then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("Tags must be an array."));
// Tags is validated before the identity is persisted, so nothing is created.
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/" + id).then().statusCode(404);
}
@Test
@Order(13)
void createEmailIdentity_withNonStringConfigurationSet_returns400() {
// ConfigurationSetName is a string; a non-string must be rejected, not coerced.
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"cs-attr-badtype@floci.test\",\"ConfigurationSetName\":123}")
.when().post("/v2/email/identities").then().statusCode(400)
.body("__type", equalTo("SerializationException"));
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/cs-attr-badtype@floci.test").then().statusCode(404);
}
@Test
@Order(14)
void createEmailIdentity_withEmptyConfigurationSet_createsWithoutDefault() {
// Floci treats an empty-string ConfigurationSetName as "no default configuration set"
// (consistent with the PutEmailIdentityConfigurationSetAttributes path). This is a deliberate
// deviation from AWS, which stores "" verbatim and echoes it back; Floci keeps the two paths
// aligned instead.
String id = "cs-attr-emptycs@floci.test";
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"" + id + "\",\"ConfigurationSetName\":\"\"}")
.when().post("/v2/email/identities").then().statusCode(200);
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/" + id).then().statusCode(200)
.body("$", org.hamcrest.Matchers.not(org.hamcrest.Matchers.hasKey("ConfigurationSetName")));
}
@Test
@Order(15)
void createEmailIdentity_withWhitespaceConfigurationSet_returns400AndDoesNotCreate() {
// A whitespace-only name is not "absent": it flows through name validation and is rejected
// as invalid input (the same 400 the Put path yields), rather than being silently ignored.
String id = "cs-attr-wscs@floci.test";
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":\"" + id + "\",\"ConfigurationSetName\":\" \"}")
.when().post("/v2/email/identities").then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("ConfigurationSetName is required."));
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/" + id).then().statusCode(404);
}
@Test
@Order(16)
void createEmailIdentity_withNonStringEmailIdentity_returns400() {
// EmailIdentity is a string; a non-string must be rejected (not coerced into "123"), the same
// as a non-string ConfigurationSetName.
given().contentType("application/json").header("Authorization", SES_AUTH)
.body("{\"EmailIdentity\":123}")
.when().post("/v2/email/identities").then().statusCode(400)
.body("__type", equalTo("SerializationException"));
given().header("Authorization", SES_AUTH)
.when().get("/v2/email/identities/123").then().statusCode(404);
}
private void drainQueue() {
for (int i = 0; i < 5; i++) {
Response r = given().contentType(JSON_10).header("Authorization", SQS_AUTH)