mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): check the TemplateArn of a v1 templated send as SES does (#9131)
SendTemplatedEmail and SendBulkTemplatedEmail now check TemplateArn with the parser the v2 sends use: partition, service, region and resource type each answer their own "Invalid template ARN" message, a value without "arn:" and an empty template name are rejected, and the account is not checked. The v1-only parser that looked for ":template/" is removed, and the name/ARN alignment check moves into SesTemplateService for both protocols; the v2 controller's remap keeps its BadRequestException. Both v1 sends require Template whatever TemplateArn holds, as the model does, and answer "Template cannot be empty" ahead of the ARN checks; SendBulkTemplatedEmail treats a whitespace-only Template as empty. The Java and Python compatibility tests build the template ARN in the client's partition and region.
This commit is contained in:
1 parent
9b19f17fe7
commit
2b9ed9cd55
9 files changed
+288
-57
No files matched your search
@@ -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 <arn:aws:ses:cn-north-1:000000000000:template/welcome>. "
|
||||
+ "Partition should be <aws-cn>");
|
||||
}
|
||||
|
||||
static Stream<Arguments> invalidArns() {
|
||||
return Stream.of(
|
||||
Arguments.of("arn:aws-cn:ses:us-west-2:000000000000:template/welcome",
|
||||
"Invalid template ARN <arn:aws-cn:ses:us-west-2:000000000000:template/welcome>. "
|
||||
+ "Partition should be <aws>"),
|
||||
Arguments.of("arn:aws:s3:us-west-2:000000000000:template/welcome",
|
||||
"Invalid template ARN <arn:aws:s3:us-west-2:000000000000:template/welcome>. "
|
||||
+ "Vendor should be <ses>"),
|
||||
Arguments.of("arn:aws:ses:us-east-1:000000000000:template/welcome",
|
||||
"Invalid template ARN <arn:aws:ses:us-east-1:000000000000:template/welcome>. "
|
||||
+ "Template must be in region <us-west-2>"),
|
||||
Arguments.of("arn:aws:ses:us-west-2:000000000000:configuration-set/welcome",
|
||||
"Invalid template ARN <arn:aws:ses:us-west-2:000000000000:configuration-set/welcome>. "
|
||||
+ "Relative ID should start with <template/>"),
|
||||
Arguments.of("arn:aws:ses:us-west-2:000000000000:template/", "The template name must be specified."),
|
||||
Arguments.of("not-an-arn", "ARNs must start with 'arn:': not-an-arn"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("invalidArns")
|
||||
void templateNameFromArn_rejectsWithTheProbedMessage(String arn, String message) {
|
||||
assertInvalid(arn, REGION, message);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireNameAlignsWithArn_acceptsTheNameTheArnNames() {
|
||||
assertDoesNotThrow(() -> SesTemplateService.requireNameAlignsWithArn("welcome",
|
||||
"arn:aws:ses:us-west-2:000000000000:template/welcome", REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireNameAlignsWithArn_rejectsAnotherName() {
|
||||
String arn = "arn:aws:ses:us-west-2:000000000000:template/other";
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> SesTemplateService.requireNameAlignsWithArn("welcome", arn, REGION));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals("The SES resource 'welcome' does not align with the given ARN '" + arn + "'.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireNameAlignsWithArn_reportsAMalformedArnFirst() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> SesTemplateService.requireNameAlignsWithArn("welcome", "not-an-arn", REGION));
|
||||
assertEquals("ARNs must start with 'arn:': not-an-arn", e.getMessage());
|
||||
}
|
||||
|
||||
private static void assertInvalid(String arn, String region, String message) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> SesTemplateService.templateNameFromArn(arn, region));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -284,7 +284,7 @@ class SesTemplateV1IntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void sendTemplatedEmail_withTemplateArn_resolvesStoredTemplate() {
|
||||
void sendTemplatedEmail_withTemplateArnOnly_isModelViolation() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
@@ -318,8 +318,10 @@ class SesTemplateV1IntegrationTest {
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<MessageId>"));
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at 'template' "
|
||||
+ "failed to satisfy constraint: Member must not be null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -350,12 +352,14 @@ class SesTemplateV1IntegrationTest {
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-arn-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("Template", "v1-arn-welcome")
|
||||
.formParam("TemplateArn", "not-an-arn")
|
||||
.formParam("TemplateData", "{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("ErrorResponse.Error.Message", equalTo("ARNs must start with 'arn:': not-an-arn"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
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 org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* How the v1 templated sends answer their Template and TemplateArn members. The checks and
|
||||
* messages match the v2 sends' and are probe-confirmed on both operations; the sender is never
|
||||
* verified, so a request whose template members pass reaches the sender check.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesV1TemplateArnIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
private static final String UNVERIFIED = "sender@unverified-v1-template-arn.floci.test";
|
||||
private static final String NAME = "v1-arn-ghost";
|
||||
private static final String TEMPLATE_REQUIRED =
|
||||
"1 validation error detected: Value at 'template' failed to satisfy constraint: Member must not be null";
|
||||
|
||||
private static String arn(String partition, String service, String region, String account, String resource) {
|
||||
return "arn:" + partition + ":" + service + ":" + region + ":" + account + ":" + resource;
|
||||
}
|
||||
|
||||
private static String arn(String partition, String service, String region, String resource) {
|
||||
return arn(partition, service, region, "000000000000", resource);
|
||||
}
|
||||
|
||||
private static ValidatableResponse send(String operation, String template, String templateArn) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", operation)
|
||||
.formParam("Source", UNVERIFIED);
|
||||
if ("SendTemplatedEmail".equals(operation)) {
|
||||
request.formParam("Destination.ToAddresses.member.1", "success@simulator.amazonses.com")
|
||||
.formParam("TemplateData", "{}");
|
||||
} else {
|
||||
request.formParam("Destinations.member.1.Destination.ToAddresses.member.1",
|
||||
"success@simulator.amazonses.com")
|
||||
.formParam("DefaultTemplateData", "{}");
|
||||
}
|
||||
if (template != null) {
|
||||
request.formParam("Template", template);
|
||||
}
|
||||
if (templateArn != null) {
|
||||
request.formParam("TemplateArn", templateArn);
|
||||
}
|
||||
return request.when().post("/").then();
|
||||
}
|
||||
|
||||
private static void assertError(ValidatableResponse response, String code, String message) {
|
||||
response
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo(code))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message));
|
||||
}
|
||||
|
||||
static Stream<Arguments> invalidTemplateArns() {
|
||||
String otherRegion = arn("aws", "ses", "eu-west-1", "template/" + NAME);
|
||||
String otherPartition = arn("aws-cn", "ses", "us-east-1", "template/" + NAME);
|
||||
String otherService = arn("aws", "s3", "us-east-1", "template/" + NAME);
|
||||
String otherResource = arn("aws", "ses", "us-east-1", "configuration-set/" + NAME);
|
||||
String otherTemplate = arn("aws", "ses", "us-east-1", "template/v1-arn-other");
|
||||
return Stream.of("SendTemplatedEmail", "SendBulkTemplatedEmail").flatMap(operation -> Stream.of(
|
||||
Arguments.of(operation, otherRegion,
|
||||
"Invalid template ARN <" + otherRegion + ">. Template must be in region <us-east-1>"),
|
||||
Arguments.of(operation, otherPartition,
|
||||
"Invalid template ARN <" + otherPartition + ">. Partition should be <aws>"),
|
||||
Arguments.of(operation, otherService,
|
||||
"Invalid template ARN <" + otherService + ">. Vendor should be <ses>"),
|
||||
Arguments.of(operation, otherResource,
|
||||
"Invalid template ARN <" + otherResource + ">. Relative ID should start with <template/>"),
|
||||
Arguments.of(operation, "not-an-arn", "ARNs must start with 'arn:': not-an-arn"),
|
||||
Arguments.of(operation, arn("aws", "ses", "us-east-1", "template/"),
|
||||
"The template name must be specified."),
|
||||
Arguments.of(operation, otherTemplate,
|
||||
"The SES resource '" + NAME + "' does not align with the given ARN '" + otherTemplate + "'.")));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} {1}")
|
||||
@MethodSource("invalidTemplateArns")
|
||||
void invalidTemplateArn_isRejectedBeforeTheSenderCheck(String operation, String templateArn, String message) {
|
||||
assertError(send(operation, NAME, templateArn), "InvalidParameterValue", message);
|
||||
}
|
||||
|
||||
static Stream<Arguments> acceptedTemplateArns() {
|
||||
return Stream.of("SendTemplatedEmail", "SendBulkTemplatedEmail").flatMap(operation -> Stream.of(
|
||||
Arguments.of(operation, arn("aws", "ses", "us-east-1", "template/" + NAME)),
|
||||
Arguments.of(operation, arn("aws", "ses", "us-east-1", "999999999999", "template/" + NAME))));
|
||||
}
|
||||
|
||||
// SES does not check the account of a template ARN.
|
||||
@ParameterizedTest(name = "{0} {1}")
|
||||
@MethodSource("acceptedTemplateArns")
|
||||
void templateArnNamingTheTemplate_reachesTheSenderCheck(String operation, String templateArn) {
|
||||
send(operation, NAME, templateArn)
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("MessageRejected"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"SendTemplatedEmail", "SendBulkTemplatedEmail"})
|
||||
void templateArnWithoutTemplate_isModelViolation(String operation) {
|
||||
assertError(send(operation, null, arn("aws", "ses", "eu-west-1", "template/" + NAME)),
|
||||
"ValidationError", TEMPLATE_REQUIRED);
|
||||
}
|
||||
|
||||
// An empty Template is reported ahead of the TemplateArn checks (probed on SendTemplatedEmail).
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"SendTemplatedEmail", "SendBulkTemplatedEmail"})
|
||||
void emptyTemplate_isRejectedBeforeTheTemplateArn(String operation) {
|
||||
assertError(send(operation, "", "not-an-arn"), "InvalidParameterValue", "Template cannot be empty");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "not-an-arn"})
|
||||
void sendBulkTemplatedEmail_whitespaceTemplate_isEmpty(String templateArn) {
|
||||
assertError(send("SendBulkTemplatedEmail", " ", templateArn.isEmpty() ? null : templateArn),
|
||||
"InvalidParameterValue", "Template cannot be empty");
|
||||
}
|
||||
|
||||
// SendTemplatedEmail does not count a whitespace-only Template as empty.
|
||||
@Test
|
||||
void sendTemplatedEmail_whitespaceTemplate_mustAlignWithTheTemplateArn() {
|
||||
String templateArn = arn("aws", "ses", "us-east-1", "template/" + NAME);
|
||||
assertError(send("SendTemplatedEmail", " ", templateArn), "InvalidParameterValue",
|
||||
"The SES resource ' ' does not align with the given ARN '" + templateArn + "'.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user