mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(ses): add v2 TagResource / UntagResource / ListTagsForResource (#749)
* feat(ses): add v2 TagResource / UntagResource / ListTagsForResource
Implements the v2 SES tag endpoints at /v2/email/tags. Initial scope
covers ConfigurationSet ARNs only
(arn:aws:ses:<region>:<account>:configuration-set/<name>); other
resource types return NotFoundException so follow-up PRs can extend
coverage to Identity, EmailTemplate, etc.
Behavior aligned with observed AWS responses:
- TagResource enforces ARN/signing region match strictly and rejects
mismatches with BadRequestException ("Failed to tag resource").
- UntagResource and ListTagsForResource route lookups to the ARN's
region instead, so a region mismatch surfaces as NotFoundException
with the resource-specific message (e.g. "No ConfigurationSet
present with name: <name>").
- TagResource merges by key (existing keys are replaced, others
retained); UntagResource silently skips keys not present.
- Empty Tags / TagKeys lists are rejected with BadRequestException to
match AWS validation.
- ARN parsing uses the shared AwsArnUtils helper; missing region or
account in the ARN is rejected as BadRequest, matching AWS.
Tests:
- New SesTagsV2IntegrationTest for endpoint-focused coverage (lifecycle,
region-mismatch asymmetry, validation errors).
- Cross-feature round-trip in SesConfigurationSetV2IntegrationTest
verifying that tags supplied to CreateConfigurationSet are reachable
via ListTagsForResource.
- New SesTagResourceTest in the Java SDK compatibility suite.
* refactor(ses): drop unused region parameter from list/untag tag service methods
listResourceTags and untagResource route lookups by the ARN's region, so
the request signing region they accepted was never read. Remove it and
stop resolving it in the controller. tagResource still takes region
because it strictly validates it against the ARN.
This commit is contained in:
1 parent
1ffe905271
commit
9b68bd0377
6 files changed
+631
No files matched your search
@@ -20,6 +20,7 @@ import jakarta.ws.rs.PUT;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
@@ -691,6 +692,73 @@ public class SesController {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Tags ───────────────────────────────
|
||||
|
||||
@POST
|
||||
@Path("/tags")
|
||||
public Response tagResource(@Context HttpHeaders headers, String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
if (body == null || body.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "Request body is required.", 400);
|
||||
}
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
String arn = request.path("ResourceArn").asText(null);
|
||||
if (arn == null || arn.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "ResourceArn is required.", 400);
|
||||
}
|
||||
JsonNode tagsNode = request.path("Tags");
|
||||
if (!tagsNode.isArray()) {
|
||||
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();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
|
||||
throw new AwsException("BadRequestException", e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/tags")
|
||||
public Response untagResource(@QueryParam("ResourceArn") String arn,
|
||||
@QueryParam("TagKeys") List<String> tagKeys) {
|
||||
try {
|
||||
sesService.untagResource(arn, tagKeys);
|
||||
LOG.infov("SES V2 UntagResource: {0}", arn);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/tags")
|
||||
public Response listTagsForResource(@QueryParam("ResourceArn") String arn) {
|
||||
try {
|
||||
List<ConfigurationSet.Tag> tags = sesService.listResourceTags(arn);
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ArrayNode arr = result.putArray("Tags");
|
||||
for (ConfigurationSet.Tag t : tags) {
|
||||
ObjectNode tagNode = objectMapper.createObjectNode();
|
||||
tagNode.put("Key", t.key());
|
||||
tagNode.put("Value", t.value());
|
||||
arr.add(tagNode);
|
||||
}
|
||||
return Response.ok(result).build();
|
||||
} catch (AwsException e) {
|
||||
throw remapV1Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
private ObjectNode buildFullIdentityResponse(Identity identity) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -26,8 +27,11 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -474,6 +478,114 @@ public class SesService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<ConfigurationSet.Tag> listResourceTags(String arn) {
|
||||
ResourceRef ref = parseSesArn(arn);
|
||||
return switch (ref.type()) {
|
||||
case "configuration-set" -> listConfigurationSetTags(ref.name(), ref.region());
|
||||
default -> throw new AwsException("NotFoundException",
|
||||
"Resource " + arn + " was not found.", 404);
|
||||
};
|
||||
}
|
||||
|
||||
public void tagResource(String arn, String region, List<ConfigurationSet.Tag> newTags) {
|
||||
ResourceRef ref = parseSesArn(arn);
|
||||
if (!ref.region().equals(region)) {
|
||||
throw new AwsException("BadRequestException", "Failed to tag resource", 400);
|
||||
}
|
||||
if (newTags == null || newTags.isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"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) {
|
||||
validateTag(t);
|
||||
}
|
||||
switch (ref.type()) {
|
||||
case "configuration-set" -> tagConfigurationSet(ref.name(), ref.region(), newTags);
|
||||
default -> throw new AwsException("NotFoundException",
|
||||
"Resource " + arn + " was not found.", 404);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagResource(String arn, List<String> tagKeys) {
|
||||
ResourceRef ref = parseSesArn(arn);
|
||||
if (tagKeys == null || tagKeys.isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"1 validation error detected: Value at 'tagKeys' failed to satisfy constraint: "
|
||||
+ "Member must have length greater than or equal to 1", 400);
|
||||
}
|
||||
switch (ref.type()) {
|
||||
case "configuration-set" -> untagConfigurationSet(ref.name(), ref.region(), tagKeys);
|
||||
default -> throw new AwsException("NotFoundException",
|
||||
"Resource " + arn + " was not found.", 404);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ConfigurationSet.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) {
|
||||
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);
|
||||
configSetStore.put(key, cs);
|
||||
LOG.infov("Tagged SES configuration set: {0} (region {1}, +{2} tags)", name, region, newTags.size());
|
||||
}
|
||||
|
||||
private void untagConfigurationSet(String name, String region, List<String> tagKeys) {
|
||||
String key = configSetKey(region, name);
|
||||
ConfigurationSet cs = configSetStore.get(key)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException",
|
||||
"No ConfigurationSet present with name: " + name, 404));
|
||||
Set<String> toRemove = new HashSet<>(tagKeys);
|
||||
cs.getTags().removeIf(t -> toRemove.contains(t.key()));
|
||||
configSetStore.put(key, cs);
|
||||
LOG.infov("Untagged SES configuration set: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
|
||||
}
|
||||
|
||||
private record ResourceRef(String region, String type, String name) {}
|
||||
|
||||
private static ResourceRef parseSesArn(String arn) {
|
||||
if (arn == null || arn.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "ResourceArn is required.", 400);
|
||||
}
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("BadRequestException", "Invalid ARN: " + arn, 400);
|
||||
}
|
||||
if (!"ses".equals(parsed.service())) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"ResourceArn must be a SES ARN: " + arn, 400);
|
||||
}
|
||||
if (parsed.region().isEmpty() || parsed.accountId().isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"ResourceArn must include region and account: " + arn, 400);
|
||||
}
|
||||
String resource = parsed.resource();
|
||||
int slash = resource.indexOf('/');
|
||||
if (slash <= 0 || slash == resource.length() - 1) {
|
||||
throw new AwsException("BadRequestException", "Invalid ARN: " + arn, 400);
|
||||
}
|
||||
return new ResourceRef(parsed.region(), resource.substring(0, slash), resource.substring(slash + 1));
|
||||
}
|
||||
|
||||
static void validateTag(ConfigurationSet.Tag tag) {
|
||||
if (tag == null) {
|
||||
throw new AwsException("InvalidParameterValue", "Tag must not be null.", 400);
|
||||
|
||||
Reference in new issue
Block a user