diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java index 6e2eb090fd..db70afa48c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java @@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.ses.model.Identity; import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.github.hectorvent.floci.services.ses.model.SuppressedDestination; +import io.github.hectorvent.floci.services.ses.model.SuppressionOptions; import io.github.hectorvent.floci.services.ses.model.Tag; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -609,6 +610,27 @@ public class SesController { if (parsedTags != null) { cs.setTags(parsedTags); } + JsonNode suppressionNode = request.path("SuppressionOptions"); + if (!suppressionNode.isMissingNode() && !suppressionNode.isNull()) { + if (!suppressionNode.isObject()) { + throw new AwsException("SerializationException", "Expected null", 400); + } + JsonNode reasonsNode = suppressionNode.path("SuppressedReasons"); + if (reasonsNode.isMissingNode() || reasonsNode.isNull()) { + throw new AwsException("InternalFailure", + "An internal failure has occurred.", 500); + } + SuppressionOptions options = new SuppressionOptions(); + options.setSuppressedReasons(parseSuppressedReasons(reasonsNode)); + cs.setSuppressionOptions(options); + } + JsonNode sendingNode = request.path("SendingOptions"); + if (!sendingNode.isMissingNode() && !sendingNode.isNull()) { + if (!sendingNode.isObject()) { + throw new AwsException("SerializationException", "Expected null", 400); + } + cs.setSendingEnabled(parseSendingEnabled(sendingNode.path("SendingEnabled"))); + } sesService.createConfigurationSet(cs, region); LOG.infov("SES V2 CreateConfigurationSet: {0}", name); return Response.ok(objectMapper.createObjectNode()).build(); @@ -674,21 +696,7 @@ public class SesController { ? objectMapper.createObjectNode() : objectMapper.readTree(body); requireJsonObject(request); - JsonNode reasonsNode = request.path("SuppressedReasons"); - List reasons = new ArrayList<>(); - if (!reasonsNode.isMissingNode() && !reasonsNode.isNull()) { - if (!reasonsNode.isArray()) { - throw new AwsException("BadRequestException", - "SuppressedReasons must be an array.", 400); - } - for (JsonNode r : reasonsNode) { - if (r.isNull() || !r.isTextual()) { - throw new AwsException("BadRequestException", - "SuppressedReasons entries must be strings.", 400); - } - reasons.add(r.asText()); - } - } + List reasons = parseSuppressedReasons(request.path("SuppressedReasons")); sesService.putConfigurationSetSuppressionOptions(name, reasons, region); LOG.infov("SES V2 PutConfigurationSetSuppressionOptions: {0} on {1}", reasons, name); return Response.ok(objectMapper.createObjectNode()).build(); @@ -1267,6 +1275,77 @@ public class SesController { return out; } + /** + * Parses a {@code SuppressedReasons} JSON array into a list, validating + * structure only; reason values are validated by the service layer. + * Structural violations reproduce the AWS deserialization-layer errors + * (verified against real AWS SES V2 on 2026-06-13): a non-array node and + * non-string scalar / container elements fail with + * {@code SerializationException}, while {@code null} elements pass + * deserialization and are rejected by the service-layer value validation, + * exactly as AWS does. Missing / null yields an empty list for the PUT + * path, which AWS treats as an explicit empty override. + */ + private static List parseSuppressedReasons(JsonNode reasonsNode) { + List reasons = new ArrayList<>(); + if (!reasonsNode.isMissingNode() && !reasonsNode.isNull()) { + if (!reasonsNode.isArray()) { + throw new AwsException("SerializationException", "Expected list or null", 400); + } + for (JsonNode r : reasonsNode) { + if (r.isTextual() || r.isNull()) { + reasons.add(r.asText(null)); + } else if (r.isNumber()) { + throw new AwsException("SerializationException", + "NUMBER_VALUE can not be converted to a String", 400); + } else if (r.isBoolean()) { + throw new AwsException("SerializationException", + (r.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE") + + " can not be converted to a String", 400); + } else { + throw unexpectedStartError(r); + } + } + } + return reasons; + } + + /** + * Reproduces the AWS deserialization behavior for {@code SendingEnabled} + * (verified against real AWS SES V2 on 2026-06-13): a missing member + * defaults to {@code false}, any string coerces to {@code true}, and + * explicit {@code null} or non-boolean scalars fail with + * {@code SerializationException}. + */ + private static boolean parseSendingEnabled(JsonNode enabledNode) { + if (enabledNode.isMissingNode()) { + return false; + } + if (enabledNode.isBoolean()) { + return enabledNode.booleanValue(); + } + if (enabledNode.isTextual()) { + return true; + } + if (enabledNode.isNull()) { + throw new AwsException("SerializationException", null, 400); + } + if (enabledNode.isNumber()) { + throw new AwsException("SerializationException", + "NUMBER_VALUE can not be converted to a Boolean", 400); + } + throw unexpectedStartError(enabledNode); + } + + private static AwsException unexpectedStartError(JsonNode node) { + if (node.isArray()) { + return new AwsException("SerializationException", + "Start of list found where not expected", 400); + } + return new AwsException("SerializationException", + "Start of structure or map found where not expected.", 400); + } + /** * Parse a V2 SES {@code Content.Simple.Headers} / {@code Content.Template.Headers} array * (additional message headers, elements use {@code Name}/{@code Value}). Returns an empty diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index 94b22797af..b5046c2595 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -626,6 +626,23 @@ public class SesService { validateTag(tag); } } + if (configSet.getSuppressionOptions() != null + && configSet.getSuppressionOptions().getSuppressedReasons() != null) { + for (String reason : configSet.getSuppressionOptions().getSuppressedReasons()) { + if (reason == null) { + throw new AwsException("BadRequestException", + invalidSuppressionReasonMessage(null), 400); + } + if (!isValidConfigSetSuppressionReason(reason)) { + throw new AwsException("BadRequestException", + "1 validation error detected: Value at " + + "'suppressionOptions.suppressedReasons' failed to satisfy " + + "constraint: Member must satisfy constraint: " + + "[Member must satisfy enum value set: [BOUNCE, COMPLAINT]]", + 400); + } + } + } if (configSetStore.get(key).isPresent()) { throw new AwsException("ConfigurationSetAlreadyExists", "Configuration set " + configSet.getName() + " already exists.", 400); @@ -1336,15 +1353,26 @@ public class SesService { * V2 SES uses a different, simpler natural-language sentence on this * endpoint than on the three older suppression APIs above: * "Reason is invalid, must be one of [BOUNCE, COMPLAINT]." - * (Verified against real AWS V2 SES on 2026-06-03.) + * (Verified against real AWS V2 SES on 2026-06-03.) CreateConfigurationSet + * reports the constraint-style validation message for invalid non-null + * values but falls back to this sentence for null elements, matching AWS + * (verified 2026-06-13); see {@link #createConfigurationSet}. */ private static void validateConfigSetSuppressionReason(String reason) { - if (reason == null || (!"BOUNCE".equals(reason) && !"COMPLAINT".equals(reason))) { + if (!isValidConfigSetSuppressionReason(reason)) { throw new AwsException("BadRequestException", - "Reason " + reason + " is invalid, must be one of [BOUNCE, COMPLAINT].", 400); + invalidSuppressionReasonMessage(reason), 400); } } + private static boolean isValidConfigSetSuppressionReason(String reason) { + return "BOUNCE".equals(reason) || "COMPLAINT".equals(reason); + } + + private static String invalidSuppressionReasonMessage(String reason) { + return "Reason " + reason + " is invalid, must be one of [BOUNCE, COMPLAINT]."; + } + static void validateTag(Tag tag) { if (tag == null) { throw new AwsException("InvalidParameterValue", "Tag must not be null.", 400); diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java index a08fc89c12..93c446bdd9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesConfigurationSetV2IntegrationTest.java @@ -320,4 +320,348 @@ class SesConfigurationSetV2IntegrationTest { .body("Tags.find { it.Key == 'team' }.Value", equalTo("platform")) .body("Tags.find { it.Key == 'env' }.Value", equalTo("stg")); } + + @Test + @Order(17) + void createConfigurationSet_inlineOptions_echoedOnGet() { + // AWS echoes inline options set at create time verbatim + // (verified against real AWS SES V2 on 2026-06-12). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-inline-options", + "SendingOptions": {"SendingEnabled": false}, + "SuppressionOptions": {"SuppressedReasons": ["BOUNCE"]} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-inline-options") + .then() + .statusCode(200) + .body("SendingOptions.SendingEnabled", equalTo(false)) + .body("SuppressionOptions.SuppressedReasons", hasSize(1)) + .body("SuppressionOptions.SuppressedReasons", hasItem("BOUNCE")); + } + + @Test + @Order(18) + void createConfigurationSet_invalidSuppressionReason_rejectedWithoutCreating() { + // Unlike PutConfigurationSetSuppressionOptions, AWS reports the + // constraint-style validation message on this endpoint, even for + // multiple invalid values (verified against real AWS SES V2 on + // 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-bad-reason", + "SuppressionOptions": {"SuppressedReasons": ["INVALID"]} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("1 validation error detected: Value at " + + "'suppressionOptions.suppressedReasons' failed to satisfy constraint: " + + "Member must satisfy constraint: " + + "[Member must satisfy enum value set: [BOUNCE, COMPLAINT]]")); + + // Validation happens before the store write, so the set must not exist. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-bad-reason") + .then() + .statusCode(404) + .body("__type", equalTo("NotFoundException")); + } + + @Test + @Order(19) + void createConfigurationSet_stringSendingEnabled_coercesToTrue() { + // AWS accepts any string for SendingEnabled and stores true + // (verified against real AWS SES V2 on 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-string-sending", + "SendingOptions": {"SendingEnabled": "yes"} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-string-sending") + .then() + .statusCode(200) + .body("SendingOptions.SendingEnabled", equalTo(true)); + } + + @Test + @Order(20) + void createConfigurationSet_emptySendingOptions_defaultsToFalse() { + // AWS treats a missing SendingEnabled member inside a present + // SendingOptions block as false, unlike a fully absent block which + // defaults to true (verified against real AWS SES V2 on 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-empty-sending", + "SendingOptions": {} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-empty-sending") + .then() + .statusCode(200) + .body("SendingOptions.SendingEnabled", equalTo(false)); + } + + @Test + @Order(21) + void createConfigurationSet_nullSendingEnabled_serializationError() { + // Explicit null fails AWS deserialization with a null message and the + // set is not created (verified against real AWS SES V2 on 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-null-sending", + "SendingOptions": {"SendingEnabled": null} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-null-sending") + .then() + .statusCode(404); + } + + @Test + @Order(22) + void createConfigurationSet_numberSendingEnabled_serializationError() { + // Verified against real AWS SES V2 on 2026-06-13. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-number-sending", + "SendingOptions": {"SendingEnabled": 1} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("NUMBER_VALUE can not be converted to a Boolean")); + } + + @Test + @Order(23) + void createConfigurationSet_missingSuppressedReasons_internalFailure() { + // AWS itself returns 500 InternalFailure when SuppressionOptions is + // present without SuppressedReasons, and the set is not created; + // reproduced faithfully (verified against real AWS SES V2 on + // 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-empty-suppression", + "SuppressionOptions": {} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(500) + .body("__type", equalTo("InternalFailure")) + .body("message", equalTo("An internal failure has occurred.")); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-empty-suppression") + .then() + .statusCode(404); + } + + @Test + @Order(24) + void createConfigurationSet_nonArraySuppressedReasons_serializationError() { + // Verified against real AWS SES V2 on 2026-06-13. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-string-reasons", + "SuppressionOptions": {"SuppressedReasons": "BOUNCE"} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("Expected list or null")); + } + + @Test + @Order(25) + void createConfigurationSet_emptySuppressedReasonsList_echoedOnGet() { + // An explicit empty list is stored and echoed as-is + // (verified against real AWS SES V2 on 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-empty-reasons", + "SuppressionOptions": {"SuppressedReasons": []} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(200); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-empty-reasons") + .then() + .statusCode(200) + .body("SuppressionOptions.SuppressedReasons", hasSize(0)); + } + + @Test + @Order(26) + void createConfigurationSet_nullSuppressedReason_rejectedWithPutStyleMessage() { + // A null element passes AWS deserialization ("Expected list or null") + // and fails value validation with the natural-language sentence, not + // the constraint-style message used for invalid non-null values + // (verified against real AWS SES V2 on 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-null-reason", + "SuppressionOptions": {"SuppressedReasons": ["BOUNCE", null]} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", equalTo("Reason null is invalid, must be one of [BOUNCE, COMPLAINT].")); + + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/v2/email/configuration-sets/v2-cs-null-reason") + .then() + .statusCode(404); + } + + @Test + @Order(27) + void createConfigurationSet_booleanSuppressedReason_serializationError() { + // Verified against real AWS SES V2 on 2026-06-13. + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-boolean-reason", + "SuppressionOptions": {"SuppressedReasons": [false]} + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("FALSE_VALUE can not be converted to a String")); + } + + @Test + @Order(28) + void createConfigurationSet_nonObjectOptionBlocks_serializationError() { + // "Expected null" looks odd but is the verbatim AWS response for a + // non-object option block (verified against real AWS SES V2 on + // 2026-06-13). + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-string-suppression", + "SuppressionOptions": "nope" + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("Expected null")); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "ConfigurationSetName": "v2-cs-string-options", + "SendingOptions": "nope" + } + """) + .when() + .post("/v2/email/configuration-sets") + .then() + .statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("Expected null")); + } }