mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ses): implement PutEmailIdentityConfigurationSetAttributes for SES v2 (#1698)
* feat(ses): implement PutEmailIdentityConfigurationSetAttributes for SES v2
Add the v2 PutEmailIdentityConfigurationSetAttributes API and wire an email
identity's default configuration set into the send path so its event
destinations fire. This includes Raw sends with no explicit FromEmailAddress:
the configuration set is re-resolved from the MIME "From" header once parsed,
so an omitted-sender Raw send still routes through the identity default.
Unknown identities surface the AWS-matched error message.
* test(ses): log instead of swallowing cleanup failures in the identity config-set compat test
* docs(ses): clarify default config-set precedence and clear-on-null semantics
* fix(ses): fail send when an identity's default configuration set was deleted
Per the SES Developer Guide, sending through an identity whose default
configuration set has been deleted fails with a bad-request error. The default
resolution was silently dropping the stale association; make it throw
BadRequestException ("Configuration set <name> does not exist.") to match AWS.
This commit is contained in:
1 parent
de13017cd3
commit
312fe377fc
6 files changed
+593
-7
No files matched your search
@@ -258,6 +258,44 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────── Identity Configuration Set ────────────────────
|
||||
|
||||
@PUT
|
||||
@Path("/identities/{emailIdentity}/configuration-set")
|
||||
public Response putEmailIdentityConfigurationSetAttributes(@Context HttpHeaders headers,
|
||||
@PathParam("emailIdentity") String emailIdentity,
|
||||
String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
String configurationSetName = null;
|
||||
if (body != null && !body.isEmpty()) {
|
||||
// Only a truly empty body is "no body". A non-empty body must be a JSON object; a
|
||||
// whitespace-only or otherwise unparseable body is a serialization error (verified
|
||||
// against real AWS: whitespace-only returns SerializationException and does not
|
||||
// clear). Within a valid object, an omitted or explicit-null ConfigurationSetName
|
||||
// clears the association (as does an empty body / {}).
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
if (request == null || !request.isObject()) {
|
||||
throw new AwsException("SerializationException", null, 400);
|
||||
}
|
||||
JsonNode node = request.path("ConfigurationSetName");
|
||||
if (!node.isMissingNode() && !node.isNull()) {
|
||||
if (!node.isTextual()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"ConfigurationSetName must be a JSON string.", 400);
|
||||
}
|
||||
configurationSetName = node.asText();
|
||||
}
|
||||
}
|
||||
sesService.setEmailIdentityConfigurationSet(emailIdentity, configurationSetName, 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);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Send Email ────────────────────────────
|
||||
|
||||
@POST
|
||||
@@ -1553,6 +1591,10 @@ public class SesController {
|
||||
mailFromAttributes.put("MailFromDomainStatus", toV2Status(identity.getMailFromDomainStatus()));
|
||||
}
|
||||
|
||||
if (identity.getConfigurationSetName() != null && !identity.getConfigurationSetName().isEmpty()) {
|
||||
result.put("ConfigurationSetName", identity.getConfigurationSetName());
|
||||
}
|
||||
|
||||
result.putObject("Policies");
|
||||
ArrayNode tags = result.putArray("Tags");
|
||||
for (Tag t : identity.getTags()) {
|
||||
|
||||
@@ -257,7 +257,8 @@ public class SesService {
|
||||
if (!hasRecipient) {
|
||||
throw new AwsException("InvalidParameterValue", "At least one destination address is required.", 400);
|
||||
}
|
||||
validateConfigurationSet(configurationSetName, region);
|
||||
String effectiveConfigSet = resolveDefaultConfigurationSet(configurationSetName, source, region);
|
||||
validateConfigurationSet(effectiveConfigSet, region);
|
||||
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
SentEmail email = new SentEmail(messageId, region, source, toAddresses, ccAddresses,
|
||||
@@ -265,7 +266,7 @@ public class SesService {
|
||||
emailStore.put("email::" + region + "::" + messageId, email);
|
||||
|
||||
List<String> envelope = allRecipients(toAddresses, ccAddresses, bccAddresses);
|
||||
Map<String, String> suppressedReasons = collectSuppressedReasons(envelope, configurationSetName, region);
|
||||
Map<String, String> suppressedReasons = collectSuppressedReasons(envelope, effectiveConfigSet, region);
|
||||
|
||||
List<String> relayedTo = filterUnsuppressed(toAddresses, suppressedReasons);
|
||||
List<String> relayedCc = filterUnsuppressed(ccAddresses, suppressedReasons);
|
||||
@@ -280,7 +281,7 @@ public class SesService {
|
||||
|
||||
LOG.infov("SES email sent: from={0}, to={1}, subject={2}, messageId={3}",
|
||||
source, toAddresses, subject, messageId);
|
||||
publishSendEvents(configurationSetName, messageId, source, subject,
|
||||
publishSendEvents(effectiveConfigSet, messageId, source, subject,
|
||||
toAddresses, ccAddresses, bccAddresses, envelope,
|
||||
suppressedReasons, emailTags, additionalHeaders, region);
|
||||
return messageId;
|
||||
@@ -292,10 +293,11 @@ public class SesService {
|
||||
if (rawMessage == null || rawMessage.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "RawMessage.Data is required.", 400);
|
||||
}
|
||||
validateConfigurationSet(configurationSetName, region);
|
||||
String effectiveConfigSet = resolveDefaultConfigurationSet(configurationSetName, source, region);
|
||||
validateConfigurationSet(effectiveConfigSet, region);
|
||||
boolean hasExplicitDestinations = destinations != null && !destinations.isEmpty();
|
||||
boolean sourceOmitted = source == null || source.isBlank();
|
||||
boolean willPublishEvents = (configurationSetName != null && !configurationSetName.isBlank())
|
||||
boolean willPublishEvents = (effectiveConfigSet != null && !effectiveConfigSet.isBlank())
|
||||
|| !resolveIdentityNotificationTargets(source, region).isEmpty();
|
||||
SmtpRelay.RawMessageHeaders headers = (hasExplicitDestinations && !willPublishEvents && !sourceOmitted)
|
||||
? null
|
||||
@@ -310,6 +312,14 @@ public class SesService {
|
||||
// both with this message.
|
||||
throw new AwsException("InvalidParameterValue", "Missing required header 'From'.", 400);
|
||||
}
|
||||
// FromEmailAddress was omitted, so the configuration set couldn't be resolved from the
|
||||
// sender until the MIME "From" was parsed. Re-resolve from the effective sender now so an
|
||||
// email identity's default configuration set still applies to a Raw send without an
|
||||
// explicit FromEmailAddress.
|
||||
if (sourceOmitted && (configurationSetName == null || configurationSetName.isBlank())) {
|
||||
effectiveConfigSet = resolveDefaultConfigurationSet(configurationSetName, effectiveSource, region);
|
||||
validateConfigurationSet(effectiveConfigSet, region);
|
||||
}
|
||||
List<String> effectiveDestinations = hasExplicitDestinations
|
||||
? destinations
|
||||
: allRecipients(headers.to(), headers.cc(), headers.bcc());
|
||||
@@ -321,7 +331,7 @@ public class SesService {
|
||||
SentEmail email = new SentEmail(messageId, region, effectiveSource, effectiveDestinations, rawMessage);
|
||||
emailStore.put("email::" + region + "::" + messageId, email);
|
||||
|
||||
Map<String, String> suppressedReasons = collectSuppressedReasons(effectiveDestinations, configurationSetName, region);
|
||||
Map<String, String> suppressedReasons = collectSuppressedReasons(effectiveDestinations, effectiveConfigSet, region);
|
||||
|
||||
List<String> relayedDestinations = filterUnsuppressed(effectiveDestinations, suppressedReasons);
|
||||
if (!relayedDestinations.isEmpty()) {
|
||||
@@ -332,7 +342,7 @@ public class SesService {
|
||||
}
|
||||
|
||||
LOG.infov("SES raw email sent: from={0}, messageId={1}", effectiveSource, messageId);
|
||||
publishSendEvents(configurationSetName, messageId, effectiveSource,
|
||||
publishSendEvents(effectiveConfigSet, messageId, effectiveSource,
|
||||
headers != null ? headers.subject() : "",
|
||||
headers != null ? headers.to() : List.of(),
|
||||
headers != null ? headers.cc() : List.of(),
|
||||
@@ -738,6 +748,73 @@ public class SesService {
|
||||
LOG.infov("Updated feedback forwarding for {0}: enabled={1}", identityValue, enabled);
|
||||
}
|
||||
|
||||
public void setEmailIdentityConfigurationSet(String identityValue, String configurationSetName,
|
||||
String region) {
|
||||
String key = identityKey(region, identityValue);
|
||||
Identity identity = identityStore.get(key)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
"Identity <" + identityValue + "> does not exist.", 404));
|
||||
boolean clearing = configurationSetName == null || configurationSetName.isEmpty();
|
||||
if (!clearing) {
|
||||
getConfigurationSet(configurationSetName, region);
|
||||
}
|
||||
identity.setConfigurationSetName(clearing ? null : configurationSetName);
|
||||
identityStore.put(key, identity);
|
||||
LOG.infov("Updated default ConfigurationSet for {0}: {1}",
|
||||
identityValue, clearing ? "<cleared>" : configurationSetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the configuration set a send should use: a non-blank configuration set explicitly
|
||||
* supplied by the caller takes precedence (a blank value is treated as absent); otherwise the
|
||||
* default configuration set associated with the sending identity (set via
|
||||
* {@code PutEmailIdentityConfigurationSetAttributes}) is used, with the email-address identity
|
||||
* taking precedence over its parent domain. If that default association is stale (its
|
||||
* configuration set was deleted), the send fails with a bad-request error, matching AWS.
|
||||
*/
|
||||
private String resolveDefaultConfigurationSet(String configurationSetName, String source, String region) {
|
||||
if (configurationSetName != null && !configurationSetName.isBlank()) {
|
||||
return configurationSetName;
|
||||
}
|
||||
if (source == null || source.isBlank()) {
|
||||
return configurationSetName;
|
||||
}
|
||||
String email = extractEmailAddress(source);
|
||||
if (email.isBlank()) {
|
||||
return configurationSetName;
|
||||
}
|
||||
String fromEmail = existingDefaultConfigSet(identityStore.get(identityKey(region, email)).orElse(null), region);
|
||||
if (fromEmail != null) {
|
||||
return fromEmail;
|
||||
}
|
||||
int at = email.indexOf('@');
|
||||
if (at >= 0 && at < email.length() - 1) {
|
||||
String fromDomain = existingDefaultConfigSet(
|
||||
identityStore.get(identityKey(region, email.substring(at + 1))).orElse(null), region);
|
||||
if (fromDomain != null) {
|
||||
return fromDomain;
|
||||
}
|
||||
}
|
||||
return configurationSetName;
|
||||
}
|
||||
|
||||
private String existingDefaultConfigSet(Identity identity, String region) {
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
String cs = identity.getConfigurationSetName();
|
||||
if (cs == null || cs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// AWS: deleting the configuration set that is an identity's default, then sending through
|
||||
// that identity, fails with a bad-request error rather than silently sending without it.
|
||||
if (configSetStore.get(configSetKey(region, cs)).isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Configuration set <" + cs + "> does not exist.", 400);
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
public void setMailFromDomain(String identityValue, String mailFromDomain,
|
||||
String behaviorOnMxFailure, String region) {
|
||||
String normalizedBehavior = null;
|
||||
|
||||
@@ -50,6 +50,9 @@ public class Identity {
|
||||
@JsonProperty("MailFromDomainStatus")
|
||||
private String mailFromDomainStatus = "Pending";
|
||||
|
||||
@JsonProperty("ConfigurationSetName")
|
||||
private String configurationSetName;
|
||||
|
||||
@JsonProperty("HeadersInNotificationsEnabled")
|
||||
private Map<String, Boolean> headersInNotificationsEnabled = new HashMap<>();
|
||||
|
||||
@@ -107,6 +110,9 @@ public class Identity {
|
||||
public String getMailFromDomainStatus() { return mailFromDomainStatus; }
|
||||
public void setMailFromDomainStatus(String mailFromDomainStatus) { this.mailFromDomainStatus = mailFromDomainStatus; }
|
||||
|
||||
public String getConfigurationSetName() { return configurationSetName; }
|
||||
public void setConfigurationSetName(String configurationSetName) { this.configurationSetName = configurationSetName; }
|
||||
|
||||
public Map<String, Boolean> getHeadersInNotificationsEnabled() { return headersInNotificationsEnabled; }
|
||||
public void setHeadersInNotificationsEnabled(Map<String, Boolean> headersInNotificationsEnabled) { this.headersInNotificationsEnabled = headersInNotificationsEnabled; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user