From 1a6f336e9fcf29baed7a9a577e306830c041c835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20Th=E1=BA=BF=20Huy?= <31652864+vuthehuyht@users.noreply.github.com> Date: Sat, 10 Oct 2026 02:51:11 +0700 Subject: [PATCH] feat(redshift): run Data API statements from Step Functions and Scheduler (#5312) * feat(redshift): run Data API statements from Step Functions and Scheduler Step Functions: add the arn:aws:states:::aws-sdk:redshiftdata: Task integration. It reuses RedshiftDataJsonHandler, so every Data API action Floci implements works for provisioned clusters (ClusterIdentifier) and Serverless workgroups (WorkgroupName). Arguments and results keep the wire PascalCase names, CreatedAt and UpdatedAt are ISO-8601 at any depth, errors surface as RedshiftData.Exception, and the task fails when redshift or redshift-data is disabled. .sync resources are rejected, as AWS has no such variant. Scheduler: add the redshiftdata:executeStatement and redshiftdata:batchExecuteStatement universal targets. The call runs as the schedule's account and region, because the dispatcher thread carries no request account and cluster and statement storage is account-aware. A SQL error stays on the statement; a request the Data API rejects propagates so the existing retry and dead-letter handling applies. * fix(redshift-data): validate task input and isolate statement storage by account * fix(redshift): run scheduled Data API statements off the dispatcher thread Keep the request check on the dispatcher so a rejected request still retries, and run the SQL on its own thread under the schedule's account. Ask ServiceRegistry whether redshift-data is enabled instead of repeating the rule, keep the resolver and connection factory package-private, and cover the provisioned-cluster account in the resolver test. * fix(scheduler): await scheduled Redshift Data statement in docker test --- docs/services/scheduler.md | 11 + docs/services/step-functions.md | 11 + .../RedshiftDataResourceResolver.java | 2 +- .../redshiftdata/RedshiftDataService.java | 45 ++- .../services/scheduler/ScheduleInvoker.java | 91 ++++- .../services/stepfunctions/AslExecutor.java | 104 +++++- .../RedshiftDataResourceResolverTest.java | 16 + ...lerRedshiftDataAccountIntegrationTest.java | 132 +++++++ .../scheduler/ScheduleInvokerTest.java | 155 +++++++- ...ulerRedshiftDataDockerIntegrationTest.java | 166 +++++++++ .../AslExecutorRedshiftDataDisabledTest.java | 86 +++++ ...ionsRedshiftDataDockerIntegrationTest.java | 156 ++++++++ ...pFunctionsRedshiftDataIntegrationTest.java | 332 ++++++++++++++++++ 13 files changed, 1291 insertions(+), 16 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/redshiftdata/SchedulerRedshiftDataAccountIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerRedshiftDataDockerIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorRedshiftDataDisabledTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataIntegrationTest.java diff --git a/docs/services/scheduler.md b/docs/services/scheduler.md index fdc815eef1..90684c7c5c 100644 --- a/docs/services/scheduler.md +++ b/docs/services/scheduler.md @@ -70,6 +70,17 @@ does. A blank `Input`, or one with text after the JSON value such as and the `Input` of a universal (`aws-sdk`) target is only checked when the schedule is invoked. +Universal targets (`arn:aws:scheduler:::aws-sdk::`) deliver `sns:publish`, +`sqs:sendMessage`, `redshiftdata:executeStatement` and `redshiftdata:batchExecuteStatement`; any +other action fails when the schedule fires. The Redshift Data targets take the Data API request +as `Input` (PascalCase) and run the SQL against a provisioned cluster (`ClusterIdentifier`) or a +Serverless workgroup (`WorkgroupName`). The SQL runs on its own thread, so a slow statement does not +delay other schedules. Delivery succeeds once the Data API accepts the request; +a SQL error is recorded on the statement and does not trigger Scheduler retries, while a request +the Data API rejects (an unknown cluster, a malformed request) is retried like any failed +delivery. When `redshift` or `redshift-data` is disabled the target fails with +`ServiceNotAvailableException`. + ### Retries and dead-letter queues A failed invocation is retried after a deterministic capped exponential delay diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md index 013dba33c1..604bded197 100644 --- a/docs/services/step-functions.md +++ b/docs/services/step-functions.md @@ -493,6 +493,8 @@ the wire and the task fails with `Sfn.StateMachineDoesNotExistException`. | `arn:aws:states:::aws-sdk:sfn:sendTaskSuccess` | `{}` | `Sfn.InvalidTokenException` when no task is waiting on the token | | `arn:aws:states:::aws-sdk:sfn:sendTaskFailure` | `{}` | `Sfn.InvalidTokenException` | | `arn:aws:states:::aws-sdk:rdsdata:executeStatement` | RDS Data statement result | `RdsData.BadRequestException` for an invalid request | +| `arn:aws:states:::aws-sdk:redshiftdata:executeStatement` | Data API statement handle (`Id`, `CreatedAt`, ...) | `RedshiftData.ValidationException` for an invalid request | +| `arn:aws:states:::aws-sdk:redshiftdata:describeStatement` | statement status, `ResultRows` | `RedshiftData.ResourceNotFoundException` for an unknown `Id` | | `arn:aws:states:::aws-sdk:scheduler:createSchedule` | `{ScheduleArn}` | `Scheduler.ConflictException` when the name is taken | | `arn:aws:states:::aws-sdk:scheduler:updateSchedule` | `{ScheduleArn}` | `Scheduler.ResourceNotFoundException` | | `arn:aws:states:::aws-sdk:scheduler:deleteSchedule` | `{}` | `Scheduler.ResourceNotFoundException` | @@ -512,6 +514,15 @@ PascalCase names such as `ResourceArn`, `SecretArn`, `Sql` and `Parameters`; the them to the direct API shape and returns a recursively PascalCase result. Other RDS Data actions are not routed through Step Functions yet. +Every Redshift Data API action Floci implements is reachable as `aws-sdk:redshiftdata:` +(`executeStatement`, `batchExecuteStatement`, `describeStatement`, `getStatementResult`, +`getStatementResultV2`, `listStatements`, `cancelStatement`, `listDatabases`, `listSchemas`, +`listTables`, `describeTable`). Arguments and results keep the Data API's PascalCase names, and +`CreatedAt` and `UpdatedAt` are ISO-8601 strings. Use `ClusterIdentifier` for a provisioned cluster +or `WorkgroupName` for a Serverless workgroup. A statement that fails in SQL is not a task failure: +`executeStatement` succeeds and `describeStatement` reports `Status: FAILED`, as on AWS. There is no +`.sync` variant, as on AWS. + ## Publishing to SNS `arn:aws:states:::sns:publish` calls the SNS Publish API with the task's parameters and returns the diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java index 0ef79e221b..33e1cffccd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolver.java @@ -180,7 +180,7 @@ class RedshiftDataResourceResolver { private String clusterArn(String clusterId, String region) { String r = (region == null || region.isBlank()) ? regionResolver.getDefaultRegion() : region; - return AwsArnUtils.Arn.of("redshift", r, "000000000000", "cluster:" + clusterId).toString(); + return AwsArnUtils.Arn.of("redshift", r, regionResolver.getAccountId(), "cluster:" + clusterId).toString(); } private static AwsException validation(String message) { diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java index 2aa84383d0..2c215bd2af 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataService.java @@ -67,6 +67,23 @@ public class RedshiftDataService implements Resettable { // ── ExecuteStatement ──────────────────────────────────────────────────── public ObjectNode executeStatement(JsonNode request, String region) { + PendingStatement pending = prepareStatement(request, region); + pending.run().run(); + return executeResponse(pending.statement()); + } + + /** + * Validates and resolves the request on the calling thread, so a rejected request throws here, + * and returns the SQL run for the caller to schedule. The statement is stored as {@code STARTED} + * first, as AWS does, so DescribeStatement can see it while the run is in flight. + */ + public Runnable submitStatement(JsonNode request, String region) { + PendingStatement pending = prepareStatement(request, region); + store.put(pending.statement()); + return pending.run(); + } + + private PendingStatement prepareStatement(JsonNode request, String region) { String sql = requiredText(request, "Sql"); rejectMultiStatement(sql); Map parameters = RedshiftDataSqlParameters.parseParameters(request, "Parameters"); @@ -82,11 +99,14 @@ public class RedshiftDataService implements Resettable { stored.database = target.database(); stored.resultFormat = request.path("ResultFormat").asText("JSON"); - runStatement(stored, target, sql, parameters); - store.put(stored); - return executeResponse(stored); + return new PendingStatement(stored, () -> { + runStatement(stored, target, sql, parameters); + store.put(stored); + }); } + private record PendingStatement(RedshiftDataStatementStore.StoredStatement statement, Runnable run) { } + private void runStatement(RedshiftDataStatementStore.StoredStatement stored, RedshiftDataResourceResolver.DatabaseTarget target, String sql, Map parameters) { @@ -316,6 +336,19 @@ public class RedshiftDataService implements Resettable { // ── BatchExecuteStatement ────────────────────────────────────────────── public ObjectNode batchExecuteStatement(JsonNode request, String region) { + PendingStatement pending = prepareBatch(request, region); + pending.run().run(); + return executeResponse(pending.statement()); + } + + /** Batch counterpart of {@link #submitStatement}. */ + public Runnable submitBatch(JsonNode request, String region) { + PendingStatement pending = prepareBatch(request, region); + store.put(pending.statement()); + return pending.run(); + } + + private PendingStatement prepareBatch(JsonNode request, String region) { JsonNode sqlsNode = request.get("Sqls"); if (sqlsNode == null || !sqlsNode.isArray() || sqlsNode.isEmpty()) { throw new AwsException("ValidationException", "Sqls must be a non-empty array of SQL statements.", 400); @@ -338,6 +371,11 @@ public class RedshiftDataService implements Resettable { parent.resultFormat = request.path("ResultFormat").asText("JSON"); parent.subStatements = new ArrayList<>(); + return new PendingStatement(parent, () -> runBatch(parent, sqls, target)); + } + + private void runBatch(RedshiftDataStatementStore.StoredStatement parent, List sqls, + RedshiftDataResourceResolver.DatabaseTarget target) { long totalDuration = 0; try (Connection connection = connectionFactory.open(target)) { BackendSql batchBackend = new RedshiftDataBackendSql(connection); @@ -411,7 +449,6 @@ public class RedshiftDataService implements Resettable { parent.durationNanos = totalDuration; parent.updatedAt = Instant.now(); store.put(parent); - return executeResponse(parent); } // ── ListStatements ───────────────────────────────────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java index 95209dbf37..bb40c06264 100644 --- a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java +++ b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java @@ -6,11 +6,13 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.services.ecs.EcsService; 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.redshiftdata.RedshiftDataService; import io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration; import io.github.hectorvent.floci.services.scheduler.model.EcsParameters; import io.github.hectorvent.floci.services.scheduler.model.EventBridgeParameters; @@ -21,6 +23,7 @@ import io.github.hectorvent.floci.services.sns.SnsService; import io.github.hectorvent.floci.services.sqs.SqsService; import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue; import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; +import jakarta.annotation.PreDestroy; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -34,12 +37,17 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.Supplier; /** * Delivers an EventBridge Scheduler target invocation to the underlying service. * Supports templated SQS, Lambda, SNS, Step Functions, and EventBridge PutEvents targets, plus * universal targets ({@code arn:aws:scheduler:::aws-sdk::}) for - * {@code sns:publish} and {@code sqs:sendMessage}. Mirrors the subset handled by + * {@code sns:publish}, {@code sqs:sendMessage}, {@code redshiftdata:executeStatement} and + * {@code redshiftdata:batchExecuteStatement}. Mirrors the subset handled by * {@code EventBridgeInvoker} but using Scheduler's {@link Target} model (raw * {@code input} string, no JSONPath/template). */ @@ -59,9 +67,12 @@ public class ScheduleInvoker { private final EventBridgeService eventBridgeService; private final EcsService ecsService; private final StepFunctionsService stepFunctionsService; + private final RedshiftDataService redshiftDataService; private final ObjectMapper objectMapper; private final String baseUrl; private final String defaultRegion; + private final ServiceRegistry serviceRegistry; + private final Executor redshiftDataExecutor; @Inject public ScheduleInvoker(SqsService sqsService, @@ -70,17 +81,52 @@ public class ScheduleInvoker { EventBridgeService eventBridgeService, EcsService ecsService, StepFunctionsService stepFunctionsService, + RedshiftDataService redshiftDataService, ObjectMapper objectMapper, - EmulatorConfig config) { + EmulatorConfig config, + ServiceRegistry serviceRegistry) { + this(sqsService, lambdaService, snsService, eventBridgeService, ecsService, stepFunctionsService, + redshiftDataService, objectMapper, config, serviceRegistry, newRedshiftDataExecutor()); + } + + ScheduleInvoker(SqsService sqsService, + LambdaService lambdaService, + SnsService snsService, + EventBridgeService eventBridgeService, + EcsService ecsService, + StepFunctionsService stepFunctionsService, + RedshiftDataService redshiftDataService, + ObjectMapper objectMapper, + EmulatorConfig config, + ServiceRegistry serviceRegistry, + Executor redshiftDataExecutor) { this.sqsService = sqsService; this.lambdaService = lambdaService; this.snsService = snsService; this.eventBridgeService = eventBridgeService; this.ecsService = ecsService; this.stepFunctionsService = stepFunctionsService; + this.redshiftDataService = redshiftDataService; this.objectMapper = objectMapper; this.baseUrl = config.baseUrl(); this.defaultRegion = config.defaultRegion(); + this.serviceRegistry = serviceRegistry; + this.redshiftDataExecutor = redshiftDataExecutor; + } + + private static ExecutorService newRedshiftDataExecutor() { + return Executors.newCachedThreadPool(r -> { + Thread t = new Thread(r, "scheduler-redshift-data"); + t.setDaemon(true); + return t; + }); + } + + @PreDestroy + void shutdown() { + if (redshiftDataExecutor instanceof ExecutorService service) { + service.shutdownNow(); + } } /** @@ -107,7 +153,7 @@ public class ScheduleInvoker { String input = contextualInput(schedule, scheduledAt, executionId, attemptNumber); if (isUniversalTarget(arn)) { invokeUniversalTarget(arn.substring(arn.indexOf(":aws-sdk:") + ":aws-sdk:".length()), - input, region); + input, region, schedule.getAccountId()); return universalTargetRequest(input); } @@ -401,9 +447,11 @@ public class ScheduleInvoker { * Dispatches an EventBridge Scheduler universal target ({@code aws-sdk::}), * reading the call parameters from the target's {@code Input} payload. Supports the * common {@code sns:publish} and {@code sqs:sendMessage} actions; other actions fail - * as unsupported. + * as unsupported. {@code scheduleAccountId} scopes the Redshift Data calls, whose cluster and + * statement storage is account-aware and the dispatcher thread carries no request account. */ - private void invokeUniversalTarget(String serviceAction, String input, String region) { + private void invokeUniversalTarget(String serviceAction, String input, String region, + String scheduleAccountId) { JsonNode params; try { params = objectMapper.readTree(input == null || input.isBlank() ? "{}" : input); @@ -436,11 +484,44 @@ public class ScheduleInvoker { messageAttributes, region); LOG.debugv("Scheduler delivered to SQS (universal target): {0}", queueUrl); } + case "redshiftdata:executeStatement" -> { + requireRedshiftData(); + submitRedshiftData(scheduleAccountId, region, () -> redshiftDataService.submitStatement(params, region)); + LOG.debugv("Scheduler submitted to the Redshift Data API (universal target): {0}", serviceAction); + } + case "redshiftdata:batchExecuteStatement" -> { + requireRedshiftData(); + submitRedshiftData(scheduleAccountId, region, () -> redshiftDataService.submitBatch(params, region)); + LOG.debugv("Scheduler submitted to the Redshift Data API (universal target): {0}", serviceAction); + } default -> throw new UnsupportedOperationException( "Scheduler: unsupported universal target action: " + serviceAction); } } + private void requireRedshiftData() { + if (!serviceRegistry.isServiceEnabled("redshift-data")) { + throw new AwsException("ServiceNotAvailableException", "Service redshift-data is not enabled.", 400); + } + } + + /** + * The Data API is asynchronous: AWS returns once the statement is submitted. The request check + * runs here, so a rejected request still fails the occurrence and is retried, while the SQL runs + * on its own thread so a slow statement cannot hold up the single dispatcher thread. Both halves + * run as the schedule's account, which neither thread carries. + */ + private void submitRedshiftData(String scheduleAccountId, String region, Supplier submit) { + Runnable statement = RequestScopes.callAs(scheduleAccountId, region, submit); + redshiftDataExecutor.execute(() -> { + try { + RequestScopes.runAs(scheduleAccountId, region, statement); + } catch (RuntimeException e) { + LOG.warnv("Scheduled Redshift Data statement failed: {0}", e.getMessage()); + } + }); + } + private static Map parseUniversalSqsMessageAttributes(JsonNode attrsNode) { Map attributes = new HashMap<>(); if (attrsNode == null || !attrsNode.isObject()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java index f90c61a4db..4dc4242bde 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java @@ -25,6 +25,7 @@ import io.github.hectorvent.floci.core.common.CustomResourceLiveness; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.core.common.XmlParser; import io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler; import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade; @@ -50,6 +51,7 @@ import io.github.hectorvent.floci.services.lambda.model.InvocationType; import io.github.hectorvent.floci.services.lambda.model.InvokeResult; import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; import io.github.hectorvent.floci.services.rdsdata.RdsDataService; +import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataJsonHandler; import io.github.hectorvent.floci.services.s3.S3Service; import io.github.hectorvent.floci.services.s3.model.S3Object; import io.github.hectorvent.floci.services.scheduler.SchedulerController; @@ -252,6 +254,8 @@ public class AslExecutor { private final SchedulerService schedulerService; private final SchedulerController schedulerController; private final RdsDataService rdsDataService; + private final RedshiftDataJsonHandler redshiftDataHandler; + private final ServiceRegistry serviceRegistry; private final ObjectMapper objectMapper; private final Configuration jsonPathConfiguration; private final JsonataEvaluator jsonataEvaluator; @@ -281,13 +285,14 @@ public class AslExecutor { SchedulerController schedulerController, RdsDataService rdsDataService, ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator, Instance sfnService, EmulatorConfig config, Vertx vertx, - CustomResourceLiveness customResourceLiveness) { + CustomResourceLiveness customResourceLiveness, RedshiftDataJsonHandler redshiftDataHandler, + ServiceRegistry serviceRegistry) { this(lambdaExecutor, targetResolver, dynamoDb, dynamoDbJsonHandler, sqsJsonHandler, snsJsonHandler, cloudFormationHandler, ec2Service, s3Service, ecsService, ecsJsonHandler, eventBridgeHandler, schedulerService, schedulerController, rdsDataService, objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness, - Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null); + Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, redshiftDataHandler, serviceRegistry); } AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver, @@ -301,8 +306,11 @@ public class AslExecutor { ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator, Instance sfnService, EmulatorConfig config, Vertx vertx, CustomResourceLiveness customResourceLiveness, - Clock clock, Sleeper sleeper, Integer maxWaitSecondsOverride) { + Clock clock, Sleeper sleeper, Integer maxWaitSecondsOverride, + RedshiftDataJsonHandler redshiftDataHandler, ServiceRegistry serviceRegistry) { + this.serviceRegistry = serviceRegistry; this.customResourceLiveness = customResourceLiveness; + this.redshiftDataHandler = redshiftDataHandler; this.lambdaExecutor = lambdaExecutor; this.targetResolver = targetResolver; this.dynamoDb = dynamoDb; @@ -340,6 +348,47 @@ public class AslExecutor { } } + // Kept for tests that do not exercise the Redshift Data API integration. + AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver, + DynamoDbFacade dynamoDb, DynamoDbJsonHandler dynamoDbJsonHandler, + SqsJsonHandler sqsJsonHandler, SnsJsonHandler snsJsonHandler, + CloudFormationQueryHandler cloudFormationHandler, + Ec2Service ec2Service, S3Service s3Service, + EcsService ecsService, EcsJsonHandler ecsJsonHandler, + EventBridgeHandler eventBridgeHandler, SchedulerService schedulerService, + SchedulerController schedulerController, RdsDataService rdsDataService, + ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator, + Instance sfnService, EmulatorConfig config, Vertx vertx, + CustomResourceLiveness customResourceLiveness) { + this(lambdaExecutor, targetResolver, dynamoDb, dynamoDbJsonHandler, + sqsJsonHandler, snsJsonHandler, cloudFormationHandler, + ec2Service, s3Service, ecsService, ecsJsonHandler, + eventBridgeHandler, schedulerService, schedulerController, rdsDataService, + objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness, + Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null); + } + + // Kept for tests that do not exercise the Redshift Data API integration. + AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver, + DynamoDbFacade dynamoDb, DynamoDbJsonHandler dynamoDbJsonHandler, + SqsJsonHandler sqsJsonHandler, SnsJsonHandler snsJsonHandler, + CloudFormationQueryHandler cloudFormationHandler, + Ec2Service ec2Service, S3Service s3Service, + EcsService ecsService, EcsJsonHandler ecsJsonHandler, + EventBridgeHandler eventBridgeHandler, SchedulerService schedulerService, + SchedulerController schedulerController, RdsDataService rdsDataService, + ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator, + Instance sfnService, EmulatorConfig config, Vertx vertx, + CustomResourceLiveness customResourceLiveness, + Clock clock, Sleeper sleeper, Integer maxWaitSecondsOverride) { + this(lambdaExecutor, targetResolver, dynamoDb, dynamoDbJsonHandler, + sqsJsonHandler, snsJsonHandler, cloudFormationHandler, + ec2Service, s3Service, ecsService, ecsJsonHandler, + eventBridgeHandler, schedulerService, schedulerController, rdsDataService, + objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness, + clock, sleeper, maxWaitSecondsOverride, null, null); + } + AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore, DynamoDbFacade dynamoDb, DynamoDbJsonHandler dynamoDbJsonHandler, SqsJsonHandler sqsJsonHandler, SnsJsonHandler snsJsonHandler, @@ -1208,6 +1257,12 @@ public class AslExecutor { return invokeAwsSdkDynamoDb(camelCaseAction, input, region); } + // AWS SDK service integrations: Redshift Data API (provisioned clusters and Serverless workgroups) + if (integration.isSdkService("redshiftdata") && integration.suffix().isEmpty()) { + String region = extractRegionFromArn(sm.getStateMachineArn()); + return invokeAwsSdkRedshiftData(integration, input, region); + } + // AWS SDK service integration: RDS Data API ExecuteStatement if (integration.isSdkService("rdsdata")) { String region = extractRegionFromArn(sm.getStateMachineArn()); @@ -2285,6 +2340,49 @@ public class AslExecutor { return objectMapper.createObjectNode(); } + /** + * The Data API is a JSON 1.1 service, so the Task input is already the PascalCase wire request and + * the response needs no re-casing. Only the timestamps differ: the wire carries epoch seconds where + * the {@code aws-sdk:} integration renders the SDK's ISO-8601 {@code Instant}. + */ + private JsonNode invokeAwsSdkRedshiftData(StatesIntegration integration, JsonNode input, String region) { + if (!serviceRegistry.isServiceEnabled("redshift-data")) { + throw new FailStateException(sdkExceptionName("RedshiftData", "ServiceNotAvailableException"), + "Service redshift-data is not enabled."); + } + if (input != null && !input.isObject()) { + throw new FailStateException(sdkExceptionName("RedshiftData", "ValidationException"), + "The task input must be a JSON object."); + } + JsonNode request = input != null ? input : objectMapper.createObjectNode(); + Response response; + try { + response = redshiftDataHandler.handle(capitalizeFirst(integration.api()), request, region); + } catch (AwsException e) { + throw new FailStateException(sdkExceptionName("RedshiftData", e.getErrorCode()), e.getMessage()); + } + if (!(response.getEntity() instanceof JsonNode body)) { + return objectMapper.createObjectNode(); + } + JsonNode rendered = body.deepCopy(); + renderRedshiftDataTimestamps(rendered); + return rendered; + } + + private static void renderRedshiftDataTimestamps(JsonNode node) { + if (node instanceof ObjectNode object) { + for (String field : List.of("CreatedAt", "UpdatedAt")) { + JsonNode value = object.get(field); + if (value != null && value.isNumber()) { + object.put(field, sdkTimestamp(value.asDouble())); + } + } + object.elements().forEachRemaining(AslExecutor::renderRedshiftDataTimestamps); + } else if (node instanceof ArrayNode array) { + array.elements().forEachRemaining(AslExecutor::renderRedshiftDataTimestamps); + } + } + private JsonNode invokeAwsSdkRdsData(StatesIntegration integration, JsonNode input, String region) { if (!"executeStatement".equals(integration.api())) { throw new FailStateException("States.TaskFailed", diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java index 4784c5998a..1f45f0424d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/RedshiftDataResourceResolverTest.java @@ -43,6 +43,7 @@ class RedshiftDataResourceResolverTest { private RedshiftDataResourceResolver resolver(RedshiftService redshift, SecretsManagerService secrets) { RegionResolver regionResolver = mock(RegionResolver.class); when(regionResolver.getAccountId()).thenReturn(ACCOUNT); + when(regionResolver.getDefaultRegion()).thenReturn(REGION); return new RedshiftDataResourceResolver(redshift, secrets, mapper, broker, regionResolver, serverless); } @@ -66,6 +67,21 @@ class RedshiftDataResourceResolverTest { assertEquals("Secret123", target.password()); } + @Test + void aProvisionedClusterArnCarriesTheCallersAccount() { + RedshiftService redshift = mock(RedshiftService.class); + when(redshift.describeClusters("wh")).thenReturn(List.of(cluster())); + ObjectNode req = mapper.createObjectNode(); + req.put("ClusterIdentifier", "wh"); + req.put("DbUser", "admin"); + req.put("Database", "dev"); + + RedshiftDataResourceResolver.DatabaseTarget target = + resolver(redshift, mock(SecretsManagerService.class)).resolve(req, REGION); + + assertEquals("arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:wh", target.arn()); + } + private static RedshiftServerlessService.WorkgroupTarget workgroupTarget() { return new RedshiftServerlessService.WorkgroupTarget( "arn:aws:redshift-serverless:us-east-1:" + ACCOUNT + ":workgroup/wg-id", "wg-1", diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftdata/SchedulerRedshiftDataAccountIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/SchedulerRedshiftDataAccountIntegrationTest.java new file mode 100644 index 0000000000..8e713be823 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftdata/SchedulerRedshiftDataAccountIntegrationTest.java @@ -0,0 +1,132 @@ +package io.github.hectorvent.floci.services.redshiftdata; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumSession; +import io.github.hectorvent.floci.services.scheduler.ScheduleInvoker; +import io.github.hectorvent.floci.services.scheduler.model.Schedule; +import io.github.hectorvent.floci.services.scheduler.model.Target; +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.SQLException; +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +/** + * The dispatcher fires a schedule on a thread with no request, so account-aware storage would + * fall back to the default account unless the invoker runs the call as the schedule's account. + */ +@QuarkusTest +class SchedulerRedshiftDataAccountIntegrationTest { + + private static final String SCHEDULE_ACCOUNT = "111122223333"; + + @Inject + ScheduleInvoker scheduleInvoker; + + @Inject + RedshiftDataService redshiftDataService; + + @InjectMock + RedshiftDataResourceResolver resolver; + + @InjectMock + RedshiftDataConnectionFactory connectionFactory; + + @BeforeEach + void setUp() throws SQLException { + redshiftDataService.clear(); + when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget( + "arn:aws:redshift:us-east-1:" + SCHEDULE_ACCOUNT + ":cluster:analytics", + "127.0.0.1", 5439, "dev", "admin", "x", + new SpectrumSession(SCHEDULE_ACCOUNT, SCHEDULE_ACCOUNT + ":analytics", "dev", List.of(), false))); + when(connectionFactory.open(any())).thenThrow(new SQLException("connection refused")); + } + + @Test + void executeStatementRunsAsTheSchedulesAccount() { + scheduleInvoker.invoke(scheduleFor("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement", + "{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}"), + Instant.parse("2026-04-21T09:17:54Z")); + + // Statement was stored under SCHEDULE_ACCOUNT + JsonNode scheduledStatements = RequestScopes.callAs(SCHEDULE_ACCOUNT, + () -> redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode())); + assertEquals(1, scheduledStatements.path("Statements").size()); + String statementId = scheduledStatements.path("Statements").path(0).path("Id").asText(); + + JsonNode described = RequestScopes.callAs(SCHEDULE_ACCOUNT, + () -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId))); + assertEquals(statementId, described.path("Id").asText()); + awaitFailed(statementId); + + // Statement is not visible under default account + JsonNode defaultStatements = redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode()); + assertEquals(0, defaultStatements.path("Statements").size()); + + AwsException exception = assertThrows(AwsException.class, + () -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId))); + assertEquals("ResourceNotFoundException", exception.getErrorCode()); + } + + @Test + void batchExecuteStatementRunsAsTheSchedulesAccount() { + scheduleInvoker.invoke(scheduleFor("arn:aws:scheduler:::aws-sdk:redshiftdata:batchExecuteStatement", + "{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sqls\":[\"select 1\"]}"), + Instant.parse("2026-04-21T09:17:54Z")); + + // Statement was stored under SCHEDULE_ACCOUNT + JsonNode scheduledStatements = RequestScopes.callAs(SCHEDULE_ACCOUNT, + () -> redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode())); + assertEquals(1, scheduledStatements.path("Statements").size()); + String statementId = scheduledStatements.path("Statements").path(0).path("Id").asText(); + + JsonNode described = RequestScopes.callAs(SCHEDULE_ACCOUNT, + () -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId))); + assertEquals(statementId, described.path("Id").asText()); + awaitFailed(statementId); + + // Statement is not visible under default account + JsonNode defaultStatements = redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode()); + assertEquals(0, defaultStatements.path("Statements").size()); + + AwsException exception = assertThrows(AwsException.class, + () -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId))); + assertEquals("ResourceNotFoundException", exception.getErrorCode()); + } + + // The SQL runs off the invoking thread. Waiting for its outcome shows it ran as the schedule's account + // too, and keeps a late write from leaking into the next test. + private void awaitFailed(String statementId) { + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertEquals("FAILED", + RequestScopes.callAs(SCHEDULE_ACCOUNT, () -> redshiftDataService.describeStatement( + JsonNodeFactory.instance.objectNode().put("Id", statementId))).path("Status").asText())); + } + + private static Schedule scheduleFor(String targetArn, String input) { + Target target = new Target(); + target.setArn(targetArn); + target.setRoleArn("arn:aws:iam::" + SCHEDULE_ACCOUNT + ":role/scheduler-role"); + target.setInput(input); + Schedule schedule = new Schedule(); + schedule.setName("rsdata-account"); + schedule.setGroupName("default"); + schedule.setAccountId(SCHEDULE_ACCOUNT); + schedule.setArn("arn:aws:scheduler:us-east-1:" + SCHEDULE_ACCOUNT + ":schedule/default/rsdata-account"); + schedule.setTarget(target); + return schedule; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java index dcf723e0b4..fe5319b8f1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java @@ -1,13 +1,16 @@ package io.github.hectorvent.floci.services.scheduler; +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.AwsException; +import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.services.ecs.EcsService; import io.github.hectorvent.floci.services.ecs.model.ContainerOverride; 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.redshiftdata.RedshiftDataService; import io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration; import io.github.hectorvent.floci.services.scheduler.model.EcsParameters; import io.github.hectorvent.floci.services.scheduler.model.EventBridgeParameters; @@ -21,10 +24,13 @@ import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue; import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.ArgumentCaptor; import java.nio.charset.StandardCharsets; import java.time.Instant; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; @@ -41,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -58,6 +65,8 @@ class ScheduleInvokerTest { private EventBridgeService eventBridgeService; private EcsService ecsService; private StepFunctionsService stepFunctionsService; + private RedshiftDataService redshiftDataService; + private final List submittedStatements = new ArrayList<>(); private ScheduleInvoker invoker; @BeforeEach @@ -68,10 +77,19 @@ class ScheduleInvokerTest { eventBridgeService = mock(EventBridgeService.class); ecsService = mock(EcsService.class); stepFunctionsService = mock(StepFunctionsService.class); - EmulatorConfig config = mock(EmulatorConfig.class); + redshiftDataService = mock(RedshiftDataService.class); + invoker = invokerWithRedshiftEnabled(true, true); + } + + private ScheduleInvoker invokerWithRedshiftEnabled(boolean redshiftEnabled, boolean redshiftDataEnabled) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); when(config.baseUrl()).thenReturn("http://localhost:4566"); - invoker = new ScheduleInvoker(sqsService, lambdaService, snsService, - eventBridgeService, ecsService, stepFunctionsService, new ObjectMapper(), config); + ServiceRegistry serviceRegistry = mock(ServiceRegistry.class); + when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(redshiftEnabled && redshiftDataEnabled); + // Queue instead of run, so a test can see that the invoker returns before the SQL runs. + return new ScheduleInvoker(sqsService, lambdaService, snsService, + eventBridgeService, ecsService, stepFunctionsService, redshiftDataService, + new ObjectMapper(), config, serviceRegistry, submittedStatements::add); } /** Delivers one occurrence of a schedule in {@code region} whose target is {@code target}. */ @@ -355,6 +373,137 @@ class ScheduleInvokerTest { assertNull(binaryAttribute.getStringValue()); } + @Test + void universalRedshiftDataExecuteStatementForwardsClusterRequestAndRegion() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\"," + + "\"DbUser\":\"admin\",\"Sql\":\"select 1\"}"); + + invoke(target, "eu-west-1"); + + ArgumentCaptor request = ArgumentCaptor.forClass(JsonNode.class); + verify(redshiftDataService).submitStatement(request.capture(), eq("eu-west-1")); + assertEquals("select 1", request.getValue().path("Sql").asText()); + assertEquals("analytics", request.getValue().path("ClusterIdentifier").asText()); + } + + @Test + void universalRedshiftDataExecuteStatementForwardsWorkgroupName() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"WorkgroupName\":\"wg-1\",\"Database\":\"dev\",\"Sql\":\"select 1\"}"); + + invoke(target, "us-east-1"); + + ArgumentCaptor request = ArgumentCaptor.forClass(JsonNode.class); + verify(redshiftDataService).submitStatement(request.capture(), eq("us-east-1")); + assertEquals("wg-1", request.getValue().path("WorkgroupName").asText()); + assertTrue(request.getValue().path("ClusterIdentifier").isMissingNode()); + } + + @Test + void universalRedshiftDataBatchExecuteStatementForwardsSqls() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:batchExecuteStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\"," + + "\"DbUser\":\"admin\",\"Sqls\":[\"select 1\",\"select 2\"]}"); + + invoke(target, "us-east-1"); + + ArgumentCaptor request = ArgumentCaptor.forClass(JsonNode.class); + verify(redshiftDataService).submitBatch(request.capture(), eq("us-east-1")); + assertEquals(2, request.getValue().path("Sqls").size()); + } + + @Test + void universalRedshiftDataRejectionPropagatesForRetryAndDeadLetterHandling() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"Sql\":\"select 1\"}"); + when(redshiftDataService.submitStatement(any(JsonNode.class), anyString())) + .thenThrow(new AwsException("ValidationException", "no cluster", 400)); + + assertThrows(AwsException.class, () -> invoke(target, "us-east-1")); + assertTrue(submittedStatements.isEmpty()); + } + + @Test + void universalRedshiftDataRunsTheStatementOffTheCallingThread() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}"); + Runnable statement = mock(Runnable.class); + when(redshiftDataService.submitStatement(any(JsonNode.class), anyString())).thenReturn(statement); + + invoke(target, "us-east-1"); + + verifyNoInteractions(statement); + assertEquals(1, submittedStatements.size()); + submittedStatements.get(0).run(); + verify(statement).run(); + } + + @Test + void universalRedshiftDataFailedStatementDoesNotEscapeTheExecutor() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}"); + when(redshiftDataService.submitStatement(any(JsonNode.class), anyString())) + .thenReturn(() -> { throw new IllegalStateException("boom"); }); + + invoke(target, "us-east-1"); + + submittedStatements.get(0).run(); + } + + @ParameterizedTest + @CsvSource({"false,true", "true,false"}) + void universalRedshiftDataFailsWithoutCallingTheServiceWhenItIsDisabled( + boolean redshiftEnabled, boolean redshiftDataEnabled) { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Sql\":\"select 1\"}"); + ScheduleInvoker disabledInvoker = invokerWithRedshiftEnabled(redshiftEnabled, redshiftDataEnabled); + + AwsException failure = assertThrows(AwsException.class, + () -> disabledInvoker.invoke(scheduleIn("us-east-1", target), SCHEDULED_AT)); + + assertEquals("ServiceNotAvailableException", failure.getErrorCode()); + verifyNoInteractions(redshiftDataService); + } + + @Test + void universalRedshiftDataNonJsonInputFailsWithInvalidParameterValue() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("not json"); + + AwsException failure = assertThrows(AwsException.class, () -> invoke(target, "us-east-1")); + + assertEquals("InvalidParameterValue", failure.getErrorCode()); + verifyNoInteractions(redshiftDataService); + } + + @Test + void universalRedshiftDataOtherActionsStayUnsupported() { + Target target = new Target(); + target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:describeStatement"); + target.setRoleArn("arn:aws:iam::000000000000:role/x"); + target.setInput("{\"Id\":\"stmt-1\"}"); + + assertThrows(UnsupportedOperationException.class, () -> invoke(target, "us-east-1")); + verifyNoInteractions(redshiftDataService); + } + @Test void universalSqsSendMessageSkipsAttributesWithoutDataType() { String queueUrl = "http://localhost:4566/000000000000/q.fifo"; diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerRedshiftDataDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerRedshiftDataDockerIntegrationTest.java new file mode 100644 index 0000000000..01e330a406 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerRedshiftDataDockerIntegrationTest.java @@ -0,0 +1,166 @@ +package io.github.hectorvent.floci.services.scheduler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.redshift.RedshiftService; +import io.github.hectorvent.floci.services.scheduler.model.Schedule; +import io.github.hectorvent.floci.services.sqs.SqsService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Runs a one-time schedule whose universal target is the Redshift Data API against a real + * PostgreSQL-backed cluster. Skipped when Docker is not available. + */ +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SchedulerRedshiftDataDockerIntegrationTest { + + private static final String REGION = "us-east-1"; + private static final String CLUSTER_ID = "it-sched-rsdata"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role"; + private static final String TARGET_ARN = "arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final DateTimeFormatter SCHEDULE_TIME = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC); + + @Inject + RedshiftService redshift; + + @Inject + ScheduleInvoker scheduleInvoker; + + @Inject + SchedulerService schedulerService; + + @Inject + SqsService sqsService; + + @Inject + EmulatorConfig config; + + private final List scheduleNames = new ArrayList<>(); + private final List testDispatchers = new ArrayList<>(); + private boolean clusterCreated; + + @BeforeAll + void createCluster() { + Assumptions.assumeTrue(dockerAvailable(), "Docker is required for this test"); + RestAssuredJsonUtils.configureAwsContentTypes(); + redshift.createCluster(CLUSTER_ID, "dc2.large", "admin", "Secret123"); + clusterCreated = true; + } + + @AfterAll + void deleteCluster() { + if (clusterCreated) { + redshift.deleteCluster(CLUSTER_ID); + } + } + + @AfterEach + void cleanUp() { + for (String scheduleName : scheduleNames) { + try { + schedulerService.deleteSchedule(scheduleName, null, REGION); + } catch (AwsException expected) { + if (!"ResourceNotFoundException".equals(expected.getErrorCode())) { + throw expected; + } + } + } + for (ScheduleDispatcher dispatcher : testDispatchers) { + dispatcher.onStop(null); + } + } + + @Test + void oneTimeScheduleRunsSqlOnAProvisionedCluster() { + String statementName = "sched-" + UUID.randomUUID().toString().substring(0, 8); + String scheduleName = "rsdata-at-" + UUID.randomUUID().toString().substring(0, 8); + String input = MAPPER.createObjectNode() + .put("ClusterIdentifier", CLUSTER_ID) + .put("Database", "dev") + .put("DbUser", "admin") + .put("StatementName", statementName) + .put("Sql", "select 1 as one") + .toString(); + Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS); + createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", input); + + dispatcherFor(scheduleName).tick(fireAt); + + String statementId = RestAssuredJsonUtils.awsAction("RedshiftData", "ListStatements", + "{\"StatementName\":\"" + statementName + "\"}") + .then().statusCode(200).extract().path("Statements[0].Id"); + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> + assertEquals("FINISHED", RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement", + "{\"Id\":\"" + statementId + "\"}") + .then().statusCode(200).extract().path("Status"))); + } + + private void createSchedule(String name, String expression, String input) { + ObjectNode request = MAPPER.createObjectNode(); + request.put("ScheduleExpression", expression); + request.putObject("FlexibleTimeWindow").put("Mode", "OFF"); + request.put("State", "ENABLED"); + ObjectNode target = request.putObject("Target"); + target.put("Arn", TARGET_ARN); + target.put("RoleArn", ROLE_ARN); + target.put("Input", input); + + given() + .contentType("application/json") + .body(request.toString()) + .when() + .post("/schedules/" + name) + .then() + .statusCode(200); + scheduleNames.add(name); + } + + private ScheduleDispatcher dispatcherFor(String scheduleName) { + Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION); + SchedulerService scopedSchedulerService = mock(SchedulerService.class); + when(scopedSchedulerService.listAllSchedules()).thenReturn(List.of(schedule)); + ScheduleDispatcher dispatcher = new ScheduleDispatcher( + scopedSchedulerService, scheduleInvoker, sqsService, config); + testDispatchers.add(dispatcher); + return dispatcher; + } + + private static boolean dockerAvailable() { + try { + Process p = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}") + .redirectErrorStream(true).start(); + return p.waitFor() == 0; + } catch (Exception ignored) { + // No docker binary or daemon: the test is skipped through the assumption, not failed. + return false; + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorRedshiftDataDisabledTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorRedshiftDataDisabledTest.java new file mode 100644 index 0000000000..4267ee4eab --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorRedshiftDataDisabledTest.java @@ -0,0 +1,86 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.ServiceRegistry; +import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataJsonHandler; +import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService; +import io.github.hectorvent.floci.services.stepfunctions.model.Execution; +import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +class AslExecutorRedshiftDataDisabledTest { + + @ParameterizedTest + @CsvSource({"false,true", "true,false"}) + void disabledServiceFailsTaskWithoutInvokingHandlerOrService(boolean parentEnabled, boolean childEnabled) { + runTask(parentEnabled, childEnabled, "", "FAILED", + "RedshiftData.ServiceNotAvailableException", "Service redshift-data is not enabled."); + } + + @ParameterizedTest + @ValueSource(strings = {".sync", ".sync:2"}) + void unsupportedSyncResourceFailsWithoutInvokingHandlerOrService(String suffix) { + runTask(true, true, suffix, "FAILED", "States.TaskFailed", + "Unsupported resource: arn:aws:states:::aws-sdk:redshiftdata:listStatements" + suffix); + } + + @Test + void bothEnabledServiceInvokesOrdinaryTask() { + runTask(true, true, "", "SUCCEEDED", null, null); + } + + private void runTask(boolean parentEnabled, boolean childEnabled, String suffix, + String status, String error, String cause) { + ObjectMapper objectMapper = new ObjectMapper(); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + ServiceRegistry serviceRegistry = mock(ServiceRegistry.class); + when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(parentEnabled && childEnabled); + RedshiftDataService service = mock(RedshiftDataService.class); + when(service.listStatements(any())).thenReturn(objectMapper.createObjectNode()); + RedshiftDataJsonHandler handler = spy(new RedshiftDataJsonHandler(service)); + AslExecutor executor = new AslExecutor(null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, objectMapper, + new JsonataEvaluator(objectMapper), null, config, null, null, handler, serviceRegistry); + + StateMachine stateMachine = new StateMachine(); + stateMachine.setStateMachineArn("arn:aws:states:us-east-1:000000000000:stateMachine:disabled-data"); + stateMachine.setDefinition(""" + {"StartAt":"List","States":{"List":{"Type":"Task", + "Resource":"arn:aws:states:::aws-sdk:redshiftdata:listStatements","End":true}}} + """.replace("listStatements\"", "listStatements" + suffix + "\"")); + Execution execution = new Execution(); + execution.setExecutionArn("arn:aws:states:us-east-1:000000000000:execution:disabled-data:run"); + execution.setStateMachineArn(stateMachine.getStateMachineArn()); + execution.setInput("{}"); + try { + executor.executeSync(stateMachine, execution, new ArrayList<>(), (updated, events) -> { }); + + assertEquals(status, execution.getStatus()); + assertEquals(error, execution.getError()); + assertEquals(cause, execution.getCause()); + if ("SUCCEEDED".equals(status)) { + verify(service).listStatements(any()); + assertEquals("{}", execution.getOutput()); + } else { + verifyNoInteractions(handler, service); + } + } finally { + executor.stop(); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java new file mode 100644 index 0000000000..b9dc0f5bf2 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataDockerIntegrationTest.java @@ -0,0 +1,156 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.redshift.RedshiftService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +@QuarkusTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class StepFunctionsRedshiftDataDockerIntegrationTest { + + private static final String CLUSTER_ID = "it-sfn-rsdata"; + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role"; + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + @Inject + RedshiftService redshift; + + private boolean clusterCreated; + + @BeforeAll + void createCluster() { + Assumptions.assumeTrue(dockerAvailable(), "Docker is required for this test"); + RestAssuredJsonUtils.configureAwsContentTypes(); + redshift.createCluster(CLUSTER_ID, "dc2.large", "admin", "Secret123"); + clusterCreated = true; + } + + @AfterAll + void deleteCluster() { + if (clusterCreated) { + redshift.deleteCluster(CLUSTER_ID); + } + } + + @Test + void executeThenDescribeStatementOnAProvisionedCluster() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeStatement", + "Arguments": {"ClusterIdentifier": "CLUSTER", "Database": "dev", + "DbUser": "admin", "Sql": "select 1 as one"}, + "Output": "{% $states.result %}", + "Next": "Describe" + }, + "Describe": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement", + "Arguments": {"Id": "{% $states.input.Id %}"}, + "End": true + } + } + } + """.replace("CLUSTER", CLUSTER_ID); + + JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition)); + assertEquals("FINISHED", result.path("Status").asText()); + assertEquals(1, result.path("ResultRows").asInt()); + } + + @Test + void failedStatementIsNotATaskFailure() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeStatement", + "Arguments": {"ClusterIdentifier": "CLUSTER", "Database": "dev", + "DbUser": "admin", "Sql": "select * from no_such_table"}, + "Output": "{% $states.result %}", + "Next": "Describe" + }, + "Describe": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement", + "Arguments": {"Id": "{% $states.input.Id %}"}, + "End": true + } + } + } + """.replace("CLUSTER", CLUSTER_ID); + + JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition)); + assertEquals("FAILED", result.path("Status").asText()); + } + + private static boolean dockerAvailable() { + try { + Process process = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}") + .redirectErrorStream(true).start(); + return process.waitFor() == 0; + } catch (Exception ignored) { + // An unavailable Docker daemon is an expected reason to skip this integration test. + return false; + } + } + + private static String succeedingOutput(String definition) { + String stateMachineArn = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine") + .contentType(CONTENT_TYPE) + .body("{\"name\":\"rsdata-docker-%s\",\"roleArn\":\"%s\",\"definition\":%s}" + .formatted(System.nanoTime(), ROLE_ARN, quote(definition))) + .when().post("/").then().statusCode(200).extract().path("stateMachineArn"); + String executionArn = given() + .header("X-Amz-Target", "AWSStepFunctions.StartExecution") + .contentType(CONTENT_TYPE) + .body("{\"stateMachineArn\":\"%s\",\"input\":\"{}\"}".formatted(stateMachineArn)) + .when().post("/").then().statusCode(200).extract().path("executionArn"); + for (int i = 0; i < 300; i++) { + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.DescribeExecution") + .contentType(CONTENT_TYPE) + .body("{\"executionArn\":\"%s\"}".formatted(executionArn)) + .when().post("/"); + String status = response.jsonPath().getString("status"); + if (!"RUNNING".equals(status)) { + assertEquals("SUCCEEDED", status, "cause: " + response.jsonPath().getString("cause")); + return response.jsonPath().getString("output"); + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + fail("Interrupted while waiting for " + executionArn); + } + } + fail("Execution did not complete: " + executionArn); + return null; + } + + private static String quote(String raw) { + return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataIntegrationTest.java new file mode 100644 index 0000000000..eb88e9cca7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsRedshiftDataIntegrationTest.java @@ -0,0 +1,332 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +@QuarkusTest +class StepFunctionsRedshiftDataIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String EXECUTE = "arn:aws:states:::aws-sdk:redshiftdata:executeStatement"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role"; + private static final String ISO_INSTANT = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"; + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + @InjectMock + RedshiftDataService redshiftDataService; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @BeforeEach + void stubService() { + when(redshiftDataService.executeStatement(any(JsonNode.class), anyString())).thenAnswer(invocation -> { + JsonNode request = invocation.getArgument(0); + if ("fail".equals(request.path("Sql").asText())) { + throw new AwsException("ValidationException", "synthetic failure", 400); + } + ObjectNode response = OBJECT_MAPPER.createObjectNode(); + response.put("Id", "stmt-1"); + response.put("CreatedAt", 1_700_000_000.5); + response.put("Database", request.path("Database").asText()); + response.put("HasResultSet", true); + return response; + }); + when(redshiftDataService.listStatements(any(JsonNode.class))).thenAnswer(invocation -> { + ObjectNode response = OBJECT_MAPPER.createObjectNode(); + ObjectNode statement = response.putArray("Statements").addObject(); + statement.put("Id", "stmt-1"); + statement.put("CreatedAt", 1_700_000_000.5); + statement.put("UpdatedAt", 1_700_000_001.0); + return response; + }); + } + + @Test + void executeStatementForwardsPascalCaseInputAndReturnsIsoCreatedAt() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "RESOURCE", + "Arguments": { + "ClusterIdentifier": "analytics", + "Database": "dev", + "DbUser": "admin", + "Sql": "select 1" + }, + "End": true + } + } + } + """.replace("RESOURCE", EXECUTE); + + JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}")); + assertEquals("stmt-1", result.path("Id").asText()); + assertEquals("dev", result.path("Database").asText()); + assertTrue(result.path("HasResultSet").asBoolean()); + assertTrue(result.path("CreatedAt").asText().matches(ISO_INSTANT), + "CreatedAt is not the SDK's ISO-8601 rendering: " + result.path("CreatedAt")); + + ArgumentCaptor request = ArgumentCaptor.forClass(JsonNode.class); + ArgumentCaptor region = ArgumentCaptor.forClass(String.class); + verify(redshiftDataService).executeStatement(request.capture(), region.capture()); + assertEquals("select 1", request.getValue().path("Sql").asText()); + assertEquals("analytics", request.getValue().path("ClusterIdentifier").asText()); + assertEquals("us-east-1", region.getValue()); + } + + @Test + void workgroupNameReachesTheService() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "RESOURCE", + "Arguments": {"WorkgroupName": "wg-1", "Database": "dev", "Sql": "select 1"}, + "End": true + } + } + } + """.replace("RESOURCE", EXECUTE); + + succeedingOutput(definition, "{}"); + + ArgumentCaptor request = ArgumentCaptor.forClass(JsonNode.class); + verify(redshiftDataService).executeStatement(request.capture(), anyString()); + assertEquals("wg-1", request.getValue().path("WorkgroupName").asText()); + assertTrue(request.getValue().path("ClusterIdentifier").isMissingNode()); + } + + @Test + void listStatementsRendersNestedTimestampsAsIso() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "List", + "States": { + "List": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:listStatements", + "Arguments": {}, + "End": true + } + } + } + """; + + ObjectNode source = (ObjectNode) OBJECT_MAPPER.readTree(""" + {"Statements":[{"Id":"stmt-1","CreatedAt":1700000000.5,"UpdatedAt":1700000001, + "SubStatements":[{"CreatedAt":1700000002.25,"ResultRows":7, + "Metadata":{"Items":[{"UpdatedAt":1700000003.125,"Sql":"select 1", + "Nested":[{"CreatedAt":"already textual","UpdatedAt":null,"Count":42}]}]}}]}], + "NextToken":"retained"} + """); + JsonNode original = source.deepCopy(); + when(redshiftDataService.listStatements(any(JsonNode.class))).thenReturn(source); + JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}")); + JsonNode statement = result.path("Statements").path(0); + assertEquals("2023-11-14T22:13:20.500Z", statement.path("CreatedAt").asText()); + assertEquals("2023-11-14T22:13:21.000Z", statement.path("UpdatedAt").asText()); + JsonNode subStatement = statement.path("SubStatements").path(0); + assertEquals("2023-11-14T22:13:22.250Z", subStatement.path("CreatedAt").asText()); + assertEquals(7, subStatement.path("ResultRows").asInt()); + JsonNode item = subStatement.path("Metadata").path("Items").path(0); + assertEquals("2023-11-14T22:13:23.125Z", item.path("UpdatedAt").asText()); + assertEquals("select 1", item.path("Sql").asText()); + assertEquals(original.path("Statements").path(0).path("SubStatements").path(0) + .path("Metadata").path("Items").path(0).path("Nested"), item.path("Nested")); + assertEquals("retained", result.path("NextToken").asText()); + assertEquals(original, source, "Rendering must not mutate the Data API response"); + assertTrue(source.path("Statements").path(0).path("CreatedAt").isNumber()); + assertTrue(source.path("Statements").path(0).path("SubStatements").path(0) + .path("Metadata").path("Items").path(0).path("UpdatedAt").isNumber()); + } + + @Test + void serviceFailureCanBeCaughtByItsSdkExceptionName() throws Exception { + String definition = """ + { + "QueryLanguage": "JSONata", + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "RESOURCE", + "Arguments": {"Sql": "fail"}, + "Catch": [{ + "ErrorEquals": ["RedshiftData.ValidationException"], + "Next": "Recovered", + "Output": {"Error": "{% $states.errorOutput.Error %}"} + }], + "End": true + }, + "Recovered": {"Type": "Pass", "End": true} + } + } + """.replace("RESOURCE", EXECUTE); + + JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}")); + assertEquals("RedshiftData.ValidationException", result.path("Error").asText()); + } + + @ParameterizedTest + @ValueSource(strings = {".sync", ".sync:2"}) + void unsupportedSyncResourceFailsBeforeExecutingSql(String suffix) { + String resource = EXECUTE + suffix; + String definition = """ + {"StartAt":"Run","States":{"Run":{"Type":"Task","Resource":"RESOURCE", + "Parameters":{"Sql":"select 1"},"End":true}}} + """.replace("RESOURCE", resource); + Response describe = terminalExecution(definition, "{}"); + assertEquals("FAILED", describe.jsonPath().getString("status")); + assertEquals("States.TaskFailed", describe.jsonPath().getString("error")); + assertEquals("Unsupported resource: " + resource, describe.jsonPath().getString("cause")); + verifyNoInteractions(redshiftDataService); + } + + @Test + void deprecatedExecuteSqlFailsWithValidationException() { + String definition = """ + { + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeSql", + "Parameters": {"Sql": "select 1"}, + "End": true + } + } + } + """; + + Response describe = terminalExecution(definition, "{}"); + assertEquals("FAILED", describe.jsonPath().getString("status")); + assertEquals("RedshiftData.ValidationException", describe.jsonPath().getString("error")); + verify(redshiftDataService, never()).executeStatement(any(JsonNode.class), anyString()); + } + + @ParameterizedTest + @ValueSource(strings = {"[1, 2]", "\"scalar\"", "123"}) + void nonObjectInputFailsWithValidationException(String input) { + String definition = """ + { + "StartAt": "Run", + "States": { + "Run": { + "Type": "Task", + "Resource": "RESOURCE", + "End": true + } + } + } + """.replace("RESOURCE", EXECUTE); + + Response describe = terminalExecution(definition, input); + assertEquals("FAILED", describe.jsonPath().getString("status")); + assertEquals("RedshiftData.ValidationException", describe.jsonPath().getString("error")); + assertEquals("The task input must be a JSON object.", describe.jsonPath().getString("cause")); + verifyNoInteractions(redshiftDataService); + } + + private static String succeedingOutput(String definition, String input) { + Response describe = terminalExecution(definition, input); + assertEquals("SUCCEEDED", describe.jsonPath().getString("status"), + "cause: " + describe.jsonPath().getString("cause")); + return describe.jsonPath().getString("output"); + } + + private static Response terminalExecution(String definition, String input) { + String stateMachineArn = createStateMachine(definition); + String executionArn = startExecution(stateMachineArn, input); + for (int i = 0; i < 150; i++) { + Response response = describeExecution(executionArn); + if (!"RUNNING".equals(response.jsonPath().getString("status"))) { + return response; + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + fail("Interrupted while waiting for execution " + executionArn); + } + } + fail("Execution did not complete within timeout: " + executionArn); + return null; + } + + private static String createStateMachine(String definition) { + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine") + .contentType(CONTENT_TYPE) + .body(""" + {"name": "redshiftdata-%s", "roleArn": "%s", "definition": %s} + """.formatted(System.nanoTime(), ROLE_ARN, quote(definition))) + .when().post("/"); + response.then().statusCode(200); + return response.jsonPath().getString("stateMachineArn"); + } + + private static String startExecution(String stateMachineArn, String input) { + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.StartExecution") + .contentType(CONTENT_TYPE) + .body(""" + {"stateMachineArn": "%s", "input": %s} + """.formatted(stateMachineArn, quote(input))) + .when().post("/"); + response.then().statusCode(200); + return response.jsonPath().getString("executionArn"); + } + + private static Response describeExecution(String executionArn) { + return given() + .header("X-Amz-Target", "AWSStepFunctions.DescribeExecution") + .contentType(CONTENT_TYPE) + .body("{\"executionArn\":\"%s\"}".formatted(executionArn)) + .when().post("/"); + } + + private static String quote(String raw) { + return "\"" + raw + .replace("\\", "\\\\") + .replace("\"", "\\\"") + .replace("\n", "\\n") + .replace("\r", "\\r") + .replace("\t", "\\t") + + "\""; + } +}