mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(eventbridge): add InputTransformer support and S3 event notifications (#294)
* feat(eventbridge): add InputTransformer support for rule targets Support InputPathsMap + InputTemplate on EventBridge targets so events can be projected to a subset of fields before delivery to SQS/SNS/Lambda. - Add InputTransformer model with inputPathsMap and inputTemplate fields - Parse and serialize InputTransformer in PutTargets / ListTargetsByRule - Apply transformer in invokeTarget: extract JSONPath variables, substitute <varName> placeholders in the template - Use Jackson JsonPointer for JSONPath resolution ($.a.b.c → /a/b/c) - Add unit tests for extractJsonPath and applyInputTransformer edge cases - Add integration tests covering full put/list/deliver/transform flow Fixes #140
This commit is contained in:
1 parent
0bad9338f9
commit
9ca82e5748
15 files changed
+1168
-19
No files matched your search
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsJson11Controller;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.EventBus;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Rule;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -178,6 +179,16 @@ public class EventBridgeHandler {
|
||||
input.isEmpty() ? null : input,
|
||||
inputPath.isEmpty() ? null : inputPath
|
||||
);
|
||||
JsonNode transformerNode = t.path("InputTransformer");
|
||||
if (!transformerNode.isMissingNode() && transformerNode.isObject()) {
|
||||
Map<String, String> pathsMap = new HashMap<>();
|
||||
JsonNode pathsNode = transformerNode.path("InputPathsMap");
|
||||
if (pathsNode.isObject()) {
|
||||
pathsNode.fields().forEachRemaining(e -> pathsMap.put(e.getKey(), e.getValue().asText()));
|
||||
}
|
||||
String template = transformerNode.path("InputTemplate").asText(null);
|
||||
target.setInputTransformer(new InputTransformer(pathsMap, template));
|
||||
}
|
||||
targets.add(target);
|
||||
}
|
||||
}
|
||||
@@ -224,6 +235,14 @@ public class EventBridgeHandler {
|
||||
if (t.getInputPath() != null) {
|
||||
node.put("InputPath", t.getInputPath());
|
||||
}
|
||||
if (t.getInputTransformer() != null) {
|
||||
ObjectNode transformerNode = node.putObject("InputTransformer");
|
||||
ObjectNode pathsNode = transformerNode.putObject("InputPathsMap");
|
||||
t.getInputTransformer().getInputPathsMap().forEach(pathsNode::put);
|
||||
if (t.getInputTransformer().getInputTemplate() != null) {
|
||||
transformerNode.put("InputTemplate", t.getInputTransformer().getInputTemplate());
|
||||
}
|
||||
}
|
||||
targetsArray.add(node);
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
|
||||
+45
-1
@@ -1,7 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -19,22 +22,32 @@ public class EventBridgeInvoker {
|
||||
private final LambdaService lambdaService;
|
||||
private final SqsService sqsService;
|
||||
private final SnsService snsService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final String baseUrl;
|
||||
|
||||
@Inject
|
||||
public EventBridgeInvoker(LambdaService lambdaService,
|
||||
SqsService sqsService,
|
||||
SnsService snsService,
|
||||
ObjectMapper objectMapper,
|
||||
EmulatorConfig config) {
|
||||
this.lambdaService = lambdaService;
|
||||
this.sqsService = sqsService;
|
||||
this.snsService = snsService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.baseUrl = config.baseUrl();
|
||||
}
|
||||
|
||||
public void invokeTarget(Target target, String eventJson, String region) {
|
||||
String arn = target.getArn();
|
||||
String payload = target.getInput() != null ? target.getInput() : eventJson;
|
||||
String payload;
|
||||
if (target.getInput() != null) {
|
||||
payload = target.getInput();
|
||||
} else if (target.getInputTransformer() != null) {
|
||||
payload = applyInputTransformer(target.getInputTransformer(), eventJson);
|
||||
} else {
|
||||
payload = eventJson;
|
||||
}
|
||||
|
||||
try {
|
||||
if (arn.contains(":lambda:") || arn.contains(":function:")) {
|
||||
@@ -58,6 +71,37 @@ public class EventBridgeInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
String applyInputTransformer(InputTransformer transformer, String eventJson) {
|
||||
String template = transformer.getInputTemplate();
|
||||
if (template == null) {
|
||||
return eventJson;
|
||||
}
|
||||
String result = template;
|
||||
for (var e : transformer.getInputPathsMap().entrySet()) {
|
||||
String value = extractJsonPath(e.getValue(), eventJson);
|
||||
result = result.replace("<" + e.getKey() + ">", value != null ? value : "");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
String extractJsonPath(String jsonPath, String eventJson) {
|
||||
if (jsonPath == null || eventJson == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String pointer = (jsonPath.startsWith("$") ? jsonPath.substring(1) : jsonPath)
|
||||
.replace('.', '/');
|
||||
JsonNode node = objectMapper.readTree(eventJson).at(pointer);
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
return node.isTextual() ? node.asText() : node.toString();
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to extract JSONPath {0}: {1}", jsonPath, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractRegionFromArn(String arn, String defaultRegion) {
|
||||
String[] parts = arn.split(":");
|
||||
return parts.length >= 4 && !parts[3].isEmpty() ? parts[3] : defaultRegion;
|
||||
|
||||
@@ -458,6 +458,7 @@ public class EventBridgeService {
|
||||
|
||||
// ──────────────────────────── Target Routing ────────────────────────────
|
||||
|
||||
|
||||
private String buildEventEnvelope(Map<String, Object> entry, String busName, String eventId) {
|
||||
try {
|
||||
String source = (String) entry.getOrDefault("Source", "");
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class InputTransformer {
|
||||
|
||||
private Map<String, String> inputPathsMap = new HashMap<>();
|
||||
private String inputTemplate;
|
||||
|
||||
public InputTransformer() {}
|
||||
|
||||
public InputTransformer(Map<String, String> inputPathsMap, String inputTemplate) {
|
||||
this.inputPathsMap = inputPathsMap != null ? inputPathsMap : new HashMap<>();
|
||||
this.inputTemplate = inputTemplate;
|
||||
}
|
||||
|
||||
public Map<String, String> getInputPathsMap() { return inputPathsMap; }
|
||||
public void setInputPathsMap(Map<String, String> inputPathsMap) {
|
||||
this.inputPathsMap = inputPathsMap != null ? inputPathsMap : new HashMap<>();
|
||||
}
|
||||
|
||||
public String getInputTemplate() { return inputTemplate; }
|
||||
public void setInputTemplate(String inputTemplate) { this.inputTemplate = inputTemplate; }
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Target {
|
||||
|
||||
private String id;
|
||||
private String arn;
|
||||
private String input;
|
||||
private String inputPath;
|
||||
private InputTransformer inputTransformer;
|
||||
|
||||
public Target() {}
|
||||
|
||||
@@ -30,4 +33,7 @@ public class Target {
|
||||
|
||||
public String getInputPath() { return inputPath; }
|
||||
public void setInputPath(String inputPath) { this.inputPath = inputPath; }
|
||||
|
||||
public InputTransformer getInputTransformer() { return inputTransformer; }
|
||||
public void setInputTransformer(InputTransformer inputTransformer) { this.inputTransformer = inputTransformer; }
|
||||
}
|
||||
@@ -995,6 +995,8 @@ public class S3Controller {
|
||||
new TopicNotification(parsed.id, parsed.arn, parsed.events, parsed.filterRules));
|
||||
}
|
||||
|
||||
config.setEventBridgeEnabled(xml.contains("<EventBridgeConfiguration"));
|
||||
|
||||
s3Service.putBucketNotificationConfiguration(bucket, config);
|
||||
return Response.ok().build();
|
||||
} catch (AwsException e) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.s3.model.*;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -45,13 +46,16 @@ public class S3Service {
|
||||
|
||||
private final SqsService sqsService;
|
||||
private final SnsService snsService;
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final String baseUrl;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public S3Service(StorageFactory storageFactory, EmulatorConfig config,
|
||||
SqsService sqsService, SnsService snsService, RegionResolver regionResolver,
|
||||
SqsService sqsService, SnsService snsService,
|
||||
EventBridgeService eventBridgeService,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this(
|
||||
storageFactory.create("s3", "s3-buckets.json",
|
||||
@@ -62,7 +66,7 @@ public class S3Service {
|
||||
}),
|
||||
Path.of(config.storage().persistentPath()).resolve("s3"),
|
||||
"memory".equals(config.storage().services().s3().mode().orElse(config.storage().mode())),
|
||||
sqsService, snsService, regionResolver, config.effectiveBaseUrl(), objectMapper
|
||||
sqsService, snsService, eventBridgeService, regionResolver, config.effectiveBaseUrl(), objectMapper
|
||||
);
|
||||
}
|
||||
|
||||
@@ -72,13 +76,14 @@ public class S3Service {
|
||||
S3Service(StorageBackend<String, Bucket> bucketStore,
|
||||
StorageBackend<String, S3Object> objectStore,
|
||||
Path dataRoot, boolean inMemory) {
|
||||
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, "http://localhost:4566",
|
||||
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, "http://localhost:4566",
|
||||
new ObjectMapper());
|
||||
}
|
||||
|
||||
private S3Service(StorageBackend<String, Bucket> bucketStore,
|
||||
StorageBackend<String, S3Object> objectStore,
|
||||
Path dataRoot, boolean inMemory, SqsService sqsService, SnsService snsService,
|
||||
EventBridgeService eventBridgeService,
|
||||
RegionResolver regionResolver, String baseUrl, ObjectMapper objectMapper) {
|
||||
this.bucketStore = bucketStore;
|
||||
this.objectStore = objectStore;
|
||||
@@ -86,6 +91,7 @@ public class S3Service {
|
||||
this.inMemory = inMemory;
|
||||
this.sqsService = sqsService;
|
||||
this.snsService = snsService;
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.baseUrl = baseUrl;
|
||||
this.objectMapper = objectMapper;
|
||||
@@ -1292,7 +1298,7 @@ public class S3Service {
|
||||
}
|
||||
|
||||
private void fireNotifications(String bucketName, String key, String eventName, S3Object obj) {
|
||||
if (sqsService == null && snsService == null) {
|
||||
if (sqsService == null && snsService == null && eventBridgeService == null) {
|
||||
return;
|
||||
}
|
||||
Bucket bucket = bucketStore.get(bucketName).orElse(null);
|
||||
@@ -1328,6 +1334,43 @@ public class S3Service {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (config.isEventBridgeEnabled() && eventBridgeService != null) {
|
||||
try {
|
||||
String detailType = eventName.startsWith("ObjectCreated") ? "Object Created" : "Object Deleted";
|
||||
Map<String, Object> entry = new java.util.HashMap<>();
|
||||
entry.put("Source", "aws.s3");
|
||||
entry.put("DetailType", detailType);
|
||||
entry.put("Detail", buildS3EventBridgeDetail(bucketName, key, eventName, obj, region));
|
||||
eventBridgeService.putEvents(List.of(entry), region);
|
||||
LOG.debugv("Fired S3 event {0} to EventBridge default bus", eventName);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to deliver S3 event to EventBridge: {0}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String buildS3EventBridgeDetail(String bucketName, String key, String eventName,
|
||||
S3Object obj, String region) {
|
||||
try {
|
||||
long size = obj != null ? obj.getSize() : 0;
|
||||
String eTag = obj != null && obj.getETag() != null ? obj.getETag().replace("\"", "") : "";
|
||||
ObjectNode detail = objectMapper.createObjectNode();
|
||||
detail.put("version", "0");
|
||||
ObjectNode bucketNode = detail.putObject("bucket");
|
||||
bucketNode.put("name", bucketName);
|
||||
ObjectNode objectNode = detail.putObject("object");
|
||||
objectNode.put("key", key);
|
||||
objectNode.put("size", size);
|
||||
objectNode.put("etag", eTag);
|
||||
detail.put("request-id", UUID.randomUUID().toString());
|
||||
detail.put("requester", "aws:emulator");
|
||||
detail.put("source-ip-address", "127.0.0.1");
|
||||
detail.put("reason", eventName);
|
||||
return objectMapper.writeValueAsString(detail);
|
||||
} catch (Exception e) {
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matchesEvent(String pattern, String eventName) {
|
||||
|
||||
+5
-1
@@ -12,6 +12,7 @@ public class NotificationConfiguration {
|
||||
|
||||
private List<QueueNotification> queueConfigurations = new ArrayList<>();
|
||||
private List<TopicNotification> topicConfigurations = new ArrayList<>();
|
||||
private boolean eventBridgeEnabled;
|
||||
|
||||
public NotificationConfiguration() {}
|
||||
|
||||
@@ -25,7 +26,10 @@ public class NotificationConfiguration {
|
||||
this.topicConfigurations = topicConfigurations != null ? topicConfigurations : new ArrayList<>();
|
||||
}
|
||||
|
||||
public boolean isEventBridgeEnabled() { return eventBridgeEnabled; }
|
||||
public void setEventBridgeEnabled(boolean eventBridgeEnabled) { this.eventBridgeEnabled = eventBridgeEnabled; }
|
||||
|
||||
public boolean isEmpty() {
|
||||
return queueConfigurations.isEmpty() && topicConfigurations.isEmpty();
|
||||
return queueConfigurations.isEmpty() && topicConfigurations.isEmpty() && !eventBridgeEnabled;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user