diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md index 2ed357ae21..e3aa704a8d 100644 --- a/docs/services/step-functions.md +++ b/docs/services/step-functions.md @@ -799,6 +799,23 @@ Service integration resources carry the partition of the state machine's region in every partition. State machine and execution ARNs follow the region they are created in. See [AWS Partitions](../configuration/partitions.md). +## API Gateway tasks + +`arn:...:states:::apigateway:invoke` supports regional, same-account REST APIs with `POST`, +`IAM_ROLE`, a named stage and resource path, and an object request body without null values. +Headers accept multiple string values and HTTP token names. AWS-reserved headers are rejected. +An explicit `Content-Length` must equal the serialized body size; the HTTP client writes that header. +Private endpoints, resource-policy authentication, query parameters and `AllowNullValues` remain unsupported. + +With IAM enforcement enabled, the execution role must trust Step Functions. The task uses its +`Credentials.RoleArn` when supplied, including assume-role authorization, identity policies, +permissions boundaries and SCPs. With enforcement disabled, missing roles and absent policies do not +block execution. Temporary signing credentials are revoked after each request. + +Successful responses preserve their headers and status. JSON response text is parsed regardless of +its content type, matching the live AWS integration; other text remains a string. Non-2xx responses +raise catchable `ApiGateway.` failures. + ## Configuration | Variable | Default | Description | diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvoker.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvoker.java new file mode 100644 index 0000000000..3199a9e4e0 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvoker.java @@ -0,0 +1,294 @@ +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.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.AwsEndpoints; +import io.github.hectorvent.floci.core.common.Digests; +import io.github.hectorvent.floci.core.common.ServicePrincipals; +import io.github.hectorvent.floci.core.common.auth.CredentialScope; +import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization; +import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator; +import io.github.hectorvent.floci.services.apigateway.ApiGatewayService; +import io.github.hectorvent.floci.services.apigateway.model.EndpointType; +import io.github.hectorvent.floci.services.apigateway.model.RestApi; +import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator; +import io.github.hectorvent.floci.services.iam.IamService; +import io.github.hectorvent.floci.services.iam.model.IamRole; +import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine; +import io.quarkus.vertx.http.HttpServer; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.core.Response; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.security.SecureRandom; +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.TreeMap; +import java.util.UUID; +import java.util.stream.Collectors; + +/** Regional, same-account REST API requests under the state machine's IAM execution role. */ +@ApplicationScoped +public class ApiGatewayTaskInvoker { + private static final SecureRandom RANDOM = new SecureRandom(); + private static final DateTimeFormatter AMZ_DATE = + DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC); + private final ApiGatewayService apiGateway; + private final IamService iam; + private final AssumeRolePolicyEvaluator trust; + private final LambdaTaskAuthorizer authorizer; + private final EmulatorConfig config; + private final ObjectMapper mapper; + private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(3)) + .followRedirects(HttpClient.Redirect.NEVER).build(); + private final HttpServer httpServer; + + @Inject + public ApiGatewayTaskInvoker(ApiGatewayService apiGateway, IamService iam, + AssumeRolePolicyEvaluator trust, LambdaTaskAuthorizer authorizer, EmulatorConfig config, + ObjectMapper mapper, HttpServer httpServer) { + this.apiGateway = apiGateway; + this.iam = iam; + this.trust = trust; + this.authorizer = authorizer; + this.config = config; + this.mapper = mapper; + this.httpServer = httpServer; + } + + @PreDestroy + void close() { + http.close(); + } + + public JsonNode invoke(JsonNode input, StateMachine machine, String credentialsRoleArn) throws Exception { + if (!"IAM_ROLE".equals(input.path("AuthType").asText()) + || !"POST".equals(input.path("Method").asText()) + || input.has("QueryParameters") || input.has("AllowNullValues")) { + throw unsupported("only REST POST with IAM_ROLE and JSON request body is supported"); + } + AwsArnUtils.Arn owner = AwsArnUtils.parse(machine.getStateMachineArn()); + String endpoint = input.path("ApiEndpoint").asText(); + int separator = endpoint.indexOf('.'); + String apiId = separator < 0 ? "" : endpoint.substring(0, separator); + if (!apiId.matches("[a-z0-9]+") + || !endpoint.equals(AwsEndpoints.executeApiHost(apiId, owner.region()))) { + throw unsupported("endpoint must be a regional execute-api hostname in the execution region"); + } + ApiGatewayService.ApiOwner apiOwner = apiGateway.findRestApiOwner(apiId) + .orElseThrow(() -> failure(404, "API does not exist")); + if (!owner.accountId().equals(apiOwner.accountId()) || !owner.region().equals(apiOwner.region())) { + throw failure(403, "API belongs to a different account or region"); + } + RestApi api = apiGateway.getRestApi(owner.region(), apiId); + if (api.getEndpointConfiguration() != null + && api.getEndpointConfiguration().getTypes().contains(EndpointType.PRIVATE)) { + throw unsupported("private APIs are not supported by this integration"); + } + if (api.getPolicy() != null && !api.getPolicy().isBlank()) { + throw unsupported("API resource policies are not supported by this integration"); + } + String stage = input.path("Stage").asText(); + String path = input.path("Path").asText(); + if (path.startsWith("/")) { + path = path.substring(1); + } + if (!stage.matches("[A-Za-z0-9_-]+") || path.isBlank() + || List.of(path.split("/", -1)).stream().anyMatch(part -> part.isEmpty() + || part.equals(".") || part.equals(".."))) { + throw unsupported("a named stage and non-empty resource path are required"); + } + String roleArn = credentialsRoleArn == null ? machine.getRoleArn() : credentialsRoleArn; + authorize(machine, owner, apiId, stage, path, roleArn, credentialsRoleArn); + AwsArnUtils.Arn roleId = AwsArnUtils.parse(roleArn); + String roleName = roleId.resource().substring(roleId.resource().lastIndexOf('/') + 1); + String principalId = iam.findRole(roleId.accountId(), roleName).map(IamRole::getRoleId).orElse(roleName); + TreeMap> headers = requestHeaders(input.path("Headers")); + headers.putIfAbsent("content-type", List.of("application/json")); + JsonNode body = input.path("RequestBody"); + if (!body.isObject() || containsNull(body)) { + throw unsupported("request body must be a JSON object without null values"); + } + byte[] payload = mapper.writeValueAsBytes(body); + List length = headers.remove("content-length"); + if (length != null && !length.equals(List.of(Integer.toString(payload.length)))) { + throw unsupported("Content-Length must match the serialized request body"); + } + URI listener = httpServer.getLocalBaseUri(); + if (listener == null) { + throw new AslExecutor.FailStateException("ApiGateway.IllegalStateException", "API listener is not ready"); + } + String encodedPath = List.of(path.split("/", -1)).stream() + .map(SigV4Canonicalization::uriEncode).collect(Collectors.joining("/")); + URI uri = listener.resolve("/restapis/" + apiId + "/" + stage + "/_user_request_/" + encodedPath); + String accessKey = accessKey(); + String secret = randomSecret(30); + String token = randomSecret(48); + String sessionName = "StepFunctions-" + UUID.randomUUID(); + iam.registerSession(accessKey, secret, token, roleArn, Instant.now().plusSeconds(120), + null, roleId.accountId(), sessionName, principalId + ":" + sessionName); + try { + sign(headers, uri, payload, owner.region(), accessKey, secret, token); + HttpRequest.Builder request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(60)) + .POST(HttpRequest.BodyPublishers.ofByteArray(payload)); + headers.forEach((name, value) -> { + if (!"host".equals(name)) { + value.forEach(item -> request.header(name, item)); + } + }); + HttpResponse response = http.send(request.build(), HttpResponse.BodyHandlers.ofByteArray()); + String text = new String(response.body(), StandardCharsets.UTF_8); + if (response.statusCode() < 200 || response.statusCode() >= 300) { + throw failure(response.statusCode(), text); + } + ObjectNode result = mapper.createObjectNode(); + result.put("StatusCode", response.statusCode()); + Response.Status status = Response.Status.fromStatusCode(response.statusCode()); + result.put("StatusText", status == null ? "" : status.getReasonPhrase()); + result.set("Headers", mapper.valueToTree(response.headers().map())); + try { + result.set("ResponseBody", mapper.readTree(text)); + } catch (IOException ignored) { + result.put("ResponseBody", text); + } + return result; + } catch (IOException error) { + throw new AslExecutor.FailStateException("ApiGateway." + error.getClass().getSimpleName(), + "API request could not complete"); + } finally { + iam.unregisterSession(roleId.accountId(), accessKey); + } + } + + private void authorize(StateMachine machine, AwsArnUtils.Arn owner, + String apiId, String stage, String path, String roleArn, String credentialsRoleArn) { + if (!config.services().iam().enforcementEnabled()) { + return; + } + AwsArnUtils.Arn executionRole = AwsArnUtils.parse(machine.getRoleArn()); + if (!"iam".equals(executionRole.service()) || !executionRole.region().isEmpty() + || !executionRole.resource().startsWith("role/") + || !executionRole.accountId().equals(owner.accountId()) + || !executionRole.partition().equals(owner.partition())) { + throw failure(403, "Execution role must belong to the state machine account"); + } + String name = executionRole.resource().substring(executionRole.resource().lastIndexOf('/') + 1); + IamRole role = iam.findRole(executionRole.accountId(), name) + .filter(found -> IamService.roleArnMatches(machine.getRoleArn(), found)) + .orElseThrow(() -> failure(403, "Execution role does not exist")); + if (!trust.allowsService(role.getAssumeRolePolicyDocument(), ServicePrincipals.of("states"), + machine.getStateMachineArn(), owner.accountId())) { + throw failure(403, "Execution role does not trust Step Functions"); + } + if (credentialsRoleArn != null) { + authorizer.assumeRefusal(machine.getRoleArn(), credentialsRoleArn, machine.getStateMachineArn()) + .ifPresent(reason -> { throw new AslExecutor.FailStateException("States.Permissions", reason); }); + } + String resource = new AwsArnUtils.Arn(owner.partition(), "execute-api", owner.region(), owner.accountId(), + apiId + "/" + stage + "/POST/" + path).toString(); + authorizer.refusal(roleArn, "execute-api:Invoke", resource) + .ifPresent(reason -> { throw failure(403, reason); }); + } + + private static boolean containsNull(JsonNode value) { + if (value.isNull()) { + return true; + } + for (JsonNode child : value) { + if (containsNull(child)) { + return true; + } + } + return false; + } + + private static TreeMap> requestHeaders(JsonNode input) { + TreeMap> result = new TreeMap<>(); + if (input.isMissingNode()) { + return result; + } + if (!input.isObject()) { + throw unsupported("headers must be an object of string arrays"); + } + input.properties().forEach(entry -> { + String name = entry.getKey().toLowerCase(Locale.ROOT); + JsonNode values = entry.getValue(); + if (!entry.getKey().matches("[!#$%&'*+.^_`|~0-9A-Za-z-]+") || name.startsWith("x-amz") || name.startsWith("x-forwarded") + || List.of("authorization", "connection", "content-md5", "expect", "host", "max-forwards", + "proxy-authenticate", "server", "te", "transfer-encoding", "trailer", "upgrade", "via", + "www-authenticate").contains(name) + || !values.isArray() || values.isEmpty() || result.containsKey(name)) { + throw unsupported("header name or value is not supported"); + } + List strings = new ArrayList<>(); + for (JsonNode value : values) { + if (!value.isTextual() || value.asText().chars().anyMatch(ch -> ch < 32 && ch != '\t' || ch == 127 || ch > 255)) { + throw unsupported("header name or value is not supported"); + } + strings.add(value.asText()); + } + result.put(name, strings); + }); + return result; + } + + private static void sign(TreeMap> headers, URI uri, byte[] payload, String region, + String accessKey, String secret, String token) throws Exception { + String stamp = AMZ_DATE.format(Instant.now()); + headers.put("host", List.of(uri.getAuthority())); + headers.put("x-amz-date", List.of(stamp)); + headers.put("x-amz-security-token", List.of(token)); + String signed = String.join(";", headers.keySet()); + CredentialScope scope = new CredentialScope(accessKey, stamp.substring(0, 8), region, "execute-api"); + String canonical = "POST\n" + uri.getRawPath() + "\n\n" + + SigV4Canonicalization.canonicalHeaders(signed, name -> headers.get(name).stream() + .map(SigV4RequestValidator::normalizeHeaderValue).collect(Collectors.joining(","))) + "\n" + signed + "\n" + + Digests.sha256Hex(payload); + String toSign = "AWS4-HMAC-SHA256\n" + stamp + "\n" + scope.credentialScope() + "\n" + + Digests.sha256Hex(canonical); + String signature = SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256( + SigV4RequestValidator.deriveSigningKey(secret, scope.date(), region, "execute-api"), toSign)); + headers.put("authorization", List.of("AWS4-HMAC-SHA256 Credential=" + accessKey + "/" + scope.credentialScope() + + ", SignedHeaders=" + signed + ", Signature=" + signature)); + } + + private static String accessKey() { + String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + StringBuilder value = new StringBuilder("ASIA"); + for (int index = 0; index < 16; index++) { + value.append(alphabet.charAt(RANDOM.nextInt(alphabet.length()))); + } + return value.toString(); + } + + private static String randomSecret(int size) { + byte[] bytes = new byte[size]; + RANDOM.nextBytes(bytes); + return Base64.getEncoder().withoutPadding().encodeToString(bytes); + } + + private static AslExecutor.FailStateException failure(int status, String cause) { + return new AslExecutor.FailStateException("ApiGateway." + status, cause); + } + + private static AslExecutor.FailStateException unsupported(String feature) { + return new AslExecutor.FailStateException("States.TaskFailed", "Unsupported apigateway:invoke feature: " + feature); + } +} 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 73be17bada..2659bb66b0 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 @@ -266,6 +266,7 @@ public class AslExecutor { private final JsonataEvaluator jsonataEvaluator; private final Instance sfnService; private final WebClient webClient; + private final ApiGatewayTaskInvoker apiGatewayTaskInvoker; private final EmulatorConfig config; private final CustomResourceLiveness customResourceLiveness; private final Clock clock; @@ -294,14 +295,15 @@ public class AslExecutor { ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator, Instance sfnService, EmulatorConfig config, Vertx vertx, CustomResourceLiveness customResourceLiveness, RedshiftDataJsonHandler redshiftDataHandler, - ServiceRegistry serviceRegistry, LambdaTaskAuthorizer lambdaTaskAuthorizer) { + ServiceRegistry serviceRegistry, LambdaTaskAuthorizer lambdaTaskAuthorizer, + ApiGatewayTaskInvoker apiGatewayTaskInvoker) { 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, redshiftDataHandler, serviceRegistry, - lambdaTaskAuthorizer, null, null); + lambdaTaskAuthorizer, null, null, apiGatewayTaskInvoker); } AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver, @@ -318,7 +320,8 @@ public class AslExecutor { Clock clock, Sleeper sleeper, Integer maxWaitSecondsOverride, RedshiftDataJsonHandler redshiftDataHandler, ServiceRegistry serviceRegistry, LambdaTaskAuthorizer lambdaTaskAuthorizer, Duration expressMaxExecutionTimeOverride, - LongSupplier nanoTimeOverride) { + LongSupplier nanoTimeOverride, ApiGatewayTaskInvoker apiGatewayTaskInvoker) { + this.apiGatewayTaskInvoker = apiGatewayTaskInvoker; this.serviceRegistry = serviceRegistry; this.lambdaTaskAuthorizer = lambdaTaskAuthorizer; this.customResourceLiveness = customResourceLiveness; @@ -380,7 +383,7 @@ public class AslExecutor { ec2Service, s3Service, ecsService, ecsJsonHandler, eventBridgeHandler, schedulerService, schedulerController, rdsDataService, objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness, - Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null); + Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null, null); } // Kept for tests that do not exercise the Redshift Data API integration. @@ -401,7 +404,7 @@ public class AslExecutor { ec2Service, s3Service, ecsService, ecsJsonHandler, eventBridgeHandler, schedulerService, schedulerController, rdsDataService, objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness, - clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null); + clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null); } AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore, @@ -1324,6 +1327,14 @@ public class AslExecutor { return invokeNonIntegrationResource(resource, input, taskToken); } + if (integration.is("apigateway", "invoke") && integration.suffix().isEmpty()) { + try { + return apiGatewayTaskInvoker.invoke(input, sm, credentialsRoleArn); + } catch (AwsException error) { + throw new FailStateException("ApiGateway." + error.getHttpStatus(), error.getMessage()); + } + } + // DynamoDB optimized integrations (4 actions) if (integration.isOptimizedService("dynamodb")) { String operation = integration.api(); diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/LambdaTaskAuthorizer.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/LambdaTaskAuthorizer.java index b9e0e4c4bb..9cf7f27744 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/LambdaTaskAuthorizer.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/LambdaTaskAuthorizer.java @@ -67,12 +67,17 @@ public class LambdaTaskAuthorizer { * @param functionArn the function, version or alias the task invokes, qualified as it names it */ public Optional refusal(String roleArn, String functionArn) { - AwsArnUtils.Arn function = AwsArnUtils.parse(functionArn); + return refusal(roleArn, INVOKE, functionArn); + } + + /** Applies the task role's identity policies, boundary and SCPs to the requested action. */ + public Optional refusal(String roleArn, String action, String resourceArn) { + AwsArnUtils.Arn resource = AwsArnUtils.parse(resourceArn); Optional invokingRole = role(roleArn); if (invokingRole.isEmpty()) { - LOG.infov("Role {0} names no existing role, so it cannot invoke {1}", roleArn, functionArn); - return Optional.of(denied(roleArn, INVOKE, functionArn, "because no identity-based policy allows the " - + INVOKE + " action")); + LOG.infov("Role {0} names no existing role, so it cannot invoke {1}", roleArn, resourceArn); + return Optional.of(denied(roleArn, action, resourceArn, "because no identity-based policy allows the " + + action + " action")); } // The role is evaluated as itself, under the ARN IAM holds for it, whichever form names it. String invokingRoleArn = invokingRole.get().getArn(); @@ -80,26 +85,27 @@ public class LambdaTaskAuthorizer { AwsArnUtils.Arn roleId = AwsArnUtils.parse(invokingRoleArn); List> scpLevels = scpLevels(roleId.accountId()); - List policies = functionPolicies.getResourcePolicies("lambda", functionArn); - String ownerAccount = policies.isEmpty() ? function.accountId() : policies.getFirst().ownerAccountId(); + List policies = "lambda".equals(resource.service()) + ? functionPolicies.getResourcePolicies("lambda", resourceArn) : List.of(); + String ownerAccount = policies.isEmpty() ? resource.accountId() : policies.getFirst().ownerAccountId(); List documents = policies.stream() .map(ResourcePolicy::policyDocument) .filter(document -> document != null && !document.isBlank()) .toList(); Map> context = IamConditionContextResolver.withGlobalContext( - Map.of("aws:PrincipalArn", List.of(invokingRoleArn)), functionArn, function.region(), + Map.of("aws:PrincipalArn", List.of(invokingRoleArn)), resourceArn, resource.region(), roleId.accountId(), ownerAccount); RequestPrincipal session = RequestPrincipal.roleSession(sessionArn(invokingRoleArn), invokingRoleArn); ResourcePolicyDecision functionPolicy = evaluator.evaluateResourcePolicyFor( - documents.isEmpty() ? null : documents, session, INVOKE, functionArn, context); + documents.isEmpty() ? null : documents, session, action, resourceArn, context); ResourceAccountRelationship relationship = relationship(roleId.accountId(), ownerAccount); Decision decision = evaluator.evaluateResolvedResourcePolicy( - role.withScpLevels(scpLevels), functionPolicy, relationship, INVOKE, functionArn, context); + role.withScpLevels(scpLevels), functionPolicy, relationship, action, resourceArn, context); if (decision == Decision.ALLOW) { return Optional.empty(); } - return Optional.of(denied(invokingRoleArn, INVOKE, functionArn, reason(role, scpLevels, INVOKE, functionArn, + return Optional.of(denied(invokingRoleArn, action, resourceArn, reason(role, scpLevels, action, resourceArn, context, relationship, ResourceGrant.of(functionPolicy)))); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ed371c2e81..5626b6cd4f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -147,6 +147,7 @@ quarkus: - --initialize-at-run-time=io.github.hectorvent.floci.services.redshift.proxy.RedshiftRoleAccess - --initialize-at-run-time=io.github.hectorvent.floci.services.redshift.spectrum.SpectrumMaterializer - --initialize-at-run-time=io.github.hectorvent.floci.services.ecs.exec.EcsExecSessionRegistry + - --initialize-at-run-time=io.github.hectorvent.floci.services.stepfunctions.ApiGatewayTaskInvoker # snappy-java (Firehose Snappy/HADOOP_SNAPPY delivery) extracts and loads a # bundled JNI library from the static initializers of these two classes. - --initialize-at-run-time=org.xerial.snappy.Snappy diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvokerTest.java new file mode 100644 index 0000000000..f5f63e50b5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/ApiGatewayTaskInvokerTest.java @@ -0,0 +1,335 @@ +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 com.sun.net.httpserver.HttpServer; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.Digests; +import io.github.hectorvent.floci.core.common.auth.CredentialScope; +import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader; +import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization; +import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator; +import io.github.hectorvent.floci.services.apigateway.ApiGatewayService; +import io.github.hectorvent.floci.services.apigateway.model.RestApi; +import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator; +import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator; +import io.github.hectorvent.floci.services.iam.IamService; +import io.github.hectorvent.floci.services.iam.ScpProvider; +import io.github.hectorvent.floci.services.iam.model.CallerContext; +import io.github.hectorvent.floci.services.iam.model.IamRole; +import io.github.hectorvent.floci.services.lambda.LambdaResourcePolicyProvider; +import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine; +import jakarta.enterprise.inject.Instance; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.net.InetSocketAddress; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +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.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class ApiGatewayTaskInvokerTest { + private static final String ACCOUNT = "111111111111"; + private static final String ROLE = "arn:aws:iam::111111111111:role/OwnedRole"; + private static final String TRUST = "{\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"states.amazonaws.com\"}," + + "\"Action\":\"sts:AssumeRole\"}]}"; + private static final String ALLOW = "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"execute-api:Invoke\"," + + "\"Resource\":\"arn:aws:execute-api:eu-west-1:111111111111:owned/gamma/POST/create-workflow-execution\"}]}"; + private final ObjectMapper mapper = new ObjectMapper(); + private final IamService iam = mock(IamService.class); + private final ApiGatewayService api = mock(ApiGatewayService.class); + private final AtomicReference credentials = new AtomicReference<>(); + private final AtomicReference received = new AtomicReference<>(); + private final AtomicReference serverFailure = new AtomicReference<>(); + private final AtomicInteger calls = new AtomicInteger(); + private final AtomicInteger status = new AtomicInteger(200); + private ApiGatewayTaskInvoker invoker; + // Quarkus and the JDK test server share the HttpServer simple name. + private final io.quarkus.vertx.http.HttpServer listener = mock(io.quarkus.vertx.http.HttpServer.class); + private HttpServer server; + private StateMachine machine; + private ObjectNode request; + private IamRole role; + private final EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + private final Instance scps = mock(Instance.class); + private final AtomicReference> receivedHeaders = new AtomicReference<>(); + + @BeforeEach + void setup() throws Exception { + IamPolicyEvaluator policies = new IamPolicyEvaluator(mapper); + AssumeRolePolicyEvaluator trust = new AssumeRolePolicyEvaluator(mapper, policies); + LambdaTaskAuthorizer authorizer = new LambdaTaskAuthorizer(iam, policies, trust, + mock(LambdaResourcePolicyProvider.class), scps); + when(config.services().iam().enforcementEnabled()).thenReturn(true); + invoker = new ApiGatewayTaskInvoker(api, iam, trust, authorizer, config, mapper, listener); + machine = new StateMachine(); + machine.setStateMachineArn("arn:aws:states:eu-west-1:" + ACCOUNT + ":stateMachine:Owned"); + machine.setRoleArn(ROLE); + role = new IamRole("AROAOWNED", "OwnedRole", "/", ROLE, TRUST); + when(iam.findRole(ACCOUNT, "OwnedRole")).thenReturn(Optional.of(role)); + when(iam.resolvePrincipalContext(ROLE)).thenReturn(CallerContext.of(List.of(ALLOW))); + when(api.findRestApiOwner("owned")).thenReturn(Optional.of(new ApiGatewayService.ApiOwner(ACCOUNT, "eu-west-1"))); + when(api.getRestApi("eu-west-1", "owned")).thenReturn(new RestApi()); + doAnswer(call -> { + credentials.set(new String[]{call.getArgument(0), call.getArgument(1), call.getArgument(2)}); + return null; + }).when(iam).registerSession(anyString(), anyString(), anyString(), eq(ROLE), any(Instant.class), isNull(), + eq(ACCOUNT), anyString(), anyString()); + request = (ObjectNode) mapper.readTree(""" + {"ApiEndpoint":"owned.execute-api.eu-west-1.amazonaws.com","Method":"POST","Stage":"gamma", + "Path":"create-workflow-execution","AuthType":"IAM_ROLE", + "Headers":{"x-api-key":["owned-key"]},"RequestBody":{"stack":"owned","value":42}} + """); + startServer("127.0.0.1"); + } + + private void startServer(String host) throws Exception { + server = HttpServer.create(new InetSocketAddress(host, 0), 0); + server.createContext("/", exchange -> { + calls.incrementAndGet(); + try { + byte[] body = exchange.getRequestBody().readAllBytes(); + received.set(mapper.readTree(body)); + receivedHeaders.set(exchange.getRequestHeaders().get("x_test")); + assertEquals("/restapis/owned/gamma/_user_request_/create-workflow-execution", exchange.getRequestURI().getPath()); + assertEquals("owned-key", exchange.getRequestHeaders().getFirst("x-api-key")); + SigV4AuthorizationHeader auth = SigV4AuthorizationHeader.parse(exchange.getRequestHeaders().getFirst("Authorization")); + CredentialScope scope = CredentialScope.parse(auth.credential()); + String[] issued = credentials.get(); + assertEquals(issued[0], scope.accessKeyId()); + assertEquals(issued[2], exchange.getRequestHeaders().getFirst("x-amz-security-token")); + assertTrue(SigV4Canonicalization.signatureMatches("POST", exchange.getRequestURI().getRawPath(), "", + SigV4Canonicalization.canonicalHeaders(auth.signedHeaders(), name -> exchange.getRequestHeaders().get(name).stream() + .map(SigV4RequestValidator::normalizeHeaderValue).collect(Collectors.joining(","))), + auth.signedHeaders(), Digests.sha256Hex(body), + exchange.getRequestHeaders().getFirst("x-amz-date"), scope, issued[1], auth.signature())); + } catch (Throwable error) { + serverFailure.set(error); + } + byte[] body = "{\"workflow_id\":\"owned\"}".getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().set("Content-Type", "text/plain"); + exchange.sendResponseHeaders(status.get(), body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.start(); + when(listener.getLocalBaseUri()).thenReturn(new URI("http", null, host, server.getAddress().getPort(), "/", null, null)); + } + + @AfterEach + void cleanup() { + if (invoker != null) { + invoker.close(); + } + if (server != null) { + server.stop(0); + } + } + + @Test + void signedSelectedRequestUsesOwnedRoleAndRevokesSession() throws Exception { + JsonNode result = invoker.invoke(request, machine, null); + assertEquals(200, result.path("StatusCode").asInt()); + assertEquals("OK", result.path("StatusText").asText()); + assertEquals("owned", result.path("ResponseBody").path("workflow_id").asText()); + assertEquals(request.path("RequestBody"), received.get()); + assertEquals(null, serverFailure.get()); + verify(iam).unregisterSession(ACCOUNT, credentials.get()[0]); + } + + @Test + void httpErrorRemainsCatchableAndRevokesSession() { + status.set(500); + AslExecutor.FailStateException error = assertThrows(AslExecutor.FailStateException.class, + () -> invoker.invoke(request, machine, null)); + assertEquals("ApiGateway.500", error.error); + verify(iam).unregisterSession(ACCOUNT, credentials.get()[0]); + assertEquals(null, serverFailure.get()); + } + + @Test + void transportFailureUsesExceptionCodeAndRevokesSession() { + server.stop(0); + server = null; + AslExecutor.FailStateException error = assertThrows(AslExecutor.FailStateException.class, + () -> invoker.invoke(request, machine, null)); + assertEquals("ApiGateway.ConnectException", error.error); + verify(iam).unregisterSession(ACCOUNT, credentials.get()[0]); + } + + @Test + void deniedRoleCannotSendRequestOrMintCredential() { + when(iam.resolvePrincipalContext(ROLE)).thenReturn(CallerContext.of(List.of())); + denied("ApiGateway.403"); + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + } + + @Test + void wrongTrustAndForeignApiAreRefused() { + role.setAssumeRolePolicyDocument(TRUST.replace("states.amazonaws.com", "lambda.amazonaws.com")); + denied("ApiGateway.403"); + role.setAssumeRolePolicyDocument(TRUST); + when(api.findRestApiOwner("owned")).thenReturn(Optional.of(new ApiGatewayService.ApiOwner("222222222222", "eu-west-1"))); + denied("ApiGateway.403"); + assertEquals(0, calls.get()); + } + + @Test + void unsupportedEndpointsAndCallerSigningHeadersNeverReachTransport() { + request.put("ApiEndpoint", "owned.execute-api.us-east-1.amazonaws.com"); + denied("States.TaskFailed"); + request.put("ApiEndpoint", "127.0.0.1"); + denied("States.TaskFailed"); + request.put("ApiEndpoint", "owned.execute-api.eu-west-1.amazonaws.com"); + ((ObjectNode) request.path("Headers")).putArray("Authorization").add("forged"); + denied("States.TaskFailed"); + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + verify(iam, never()).unregisterSession(anyString(), anyString()); + } + + @Test + void optionalRequestAndResourcePolicyFeaturesRefuseBeforeTransport() { + request.putObject("QueryParameters").putArray("key").add("value"); + denied("States.TaskFailed"); + request.remove("QueryParameters"); + request.put("AllowNullValues", true); + denied("States.TaskFailed"); + request.remove("AllowNullValues"); + ((ObjectNode) request.path("RequestBody")).putNull("optional"); + denied("States.TaskFailed"); + ((ObjectNode) request.path("RequestBody")).remove("optional"); + RestApi resourcePolicyApi = new RestApi(); + resourcePolicyApi.setPolicy("{}"); + when(api.getRestApi("eu-west-1", "owned")).thenReturn(resourcePolicyApi); + denied("States.TaskFailed"); + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + } + + @Test + void enforcementOffDoesNotRequireAnExistingExecutionRole() throws Exception { + when(config.services().iam().enforcementEnabled()).thenReturn(false); + when(iam.findRole(ACCOUNT, "OwnedRole")).thenReturn(Optional.empty()); + assertEquals(200, invoker.invoke(request, machine, null).path("StatusCode").asInt()); + assertEquals(null, serverFailure.get()); + } + + @Test + void multipleValuesAndTokenHeaderNamesAreSigned() throws Exception { + ((ObjectNode) request.path("Headers")).putArray("x_test").add("one").add("two"); + assertEquals(200, invoker.invoke(request, machine, null).path("StatusCode").asInt()); + assertEquals(List.of("one", "two"), receivedHeaders.get()); + assertEquals(null, serverFailure.get()); + } + + @Test + void missingRequiredMembersAndInvalidEnumsNeverMintCredentials() { + ObjectNode original = request.deepCopy(); + for (String member : List.of("ApiEndpoint", "Method", "Stage", "Path", "AuthType", "RequestBody")) { + request = original.deepCopy(); + request.remove(member); + denied("States.TaskFailed"); + } + for (String member : List.of("Method", "AuthType")) { + request = original.deepCopy(); + request.put(member, "invalid"); + denied("States.TaskFailed"); + } + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + } + + @Test + void reservedAndInvalidHeadersNeverReachTransport() { + for (String name : List.of("Expect", "Upgrade", "Content-MD5", "TE", "X-Amzn-Test", "X-Forwarded-For", + "bad header")) { + request.putObject("Headers").putArray(name).add("value"); + denied("States.TaskFailed"); + } + request.putObject("Headers").putArray("x-test").add("bad\r\nvalue"); + denied("States.TaskFailed"); + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + } + + @Test + void taskCredentialsSelectTheInvokingRole() throws Exception { + String selected = ROLE.replace("OwnedRole", "TaskRole"); + IamRole taskRole = new IamRole("AROATASK", "TaskRole", "/", selected, + "{\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"" + ROLE + + "\"},\"Action\":\"sts:AssumeRole\"}]}"); + when(iam.findRole(ACCOUNT, "TaskRole")).thenReturn(Optional.of(taskRole)); + when(iam.resolvePrincipalContext(selected)).thenReturn(CallerContext.of(List.of(ALLOW))); + when(iam.resolvePrincipalContext(ROLE)).thenReturn(CallerContext.of(List.of())); + doAnswer(call -> { + credentials.set(new String[]{call.getArgument(0), call.getArgument(1), call.getArgument(2)}); + return null; + }).when(iam).registerSession(anyString(), anyString(), anyString(), eq(selected), any(Instant.class), isNull(), + eq(ACCOUNT), anyString(), anyString()); + assertEquals(200, invoker.invoke(request, machine, selected).path("StatusCode").asInt()); + assertEquals(null, serverFailure.get()); + verify(iam).unregisterSession(ACCOUNT, credentials.get()[0]); + when(iam.resolvePrincipalContext(selected)).thenReturn(CallerContext.of(List.of())); + AslExecutor.FailStateException error = assertThrows(AslExecutor.FailStateException.class, + () -> invoker.invoke(request, machine, selected)); + assertEquals("ApiGateway.403", error.error); + assertEquals(1, calls.get()); + } + + @Test + void scpDenialPreventsIssuingCredentials() { + ScpProvider provider = mock(ScpProvider.class); + when(scps.isResolvable()).thenReturn(true); + when(scps.get()).thenReturn(provider); + when(provider.effectiveScpLevels(ACCOUNT)).thenReturn(List.of(List.of( + "{\"Statement\":[{\"Effect\":\"Deny\",\"Action\":\"execute-api:Invoke\",\"Resource\":\"*\"}]}"))); + denied("ApiGateway.403"); + assertEquals(0, calls.get()); + assertEquals(null, credentials.get()); + } + + @Test + void ipv6OnlyListenerReceivesSignedRequest() throws Exception { + server.stop(0); + startServer("::1"); + assertEquals(200, invoker.invoke(request, machine, null).path("StatusCode").asInt()); + assertEquals(null, serverFailure.get()); + } + + @Test + void matchingContentLengthUsesTheTransportGeneratedHeader() throws Exception { + int length = mapper.writeValueAsBytes(request.path("RequestBody")).length; + ((ObjectNode) request.path("Headers")).putArray("Content-Length").add(Integer.toString(length)); + assertEquals(200, invoker.invoke(request, machine, null).path("StatusCode").asInt()); + assertEquals(null, serverFailure.get()); + } + + private void denied(String expected) { + AslExecutor.FailStateException error = assertThrows(AslExecutor.FailStateException.class, + () -> invoker.invoke(request, machine, null)); + assertEquals(expected, error.error); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorFixture.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorFixture.java index 1d16bda46b..9a37025279 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorFixture.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorFixture.java @@ -245,7 +245,7 @@ final class AslExecutorFixture { jsonataEvaluator != null ? jsonataEvaluator : new JsonataEvaluator(objectMapper), sfnService, config, vertx, customResourceLiveness, clock, sleeper, maxWaitSeconds, redshiftDataHandler, serviceRegistry, lambdaTaskAuthorizer, expressMaxExecutionTime, - nanoTime); + nanoTime, null); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIamIntegrationTest.java new file mode 100644 index 0000000000..7ab05aaa3b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIamIntegrationTest.java @@ -0,0 +1,10 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import io.github.hectorvent.floci.testing.IamEnforcementProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; + +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class StepFunctionsApiGatewayIamIntegrationTest extends StepFunctionsApiGatewayIntegrationTest { +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIntegrationTest.java new file mode 100644 index 0000000000..940baf8d1a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsApiGatewayIntegrationTest.java @@ -0,0 +1,177 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +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.services.iam.IamService; +import io.github.hectorvent.floci.services.iam.model.IamRole; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.github.hectorvent.floci.testutil.AwsRequestSigner; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import io.restassured.response.Response; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Normal HTTP listener, real IAM storage/policies and actual execute-api SigV4 authorization. */ +@QuarkusTest +class StepFunctionsApiGatewayIntegrationTest { + @Inject + IamService iam; + @Inject + EmulatorConfig config; + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @BeforeAll + static void contentTypes() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void roleAuthorizedPostRespectsConfiguredIamEnforcement() throws Exception { + String name = "sfn-api-" + UUID.randomUUID(); + String api = null; + String machine = null; + boolean policy = false; + IamRole taskRole = null; + boolean taskPolicy = false; + boolean assumePolicy = false; + IamRole role = iam.createRole(name, "/", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow", + "Principal":{"Service":"states.amazonaws.com"},"Action":"sts:AssumeRole"}]} + """, null, 3600, Map.of()); + try { + api = given().contentType(ContentType.JSON).body(Map.of("name", name)).post("/restapis") + .then().statusCode(201).extract().path("id"); + String root = given().get("/restapis/" + api + "/resources") + .then().statusCode(200).extract().path("item[0].id"); + String resource = given().contentType(ContentType.JSON).body(Map.of("pathPart", "invoke")) + .post("/restapis/" + api + "/resources/" + root) + .then().statusCode(201).extract().path("id"); + String method = "/restapis/" + api + "/resources/" + resource + "/methods/POST"; + given().contentType(ContentType.JSON).body(Map.of("authorizationType", "AWS_IAM")) + .put(method).then().statusCode(201); + given().contentType(ContentType.JSON).body(Map.of("responseParameters", Map.of())) + .put(method + "/responses/200").then().statusCode(201); + given().contentType(ContentType.JSON).body(Map.of("type", "MOCK", "requestTemplates", + Map.of("application/json", "{\"statusCode\":200}"))) + .put(method + "/integration").then().statusCode(201); + given().contentType(ContentType.JSON).body(Map.of("selectionPattern", "", "responseTemplates", + Map.of("application/json", "{\"owned\":true}"))) + .put(method + "/integration/responses/200").then().statusCode(201); + String deployment = given().contentType(ContentType.JSON).body(Map.of("description", "owned")) + .post("/restapis/" + api + "/deployments").then().statusCode(201).extract().path("id"); + given().contentType(ContentType.JSON).body(Map.of("stageName", "gamma", "deploymentId", deployment)) + .post("/restapis/" + api + "/stages").then().statusCode(201); + iam.putRolePolicy(name, "invoke", MAPPER.writeValueAsString(Map.of("Version", "2012-10-17", + "Statement", List.of(Map.of("Effect", "Allow", "Action", "execute-api:Invoke", "Resource", + "arn:aws:execute-api:us-east-1:000000000000:" + api + "/gamma/POST/invoke"))))); + policy = true; + String definition = MAPPER.writeValueAsString(Map.of("StartAt", "Invoke", "States", Map.of("Invoke", + Map.of("Type", "Task", "Resource", "arn:aws:states:::apigateway:invoke", "End", true, + "Parameters", Map.of("ApiEndpoint", api + ".execute-api.us-east-1.amazonaws.com", + "Stage", "gamma", "Path", "invoke", "Method", "POST", "AuthType", "IAM_ROLE", + "RequestBody", Map.of("owned", true)))))); + machine = sfn("CreateStateMachine", Map.of("name", name, "roleArn", role.getArn(), "definition", definition)) + .then().statusCode(200).extract().path("stateMachineArn"); + Response success = execute(machine); + assertEquals("SUCCEEDED", success.jsonPath().getString("status"), success.asString()); + assertTrue(MAPPER.readTree(success.jsonPath().getString("output")).path("ResponseBody").path("owned").asBoolean()); + iam.deleteRolePolicy(name, "invoke"); + policy = false; + Response stillAllowed = execute(machine); + assertPolicyResult(stillAllowed); + String taskName = name + "-task"; + taskRole = iam.createRole(taskName, "/", MAPPER.writeValueAsString(Map.of("Statement", List.of(Map.of( + "Effect", "Allow", "Principal", Map.of("AWS", role.getArn()), "Action", "sts:AssumeRole")))), + null, 3600, Map.of()); + iam.putRolePolicy(name, "assume", MAPPER.writeValueAsString(Map.of("Statement", List.of(Map.of( + "Effect", "Allow", "Action", "sts:AssumeRole", "Resource", taskRole.getArn()))))); + assumePolicy = true; + iam.putRolePolicy(taskName, "invoke", MAPPER.writeValueAsString(Map.of("Statement", List.of(Map.of( + "Effect", "Allow", "Action", "execute-api:Invoke", "Resource", + "arn:aws:execute-api:us-east-1:000000000000:" + api + "/gamma/POST/invoke"))))); + taskPolicy = true; + ObjectNode taskDefinition = (ObjectNode) MAPPER.readTree(definition); + ((ObjectNode) taskDefinition.path("States").path("Invoke")).putObject("Credentials") + .put("RoleArn", taskRole.getArn()); + sfn("UpdateStateMachine", Map.of("stateMachineArn", machine, "definition", taskDefinition.toString())) + .then().statusCode(200); + Response selectedRole = execute(machine); + assertEquals("SUCCEEDED", selectedRole.jsonPath().getString("status"), selectedRole.asString()); + iam.deleteRolePolicy(taskName, "invoke"); + taskPolicy = false; + assertPolicyResult(execute(machine)); + sfn("UpdateStateMachine", Map.of("stateMachineArn", machine, "definition", definition)) + .then().statusCode(200); + iam.deleteRolePolicy(name, "assume"); + assumePolicy = false; + iam.deleteRole(name); + Response absentRole = execute(machine); + assertPolicyResult(absentRole); + } finally { + if (machine != null) { + sfn("DeleteStateMachine", Map.of("stateMachineArn", machine)).then().statusCode(200); + } + if (api != null) { + given().delete("/restapis/" + api).then().statusCode(202); + } + if (taskRole != null) { + if (taskPolicy) { + iam.deleteRolePolicy(taskRole.getRoleName(), "invoke"); + } + iam.deleteRole(taskRole.getRoleName()); + } + if (assumePolicy) { + iam.deleteRolePolicy(name, "assume"); + } + if (policy) { + iam.deleteRolePolicy(name, "invoke"); + } + if (iam.findRole("000000000000", name).isPresent()) { + iam.deleteRole(name); + } + } + } + + private void assertPolicyResult(Response response) { + if (config.services().iam().enforcementEnabled()) { + assertEquals("FAILED", response.jsonPath().getString("status"), response.asString()); + assertEquals("ApiGateway.403", response.jsonPath().getString("error")); + } else { + assertEquals("SUCCEEDED", response.jsonPath().getString("status"), response.asString()); + } + } + + private static Response execute(String machine) throws Exception { + String arn = sfn("StartExecution", Map.of("stateMachineArn", machine, "input", "{}")) + .then().statusCode(200).extract().path("executionArn"); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (true) { + Response response = sfn("DescribeExecution", Map.of("executionArn", arn)); + response.then().statusCode(200); + if (!"RUNNING".equals(response.jsonPath().getString("status"))) { + return response; + } + assertTrue(System.nanoTime() < deadline, "API workflow did not terminate"); + Thread.sleep(25); + } + } + + private static Response sfn(String action, Map body) throws Exception { + return given().filter(AwsRequestSigner.signedAs("000000000000", "test", "states")) + .contentType("application/x-amz-json-1.0") + .header("X-Amz-Target", "AWSStepFunctions." + action) + .body(MAPPER.writeValueAsString(body)).post("/"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java index c029edd109..38c8aa6f7c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java @@ -30,7 +30,7 @@ class StepFunctionsMockedServiceIntegrationTest { private static final String DDB_CONTENT_TYPE = "application/x-amz-json-1.0"; private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role"; private static final String TABLE_NAME = "sfn-mock-items"; - private static final String UNSUPPORTED_RESOURCE = "arn:aws:states:::apigateway:invoke"; + private static final String API_RESOURCE = "arn:aws:states:::apigateway:invoke"; private static final ObjectMapper mapper = new ObjectMapper(); private static String mockSmArn; @@ -72,7 +72,7 @@ class StepFunctionsMockedServiceIntegrationTest { } } } - """.formatted(UNSUPPORTED_RESOURCE)); + """.formatted(API_RESOURCE)); ddbSmArn = createStateMachine("sfn-mock-ddb-test", null, """ { @@ -98,7 +98,7 @@ class StepFunctionsMockedServiceIntegrationTest { } } } - """.formatted(UNSUPPORTED_RESOURCE, TABLE_NAME)); + """.formatted(API_RESOURCE, TABLE_NAME)); expressSmArn = createStateMachine("sfn-mock-express-test", "EXPRESS", """ { @@ -111,7 +111,7 @@ class StepFunctionsMockedServiceIntegrationTest { } } } - """.formatted(UNSUPPORTED_RESOURCE)); + """.formatted(API_RESOURCE)); skipSmArn = createStateMachine("sfn-mock-skip-test", null, """ { @@ -134,7 +134,7 @@ class StepFunctionsMockedServiceIntegrationTest { } } } - """.formatted(UNSUPPORTED_RESOURCE, UNSUPPORTED_RESOURCE)); + """.formatted(API_RESOURCE, API_RESOURCE)); given() .header("X-Amz-Target", "DynamoDB_20120810.CreateTable") @@ -254,7 +254,7 @@ class StepFunctionsMockedServiceIntegrationTest { Response describe = waitForTerminalState(execArn); assertEquals("FAILED", describe.jsonPath().getString("status")); assertEquals("States.TaskFailed", describe.jsonPath().getString("error")); - assertTrue(describe.jsonPath().getString("cause").contains("Unsupported resource")); + assertTrue(describe.jsonPath().getString("cause").contains("only REST POST with IAM_ROLE and JSON request body is supported")); } @Test @@ -283,7 +283,7 @@ class StepFunctionsMockedServiceIntegrationTest { Response describe = waitForTerminalState(execArn); assertEquals("FAILED", describe.jsonPath().getString("status")); assertEquals("States.TaskFailed", describe.jsonPath().getString("error")); - assertTrue(describe.jsonPath().getString("cause").contains("Unsupported resource")); + assertTrue(describe.jsonPath().getString("cause").contains("only REST POST with IAM_ROLE and JSON request body is supported")); } @Test