mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(eventbridge): AWS-conformant InputTransformer from CloudFormation and PutTargets (#1885)
* refactor(eventbridge): parse InputTransformer via shared InputTransformer.fromJson * fix(eventbridge): substitute InputTransformer placeholders with AWS-conformant JSON representation * feat(cloudformation): carry AWS::Events::Rule target InputTransformer into the rule * test(eventbridge): verify native PutTargets InputTransformer delivers transformed body to SQS * chore(eventbridge): log swallowed escape failure and use idiomatic indexOf guard in transformer engine --------- Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
ea11e67719
commit
9b3aee6a03
8 files changed
+333
-27
No files matched your search
+2
@@ -20,6 +20,7 @@ import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.BatchParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.EventBus;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.SqsParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
@@ -3651,6 +3652,7 @@ public class CloudFormationResourceProvisioner {
|
||||
String inputPath = resolved.path("InputPath").asText(null);
|
||||
if (targetId != null && targetArn != null) {
|
||||
Target target = new Target(targetId, targetArn, input, inputPath);
|
||||
target.setInputTransformer(InputTransformer.fromJson(resolved.path("InputTransformer")));
|
||||
JsonNode sqsParamsNode = resolved.path("SqsParameters");
|
||||
if (!sqsParamsNode.isMissingNode() && sqsParamsNode.isObject()) {
|
||||
String messageGroupId = sqsParamsNode.path("MessageGroupId").asText(null);
|
||||
|
||||
+1
-10
@@ -223,16 +223,7 @@ 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));
|
||||
}
|
||||
target.setInputTransformer(InputTransformer.fromJson(t.path("InputTransformer")));
|
||||
JsonNode sqsParamsNode = t.path("SqsParameters");
|
||||
if (!sqsParamsNode.isMissingNode() && sqsParamsNode.isObject()) {
|
||||
String messageGroupId = sqsParamsNode.path("MessageGroupId").asText(null);
|
||||
|
||||
+78
-17
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -224,30 +225,90 @@ public class EventBridgeInvoker {
|
||||
if (template == null) {
|
||||
return eventJson;
|
||||
}
|
||||
String result = template;
|
||||
Map<String, JsonNode> resolved = new LinkedHashMap<>();
|
||||
for (var e : transformer.getInputPathsMap().entrySet()) {
|
||||
String value = extractJsonPath(e.getValue(), eventJson);
|
||||
result = result.replace("<" + e.getKey() + ">", value != null ? value : "");
|
||||
resolved.put(e.getKey(), extractNode(e.getValue(), eventJson));
|
||||
}
|
||||
return result;
|
||||
StringBuilder out = new StringBuilder(template.length() + 32);
|
||||
boolean inString = false;
|
||||
for (int i = 0; i < template.length(); i++) {
|
||||
char c = template.charAt(i);
|
||||
if (c == '<') {
|
||||
int close = template.indexOf('>', i + 1);
|
||||
if (close >= 0) {
|
||||
String name = template.substring(i + 1, close);
|
||||
if (resolved.containsKey(name)) {
|
||||
JsonNode node = resolved.get(name);
|
||||
out.append(inString ? rawValue(node) : jsonValue(node));
|
||||
i = close;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out.append(c);
|
||||
continue;
|
||||
}
|
||||
if (c == '"' && !isEscaped(template, i)) {
|
||||
inString = !inString;
|
||||
}
|
||||
out.append(c);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
// JSON representation for a value-position placeholder: strings quoted+escaped, objects/arrays/
|
||||
// numbers/bools literal JSON, missing/null empty.
|
||||
private String jsonValue(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull()) {
|
||||
return "";
|
||||
}
|
||||
return node.toString();
|
||||
}
|
||||
|
||||
// Raw value for a placeholder inside a quoted string: JSON-escaped, no surrounding quotes.
|
||||
private String rawValue(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull()) {
|
||||
return "";
|
||||
}
|
||||
String raw = node.isValueNode() ? node.asText() : node.toString();
|
||||
try {
|
||||
String quoted = objectMapper.writeValueAsString(raw); // "escaped"
|
||||
return quoted.substring(1, quoted.length() - 1); // strip surrounding quotes
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to JSON-escape raw template value ''{0}'': {1}", raw, e.getMessage());
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEscaped(String s, int i) {
|
||||
int backslashes = 0;
|
||||
for (int j = i - 1; j >= 0 && s.charAt(j) == '\\'; j--) {
|
||||
backslashes++;
|
||||
}
|
||||
return (backslashes & 1) == 1;
|
||||
}
|
||||
|
||||
JsonNode extractNode(String jsonPath, String eventJson) {
|
||||
if (jsonPath == null || eventJson == null) {
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
try {
|
||||
return objectMapper.readTree(eventJson).at(toPointer(jsonPath));
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to extract JSONPath {0}: {1}", jsonPath, e.getMessage());
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
private static String toPointer(String jsonPath) {
|
||||
return (jsonPath.startsWith("$") ? jsonPath.substring(1) : jsonPath).replace('.', '/');
|
||||
}
|
||||
|
||||
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());
|
||||
JsonNode node = extractNode(jsonPath, eventJson);
|
||||
if (node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
return node.isTextual() ? node.asText() : node.toString();
|
||||
}
|
||||
|
||||
private Map<String, String> parametersFromBatchPayload(String payload) {
|
||||
|
||||
+13
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -20,6 +21,18 @@ public class InputTransformer {
|
||||
this.inputTemplate = inputTemplate;
|
||||
}
|
||||
|
||||
public static InputTransformer fromJson(JsonNode transformerNode) {
|
||||
if (transformerNode == null || transformerNode.isMissingNode() || !transformerNode.isObject()) {
|
||||
return null;
|
||||
}
|
||||
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()));
|
||||
}
|
||||
return new InputTransformer(pathsMap, transformerNode.path("InputTemplate").asText(null));
|
||||
}
|
||||
|
||||
public Map<String, String> getInputPathsMap() { return inputPathsMap; }
|
||||
public void setInputPathsMap(Map<String, String> inputPathsMap) {
|
||||
this.inputPathsMap = inputPathsMap != null ? inputPathsMap : new HashMap<>();
|
||||
|
||||
Reference in new issue
Block a user