feat(stepfunctions): invoke regional API Gateway tasks with IAM (#6043)

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-20 10:17:47 -04:00
1 parent 32b64e5e41
commit a899318862
10 files changed
+874 -23

No files matched your search

@@ -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<String, List<String>> 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<String> 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<byte[]> 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<String, List<String>> requestHeaders(JsonNode input) {
TreeMap<String, List<String>> 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<String> 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<String, List<String>> 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);
}
}
@@ -266,6 +266,7 @@ public class AslExecutor {
private final JsonataEvaluator jsonataEvaluator;
private final Instance<StepFunctionsService> 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<StepFunctionsService> 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();
@@ -67,12 +67,17 @@ public class LambdaTaskAuthorizer {
* @param functionArn the function, version or alias the task invokes, qualified as it names it
*/
public Optional<String> 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<String> refusal(String roleArn, String action, String resourceArn) {
AwsArnUtils.Arn resource = AwsArnUtils.parse(resourceArn);
Optional<IamRole> 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<List<String>> scpLevels = scpLevels(roleId.accountId());
List<ResourcePolicy> policies = functionPolicies.getResourcePolicies("lambda", functionArn);
String ownerAccount = policies.isEmpty() ? function.accountId() : policies.getFirst().ownerAccountId();
List<ResourcePolicy> policies = "lambda".equals(resource.service())
? functionPolicies.getResourcePolicies("lambda", resourceArn) : List.of();
String ownerAccount = policies.isEmpty() ? resource.accountId() : policies.getFirst().ownerAccountId();
List<String> documents = policies.stream()
.map(ResourcePolicy::policyDocument)
.filter(document -> document != null && !document.isBlank())
.toList();
Map<String, List<String>> 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))));
}