feat: add SES V2 REST JSON protocol support (#265)

* feat: add SES V2 REST JSON protocol support
* fix: address SES V2 review feedback
This commit is contained in:
Louis Garret authored and GitHub committed 2026-04-07 13:59:28 -05:00
1 parent 13916913e8
commit e7ab687a6a
10 files changed
+1073 -7

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.services.cognito.CognitoOAuthController;
import io.github.hectorvent.floci.services.cognito.CognitoWellKnownController;
import io.github.hectorvent.floci.services.ses.SesController;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
@@ -78,6 +79,7 @@ public class ServiceEnabledFilter implements ContainerRequestFilter {
private String mapCredentialScope(String scope) {
return switch (scope) {
case "execute-api" -> "apigateway";
case "ses", "sesv2" -> "email";
default -> scope;
};
}
@@ -91,6 +93,9 @@ public class ServiceEnabledFilter implements ContainerRequestFilter {
|| CognitoWellKnownController.class.equals(resourceClass)) {
return "cognito-idp";
}
if (SesController.class.equals(resourceClass)) {
return "email";
}
return null;
}
@@ -99,8 +104,10 @@ public class ServiceEnabledFilter implements ContainerRequestFilter {
String target = ctx.getHeaderString("X-Amz-Target");
String contentType = ctx.getMediaType() != null ? ctx.getMediaType().toString() : "";
boolean jsonEndpoint = serviceKeyFromMatchedResource() != null;
String accept = ctx.getHeaderString("Accept");
boolean acceptsJson = accept != null && accept.contains("json");
if (target != null || contentType.contains("json") || jsonEndpoint) {
if (target != null || contentType.contains("json") || jsonEndpoint || acceptsJson) {
return Response.status(400)
.type(MediaType.APPLICATION_JSON)
.entity(new AwsErrorResponse("ServiceNotAvailableException", message))
@@ -0,0 +1,362 @@
package io.github.hectorvent.floci.services.ses;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.ses.model.Identity;
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 jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
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.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* REST JSON controller for the AWS SES V2 API.
* Implements the AWS SES V2 wire protocol at /v2/email/* for the operations
* exposed by this controller.
* Reuses the shared {@link SesService} for business logic shared with other SES
* protocol handlers.
*
* Follows the same pattern as {@code LambdaController}: AwsExceptions are thrown
* directly and converted by the global {@code AwsExceptionMapper}.
*/
@Path("/v2/email")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SesController {
private static final Logger LOG = Logger.getLogger(SesController.class);
private final SesService sesService;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
@Inject
public SesController(SesService sesService, RegionResolver regionResolver,
ObjectMapper objectMapper) {
this.sesService = sesService;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
}
// ──────────────────────────── Identities ────────────────────────────
@POST
@Path("/identities")
public Response createEmailIdentity(@Context HttpHeaders headers, String body) {
String region = regionResolver.resolveRegion(headers);
try {
JsonNode request = objectMapper.readTree(body);
String emailIdentity = request.path("EmailIdentity").asText(null);
if (emailIdentity == null || emailIdentity.isBlank()) {
throw new AwsException("BadRequestException", "EmailIdentity is required.", 400);
}
Identity identity = emailIdentity.contains("@")
? sesService.verifyEmailIdentity(emailIdentity, region)
: sesService.verifyDomainIdentity(emailIdentity, region);
ObjectNode result = objectMapper.createObjectNode();
result.put("IdentityType", toV2IdentityType(identity.getIdentityType()));
result.put("VerifiedForSendingStatus", true);
result.set("DkimAttributes", buildDkimAttributes(identity));
LOG.infov("SES V2 CreateEmailIdentity: {0}", emailIdentity);
return Response.ok(result).build();
} catch (AwsException e) {
throw remapV1Exception(e);
} catch (Exception e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
@GET
@Path("/identities")
public Response listEmailIdentities(@Context HttpHeaders headers) {
String region = regionResolver.resolveRegion(headers);
List<Identity> identities = sesService.listIdentities(null, region);
ObjectNode result = objectMapper.createObjectNode();
ArrayNode items = result.putArray("EmailIdentities");
for (Identity id : identities) {
ObjectNode item = objectMapper.createObjectNode();
item.put("IdentityType", toV2IdentityType(id.getIdentityType()));
item.put("IdentityName", id.getIdentity());
item.put("SendingEnabled", true);
items.add(item);
}
return Response.ok(result).build();
}
@GET
@Path("/identities/{emailIdentity}")
public Response getEmailIdentity(@Context HttpHeaders headers,
@PathParam("emailIdentity") String emailIdentity) {
String region = regionResolver.resolveRegion(headers);
Identity identity = sesService.getIdentityVerificationAttributes(emailIdentity, region);
if (identity == null) {
throw new AwsException("NotFoundException",
"Identity " + emailIdentity + " does not exist.", 404);
}
return Response.ok(buildFullIdentityResponse(identity)).build();
}
@DELETE
@Path("/identities/{emailIdentity}")
public Response deleteEmailIdentity(@Context HttpHeaders headers,
@PathParam("emailIdentity") String emailIdentity) {
String region = regionResolver.resolveRegion(headers);
sesService.deleteIdentity(emailIdentity, region);
LOG.infov("SES V2 DeleteEmailIdentity: {0}", emailIdentity);
return Response.ok(objectMapper.createObjectNode()).build();
}
// ──────────────────────── Identity DKIM ─────────────────────────
@PUT
@Path("/identities/{emailIdentity}/dkim")
public Response putEmailIdentityDkimAttributes(@Context HttpHeaders headers,
@PathParam("emailIdentity") String emailIdentity,
String body) {
String region = regionResolver.resolveRegion(headers);
try {
JsonNode request = objectMapper.readTree(body);
JsonNode signingEnabledNode = request.get("SigningEnabled");
if (signingEnabledNode == null || !signingEnabledNode.isBoolean()) {
throw new AwsException("BadRequestException",
"SigningEnabled must be present and must be a boolean", 400);
}
boolean signingEnabled = signingEnabledNode.booleanValue();
sesService.setDkimAttributes(emailIdentity, signingEnabled, region);
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
throw remapV1Exception(e);
} catch (Exception e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
// ──────────────────── Identity Feedback ─────────────────────────
@PUT
@Path("/identities/{emailIdentity}/feedback")
public Response putEmailIdentityFeedbackAttributes(@Context HttpHeaders headers,
@PathParam("emailIdentity") String emailIdentity,
String body) {
String region = regionResolver.resolveRegion(headers);
try {
JsonNode request = objectMapper.readTree(body);
JsonNode emailForwardingEnabledNode = request.get("EmailForwardingEnabled");
if (emailForwardingEnabledNode == null || !emailForwardingEnabledNode.isBoolean()) {
throw new AwsException("BadRequestException",
"EmailForwardingEnabled must be present and must be a boolean", 400);
}
boolean emailForwardingEnabled = emailForwardingEnabledNode.booleanValue();
sesService.setFeedbackForwardingEnabled(emailIdentity, emailForwardingEnabled, region);
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
throw remapV1Exception(e);
} catch (Exception e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
// ──────────────────────────── Send Email ────────────────────────────
@POST
@Path("/outbound-emails")
public Response sendEmail(@Context HttpHeaders headers, String body) {
String region = regionResolver.resolveRegion(headers);
try {
if (!sesService.isAccountSendingEnabled(region)) {
throw new AwsException("SendingPausedException",
"Account sending is disabled.", 400);
}
JsonNode request = objectMapper.readTree(body);
String fromEmailAddress = request.path("FromEmailAddress").asText(null);
if (fromEmailAddress == null || fromEmailAddress.isBlank()) {
throw new AwsException("BadRequestException",
"FromEmailAddress is required.", 400);
}
JsonNode destination = request.path("Destination");
List<String> toAddresses = jsonArrayToList(destination.path("ToAddresses"));
List<String> ccAddresses = jsonArrayToList(destination.path("CcAddresses"));
List<String> bccAddresses = jsonArrayToList(destination.path("BccAddresses"));
JsonNode content = request.path("Content");
String messageId;
if (content.has("Raw")) {
String rawData = content.path("Raw").path("Data").asText(null);
if (rawData == null || rawData.isBlank()) {
throw new AwsException("BadRequestException",
"Content.Raw.Data is required.", 400);
}
List<String> allDestinations = mergeLists(toAddresses, ccAddresses, bccAddresses);
if (allDestinations.isEmpty()) {
throw new AwsException("BadRequestException",
"At least one destination address is required.", 400);
}
messageId = sesService.sendRawEmail(fromEmailAddress, allDestinations, rawData, region);
} else if (content.has("Simple")) {
JsonNode simple = content.path("Simple");
String subject = simple.path("Subject").path("Data").asText("");
String bodyText = simple.path("Body").path("Text").path("Data").asText(null);
String bodyHtml = simple.path("Body").path("Html").path("Data").asText(null);
messageId = sesService.sendEmail(fromEmailAddress, toAddresses, ccAddresses,
bccAddresses, subject, bodyText, bodyHtml, region);
} else if (content.has("Template")) {
String subject = content.path("Template").path("TemplateName").asText("(template)");
String templateData = content.path("Template").path("TemplateData").asText("");
messageId = sesService.sendEmail(fromEmailAddress, toAddresses, ccAddresses,
bccAddresses, subject, templateData, null, region);
} else {
throw new AwsException("BadRequestException",
"Content must contain Raw, Simple, or Template.", 400);
}
ObjectNode result = objectMapper.createObjectNode();
result.put("MessageId", messageId);
LOG.infov("SES V2 SendEmail: from={0}, to={1}, messageId={2}",
fromEmailAddress, toAddresses, messageId);
return Response.ok(result).build();
} catch (AwsException e) {
throw remapV1Exception(e);
} catch (Exception e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
// ──────────────────────────── Account ────────────────────────────
@GET
@Path("/account")
public Response getAccount(@Context HttpHeaders headers) {
String region = regionResolver.resolveRegion(headers);
long sentCount = sesService.getSentEmailCount(region);
boolean sendingEnabled = sesService.isAccountSendingEnabled(region);
ObjectNode result = objectMapper.createObjectNode();
result.put("DedicatedIpAutoWarmupEnabled", false);
result.put("EnforcementStatus", "HEALTHY");
result.put("ProductionAccessEnabled", true);
result.put("SendingEnabled", sendingEnabled);
ObjectNode sendQuota = result.putObject("SendQuota");
sendQuota.put("Max24HourSend", 200.0);
sendQuota.put("MaxSendRate", 1.0);
sendQuota.put("SentLast24Hours", (double) sentCount);
return Response.ok(result).build();
}
@PUT
@Path("/account/sending")
public Response putAccountSendingAttributes(@Context HttpHeaders headers, String body) {
String region = regionResolver.resolveRegion(headers);
try {
JsonNode request = objectMapper.readTree(body);
JsonNode sendingEnabledNode = request.get("SendingEnabled");
if (sendingEnabledNode == null || !sendingEnabledNode.isBoolean()) {
throw new AwsException("BadRequestException",
"SendingEnabled must be present and must be a boolean", 400);
}
sesService.setAccountSendingEnabled(region, sendingEnabledNode.booleanValue());
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
throw remapV1Exception(e);
} catch (Exception e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
// ──────────────────────────── Helpers ────────────────────────────
private ObjectNode buildFullIdentityResponse(Identity identity) {
ObjectNode result = objectMapper.createObjectNode();
result.put("IdentityType", toV2IdentityType(identity.getIdentityType()));
result.put("VerifiedForSendingStatus",
"Success".equals(identity.getVerificationStatus()));
result.put("VerificationStatus", toV2Status(identity.getVerificationStatus()));
result.put("FeedbackForwardingStatus", identity.isFeedbackForwardingEnabled());
result.set("DkimAttributes", buildDkimAttributes(identity));
ObjectNode mailFromAttributes = result.putObject("MailFromAttributes");
mailFromAttributes.put("MailFromDomain", "");
mailFromAttributes.put("MailFromDomainStatus", "NOT_STARTED");
mailFromAttributes.put("BehaviorOnMxFailure", "USE_DEFAULT_VALUE");
result.putObject("Policies");
result.putArray("Tags");
return result;
}
private ObjectNode buildDkimAttributes(Identity identity) {
ObjectNode dkim = objectMapper.createObjectNode();
dkim.put("SigningEnabled", identity.isDkimEnabled());
dkim.put("Status", toV2Status(identity.getDkimVerificationStatus()));
dkim.putArray("Tokens");
return dkim;
}
private static String toV2IdentityType(String v1Type) {
return "EmailAddress".equals(v1Type) ? "EMAIL_ADDRESS" : "DOMAIN";
}
private static String toV2Status(String v1Status) {
if (v1Status == null) return null;
return switch (v1Status) {
case "Success" -> "SUCCESS";
case "NotStarted" -> "NOT_STARTED";
case "Pending" -> "PENDING";
case "Failed" -> "FAILED";
case "TemporaryFailure" -> "TEMPORARY_FAILURE";
default -> v1Status;
};
}
private List<String> jsonArrayToList(JsonNode arrayNode) {
if (arrayNode == null || arrayNode.isMissingNode() || !arrayNode.isArray()) {
return Collections.emptyList();
}
List<String> list = new ArrayList<>();
arrayNode.forEach(node -> list.add(node.asText()));
return list;
}
private List<String> mergeLists(List<String> to, List<String> cc, List<String> bcc) {
List<String> all = new ArrayList<>(to);
all.addAll(cc);
all.addAll(bcc);
return all;
}
private static AwsException remapV1Exception(AwsException e) {
if ("InvalidParameterValue".equals(e.getErrorCode())) {
return new AwsException("BadRequestException", e.getMessage(), 400);
}
return e;
}
}
@@ -45,7 +45,7 @@ public class SesQueryHandler {
case "SendRawEmail" -> handleSendRawEmail(params, region);
case "GetSendQuota" -> handleGetSendQuota(region);
case "GetSendStatistics" -> handleGetSendStatistics(region);
case "GetAccountSendingEnabled" -> handleGetAccountSendingEnabled();
case "GetAccountSendingEnabled" -> handleGetAccountSendingEnabled(region);
case "ListVerifiedEmailAddresses" -> handleListVerifiedEmailAddresses(region);
case "DeleteVerifiedEmailAddress" -> handleDeleteVerifiedEmailAddress(params, region);
case "SetIdentityNotificationTopic" -> handleSetIdentityNotificationTopic(params, region);
@@ -171,8 +171,9 @@ public class SesQueryHandler {
return Response.ok(AwsQueryResponse.envelope("GetSendStatistics", AwsNamespaces.SES, xml.build())).build();
}
private Response handleGetAccountSendingEnabled() {
String result = new XmlBuilder().elem("Enabled", "true").build();
private Response handleGetAccountSendingEnabled(String region) {
boolean enabled = sesService.isAccountSendingEnabled(region);
String result = new XmlBuilder().elem("Enabled", String.valueOf(enabled)).build();
return Response.ok(AwsQueryResponse.envelope("GetAccountSendingEnabled", AwsNamespaces.SES, result)).build();
}
@@ -25,6 +25,7 @@ public class SesService {
private final StorageBackend<String, Identity> identityStore;
private final StorageBackend<String, SentEmail> emailStore;
private final StorageBackend<String, Boolean> accountSettingsStore;
private final RegionResolver regionResolver;
@Inject
@@ -34,14 +35,18 @@ public class SesService {
new TypeReference<Map<String, Identity>>() {});
this.emailStore = storageFactory.create("ses", "ses-emails.json",
new TypeReference<Map<String, SentEmail>>() {});
this.accountSettingsStore = storageFactory.create("ses", "ses-account-settings.json",
new TypeReference<Map<String, Boolean>>() {});
this.regionResolver = regionResolver;
}
SesService(StorageBackend<String, Identity> identityStore,
StorageBackend<String, SentEmail> emailStore,
StorageBackend<String, Boolean> accountSettingsStore,
RegionResolver regionResolver) {
this.identityStore = identityStore;
this.emailStore = emailStore;
this.accountSettingsStore = accountSettingsStore;
this.regionResolver = regionResolver;
}
@@ -101,7 +106,10 @@ public class SesService {
if (source == null || source.isBlank()) {
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
}
if (toAddresses == null || toAddresses.isEmpty()) {
boolean hasRecipient = (toAddresses != null && !toAddresses.isEmpty())
|| (ccAddresses != null && !ccAddresses.isEmpty())
|| (bccAddresses != null && !bccAddresses.isEmpty());
if (!hasRecipient) {
throw new AwsException("InvalidParameterValue", "At least one destination address is required.", 400);
}
@@ -150,6 +158,31 @@ public class SesService {
return identityStore.get(key).orElse(null);
}
public void setDkimAttributes(String identityValue, boolean signingEnabled, String region) {
String key = identityKey(region, identityValue);
Identity identity = identityStore.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"Identity does not exist: " + identityValue, 404));
identity.setDkimEnabled(signingEnabled);
if (signingEnabled) {
identity.setDkimVerificationStatus("Success");
} else {
identity.setDkimVerificationStatus("NotStarted");
}
identityStore.put(key, identity);
LOG.infov("Updated DKIM attributes for {0}: signingEnabled={1}", identityValue, signingEnabled);
}
public void setFeedbackForwardingEnabled(String identityValue, boolean enabled, String region) {
String key = identityKey(region, identityValue);
Identity identity = identityStore.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"Identity does not exist: " + identityValue, 404));
identity.setFeedbackForwardingEnabled(enabled);
identityStore.put(key, identity);
LOG.infov("Updated feedback forwarding for {0}: enabled={1}", identityValue, enabled);
}
public List<String> getVerifiedEmailAddresses(String region) {
String prefix = "identity::" + region + "::";
List<Identity> all = identityStore.scan(k -> k.startsWith(prefix));
@@ -177,6 +210,15 @@ public class SesService {
LOG.infov("Cleared all SES emails in region {0}", region);
}
public boolean isAccountSendingEnabled(String region) {
return accountSettingsStore.get("sending::" + region).orElse(true);
}
public void setAccountSendingEnabled(String region, boolean enabled) {
accountSettingsStore.put("sending::" + region, enabled);
LOG.infov("Updated account sending enabled for region {0}: {1}", region, enabled);
}
private static String identityKey(String region, String identity) {
return "identity::" + region + "::" + identity;
}
@@ -33,6 +33,9 @@ public class Identity {
@JsonProperty("NotificationAttributes")
private Map<String, String> notificationAttributes = new HashMap<>();
@JsonProperty("FeedbackForwardingEnabled")
private boolean feedbackForwardingEnabled = true;
@JsonProperty("CreatedAt")
private Instant createdAt;
@@ -69,6 +72,9 @@ public class Identity {
public Map<String, String> getNotificationAttributes() { return notificationAttributes; }
public void setNotificationAttributes(Map<String, String> notificationAttributes) { this.notificationAttributes = notificationAttributes; }
public boolean isFeedbackForwardingEnabled() { return feedbackForwardingEnabled; }
public void setFeedbackForwardingEnabled(boolean feedbackForwardingEnabled) { this.feedbackForwardingEnabled = feedbackForwardingEnabled; }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
}