feat(ses): extend tag endpoints to v2 EmailTemplate ARNs (#759)

Adds support for arn:aws:ses:<region>:<account>:template/<name> ARNs
across TagResource / UntagResource / ListTagsForResource. Behaviour
was verified against real AWS via aws sesv2 commands and aligned where
it diverged.

- EmailTemplate gains a tags field. CreateEmailTemplate accepts an
  optional Tags array; tag validation reuses validateTag().
- UpdateEmailTemplate intentionally preserves the existing tags so
  that AWS-style "tags only mutated via Tag/UntagResource" semantics
  hold.
- GetEmailTemplate response surfaces stored tags.
- Missing templates return NotFoundException with the AWS-canonical
  "No Template present with name: <name>" message — AWS uses the bare
  "Template" (not "EmailTemplate") for tag-endpoint lookups even
  though the SDK type is EmailTemplate.

Resource-type-specific routing in the tag endpoints:

- TagResource on a template ARN strictly validates ARN/signing region
  match (BadRequestException "Failed to tag resource" on mismatch),
  matching the existing ConfigurationSet behaviour.
- UntagResource on a template ARN strictly validates ARN/signing
  region match (BadRequestException "Failed to untag resource"),
  whereas ConfigurationSet's lenient ARN-region lookup is preserved.
  AWS treats the two resource types asymmetrically here.
- ListTagsForResource on a template ARN ignores the ARN region and
  resolves the template against the signing region. ConfigurationSet
  continues to route by ARN region.

Three small refactors that fall out naturally from adding a second
taggable resource:

- Tag is promoted from ConfigurationSet.Tag to a top-level
  services/ses/model/Tag so EmailTemplate and the shared helpers no
  longer reference a Configuration-scoped name. JSON serialization is
  unchanged.
- A private mergeTags(existing, incoming) helper centralizes the
  LinkedHashMap merge previously duplicated between
  tagConfigurationSet and tagEmailTemplate.
- A private parseTagsArray(JsonNode) helper centralizes the JSON Tags
  array parse previously duplicated across createConfigurationSet,
  createEmailTemplate, and tagResource. The helper returns null for
  missing/null nodes; the required caller (tagResource) treats null
  as BadRequestException.

Tests:

- SesTagsV2IntegrationTest gains a template lifecycle case, a
  template-not-found assertion, and the two template-specific
  region-mismatch / signing-region-lookup cases.
- SesTemplateV2IntegrationTest covers create-with-tags, list-via-tags
  endpoint, and update-preserves-tags round-trips.
- SesTagResourceTest (Java SDK compat) exercises CreateEmailTemplate
  with Tags and the Tag/Untag lifecycle on the template ARN.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-05-09 20:57:29 -05:00
1 parent ce2012de45
commit 646b4199df
9 files changed
+425 -62

No files matched your search

@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult;
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.Tag;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -459,6 +460,10 @@ public class SesController {
throw new AwsException("BadRequestException", "TemplateName is required.", 400);
}
EmailTemplate template = parseTemplateContent(templateName, request.path("TemplateContent"));
List<Tag> parsedTags = parseTagsArray(request.path("Tags"));
if (parsedTags != null) {
template.setTags(parsedTags);
}
sesService.createTemplate(template, region);
LOG.infov("SES V2 CreateEmailTemplate: {0}", templateName);
return Response.ok(objectMapper.createObjectNode()).build();
@@ -576,19 +581,9 @@ public class SesController {
throw new AwsException("BadRequestException", "ConfigurationSetName is required.", 400);
}
ConfigurationSet cs = new ConfigurationSet(name);
JsonNode tags = request.get("Tags");
if (tags != null && !tags.isNull()) {
if (!tags.isArray()) {
throw new AwsException("BadRequestException",
"Tags must be an array.", 400);
}
List<ConfigurationSet.Tag> tagList = new ArrayList<>();
for (JsonNode t : tags) {
tagList.add(new ConfigurationSet.Tag(
t.path("Key").asText(null),
t.path("Value").asText(null)));
}
cs.setTags(tagList);
List<Tag> parsedTags = parseTagsArray(request.path("Tags"));
if (parsedTags != null) {
cs.setTags(parsedTags);
}
sesService.createConfigurationSet(cs, region);
LOG.infov("SES V2 CreateConfigurationSet: {0}", name);
@@ -623,7 +618,7 @@ public class SesController {
ObjectNode result = objectMapper.createObjectNode();
result.put("ConfigurationSetName", cs.getName());
ArrayNode tags = result.putArray("Tags");
for (ConfigurationSet.Tag t : cs.getTags()) {
for (Tag t : cs.getTags()) {
ObjectNode tagNode = objectMapper.createObjectNode();
tagNode.put("Key", t.key());
tagNode.put("Value", t.value());
@@ -707,16 +702,10 @@ public class SesController {
if (arn == null || arn.isBlank()) {
throw new AwsException("BadRequestException", "ResourceArn is required.", 400);
}
JsonNode tagsNode = request.path("Tags");
if (!tagsNode.isArray()) {
List<Tag> tags = parseTagsArray(request.path("Tags"));
if (tags == null) {
throw new AwsException("BadRequestException", "Tags must be an array.", 400);
}
List<ConfigurationSet.Tag> tags = new ArrayList<>();
for (JsonNode t : tagsNode) {
String key = t.path("Key").asText(null);
String value = t.path("Value").asText(null);
tags.add(new ConfigurationSet.Tag(key, value));
}
sesService.tagResource(arn, region, tags);
LOG.infov("SES V2 TagResource: {0}", arn);
return Response.ok(objectMapper.createObjectNode()).build();
@@ -729,10 +718,12 @@ public class SesController {
@DELETE
@Path("/tags")
public Response untagResource(@QueryParam("ResourceArn") String arn,
public Response untagResource(@Context HttpHeaders headers,
@QueryParam("ResourceArn") String arn,
@QueryParam("TagKeys") List<String> tagKeys) {
String region = regionResolver.resolveRegion(headers);
try {
sesService.untagResource(arn, tagKeys);
sesService.untagResource(arn, region, tagKeys);
LOG.infov("SES V2 UntagResource: {0}", arn);
return Response.ok(objectMapper.createObjectNode()).build();
} catch (AwsException e) {
@@ -742,12 +733,14 @@ public class SesController {
@GET
@Path("/tags")
public Response listTagsForResource(@QueryParam("ResourceArn") String arn) {
public Response listTagsForResource(@Context HttpHeaders headers,
@QueryParam("ResourceArn") String arn) {
String region = regionResolver.resolveRegion(headers);
try {
List<ConfigurationSet.Tag> tags = sesService.listResourceTags(arn);
List<Tag> tags = sesService.listResourceTags(arn, region);
ObjectNode result = objectMapper.createObjectNode();
ArrayNode arr = result.putArray("Tags");
for (ConfigurationSet.Tag t : tags) {
for (Tag t : tags) {
ObjectNode tagNode = objectMapper.createObjectNode();
tagNode.put("Key", t.key());
tagNode.put("Value", t.value());
@@ -867,6 +860,13 @@ public class SesController {
if (template.getHtmlPart() != null) {
content.put("Html", template.getHtmlPart());
}
ArrayNode tags = result.putArray("Tags");
for (Tag t : template.getTags()) {
ObjectNode tagNode = objectMapper.createObjectNode();
tagNode.put("Key", t.key());
tagNode.put("Value", t.value());
tags.add(tagNode);
}
return result;
}
@@ -923,6 +923,28 @@ public class SesController {
return child;
}
/**
* Parse a JSON {@code Tags} array node into a list of tag records. Returns {@code null}
* when the node is missing or null so callers can decide whether that is an error
* (TagResource) or a no-op (CreateConfigurationSet / CreateEmailTemplate). Throws
* {@code BadRequestException} when the node is present but not an array.
*/
private List<Tag> parseTagsArray(JsonNode tagsNode) {
if (tagsNode.isMissingNode() || tagsNode.isNull()) {
return null;
}
if (!tagsNode.isArray()) {
throw new AwsException("BadRequestException", "Tags must be an array.", 400);
}
List<Tag> out = new ArrayList<>();
for (JsonNode t : tagsNode) {
out.add(new Tag(
t.path("Key").asText(null),
t.path("Value").asText(null)));
}
return out;
}
private static AwsException remapV1Exception(AwsException e) {
return switch (e.getErrorCode()) {
case "InvalidParameterValue", "InvalidTemplate",
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.SentEmail;
import io.github.hectorvent.floci.services.ses.model.Tag;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -358,6 +359,11 @@ public class SesService {
public EmailTemplate createTemplate(EmailTemplate template, String region) {
validateTemplate(template);
if (template.getTags() != null) {
for (Tag tag : template.getTags()) {
validateTag(tag);
}
}
String key = templateKey(region, template.getTemplateName());
if (templateStore.get(key).isPresent()) {
throw new AwsException("AlreadyExists",
@@ -385,6 +391,8 @@ public class SesService {
"Template " + template.getTemplateName() + " does not exist.", 400));
template.setCreatedTimestamp(existing.getCreatedTimestamp());
template.setLastUpdatedTimestamp(Instant.now());
// Tags are managed exclusively via Tag/UntagResource — preserve them on update.
template.setTags(existing.getTags());
templateStore.put(key, template);
LOG.infov("Updated SES template: {0} in region {1}", template.getTemplateName(), region);
return template;
@@ -417,7 +425,7 @@ public class SesService {
}
String key = configSetKey(region, configSet.getName());
if (configSet.getTags() != null) {
for (ConfigurationSet.Tag tag : configSet.getTags()) {
for (Tag tag : configSet.getTags()) {
validateTag(tag);
}
}
@@ -478,16 +486,20 @@ public class SesService {
}
}
public List<ConfigurationSet.Tag> listResourceTags(String arn) {
public List<Tag> listResourceTags(String arn, String region) {
ResourceRef ref = parseSesArn(arn);
return switch (ref.type()) {
case "configuration-set" -> listConfigurationSetTags(ref.name(), ref.region());
// AWS ListTagsForResource on template ARNs uses the signing region for lookup
// (the ARN region is effectively ignored), unlike configuration-set which routes
// by the ARN's region.
case "template" -> listEmailTemplateTags(ref.name(), region);
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
};
}
public void tagResource(String arn, String region, List<ConfigurationSet.Tag> newTags) {
public void tagResource(String arn, String region, List<Tag> newTags) {
ResourceRef ref = parseSesArn(arn);
if (!ref.region().equals(region)) {
throw new AwsException("BadRequestException", "Failed to tag resource", 400);
@@ -497,17 +509,18 @@ public class SesService {
"1 validation error detected: Value at 'tags' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 1", 400);
}
for (ConfigurationSet.Tag t : newTags) {
for (Tag t : newTags) {
validateTag(t);
}
switch (ref.type()) {
case "configuration-set" -> tagConfigurationSet(ref.name(), ref.region(), newTags);
case "template" -> tagEmailTemplate(ref.name(), ref.region(), newTags);
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
}
}
public void untagResource(String arn, List<String> tagKeys) {
public void untagResource(String arn, String region, List<String> tagKeys) {
ResourceRef ref = parseSesArn(arn);
if (tagKeys == null || tagKeys.isEmpty()) {
throw new AwsException("BadRequestException",
@@ -516,33 +529,33 @@ public class SesService {
}
switch (ref.type()) {
case "configuration-set" -> untagConfigurationSet(ref.name(), ref.region(), tagKeys);
case "template" -> {
// AWS UntagResource on template ARNs strictly requires the ARN region to match
// the signing region (rejects mismatch with BadRequestException), unlike
// configuration-set which routes the lookup to the ARN's region.
if (!ref.region().equals(region)) {
throw new AwsException("BadRequestException", "Failed to untag resource", 400);
}
untagEmailTemplate(ref.name(), region, tagKeys);
}
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
}
}
private List<ConfigurationSet.Tag> listConfigurationSetTags(String name, String region) {
private List<Tag> listConfigurationSetTags(String name, String region) {
ConfigurationSet cs = configSetStore.get(configSetKey(region, name))
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
return new ArrayList<>(cs.getTags());
}
private void tagConfigurationSet(String name, String region, List<ConfigurationSet.Tag> newTags) {
private void tagConfigurationSet(String name, String region, List<Tag> newTags) {
String key = configSetKey(region, name);
ConfigurationSet cs = configSetStore.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
Map<String, String> merged = new LinkedHashMap<>();
for (ConfigurationSet.Tag t : cs.getTags()) {
merged.put(t.key(), t.value());
}
for (ConfigurationSet.Tag t : newTags) {
merged.put(t.key(), t.value());
}
List<ConfigurationSet.Tag> out = new ArrayList<>();
merged.forEach((k, v) -> out.add(new ConfigurationSet.Tag(k, v)));
cs.setTags(out);
cs.setTags(mergeTags(cs.getTags(), newTags));
configSetStore.put(key, cs);
LOG.infov("Tagged SES configuration set: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
@@ -558,6 +571,48 @@ public class SesService {
LOG.infov("Untagged SES configuration set: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
private List<Tag> listEmailTemplateTags(String name, String region) {
EmailTemplate template = templateStore.get(templateKey(region, name))
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
return new ArrayList<>(template.getTags());
}
private void tagEmailTemplate(String name, String region, List<Tag> newTags) {
String key = templateKey(region, name);
EmailTemplate template = templateStore.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
template.setTags(mergeTags(template.getTags(), newTags));
templateStore.put(key, template);
LOG.infov("Tagged SES template: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
private static List<Tag> mergeTags(List<Tag> existing,
List<Tag> incoming) {
Map<String, String> merged = new LinkedHashMap<>();
for (Tag t : existing) {
merged.put(t.key(), t.value());
}
for (Tag t : incoming) {
merged.put(t.key(), t.value());
}
List<Tag> out = new ArrayList<>();
merged.forEach((k, v) -> out.add(new Tag(k, v)));
return out;
}
private void untagEmailTemplate(String name, String region, List<String> tagKeys) {
String key = templateKey(region, name);
EmailTemplate template = templateStore.get(key)
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
Set<String> toRemove = new HashSet<>(tagKeys);
template.getTags().removeIf(t -> toRemove.contains(t.key()));
templateStore.put(key, template);
LOG.infov("Untagged SES template: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
private record ResourceRef(String region, String type, String name) {}
private static ResourceRef parseSesArn(String arn) {
@@ -586,7 +641,7 @@ public class SesService {
return new ResourceRef(parsed.region(), resource.substring(0, slash), resource.substring(slash + 1));
}
static void validateTag(ConfigurationSet.Tag tag) {
static void validateTag(Tag tag) {
if (tag == null) {
throw new AwsException("InvalidParameterValue", "Tag must not be null.", 400);
}
@@ -36,10 +36,4 @@ public class ConfigurationSet {
public List<Tag> getTags() { return tags; }
public void setTags(List<Tag> tags) { this.tags = tags != null ? tags : new ArrayList<>(); }
@RegisterForReflection
public record Tag(
@JsonProperty("Key") String key,
@JsonProperty("Value") String value) {
}
}
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
@@ -28,6 +30,9 @@ public class EmailTemplate {
@JsonProperty("LastUpdatedTimestamp")
private Instant lastUpdatedTimestamp;
@JsonProperty("Tags")
private List<Tag> tags = new ArrayList<>();
public EmailTemplate() {}
public EmailTemplate(String templateName, String subject, String textPart, String htmlPart) {
@@ -54,4 +59,7 @@ public class EmailTemplate {
public Instant getLastUpdatedTimestamp() { return lastUpdatedTimestamp; }
public void setLastUpdatedTimestamp(Instant lastUpdatedTimestamp) { this.lastUpdatedTimestamp = lastUpdatedTimestamp; }
public List<Tag> getTags() { return tags; }
public void setTags(List<Tag> tags) { this.tags = tags != null ? tags : new ArrayList<>(); }
}
@@ -0,0 +1,10 @@
package io.github.hectorvent.floci.services.ses.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public record Tag(
@JsonProperty("Key") String key,
@JsonProperty("Value") String value) {
}