diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTemplateTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTemplateTest.java index 41561a7e35..92a6a9f3b5 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTemplateTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesTemplateTest.java @@ -240,7 +240,7 @@ class SesTemplateTest { .build()) .content(EmailContent.builder() .template(Template.builder() - .templateArn("arn:aws:ses:us-east-1:000000000000:template/" + name) + .templateArn(TestFixtures.arn("ses", "000000000000", "template/" + name)) .templateData("{\"name\":\"Alice\"}") .build()) .build()) @@ -446,7 +446,7 @@ class SesTemplateTest { .source(v1Sender) .destination(d -> d.toAddresses("recipient@example.com")) .template(name) - .templateArn("arn:aws:ses:us-east-1:000000000000:template/" + name) + .templateArn(TestFixtures.arn("ses", "000000000000", "template/" + name)) .templateData("{\"name\":\"Alice\"}") .build()); assertThat(response.messageId()).isNotBlank(); diff --git a/compatibility-tests/sdk-test-python/tests/test_ses_templates.py b/compatibility-tests/sdk-test-python/tests/test_ses_templates.py index 7ef813826c..f5f0a367dd 100644 --- a/compatibility-tests/sdk-test-python/tests/test_ses_templates.py +++ b/compatibility-tests/sdk-test-python/tests/test_ses_templates.py @@ -207,7 +207,8 @@ class TestSesV2SendTemplatedEmail: ): template_name = f"pytest-arn-tpl-{unique_name}" sender = f"pytest-arn-sender-{unique_name}@example.com" - arn = f"arn:aws:ses:us-east-1:000000000000:template/{template_name}" + meta = sesv2_client.meta + arn = f"arn:{meta.partition}:ses:{meta.region_name}:000000000000:template/{template_name}" try: sesv2_client.create_email_identity(EmailIdentity=sender) sesv2_client.create_email_template( @@ -406,7 +407,8 @@ class TestSesV1SendTemplatedEmail: accepts both and resolves via the name.""" template_name = f"pytest-v1-arn-tpl-{unique_name}" sender = f"pytest-v1-arn-sender-{unique_name}@example.com" - arn = f"arn:aws:ses:us-east-1:000000000000:template/{template_name}" + meta = ses_client.meta + arn = f"arn:{meta.partition}:ses:{meta.region_name}:000000000000:template/{template_name}" try: ses_client.verify_email_identity(EmailAddress=sender) ses_client.create_template( diff --git a/docs/services/ses.md b/docs/services/ses.md index 51b5a425e1..0b9f861793 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -304,7 +304,8 @@ curl $AWS_ENDPOINT_URL/_aws/ses - An email address identity is verified as soon as it is created; no inbox verification flow is required. A domain identity stays `Pending` until its DKIM CNAME records exist in a Route 53 hosted zone in Floci, then becomes `Success`. - `SendEmail`, `SendRawEmail`, `SendTemplatedEmail` and `SendBulkTemplatedEmail`, and v2 `SendEmail` and `SendBulkEmail`, require verified senders, as on AWS ([SendEmail](https://docs.aws.amazon.com/ses/latest/APIReference/API_SendEmail.html), [Email sending errors](https://docs.aws.amazon.com/ses/latest/dg/troubleshoot-error-messages.html)): each address used as a "From", "Source", "Sender" or "Return-Path" address must be a verified email address identity or an address in a verified domain identity or one of its subdomains. That is the `Source` or `FromEmailAddress` and the `ReturnPath` or `FeedbackForwardingEmailAddress`, plus, on a raw message, every mailbox of its `From` header, its `Sender` and its `Return-Path` header. A v2 raw send reads the `From` header only in place of a missing `FromEmailAddress` and accepts any `From` header when one is given (probe-confirmed), so the header is checked only then. Every other address present is checked; whether AWS still checks a `Return-Path` header that the request's return path overrides has not been probed. One `MessageRejected` lists every unverified address ("Email address is not verified. The following identities failed the check in region US-EAST-1: a@example.com, b@example.com") and nothing is captured or relayed. An address covered by a sending-authorization ARN of another account (`SourceArn`, `FromArn`, `ReturnPathArn`, their `X-SES-*-ARN` headers, or v2 `FromEmailAddressIdentityArn` and `FeedbackForwardingEmailAddressIdentityArn`) is not checked, since it is the owner's identity; on a raw send the source ARN also covers the `From` and return-path addresses that have no ARN of their own. A bulk send checks its sender and return path once and rejects the whole call; when no entry has a recipient it checks neither and every entry fails on its own (probe-confirmed on v2 `SendBulkEmail` and v1 `SendBulkTemplatedEmail`). On v2 `SendEmail` and `SendBulkEmail`, and v1 `SendTemplatedEmail` and `SendBulkTemplatedEmail`, an unknown configuration set is reported before the check and an unknown stored template only after it (probe-confirmed). The verification codes and invitations Cognito sends are not affected. -- `SendBulkTemplatedEmail` reads every `Destinations.member.N` up to the highest N. A missing `Template` (with no `TemplateArn`), missing `Destinations`, and an entry without a `Destination` or a skipped N are model violations, answered together as one 400 `ValidationError` ("2 validation errors detected: Value at 'template' failed to satisfy constraint: Member must not be null; Value at 'destinations.2.member.destination' failed to satisfy constraint: Member must not be null") ahead of every other check of the request, the address syntax and the destination count included (probe-confirmed). Floci lists the skipped entries of the first 1000 indexes only; a higher index fails the destination count. On both bulk sends an entry whose address lists are empty answers "Missing required header 'To'." on its own when another entry has a recipient, and when none has one every entry answers "Destination cannot be empty" (probe-confirmed). A `DefaultTags` or `ReplacementTags` tag without a `Name` or a `Value` is a model violation too, a skipped tag index listing both, and the top-level members are listed ahead of the nested ones ('template' before 'defaultTags.1.member.name'). A missing or empty `DefaultTemplateData` is a 400 `InvalidParameterValue` ("The default template data does not exist"), answered after the `Source` syntax and the destination count and before the template lookup and the sender verification (probe-confirmed). On `SendBulkTemplatedEmail` an entry with more than 50 recipients ("Recipient count exceeds 50."), template data that is not a JSON object ("The template data is invalid") or a tag with an empty name ("The tag name must be specified.") fails on its own with `InvalidParameterValue`. Invalid `DefaultTemplateData` or an empty `DefaultTags` name fails every entry, and within one entry an address syntax error wins over an empty tag name, which wins over invalid template data, which wins over the recipient count (probe-confirmed). v2 `SendBulkEmail` still rejects the whole call for more than 50 recipients in a destination (not probed). +- `SendBulkTemplatedEmail` reads every `Destinations.member.N` up to the highest N. A missing `Template` (whatever `TemplateArn` holds), missing `Destinations`, and an entry without a `Destination` or a skipped N are model violations, answered together as one 400 `ValidationError` ("2 validation errors detected: Value at 'template' failed to satisfy constraint: Member must not be null; Value at 'destinations.2.member.destination' failed to satisfy constraint: Member must not be null") ahead of every other check of the request, the address syntax and the destination count included (probe-confirmed). Floci lists the skipped entries of the first 1000 indexes only; a higher index fails the destination count. On both bulk sends an entry whose address lists are empty answers "Missing required header 'To'." on its own when another entry has a recipient, and when none has one every entry answers "Destination cannot be empty" (probe-confirmed). A `DefaultTags` or `ReplacementTags` tag without a `Name` or a `Value` is a model violation too, a skipped tag index listing both, and the top-level members are listed ahead of the nested ones ('template' before 'defaultTags.1.member.name'). A missing or empty `DefaultTemplateData` is a 400 `InvalidParameterValue` ("The default template data does not exist"), answered after the `Source` syntax and the destination count and before the template lookup and the sender verification (probe-confirmed). On `SendBulkTemplatedEmail` an entry with more than 50 recipients ("Recipient count exceeds 50."), template data that is not a JSON object ("The template data is invalid") or a tag with an empty name ("The tag name must be specified.") fails on its own with `InvalidParameterValue`. Invalid `DefaultTemplateData` or an empty `DefaultTags` name fails every entry, and within one entry an address syntax error wins over an empty tag name, which wins over invalid template data, which wins over the recipient count (probe-confirmed). v2 `SendBulkEmail` still rejects the whole call for more than 50 recipients in a destination (not probed). +- A `TemplateArn` on v1 `SendTemplatedEmail` and `SendBulkTemplatedEmail` and v2 `SendEmail` and `SendBulkEmail` must be in the request's partition and region, name the `ses` service and start its resource with `template/`; its account is not checked. Each mismatch has its own message ("Invalid template ARN . Template must be in region "), a value that does not start with `arn:` answers "ARNs must start with 'arn:': ", and an ARN beside a template name must name the same template ("The SES resource '' does not align with the given ARN ''."). The checks run before the sender verification; v1 answers `InvalidParameterValue` and v2 `BadRequestException` (probe-confirmed). The v1 sends require `Template` even beside a `TemplateArn`, and an empty one answers "Template cannot be empty" ahead of the ARN checks; `SendBulkTemplatedEmail` answers the same for a whitespace-only `Template` (probe-confirmed). - `SendEmail` stores the text body or the HTML body as the captured message body. - `SetIdentityNotificationTopic` publishes to the configured topic on a Bounce/Complaint/Delivery event (triggered via the mailbox simulator addresses or the suppression list), independent of any configuration set. The payload uses the legacy format (`notificationType`, no `mail.tags`, headers only when `SetIdentityHeadersInNotificationsEnabled` is on). The topic is checked when it is set: one that does not exist in the identity's region, or whose policy does not let SES publish under IAM enforcement, is refused with `InvalidParameterValue`. A topic of another account that exists in Floci and grants SES is accepted. - Identity (sending authorization) policies are stored and returned as metadata: the policy document, the per-identity limit of 20, and the create/update/delete error shapes match AWS, but Floci does not evaluate policy authorization (Principal-account existence, Resource-ARN match) or gate sending on it. A send naming another account's identity ARN skips the verified-sender check for the addresses it covers, without looking that identity up or reading its policy. diff --git a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java index 93383cf467..37b9ca0a4d 100644 --- a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java +++ b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java @@ -648,13 +648,10 @@ public class SesQueryHandler { String templateArn = getParam(params, "TemplateArn"); String templateDataRaw = getParam(params, "TemplateData"); - boolean hasName = templateName != null && !templateName.isBlank(); - boolean hasArn = templateArn != null && !templateArn.isBlank(); - if (!hasName && !hasArn) { - throw new AwsException("InvalidParameterValue", - "Template or TemplateArn is required.", 400); + if (templateName == null) { + throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNullWithoutValue("template")); } - String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn); + String resolvedName = sendTemplateName(templateName.isEmpty(), templateName, templateArn, region); JsonNode templateData = parseTemplateData(templateDataRaw); String configurationSetName = getParam(params, "ConfigurationSetName"); @@ -786,7 +783,7 @@ public class SesQueryHandler { // top-level members before the nested ones ('template' ahead of 'defaultTags.1.member.name'), // each group in the model's member order (probe-confirmed). List violations = new ArrayList<>(); - if (templateName == null && templateArn == null) { + if (templateName == null) { violations.add(SmithyValidationErrors.notNullWithoutValue("template")); } if (!params.containsKey("Destinations") && !hasParamWithPrefix(params, "Destinations.")) { @@ -812,13 +809,7 @@ public class SesQueryHandler { List replyToAddresses = extractMembers(params, "ReplyToAddresses"); String defaultDataRaw = getParam(params, "DefaultTemplateData"); - boolean hasName = templateName != null && !templateName.isBlank(); - boolean hasArn = templateArn != null && !templateArn.isBlank(); - if (!hasName && !hasArn) { - throw new AwsException("InvalidParameterValue", - "Template or TemplateArn is required.", 400); - } - String resolvedName = hasName ? templateName : SesTemplateService.templateNameFromArn(templateArn); + String resolvedName = sendTemplateName(templateName.isBlank(), templateName, templateArn, region); int lastIndex = destinations.last(); if (lastIndex == 0) { @@ -1738,6 +1729,19 @@ public class SesQueryHandler { } } + // The templated sends require Template whatever TemplateArn holds, word an empty one ahead of + // the ARN checks, and take a TemplateArn only beside the Template it names (probe-confirmed). + // SendBulkTemplatedEmail counts a whitespace-only Template as empty; SendTemplatedEmail does not. + private static String sendTemplateName(boolean empty, String templateName, String templateArn, String region) { + if (empty) { + throw new AwsException("InvalidParameterValue", "Template cannot be empty", 400); + } + if (templateArn != null && !templateArn.isBlank()) { + SesTemplateService.requireNameAlignsWithArn(templateName, templateArn, region); + } + return templateName; + } + private String getParam(MultivaluedMap params, String name) { return params.getFirst(name); } diff --git a/src/main/java/io/floci/aws/services/ses/SesSendController.java b/src/main/java/io/floci/aws/services/ses/SesSendController.java index 7a64644f7f..5995dd8f11 100644 --- a/src/main/java/io/floci/aws/services/ses/SesSendController.java +++ b/src/main/java/io/floci/aws/services/ses/SesSendController.java @@ -451,16 +451,12 @@ public class SesSendController { } String storedName(String region) { - return hasName() ? name : SesTemplateService.sendTemplateNameFromArn(arn, region); + return hasName() ? name : SesTemplateService.templateNameFromArn(arn, region); } - // AWS accepts a TemplateName beside a TemplateArn when both name the same template, and - // reports a malformed ARN first (probe-confirmed on SendEmail and SendBulkEmail). void requireNameAlignsWithArn(String region) { - if (hasName() && hasArn() - && !name.equals(SesTemplateService.sendTemplateNameFromArn(arn, region))) { - throw new AwsException("BadRequestException", "The SES resource '" + name - + "' does not align with the given ARN '" + arn + "'.", 400); + if (hasName() && hasArn()) { + SesTemplateService.requireNameAlignsWithArn(name, arn, region); } } } diff --git a/src/main/java/io/floci/aws/services/ses/SesTemplateService.java b/src/main/java/io/floci/aws/services/ses/SesTemplateService.java index 278e05e81e..f5a19e5978 100644 --- a/src/main/java/io/floci/aws/services/ses/SesTemplateService.java +++ b/src/main/java/io/floci/aws/services/ses/SesTemplateService.java @@ -237,34 +237,11 @@ public class SesTemplateService implements SesTaggable { } /** - * Extracts the template name from an SES template ARN of the form - * {@code arn:aws:ses:::template/}. Region and - * account segments are not validated; only the {@code template/} - * suffix is required. + * The template a send's {@code TemplateArn} names, on v1 and v2 alike. SES checks the ARN's + * partition, service, region and resource type, each with a message of its own, and not its + * account (probe-confirmed). The order among several mismatches at once is not probed. */ - public static String templateNameFromArn(String arn) { - if (arn == null || arn.isBlank()) { - throw new AwsException("InvalidParameterValue", "TemplateArn is required.", 400); - } - int marker = arn.indexOf(":template/"); - if (!AwsArnUtils.looksLikeArn(arn) || marker < 0) { - throw new AwsException("InvalidParameterValue", - "TemplateArn is not a valid SES template ARN: " + arn, 400); - } - String name = arn.substring(marker + ":template/".length()); - if (name.isEmpty()) { - throw new AwsException("InvalidParameterValue", - "TemplateArn is missing a template name: " + arn, 400); - } - return name; - } - - /** - * The template a v2 send's {@code TemplateArn} names. SES checks the ARN's partition, service, - * region and resource type, each with a message of its own, and not its account - * (probe-confirmed). The order among several mismatches at once is not probed. - */ - public static String sendTemplateNameFromArn(String arn, String region) { + public static String templateNameFromArn(String arn, String region) { if (!AwsArnUtils.looksLikeArn(arn)) { throw new AwsException("InvalidParameterValue", "ARNs must start with 'arn:': " + arn, 400); } @@ -295,6 +272,17 @@ public class SesTemplateService implements SesTaggable { return name; } + /** + * A send may name its template beside a {@code TemplateArn} only when both name the same + * template; a malformed ARN is reported first (probe-confirmed on both protocols). + */ + public static void requireNameAlignsWithArn(String name, String arn, String region) { + if (!name.equals(templateNameFromArn(arn, region))) { + throw new AwsException("InvalidParameterValue", "The SES resource '" + name + + "' does not align with the given ARN '" + arn + "'.", 400); + } + } + private static AwsException invalidTemplateArn(String arn, String reason) { return new AwsException("InvalidParameterValue", "Invalid template ARN <" + arn + ">. " + reason, 400); } diff --git a/src/test/java/io/floci/aws/services/ses/SesTemplateArnTest.java b/src/test/java/io/floci/aws/services/ses/SesTemplateArnTest.java new file mode 100644 index 0000000000..5f6f5d08c3 --- /dev/null +++ b/src/test/java/io/floci/aws/services/ses/SesTemplateArnTest.java @@ -0,0 +1,95 @@ +package io.floci.aws.services.ses; + +import io.floci.aws.core.common.AwsException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * The template ARN parser and the name/ARN alignment check that the v1 and v2 sends share. + */ +class SesTemplateArnTest { + + private static final String REGION = "us-west-2"; + + @Test + void templateNameFromArn_returnsTheRelativeIdAfterTemplate() { + assertEquals("welcome", + SesTemplateService.templateNameFromArn("arn:aws:ses:us-west-2:000000000000:template/welcome", REGION)); + } + + @Test + void templateNameFromArn_ignoresTheAccount() { + assertEquals("welcome", + SesTemplateService.templateNameFromArn("arn:aws:ses:us-west-2:999999999999:template/welcome", REGION)); + } + + @Test + void templateNameFromArn_expectsThePartitionOfTheRegion() { + assertEquals("welcome", SesTemplateService.templateNameFromArn( + "arn:aws-cn:ses:cn-north-1:000000000000:template/welcome", "cn-north-1")); + assertInvalid("arn:aws:ses:cn-north-1:000000000000:template/welcome", "cn-north-1", + "Invalid template ARN . " + + "Partition should be "); + } + + static Stream invalidArns() { + return Stream.of( + Arguments.of("arn:aws-cn:ses:us-west-2:000000000000:template/welcome", + "Invalid template ARN . " + + "Partition should be "), + Arguments.of("arn:aws:s3:us-west-2:000000000000:template/welcome", + "Invalid template ARN . " + + "Vendor should be "), + Arguments.of("arn:aws:ses:us-east-1:000000000000:template/welcome", + "Invalid template ARN . " + + "Template must be in region "), + Arguments.of("arn:aws:ses:us-west-2:000000000000:configuration-set/welcome", + "Invalid template ARN . " + + "Relative ID should start with