mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(ses): align /_aws/ses inspection endpoint with LocalStack behavior (#512)
* fix(ses): store text and html body separately and return raw as RawData
The inspection endpoint (/_aws/ses) previously collapsed bodyHtml and
bodyText into a single field, making both text_part and html_part
identical. Split SentEmail.body into bodyText and bodyHtml so the
inspection response correctly distinguishes the two, with null for the
absent part — matching LocalStack 4.14.0 behavior.
Raw emails now use a dedicated rawData field and constructor. The
inspection endpoint returns RawData (the full MIME message) without
Destination, Subject, or Body fields, matching LocalStack's shape.
Tests: 4 new integration tests covering text+html separation, text-only
(html_part=null), html-only (text_part=null), and raw email (RawData
without Body/Subject/Destination).
* fix(ses): parse and store ReplyToAddresses
ReplyToAddresses was silently dropped on both SES v1 (Query) and v2
(REST JSON) send paths. Parse it from the request, store it on
SentEmail, and return it in the inspection endpoint (/_aws/ses) for
non-raw emails. The field is omitted when empty, matching LocalStack
behavior.
* fix(ses): return all regions in inspection endpoint
LocalStack's /_aws/ses returns emails from all regions regardless of
request headers, with each message carrying its own Region field.
Floci previously filtered by the default region, hiding emails sent
to other regions.
- Add region field to SentEmail, set on send.
- SesInspectionController GET now calls getAllEmails() (no region
filter) and reads region from each email.
- DELETE clears all regions.
* refactor(ses): remove unused RegionResolver and use putNull for region
SesInspectionController no longer filters by region, so the injected
RegionResolver is dead code. Remove it and align the null-region
handling with other optional fields (putNull instead of empty string).
* fix(ses): use bulk clear and add v1 ReplyToAddresses test
- clearAllEmails() now calls emailStore.clear() instead of deleting
entries one-by-one, avoiding N disk writes on persistent backends.
- New SesIntegrationTest covering the SES v1 Query path for
ReplyToAddresses: sends via SendEmail with ReplyToAddresses.member.1
and verifies round-trip through the /_aws/ses inspection endpoint.
* refactor(ses): simplify getEmails/clearEmails to region-agnostic
The inspection endpoint now returns all regions unconditionally, so
the region-scoped getEmails(region) and clearEmails(region) are dead
code. Replace them with parameter-less getEmails() and clearEmails(),
and drop the intermediate getAllEmails/clearAllEmails aliases.
* fix(ses): add backward-compat alias for legacy Body field and multi-region test
- @JsonAlias("Body") on bodyText so persisted SentEmail objects from
before the text/html split still deserialize their body into bodyText
rather than silently losing it.
- New test: send emails in two different regions (us-east-1 and
ap-northeast-1) and verify /_aws/ses returns both with correct
per-message Region values.
* refactor(ses): move region and replyToAddresses into SentEmail constructors
Both fields were set via setters immediately after construction, which
is inconsistent with the other fields that are constructor arguments.
Add them to the Simple and Raw constructors so all required state is
established at creation time.
* refactor(ses): align sendEmail parameter order with SentEmail constructor
Move region to the first parameter position so the argument order
matches the SentEmail constructor (messageId, region, source, to, cc,
bcc, replyTo, subject, bodyText, bodyHtml). No functional change.
* refactor(ses): remove unused RegionResolver and validate rawMessage
- SesService no longer uses RegionResolver (region is always passed as
a parameter from controllers). Remove the field, constructor parameter,
and import.
- sendRawEmail now rejects null/blank rawMessage with
InvalidParameterValue, preventing a stored email that would appear
as non-raw in the inspection endpoint.
* fix(ses): validate source in sendRawEmail
sendRawEmail lacked the null/blank check on source that sendEmail
already had, allowing stored messages with Source=null and a 200
response. Add the same guard for consistency.
* refactor(ses): remove unused EmulatorConfig from SesService constructor
The config parameter was injected but never referenced.
* revert: restore EmulatorConfig in SesService constructor
Removing the unused EmulatorConfig parameter caused an awscli compat
test failure (SES: verify identities deleted). The parameter likely
influences Quarkus CDI initialization ordering — StorageFactory may
depend on EmulatorConfig being resolved first. Restore with
@SuppressWarnings("unused") to document intent.
* fix(ses): omit empty CcAddresses and BccAddresses from inspection response
LocalStack omits these keys when the list is empty rather than returning
an empty array. Apply the same !isEmpty() guard already used for
ReplyToAddresses.
* fix(ses): omit empty ToAddresses and restore region parameter order
- Apply the same !isEmpty() guard to ToAddresses so it is omitted from
the inspection response when empty, consistent with CcAddresses,
BccAddresses, and ReplyToAddresses.
- Revert sendEmail parameter order: move region back to the last
position to match the convention used by all other SesService methods
(verifyEmailIdentity, deleteIdentity, listIdentities, etc.).
* refactor(ses): remove unused EmulatorConfig from SesService constructor
The config parameter was injected but never referenced. A previous
attempt to remove it (76d6c22) was reverted due to a CI failure, but
that failure may have been flaky (S3 Notifications queue filter
round-trip, a known pre-existing issue). Retrying after rebase onto
latest main.
This commit is contained in:
1 parent
0f70ff75a8
commit
4719269df4
7 files changed
+463
-63
No files matched your search
@@ -201,6 +201,7 @@ public class SesController {
|
||||
List<String> toAddresses = jsonArrayToList(destination.path("ToAddresses"));
|
||||
List<String> ccAddresses = jsonArrayToList(destination.path("CcAddresses"));
|
||||
List<String> bccAddresses = jsonArrayToList(destination.path("BccAddresses"));
|
||||
List<String> replyToAddresses = jsonArrayToList(request.path("ReplyToAddresses"));
|
||||
|
||||
JsonNode content = request.path("Content");
|
||||
String messageId;
|
||||
@@ -223,12 +224,12 @@ public class SesController {
|
||||
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);
|
||||
bccAddresses, replyToAddresses, 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);
|
||||
bccAddresses, replyToAddresses, subject, templateData, null, region);
|
||||
} else {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Content must contain Raw, Simple, or Template.", 400);
|
||||
|
||||
+44
-27
@@ -1,6 +1,5 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.ses.model.SentEmail;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@@ -25,21 +24,17 @@ import java.util.List;
|
||||
public class SesInspectionController {
|
||||
|
||||
private final SesService sesService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public SesInspectionController(SesService sesService, RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
public SesInspectionController(SesService sesService, ObjectMapper objectMapper) {
|
||||
this.sesService = sesService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@GET
|
||||
public Response getEmails(@QueryParam("id") String messageId) {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
List<SentEmail> emails = sesService.getEmails(region);
|
||||
List<SentEmail> emails = sesService.getEmails();
|
||||
|
||||
ArrayNode messages = objectMapper.createArrayNode();
|
||||
for (SentEmail email : emails) {
|
||||
@@ -48,28 +43,51 @@ public class SesInspectionController {
|
||||
}
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Id", email.getMessageId());
|
||||
node.put("Region", region);
|
||||
if (email.getRegion() != null) {
|
||||
node.put("Region", email.getRegion());
|
||||
} else {
|
||||
node.putNull("Region");
|
||||
}
|
||||
node.put("Source", email.getSource());
|
||||
|
||||
ObjectNode destination = node.putObject("Destination");
|
||||
if (email.getToAddresses() != null) {
|
||||
ArrayNode toArr = destination.putArray("ToAddresses");
|
||||
email.getToAddresses().forEach(toArr::add);
|
||||
}
|
||||
if (email.getCcAddresses() != null) {
|
||||
ArrayNode ccArr = destination.putArray("CcAddresses");
|
||||
email.getCcAddresses().forEach(ccArr::add);
|
||||
}
|
||||
if (email.getBccAddresses() != null) {
|
||||
ArrayNode bccArr = destination.putArray("BccAddresses");
|
||||
email.getBccAddresses().forEach(bccArr::add);
|
||||
}
|
||||
if (email.isRaw()) {
|
||||
// LocalStack returns RawData for raw emails, without
|
||||
// Destination / Subject / Body fields.
|
||||
node.put("RawData", email.getRawData());
|
||||
} else {
|
||||
ObjectNode destination = node.putObject("Destination");
|
||||
if (email.getToAddresses() != null && !email.getToAddresses().isEmpty()) {
|
||||
ArrayNode toArr = destination.putArray("ToAddresses");
|
||||
email.getToAddresses().forEach(toArr::add);
|
||||
}
|
||||
if (email.getCcAddresses() != null && !email.getCcAddresses().isEmpty()) {
|
||||
ArrayNode ccArr = destination.putArray("CcAddresses");
|
||||
email.getCcAddresses().forEach(ccArr::add);
|
||||
}
|
||||
if (email.getBccAddresses() != null && !email.getBccAddresses().isEmpty()) {
|
||||
ArrayNode bccArr = destination.putArray("BccAddresses");
|
||||
email.getBccAddresses().forEach(bccArr::add);
|
||||
}
|
||||
|
||||
node.put("Subject", email.getSubject());
|
||||
if (email.getReplyToAddresses() != null && !email.getReplyToAddresses().isEmpty()) {
|
||||
ArrayNode replyTo = node.putArray("ReplyToAddresses");
|
||||
email.getReplyToAddresses().forEach(replyTo::add);
|
||||
}
|
||||
|
||||
ObjectNode body = node.putObject("Body");
|
||||
body.put("text_part", email.getBody() != null ? email.getBody() : "");
|
||||
body.put("html_part", email.getBody() != null ? email.getBody() : "");
|
||||
node.put("Subject", email.getSubject());
|
||||
|
||||
ObjectNode body = node.putObject("Body");
|
||||
if (email.getBodyText() != null) {
|
||||
body.put("text_part", email.getBodyText());
|
||||
} else {
|
||||
body.putNull("text_part");
|
||||
}
|
||||
if (email.getBodyHtml() != null) {
|
||||
body.put("html_part", email.getBodyHtml());
|
||||
} else {
|
||||
body.putNull("html_part");
|
||||
}
|
||||
}
|
||||
|
||||
if (email.getSentAt() != null) {
|
||||
node.put("Timestamp", email.getSentAt().toString());
|
||||
@@ -85,8 +103,7 @@ public class SesInspectionController {
|
||||
|
||||
@DELETE
|
||||
public Response clearEmails() {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
sesService.clearEmails(region);
|
||||
sesService.clearEmails();
|
||||
return Response.ok().build();
|
||||
}
|
||||
}
|
||||
@@ -125,12 +125,13 @@ public class SesQueryHandler {
|
||||
List<String> toAddresses = extractMembers(params, "Destination.ToAddresses");
|
||||
List<String> ccAddresses = extractMembers(params, "Destination.CcAddresses");
|
||||
List<String> bccAddresses = extractMembers(params, "Destination.BccAddresses");
|
||||
List<String> replyToAddresses = extractMembers(params, "ReplyToAddresses");
|
||||
String subject = getParam(params, "Message.Subject.Data");
|
||||
String bodyText = getParam(params, "Message.Body.Text.Data");
|
||||
String bodyHtml = getParam(params, "Message.Body.Html.Data");
|
||||
|
||||
String messageId = sesService.sendEmail(source, toAddresses, ccAddresses, bccAddresses,
|
||||
subject, bodyText, bodyHtml, region);
|
||||
replyToAddresses, subject, bodyText, bodyHtml, region);
|
||||
|
||||
String result = new XmlBuilder().elem("MessageId", messageId).build();
|
||||
return Response.ok(AwsQueryResponse.envelope("SendEmail", AwsNamespaces.SES, result)).build();
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ses.model.Identity;
|
||||
@@ -26,28 +24,23 @@ 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
|
||||
public SesService(StorageFactory storageFactory, EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
public SesService(StorageFactory storageFactory) {
|
||||
this.identityStore = storageFactory.create("ses", "ses-identities.json",
|
||||
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) {
|
||||
StorageBackend<String, Boolean> accountSettingsStore) {
|
||||
this.identityStore = identityStore;
|
||||
this.emailStore = emailStore;
|
||||
this.accountSettingsStore = accountSettingsStore;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
public Identity verifyEmailIdentity(String emailAddress, String region) {
|
||||
@@ -101,8 +94,8 @@ public class SesService {
|
||||
}
|
||||
|
||||
public String sendEmail(String source, List<String> toAddresses, List<String> ccAddresses,
|
||||
List<String> bccAddresses, String subject, String bodyText,
|
||||
String bodyHtml, String region) {
|
||||
List<String> bccAddresses, List<String> replyToAddresses,
|
||||
String subject, String bodyText, String bodyHtml, String region) {
|
||||
if (source == null || source.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
}
|
||||
@@ -114,8 +107,8 @@ public class SesService {
|
||||
}
|
||||
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
String body = bodyHtml != null ? bodyHtml : bodyText;
|
||||
SentEmail email = new SentEmail(messageId, source, toAddresses, ccAddresses, bccAddresses, subject, body);
|
||||
SentEmail email = new SentEmail(messageId, region, source, toAddresses, ccAddresses,
|
||||
bccAddresses, replyToAddresses, subject, bodyText, bodyHtml);
|
||||
emailStore.put("email::" + region + "::" + messageId, email);
|
||||
|
||||
LOG.infov("SES email sent: from={0}, to={1}, subject={2}, messageId={3}",
|
||||
@@ -124,10 +117,16 @@ public class SesService {
|
||||
}
|
||||
|
||||
public String sendRawEmail(String source, List<String> destinations, String rawMessage, String region) {
|
||||
if (source == null || source.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
}
|
||||
if (rawMessage == null || rawMessage.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "RawMessage.Data is required.", 400);
|
||||
}
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
SentEmail email = new SentEmail(messageId, source,
|
||||
SentEmail email = new SentEmail(messageId, region, source,
|
||||
destinations != null ? destinations : Collections.emptyList(),
|
||||
null, null, "(raw)", rawMessage);
|
||||
rawMessage);
|
||||
emailStore.put("email::" + region + "::" + messageId, email);
|
||||
|
||||
LOG.infov("SES raw email sent: from={0}, messageId={1}", source, messageId);
|
||||
@@ -196,18 +195,13 @@ public class SesService {
|
||||
return emails;
|
||||
}
|
||||
|
||||
public List<SentEmail> getEmails(String region) {
|
||||
String prefix = "email::" + region + "::";
|
||||
return emailStore.scan(k -> k.startsWith(prefix));
|
||||
public List<SentEmail> getEmails() {
|
||||
return emailStore.scan(k -> k.startsWith("email::"));
|
||||
}
|
||||
|
||||
public void clearEmails(String region) {
|
||||
String prefix = "email::" + region + "::";
|
||||
List<String> keys = new ArrayList<>(emailStore.keys().stream()
|
||||
.filter(k -> k.startsWith(prefix))
|
||||
.toList());
|
||||
keys.forEach(emailStore::delete);
|
||||
LOG.infov("Cleared all SES emails in region {0}", region);
|
||||
public void clearEmails() {
|
||||
emailStore.clear();
|
||||
LOG.info("Cleared all SES emails");
|
||||
}
|
||||
|
||||
public boolean isAccountSendingEnabled(String region) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ses.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
@@ -14,6 +15,9 @@ public class SentEmail {
|
||||
@JsonProperty("MessageId")
|
||||
private String messageId;
|
||||
|
||||
@JsonProperty("Region")
|
||||
private String region;
|
||||
|
||||
@JsonProperty("Source")
|
||||
private String source;
|
||||
|
||||
@@ -29,30 +33,59 @@ public class SentEmail {
|
||||
@JsonProperty("Subject")
|
||||
private String subject;
|
||||
|
||||
@JsonProperty("Body")
|
||||
private String body;
|
||||
@JsonProperty("ReplyToAddresses")
|
||||
private List<String> replyToAddresses;
|
||||
|
||||
@JsonProperty("BodyText")
|
||||
@JsonAlias("Body")
|
||||
private String bodyText;
|
||||
|
||||
@JsonProperty("BodyHtml")
|
||||
private String bodyHtml;
|
||||
|
||||
@JsonProperty("RawData")
|
||||
private String rawData;
|
||||
|
||||
@JsonProperty("SentAt")
|
||||
private Instant sentAt;
|
||||
|
||||
public SentEmail() {}
|
||||
|
||||
public SentEmail(String messageId, String source, List<String> toAddresses,
|
||||
List<String> ccAddresses, List<String> bccAddresses,
|
||||
String subject, String body) {
|
||||
/** Constructor for Simple / Template content. */
|
||||
public SentEmail(String messageId, String region, String source,
|
||||
List<String> toAddresses, List<String> ccAddresses,
|
||||
List<String> bccAddresses, List<String> replyToAddresses,
|
||||
String subject, String bodyText, String bodyHtml) {
|
||||
this.messageId = messageId;
|
||||
this.region = region;
|
||||
this.source = source;
|
||||
this.toAddresses = toAddresses;
|
||||
this.ccAddresses = ccAddresses;
|
||||
this.bccAddresses = bccAddresses;
|
||||
this.replyToAddresses = replyToAddresses;
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
this.bodyText = bodyText;
|
||||
this.bodyHtml = bodyHtml;
|
||||
this.sentAt = Instant.now();
|
||||
}
|
||||
|
||||
/** Constructor for Raw content. */
|
||||
public SentEmail(String messageId, String region, String source,
|
||||
List<String> destinations, String rawData) {
|
||||
this.messageId = messageId;
|
||||
this.region = region;
|
||||
this.source = source;
|
||||
this.toAddresses = destinations;
|
||||
this.rawData = rawData;
|
||||
this.sentAt = Instant.now();
|
||||
}
|
||||
|
||||
public String getMessageId() { return messageId; }
|
||||
public void setMessageId(String messageId) { this.messageId = messageId; }
|
||||
|
||||
public String getRegion() { return region; }
|
||||
public void setRegion(String region) { this.region = region; }
|
||||
|
||||
public String getSource() { return source; }
|
||||
public void setSource(String source) { this.source = source; }
|
||||
|
||||
@@ -68,8 +101,19 @@ public class SentEmail {
|
||||
public String getSubject() { return subject; }
|
||||
public void setSubject(String subject) { this.subject = subject; }
|
||||
|
||||
public String getBody() { return body; }
|
||||
public void setBody(String body) { this.body = body; }
|
||||
public List<String> getReplyToAddresses() { return replyToAddresses; }
|
||||
public void setReplyToAddresses(List<String> replyToAddresses) { this.replyToAddresses = replyToAddresses; }
|
||||
|
||||
public String getBodyText() { return bodyText; }
|
||||
public void setBodyText(String bodyText) { this.bodyText = bodyText; }
|
||||
|
||||
public String getBodyHtml() { return bodyHtml; }
|
||||
public void setBodyHtml(String bodyHtml) { this.bodyHtml = bodyHtml; }
|
||||
|
||||
public String getRawData() { return rawData; }
|
||||
public void setRawData(String rawData) { this.rawData = rawData; }
|
||||
|
||||
public boolean isRaw() { return rawData != null; }
|
||||
|
||||
public Instant getSentAt() { return sentAt; }
|
||||
public void setSentAt(Instant sentAt) { this.sentAt = sentAt; }
|
||||
|
||||
Reference in new issue
Block a user