fix(lambda): return event source mapping ARN and support tagging mappings (#4587)

* fix(lambda): return event source mapping ARN and support tagging mappings

CreateEventSourceMapping now stores Tags, every mapping response carries
EventSourceMappingArn, and TagResource/ListTags/UntagResource route
event-source-mapping ARNs to the mapping instead of rejecting them as
non-function ARNs.

* fix(lambda): address review on event source mapping ARN and tags

- CloudFormation builds EventSourceMappingArn from the region and account
  the mapping is stored in, not the stack's, so it matches Lambda's ARN.
- TagResource rejects a non-object Tags or a non-string tag value with
  SerializationException instead of persisting it.
- The tag APIs match a mapping ARN exactly, so an ARN in another partition
  is reported as ResourceNotFoundException.
- Explain the tolerated teardown failures in LambdaEsmTagsTest.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-28 23:43:54 -04:00
1 parent 76f8c09341
commit 0a786207d6
10 files changed
+484 -18

No files matched your search

@@ -4,9 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
import jakarta.enterprise.context.ApplicationScoped;
@@ -177,14 +177,14 @@ public class LambdaEventSourceMappingCfnProvisioner implements CfnResourceProvis
"Updating SelfManagedEventSource requires resource replacement, which is not supported.", 400);
}
}
lambdaService.updateEventSourceMapping(uuid, req);
String esmArn = AwsArnUtils.Arn.of("lambda", ctx.region(), ctx.accountId(), "event-source-mapping:" + uuid).toString();
EventSourceMapping updated = lambdaService.updateEventSourceMapping(uuid, req);
String esmArn = LambdaArnUtils.eventSourceMappingArn(updated.getRegion(), updated.getAccountId(), uuid);
r.setPhysicalId(uuid);
r.getAttributes().put("Id", uuid);
r.getAttributes().put("EventSourceMappingArn", esmArn);
} else {
var esm = lambdaService.createEventSourceMapping(ctx.region(), req);
String esmArn = AwsArnUtils.Arn.of("lambda", ctx.region(), ctx.accountId(), "event-source-mapping:" + esm.getUuid()).toString();
EventSourceMapping esm = lambdaService.createEventSourceMapping(ctx.region(), req);
String esmArn = LambdaArnUtils.eventSourceMappingArn(esm.getRegion(), esm.getAccountId(), esm.getUuid());
r.setPhysicalId(esm.getUuid());
r.getAttributes().put("Id", esm.getUuid());
r.getAttributes().put("EventSourceMappingArn", esmArn);
@@ -27,8 +27,26 @@ public final class LambdaArnUtils {
private static final Pattern ACCOUNT_PATTERN = Pattern.compile("\\d{12}");
private static final Pattern QUALIFIER_PATTERN = Pattern.compile("\\$LATEST|[a-zA-Z0-9-_]+");
private static final String EVENT_SOURCE_MAPPING_PREFIX = "event-source-mapping:";
private LambdaArnUtils() {}
/** {@code arn:<partition>:lambda:<region>:<account>:event-source-mapping:<uuid>}. */
public static String eventSourceMappingArn(String region, String accountId, String uuid) {
return AwsArnUtils.Arn.of("lambda", region, accountId, EVENT_SOURCE_MAPPING_PREFIX + uuid).toString();
}
/**
* The UUID of an event source mapping ARN, or {@code null} when {@code arn} names
* some other Lambda resource.
*/
public static String eventSourceMappingUuid(AwsArnUtils.Arn arn) {
if (!"lambda".equals(arn.service()) || !arn.resource().startsWith(EVENT_SOURCE_MAPPING_PREFIX)) {
return null;
}
return arn.resource().substring(EVENT_SOURCE_MAPPING_PREFIX.length());
}
/**
* Resolved components of a Lambda function reference.
*
@@ -345,6 +345,8 @@ public class LambdaController {
Map<String, Object> buildEsmResponse(EventSourceMapping esm) {
ObjectNode node = objectMapper.createObjectNode();
node.put("UUID", esm.getUuid());
node.put("EventSourceMappingArn",
LambdaArnUtils.eventSourceMappingArn(esm.getRegion(), esm.getAccountId(), esm.getUuid()));
node.put("FunctionArn", esm.getFunctionArn());
if (esm.getEventSourceArn() != null) {
node.put("EventSourceArn", esm.getEventSourceArn());
@@ -47,6 +47,7 @@ import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -1281,6 +1282,7 @@ public class LambdaService implements ResourceProvider {
esm.setSelfManagedEventSource(selfManagedEventSource);
esm.setTopics(topics);
esm.setSourceAccessConfigurations(sourceAccessConfigurations);
esm.setTags(requestTags(request));
esm.setLastModified(System.currentTimeMillis());
if (eventSourceArn != null && eventSourceArn.contains(":dynamodb:")) {
@@ -2215,6 +2217,25 @@ public class LambdaService implements ResourceProvider {
environment.get("Variables"), "Environment.Variables");
}
/**
* The request's {@code Tags} member as a string-to-string map, or {@code null} when it is
* absent. A non-object member or a non-string tag value is a {@code SerializationException}.
*/
static Map<String, String> requestTags(Map<String, Object> request) {
Map<String, Object> requested = structureMember(request, "Tags");
if (requested == null) {
return null;
}
Map<String, String> tags = new HashMap<>();
for (Map.Entry<String, Object> entry : requested.entrySet()) {
if (!(entry.getValue() instanceof String value)) {
throw new AwsException("SerializationException", "Tags values must be strings", 400);
}
tags.put(entry.getKey(), value);
}
return tags;
}
private static void validateEventSourceMappingStructures(Map<String, Object> request) {
structureMember(request, "ScalingConfig");
Map<String, Object> destinationConfig = structureMember(request, "DestinationConfig");
@@ -3107,20 +3128,36 @@ public class LambdaService implements ResourceProvider {
// ──────────────────────────── Tags ────────────────────────────
public Map<String, String> listTags(String functionArn) {
EventSourceMapping esm = taggedEventSourceMapping(functionArn);
if (esm != null) {
return esm.getTags();
}
TagTarget target = resolveTagTarget(functionArn);
LambdaFunction fn = getFunction(target.region, target.name);
return fn.getTags() != null ? fn.getTags() : Map.of();
}
public void tagResource(String functionArn, Map<String, String> tags) {
EventSourceMapping esm = taggedEventSourceMapping(functionArn);
if (esm != null) {
esm.getTags().putAll(tags);
esmStore.save(esm);
return;
}
TagTarget target = resolveTagTarget(functionArn);
LambdaFunction fn = getFunction(target.region, target.name);
if (fn.getTags() == null) fn.setTags(new java.util.HashMap<>());
if (fn.getTags() == null) fn.setTags(new HashMap<>());
fn.getTags().putAll(tags);
functionStore.save(target.region, fn);
}
public void untagResource(String functionArn, List<String> tagKeys) {
EventSourceMapping esm = taggedEventSourceMapping(functionArn);
if (esm != null) {
tagKeys.forEach(esm.getTags()::remove);
esmStore.save(esm);
return;
}
TagTarget target = resolveTagTarget(functionArn);
LambdaFunction fn = getFunction(target.region, target.name);
if (fn.getTags() != null) {
@@ -3131,6 +3168,33 @@ public class LambdaService implements ResourceProvider {
private record TagTarget(String region, String name) {}
/**
* The mapping named by an {@code event-source-mapping:} tag-endpoint ARN, or {@code null}
* when the ARN names some other resource. An ARN in another partition, region or account
* than the mapping's is reported as missing, as AWS does.
*/
private EventSourceMapping taggedEventSourceMapping(String resourceArn) {
if (resourceArn == null || !resourceArn.startsWith("arn:")) {
return null;
}
AwsArnUtils.Arn arn;
try {
arn = AwsArnUtils.parse(resourceArn);
} catch (IllegalArgumentException ignored) {
// Not a well-formed ARN, so not a mapping ARN; resolveTagTarget reports the error.
return null;
}
String uuid = LambdaArnUtils.eventSourceMappingUuid(arn);
if (uuid == null) {
return null;
}
return esmStore.get(uuid)
.filter(esm -> resourceArn.equals(LambdaArnUtils.eventSourceMappingArn(
esm.getRegion(), esm.getAccountId(), esm.getUuid())))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"The resource you requested does not exist.", 404));
}
/**
* Resolves a tag-endpoint ARN to a (region, shortName) pair. The Lambda
* tag APIs only accept an unqualified full function ARN; reject partial
@@ -75,8 +75,7 @@ public class LambdaTagController {
try {
@SuppressWarnings("unchecked")
Map<String, Object> request = objectMapper.readValue(body, Map.class);
@SuppressWarnings("unchecked")
Map<String, String> tags = (Map<String, String>) request.get("Tags");
Map<String, String> tags = LambdaService.requestTags(request);
if (tags == null) {
throw new AwsException("InvalidParameterValueException", "Tags is required", 400);
}
@@ -38,6 +38,7 @@ public class EventSourceMapping {
private Map<String, Object> selfManagedEventSource;
private List<String> topics = new ArrayList<>();
private List<Map<String, Object>> sourceAccessConfigurations = new ArrayList<>();
private Map<String, String> tags = new HashMap<>();
public EventSourceMapping() {
}
@@ -101,7 +102,7 @@ public class EventSourceMapping {
public Map<String, String> getShardSequenceNumbers() { return shardSequenceNumbers; }
public void setShardSequenceNumbers(Map<String, String> shardSequenceNumbers) {
this.shardSequenceNumbers = shardSequenceNumbers != null ? shardSequenceNumbers : new java.util.HashMap<>();
this.shardSequenceNumbers = shardSequenceNumbers != null ? shardSequenceNumbers : new HashMap<>();
}
public ScalingConfig getScalingConfig() { return scalingConfig; }
@@ -176,6 +177,14 @@ public class EventSourceMapping {
this.sourceAccessConfigurations = sourceAccessConfigurations != null ? sourceAccessConfigurations : new ArrayList<>();
}
public Map<String, String> getTags() {
return tags;
}
public void setTags(Map<String, String> tags) {
this.tags = tags != null ? tags : new HashMap<>();
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public static class DestinationConfig {