diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index e4bf5ff6a7..e623b00e60 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -41,6 +41,7 @@ import io.github.hectorvent.floci.services.ses.SesDedicatedIpController; import io.github.hectorvent.floci.services.ses.SesSuppressionController; import io.github.hectorvent.floci.services.ses.SesTagController; import io.github.hectorvent.floci.services.ses.SesTemplateController; +import io.github.hectorvent.floci.services.ses.SesTenantController; import io.github.hectorvent.floci.services.appsync.AppSyncController; import io.github.hectorvent.floci.services.rdsdata.RdsDataController; import io.github.hectorvent.floci.services.guardduty.GuardDutyController; @@ -296,7 +297,8 @@ public class ResolvedServiceCatalog { Set.of(SesController.class, SesAccountController.class, SesContactController.class, SesCvetController.class, SesDedicatedIpController.class, SesSuppressionController.class, - SesTagController.class, SesTemplateController.class)), + SesTagController.class, SesTemplateController.class, + SesTenantController.class)), descriptor("es", "opensearch", config.services().opensearch().enabled(), true, "opensearch", storageMode(config.storage().services().opensearch().mode(), config.storage().mode()), config.storage().services().opensearch().flushIntervalMs(), null, ServiceProtocol.REST_JSON, diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java index cca37b3c39..f2b7d7b84d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java @@ -17,8 +17,6 @@ import io.github.hectorvent.floci.services.ses.model.MessageHeader; import io.github.hectorvent.floci.services.ses.model.MessageTag; import io.github.hectorvent.floci.services.ses.model.SuppressionOptions; 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.TrackingOptions; import io.github.hectorvent.floci.services.ses.model.VdmOptions; import com.fasterxml.jackson.databind.JsonNode; @@ -45,7 +43,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseOptionString; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseSendingEnabled; import static io.github.hectorvent.floci.services.ses.SesV2Json.parseSuppressedReasons; @@ -54,7 +51,6 @@ import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; import static io.github.hectorvent.floci.services.ses.SesV2Json.requireObjectOrAbsent; -import static io.github.hectorvent.floci.services.ses.SesV2Json.stringArrayOrAbsent; import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; /** @@ -767,287 +763,6 @@ public class SesController { } } - // ──────────────────────────── Tenants (multi-tenancy) ──────────────────────────── - // The SES v2 tenant operations use RPC-style POST subpaths (/tenants, /tenants/get, /tenants/list, - // /tenants/delete). The service owns id/ARN generation and name validation; the controller only - // parses the request and renders the response. - - @POST - @Path("/tenants") - public Response createTenant(@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 tags = parseTagsArray(request.path("Tags")); - List 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, suppressedReasons, - suppressionScope, accountId, region); - return Response.ok(tenantJson(tenant)).build(); - } catch (AwsException e) { - throw remapV1Exception(e); - } catch (com.fasterxml.jackson.core.JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); - } - } - - @POST - @Path("/tenants/get") - public Response getTenant(@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"); - Tenant tenant = sesService.getTenant(tenantName, region); - ObjectNode result = objectMapper.createObjectNode(); - result.set("Tenant", tenantJson(tenant)); - return Response.ok(result).build(); - } catch (AwsException e) { - throw remapV1Exception(e); - } catch (com.fasterxml.jackson.core.JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); - } - } - - @POST - @Path("/tenants/list") - public Response listTenants(@Context HttpHeaders headers, String body) { - String region = regionResolver.resolveRegion(headers); - try { - // Parse the body so a malformed request is rejected rather than silently accepted. Phase 1 - // returns every tenant in one page; PageSize/NextToken pagination is a follow-up. - JsonNode request = (body == null || body.isBlank()) - ? objectMapper.createObjectNode() - : objectMapper.readTree(body); - requireJsonObject(request); - } catch (com.fasterxml.jackson.core.JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); - } - ObjectNode result = objectMapper.createObjectNode(); - ArrayNode tenants = result.putArray("Tenants"); - for (Tenant t : sesService.listTenants(region)) { - // ListTenants returns the TenantInfo subset (no Tags / SendingStatus). - ObjectNode item = tenants.addObject(); - item.put("TenantName", t.tenantName()); - item.put("TenantId", t.tenantId()); - item.put("TenantArn", t.tenantArn()); - if (t.createdTimestamp() != null) { - item.put("CreatedTimestamp", t.createdTimestamp().toEpochMilli() / 1000.0); - } - } - return Response.ok(result).build(); - } - - @POST - @Path("/tenants/delete") - public Response deleteTenant(@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"); - sesService.deleteTenant(tenantName, 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); - } - } - - // Phase 2: tenant→resource associations. AWS's wire format for ResourceType — in responses and as - // the RESOURCE_TYPE filter value — is the ARN segment (identity / configuration-set / template), - // not the SDK's EMAIL_IDENTITY-style enum spelling; real AWS rejects the enum spelling. - - @POST - @Path("/tenants/resources") - public Response createTenantResourceAssociation(@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"); - String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); - sesService.createTenantResourceAssociation(tenantName, resourceArn, - regionResolver.getAccountId(), 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); - } - } - - @POST - @Path("/tenants/resources/delete") - public Response deleteTenantResourceAssociation(@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"); - String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); - sesService.deleteTenantResourceAssociation(tenantName, resourceArn, - regionResolver.getAccountId(), 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); - } - } - - @POST - @Path("/tenants/resources/list") - public Response listTenantResources(@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"); - String resourceTypeFilter = null; - JsonNode filter = request.path("Filter"); - if (!filter.isMissingNode() && !filter.isNull()) { - if (!filter.isObject()) { - throw new AwsException("SerializationException", null, 400); - } - resourceTypeFilter = stringMemberOrAbsent(filter, "RESOURCE_TYPE"); - } - Integer pageSize = intMemberOrAbsent(request, "PageSize"); - String nextToken = stringMemberOrAbsent(request, "NextToken"); - List associations = sesService.listTenantResources( - tenantName, resourceTypeFilter, pageSize, nextToken, region); - ObjectNode result = objectMapper.createObjectNode(); - // AWS renders NextToken as an explicit null on the last (here: only) page. - result.putNull("NextToken"); - ArrayNode resources = result.putArray("TenantResources"); - for (TenantResourceAssociation a : associations) { - ObjectNode item = resources.addObject(); - item.put("ResourceArn", a.resourceArn()); - item.put("ResourceType", a.resourceType()); - } - return Response.ok(result).build(); - } catch (AwsException e) { - throw remapV1Exception(e); - } catch (com.fasterxml.jackson.core.JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); - } - } - - @POST - @Path("/resources/tenants/list") - public Response listResourceTenants(@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 resourceArn = stringMemberOrAbsent(request, "ResourceArn"); - Integer pageSize = intMemberOrAbsent(request, "PageSize"); - String nextToken = stringMemberOrAbsent(request, "NextToken"); - List associations = sesService.listResourceTenants( - resourceArn, pageSize, nextToken, regionResolver.getAccountId(), region); - ObjectNode result = objectMapper.createObjectNode(); - result.putNull("NextToken"); - ArrayNode tenants = result.putArray("ResourceTenants"); - for (TenantResourceAssociation a : associations) { - // ResourceTenantMetadata has no TenantArn (probe-confirmed). - ObjectNode item = tenants.addObject(); - item.put("TenantName", a.tenantName()); - item.put("TenantId", a.tenantId()); - item.put("ResourceArn", a.resourceArn()); - if (a.associatedTimestamp() != null) { - item.put("AssociatedTimestamp", a.associatedTimestamp().toEpochMilli() / 1000.0); - } - } - return Response.ok(result).build(); - } catch (AwsException e) { - throw remapV1Exception(e); - } catch (com.fasterxml.jackson.core.JsonProcessingException e) { - throw new AwsException("SerializationException", null, 400); - } - } - - // 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 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); - } - } - - private ObjectNode tenantJson(Tenant tenant) { - ObjectNode node = objectMapper.createObjectNode(); - node.put("TenantName", tenant.tenantName()); - node.put("TenantId", tenant.tenantId()); - node.put("TenantArn", tenant.tenantArn()); - if (tenant.createdTimestamp() != null) { - node.put("CreatedTimestamp", tenant.createdTimestamp().toEpochMilli() / 1000.0); - } - if (tenant.tags() != null && !tenant.tags().isEmpty()) { - ArrayNode tags = node.putArray("Tags"); - for (Tag t : tenant.tags()) { - ObjectNode tagNode = tags.addObject(); - tagNode.put("Key", t.key()); - tagNode.put("Value", t.value()); - } - } - 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; - } - // ──────────────────────── Configuration Sets ─────────────────────── @POST diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index bb44a39c07..9392d0f757 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -88,7 +88,9 @@ public class SesService { // controller and the v1 handler call directly for get/list/delete. The facade keeps create and // update for the identity-dependent validation, plus the send path and the tag dispatch. private final SesCvetService cvetService; - // Tenants (multi-tenancy) live in SesTenantService. The facade delegates. + // Tenants (multi-tenancy) live in SesTenantService, which the v2 controller calls directly for + // the tenant record; the facade keeps the associations, the delete cascade, the send-time tenant + // gate and the tenant-scoped suppression routing. private final SesTenantService tenantService; private final SmtpRelay smtpRelay; private final SesEventPublisher eventPublisher; @@ -903,26 +905,9 @@ public class SesService { } // ──────────────────────── Tenants (multi-tenancy) ──────────────────────── - // Tenants live in SesTenantService; the facade forwards. - - public Tenant createTenant(String tenantName, List tags, List suppressedReasons, - String suppressionScope, String accountId, String region) { - return tenantService.createTenant(tenantName, tags, suppressedReasons, suppressionScope, - accountId, region); - } - - public void putTenantSuppressionAttributes(String tenantName, List suppressedReasons, - String suppressionScope, String region) { - tenantService.putSuppressionAttributes(tenantName, suppressedReasons, suppressionScope, region); - } - - public Tenant getTenant(String tenantName, String region) { - return tenantService.getTenant(tenantName, region); - } - - public List listTenants(String region) { - return tenantService.listTenants(region); - } + // Tenants live in SesTenantService, which the v2 controller calls directly for the tenant + // record and its suppression attributes; the facade keeps the resource associations (they + // check the identity, configuration set or template exists) and the delete cascade. public void deleteTenant(String tenantName, String region) { // The tenant-scoped suppression entries live in the suppression domain; the callback runs diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java new file mode 100644 index 0000000000..6cbd13363b --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesTenantController.java @@ -0,0 +1,336 @@ +package io.github.hectorvent.floci.services.ses; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +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 jakarta.inject.Inject; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.jboss.logging.Logger; + +import java.util.List; + +import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent; +import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray; +import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception; +import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject; +import static io.github.hectorvent.floci.services.ses.SesV2Json.stringArrayOrAbsent; +import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; + +/** + * SES V2 tenant endpoints ({@code /v2/email/tenants}, {@code /v2/email/tenant} and + * {@code /v2/email/resources/tenants}), split out of {@link SesController}. Tenant create, get, + * list and suppression attributes call {@link SesTenantService} directly; the resource + * associations and tenant delete go through the {@link SesService} facade, which checks the + * associated identity, configuration set or template exists and cascades the tenant's + * suppression entries, work that spans several domains. + */ +@Path("/v2/email") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class SesTenantController { + + private static final Logger LOG = Logger.getLogger(SesTenantController.class); + + private final SesTenantService tenantService; + private final SesService sesService; + private final RegionResolver regionResolver; + private final ObjectMapper objectMapper; + + @Inject + public SesTenantController(SesTenantService tenantService, SesService sesService, + RegionResolver regionResolver, ObjectMapper objectMapper) { + this.tenantService = tenantService; + this.sesService = sesService; + this.regionResolver = regionResolver; + this.objectMapper = objectMapper; + } + + @POST + @Path("/tenants") + public Response createTenant(@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 tags = parseTagsArray(request.path("Tags")); + List 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 = tenantService.createTenant(tenantName, tags, suppressedReasons, + suppressionScope, accountId, region); + return Response.ok(tenantJson(tenant)).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + @POST + @Path("/tenants/get") + public Response getTenant(@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"); + Tenant tenant = tenantService.getTenant(tenantName, region); + ObjectNode result = objectMapper.createObjectNode(); + result.set("Tenant", tenantJson(tenant)); + return Response.ok(result).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + @POST + @Path("/tenants/list") + public Response listTenants(@Context HttpHeaders headers, String body) { + String region = regionResolver.resolveRegion(headers); + try { + // Parse the body so a malformed request is rejected rather than silently accepted. Phase 1 + // returns every tenant in one page; PageSize/NextToken pagination is a follow-up. + JsonNode request = (body == null || body.isBlank()) + ? objectMapper.createObjectNode() + : objectMapper.readTree(body); + requireJsonObject(request); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + ObjectNode result = objectMapper.createObjectNode(); + ArrayNode tenants = result.putArray("Tenants"); + for (Tenant t : tenantService.listTenants(region)) { + // ListTenants returns the TenantInfo subset (no Tags / SendingStatus). + ObjectNode item = tenants.addObject(); + item.put("TenantName", t.tenantName()); + item.put("TenantId", t.tenantId()); + item.put("TenantArn", t.tenantArn()); + if (t.createdTimestamp() != null) { + item.put("CreatedTimestamp", t.createdTimestamp().toEpochMilli() / 1000.0); + } + } + return Response.ok(result).build(); + } + + @POST + @Path("/tenants/delete") + public Response deleteTenant(@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"); + sesService.deleteTenant(tenantName, region); + return Response.ok(objectMapper.createObjectNode()).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + // AWS's wire format for ResourceType, in responses and as the RESOURCE_TYPE filter value, is the + // ARN segment (identity / configuration-set / template), not the SDK's EMAIL_IDENTITY-style enum + // spelling: real AWS rejects the enum spelling. + @POST + @Path("/tenants/resources") + public Response createTenantResourceAssociation(@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"); + String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); + sesService.createTenantResourceAssociation(tenantName, resourceArn, + regionResolver.getAccountId(), region); + return Response.ok(objectMapper.createObjectNode()).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + @POST + @Path("/tenants/resources/delete") + public Response deleteTenantResourceAssociation(@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"); + String resourceArn = stringMemberOrAbsent(request, "ResourceArn"); + sesService.deleteTenantResourceAssociation(tenantName, resourceArn, + regionResolver.getAccountId(), region); + return Response.ok(objectMapper.createObjectNode()).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + @POST + @Path("/tenants/resources/list") + public Response listTenantResources(@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"); + String resourceTypeFilter = null; + JsonNode filter = request.path("Filter"); + if (!filter.isMissingNode() && !filter.isNull()) { + if (!filter.isObject()) { + throw new AwsException("SerializationException", null, 400); + } + resourceTypeFilter = stringMemberOrAbsent(filter, "RESOURCE_TYPE"); + } + Integer pageSize = intMemberOrAbsent(request, "PageSize"); + String nextToken = stringMemberOrAbsent(request, "NextToken"); + List associations = sesService.listTenantResources( + tenantName, resourceTypeFilter, pageSize, nextToken, region); + ObjectNode result = objectMapper.createObjectNode(); + // AWS renders NextToken as an explicit null on the last (here: only) page. + result.putNull("NextToken"); + ArrayNode resources = result.putArray("TenantResources"); + for (TenantResourceAssociation a : associations) { + ObjectNode item = resources.addObject(); + item.put("ResourceArn", a.resourceArn()); + item.put("ResourceType", a.resourceType()); + } + return Response.ok(result).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + @POST + @Path("/resources/tenants/list") + public Response listResourceTenants(@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 resourceArn = stringMemberOrAbsent(request, "ResourceArn"); + Integer pageSize = intMemberOrAbsent(request, "PageSize"); + String nextToken = stringMemberOrAbsent(request, "NextToken"); + List associations = sesService.listResourceTenants( + resourceArn, pageSize, nextToken, regionResolver.getAccountId(), region); + ObjectNode result = objectMapper.createObjectNode(); + result.putNull("NextToken"); + ArrayNode tenants = result.putArray("ResourceTenants"); + for (TenantResourceAssociation a : associations) { + // ResourceTenantMetadata has no TenantArn (probe-confirmed). + ObjectNode item = tenants.addObject(); + item.put("TenantName", a.tenantName()); + item.put("TenantId", a.tenantId()); + item.put("ResourceArn", a.resourceArn()); + if (a.associatedTimestamp() != null) { + item.put("AssociatedTimestamp", a.associatedTimestamp().toEpochMilli() / 1000.0); + } + } + return Response.ok(result).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + // 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 suppressedReasons = stringArrayOrAbsent(request, "SuppressedReasons"); + String suppressionScope = stringMemberOrAbsent(request, "SuppressionScope"); + tenantService.putSuppressionAttributes(tenantName, suppressedReasons, + suppressionScope, region); + return Response.ok(objectMapper.createObjectNode()).build(); + } catch (AwsException e) { + throw remapV1Exception(e); + } catch (JsonProcessingException e) { + throw new AwsException("SerializationException", null, 400); + } + } + + private ObjectNode tenantJson(Tenant tenant) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("TenantName", tenant.tenantName()); + node.put("TenantId", tenant.tenantId()); + node.put("TenantArn", tenant.tenantArn()); + if (tenant.createdTimestamp() != null) { + node.put("CreatedTimestamp", tenant.createdTimestamp().toEpochMilli() / 1000.0); + } + if (tenant.tags() != null && !tenant.tags().isEmpty()) { + ArrayNode tags = node.putArray("Tags"); + for (Tag t : tenant.tags()) { + ObjectNode tagNode = tags.addObject(); + tagNode.put("Key", t.key()); + tagNode.put("Value", t.value()); + } + } + 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; + } +}