mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ses): validate the SNS topic of an event destination (#8275)
This commit is contained in:
1 parent
e4d7061eff
commit
2cb54dad8f
17 files changed
+542
-70
No files matched your search
+32
-15
@@ -32,6 +32,7 @@ import software.amazon.awssdk.services.sesv2.model.GetConfigurationSetEventDesti
|
||||
import software.amazon.awssdk.services.sesv2.model.GetConfigurationSetEventDestinationsResponse;
|
||||
import software.amazon.awssdk.services.sesv2.model.SnsDestination;
|
||||
import software.amazon.awssdk.services.sesv2.model.UpdateConfigurationSetEventDestinationRequest;
|
||||
import software.amazon.awssdk.services.sns.SnsClient;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
@@ -52,11 +53,12 @@ class SesConfigurationSetEventDestinationTest {
|
||||
|
||||
private static SesClient sesV1;
|
||||
private static SesV2Client sesV2;
|
||||
private static SnsClient sns;
|
||||
private static String v1CsName;
|
||||
private static String v2CsName;
|
||||
private static final String ED_NAME = "ed-sns";
|
||||
private static final String TOPIC_ARN = TestFixtures.arn("sns", "000000000000", "ses-events");
|
||||
private static final String TOPIC_ARN_2 = TestFixtures.arn("sns", "000000000000", "ses-events-2");
|
||||
private static String topicArn;
|
||||
private static String topicArn2;
|
||||
private static final String DEFAULT_BUS_ARN = TestFixtures.arn("events", "000000000000", "event-bus/default");
|
||||
private static final String CUSTOM_BUS_ARN = TestFixtures.arn("events", "000000000000", "event-bus/custom");
|
||||
|
||||
@@ -64,7 +66,11 @@ class SesConfigurationSetEventDestinationTest {
|
||||
static void setup() {
|
||||
sesV1 = TestFixtures.sesClient();
|
||||
sesV2 = TestFixtures.sesV2Client();
|
||||
// SES checks an SNS destination's topic when the destination is set.
|
||||
sns = TestFixtures.snsClient();
|
||||
String suffix = TestFixtures.uniqueName();
|
||||
topicArn = sns.createTopic(b -> b.name("sdk-ed-t-" + suffix)).topicArn();
|
||||
topicArn2 = sns.createTopic(b -> b.name("sdk-ed-t2-" + suffix)).topicArn();
|
||||
v1CsName = "sdk-v1-ed-cs-" + suffix;
|
||||
v2CsName = "sdk-v2-ed-cs-" + suffix;
|
||||
sesV1.createConfigurationSet(software.amazon.awssdk.services.ses.model.CreateConfigurationSetRequest.builder()
|
||||
@@ -77,6 +83,17 @@ class SesConfigurationSetEventDestinationTest {
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (sns != null) {
|
||||
for (String arn : new String[] {topicArn, topicArn2}) {
|
||||
try {
|
||||
sns.deleteTopic(b -> b.topicArn(arn));
|
||||
} catch (Exception ignored) {
|
||||
// Setup may have failed before the topic existed, and a topic left behind
|
||||
// has a name no other class uses, so it cannot change another result.
|
||||
}
|
||||
}
|
||||
sns.close();
|
||||
}
|
||||
if (sesV2 != null) {
|
||||
try {
|
||||
sesV2.deleteConfigurationSetEventDestination(DeleteConfigurationSetEventDestinationRequest.builder()
|
||||
@@ -117,7 +134,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.eventDestination(EventDestinationDefinition.builder()
|
||||
.enabled(true)
|
||||
.matchingEventTypes(EventType.SEND, EventType.BOUNCE)
|
||||
.snsDestination(SnsDestination.builder().topicArn(TOPIC_ARN).build())
|
||||
.snsDestination(SnsDestination.builder().topicArn(topicArn).build())
|
||||
.build())
|
||||
.build());
|
||||
}
|
||||
@@ -135,7 +152,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
assertThat(response.eventDestinations().get(0).enabled()).isTrue();
|
||||
assertThat(response.eventDestinations().get(0).matchingEventTypes())
|
||||
.contains(EventType.SEND, EventType.BOUNCE);
|
||||
assertThat(response.eventDestinations().get(0).snsDestination().topicArn()).isEqualTo(TOPIC_ARN);
|
||||
assertThat(response.eventDestinations().get(0).snsDestination().topicArn()).isEqualTo(topicArn);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,7 +164,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.eventDestinationName(ED_NAME)
|
||||
.eventDestination(EventDestinationDefinition.builder()
|
||||
.matchingEventTypes(EventType.SEND)
|
||||
.snsDestination(SnsDestination.builder().topicArn(TOPIC_ARN).build())
|
||||
.snsDestination(SnsDestination.builder().topicArn(topicArn).build())
|
||||
.build())
|
||||
.build()))
|
||||
.isInstanceOf(AwsServiceException.class)
|
||||
@@ -164,7 +181,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.eventDestination(EventDestinationDefinition.builder()
|
||||
.enabled(false)
|
||||
.matchingEventTypes(EventType.DELIVERY)
|
||||
.snsDestination(SnsDestination.builder().topicArn(TOPIC_ARN_2).build())
|
||||
.snsDestination(SnsDestination.builder().topicArn(topicArn2).build())
|
||||
.build())
|
||||
.build());
|
||||
|
||||
@@ -175,7 +192,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
assertThat(response.eventDestinations()).hasSize(1);
|
||||
assertThat(response.eventDestinations().get(0).enabled()).isFalse();
|
||||
assertThat(response.eventDestinations().get(0).matchingEventTypes()).contains(EventType.DELIVERY);
|
||||
assertThat(response.eventDestinations().get(0).snsDestination().topicArn()).isEqualTo(TOPIC_ARN_2);
|
||||
assertThat(response.eventDestinations().get(0).snsDestination().topicArn()).isEqualTo(topicArn2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -187,7 +204,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.eventDestinationName("ed-ghost")
|
||||
.eventDestination(EventDestinationDefinition.builder()
|
||||
.matchingEventTypes(EventType.SEND)
|
||||
.snsDestination(SnsDestination.builder().topicArn(TOPIC_ARN).build())
|
||||
.snsDestination(SnsDestination.builder().topicArn(topicArn).build())
|
||||
.build())
|
||||
.build()))
|
||||
.isInstanceOf(AwsServiceException.class)
|
||||
@@ -232,7 +249,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.eventDestinationName("ed-x")
|
||||
.eventDestination(EventDestinationDefinition.builder()
|
||||
.matchingEventTypes(EventType.SEND)
|
||||
.snsDestination(SnsDestination.builder().topicArn(TOPIC_ARN).build())
|
||||
.snsDestination(SnsDestination.builder().topicArn(topicArn).build())
|
||||
.build())
|
||||
.build()))
|
||||
.isInstanceOf(AwsServiceException.class)
|
||||
@@ -336,7 +353,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
software.amazon.awssdk.services.ses.model.EventType.SEND,
|
||||
software.amazon.awssdk.services.ses.model.EventType.BOUNCE)
|
||||
.snsDestination(SNSDestination.builder()
|
||||
.topicARN(TOPIC_ARN)
|
||||
.topicARN(topicArn)
|
||||
.build())
|
||||
.build())
|
||||
.build());
|
||||
@@ -360,7 +377,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
software.amazon.awssdk.services.ses.model.EventType.SEND,
|
||||
software.amazon.awssdk.services.ses.model.EventType.BOUNCE);
|
||||
assertThat(ed.snsDestination()).isNotNull();
|
||||
assertThat(ed.snsDestination().topicARN()).isEqualTo(TOPIC_ARN);
|
||||
assertThat(ed.snsDestination().topicARN()).isEqualTo(topicArn);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -385,7 +402,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.enabled(true)
|
||||
.matchingEventTypes(software.amazon.awssdk.services.ses.model.EventType.SEND)
|
||||
.snsDestination(SNSDestination.builder()
|
||||
.topicARN(TOPIC_ARN)
|
||||
.topicARN(topicArn)
|
||||
.build())
|
||||
.build())
|
||||
.build()))
|
||||
@@ -405,7 +422,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.enabled(false)
|
||||
.matchingEventTypes(software.amazon.awssdk.services.ses.model.EventType.COMPLAINT)
|
||||
.snsDestination(SNSDestination.builder()
|
||||
.topicARN(TOPIC_ARN_2)
|
||||
.topicARN(topicArn2)
|
||||
.build())
|
||||
.build())
|
||||
.build());
|
||||
@@ -421,7 +438,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
assertThat(ed.enabled()).isFalse();
|
||||
assertThat(ed.matchingEventTypes())
|
||||
.containsExactly(software.amazon.awssdk.services.ses.model.EventType.COMPLAINT);
|
||||
assertThat(ed.snsDestination().topicARN()).isEqualTo(TOPIC_ARN_2);
|
||||
assertThat(ed.snsDestination().topicARN()).isEqualTo(topicArn2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -435,7 +452,7 @@ class SesConfigurationSetEventDestinationTest {
|
||||
.enabled(true)
|
||||
.matchingEventTypes(software.amazon.awssdk.services.ses.model.EventType.SEND)
|
||||
.snsDestination(SNSDestination.builder()
|
||||
.topicARN(TOPIC_ARN)
|
||||
.topicARN(topicArn)
|
||||
.build())
|
||||
.build())
|
||||
.build()))
|
||||
|
||||
@@ -430,7 +430,7 @@ Alongside the classic Query API, Floci implements a subset of the SES v2 REST JS
|
||||
|
||||
Floci models no leased dedicated IPs: `GetDedicatedIps` is empty and IP-targeted operations return `NotFoundException`, as real AWS does for an account with no leased IPs, with required request members validated first (`BadRequestException`). `PutDedicatedIpPoolScalingAttributes` rejects downgrading a `MANAGED` pool to `STANDARD`, and `PutAccountDedicatedIpWarmupAttributes` stores the flag behind `GetAccount.DedicatedIpAutoWarmupEnabled` (default `true`).
|
||||
|
||||
Configuration set event destinations are stored as configuration. The target is not validated for existence; missing targets cause Floci to log a warning and skip that destination. Each event destination must specify exactly one destination type and at least one matching event type. A CloudWatch destination requires a non-empty dimension configuration list, and a Pinpoint destination requires an application ARN.
|
||||
Configuration set event destinations are stored as configuration. An `SnsDestination` topic is checked when the destination is created or updated: a value that is not an SNS topic ARN, a topic of another region, one that does not exist, or one whose policy does not let SES publish under IAM enforcement is refused with `InvalidSNSDestination` (v1) or `BadRequestException` (v2). AWS answers a topic of an unknown account with its own message; Floci reports it as not found. The other destination types are not validated for existence; missing targets cause Floci to log a warning and skip that destination. Each event destination must specify exactly one destination type and at least one matching event type. A CloudWatch destination requires a non-empty dimension configuration list, and a Pinpoint destination requires an application ARN.
|
||||
|
||||
A configuration set's `MessageSecurityOptions`, set with `CreateConfigurationSet` or the partial update [`UpdateConfigurationSet`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_UpdateConfigurationSet.html) and read back by `GetConfigurationSet`, is stored as configuration only: Floci never adds an S/MIME signature to a message. Where the API reference is silent, Floci follows what real SES answered (probed 2026-10-04): an empty `SigningScheme` resets the set to `DefaultScheme`, a request naming both union members stores `SmimeScheme`, and an `SmimeScheme` without a `SignatureFormat` stores `DETACHED`.
|
||||
|
||||
@@ -584,8 +584,8 @@ the identity that names the topic, the sender's own or its domain's. Both carry
|
||||
`aws:SourceAccount` and `aws:SourceOwner`. The default policy of a topic in the sending account admits
|
||||
SES; a topic whose policy was replaced, or a topic in another account, needs a statement for SES like
|
||||
the one in the [SES Developer Guide](https://docs.aws.amazon.com/ses/latest/dg/event-publishing-add-event-destination-sns.html).
|
||||
Floci checks the topic when SES publishes, not when an event destination is created or a notification
|
||||
topic is set. A refused event is not delivered. When a notification cannot be published because the
|
||||
Floci checks the topic when an event destination is created or updated or a notification topic is
|
||||
set, and again when SES publishes. A refused event is not delivered. When a notification cannot be published because the
|
||||
topic is gone or its policy refuses SES, Floci does what SES does: it takes the topic out of the
|
||||
identity's bounce and complaint settings that name it and turns email feedback forwarding back on,
|
||||
while a delivery setting stays. A topic that is gone counts with enforcement off too. SES also emails the account about
|
||||
|
||||
@@ -549,7 +549,7 @@ public class SesConfigurationSetController {
|
||||
}
|
||||
EventDestination dest = parseEventDestination(edNode);
|
||||
configSetService.createEventDestination(configurationSetName, edName, dest,
|
||||
regionResolver.getAccountId(), region);
|
||||
regionResolver.getAccountId(), region, true);
|
||||
LOG.infov("SES V2 CreateConfigurationSetEventDestination: {0} on {1}", edName, configurationSetName);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -594,7 +594,7 @@ public class SesConfigurationSetController {
|
||||
}
|
||||
EventDestination dest = parseEventDestination(edNode);
|
||||
configSetService.updateEventDestination(configurationSetName, eventDestinationName,
|
||||
dest, regionResolver.getAccountId(), region);
|
||||
dest, regionResolver.getAccountId(), region, true);
|
||||
LOG.infov("SES V2 UpdateConfigurationSetEventDestination: {0} on {1}",
|
||||
eventDestinationName, configurationSetName);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
|
||||
@@ -3,7 +3,9 @@ package io.floci.aws.services.ses;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsPartitions;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -59,20 +61,33 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
|
||||
private static final Pattern EVENT_DESTINATION_NAME_CHARS = Pattern.compile("^[A-Za-z0-9_-]+$");
|
||||
private static final int MAX_EVENT_DESTINATION_NAME_LENGTH = 64;
|
||||
private static final String INVALID_SNS_DESTINATION = "InvalidSNSDestination";
|
||||
private static final Pattern SNS_TOPIC_NAME = Pattern.compile("[A-Za-z0-9_-]{1,256}(\\.fifo)?");
|
||||
// The pattern SES declares for the stored value, quoted in the violation it answers on v2.
|
||||
private static final Pattern STORED_SNS_TOPIC_ARN = Pattern.compile(
|
||||
"arn:aws[^:]*:sns:[a-zA-Z0-9\\-]*:\\d{12}:[a-zA-Z_0-9+=,.@\\-_/:]+"); // partition-literal: AWS's fixed wording
|
||||
private static final List<String> VALID_EVENT_TYPES = List.of(
|
||||
"SEND", "REJECT", "BOUNCE", "COMPLAINT", "DELIVERY", "OPEN", "CLICK",
|
||||
"RENDERING_FAILURE", "DELIVERY_DELAY", "SUBSCRIPTION");
|
||||
|
||||
private final StorageBackend<String, ConfigurationSet> configSetStore;
|
||||
|
||||
private final SesEventPublisher eventPublisher;
|
||||
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public SesConfigurationSetService(StorageFactory storageFactory) {
|
||||
public SesConfigurationSetService(StorageFactory storageFactory, SesEventPublisher eventPublisher,
|
||||
RegionResolver regionResolver) {
|
||||
this(storageFactory.create("ses", "ses-config-sets.json",
|
||||
new TypeReference<Map<String, ConfigurationSet>>() {}));
|
||||
new TypeReference<Map<String, ConfigurationSet>>() {}), eventPublisher, regionResolver);
|
||||
}
|
||||
|
||||
SesConfigurationSetService(StorageBackend<String, ConfigurationSet> configSetStore) {
|
||||
SesConfigurationSetService(StorageBackend<String, ConfigurationSet> configSetStore,
|
||||
SesEventPublisher eventPublisher, RegionResolver regionResolver) {
|
||||
this.configSetStore = configSetStore;
|
||||
this.eventPublisher = eventPublisher;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -524,16 +539,27 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
|
||||
// ──────────────────────────── Event destinations ────────────────────────────
|
||||
|
||||
/**
|
||||
* @param v2 whether the call came through the v2 API. Probed: v2 create checks the SNS topic
|
||||
* before it looks up the configuration set, while v1 create and both updates
|
||||
* look up the set and the destination name first.
|
||||
*/
|
||||
public void createEventDestination(String configSetName, String eventDestinationName,
|
||||
EventDestination dest, String accountId, String region) {
|
||||
EventDestination dest, String accountId, String region, boolean v2) {
|
||||
validateEventDestinationName(eventDestinationName);
|
||||
validateEventDestination(dest, accountId, region);
|
||||
if (v2) {
|
||||
validateSnsTopic(dest, configSetName, accountId, region, true);
|
||||
}
|
||||
ConfigurationSet cs = get(configSetName, region);
|
||||
if (indexOfEventDestination(cs.getEventDestinations(), eventDestinationName) >= 0) {
|
||||
throw new AwsException("AlreadyExists",
|
||||
"An event destination with name <" + eventDestinationName
|
||||
+ "> already exists for configuration set <" + configSetName + ">.", 400);
|
||||
}
|
||||
if (!v2) {
|
||||
validateSnsTopic(dest, configSetName, accountId, region, false);
|
||||
}
|
||||
dest.setName(eventDestinationName);
|
||||
cs.getEventDestinations().add(dest);
|
||||
configSetStore.put(configSetKey(region, configSetName), cs);
|
||||
@@ -546,7 +572,7 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
}
|
||||
|
||||
public void updateEventDestination(String configSetName, String eventDestinationName,
|
||||
EventDestination dest, String accountId, String region) {
|
||||
EventDestination dest, String accountId, String region, boolean v2) {
|
||||
validateEventDestinationName(eventDestinationName);
|
||||
validateEventDestination(dest, accountId, region);
|
||||
ConfigurationSet cs = get(configSetName, region);
|
||||
@@ -556,6 +582,7 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
"An event destination with name <" + eventDestinationName
|
||||
+ "> does not exist for configuration set <" + configSetName + ">.", 404);
|
||||
}
|
||||
validateSnsTopic(dest, configSetName, accountId, region, v2);
|
||||
dest.setName(eventDestinationName);
|
||||
cs.getEventDestinations().set(index, dest);
|
||||
configSetStore.put(configSetKey(region, configSetName), cs);
|
||||
@@ -624,12 +651,6 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
"Please provide only one destination with each request. Either a Firehose Destination "
|
||||
+ "or a Cloudwatch Destination or an SNS Destination or an EventBridge Destination.", 400);
|
||||
}
|
||||
if (dest.getSnsDestination() != null
|
||||
&& (dest.getSnsDestination().getTopicArn() == null
|
||||
|| dest.getSnsDestination().getTopicArn().isBlank())) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"SnsDestination requires a non-blank TopicArn.", 400);
|
||||
}
|
||||
if (dest.getKinesisFirehoseDestination() != null
|
||||
&& (dest.getKinesisFirehoseDestination().getIamRoleArn() == null
|
||||
|| dest.getKinesisFirehoseDestination().getIamRoleArn().isBlank()
|
||||
@@ -673,6 +694,63 @@ public class SesConfigurationSetService implements SesTaggable {
|
||||
}
|
||||
}
|
||||
|
||||
// Probed against SES: each way a topic can be unusable has its own message, the same on
|
||||
// v1 and v2 except for a missing or empty value, and a disabled destination is checked too.
|
||||
// AWS answers a topic of an unknown account with "No account can be found for SNS topic";
|
||||
// Floci cannot tell an unknown account from a missing topic and answers "not found".
|
||||
private void validateSnsTopic(EventDestination dest, String configSetName, String accountId,
|
||||
String region, boolean v2) {
|
||||
if (dest.getSnsDestination() == null) {
|
||||
return;
|
||||
}
|
||||
String given = dest.getSnsDestination().getTopicArn();
|
||||
if (given == null || given.isEmpty()) {
|
||||
throw new AwsException(INVALID_SNS_DESTINATION, v2
|
||||
? "Invalid topic ARN provided: " + given + "."
|
||||
: "Invalid ARN provided: topic ARN = .", 400);
|
||||
}
|
||||
// Probed: SES judges the value without its padding, and refuses the padding itself last.
|
||||
String topicArn = given.strip();
|
||||
AwsArnUtils.Arn arn = snsTopicArn(topicArn);
|
||||
if (!arn.partition().equals(regionResolver.partitionForRegion(region))
|
||||
|| !SNS_TOPIC_NAME.matcher(arn.resource()).matches()) {
|
||||
throw new AwsException(INVALID_SNS_DESTINATION, "Invalid parameter for SNS topic <" + topicArn
|
||||
+ ">. Verify the SNS topic configuration is correct.", 400);
|
||||
}
|
||||
if (!arn.region().equals(region)) {
|
||||
throw new AwsException(INVALID_SNS_DESTINATION,
|
||||
"SNS topic must be located in same region as SES <" + region + ">", 400);
|
||||
}
|
||||
SesEventPublisher.TopicAccess access =
|
||||
eventPublisher.eventDestinationTopicAccess(topicArn, configSetName, accountId, region);
|
||||
if (access == SesEventPublisher.TopicAccess.NOT_FOUND) {
|
||||
throw new AwsException(INVALID_SNS_DESTINATION, "SNS topic <" + topicArn + "> not found.", 400);
|
||||
}
|
||||
if (access == SesEventPublisher.TopicAccess.DENIED) {
|
||||
throw new AwsException(INVALID_SNS_DESTINATION, "Access denied to SNS topic <" + topicArn
|
||||
+ ">. Verify the SNS topic policy grants Amazon SES the sns:Publish permission.", 400);
|
||||
}
|
||||
if (!STORED_SNS_TOPIC_ARN.matcher(given).matches()) {
|
||||
throw v2
|
||||
? SmithyValidationErrors.badRequest(SmithyValidationErrors.pattern(
|
||||
"destinations.1.member.snsTopicArn", given, STORED_SNS_TOPIC_ARN.pattern()))
|
||||
: new AwsException(INVALID_SNS_DESTINATION, "Invalid SNSDestination", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsArnUtils.Arn snsTopicArn(String topicArn) {
|
||||
if (AwsArnUtils.isArnFor(topicArn, "sns")) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(topicArn);
|
||||
if (AwsPartitions.find(arn.partition()).isPresent()
|
||||
&& arn.region().matches("[a-z0-9-]*")
|
||||
&& arn.accountId().matches("[0-9]{12}")) {
|
||||
return arn;
|
||||
}
|
||||
}
|
||||
throw new AwsException(INVALID_SNS_DESTINATION, "Invalid SNS topic ARN <" + topicArn
|
||||
+ "> should have the form <arn:aws:sns:region:[account-id]:[topic-name]>", 400); // partition-literal: AWS's fixed wording
|
||||
}
|
||||
|
||||
// SES publishes email events only to the default EventBridge bus, so the
|
||||
// EventBusArn member must name event-bus/default of the events service in the
|
||||
// configuration set's account and region. All supported AWS partitions are accepted.
|
||||
|
||||
@@ -157,9 +157,8 @@ public class SesEventPublisher {
|
||||
ed.getName(), eventType);
|
||||
return;
|
||||
}
|
||||
String configurationSetArn = AwsArnUtils.Arn.of("ses", defaultRegion, sendingAccountId,
|
||||
"configuration-set/" + configurationSetName).toString();
|
||||
if (!sesMayPublish(topicArn, configurationSetArn, sendingAccountId)) {
|
||||
if (!sesMayPublish(topicArn, configurationSetArn(configurationSetName, sendingAccountId, defaultRegion),
|
||||
sendingAccountId)) {
|
||||
LOG.warnf("SES SNS destination %s: event %s not delivered (topic %s does not allow SES to publish).",
|
||||
ed.getName(), eventType, topicArn);
|
||||
return;
|
||||
@@ -212,6 +211,27 @@ public class SesEventPublisher {
|
||||
return snsService.topicExists(topicArn, region) && sesMayPublish(topicArn, identityArn, identityAccountId);
|
||||
}
|
||||
|
||||
/** What SES finds when it looks at the topic an event destination names. */
|
||||
public enum TopicAccess { PUBLISHABLE, NOT_FOUND, DENIED }
|
||||
|
||||
/**
|
||||
* How the topic of an event destination on {@code configurationSetName} answers SES: whether
|
||||
* it exists in {@code region}, and whether its policy lets SES publish for that configuration
|
||||
* set. Probed: the event destination actions refuse both cases when the destination is set.
|
||||
*/
|
||||
public TopicAccess eventDestinationTopicAccess(String topicArn, String configurationSetName,
|
||||
String accountId, String region) {
|
||||
if (!snsService.topicExists(topicArn, region)) {
|
||||
return TopicAccess.NOT_FOUND;
|
||||
}
|
||||
return sesMayPublish(topicArn, configurationSetArn(configurationSetName, accountId, region), accountId)
|
||||
? TopicAccess.PUBLISHABLE : TopicAccess.DENIED;
|
||||
}
|
||||
|
||||
private static String configurationSetArn(String configurationSetName, String accountId, String region) {
|
||||
return AwsArnUtils.Arn.of("ses", region, accountId, "configuration-set/" + configurationSetName).toString();
|
||||
}
|
||||
|
||||
/** Whether the topic's policy lets SES publish to it for {@code sourceArn}, owned by {@code sourceAccount}. */
|
||||
private boolean sesMayPublish(String topicArn, String sourceArn, String sourceAccount) {
|
||||
return serviceCallAuthorizer.authorize("ses", "sns:Publish", topicArn, sourceArn, sourceAccount)
|
||||
|
||||
@@ -1005,7 +1005,7 @@ public class SesQueryHandler {
|
||||
String configSet = requireConfigurationSetName(params, "ConfigurationSetName");
|
||||
String edName = requireParam(params, "EventDestination.Name");
|
||||
EventDestination dest = readEventDestination(params, "EventDestination");
|
||||
configSetService.createEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region);
|
||||
configSetService.createEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region, false);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult(
|
||||
"CreateConfigurationSetEventDestination", AwsNamespaces.SES)).build();
|
||||
}
|
||||
@@ -1015,7 +1015,7 @@ public class SesQueryHandler {
|
||||
String configSet = requireConfigurationSetName(params, "ConfigurationSetName");
|
||||
String edName = requireParam(params, "EventDestination.Name");
|
||||
EventDestination dest = readEventDestination(params, "EventDestination");
|
||||
configSetService.updateEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region);
|
||||
configSetService.updateEventDestination(configSet, edName, dest, regionResolver.getAccountId(), region, false);
|
||||
return Response.ok(AwsQueryResponse.envelopeEmptyResult(
|
||||
"UpdateConfigurationSetEventDestination", AwsNamespaces.SES)).build();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ final class SesV2Json {
|
||||
static AwsException remapV1Exception(AwsException e) {
|
||||
return switch (e.getErrorCode()) {
|
||||
case "InvalidParameterValue", "InvalidTemplate", "ValidationError",
|
||||
"InvalidRenderingParameter", "MissingRenderingAttribute" ->
|
||||
"InvalidRenderingParameter", "MissingRenderingAttribute", "InvalidSNSDestination" ->
|
||||
new AwsException("BadRequestException", e.getMessage(), 400);
|
||||
case "TemplateDoesNotExist", "ConfigurationSetDoesNotExist",
|
||||
"CustomVerificationEmailTemplateDoesNotExist", "FromEmailAddressNotVerified" ->
|
||||
|
||||
@@ -38,6 +38,7 @@ class SesBooleanCoercionV2IntegrationTest {
|
||||
send("POST", "/v2/email/identities", "{\"EmailIdentity\":\"" + DOMAIN + "\"}").statusCode(200);
|
||||
send("POST", "/v2/email/configuration-sets", "{\"ConfigurationSetName\":\"" + CS + "\"}")
|
||||
.statusCode(200);
|
||||
SnsTopicFixture.create(REGION, "coercion");
|
||||
send("POST", "/v2/email/configuration-sets/" + CS + "/event-destinations", """
|
||||
{"EventDestinationName": "%s",
|
||||
"EventDestination": {"Enabled": "yes", "MatchingEventTypes": ["SEND"],
|
||||
|
||||
@@ -151,30 +151,6 @@ class SesConfigurationSetEventDestinationTest {
|
||||
assertDoesNotThrow(() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void snsBlankTopicArn_throws() {
|
||||
EventDestination ed = new EventDestination();
|
||||
ed.setMatchingEventTypes(List.of("SEND"));
|
||||
SnsDestination sns = new SnsDestination();
|
||||
sns.setTopicArn("");
|
||||
ed.setSnsDestination(sns);
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION));
|
||||
assertEquals("InvalidParameterValue", ex.getErrorCode());
|
||||
assertEquals("SnsDestination requires a non-blank TopicArn.", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void snsNullTopicArn_throws() {
|
||||
EventDestination ed = new EventDestination();
|
||||
ed.setMatchingEventTypes(List.of("SEND"));
|
||||
ed.setSnsDestination(new SnsDestination()); // TopicArn left null
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> SesConfigurationSetService.validateEventDestination(ed, ACCOUNT_ID, REGION));
|
||||
assertEquals("InvalidParameterValue", ex.getErrorCode());
|
||||
assertEquals("SnsDestination requires a non-blank TopicArn.", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void firehoseMissingDeliveryStream_throws() {
|
||||
EventDestination ed = new EventDestination();
|
||||
|
||||
+24
-1
@@ -38,6 +38,8 @@ class SesConfigurationSetEventDestinationV1IntegrationTest {
|
||||
.formParam("ConfigurationSet.Name", CS)
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
SnsTopicFixture.create("us-east-1", "topic-a");
|
||||
SnsTopicFixture.create("us-east-1", "topic-b");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -304,7 +306,28 @@ class SesConfigurationSetEventDestinationV1IntegrationTest {
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("SnsDestination requires a non-blank TopicArn"));
|
||||
.body(containsString("<Code>InvalidSNSDestination</Code>"))
|
||||
.body(containsString("<Message>Invalid ARN provided: topic ARN = .</Message>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void createConfigurationSetEventDestination_missingSnsTopic_rejected() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateConfigurationSetEventDestination")
|
||||
.formParam("ConfigurationSetName", CS)
|
||||
.formParam("EventDestination.Name", "v1-ed-sns-missing")
|
||||
.formParam("EventDestination.Enabled", "true")
|
||||
.formParam("EventDestination.MatchingEventTypes.member.1", "send")
|
||||
.formParam("EventDestination.SNSDestination.TopicARN", "arn:aws:sns:us-east-1:000000000000:never-created")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidSNSDestination</Code>"))
|
||||
.body(containsString("<Message>SNS topic <arn:aws:sns:us-east-1:000000000000:never-created> "
|
||||
+ "not found.</Message>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+66
-1
@@ -37,6 +37,10 @@ class SesConfigurationSetEventDestinationV2IntegrationTest {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createConfigurationSet() {
|
||||
for (String topic : new String[] {"ses-events", "ses-events-2", "dup", "x", "ed-a", "ed-b", "ed-c",
|
||||
"ed-a-updated"}) {
|
||||
SnsTopicFixture.create("us-east-1", topic);
|
||||
}
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
@@ -122,7 +126,7 @@ class SesConfigurationSetEventDestinationV2IntegrationTest {
|
||||
"EventDestinationName": "ed-x",
|
||||
"EventDestination": {
|
||||
"MatchingEventTypes": ["SEND"],
|
||||
"SnsDestination": {"TopicArn": "arn:aws:sns:us-east-1:000000000000:ghost"}
|
||||
"SnsDestination": {"TopicArn": "arn:aws:sns:us-east-1:000000000000:x"}
|
||||
}
|
||||
}
|
||||
""")
|
||||
@@ -738,4 +742,65 @@ class SesConfigurationSetEventDestinationV2IntegrationTest {
|
||||
.body("EventDestinations[0].EventBridgeDestination.EventBusArn",
|
||||
equalTo(SCOPED_DEFAULT_BUS_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(100)
|
||||
void snsDestination_missingTopicRejectedOnCreateAndUpdate() {
|
||||
String body = """
|
||||
{
|
||||
"EventDestinationName": "ed-sns",
|
||||
"EventDestination": {
|
||||
"MatchingEventTypes": ["SEND"],
|
||||
"SnsDestination": {"TopicArn": "arn:aws:sns:us-east-1:000000000000:never-created"}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String message = "SNS topic <arn:aws:sns:us-east-1:000000000000:never-created> not found.";
|
||||
String set = "v2-cs-sns-check";
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"ConfigurationSetName\": \"" + set + "\"}")
|
||||
.when()
|
||||
.post("/v2/email/configuration-sets")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body.replace("never-created", "x"))
|
||||
.when()
|
||||
.post("/v2/email/configuration-sets/" + set + "/event-destinations")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/v2/email/configuration-sets/" + set + "/event-destinations")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/v2/email/configuration-sets/v2-cs-ghost/event-destinations")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(message));
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body)
|
||||
.when()
|
||||
.put("/v2/email/configuration-sets/" + set + "/event-destinations/ed-sns")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -2,6 +2,7 @@ package io.floci.aws.services.ses;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.ses.model.ConfigurationSet;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -13,6 +14,7 @@ import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* The ListConfigurationSets {@code Filter}. Every message and precedence below is what SES v2
|
||||
@@ -26,7 +28,8 @@ class SesConfigurationSetListFilterServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new SesConfigurationSetService(new InMemoryStorage<>());
|
||||
service = new SesConfigurationSetService(new InMemoryStorage<>(), mock(SesEventPublisher.class),
|
||||
new RegionResolver(REGION, "000000000000"));
|
||||
for (String name : List.of("Filter-Alpha", "filter-beta", "other-FILTER-gamma", "unrelated")) {
|
||||
ConfigurationSet configSet = new ConfigurationSet();
|
||||
configSet.setName(name);
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.floci.aws.services.ses;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.PersistentStorage;
|
||||
import io.floci.aws.services.ses.model.ConfigurationSet;
|
||||
@@ -24,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Unit test for the extracted configuration-set domain: store ownership, key derivation and name
|
||||
@@ -44,7 +46,8 @@ class SesConfigurationSetServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new SesConfigurationSetService(new InMemoryStorage<>());
|
||||
service = new SesConfigurationSetService(new InMemoryStorage<>(), mock(SesEventPublisher.class),
|
||||
new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -203,7 +206,8 @@ class SesConfigurationSetServiceTest {
|
||||
void messageSecurityOptions_surviveAPersistentStorageReload(@TempDir Path dir) {
|
||||
Path file = dir.resolve("ses-config-sets.json");
|
||||
SesConfigurationSetService writer = new SesConfigurationSetService(
|
||||
new PersistentStorage<>(file, new TypeReference<Map<String, ConfigurationSet>>() {}));
|
||||
new PersistentStorage<>(file, new TypeReference<Map<String, ConfigurationSet>>() {}),
|
||||
mock(SesEventPublisher.class), new RegionResolver(REGION, "000000000000"));
|
||||
writer.create(cs("smime-cs"), REGION);
|
||||
writer.create(cs("default-cs"), REGION);
|
||||
writer.updateMessageSecurityOptions("smime-cs",
|
||||
@@ -214,7 +218,8 @@ class SesConfigurationSetServiceTest {
|
||||
PersistentStorage<String, ConfigurationSet> reloaded =
|
||||
new PersistentStorage<>(file, new TypeReference<Map<String, ConfigurationSet>>() {});
|
||||
reloaded.load();
|
||||
SesConfigurationSetService reader = new SesConfigurationSetService(reloaded);
|
||||
SesConfigurationSetService reader = new SesConfigurationSetService(reloaded, mock(SesEventPublisher.class),
|
||||
new RegionResolver(REGION, "000000000000"));
|
||||
|
||||
SigningScheme smime = reader.get("smime-cs", REGION).getMessageSecurityOptions().getSigningScheme();
|
||||
assertEquals("DETACHED", smime.getSmimeScheme().getSignatureFormat());
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.ses.SesEventPublisher.TopicAccess;
|
||||
import io.floci.aws.services.ses.model.ConfigurationSet;
|
||||
import io.floci.aws.services.ses.model.EventDestination;
|
||||
import io.floci.aws.services.ses.model.SnsDestination;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** The SNS topic of an event destination is checked when it is set; messages and order were probed. */
|
||||
class SesConfigurationSetSnsDestinationTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String CS = "events";
|
||||
private static final String TOPIC = "arn:aws:sns:us-east-1:000000000000:events";
|
||||
private static final String OTHER_TOPIC = "arn:aws:sns:us-east-1:000000000000:other";
|
||||
|
||||
private SesEventPublisher publisher;
|
||||
private SesConfigurationSetService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
publisher = mock(SesEventPublisher.class);
|
||||
when(publisher.eventDestinationTopicAccess(any(), any(), any(), any())).thenReturn(TopicAccess.NOT_FOUND);
|
||||
when(publisher.eventDestinationTopicAccess(TOPIC, CS, ACCOUNT_ID, REGION)).thenReturn(TopicAccess.PUBLISHABLE);
|
||||
serviceWithConfigurationSet(new RegionResolver(REGION, ACCOUNT_ID), REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishableTopicIsStored() {
|
||||
create("to-sns", TOPIC, false);
|
||||
|
||||
assertEquals(TOPIC, storedTopic());
|
||||
}
|
||||
|
||||
@Test
|
||||
void paddedArnOfAPublishableTopicIsRefusedAndNotStored() {
|
||||
String padded = " " + TOPIC + " ";
|
||||
|
||||
assertEquals("Invalid SNSDestination", refusal(() -> create("to-sns", padded, false)).getMessage());
|
||||
AwsException v2 = assertThrows(AwsException.class, () -> create("to-sns", padded, true));
|
||||
assertEquals("BadRequestException", v2.getErrorCode());
|
||||
assertEquals("1 validation error detected: Value '" + padded + "' at 'destinations.1.member.snsTopicArn' "
|
||||
+ "failed to satisfy constraint: Member must satisfy regular expression pattern: "
|
||||
+ "arn:aws[^:]*:sns:[a-zA-Z0-9\\-]*:\\d{12}:[a-zA-Z_0-9+=,.@\\-_/:]+", v2.getMessage());
|
||||
assertEquals(List.of(), service.getEventDestinations(CS, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void paddedArnOfAMissingTopicIsReportedWithoutItsPadding() {
|
||||
assertRefused("SNS topic <" + OTHER_TOPIC + "> not found.", " " + OTHER_TOPIC + " ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingTopicIsRefused() {
|
||||
assertRefused("SNS topic <" + OTHER_TOPIC + "> not found.", OTHER_TOPIC);
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicThatRefusesSesIsRefused() {
|
||||
when(publisher.eventDestinationTopicAccess(OTHER_TOPIC, CS, ACCOUNT_ID, REGION))
|
||||
.thenReturn(TopicAccess.DENIED);
|
||||
|
||||
assertRefused("Access denied to SNS topic <" + OTHER_TOPIC
|
||||
+ ">. Verify the SNS topic policy grants Amazon SES the sns:Publish permission.", OTHER_TOPIC);
|
||||
}
|
||||
|
||||
@Test
|
||||
void valueThatIsNotATopicArnIsRefused() {
|
||||
String form = " should have the form <arn:aws:sns:region:[account-id]:[topic-name]>";
|
||||
|
||||
assertRefused("Invalid SNS topic ARN <not-an-arn>" + form, "not-an-arn");
|
||||
assertRefused("Invalid SNS topic ARN <arn:aws:sqs:us-east-1:000000000000:x>" + form,
|
||||
"arn:aws:sqs:us-east-1:000000000000:x");
|
||||
assertRefused("Invalid SNS topic ARN <>" + form, " ");
|
||||
for (String arn : List.of("arn:aws:sns:us-east-1:abc:t", "arn:aws:sns:us-east-1:12345678901:t",
|
||||
"arn:aws:sns:us-east-1::t", "arn:aws:sns:US-EAST-1:000000000000:t",
|
||||
"arn:foo:sns:us-east-1:000000000000:t")) {
|
||||
assertRefused("Invalid SNS topic ARN <" + arn + ">" + form, arn);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicArnWithoutARegionIsATopicOfAnotherRegion() {
|
||||
assertRefused("SNS topic must be located in same region as SES <us-east-1>",
|
||||
"arn:aws:sns::000000000000:events");
|
||||
assertRefused("SNS topic must be located in same region as SES <us-east-1>",
|
||||
"arn:aws:sns:not-a-region:000000000000:events");
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicArnSnsWouldNotAcceptIsRefused() {
|
||||
for (String arn : List.of("arn:aws:sns:us-east-1:000000000000:", "arn:aws:sns:us-east-1:000000000000:bad name!",
|
||||
"arn:aws-cn:sns:us-east-1:000000000000:events")) {
|
||||
assertRefused("Invalid parameter for SNS topic <" + arn
|
||||
+ ">. Verify the SNS topic configuration is correct.", arn);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicOfARegionNoPartitionPublishesIsJudgedByTheDeploymentPartition() {
|
||||
String region = "local-region-1";
|
||||
String topic = "arn:aws-cn:sns:local-region-1:000000000000:events";
|
||||
when(publisher.eventDestinationTopicAccess(topic, CS, ACCOUNT_ID, region)).thenReturn(TopicAccess.PUBLISHABLE);
|
||||
serviceWithConfigurationSet(new RegionResolver("cn-north-1", ACCOUNT_ID, "aws-cn"), region);
|
||||
|
||||
service.createEventDestination(CS, "to-sns", destination(topic), ACCOUNT_ID, region, false);
|
||||
|
||||
assertEquals(topic, service.getEventDestinations(CS, region).get(0).getSnsDestination().getTopicArn());
|
||||
String commercial = "arn:aws:sns:local-region-1:000000000000:events";
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.updateEventDestination(CS, "to-sns", destination(commercial), ACCOUNT_ID, region,
|
||||
false));
|
||||
assertEquals("Invalid parameter for SNS topic <" + commercial
|
||||
+ ">. Verify the SNS topic configuration is correct.", refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicOfAnotherRegionIsRefusedWithoutALookup() {
|
||||
assertRefused("SNS topic must be located in same region as SES <us-east-1>",
|
||||
"arn:aws:sns:us-west-2:000000000000:events");
|
||||
|
||||
verify(publisher, never()).eventDestinationTopicAccess(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyAndMissingTopicArnHaveTheirOwnMessagesPerApi() {
|
||||
assertEquals("Invalid ARN provided: topic ARN = .", refusal(() -> create("to-sns", "", false)).getMessage());
|
||||
assertEquals("Invalid topic ARN provided: .", refusal(() -> create("to-sns", "", true)).getMessage());
|
||||
assertEquals("Invalid topic ARN provided: null.", refusal(() -> create("to-sns", null, true)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledDestinationIsCheckedToo() {
|
||||
EventDestination destination = destination(OTHER_TOPIC);
|
||||
destination.setEnabled(false);
|
||||
|
||||
AwsException e = refusal(() -> service.createEventDestination(CS, "to-sns", destination, ACCOUNT_ID,
|
||||
REGION, false));
|
||||
|
||||
assertEquals("SNS topic <" + OTHER_TOPIC + "> not found.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void v1CreateLooksUpTheSetAndTheNameBeforeTheTopic() {
|
||||
create("to-sns", TOPIC, false);
|
||||
|
||||
assertEquals("ConfigurationSetDoesNotExist", assertThrows(AwsException.class,
|
||||
() -> service.createEventDestination("ghost", "to-sns", destination(OTHER_TOPIC), ACCOUNT_ID,
|
||||
REGION, false)).getErrorCode());
|
||||
assertEquals("AlreadyExists", assertThrows(AwsException.class,
|
||||
() -> create("to-sns", OTHER_TOPIC, false)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void v2CreateChecksTheTopicBeforeTheSetAndTheName() {
|
||||
create("to-sns", TOPIC, true);
|
||||
|
||||
assertEquals("InvalidSNSDestination", assertThrows(AwsException.class,
|
||||
() -> service.createEventDestination("ghost", "to-sns", destination(OTHER_TOPIC), ACCOUNT_ID,
|
||||
REGION, true)).getErrorCode());
|
||||
assertEquals("InvalidSNSDestination", assertThrows(AwsException.class,
|
||||
() -> create("to-sns", OTHER_TOPIC, true)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateLooksUpTheDestinationBeforeTheTopic() {
|
||||
for (boolean v2 : new boolean[] {false, true}) {
|
||||
assertEquals("NotFoundException", assertThrows(AwsException.class,
|
||||
() -> service.updateEventDestination(CS, "unknown", destination(OTHER_TOPIC), ACCOUNT_ID,
|
||||
REGION, v2)).getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusedUpdateKeepsTheStoredDestination() {
|
||||
create("to-sns", TOPIC, false);
|
||||
|
||||
AwsException e = refusal(() -> service.updateEventDestination(CS, "to-sns", destination(OTHER_TOPIC),
|
||||
ACCOUNT_ID, REGION, false));
|
||||
|
||||
assertEquals("SNS topic <" + OTHER_TOPIC + "> not found.", e.getMessage());
|
||||
assertEquals(TOPIC, storedTopic());
|
||||
}
|
||||
|
||||
private void serviceWithConfigurationSet(RegionResolver regionResolver, String region) {
|
||||
service = new SesConfigurationSetService(new InMemoryStorage<>(), publisher, regionResolver);
|
||||
ConfigurationSet configurationSet = new ConfigurationSet();
|
||||
configurationSet.setName(CS);
|
||||
service.create(configurationSet, region);
|
||||
}
|
||||
|
||||
private void assertRefused(String message, String topicArn) {
|
||||
for (boolean v2 : new boolean[] {false, true}) {
|
||||
assertEquals(message, refusal(() -> create("to-sns", topicArn, v2)).getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException refusal(Runnable call) {
|
||||
AwsException e = assertThrows(AwsException.class, call::run);
|
||||
assertEquals("InvalidSNSDestination", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
return e;
|
||||
}
|
||||
|
||||
private void create(String name, String topicArn, boolean v2) {
|
||||
service.createEventDestination(CS, name, destination(topicArn), ACCOUNT_ID, REGION, v2);
|
||||
}
|
||||
|
||||
private String storedTopic() {
|
||||
return service.getEventDestinations(CS, REGION).get(0).getSnsDestination().getTopicArn();
|
||||
}
|
||||
|
||||
private static EventDestination destination(String topicArn) {
|
||||
EventDestination destination = new EventDestination();
|
||||
destination.setEnabled(true);
|
||||
destination.setMatchingEventTypes(List.of("SEND"));
|
||||
SnsDestination sns = new SnsDestination();
|
||||
sns.setTopicArn(topicArn);
|
||||
destination.setSnsDestination(sns);
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.acm.AcmService;
|
||||
import io.floci.aws.services.route53.Route53Service;
|
||||
@@ -172,7 +173,8 @@ final class SesServiceTestBuilder {
|
||||
contactService = new SesContactService(contactListStore, contactStore, clock);
|
||||
suppressionService = new SesSuppressionService(suppressionStore, accountSuppressionStore,
|
||||
new InMemoryStorage<>());
|
||||
configSetService = new SesConfigurationSetService(configSetStore);
|
||||
configSetService = new SesConfigurationSetService(configSetStore, mock(SesEventPublisher.class),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
identityService = new SesIdentityService(identityStore, route53Service, clock);
|
||||
sentEmailService = new SesSentEmailService(emailStore);
|
||||
cvetService = new SesCvetService(cvetStore);
|
||||
|
||||
+24
-2
@@ -53,11 +53,31 @@ class SesSnsPublishAuthorizationIntegrationTest {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEventDestinationIsRefusedWhileItsTopicRefusesSes() {
|
||||
String topicArn = createSubscribedTopic(OWNER, createQueue(OWNER));
|
||||
setTopicPolicy(OWNER, topicArn, rootOnly(topicArn));
|
||||
String name = "authz-" + suffix();
|
||||
ses(OWNER, "CreateConfigurationSet", Map.of("ConfigurationSet.Name", name)).statusCode(200);
|
||||
cleanup.register(() -> ses(OWNER, "DeleteConfigurationSet", Map.of("ConfigurationSetName", name))
|
||||
.statusCode(200));
|
||||
|
||||
ses(OWNER, "CreateConfigurationSetEventDestination", Map.of(
|
||||
"ConfigurationSetName", name,
|
||||
"EventDestination.Name", "to-sns",
|
||||
"EventDestination.Enabled", "true",
|
||||
"EventDestination.MatchingEventTypes.member.1", "send",
|
||||
"EventDestination.SNSDestination.TopicARN", topicArn))
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidSNSDestination</Code>"))
|
||||
.body(containsString("Access denied to SNS topic <" + topicArn + ">. Verify the SNS topic "
|
||||
+ "policy grants Amazon SES the sns:Publish permission."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCallerRefusedSnsPublishCannotReachATopicThatRefusesSesThroughAConfigurationSet() {
|
||||
String queueUrl = createQueue(OWNER);
|
||||
String topicArn = createSubscribedTopic(OWNER, queueUrl);
|
||||
setTopicPolicy(OWNER, topicArn, rootOnly(topicArn));
|
||||
String sender = createSender();
|
||||
String user = "ses-only-" + suffix();
|
||||
String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
|
||||
@@ -66,6 +86,9 @@ class SesSnsPublishAuthorizationIntegrationTest {
|
||||
"PolicyName", "ses-only", "PolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"ses:*","Resource":"*"}]}""")).statusCode(200);
|
||||
// The destination is refused while the topic refuses SES, so the policy changes after it is set.
|
||||
String configurationSet = createConfigurationSet(accessKeyId, topicArn);
|
||||
setTopicPolicy(OWNER, topicArn, rootOnly(topicArn));
|
||||
given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accessKeyId, "sns"))
|
||||
.header("X-Amz-Target", "SNS_20100331.Publish")
|
||||
@@ -76,7 +99,6 @@ class SesSnsPublishAuthorizationIntegrationTest {
|
||||
.then()
|
||||
.statusCode(allOf(greaterThanOrEqualTo(400), lessThan(500)));
|
||||
|
||||
String configurationSet = createConfigurationSet(accessKeyId, topicArn);
|
||||
sendEmail(accessKeyId, sender, SUCCESS, configurationSet);
|
||||
|
||||
assertEquals(0, receive(OWNER, queueUrl));
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
/** Creates the SNS topics that SES settings name, since SES checks a topic when it is set. */
|
||||
final class SnsTopicFixture {
|
||||
|
||||
private SnsTopicFixture() {
|
||||
}
|
||||
|
||||
static String create(String region, String name) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/" + region + "/sns/aws4_request")
|
||||
.formParam("Action", "CreateTopic")
|
||||
.formParam("Name", name)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user