mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix(scheduler): deliver the default event to targets without Input (#4018)
* fix(scheduler): deliver the default event to targets without Input
Templated targets without Input received "{}" instead of the default
"Scheduled Event" notification that EventBridge Scheduler delivers, so
consumers that switch on detail-type or source never matched. Only SQS
targets had a dead-letter body in the shape of the target service's API;
every other target's body was the raw payload. CreateSchedule and
UpdateSchedule also accepted a non-JSON Input for the templated Lambda,
Step Functions, and EventBridge targets that the API reference requires
to be well-formed JSON.
Deliver the default notification (version, id, detail-type, source,
account, time, region, resources, and detail as the string "{}") to
every templated target whose Input is absent, with an id that is stable
per occurrence so retries and the dead-letter body carry the same event.
Materialize dead-letter bodies with the request fields of Lambda Invoke,
SNS Publish, Step Functions StartExecution, EventBridge PutEvents, and
ECS RunTask, next to the existing SQS SendMessage shape. Reject a
non-JSON Input for Lambda, Step Functions, and EventBridge targets with
a ValidationException, while SQS, SNS, and universal targets keep
accepting any text.
The invoker now takes the schedule and the occurrence time, since the
default event needs the schedule ARN, account, region, and time. RoleArn
is still not consulted at delivery: no Floci delivery path evaluates a
role's permissions, so that is left for a platform-wide follow-up.
Refs #3951
* fix(scheduler): reject blank and trailing-token Input for JSON targets
CreateSchedule and UpdateSchedule checked Input with readTree, which
accepts text after the JSON value ("{} garbage") and returns a missing
node for a blank value, so both were stored for Lambda, Step Functions,
and EventBridge targets. Parse with FAIL_ON_TRAILING_TOKENS and reject a
blank Input, which the API reference gives a minimum length of 1.
Import the scheduler model's AwsVpcConfiguration in ScheduleInvoker
instead of qualifying it inline. The ECS model's type of the same name
stays qualified, with a comment naming the clash.
This commit is contained in:
1 parent
bb67a443ce
commit
c0ed88d647
8 files changed
+590
-125
No files matched your search
@@ -206,7 +206,7 @@ public class ScheduleDispatcher implements Resettable {
|
||||
|
||||
// Record the fire before delivering so a failing occurrence never holds back the next one.
|
||||
recordFire(schedule, now);
|
||||
String requestBody = invoker.materializeRequest(schedule.getTarget(), regionOf(schedule));
|
||||
String requestBody = invoker.materializeRequest(schedule, nextFire);
|
||||
attempt(schedule, new Occurrence(schedule.getArn(), nextFire),
|
||||
Delivery.first(kind, schedule, requestBody), now);
|
||||
}
|
||||
@@ -241,7 +241,7 @@ public class ScheduleDispatcher implements Resettable {
|
||||
|
||||
private void attempt(Schedule schedule, Occurrence occurrence, Delivery delivery, Instant now) {
|
||||
try {
|
||||
invoker.invoke(schedule.getTarget(), regionOf(schedule));
|
||||
invoker.invoke(schedule, occurrence.scheduledAt());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Schedule {0} invocation failed: {1}", schedule.getArn(), e.getMessage());
|
||||
Delivery failed = delivery.failedWith(e, now);
|
||||
@@ -365,7 +365,7 @@ public class ScheduleDispatcher implements Resettable {
|
||||
}
|
||||
|
||||
private static String regionOf(Schedule schedule) {
|
||||
return AwsArnUtils.regionOrDefault(schedule.getArn(), "us-east-1");
|
||||
return ScheduleInvoker.regionOf(schedule);
|
||||
}
|
||||
|
||||
private record Occurrence(String scheduleArn, Instant scheduledAt) {
|
||||
|
||||
@@ -11,8 +11,10 @@ import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EventBridgeParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EcsParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Target;
|
||||
import io.github.hectorvent.floci.services.sns.SnsMessageAttributes;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
@@ -23,11 +25,15 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Delivers an EventBridge Scheduler target invocation to the underlying service.
|
||||
@@ -70,76 +76,208 @@ public class ScheduleInvoker {
|
||||
this.baseUrl = config.baseUrl();
|
||||
}
|
||||
|
||||
public String invoke(Target target, String region) {
|
||||
/**
|
||||
* Delivers one occurrence of {@code schedule}, scheduled at {@code scheduledAt}, to its target
|
||||
* and returns the JSON request that was sent (see {@link #materializeRequest}).
|
||||
*/
|
||||
public String invoke(Schedule schedule, Instant scheduledAt) {
|
||||
Target target = schedule.getTarget();
|
||||
if (target == null || target.getArn() == null) {
|
||||
return "{}";
|
||||
}
|
||||
String arn = target.getArn();
|
||||
String payload = target.getInput() != null ? target.getInput() : "{}";
|
||||
String requestBody = materializeRequest(target, region);
|
||||
|
||||
// Universal targets (arn:aws:scheduler:::aws-sdk:<service>:<action>) carry the
|
||||
// real resource identifiers inside Input, not in the target ARN. Detect and
|
||||
// dispatch them before the ARN-substring routing below, which would otherwise
|
||||
// mis-match (e.g. "aws-sdk:sns:publish" contains ":sns:").
|
||||
int sdkIdx = arn.indexOf(":aws-sdk:");
|
||||
if (sdkIdx >= 0) {
|
||||
invokeUniversalTarget(arn.substring(sdkIdx + ":aws-sdk:".length()), payload, region);
|
||||
return requestBody;
|
||||
String region = regionOf(schedule);
|
||||
if (isUniversalTarget(arn)) {
|
||||
invokeUniversalTarget(arn.substring(arn.indexOf(":aws-sdk:") + ":aws-sdk:".length()),
|
||||
target.getInput(), region);
|
||||
return universalTargetRequest(target);
|
||||
}
|
||||
|
||||
TargetKind kind = TargetKind.of(target);
|
||||
String payload = templatedPayload(schedule, scheduledAt);
|
||||
String targetRegion = extractRegion(arn, region);
|
||||
if (arn.contains(":sqs:")) {
|
||||
String queueUrl = AwsArnUtils.arnToQueueUrl(arn, baseUrl);
|
||||
String messageGroupId = target.getSqsParameters() != null
|
||||
? target.getSqsParameters().getMessageGroupId() : null;
|
||||
sqsService.sendMessage(queueUrl, payload, 0, messageGroupId, null, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SQS: {0}", arn);
|
||||
} else if (arn.contains(":lambda:") || arn.contains(":function:")) {
|
||||
lambdaService.invokeArn(arn, payload.getBytes(), InvocationType.Event);
|
||||
LOG.debugv("Scheduler delivered to Lambda: {0}", arn);
|
||||
} else if (arn.contains(":sns:")) {
|
||||
snsService.publish(arn, null, payload, "Scheduler", targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SNS: {0}", arn);
|
||||
} else if (arn.contains(":ecs:") && target.getEcsParameters() != null) {
|
||||
deliverToEcsRunTask(target, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to ECS RunTask: {0}", arn);
|
||||
} else if (isStateMachineArn(arn)) {
|
||||
String targetAccount = AwsArnUtils.parse(arn).accountId();
|
||||
RequestScopes.runAs(targetAccount,
|
||||
() -> stepFunctionsService.startExecution(arn, null, payload, targetRegion));
|
||||
LOG.debugv("Scheduler started Step Functions execution: {0}", arn);
|
||||
} else if (isEventBridgePutEventsArn(arn)) {
|
||||
deliverToEventBridge(target, payload, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to EventBridge: {0}", arn);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Scheduler: unsupported target ARN type: " + arn);
|
||||
switch (kind) {
|
||||
case SQS -> {
|
||||
String queueUrl = AwsArnUtils.arnToQueueUrl(arn, baseUrl);
|
||||
String messageGroupId = target.getSqsParameters() != null
|
||||
? target.getSqsParameters().getMessageGroupId() : null;
|
||||
sqsService.sendMessage(queueUrl, payload, 0, messageGroupId, null, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SQS: {0}", arn);
|
||||
}
|
||||
case LAMBDA -> {
|
||||
lambdaService.invokeArn(arn, payload.getBytes(), InvocationType.Event);
|
||||
LOG.debugv("Scheduler delivered to Lambda: {0}", arn);
|
||||
}
|
||||
case SNS -> {
|
||||
snsService.publish(arn, null, payload, "Scheduler", targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SNS: {0}", arn);
|
||||
}
|
||||
case ECS_RUN_TASK -> {
|
||||
deliverToEcsRunTask(target, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to ECS RunTask: {0}", arn);
|
||||
}
|
||||
case STEP_FUNCTIONS -> {
|
||||
String targetAccount = AwsArnUtils.parse(arn).accountId();
|
||||
RequestScopes.runAs(targetAccount,
|
||||
() -> stepFunctionsService.startExecution(arn, null, payload, targetRegion));
|
||||
LOG.debugv("Scheduler started Step Functions execution: {0}", arn);
|
||||
}
|
||||
case EVENT_BRIDGE -> {
|
||||
deliverToEventBridge(target, payload, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to EventBridge: {0}", arn);
|
||||
}
|
||||
case UNSUPPORTED ->
|
||||
throw new UnsupportedOperationException("Scheduler: unsupported target ARN type: " + arn);
|
||||
}
|
||||
return requestBody;
|
||||
return templatedTargetRequest(target, kind, payload);
|
||||
}
|
||||
|
||||
/** Returns the JSON request sent to the target service, for invocation diagnostics and DLQs. */
|
||||
public String materializeRequest(Target target, String region) {
|
||||
/**
|
||||
* Returns the JSON request sent to the target service, for invocation diagnostics and DLQs.
|
||||
* Templated targets use the field names of the target service's API (SQS SendMessage, Lambda
|
||||
* Invoke, SNS Publish, ECS RunTask, Step Functions StartExecution, EventBridge PutEvents), as
|
||||
* in the dead-letter queue example of the Scheduler user guide.
|
||||
*/
|
||||
public String materializeRequest(Schedule schedule, Instant scheduledAt) {
|
||||
Target target = schedule.getTarget();
|
||||
if (target == null || target.getArn() == null) {
|
||||
return "{}";
|
||||
}
|
||||
String arn = target.getArn();
|
||||
String payload = target.getInput() != null ? target.getInput() : "{}";
|
||||
int sdkIdx = arn.indexOf(":aws-sdk:");
|
||||
if (sdkIdx >= 0) {
|
||||
return validJsonOrString(payload);
|
||||
if (isUniversalTarget(target.getArn())) {
|
||||
return universalTargetRequest(target);
|
||||
}
|
||||
if (arn.contains(":sqs:")) {
|
||||
Map<String, Object> request = new LinkedHashMap<>();
|
||||
request.put("MessageBody", payload);
|
||||
request.put("QueueUrl", AwsArnUtils.arnToQueueUrl(arn, baseUrl));
|
||||
if (target.getSqsParameters() != null
|
||||
&& target.getSqsParameters().getMessageGroupId() != null) {
|
||||
request.put("MessageGroupId", target.getSqsParameters().getMessageGroupId());
|
||||
return templatedTargetRequest(target, TargetKind.of(target), templatedPayload(schedule, scheduledAt));
|
||||
}
|
||||
|
||||
/** The templated target types Floci delivers to, in the order their ARNs are recognised. */
|
||||
private enum TargetKind {
|
||||
SQS, LAMBDA, SNS, ECS_RUN_TASK, STEP_FUNCTIONS, EVENT_BRIDGE, UNSUPPORTED;
|
||||
|
||||
static TargetKind of(Target target) {
|
||||
String arn = target.getArn();
|
||||
if (arn.contains(":sqs:")) {
|
||||
return SQS;
|
||||
}
|
||||
return writeJson(request);
|
||||
if (arn.contains(":lambda:") || arn.contains(":function:")) {
|
||||
return LAMBDA;
|
||||
}
|
||||
if (arn.contains(":sns:")) {
|
||||
return SNS;
|
||||
}
|
||||
if (arn.contains(":ecs:") && target.getEcsParameters() != null) {
|
||||
return ECS_RUN_TASK;
|
||||
}
|
||||
if (isStateMachineArn(arn)) {
|
||||
return STEP_FUNCTIONS;
|
||||
}
|
||||
if (isEventBridgePutEventsArn(arn)) {
|
||||
return EVENT_BRIDGE;
|
||||
}
|
||||
return UNSUPPORTED;
|
||||
}
|
||||
return validJsonOrString(payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* Universal targets (arn:aws:scheduler:::aws-sdk:<service>:<action>) carry the real
|
||||
* resource identifiers inside Input, not in the target ARN. They are detected before the
|
||||
* ARN-substring routing of {@link TargetKind}, which would otherwise mis-match (e.g.
|
||||
* "aws-sdk:sns:publish" contains ":sns:").
|
||||
*/
|
||||
private static boolean isUniversalTarget(String arn) {
|
||||
return arn.contains(":aws-sdk:");
|
||||
}
|
||||
|
||||
private String universalTargetRequest(Target target) {
|
||||
return validJsonOrString(target.getInput() != null ? target.getInput() : "{}");
|
||||
}
|
||||
|
||||
private String templatedTargetRequest(Target target, TargetKind kind, String payload) {
|
||||
String arn = target.getArn();
|
||||
Map<String, Object> request = new LinkedHashMap<>();
|
||||
switch (kind) {
|
||||
case SQS -> {
|
||||
request.put("MessageBody", payload);
|
||||
request.put("QueueUrl", AwsArnUtils.arnToQueueUrl(arn, baseUrl));
|
||||
if (target.getSqsParameters() != null
|
||||
&& target.getSqsParameters().getMessageGroupId() != null) {
|
||||
request.put("MessageGroupId", target.getSqsParameters().getMessageGroupId());
|
||||
}
|
||||
}
|
||||
case LAMBDA -> {
|
||||
request.put("FunctionName", arn);
|
||||
request.put("InvocationType", InvocationType.Event.name());
|
||||
request.put("Payload", payload);
|
||||
}
|
||||
case SNS -> {
|
||||
request.put("TopicArn", arn);
|
||||
request.put("Message", payload);
|
||||
}
|
||||
case ECS_RUN_TASK -> request.putAll(ecsRunTaskRequest(target));
|
||||
case STEP_FUNCTIONS -> {
|
||||
request.put("stateMachineArn", arn);
|
||||
request.put("input", payload);
|
||||
}
|
||||
case EVENT_BRIDGE -> request.put("Entries", List.of(eventBridgeEntry(target, payload)));
|
||||
case UNSUPPORTED -> {
|
||||
return validJsonOrString(payload);
|
||||
}
|
||||
}
|
||||
return writeJson(request);
|
||||
}
|
||||
|
||||
private static Map<String, Object> ecsRunTaskRequest(Target target) {
|
||||
EcsParameters ecs = target.getEcsParameters();
|
||||
Map<String, Object> request = new LinkedHashMap<>();
|
||||
request.put("cluster", target.getArn());
|
||||
request.put("taskDefinition", ecs.getTaskDefinitionArn());
|
||||
request.put("count", ecs.getTaskCount() != null ? ecs.getTaskCount() : 1);
|
||||
putIfPresent(request, "launchType", ecs.getLaunchType());
|
||||
putIfPresent(request, "group", ecs.getGroup());
|
||||
if (ecs.getNetworkConfiguration() != null && ecs.getNetworkConfiguration().getAwsvpcConfiguration() != null) {
|
||||
AwsVpcConfiguration vpc = ecs.getNetworkConfiguration().getAwsvpcConfiguration();
|
||||
Map<String, Object> awsvpc = new LinkedHashMap<>();
|
||||
putIfPresent(awsvpc, "subnets", vpc.getSubnets());
|
||||
putIfPresent(awsvpc, "securityGroups", vpc.getSecurityGroups());
|
||||
putIfPresent(awsvpc, "assignPublicIp", vpc.getAssignPublicIp());
|
||||
request.put("networkConfiguration", Map.of("awsvpcConfiguration", awsvpc));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private static void putIfPresent(Map<String, Object> map, String key, Object value) {
|
||||
if (value != null) {
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload a templated target receives: the target's {@code Input}, or Scheduler's default
|
||||
* notification when no {@code Input} was configured (API reference, {@code Target.Input}).
|
||||
*/
|
||||
private String templatedPayload(Schedule schedule, Instant scheduledAt) {
|
||||
String input = schedule.getTarget().getInput();
|
||||
return input != null ? input : defaultScheduledEvent(schedule, scheduledAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scheduler's default notification: an EventBridge-style {@code Scheduled Event} whose
|
||||
* {@code resources} names the schedule and whose {@code detail} is the string "{}". The event
|
||||
* id is stable per occurrence so retries and the dead-letter body carry the same event.
|
||||
*/
|
||||
private String defaultScheduledEvent(Schedule schedule, Instant scheduledAt) {
|
||||
AwsArnUtils.Arn scheduleArn = AwsArnUtils.parse(schedule.getArn());
|
||||
Map<String, Object> event = new LinkedHashMap<>();
|
||||
event.put("version", "0");
|
||||
event.put("id", UUID.nameUUIDFromBytes(
|
||||
(schedule.getArn() + "@" + scheduledAt).getBytes(StandardCharsets.UTF_8)).toString());
|
||||
event.put("detail-type", "Scheduled Event");
|
||||
event.put("source", "aws.scheduler");
|
||||
event.put("account", scheduleArn.accountId());
|
||||
event.put("time", scheduledAt.truncatedTo(ChronoUnit.SECONDS).toString());
|
||||
event.put("region", scheduleArn.region());
|
||||
event.put("resources", List.of(schedule.getArn()));
|
||||
event.put("detail", "{}");
|
||||
return writeJson(event);
|
||||
}
|
||||
|
||||
private String validJsonOrString(String payload) {
|
||||
@@ -190,7 +328,8 @@ public class ScheduleInvoker {
|
||||
if (source == null || source.getAwsvpcConfiguration() == null) {
|
||||
return null;
|
||||
}
|
||||
io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration sourceVpc = source.getAwsvpcConfiguration();
|
||||
AwsVpcConfiguration sourceVpc = source.getAwsvpcConfiguration();
|
||||
// The ECS model's AwsVpcConfiguration clashes with the scheduler model's, which is imported.
|
||||
io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration targetVpc =
|
||||
new io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration();
|
||||
targetVpc.setSubnets(sourceVpc.getSubnets());
|
||||
@@ -283,7 +422,7 @@ public class ScheduleInvoker {
|
||||
return value != null && !value.isNull() ? value.asText() : null;
|
||||
}
|
||||
|
||||
private boolean isEventBridgePutEventsArn(String arn) {
|
||||
private static boolean isEventBridgePutEventsArn(String arn) {
|
||||
return arn.contains(":events:") && arn.contains(":event-bus/");
|
||||
}
|
||||
|
||||
@@ -297,6 +436,11 @@ public class ScheduleInvoker {
|
||||
}
|
||||
|
||||
private void deliverToEventBridge(Target target, String payload, String region) {
|
||||
eventBridgeService.putEvents(List.of(eventBridgeEntry(target, payload)), region);
|
||||
}
|
||||
|
||||
/** One PutEvents entry for an event-bus target, as delivered and as reported in diagnostics. */
|
||||
private Map<String, Object> eventBridgeEntry(Target target, String payload) {
|
||||
String busArn = target.getArn();
|
||||
String busName = busArn.substring(busArn.indexOf(":event-bus/") + ":event-bus/".length());
|
||||
|
||||
@@ -309,12 +453,12 @@ public class ScheduleInvoker {
|
||||
String detailType = ebp != null && ebp.getDetailType() != null && !ebp.getDetailType().isBlank()
|
||||
? ebp.getDetailType() : "Scheduled Event";
|
||||
|
||||
Map<String, Object> entry = new HashMap<>();
|
||||
Map<String, Object> entry = new LinkedHashMap<>();
|
||||
entry.put("EventBusName", busName);
|
||||
entry.put("Source", source);
|
||||
entry.put("DetailType", detailType);
|
||||
entry.put("Detail", asDetail(payload));
|
||||
eventBridgeService.putEvents(List.of(entry), region);
|
||||
return entry;
|
||||
}
|
||||
|
||||
private String asDetail(String payload) {
|
||||
@@ -333,4 +477,9 @@ public class ScheduleInvoker {
|
||||
private static String extractRegion(String arn, String defaultRegion) {
|
||||
return AwsArnUtils.regionOrDefault(arn, defaultRegion);
|
||||
}
|
||||
|
||||
/** The region a schedule lives in, taken from its ARN. */
|
||||
static String regionOf(Schedule schedule) {
|
||||
return AwsArnUtils.regionOrDefault(schedule.getArn(), "us-east-1");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -8,7 +9,12 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.ScheduleGroup;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.ScheduleRequest;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -26,6 +32,11 @@ public class SchedulerService {
|
||||
// AWS EventBridge Scheduler name constraints: [0-9a-zA-Z-_.]+, 1-64 chars.
|
||||
private static final Pattern NAME_PATTERN = Pattern.compile("[0-9a-zA-Z\\-_.]{1,64}");
|
||||
private static final String DEFAULT_GROUP = "default";
|
||||
// FAIL_ON_TRAILING_TOKENS matters here: without it an Input of "{} garbage" parses as the
|
||||
// leading object and the rest is silently dropped, so a value AWS rejects would be stored.
|
||||
private static final ObjectReader JSON = new ObjectMapper()
|
||||
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
.readerFor(JsonNode.class);
|
||||
|
||||
private final StorageBackend<String, ScheduleGroup> groupStore;
|
||||
private final StorageBackend<String, Schedule> scheduleStore;
|
||||
@@ -374,6 +385,38 @@ public class SchedulerService {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value null at 'target.deadLetterConfig.arn' failed to satisfy constraint: Member must not be null", 400);
|
||||
}
|
||||
String input = req.getTarget().getInput();
|
||||
if (input != null && requiresJsonInput(req.getTarget().getArn()) && !isWellFormedJson(input)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value at 'target.input' failed to satisfy constraint: Member must be well-formed JSON for Lambda, Step Functions, and EventBridge targets", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Templated Lambda, Step Functions and EventBridge targets require well-formed JSON
|
||||
* {@code Input} (API reference, {@code Target.Input}); other templated targets accept any
|
||||
* text, and universal ({@code aws-sdk}) targets are only checked when invoked.
|
||||
*/
|
||||
private static boolean requiresJsonInput(String targetArn) {
|
||||
return AwsArnUtils.isArnFor(targetArn, "lambda")
|
||||
|| AwsArnUtils.isArnFor(targetArn, "states")
|
||||
|| AwsArnUtils.isArnFor(targetArn, "events");
|
||||
}
|
||||
|
||||
/**
|
||||
* A blank value is rejected up front: {@code Input} has a minimum length of 1, and
|
||||
* {@code readTree} returns a missing node for it instead of failing.
|
||||
*/
|
||||
private static boolean isWellFormedJson(String value) {
|
||||
if (value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
JSON.readTree(value);
|
||||
return true;
|
||||
} catch (JsonProcessingException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String buildGroupArn(String region, String name) {
|
||||
|
||||
Reference in new issue
Block a user