mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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" ->
|
||||
|
||||
Reference in new issue
Block a user