mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ses): support v2 tenant suppression attributes and tenant-scoped suppression list (#2743)
* feat(ses): support v2 tenant suppression attributes and tenant-scoped suppression list * fix(ses): guard runWithTenant against a null TenantName * fix(ses): collapse an absent TenantName to the service-level message on tenant suppression ops * fix(ses): collapse an absent TenantName to the service-level message on the remaining tenant ops * fix(ses): collapse an empty TenantName to the service-level message on GetTenant/DeleteTenant
This commit is contained in:
1 parent
12a6feb0c9
commit
fc3c28323a
14 files changed
+961
-80
No files matched your search
@@ -867,8 +867,19 @@ public class SesController {
|
||||
requireJsonObject(request);
|
||||
String tenantName = stringMemberOrAbsent(request, "TenantName");
|
||||
List<Tag> tags = parseTagsArray(request.path("Tags"));
|
||||
List<String> suppressedReasons = null;
|
||||
String suppressionScope = null;
|
||||
JsonNode attrs = request.path("SuppressionAttributes");
|
||||
if (!attrs.isMissingNode() && !attrs.isNull()) {
|
||||
if (!attrs.isObject()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
suppressedReasons = stringArrayOrAbsent(attrs, "SuppressedReasons");
|
||||
suppressionScope = stringMemberOrAbsent(attrs, "SuppressionScope");
|
||||
}
|
||||
String accountId = regionResolver.getAccountId();
|
||||
Tenant tenant = sesService.createTenant(tenantName, tags, accountId, region);
|
||||
Tenant tenant = sesService.createTenant(tenantName, tags, suppressedReasons,
|
||||
suppressionScope, accountId, region);
|
||||
return Response.ok(tenantJson(tenant)).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
@@ -1066,6 +1077,30 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 3: PutTenantSuppressionAttributes. The route really is the singular "tenant", unlike
|
||||
// every other tenant route (verified against real AWS and the SDK marshaller).
|
||||
@POST
|
||||
@Path("/tenant/suppression")
|
||||
public Response putTenantSuppressionAttributes(@Context HttpHeaders headers, String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
JsonNode request = (body == null || body.isBlank())
|
||||
? objectMapper.createObjectNode()
|
||||
: objectMapper.readTree(body);
|
||||
requireJsonObject(request);
|
||||
String tenantName = stringMemberOrAbsent(request, "TenantName");
|
||||
List<String> suppressedReasons = stringArrayOrAbsent(request, "SuppressedReasons");
|
||||
String suppressionScope = stringMemberOrAbsent(request, "SuppressionScope");
|
||||
sesService.putTenantSuppressionAttributes(tenantName, suppressedReasons,
|
||||
suppressionScope, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Read a typed string member: absent/null returns null, but a present value of the wrong JSON type
|
||||
// is rejected rather than coerced (asText would turn 123 into "123"), matching AWS.
|
||||
private static String stringMemberOrAbsent(JsonNode parent, String field) {
|
||||
@@ -1096,9 +1131,41 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
node.put("SendingStatus", tenant.sendingStatus());
|
||||
// AWS renders the block as an explicit null when the tenant has none.
|
||||
if (tenant.suppressionAttributes() == null) {
|
||||
node.putNull("SuppressionAttributes");
|
||||
} else {
|
||||
ObjectNode attrs = node.putObject("SuppressionAttributes");
|
||||
ArrayNode reasons = attrs.putArray("SuppressedReasons");
|
||||
for (String reason : tenant.suppressionAttributes().suppressedReasons()) {
|
||||
reasons.add(reason);
|
||||
}
|
||||
attrs.put("SuppressionScope", tenant.suppressionAttributes().suppressionScope());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// Parse an optional array of strings: absent/null returns null, an empty array stays an empty
|
||||
// list (the distinction matters for the suppression-attributes pair rules), and a non-string
|
||||
// element is rejected rather than coerced.
|
||||
private static List<String> stringArrayOrAbsent(JsonNode parent, String field) {
|
||||
JsonNode n = parent.path(field);
|
||||
if (n.isMissingNode() || n.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!n.isArray()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
List<String> values = new ArrayList<>();
|
||||
for (JsonNode item : n) {
|
||||
if (!item.isTextual()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
values.add(item.textValue());
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/templates")
|
||||
public Response createEmailTemplate(@Context HttpHeaders headers, String body) {
|
||||
@@ -2371,7 +2438,8 @@ public class SesController {
|
||||
requireJsonObject(request);
|
||||
String emailAddress = readRequiredStringField(request, "EmailAddress");
|
||||
String reason = readRequiredStringField(request, "Reason");
|
||||
sesService.putSuppressedDestination(region, emailAddress, reason);
|
||||
String tenantName = stringMemberOrAbsent(request, "TenantName");
|
||||
sesService.putSuppressedDestination(region, emailAddress, reason, tenantName);
|
||||
LOG.infov("SES V2 PutSuppressedDestination: {0} ({1})", emailAddress, reason);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -2392,10 +2460,12 @@ public class SesController {
|
||||
@GET
|
||||
@Path("/suppression/addresses/{emailAddress}")
|
||||
public Response getSuppressedDestination(@Context HttpHeaders headers,
|
||||
@PathParam("emailAddress") String emailAddress) {
|
||||
@PathParam("emailAddress") String emailAddress,
|
||||
@QueryParam("TenantName") String tenantName) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
SuppressedDestination suppressed = sesService.getSuppressedDestination(region, emailAddress);
|
||||
SuppressedDestination suppressed =
|
||||
sesService.getSuppressedDestination(region, emailAddress, tenantName);
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ObjectNode entry = result.putObject("SuppressedDestination");
|
||||
entry.put("EmailAddress", suppressed.getEmailAddress());
|
||||
@@ -2403,6 +2473,8 @@ public class SesController {
|
||||
if (suppressed.getLastUpdateTime() != null) {
|
||||
entry.put("LastUpdateTime", suppressed.getLastUpdateTime().getEpochSecond());
|
||||
}
|
||||
// AWS renders TenantName on every entry — an explicit null for account-level ones.
|
||||
entry.put("TenantName", suppressed.getTenantName());
|
||||
return Response.ok(result).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
@@ -2412,10 +2484,11 @@ public class SesController {
|
||||
@DELETE
|
||||
@Path("/suppression/addresses/{emailAddress}")
|
||||
public Response deleteSuppressedDestination(@Context HttpHeaders headers,
|
||||
@PathParam("emailAddress") String emailAddress) {
|
||||
@PathParam("emailAddress") String emailAddress,
|
||||
@QueryParam("TenantName") String tenantName) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
sesService.deleteSuppressedDestination(region, emailAddress);
|
||||
sesService.deleteSuppressedDestination(region, emailAddress, tenantName);
|
||||
LOG.infov("SES V2 DeleteSuppressedDestination: {0}", emailAddress);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -2426,11 +2499,12 @@ public class SesController {
|
||||
@GET
|
||||
@Path("/suppression/addresses")
|
||||
public Response listSuppressedDestinations(@Context HttpHeaders headers,
|
||||
@QueryParam("Reason") List<String> reasons) {
|
||||
@QueryParam("Reason") List<String> reasons,
|
||||
@QueryParam("TenantName") String tenantName) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<SuppressedDestination> entries;
|
||||
try {
|
||||
entries = sesService.listSuppressedDestinations(region, reasons);
|
||||
entries = sesService.listSuppressedDestinations(region, reasons, tenantName);
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
|
||||
@@ -1658,8 +1658,15 @@ public class SesService {
|
||||
// ──────────────────────── Tenants (multi-tenancy) ────────────────────────
|
||||
// Tenants live in SesTenantService; the facade forwards.
|
||||
|
||||
public Tenant createTenant(String tenantName, List<Tag> tags, String accountId, String region) {
|
||||
return tenantService.createTenant(tenantName, tags, accountId, region);
|
||||
public Tenant createTenant(String tenantName, List<Tag> tags, List<String> suppressedReasons,
|
||||
String suppressionScope, String accountId, String region) {
|
||||
return tenantService.createTenant(tenantName, tags, suppressedReasons, suppressionScope,
|
||||
accountId, region);
|
||||
}
|
||||
|
||||
public void putTenantSuppressionAttributes(String tenantName, List<String> suppressedReasons,
|
||||
String suppressionScope, String region) {
|
||||
tenantService.putSuppressionAttributes(tenantName, suppressedReasons, suppressionScope, region);
|
||||
}
|
||||
|
||||
public Tenant getTenant(String tenantName, String region) {
|
||||
@@ -1671,7 +1678,10 @@ public class SesService {
|
||||
}
|
||||
|
||||
public void deleteTenant(String tenantName, String region) {
|
||||
tenantService.deleteTenant(tenantName, region);
|
||||
// The tenant-scoped suppression entries live in the suppression domain; the callback runs
|
||||
// their cascade inside the tenant lock, before the associations and the tenant record.
|
||||
tenantService.deleteTenant(tenantName, region,
|
||||
tenant -> suppressionService.deleteAllForTenant(region, tenant.tenantId()));
|
||||
}
|
||||
|
||||
// The association operations validate resource existence here in the facade — the tenant domain
|
||||
@@ -2372,20 +2382,78 @@ public class SesService {
|
||||
suppressionService.putAccountSuppressionAttributes(region, suppressedReasons);
|
||||
}
|
||||
|
||||
// A TenantName routes each suppression-list operation to that tenant's own list (fully separate
|
||||
// from the account list on AWS); the reason/address validation still runs first, matching the
|
||||
// probed precedence where request validation precedes tenant existence.
|
||||
|
||||
public void putSuppressedDestination(String region, String emailAddress, String reason) {
|
||||
suppressionService.putSuppressedDestination(region, emailAddress, reason);
|
||||
putSuppressedDestination(region, emailAddress, reason, null);
|
||||
}
|
||||
|
||||
public SuppressedDestination getSuppressedDestination(String region, String emailAddress) {
|
||||
return suppressionService.getSuppressedDestination(region, emailAddress);
|
||||
return getSuppressedDestination(region, emailAddress, null);
|
||||
}
|
||||
|
||||
public void deleteSuppressedDestination(String region, String emailAddress) {
|
||||
suppressionService.deleteSuppressedDestination(region, emailAddress);
|
||||
deleteSuppressedDestination(region, emailAddress, null);
|
||||
}
|
||||
|
||||
public List<SuppressedDestination> listSuppressedDestinations(String region, List<String> reasonFilters) {
|
||||
return suppressionService.listSuppressedDestinations(region, reasonFilters);
|
||||
public List<SuppressedDestination> listSuppressedDestinations(String region,
|
||||
List<String> reasonFilters) {
|
||||
return listSuppressedDestinations(region, reasonFilters, null);
|
||||
}
|
||||
|
||||
public void putSuppressedDestination(String region, String emailAddress, String reason,
|
||||
String tenantName) {
|
||||
if (tenantName == null) {
|
||||
suppressionService.putSuppressedDestination(region, emailAddress, reason);
|
||||
return;
|
||||
}
|
||||
// The address and reason are validated before the tenant is resolved, keeping request
|
||||
// validation ahead of tenant existence for every member, as on the attribute operations.
|
||||
SesSuppressionService.normalizeSuppressionEmail(emailAddress);
|
||||
SesSuppressionService.validateSuppressionReason(reason, "reason", false);
|
||||
tenantService.runWithTenant(tenantName, region, tenant -> {
|
||||
suppressionService.putTenantSuppressedDestination(region, tenant.tenantId(),
|
||||
tenantName, emailAddress, reason);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public SuppressedDestination getSuppressedDestination(String region, String emailAddress,
|
||||
String tenantName) {
|
||||
if (tenantName == null) {
|
||||
return suppressionService.getSuppressedDestination(region, emailAddress);
|
||||
}
|
||||
SesSuppressionService.normalizeSuppressionEmail(emailAddress);
|
||||
return tenantService.runWithTenant(tenantName, region, tenant ->
|
||||
suppressionService.getTenantSuppressedDestination(region, tenant.tenantId(),
|
||||
emailAddress));
|
||||
}
|
||||
|
||||
public void deleteSuppressedDestination(String region, String emailAddress, String tenantName) {
|
||||
if (tenantName == null) {
|
||||
suppressionService.deleteSuppressedDestination(region, emailAddress);
|
||||
return;
|
||||
}
|
||||
SesSuppressionService.normalizeSuppressionEmail(emailAddress);
|
||||
tenantService.runWithTenant(tenantName, region, tenant -> {
|
||||
suppressionService.deleteTenantSuppressedDestination(region, tenant.tenantId(),
|
||||
emailAddress);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public List<SuppressedDestination> listSuppressedDestinations(String region,
|
||||
List<String> reasonFilters,
|
||||
String tenantName) {
|
||||
if (tenantName == null) {
|
||||
return suppressionService.listSuppressedDestinations(region, reasonFilters);
|
||||
}
|
||||
SesSuppressionService.validateReasonFilters(reasonFilters);
|
||||
return tenantService.runWithTenant(tenantName, region, tenant ->
|
||||
suppressionService.listTenantSuppressedDestinations(region, tenant.tenantId(),
|
||||
reasonFilters));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,19 +39,24 @@ public class SesSuppressionService {
|
||||
|
||||
private final StorageBackend<String, SuppressedDestination> suppressionStore;
|
||||
private final StorageBackend<String, AccountSuppressionAttributes> accountSuppressionStore;
|
||||
private final StorageBackend<String, SuppressedDestination> tenantSuppressionStore;
|
||||
|
||||
@Inject
|
||||
public SesSuppressionService(StorageFactory storageFactory) {
|
||||
this.suppressionStore = storageFactory.create("ses", "ses-suppression.json",
|
||||
new TypeReference<Map<String, SuppressedDestination>>() {});
|
||||
this.accountSuppressionStore = storageFactory.create("ses", "ses-account-suppression.json",
|
||||
new TypeReference<Map<String, AccountSuppressionAttributes>>() {});
|
||||
this(storageFactory.create("ses", "ses-suppression.json",
|
||||
new TypeReference<Map<String, SuppressedDestination>>() {}),
|
||||
storageFactory.create("ses", "ses-account-suppression.json",
|
||||
new TypeReference<Map<String, AccountSuppressionAttributes>>() {}),
|
||||
storageFactory.create("ses", "ses-tenant-suppression.json",
|
||||
new TypeReference<Map<String, SuppressedDestination>>() {}));
|
||||
}
|
||||
|
||||
SesSuppressionService(StorageBackend<String, SuppressedDestination> suppressionStore,
|
||||
StorageBackend<String, AccountSuppressionAttributes> accountSuppressionStore) {
|
||||
StorageBackend<String, AccountSuppressionAttributes> accountSuppressionStore,
|
||||
StorageBackend<String, SuppressedDestination> tenantSuppressionStore) {
|
||||
this.suppressionStore = suppressionStore;
|
||||
this.accountSuppressionStore = accountSuppressionStore;
|
||||
this.tenantSuppressionStore = tenantSuppressionStore;
|
||||
}
|
||||
|
||||
// ──────────────────── Account-level suppression attributes ────────────────────
|
||||
@@ -168,15 +173,7 @@ public class SesSuppressionService {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Set<String> filters = validateReasonFilters(reasonFilters);
|
||||
String prefix = "suppression::" + region + "::";
|
||||
List<SuppressedDestination> all = new ArrayList<>(suppressionStore.scan(k -> k.startsWith(prefix)));
|
||||
all.sort(Comparator.comparing(SuppressedDestination::getLastUpdateTime,
|
||||
@@ -195,7 +192,101 @@ public class SesSuppressionService {
|
||||
return "suppression::" + region + "::" + emailAddress;
|
||||
}
|
||||
|
||||
private static String normalizeSuppressionEmail(String emailAddress) {
|
||||
// ──────────────────── Tenant-scoped suppression list (Phase 3) ────────────────────
|
||||
// Probe-confirmed 2026-08-30: each tenant's list is fully separate from the account list (they
|
||||
// are mutually invisible), the tenant's SuppressionScope does not gate these operations, and the
|
||||
// not-found message says "tenant suppression list". Keys carry the TenantId, so a recreated
|
||||
// same-name tenant starts with an empty list; DeleteTenant cascades via deleteAllForTenant.
|
||||
// Callers pass through SesTenantService.runWithTenant, which resolves the tenant (404) and
|
||||
// serializes against the DeleteTenant cascade.
|
||||
|
||||
public void putTenantSuppressedDestination(String region, String tenantId, String tenantName,
|
||||
String emailAddress, String reason) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
validateSuppressionReason(reason, "reason", false);
|
||||
SuppressedDestination entry = new SuppressedDestination(normalized, reason);
|
||||
entry.setTenantName(tenantName);
|
||||
tenantSuppressionStore.put(tenantSuppressionKey(region, tenantId, normalized), entry);
|
||||
LOG.infov("Suppressed destination {0} for tenant {1} in region {2} (reason={3})",
|
||||
normalized, tenantName, region, reason);
|
||||
}
|
||||
|
||||
public SuppressedDestination getTenantSuppressedDestination(String region, String tenantId,
|
||||
String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
return tenantSuppressionStore.get(tenantSuppressionKey(region, tenantId, normalized))
|
||||
.orElseThrow(() -> tenantEntryNotFound(normalized));
|
||||
}
|
||||
|
||||
/** Unlike the tenant resource associations, this delete is not idempotent on AWS: a second
|
||||
* delete of the same address is a NotFound. */
|
||||
public void deleteTenantSuppressedDestination(String region, String tenantId, String emailAddress) {
|
||||
String normalized = normalizeSuppressionEmail(emailAddress);
|
||||
String key = tenantSuppressionKey(region, tenantId, normalized);
|
||||
if (tenantSuppressionStore.get(key).isEmpty()) {
|
||||
throw tenantEntryNotFound(normalized);
|
||||
}
|
||||
tenantSuppressionStore.delete(key);
|
||||
LOG.infov("Removed tenant suppression entry for {0} in region {1}", normalized, region);
|
||||
}
|
||||
|
||||
public List<SuppressedDestination> listTenantSuppressedDestinations(String region, String tenantId,
|
||||
List<String> reasonFilters) {
|
||||
Set<String> filters = validateReasonFilters(reasonFilters);
|
||||
String prefix = tenantSuppressionKeyPrefix(region, tenantId);
|
||||
List<SuppressedDestination> all =
|
||||
new ArrayList<>(tenantSuppressionStore.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();
|
||||
}
|
||||
|
||||
/** DeleteTenant's cascade for this domain, run from inside the tenant lock. */
|
||||
public void deleteAllForTenant(String region, String tenantId) {
|
||||
String prefix = tenantSuppressionKeyPrefix(region, tenantId);
|
||||
for (String key : tenantSuppressionStore.keys().stream()
|
||||
.filter(k -> k.startsWith(prefix)).toList()) {
|
||||
tenantSuppressionStore.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException tenantEntryNotFound(String normalizedEmail) {
|
||||
return new AwsException("NotFoundException",
|
||||
"Email address " + normalizedEmail + " does not exist on your tenant suppression list.",
|
||||
404);
|
||||
}
|
||||
|
||||
private static String tenantSuppressionKey(String region, String tenantId, String emailAddress) {
|
||||
return tenantSuppressionKeyPrefix(region, tenantId) + emailAddress;
|
||||
}
|
||||
|
||||
private static String tenantSuppressionKeyPrefix(String region, String tenantId) {
|
||||
return "tenantSuppression::" + region + "::" + tenantId + "::";
|
||||
}
|
||||
|
||||
/** Validates a Reasons filter list, returning the non-blank values; shared by the account and
|
||||
* tenant list paths, and by the facade to keep request validation ahead of tenant existence. */
|
||||
static Set<String> validateReasonFilters(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filters;
|
||||
}
|
||||
|
||||
static String normalizeSuppressionEmail(String emailAddress) {
|
||||
if (emailAddress == null || emailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "EmailAddress is required.", 400);
|
||||
}
|
||||
@@ -223,7 +314,7 @@ public class SesSuppressionService {
|
||||
* (single Reason field) returns the unwrapped form; the two list-bearing APIs return the wrapped
|
||||
* form. Shared by the two sub-domains above, which is why they were extracted together.
|
||||
*/
|
||||
private static void validateSuppressionReason(String reason, String fieldName, boolean nested) {
|
||||
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]]"
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ses.model.Tag;
|
||||
import io.github.hectorvent.floci.services.ses.model.Tenant;
|
||||
import io.github.hectorvent.floci.services.ses.model.TenantResourceAssociation;
|
||||
import io.github.hectorvent.floci.services.ses.model.TenantSuppressionAttributes;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -16,8 +17,12 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -30,8 +35,10 @@ import java.util.regex.Pattern;
|
||||
* AlreadyExists message), not the key.
|
||||
*
|
||||
* <p>Phase 2 adds tenant→resource associations, owned here as well ({@code associationStore}); the
|
||||
* facade validates that the referenced resource exists before delegating. Tenant suppression and
|
||||
* tenant-scoped sending are separate follow-ups.
|
||||
* facade validates that the referenced resource exists before delegating. Phase 3 adds the tenant
|
||||
* suppression attributes (stored on the tenant record) — the tenant-scoped suppression list itself
|
||||
* lives in {@code SesSuppressionService} with the account list, and cascades through the
|
||||
* {@code DeleteTenant} callback. Tenant-scoped sending is a separate follow-up.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class SesTenantService {
|
||||
@@ -87,12 +94,20 @@ public class SesTenantService {
|
||||
}
|
||||
|
||||
public Tenant createTenant(String tenantName, List<Tag> tags, String accountId, String region) {
|
||||
validateTenantName(tenantName);
|
||||
return createTenant(tenantName, tags, null, null, accountId, region);
|
||||
}
|
||||
|
||||
public Tenant createTenant(String tenantName, List<Tag> tags, List<String> suppressedReasons,
|
||||
String suppressionScope, String accountId, String region) {
|
||||
validateTenantName(tenantName, true);
|
||||
SesTags.validate(tags);
|
||||
TenantSuppressionAttributes attrs =
|
||||
validateSuppressionAttributesPair(suppressedReasons, suppressionScope);
|
||||
String key = tenantKey(region, tenantName);
|
||||
String tenantId = generateTenantId();
|
||||
String tenantArn = "arn:aws:ses:" + region + ":" + accountId + ":tenant/" + tenantName + "/" + tenantId;
|
||||
Tenant tenant = new Tenant(tenantName, tenantId, tenantArn, Instant.now(clock), tags, "ENABLED");
|
||||
Tenant tenant = new Tenant(tenantName, tenantId, tenantArn, Instant.now(clock), tags,
|
||||
"ENABLED", attrs);
|
||||
// Only the check-then-put needs to be atomic, so two concurrent creates for the same name can't
|
||||
// both observe the key as absent; the id/ARN/record are built outside the lock.
|
||||
synchronized (tenantMutationLock) {
|
||||
@@ -110,7 +125,7 @@ public class SesTenantService {
|
||||
public Tenant getTenant(String tenantName, String region) {
|
||||
// TenantName is a required, min-length-1 member, so a malformed name is a BadRequest, not a
|
||||
// lookup miss.
|
||||
validateTenantName(tenantName);
|
||||
validateTenantName(tenantName, false);
|
||||
return tenantStore.get(tenantKey(region, tenantName))
|
||||
.orElseThrow(() -> tenantNotFound(tenantName));
|
||||
}
|
||||
@@ -125,15 +140,25 @@ public class SesTenantService {
|
||||
}
|
||||
|
||||
public void deleteTenant(String tenantName, String region) {
|
||||
validateTenantName(tenantName);
|
||||
deleteTenant(tenantName, region, tenant -> { });
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the tenant. {@code dependentCascade} runs inside the lock with the resolved tenant so
|
||||
* the facade can cascade state held by other domains (the tenant suppression list); like the
|
||||
* associations, it runs before the tenant record is removed — persistent/wal backends apply each
|
||||
* deletion durably, so a crash mid-cascade must leave the tenant record (a retryable
|
||||
* DeleteTenant) rather than orphans no API call can remove.
|
||||
*/
|
||||
public void deleteTenant(String tenantName, String region, Consumer<Tenant> dependentCascade) {
|
||||
validateTenantName(tenantName, false);
|
||||
String key = tenantKey(region, tenantName);
|
||||
synchronized (tenantMutationLock) {
|
||||
Tenant tenant = tenantStore.get(key).orElseThrow(() -> tenantNotFound(tenantName));
|
||||
dependentCascade.accept(tenant);
|
||||
// AWS cascades: deleting a tenant silently removes its resource associations
|
||||
// (probe-confirmed 2026-08-28). Keys carry the TenantId, so a recreated same-name tenant
|
||||
// never sees the old associations. The associations go first — persistent/wal backends
|
||||
// apply each deletion durably, so a crash mid-cascade must leave the tenant record (a
|
||||
// retryable DeleteTenant) rather than orphan associations no API call can remove.
|
||||
// never sees the old associations.
|
||||
String assocPrefix = associationKeyPrefix(region, tenant.tenantId());
|
||||
for (String assocKey : associationStore.keys().stream()
|
||||
.filter(k -> k.startsWith(assocPrefix)).toList()) {
|
||||
@@ -159,12 +184,9 @@ public class SesTenantService {
|
||||
* service-level message instead of the Smithy one.
|
||||
*/
|
||||
public Tenant tenantForAssociation(String tenantName, String region) {
|
||||
if (tenantName == null) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tenantName' failed to satisfy constraint: "
|
||||
+ "Member must not be null", 400);
|
||||
}
|
||||
if (tenantName.isBlank()) {
|
||||
// Absent and empty both get the service-level message here (probe-confirmed 2026-08-30) —
|
||||
// the Smithy not-null variant exists only on CreateTenant.
|
||||
if (tenantName == null || tenantName.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
}
|
||||
return tenantStore.get(tenantKey(region, tenantName))
|
||||
@@ -344,19 +366,116 @@ public class SesTenantService {
|
||||
return "tenantAssoc::" + region + "::" + tenantId + "::";
|
||||
}
|
||||
|
||||
// Validation order and messages verified against real AWS (2026-08-22): an empty string is the
|
||||
// Smithy min-length violation, a whitespace-only value is "cannot be empty", then length, then the
|
||||
// character-set rule.
|
||||
private static void validateTenantName(String name) {
|
||||
if (name == null) {
|
||||
// ──────────────────────── Suppression attributes (Phase 3) ────────────────────────
|
||||
// Behavior and messages probe-confirmed against real AWS us-east-1, 2026-08-30.
|
||||
|
||||
/**
|
||||
* {@code PutTenantSuppressionAttributes}: both members set the block (an empty reason list is a
|
||||
* valid state), neither member clears it. Observed precedence: empty TenantName, then the Smithy
|
||||
* enum checks, then duplicates, then the pair rules, then tenant existence — a put on a missing
|
||||
* tenant still gets its request validated first.
|
||||
*/
|
||||
public void putSuppressionAttributes(String tenantName, List<String> suppressedReasons,
|
||||
String suppressionScope, String region) {
|
||||
// Unlike CreateTenant, an absent TenantName gets the same service-level message as an empty
|
||||
// one here (probe-confirmed 2026-08-30) — no Smithy not-null variant on this operation.
|
||||
if (tenantName == null || tenantName.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
}
|
||||
TenantSuppressionAttributes attrs =
|
||||
validateSuppressionAttributesPair(suppressedReasons, suppressionScope);
|
||||
String key = tenantKey(region, tenantName);
|
||||
synchronized (tenantMutationLock) {
|
||||
Tenant tenant = tenantStore.get(key).orElseThrow(() -> tenantNotFound(tenantName));
|
||||
tenantStore.put(key, tenant.withSuppressionAttributes(attrs));
|
||||
}
|
||||
LOG.infov("Updated suppression attributes of SES tenant {0} in region {1}: {2}",
|
||||
tenantName, region, attrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the tenant and runs {@code action} under the shared lock — the tenant-scoped
|
||||
* suppression-list operations go through this so a concurrent {@code DeleteTenant} cascade
|
||||
* cannot interleave and leave entries for a deleted tenant.
|
||||
*/
|
||||
public <T> T runWithTenant(String tenantName, String region, Function<Tenant, T> action) {
|
||||
// Same service-level message for null and blank: on the operations served here the probed
|
||||
// AWS behavior collapses an absent TenantName to "cannot be empty" (only CreateTenant has
|
||||
// the Smithy not-null variant).
|
||||
if (tenantName == null || tenantName.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
}
|
||||
synchronized (tenantMutationLock) {
|
||||
Tenant tenant = tenantStore.get(tenantKey(region, tenantName))
|
||||
.orElseThrow(() -> tenantNotFound(tenantName));
|
||||
return action.apply(tenant);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the SuppressedReasons/SuppressionScope pair (shared by CreateTenant and
|
||||
* PutTenantSuppressionAttributes) and returns the block to store — {@code null} when neither
|
||||
* member was given. AWS rejects half a pair with member-specific messages, and an empty reason
|
||||
* list without a scope gets its own third wording.
|
||||
*/
|
||||
private static TenantSuppressionAttributes validateSuppressionAttributesPair(
|
||||
List<String> suppressedReasons, String suppressionScope) {
|
||||
if (suppressedReasons != null) {
|
||||
for (String reason : suppressedReasons) {
|
||||
SesSuppressionService.validateSuppressionReason(reason, "suppressedReasons", true);
|
||||
}
|
||||
}
|
||||
if (suppressionScope != null && !"TENANT".equals(suppressionScope)
|
||||
&& !"ACCOUNT".equals(suppressionScope)) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tenantName' failed to satisfy constraint: "
|
||||
+ "Member must not be null", 400);
|
||||
"1 validation error detected: Value at 'suppressionScope' failed to satisfy "
|
||||
+ "constraint: Member must satisfy enum value set: [TENANT, ACCOUNT]", 400);
|
||||
}
|
||||
if (suppressedReasons != null) {
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (String reason : suppressedReasons) {
|
||||
if (!seen.add(reason)) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Each suppressed reason can only be specified at most once", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (suppressedReasons != null && suppressionScope == null) {
|
||||
throw new AwsException("BadRequestException", suppressedReasons.isEmpty()
|
||||
? "SuppressionScope is required when SuppressedReasons are provided. "
|
||||
+ "Valid values are: TENANT, ACCOUNT"
|
||||
: "SuppressedReasons cannot be specified without SuppressionScope.", 400);
|
||||
}
|
||||
if (suppressedReasons == null && suppressionScope != null) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"SuppressionScope cannot be specified without SuppressedReasons.", 400);
|
||||
}
|
||||
if (suppressedReasons == null) {
|
||||
return null;
|
||||
}
|
||||
return new TenantSuppressionAttributes(List.copyOf(suppressedReasons), suppressionScope);
|
||||
}
|
||||
|
||||
// Validation order and messages verified against real AWS (2026-08-22 and 2026-08-30): the two
|
||||
// Smithy wordings (not-null for an absent name, min-length for an empty one) exist ONLY on
|
||||
// CreateTenant; every other tenant operation collapses both to "TenantName cannot be empty".
|
||||
// Then a whitespace-only value is "cannot be empty", then length, then the character-set rule.
|
||||
private static void validateTenantName(String name, boolean createTenantSmithyVariants) {
|
||||
if (name == null) {
|
||||
if (createTenantSmithyVariants) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tenantName' failed to satisfy "
|
||||
+ "constraint: Member must not be null", 400);
|
||||
}
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
}
|
||||
if (name.isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tenantName' failed to satisfy constraint: "
|
||||
+ "Member must have length greater than or equal to 1", 400);
|
||||
if (createTenantSmithyVariants) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tenantName' failed to satisfy "
|
||||
+ "constraint: Member must have length greater than or equal to 1", 400);
|
||||
}
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
}
|
||||
if (name.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "TenantName cannot be empty", 400);
|
||||
|
||||
@@ -19,6 +19,10 @@ public class SuppressedDestination {
|
||||
@JsonProperty("LastUpdateTime")
|
||||
private Instant lastUpdateTime;
|
||||
|
||||
// Set only on tenant-scoped entries; account-level entries keep it null (AWS renders the null).
|
||||
@JsonProperty("TenantName")
|
||||
private String tenantName;
|
||||
|
||||
public SuppressedDestination() {}
|
||||
|
||||
public SuppressedDestination(String emailAddress, String reason) {
|
||||
@@ -35,4 +39,7 @@ public class SuppressedDestination {
|
||||
|
||||
public Instant getLastUpdateTime() { return lastUpdateTime; }
|
||||
public void setLastUpdateTime(Instant lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; }
|
||||
|
||||
public String getTenantName() { return tenantName; }
|
||||
public void setTenantName(String tenantName) { this.tenantName = tenantName; }
|
||||
}
|
||||
@@ -16,5 +16,11 @@ public record Tenant(String tenantName,
|
||||
String tenantArn,
|
||||
Instant createdTimestamp,
|
||||
List<Tag> tags,
|
||||
String sendingStatus) {
|
||||
String sendingStatus,
|
||||
TenantSuppressionAttributes suppressionAttributes) {
|
||||
|
||||
/** Copy with different suppression attributes (records are immutable; the store is replace-only). */
|
||||
public Tenant withSuppressionAttributes(TenantSuppressionAttributes attrs) {
|
||||
return new Tenant(tenantName, tenantId, tenantArn, createdTimestamp, tags, sendingStatus, attrs);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.ses.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A SES v2 tenant's suppression attributes, set by {@code PutTenantSuppressionAttributes} (or on
|
||||
* {@code CreateTenant}). AWS treats the pair as all-or-nothing: both members are set together (an
|
||||
* empty reason list is a valid state) and the whole block is cleared by a put with neither member.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public record TenantSuppressionAttributes(List<String> suppressedReasons, String suppressionScope) {
|
||||
}
|
||||
Reference in new issue
Block a user