fix(apigateway): resolve AWS variables on REST import (#4547)

* fix(apigateway): resolve AWS variables on REST import

* fix(apigateway): preserve OpenAPI mapping templates

* fix(apigateway): resolve imported authorizer variables

* chore(ci): retry Docker Hub limited tests

* fix(apigateway): resolve import variables across definitions

* test(apigateway): cover variable imports with AWS SDK

* refactor(apigateway): remove unused import validation region

* style(apigateway): sort OpenAPI parser import

* fix(apigateway): preserve imported authorizer merge behavior

---------

Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
vamsiakandVamsi Akavarapu authored and GitHub committed 2026-09-29 15:32:17 -04:00
1 parent 86c4904bcc
commit 0383ecf53e
4 files changed
+327 -5

No files matched your search

@@ -7,6 +7,7 @@ import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
import software.amazon.awssdk.services.apigateway.model.GetIntegrationResponse;
import software.amazon.awssdk.services.apigateway.model.GetMethodResponse;
import software.amazon.awssdk.services.apigateway.model.GatewayResponseType;
import software.amazon.awssdk.services.apigateway.model.ImportRestApiResponse;
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
import software.amazon.awssdk.services.apigateway.model.PutMode;
@@ -24,6 +25,72 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
@DisplayName("API Gateway OpenAPI import")
class ApiGatewayOpenApiImportCompatibilityTest {
@Test
@DisplayName("ImportRestApi and PutRestApi resolve AWS variables through the SDK")
void sdkImportAndPutResolveAwsVariables() {
String title = TestFixtures.uniqueName("apigw-variable-import");
try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) {
ImportRestApiResponse imported = apiGateway.importRestApi(request -> request
.body(SdkBytes.fromUtf8String(variableSpec(title, "${AWS::Region}"))));
try {
String initialId = resourceId(apiGateway, imported.id(), "/variable");
GetIntegrationResponse integration = apiGateway.getIntegration(request -> request
.restApiId(imported.id()).resourceId(initialId).httpMethod("POST"));
assertThat(integration.uri())
.isEqualTo("arn:aws:apigateway:us-east-1:states:action/StartExecution");
assertThat(integration.requestTemplates())
.containsEntry("application/json", "{\"value\":\"us-east-1\"}");
assertThat(integration.requestParameters())
.containsEntry("integration.request.header.X-Region", "'us-east-1'");
assertThat(apiGateway.getGatewayResponse(request -> request
.restApiId(imported.id()).responseType(GatewayResponseType.UNAUTHORIZED))
.responseTemplates())
.containsEntry("application/json", "{\"account\":\"000000000000\"}");
apiGateway.putRestApi(request -> request.restApiId(imported.id())
.mode(PutMode.OVERWRITE)
.body(SdkBytes.fromUtf8String(variableSpec(title, "${AWS::AccountId}"))));
String updatedId = resourceId(apiGateway, imported.id(), "/variable");
integration = apiGateway.getIntegration(request -> request
.restApiId(imported.id()).resourceId(updatedId).httpMethod("POST"));
assertThat(integration.requestTemplates())
.containsEntry("application/json", "{\"value\":\"000000000000\"}");
} finally {
apiGateway.deleteRestApi(request -> request.restApiId(imported.id()));
}
}
}
private static String variableSpec(String title, String templateValue) {
return """
{
"openapi": "3.0.1",
"info": {"title": "%s", "version": "1"},
"paths": {
"/variable": {
"post": {
"responses": {"200": {"description": "ok"}},
"x-amazon-apigateway-integration": {
"type": "aws", "httpMethod": "POST",
"uri": "arn:${AWS::Partition}:apigateway:${AWS::Region}:states:action/StartExecution",
"requestParameters": {
"integration.request.header.X-Region": "'${AWS::Region}'"
},
"requestTemplates": {"application/json": "{\\"value\\":\\"%s\\"}"},
"responses": {"default": {"statusCode": "200"}}
}
}
}
},
"x-amazon-apigateway-gateway-responses": {
"UNAUTHORIZED": {
"responseTemplates": {"application/json": "{\\"account\\":\\"${AWS::AccountId}\\"}"}
}
}
}
""".formatted(title, templateValue);
}
private static final String BROKEN_ANY_METHOD_PATHS = """
"paths": {
"/p/{proxy+}": {
+5
View File
@@ -70,6 +70,11 @@ operation-level `security: []` leaves that method without authorization.
For a same-name Cognito authorizer, merge also keeps its existing pool ARNs when the incoming
definition provides no new `providerARNs`; an explicit list replaces them, including `[]`.
REST OpenAPI imports resolve `${AWS::Region}`, `${AWS::AccountId}`, and `${AWS::Partition}`
throughout the definition before parsing it. Values come from the request region and
account, so the same definition can be imported across regions and partitions. This
includes mapping templates and applies to both `ImportRestApi` and `PutRestApi`.
### Supported Operations
| Category | Operations |
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsPartition;
import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.AwsRegionFacts;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -36,6 +37,7 @@ import io.github.hectorvent.floci.services.apigateway.model.Stage;
import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;
import io.github.hectorvent.floci.services.apigateway.model.UsagePlanKey;
import io.github.hectorvent.floci.services.apigateway.model.VpcLink;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
@@ -108,17 +110,25 @@ public class ApiGatewayService {
private final Object domainNameLock = new Object();
private final TlsCertificateManager certificateManager;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
// Constants
private static final String EPC_KEY = "endpointConfiguration";
private static final String EPC_TYPES_KEY = "types";
private static final String EPC_VPC_IDS_KEY = "vpcEndpointIds";
ApiGatewayService(StorageFactory storageFactory, EmulatorConfig config,
TlsCertificateManager certificateManager) {
this(storageFactory, config, certificateManager,
new RegionResolver(config.defaultRegion(), config.defaultAccountId()));
}
@Inject
public ApiGatewayService(StorageFactory storageFactory, EmulatorConfig config,
TlsCertificateManager certificateManager) {
TlsCertificateManager certificateManager, RegionResolver regionResolver) {
this.certificateManager = certificateManager;
this.config = config;
this.regionResolver = regionResolver;
this.apiStore = storageFactory.create("apigateway", "apigateway-apis.json",
new TypeReference<>() {
});
@@ -2681,7 +2691,7 @@ public class ApiGatewayService {
}
public RestApi importRestApi(String region, String specBody, boolean failOnWarnings) {
ParsedOpenApi parsed = parseOpenApiSpec(specBody, failOnWarnings);
ParsedOpenApi parsed = parseOpenApiSpec(region, specBody, failOnWarnings);
OpenAPI openAPI = parsed.openAPI();
String name = openAPI.getInfo() != null ? openAPI.getInfo().getTitle() : "Imported API";
@@ -2717,7 +2727,7 @@ public class ApiGatewayService {
throw new AwsException("BadRequestException",
"Invalid mode specified. Valid modes are 'merge' and 'overwrite'.", 400);
}
ParsedOpenApi parsed = parseOpenApiSpec(specBody, failOnWarnings);
ParsedOpenApi parsed = parseOpenApiSpec(region, specBody, failOnWarnings);
RestApiSnapshot snapshot = snapshotRestApi(region, apiId);
try {
RestApi api = mode == null || "merge".equals(mode)
@@ -2733,6 +2743,16 @@ public class ApiGatewayService {
}
}
private String resolveOpenApiAwsVariables(String region, String value) {
if (value == null) {
return null;
}
return value
.replace("${AWS::Region}", region)
.replace("${AWS::AccountId}", regionResolver.getAccountId())
.replace("${AWS::Partition}", regionResolver.partitionForRegion(region));
}
private RestApi overwriteRestApi(String region, String apiId, OpenAPI openAPI) {
// Delete all non-root resources
List<ApiGatewayResource> existing = getResources(region, apiId);
@@ -2830,8 +2850,9 @@ public class ApiGatewayService {
private record ParsedOpenApi(OpenAPI openAPI, List<String> warnings) {}
private ParsedOpenApi parseOpenApiSpec(String specBody, boolean failOnWarnings) {
SwaggerParseResult result = new io.swagger.parser.OpenAPIParser().readContents(specBody, null, null);
private ParsedOpenApi parseOpenApiSpec(String region, String specBody, boolean failOnWarnings) {
SwaggerParseResult result = new OpenAPIParser()
.readContents(resolveOpenApiAwsVariables(region, specBody), null, null);
if (result.getOpenAPI() == null) {
String errors = result.getMessages() != null ? String.join(", ", result.getMessages()) : "unknown error";
throw new AwsException("BadRequestException", "Failed to parse OpenAPI spec: " + errors, 400);
@@ -0,0 +1,229 @@
package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class ApiGatewayOpenApiAwsVariablesIntegrationTest {
private static final ObjectMapper JSON = new ObjectMapper();
private static final String URI =
"arn:${AWS::Partition}:apigateway:${AWS::Region}:states:action/StartExecution";
private static final String CREDENTIALS =
"arn:${AWS::Partition}:iam::${AWS::AccountId}:role/example-role";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void importRestApiResolvesVariablesAndInvokesStateMachine() throws Exception {
ObjectNode stateMachine = JSON.createObjectNode();
stateMachine.put("name", "apigw-variables-test");
stateMachine.put("definition", "{\"StartAt\":\"Echo\",\"States\":{\"Echo\":{\"Type\":\"Pass\",\"End\":true}}}");
stateMachine.put("roleArn", "arn:aws:iam::000000000000:role/example-role");
String stateMachineArn = given()
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
.contentType("application/x-amz-json-1.0")
.body(stateMachine.toString())
.when().post("/")
.then().statusCode(200)
.extract().path("stateMachineArn");
String template = "{\"stateMachineArn\":\"" + stateMachineArn + "\",\"input\":\"{}\"}";
String apiId = given()
.contentType(ContentType.JSON)
.queryParam("mode", "import")
.body(spec("VariableImport", URI, CREDENTIALS, template))
.when().post("/restapis")
.then().statusCode(201)
.extract().path("id");
String resourceId = startResourceId(apiId, null);
String integration = given()
.when().get("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST/integration")
.then().statusCode(200)
.extract().asString();
JsonNode imported = JSON.readTree(integration);
assertEquals("arn:aws:apigateway:us-east-1:states:action/StartExecution",
imported.path("uri").asText());
assertEquals("arn:aws:iam::000000000000:role/example-role",
imported.path("credentials").asText());
given()
.contentType(ContentType.JSON)
.body("{\"stageName\":\"test\"}")
.when().post("/restapis/" + apiId + "/deployments")
.then().statusCode(201);
String response = given()
.contentType(ContentType.JSON)
.body("{}")
.when().post("/execute-api/" + apiId + "/test/start")
.then().statusCode(200)
.extract().asString();
assertTrue(JSON.readTree(response).path("executionArn").asText().contains("apigw-variables-test"));
}
@Test
void putRestApiUsesRequestAccountAndPartition() throws Exception {
String authorization = "AWS4-HMAC-SHA256 Credential=000000000001/20260928/us-gov-west-1/apigateway/aws4_request, "
+ "SignedHeaders=host, Signature=abc";
String apiId = given()
.header("Authorization", authorization)
.contentType(ContentType.JSON)
.body("{\"name\":\"variable-overwrite\"}")
.when().post("/restapis")
.then().statusCode(201)
.extract().path("id");
given()
.header("Authorization", authorization)
.contentType(ContentType.JSON)
.queryParam("mode", "overwrite")
.body(spec("VariableOverwrite", URI, CREDENTIALS,
"{\"region\":\"${AWS::Region}\"}"))
.when().put("/restapis/" + apiId)
.then().statusCode(200);
String resourceId = startResourceId(apiId, authorization);
String integration = given()
.header("Authorization", authorization)
.when().get("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST/integration")
.then().statusCode(200)
.extract().asString();
JsonNode imported = JSON.readTree(integration);
assertEquals("arn:aws-us-gov:apigateway:us-gov-west-1:states:action/StartExecution",
imported.path("uri").asText());
assertEquals("arn:aws-us-gov:iam::000000000001:role/example-role",
imported.path("credentials").asText());
assertEquals("{\"region\":\"us-gov-west-1\"}", imported.path("requestTemplates")
.path("application/json").asText());
}
@Test
void importRestApiResolvesVariablesAcrossDefinition() throws Exception {
String requestTemplate = "{\"region\":\"${AWS::Region}\"}";
String responseTemplate = "{\"account\":\"${AWS::AccountId}\"}";
ObjectNode definition = (ObjectNode) JSON.readTree(spec("DefinitionVariables", URI, CREDENTIALS,
requestTemplate));
ObjectNode integration = (ObjectNode) definition.path("paths").path("/start").path("post")
.path("x-amazon-apigateway-integration");
integration.putObject("requestParameters")
.put("integration.request.header.X-Region", "'${AWS::Region}'");
((ObjectNode) integration.path("responses").path("default"))
.putObject("responseTemplates").put("application/json", responseTemplate);
definition.putObject("x-amazon-apigateway-gateway-responses")
.putObject("UNAUTHORIZED").putObject("responseTemplates")
.put("application/json", "{\"partition\":\"${AWS::Partition}\"}");
String apiId = given()
.contentType(ContentType.JSON)
.queryParam("mode", "import")
.body(definition.toString())
.when().post("/restapis")
.then().statusCode(201)
.extract().path("id");
String resourceId = startResourceId(apiId, null);
String importedIntegration = given()
.when().get("/restapis/" + apiId + "/resources/" + resourceId + "/methods/POST/integration")
.then().statusCode(200)
.extract().asString();
JsonNode imported = JSON.readTree(importedIntegration);
assertEquals("{\"region\":\"us-east-1\"}", imported.path("requestTemplates")
.path("application/json").asText());
assertEquals("'us-east-1'", imported.path("requestParameters")
.path("integration.request.header.X-Region").asText());
String importedResponse = given()
.when().get("/restapis/" + apiId + "/resources/" + resourceId
+ "/methods/POST/integration/responses/200")
.then().statusCode(200)
.extract().asString();
assertEquals("{\"account\":\"000000000000\"}", JSON.readTree(importedResponse)
.path("responseTemplates").path("application/json").asText());
String gatewayResponse = given()
.when().get("/restapis/" + apiId + "/gatewayresponses/UNAUTHORIZED")
.then().statusCode(200)
.extract().asString();
assertEquals("{\"partition\":\"aws\"}", JSON.readTree(gatewayResponse)
.path("responseTemplates").path("application/json").asText());
}
@Test
void importRestApiResolvesAuthorizerArns() throws Exception {
ObjectNode definition = (ObjectNode) JSON.readTree(spec("AuthorizerVariables", URI, CREDENTIALS, "{}"));
ObjectNode schemes = definition.putObject("components").putObject("securitySchemes");
ObjectNode cognito = schemes.putObject("CognitoAuth");
cognito.put("type", "apiKey").put("name", "Authorization").put("in", "header");
cognito.put("x-amazon-apigateway-authtype", "cognito_user_pools");
cognito.putObject("x-amazon-apigateway-authorizer").put("type", "cognito_user_pools")
.putArray("providerARNs").add("arn:${AWS::Partition}:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/us-east-1_TEST");
ObjectNode lambda = schemes.putObject("LambdaAuth");
lambda.put("type", "apiKey").put("name", "Authorization").put("in", "header");
lambda.put("x-amazon-apigateway-authtype", "custom");
lambda.putObject("x-amazon-apigateway-authorizer").put("type", "token")
.put("authorizerUri", "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:auth/invocations");
String apiId = given().contentType(ContentType.JSON).queryParam("mode", "import")
.body(definition.toString()).when().post("/restapis").then().statusCode(201).extract().path("id");
JsonNode items = JSON.readTree(given().when().get("/restapis/" + apiId + "/authorizers")
.then().statusCode(200).extract().asString()).path("item");
assertEquals("arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_TEST",
findByName(items, "CognitoAuth").path("providerARNs").path(0).asText());
assertEquals("arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations",
findByName(items, "LambdaAuth").path("authorizerUri").asText());
}
private static JsonNode findByName(JsonNode items, String name) {
for (JsonNode item : items) {
if (name.equals(item.path("name").asText())) {
return item;
}
}
throw new AssertionError("Authorizer was not found: " + name);
}
private static String startResourceId(String apiId, String authorization) throws Exception {
String resources = authorization == null
? given().when().get("/restapis/" + apiId + "/resources")
.then().statusCode(200).extract().asString()
: given().header("Authorization", authorization)
.when().get("/restapis/" + apiId + "/resources")
.then().statusCode(200).extract().asString();
for (JsonNode resource : JSON.readTree(resources).path("item")) {
if ("/start".equals(resource.path("path").asText())) {
return resource.path("id").asText();
}
}
throw new AssertionError("Imported /start resource was not found");
}
private static String spec(String title, String uri, String credentials, String template) {
ObjectNode root = JSON.createObjectNode();
root.put("openapi", "3.0.1");
root.putObject("info").put("title", title).put("version", "1.0");
ObjectNode operation = root.putObject("paths").putObject("/start").putObject("post");
operation.putObject("responses").putObject("200").put("description", "ok");
ObjectNode integration = operation.putObject("x-amazon-apigateway-integration");
integration.put("type", "aws");
integration.put("httpMethod", "POST");
integration.put("uri", uri);
integration.put("credentials", credentials);
integration.putObject("requestTemplates").put("application/json", template);
integration.putObject("responses").putObject("default").put("statusCode", "200");
return root.toString();
}
}