mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ses): add v2 suppression list endpoints (#813)
* feat(ses): add v2 suppression list endpoints
Implements the v2 SES suppression list management plane at
/v2/email/suppression/addresses:
- PUT /v2/email/suppression/addresses PutSuppressedDestination
- GET /v2/email/suppression/addresses/{EmailAddress} GetSuppressedDestination
- DELETE /v2/email/suppression/addresses/{EmailAddress} DeleteSuppressedDestination
- GET /v2/email/suppression/addresses ListSuppressedDestinations
(repeated Reason filter)
Entries are persisted per region in ses-suppression.json. A repeat Put for
the same EmailAddress updates the existing entry's Reason and LastUpdateTime.
List responses are sorted by LastUpdateTime then EmailAddress for stable
output across runs.
Behaviour aligned with real AWS responses:
- EmailAddress is silently trimmed of leading/trailing whitespace on Put
/ Get / Delete; whitespace-only is rejected with BadRequestException.
- Reason validation errors use the AWS-canonical "1 validation error
detected: Value at '<field>' failed to satisfy constraint: Member must
satisfy enum value set: [BOUNCE, COMPLAINT]" wording, with the nested
"Member must satisfy constraint: [...]" wrapping AWS emits for the
list-typed reasons filter.
- ListSuppressedDestinations accepts a repeated Reason query param (the
Java SDK's .reasons(...) varargs serialise this way) and applies
OR-semantics across the supplied values.
- GetSuppressedDestination and DeleteSuppressedDestination return
NotFoundException with "Email address <X> does not exist on your
suppression list." for unknown addresses — Delete is not idempotent
on AWS so floci matches.
SendEmail integration is intentionally out of scope: suppressed addresses
are still delivered locally so that existing send tests remain unaffected.
A follow-up PR can wire suppression checks into the send path along with
the PutAccount/ConfigurationSetSuppressionOptions configuration endpoints.
Tests:
- SesSuppressionV2IntegrationTest covers Put/Get/Delete lifecycle, List
sort order, single- and multi-Reason filter (with OR semantics), Put
updates, trim-on-Put, idempotent-Get-on-known, and validation errors.
- SesSuppressionListTest exercises the same surface through the Java SDK
v2 SesV2Client.
* fix(ses): reject null and non-object bodies on PutSuppressedDestination
Jackson's NullNode.asText("default") returns the literal string "null"
rather than the default, so a body like
{"EmailAddress": null, "Reason": "BOUNCE"}
silently stored an entry under the email address "null". Validate the
body is a JSON object via requireJsonObject and require both
EmailAddress and Reason to be present and textual via a new
readRequiredStringField helper.
SesSuppressionV2IntegrationTest gains three cases covering null
EmailAddress, null Reason, and a non-object root body.
This commit is contained in:
1 parent
7516acb5f9
commit
944f82a6a4
7 files changed
+681
No files matched your search
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult;
|
||||
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
|
||||
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
|
||||
import io.github.hectorvent.floci.services.ses.model.Identity;
|
||||
import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;
|
||||
import io.github.hectorvent.floci.services.ses.model.Tag;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -692,6 +693,97 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────── Suppression list ───────────────────────────
|
||||
|
||||
@PUT
|
||||
@Path("/suppression/addresses")
|
||||
public Response putSuppressedDestination(@Context HttpHeaders headers, String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
if (body == null || body.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "Request body is required.", 400);
|
||||
}
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
requireJsonObject(request);
|
||||
String emailAddress = readRequiredStringField(request, "EmailAddress");
|
||||
String reason = readRequiredStringField(request, "Reason");
|
||||
sesService.putSuppressedDestination(region, emailAddress, reason);
|
||||
LOG.infov("SES V2 PutSuppressedDestination: {0} ({1})", emailAddress, reason);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
|
||||
throw new AwsException("BadRequestException", e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String readRequiredStringField(JsonNode request, String fieldName) {
|
||||
JsonNode node = request.path(fieldName);
|
||||
if (node.isMissingNode() || node.isNull() || !node.isTextual()) {
|
||||
throw new AwsException("BadRequestException", fieldName + " is required.", 400);
|
||||
}
|
||||
return node.asText();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/suppression/addresses/{emailAddress}")
|
||||
public Response getSuppressedDestination(@Context HttpHeaders headers,
|
||||
@PathParam("emailAddress") String emailAddress) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
SuppressedDestination suppressed = sesService.getSuppressedDestination(region, emailAddress);
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ObjectNode entry = result.putObject("SuppressedDestination");
|
||||
entry.put("EmailAddress", suppressed.getEmailAddress());
|
||||
entry.put("Reason", suppressed.getReason());
|
||||
if (suppressed.getLastUpdateTime() != null) {
|
||||
entry.put("LastUpdateTime", suppressed.getLastUpdateTime().getEpochSecond());
|
||||
}
|
||||
return Response.ok(result).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/suppression/addresses/{emailAddress}")
|
||||
public Response deleteSuppressedDestination(@Context HttpHeaders headers,
|
||||
@PathParam("emailAddress") String emailAddress) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
sesService.deleteSuppressedDestination(region, emailAddress);
|
||||
LOG.infov("SES V2 DeleteSuppressedDestination: {0}", emailAddress);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/suppression/addresses")
|
||||
public Response listSuppressedDestinations(@Context HttpHeaders headers,
|
||||
@QueryParam("Reason") List<String> reasons) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<SuppressedDestination> entries;
|
||||
try {
|
||||
entries = sesService.listSuppressedDestinations(region, reasons);
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ArrayNode summaries = result.putArray("SuppressedDestinationSummaries");
|
||||
for (SuppressedDestination s : entries) {
|
||||
ObjectNode item = objectMapper.createObjectNode();
|
||||
item.put("EmailAddress", s.getEmailAddress());
|
||||
item.put("Reason", s.getReason());
|
||||
if (s.getLastUpdateTime() != null) {
|
||||
item.put("LastUpdateTime", s.getLastUpdateTime().getEpochSecond());
|
||||
}
|
||||
summaries.add(item);
|
||||
}
|
||||
return Response.ok(result).build();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Tags ───────────────────────────────
|
||||
|
||||
@POST
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
|
||||
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
|
||||
import io.github.hectorvent.floci.services.ses.model.Identity;
|
||||
import io.github.hectorvent.floci.services.ses.model.SentEmail;
|
||||
import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;
|
||||
import io.github.hectorvent.floci.services.ses.model.Tag;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -54,6 +55,7 @@ public class SesService {
|
||||
private final StorageBackend<String, Boolean> accountSettingsStore;
|
||||
private final StorageBackend<String, EmailTemplate> templateStore;
|
||||
private final StorageBackend<String, ConfigurationSet> configSetStore;
|
||||
private final StorageBackend<String, SuppressedDestination> suppressionStore;
|
||||
private final SmtpRelay smtpRelay;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@@ -69,6 +71,8 @@ public class SesService {
|
||||
new TypeReference<Map<String, EmailTemplate>>() {});
|
||||
this.configSetStore = storageFactory.create("ses", "ses-config-sets.json",
|
||||
new TypeReference<Map<String, ConfigurationSet>>() {});
|
||||
this.suppressionStore = storageFactory.create("ses", "ses-suppression.json",
|
||||
new TypeReference<Map<String, SuppressedDestination>>() {});
|
||||
this.smtpRelay = smtpRelay;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
@@ -78,6 +82,7 @@ public class SesService {
|
||||
StorageBackend<String, Boolean> accountSettingsStore,
|
||||
StorageBackend<String, EmailTemplate> templateStore,
|
||||
StorageBackend<String, ConfigurationSet> configSetStore,
|
||||
StorageBackend<String, SuppressedDestination> suppressionStore,
|
||||
SmtpRelay smtpRelay,
|
||||
ObjectMapper objectMapper) {
|
||||
this.identityStore = identityStore;
|
||||
@@ -85,6 +90,7 @@ public class SesService {
|
||||
this.accountSettingsStore = accountSettingsStore;
|
||||
this.templateStore = templateStore;
|
||||
this.configSetStore = configSetStore;
|
||||
this.suppressionStore = suppressionStore;
|
||||
this.smtpRelay = smtpRelay;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
@@ -689,6 +695,87 @@ public class SesService {
|
||||
return new ResourceRef(parsed.region(), resource.substring(0, slash), resource.substring(slash + 1));
|
||||
}
|
||||
|
||||
// ──────────────────────────── Suppression list ────────────────────────────
|
||||
|
||||
public void putSuppressedDestination(String region, String emailAddress, String reason) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
validateSuppressionReason(reason, "reason", false);
|
||||
String key = suppressionKey(region, normalized);
|
||||
SuppressedDestination existing = suppressionStore.get(key).orElse(null);
|
||||
SuppressedDestination entry = existing != null ? existing : new SuppressedDestination(normalized, reason);
|
||||
entry.setReason(reason);
|
||||
entry.setLastUpdateTime(Instant.now());
|
||||
suppressionStore.put(key, entry);
|
||||
LOG.infov("Suppressed destination {0} in region {1} (reason={2})", normalized, region, reason);
|
||||
}
|
||||
|
||||
public SuppressedDestination getSuppressedDestination(String region, String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
return suppressionStore.get(suppressionKey(region, normalized))
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
"Email address " + normalized + " does not exist on your suppression list.",
|
||||
404));
|
||||
}
|
||||
|
||||
public void deleteSuppressedDestination(String region, String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
String key = suppressionKey(region, normalized);
|
||||
if (suppressionStore.get(key).isEmpty()) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"Email address " + normalized + " does not exist on your suppression list.",
|
||||
404);
|
||||
}
|
||||
suppressionStore.delete(key);
|
||||
LOG.infov("Removed suppression entry for {0} in region {1}", normalized, region);
|
||||
}
|
||||
|
||||
public List<SuppressedDestination> listSuppressedDestinations(String region, List<String> reasonFilters) {
|
||||
Set<String> filters = new HashSet<>();
|
||||
if (reasonFilters != null) {
|
||||
for (String r : reasonFilters) {
|
||||
if (r != null && !r.isBlank()) {
|
||||
validateSuppressionReason(r, "reasons", true);
|
||||
filters.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
String prefix = "suppression::" + region + "::";
|
||||
List<SuppressedDestination> all = new ArrayList<>(suppressionStore.scan(k -> k.startsWith(prefix)));
|
||||
all.sort(Comparator.comparing(SuppressedDestination::getLastUpdateTime,
|
||||
Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
.thenComparing(SuppressedDestination::getEmailAddress,
|
||||
Comparator.nullsLast(Comparator.naturalOrder())));
|
||||
if (filters.isEmpty()) {
|
||||
return all;
|
||||
}
|
||||
return all.stream()
|
||||
.filter(s -> filters.contains(s.getReason()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static String suppressionKey(String region, String emailAddress) {
|
||||
return "suppression::" + region + "::" + emailAddress;
|
||||
}
|
||||
|
||||
private static String normalizeSuppressionEmail(String emailAddress) {
|
||||
if (emailAddress == null || emailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "EmailAddress is required.", 400);
|
||||
}
|
||||
// AWS trims leading/trailing whitespace from EmailAddress before storing.
|
||||
return emailAddress.trim();
|
||||
}
|
||||
|
||||
private static void validateSuppressionReason(String reason, String fieldName, boolean nested) {
|
||||
if (reason == null || (!"BOUNCE".equals(reason) && !"COMPLAINT".equals(reason))) {
|
||||
String constraint = nested
|
||||
? "Member must satisfy constraint: [Member must satisfy enum value set: [BOUNCE, COMPLAINT]]"
|
||||
: "Member must satisfy enum value set: [BOUNCE, COMPLAINT]";
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at '" + fieldName + "' failed to satisfy constraint: "
|
||||
+ constraint, 400);
|
||||
}
|
||||
}
|
||||
|
||||
static void validateTag(Tag tag) {
|
||||
if (tag == null) {
|
||||
throw new AwsException("InvalidParameterValue", "Tag must not be null.", 400);
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.services.ses.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class SuppressedDestination {
|
||||
|
||||
@JsonProperty("EmailAddress")
|
||||
private String emailAddress;
|
||||
|
||||
@JsonProperty("Reason")
|
||||
private String reason;
|
||||
|
||||
@JsonProperty("LastUpdateTime")
|
||||
private Instant lastUpdateTime;
|
||||
|
||||
public SuppressedDestination() {}
|
||||
|
||||
public SuppressedDestination(String emailAddress, String reason) {
|
||||
this.emailAddress = emailAddress;
|
||||
this.reason = reason;
|
||||
this.lastUpdateTime = Instant.now();
|
||||
}
|
||||
|
||||
public String getEmailAddress() { return emailAddress; }
|
||||
public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; }
|
||||
|
||||
public String getReason() { return reason; }
|
||||
public void setReason(String reason) { this.reason = reason; }
|
||||
|
||||
public Instant getLastUpdateTime() { return lastUpdateTime; }
|
||||
public void setLastUpdateTime(Instant lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; }
|
||||
}
|
||||
Reference in new issue
Block a user