mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ses): SMTP relay for SendEmail and SendRawEmail (#534)
* feat(ses): add SMTP relay configuration to EmulatorConfig Add smtp-host, smtp-port, smtp-user, smtp-pass, and smtp-starttls settings to SesServiceConfig. smtp-host is Optional — when absent or empty, relay is disabled and emails are stored only (existing behavior). * feat(ses): implement SMTP relay with Vert.x MailClient and Mime4j Add SmtpRelay that forwards SES emails to a configured SMTP server using a dedicated Vert.x MailClient built from EmulatorConfig settings. Simple emails are sent as MailMessage with From/To/Cc/Bcc/ReplyTo/ Subject/Text/Html. Raw MIME emails are parsed with Apache Mime4j to extract headers and body, then sent via the same MailClient. When SES Destinations is provided, it is used as the authoritative envelope recipient list (MIME Cc headers do not add extra SMTP recipients). Relay is dispatched asynchronously on a dedicated daemon thread with a 30-second send timeout. Failures are logged with stack traces but never affect the API response. The executor and MailClient are only created when smtp-host is configured and ses.enabled is true, and shut down in @PreDestroy. SesService calls SmtpRelay.relay/relayRaw after storing emails so the inspection endpoint always has the data regardless of relay. Tests: SmtpRelayTest (12 cases) covers simple/raw relay, disabled path, error swallowing, base64 decoding, HTML content type, envelope fallback. SesServiceSmtpTest (5 cases) verifies relay integration and store persistence. * docs(ses): document SMTP relay configuration Add SMTP Relay section to docs/services/ses.md with configuration example, docker-compose snippet with Mailpit, and behavioral notes. Add SMTP settings to the YAML sample and environment variable table in docs/configuration/application-yml.md. * fix(ses): preserve body against attachment overwrite and extract Bcc from raw MIME - extractBodyFromEntity now only sets text/html when not already set, so a text-typed attachment in multipart/mixed cannot clobber the body picked up from multipart/alternative. - doRelayRaw now reads Bcc from MIME headers alongside To/Cc when the caller did not supply Destinations. * fix(ses): initialize Vert.x NTLMEngineImpl at run time for native image The vertx-mail-client dependency brings in io.vertx.ext.mail.impl.sasl.NTLMEngineImpl, whose static state embeds a SecureRandom instance. GraalVM native-image rejects build-time Random/SecureRandom objects because the cached seed makes runtime behavior unpredictable. Mark the class for run-time initialization, matching the existing entry for the Apache HttpClient NTLM engine.
This commit is contained in:
1 parent
16fc5c9691
commit
c69580764e
9 files changed
+791
-13
No files matched your search
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.ses.model.Identity;
|
||||
import io.github.hectorvent.floci.services.ses.model.SentEmail;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SesServiceSmtpTest {
|
||||
|
||||
@Mock SmtpRelay smtpRelay;
|
||||
|
||||
private SesService service;
|
||||
private InMemoryStorage<String, SentEmail> emailStore;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
emailStore = new InMemoryStorage<>();
|
||||
service = new SesService(
|
||||
new InMemoryStorage<String, Identity>(),
|
||||
emailStore,
|
||||
new InMemoryStorage<String, Boolean>(),
|
||||
smtpRelay);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_callsRelayWithAllFields() {
|
||||
service.sendEmail("from@example.com",
|
||||
List.of("to@example.com"),
|
||||
List.of("cc@example.com"),
|
||||
List.of("bcc@example.com"),
|
||||
List.of("reply@example.com"),
|
||||
"Subject", "text body", "<p>html</p>", "us-east-1");
|
||||
|
||||
verify(smtpRelay).relay(
|
||||
"from@example.com",
|
||||
List.of("to@example.com"),
|
||||
List.of("cc@example.com"),
|
||||
List.of("bcc@example.com"),
|
||||
List.of("reply@example.com"),
|
||||
"Subject", "text body", "<p>html</p>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_storesAndRelays() {
|
||||
String messageId = service.sendEmail("from@example.com",
|
||||
List.of("to@example.com"), null, null, null,
|
||||
"Subject", "text", null, "us-east-1");
|
||||
|
||||
assertNotNull(messageId);
|
||||
assertFalse(emailStore.scan(k -> true).isEmpty());
|
||||
verify(smtpRelay).relay(any(), any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendRawEmail_callsRelayRaw() {
|
||||
service.sendRawEmail("from@example.com",
|
||||
List.of("to@example.com"), "raw MIME", "us-east-1");
|
||||
|
||||
verify(smtpRelay).relayRaw(
|
||||
"from@example.com",
|
||||
List.of("to@example.com"),
|
||||
"raw MIME");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendRawEmail_storesAndRelays() {
|
||||
String messageId = service.sendRawEmail("from@example.com",
|
||||
List.of("to@example.com"), "raw", "us-east-1");
|
||||
|
||||
assertNotNull(messageId);
|
||||
assertFalse(emailStore.scan(k -> true).isEmpty());
|
||||
verify(smtpRelay).relayRaw(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_relayReceivesCorrectFieldsWithNulls() {
|
||||
service.sendEmail("from@example.com",
|
||||
List.of("to@example.com"),
|
||||
null, null, null,
|
||||
"Subject", null, "<p>html only</p>", "us-east-1");
|
||||
|
||||
verify(smtpRelay).relay(
|
||||
"from@example.com",
|
||||
List.of("to@example.com"),
|
||||
null, null, null,
|
||||
"Subject", null, "<p>html only</p>");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.ext.mail.MailClient;
|
||||
import io.vertx.ext.mail.MailMessage;
|
||||
import io.vertx.ext.mail.MailResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SmtpRelayTest {
|
||||
|
||||
@Mock MailClient mailClient;
|
||||
|
||||
private SmtpRelay enabledRelay() {
|
||||
when(mailClient.sendMail(any(MailMessage.class)))
|
||||
.thenReturn(Future.succeededFuture(new MailResult()));
|
||||
return new SmtpRelay(mailClient, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_whenDisabled_doesNotSend() {
|
||||
SmtpRelay relay = new SmtpRelay(mailClient, false);
|
||||
assertFalse(relay.isEnabled());
|
||||
|
||||
relay.relay("from@example.com", List.of("to@example.com"),
|
||||
null, null, null, "Subject", "text", null);
|
||||
|
||||
verify(mailClient, never()).sendMail(any(MailMessage.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_whenEnabled_sendsMail() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
relay.relay("from@example.com",
|
||||
List.of("to@example.com"),
|
||||
List.of("cc@example.com"),
|
||||
List.of("bcc@example.com"),
|
||||
List.of("reply@example.com"),
|
||||
"Test Subject",
|
||||
"plain text",
|
||||
"<p>html</p>");
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
|
||||
MailMessage sent = captor.getValue();
|
||||
assertEquals("from@example.com", sent.getFrom());
|
||||
assertEquals(List.of("to@example.com"), sent.getTo());
|
||||
assertEquals(List.of("cc@example.com"), sent.getCc());
|
||||
assertEquals(List.of("bcc@example.com"), sent.getBcc());
|
||||
assertEquals("Test Subject", sent.getSubject());
|
||||
assertEquals("plain text", sent.getText());
|
||||
assertEquals("<p>html</p>", sent.getHtml());
|
||||
assertEquals("reply@example.com", sent.getHeaders().get("Reply-To"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_noReplyTo_omitsHeader() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
relay.relay("from@example.com", List.of("to@example.com"),
|
||||
null, null, null, "Subject", "text", null);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertNull(captor.getValue().getHeaders());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_textOnly_setsTextWithoutHtml() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
relay.relay("from@example.com", List.of("to@example.com"),
|
||||
null, null, null, "Subject", "only text", null);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("only text", captor.getValue().getText());
|
||||
assertNull(captor.getValue().getHtml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_htmlOnly_setsHtmlWithoutText() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
relay.relay("from@example.com", List.of("to@example.com"),
|
||||
null, null, null, "Subject", null, "<b>html</b>");
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertNull(captor.getValue().getText());
|
||||
assertEquals("<b>html</b>", captor.getValue().getHtml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relay_mailClientThrows_doesNotPropagate() {
|
||||
SmtpRelay relay = new SmtpRelay(mailClient, true);
|
||||
when(mailClient.sendMail(any(MailMessage.class)))
|
||||
.thenReturn(Future.failedFuture(new RuntimeException("SMTP refused")));
|
||||
|
||||
assertDoesNotThrow(() -> relay.relay("from@example.com",
|
||||
List.of("to@example.com"), null, null, null, "Subject", "text", null));
|
||||
}
|
||||
|
||||
// ── Raw relay ──
|
||||
|
||||
@Test
|
||||
void relayRaw_whenDisabled_doesNotSend() {
|
||||
SmtpRelay relay = new SmtpRelay(mailClient, false);
|
||||
|
||||
relay.relayRaw("from@example.com", List.of("to@example.com"), "raw");
|
||||
|
||||
verify(mailClient, never()).sendMail(any(MailMessage.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_parsesHeadersAndBody() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: sender@example.com\r\n"
|
||||
+ "To: to@example.com\r\n"
|
||||
+ "Subject: Raw Test\r\n"
|
||||
+ "Content-Type: text/plain; charset=UTF-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "Raw body content";
|
||||
relay.relayRaw("envelope@example.com", List.of("dest@example.com"), rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
|
||||
MailMessage sent = captor.getValue();
|
||||
assertEquals("sender@example.com", sent.getFrom());
|
||||
assertEquals(List.of("dest@example.com"), sent.getTo());
|
||||
assertEquals("Raw Test", sent.getSubject());
|
||||
assertEquals("Raw body content", sent.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_base64Encoded_decodesFirst() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: s@example.com\r\nTo: t@example.com\r\nSubject: B64\r\n\r\nDecoded body";
|
||||
String encoded = java.util.Base64.getEncoder().encodeToString(
|
||||
rawMime.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
relay.relayRaw("from@example.com", List.of("to@example.com"), encoded);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("B64", captor.getValue().getSubject());
|
||||
assertEquals("Decoded body", captor.getValue().getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_htmlContentType_setsHtml() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: s@example.com\r\nTo: t@example.com\r\nSubject: HTML\r\n"
|
||||
+ "Content-Type: text/html; charset=UTF-8\r\n\r\n<h1>Hello</h1>";
|
||||
relay.relayRaw("from@example.com", List.of("to@example.com"), rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("<h1>Hello</h1>", captor.getValue().getHtml());
|
||||
assertNull(captor.getValue().getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_destinationsEmpty_extractsBccFromHeaders() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: s@example.com\r\n"
|
||||
+ "To: t@example.com\r\n"
|
||||
+ "Cc: c@example.com\r\n"
|
||||
+ "Bcc: b@example.com\r\n"
|
||||
+ "Subject: BccHeader\r\n"
|
||||
+ "Content-Type: text/plain; charset=UTF-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "body";
|
||||
relay.relayRaw("envelope@example.com", null, rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
MailMessage sent = captor.getValue();
|
||||
assertEquals(List.of("t@example.com"), sent.getTo());
|
||||
assertEquals(List.of("c@example.com"), sent.getCc());
|
||||
assertEquals(List.of("b@example.com"), sent.getBcc());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_fallsBackToEnvelopeAddresses() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "Subject: NoAddr\r\n\r\nBody";
|
||||
relay.relayRaw("envelope@example.com", List.of("dest@example.com"), rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("envelope@example.com", captor.getValue().getFrom());
|
||||
assertEquals(List.of("dest@example.com"), captor.getValue().getTo());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_nestedMultipart_extractsTextAndHtml() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: s@example.com\r\n"
|
||||
+ "To: t@example.com\r\n"
|
||||
+ "Subject: Nested\r\n"
|
||||
+ "Content-Type: multipart/mixed; boundary=\"outer\"\r\n"
|
||||
+ "\r\n"
|
||||
+ "--outer\r\n"
|
||||
+ "Content-Type: multipart/alternative; boundary=\"inner\"\r\n"
|
||||
+ "\r\n"
|
||||
+ "--inner\r\n"
|
||||
+ "Content-Type: text/plain; charset=UTF-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "plain text\r\n"
|
||||
+ "--inner\r\n"
|
||||
+ "Content-Type: text/html; charset=UTF-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "<p>html</p>\r\n"
|
||||
+ "--inner--\r\n"
|
||||
+ "--outer--";
|
||||
relay.relayRaw("from@example.com", List.of("to@example.com"), rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("plain text", captor.getValue().getText().trim());
|
||||
assertEquals("<p>html</p>", captor.getValue().getHtml().trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_multipartMixedWithTextAttachment_preservesBody() {
|
||||
SmtpRelay relay = enabledRelay();
|
||||
|
||||
String rawMime = "From: s@example.com\r\n"
|
||||
+ "To: t@example.com\r\n"
|
||||
+ "Subject: Mixed\r\n"
|
||||
+ "Content-Type: multipart/mixed; boundary=\"outer\"\r\n"
|
||||
+ "\r\n"
|
||||
+ "--outer\r\n"
|
||||
+ "Content-Type: text/plain; charset=UTF-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "real body\r\n"
|
||||
+ "--outer\r\n"
|
||||
+ "Content-Type: text/plain; charset=UTF-8\r\n"
|
||||
+ "Content-Disposition: attachment; filename=\"notes.txt\"\r\n"
|
||||
+ "\r\n"
|
||||
+ "attachment content that must not clobber body\r\n"
|
||||
+ "--outer--";
|
||||
relay.relayRaw("from@example.com", List.of("to@example.com"), rawMime);
|
||||
|
||||
ArgumentCaptor<MailMessage> captor = ArgumentCaptor.forClass(MailMessage.class);
|
||||
verify(mailClient).sendMail(captor.capture());
|
||||
assertEquals("real body", captor.getValue().getText().trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
void relayRaw_mailClientThrows_doesNotPropagate() {
|
||||
SmtpRelay relay = new SmtpRelay(mailClient, true);
|
||||
when(mailClient.sendMail(any(MailMessage.class)))
|
||||
.thenReturn(Future.failedFuture(new RuntimeException("SMTP timeout")));
|
||||
|
||||
assertDoesNotThrow(() -> relay.relayRaw("from@example.com",
|
||||
List.of("to@example.com"), "Subject: X\r\n\r\nBody"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user