mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(apigateway): export deployed REST APIs as OpenAPI (#5131)
* feat(apigateway): export deployed REST APIs as OpenAPI * fix(apigateway): reject lossy export round trips * fix(apigateway): preserve native export round trips --------- Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
1 parent
760c38ef99
commit
1acedaf068
13 files changed
+1132
-12
No files matched your search
+193
@@ -0,0 +1,193 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.Authorizer;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.Deployment;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.MethodConfig;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.RequestValidator;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayExportIntegrationTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
private static final String TEMPLATE = "{\"quote\":\"a\\\"b\",\"path\":\"$input.path('$')\"}";
|
||||
|
||||
@Inject
|
||||
ApiGatewayService service;
|
||||
private String apiId;
|
||||
private String resourceId;
|
||||
private String deploymentId;
|
||||
private final List<String> imported = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void createDeployedApi() {
|
||||
apiId = service.createRestApi(REGION, Map.of("name", "export-fixture", "description", "Synthetic export",
|
||||
"binaryMediaTypes", List.of("application/octet-stream"),
|
||||
"policy", "{\"Version\":\"2012-10-17\",\"Statement\":[]}")).getId();
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
resourceId = service.createResource(REGION, apiId, rootId, Map.of("pathPart", "widgets")).getId();
|
||||
service.createModel(REGION, apiId, Map.of("name", "Widget", "contentType", "application/json",
|
||||
"schema", "{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"}}}"));
|
||||
RequestValidator validator = service.createRequestValidator(REGION, apiId,
|
||||
Map.of("name", "body-and-parameters", "validateRequestBody", true, "validateRequestParameters", true));
|
||||
service.putMethod(REGION, apiId, resourceId, "POST", Map.of("authorizationType", "AWS_IAM", "apiKeyRequired", true,
|
||||
"requestValidatorId", validator.getId(), "requestModels", Map.of("application/json", "Widget"),
|
||||
"requestParameters", Map.of("method.request.header.X-Trace", true, "method.request.querystring.limit", false)));
|
||||
service.putIntegration(REGION, apiId, resourceId, "POST", Map.of("type", "MOCK", "timeoutInMillis", 9000,
|
||||
"requestTemplates", Map.of("application/json", TEMPLATE)));
|
||||
service.putMethodResponse(REGION, apiId, resourceId, "POST", "201",
|
||||
Map.of("responseParameters", Map.of("method.response.header.X-Trace", false)));
|
||||
service.putIntegrationResponse(REGION, apiId, resourceId, "POST", "201", Map.of("selectionPattern", "",
|
||||
"responseParameters", Map.of("method.response.header.X-Trace", "'trace'"),
|
||||
"responseTemplates", Map.of("application/json", TEMPLATE), "contentHandling", "CONVERT_TO_TEXT"));
|
||||
Authorizer authorizer = service.createAuthorizer(REGION, apiId, Map.of("name", "TokenAuth", "type", "TOKEN",
|
||||
"identitySource", "method.request.header.Authorization", "authorizerResultTtlInSeconds", 0,
|
||||
"authorizerUri", "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations"));
|
||||
service.putMethod(REGION, apiId, rootId, "ANY", Map.of("authorizationType", "CUSTOM", "authorizerId", authorizer.getId()));
|
||||
service.putIntegration(REGION, apiId, rootId, "ANY", Map.of("type", "MOCK"));
|
||||
service.putGatewayResponse(REGION, apiId, "DEFAULT_4XX", Map.of("responseTemplates", Map.of("application/json", TEMPLATE)));
|
||||
service.putMethod(REGION, apiId, resourceId, "GET", Map.of("authorizationType", "NONE"));
|
||||
service.putIntegration(REGION, apiId, resourceId, "GET", Map.of("type", "MOCK",
|
||||
"requestTemplates", Map.of("application/json", "{\"statusCode\":200}")));
|
||||
service.putMethodResponse(REGION, apiId, resourceId, "GET", "200", Map.of());
|
||||
service.putIntegrationResponse(REGION, apiId, resourceId, "GET", "200",
|
||||
Map.of("responseTemplates", Map.of("application/json", "{\"exported\":true}")));
|
||||
deploymentId = service.createDeployment(REGION, apiId, Map.of("stageName", "dev")).id();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
imported.forEach(id -> service.deleteRestApi(REGION, id));
|
||||
service.deleteRestApi(REGION, apiId);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"oas30,application/json", "swagger,application/json", "oas30,application/yaml", "swagger,application/yaml"})
|
||||
void exportedStageReimportsSupportedConfiguration(String type, String accepts) throws Exception {
|
||||
Response response = given().accept(accepts).queryParam("extensions", "apigateway")
|
||||
.get(exportPath("dev", type)).then().statusCode(200)
|
||||
.header("Content-Type", containsString(accepts)).header("Content-Disposition", containsString("attachment"))
|
||||
.extract().response();
|
||||
JsonNode document = parse(response.asString(), accepts);
|
||||
assertEquals(type.equals("swagger") ? "2.0" : "3.0.1", document.path(type.equals("swagger") ? "swagger" : "openapi").asText());
|
||||
assertEquals(TEMPLATE, document.at("/paths/~1widgets/post/x-amazon-apigateway-integration/requestTemplates/application~1json").asText());
|
||||
assertTrue(document.at("/paths/~1/x-amazon-apigateway-any-method").isObject());
|
||||
String restoredId = given().contentType(accepts).queryParam("mode", "import").body(response.asByteArray())
|
||||
.post("/restapis").then().statusCode(201).extract().path("id");
|
||||
imported.add(restoredId);
|
||||
ApiGatewayResource widgets = service.getResources(REGION, restoredId).stream()
|
||||
.filter(resource -> "/widgets".equals(resource.getPath())).findFirst().orElseThrow();
|
||||
MethodConfig method = widgets.getResourceMethods().get("POST");
|
||||
assertEquals(List.of("application/octet-stream"), service.getRestApi(REGION, restoredId).getBinaryMediaTypes());
|
||||
assertEquals(JSON.readTree(service.getRestApi(REGION, apiId).getPolicy()),
|
||||
JSON.readTree(service.getRestApi(REGION, restoredId).getPolicy()));
|
||||
assertEquals("AWS_IAM", method.getAuthorizationType());
|
||||
assertTrue(method.isApiKeyRequired());
|
||||
assertEquals(Map.of("method.request.header.X-Trace", true, "method.request.querystring.limit", false), method.getRequestParameters());
|
||||
assertEquals(Map.of("application/json", "Widget"), method.getRequestModels());
|
||||
assertEquals("body-and-parameters", service.getRequestValidator(REGION, restoredId, method.getRequestValidatorId()).getName());
|
||||
assertEquals(Map.of("method.response.header.X-Trace", false), method.getMethodResponses().get("201").responseParameters());
|
||||
assertEquals(TEMPLATE, method.getMethodIntegration().getRequestTemplates().get("application/json"));
|
||||
assertEquals(9000, method.getMethodIntegration().getTimeoutInMillis());
|
||||
assertEquals("CONVERT_TO_TEXT", method.getMethodIntegration().getIntegrationResponses().get("201").contentHandling());
|
||||
ApiGatewayResource root = service.getResources(REGION, restoredId).stream()
|
||||
.filter(resource -> "/".equals(resource.getPath())).findFirst().orElseThrow();
|
||||
MethodConfig any = root.getResourceMethods().get("ANY");
|
||||
assertEquals("CUSTOM", any.getAuthorizationType());
|
||||
assertEquals("TokenAuth", service.getAuthorizer(REGION, restoredId, any.getAuthorizerId()).getName());
|
||||
assertEquals(TEMPLATE, service.getGatewayResponse(REGION, restoredId, "DEFAULT_4XX").getResponseTemplates().get("application/json"));
|
||||
service.createDeployment(REGION, restoredId, Map.of("stageName", "dev"));
|
||||
given().accept("application/json").get("/restapis/" + restoredId + "/dev/_user_request_/widgets")
|
||||
.then().statusCode(200).body("exported", equalTo(true));
|
||||
assertTrue(JSON.readTree(service.getModel(REGION, restoredId, "Widget").getSchema()).path("properties").has("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extensionSelectionAndDefaultJsonAreRespected() throws Exception {
|
||||
JsonNode basic = JSON.readTree(given().get(exportPath("dev", "oas30")).then().statusCode(200).extract().asString());
|
||||
assertFalse(basic.at("/paths/~1widgets/post").has("x-amazon-apigateway-integration"));
|
||||
assertFalse(basic.at("/components/securitySchemes/TokenAuth").has("x-amazon-apigateway-authorizer"));
|
||||
assertFalse(basic.has("x-amazon-apigateway-request-validators"));
|
||||
JsonNode integrations = JSON.readTree(given().queryParam("extensions", "integrations")
|
||||
.get(exportPath("dev", "oas30")).then().statusCode(200).extract().asString());
|
||||
assertTrue(integrations.at("/paths/~1widgets/post").has("x-amazon-apigateway-integration"));
|
||||
assertFalse(integrations.at("/components/securitySchemes/TokenAuth").has("x-amazon-apigateway-authorizer"));
|
||||
JsonNode auth = JSON.readTree(given().queryParam("extensions", "authorizers")
|
||||
.get(exportPath("dev", "oas30")).then().statusCode(200).extract().asString());
|
||||
assertTrue(auth.at("/components/securitySchemes/TokenAuth").has("x-amazon-apigateway-authorizer"));
|
||||
assertFalse(auth.at("/paths/~1widgets/post").has("x-amazon-apigateway-integration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exportUsesSelectedDeploymentAndSurvivesDescriptionPatch() throws Exception {
|
||||
service.updateDeployment(REGION, apiId, deploymentId, List.of(Map.of("op", "replace", "path", "/description", "value", "new description")));
|
||||
service.updateModel(REGION, apiId, "Widget", List.of(Map.of("op", "replace", "path", "/schema", "value", "{\"type\":\"integer\"}")));
|
||||
assertEquals("object", JSON.readTree(given().queryParam("extensions", "apigateway").get(exportPath("dev", "oas30"))
|
||||
.then().statusCode(200).extract().asString()).at("/components/schemas/Widget/type").asText());
|
||||
service.deleteMethod(REGION, apiId, resourceId, "POST");
|
||||
assertTrue(JSON.readTree(given().get(exportPath("dev", "oas30")).then().statusCode(200).extract().asString())
|
||||
.at("/paths/~1widgets/post").isObject());
|
||||
service.deleteMethod(REGION, apiId, resourceId, "GET");
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "next"));
|
||||
assertFalse(JSON.readTree(given().get(exportPath("next", "oas30")).then().statusCode(200).extract().asString())
|
||||
.path("paths").has("/widgets"));
|
||||
service.updateStage(REGION, apiId, "dev", List.of(Map.of("op", "replace", "path", "/deploymentId", "value",
|
||||
service.getStage(REGION, apiId, "next").getDeploymentId())));
|
||||
assertFalse(JSON.readTree(given().get(exportPath("dev", "oas30")).then().statusCode(200).extract().asString())
|
||||
.path("paths").has("/widgets"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidFormatsAndMissingResourcesReturnManagementErrors() {
|
||||
given().get(exportPath("dev", "invalid")).then().statusCode(400);
|
||||
given().queryParam("extensions", "invalid").get(exportPath("dev", "oas30")).then().statusCode(400);
|
||||
given().get(exportPath("missing", "oas30")).then().statusCode(404);
|
||||
given().get("/restapis/missing/stages/dev/exports/oas30").then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deploymentSnapshotSurvivesJacksonStorageRoundTrip() throws Exception {
|
||||
Deployment original = service.getDeployment(REGION, apiId, deploymentId);
|
||||
Deployment restored = JSON.readValue(JSON.writeValueAsString(original), Deployment.class);
|
||||
assertEquals(original.exportSnapshot(), restored.exportSnapshot());
|
||||
Deployment legacy = JSON.readValue("{\"id\":\"old\",\"description\":\"old\",\"createdDate\":1}", Deployment.class);
|
||||
assertNull(legacy.exportSnapshot());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonJsonModelsFailAtExportWithoutPreventingDeployment() {
|
||||
service.createModel(REGION, apiId, Map.of("name", "XmlBody", "contentType", "application/xml", "schema", "{}"));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "xml"));
|
||||
given().get(exportPath("xml", "oas30")).then().statusCode(400).body("message", containsString("non-JSON"));
|
||||
given().get("/restapis/" + apiId + "/deployments/" + deploymentId).then().statusCode(200)
|
||||
.body("exportSnapshot", org.hamcrest.Matchers.nullValue());
|
||||
}
|
||||
|
||||
private String exportPath(String stage, String type) {
|
||||
return "/restapis/" + apiId + "/stages/" + stage + "/exports/" + type;
|
||||
}
|
||||
|
||||
private JsonNode parse(String body, String type) throws Exception {
|
||||
return type.equals("application/yaml") ? new YAMLMapper().readTree(body) : JSON.readTree(body);
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsCertificateManager;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.Authorizer;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.Deployment;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.MethodConfig;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ApiGatewayExportServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private AccountAwareStorageBackend<Deployment> deployments;
|
||||
private ApiGatewayService service;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
deployments = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.create(anyString(), anyString(), any())).thenAnswer(invocation ->
|
||||
"apigateway-deployments.json".equals(invocation.getArgument(1))
|
||||
? deployments : AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
service = new ApiGatewayService(factory, mock(EmulatorConfig.class), mock(TlsCertificateManager.class),
|
||||
new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyDeploymentRequiresRedeploymentRatherThanExportingLiveMethods() {
|
||||
String apiId = service.createRestApi(REGION, Map.of("name", "legacy")).getId();
|
||||
deployments.put(REGION + "::" + apiId + "::old", new Deployment("old", "legacy", 1));
|
||||
service.createStage(REGION, apiId, Map.of("stageName", "dev", "deploymentId", "old"));
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.exportRestApi(REGION, apiId, "dev", "oas30", null));
|
||||
assertTrue(error.getMessage().contains("Redeploy"));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
assertNotNull(service.exportRestApi(REGION, apiId, "dev", "oas30", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedSecurityNamesDoNotOverwriteAuthorizers() {
|
||||
String apiId = service.createRestApi(REGION, Map.of("name", "scheme-collision")).getId();
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
Authorizer authorizer = service.createAuthorizer(REGION, apiId, Map.of("name", "sigv4", "type", "TOKEN",
|
||||
"authorizerUri", "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations"));
|
||||
service.putMethod(REGION, apiId, rootId, "GET", Map.of("authorizationType", "CUSTOM", "authorizerId", authorizer.getId()));
|
||||
service.putMethod(REGION, apiId, rootId, "POST", Map.of("authorizationType", "AWS_IAM"));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
JsonNode exported = service.exportRestApi(REGION, apiId, "dev", "oas30", "apigateway");
|
||||
assertTrue(exported.at("/components/securitySchemes/sigv4").has("x-amazon-apigateway-authorizer"));
|
||||
assertEquals("awsSigv4", exported.at("/components/securitySchemes/sigv4_1/x-amazon-apigateway-authtype").asText());
|
||||
assertTrue(exported.at("/paths/~1/get/security/0").has("sigv4"));
|
||||
assertTrue(exported.at("/paths/~1/post/security/0").has("sigv4_1"));
|
||||
}
|
||||
@ParameterizedTest
|
||||
@CsvSource({"TOKEN,oas30", "TOKEN,swagger", "COGNITO_USER_POOLS,oas30", "COGNITO_USER_POOLS,swagger"})
|
||||
void incompleteAuthorizerExportCannotBeImportedAsApiKey(String kind, String format) {
|
||||
String apiId = protectedApi(kind);
|
||||
JsonNode document = service.exportRestApi(REGION, apiId, "dev", format, null);
|
||||
String schemes = "swagger".equals(format) ? "securityDefinitions" : "components/securitySchemes";
|
||||
assertEquals("TOKEN".equals(kind) ? "custom" : "cognito_user_pools",
|
||||
document.at("/" + schemes + "/Protected/x-amazon-apigateway-authtype").asText());
|
||||
assertFalse(document.at("/" + schemes + "/Protected").has("x-amazon-apigateway-authorizer"));
|
||||
int before = service.getRestApis(REGION).size();
|
||||
AwsException rejected = assertThrows(AwsException.class,
|
||||
() -> service.importRestApi(REGION, document.toString()));
|
||||
assertTrue(rejected.getMessage().contains("authorizer definition"));
|
||||
assertEquals(before, service.getRestApis(REGION).size());
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.putRestApi(REGION, apiId, "overwrite", document.toString()));
|
||||
MethodConfig original = service.getResources(REGION, apiId).get(0).getResourceMethods().get("GET");
|
||||
assertEquals("TOKEN".equals(kind) ? "CUSTOM" : kind, original.getAuthorizationType());
|
||||
assertFalse(original.isApiKeyRequired());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"TOKEN,oas30,authorizers", "TOKEN,swagger,apigateway",
|
||||
"COGNITO_USER_POOLS,oas30,apigateway", "COGNITO_USER_POOLS,swagger,authorizers"})
|
||||
void fullAuthorizerExportPreservesAuthorizationAndScopes(String kind, String format, String extensions) {
|
||||
String apiId = protectedApi(kind);
|
||||
JsonNode document = service.exportRestApi(REGION, apiId, "dev", format, extensions);
|
||||
String restoredId = service.importRestApi(REGION, document.toString()).getId();
|
||||
MethodConfig method = service.getResources(REGION, restoredId).get(0).getResourceMethods().get("GET");
|
||||
assertEquals("TOKEN".equals(kind) ? "CUSTOM" : kind, method.getAuthorizationType());
|
||||
assertFalse(method.isApiKeyRequired());
|
||||
assertEquals("TOKEN".equals(kind) ? List.of() : List.of("widgets/read", "widgets/write"), method.getAuthorizationScopes());
|
||||
Authorizer original = service.getAuthorizers(REGION, apiId).get(0);
|
||||
Authorizer restored = service.getAuthorizer(REGION, restoredId, method.getAuthorizerId());
|
||||
assertEquals(original.getType(), restored.getType());
|
||||
assertEquals(original.getProviderARNs(), restored.getProviderARNs());
|
||||
assertEquals(original.getAuthorizerUri(), restored.getAuthorizerUri());
|
||||
assertEquals(original.getIdentitySource(), restored.getIdentitySource());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(value = {"oas30|", "swagger|", "oas30|4[0-9]{2}", "swagger|4[0-9]{2}",
|
||||
"oas30|default", "swagger|default"}, delimiter = '|')
|
||||
void duplicateResponsePatternsFailExplicitly(String format, String configuredPattern) {
|
||||
String apiId = service.createRestApi(REGION, Map.of("name", "duplicate-responses")).getId();
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
service.putMethod(REGION, apiId, rootId, "GET", Map.of("authorizationType", "NONE"));
|
||||
service.putIntegration(REGION, apiId, rootId, "GET", Map.of("type", "MOCK"));
|
||||
String pattern = configuredPattern == null ? "" : configuredPattern;
|
||||
service.putIntegrationResponse(REGION, apiId, rootId, "GET", "200",
|
||||
Map.of("selectionPattern", pattern, "responseTemplates", Map.of("application/json", "first")));
|
||||
service.putIntegrationResponse(REGION, apiId, rootId, "GET", "400",
|
||||
Map.of("selectionPattern", pattern, "responseTemplates", Map.of("application/json", "second")));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.exportRestApi(REGION, apiId, "dev", format, "integrations"));
|
||||
assertTrue(error.getMessage().contains("selection pattern"));
|
||||
assertEquals(2, service.getIntegration(REGION, apiId, rootId, "GET").getIntegrationResponses().size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"CUSTOM,oas30", "COGNITO_USER_POOLS,swagger"})
|
||||
void missingStoredAuthorizerCannotProduceAnUnprotectedExport(String authorization, String format) {
|
||||
String apiId = service.createRestApi(REGION, Map.of("name", "missing-authorizer")).getId();
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
service.putMethod(REGION, apiId, rootId, "GET", Map.of("authorizationType", authorization));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.exportRestApi(REGION, apiId, "dev", format, "authorizers"));
|
||||
assertTrue(error.getMessage().contains("Missing authorizer for protected method"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"TOKEN,oas30", "TOKEN,swagger", "COGNITO_USER_POOLS,oas30", "COGNITO_USER_POOLS,swagger"})
|
||||
void unusedAuthorizerDoesNotBlockDefaultExportImportOrOverwrite(String kind, String format) {
|
||||
String apiId = protectedApi(kind);
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
service.putMethod(REGION, apiId, rootId, "GET", Map.of("authorizationType", "NONE"));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
JsonNode document = service.exportRestApi(REGION, apiId, "dev", format, null);
|
||||
String restoredId = service.importRestApi(REGION, document.toString()).getId();
|
||||
MethodConfig restored = service.getResources(REGION, restoredId).get(0).getResourceMethods().get("GET");
|
||||
assertEquals("NONE", restored.getAuthorizationType());
|
||||
assertFalse(restored.isApiKeyRequired());
|
||||
service.putRestApi(REGION, apiId, "overwrite", document.toString());
|
||||
assertEquals("NONE", service.getResources(REGION, apiId).get(0).getResourceMethods().get("GET").getAuthorizationType());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"get,false", "get,true", "x-amazon-apigateway-any-method,false", "x-amazon-apigateway-any-method,true"})
|
||||
void incompleteAuthorizerCheckUsesEffectiveOperationSecurity(String verb, boolean overrideSecurity) {
|
||||
String document = """
|
||||
{
|
||||
"openapi": "3.0.1", "info": {"title": "user-definition", "version": "1"},
|
||||
"components": {"securitySchemes": {"Unused": {
|
||||
"type": "apiKey", "in": "header", "name": "Authorization",
|
||||
"x-amazon-apigateway-authtype": "custom"
|
||||
}}},
|
||||
"security": [{"Unused": []}],
|
||||
"paths": {"/": {"%s": {
|
||||
%s
|
||||
"responses": {"200": {"description": "OK"}}
|
||||
}}}
|
||||
}
|
||||
""".formatted(verb, overrideSecurity ? "\"security\": []," : "");
|
||||
if (overrideSecurity) {
|
||||
String apiId = service.importRestApi(REGION, document).getId();
|
||||
MethodConfig method = service.getResources(REGION, apiId).get(0).getResourceMethods()
|
||||
.get("get".equals(verb) ? "GET" : "ANY");
|
||||
assertEquals("NONE", method.getAuthorizationType());
|
||||
assertFalse(method.isApiKeyRequired());
|
||||
} else {
|
||||
AwsException rejected = assertThrows(AwsException.class, () -> service.importRestApi(REGION, document));
|
||||
assertTrue(rejected.getMessage().contains("authorizer definition"));
|
||||
}
|
||||
}
|
||||
|
||||
private String protectedApi(String kind) {
|
||||
String apiId = service.createRestApi(REGION, Map.of("name", "protected-export")).getId();
|
||||
String rootId = service.getResources(REGION, apiId).get(0).getId();
|
||||
Map<String, Object> request = new HashMap<>();
|
||||
request.put("name", "Protected");
|
||||
request.put("type", kind);
|
||||
request.put("identitySource", "method.request.header.Authorization");
|
||||
if ("TOKEN".equals(kind)) {
|
||||
request.put("authorizerUri", "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations");
|
||||
} else {
|
||||
request.put("providerARNs", List.of("arn:aws:cognito-idp:us-east-1:000000000000:userpool/us-east-1_fixture"));
|
||||
}
|
||||
Authorizer authorizer = service.createAuthorizer(REGION, apiId, request);
|
||||
service.putMethod(REGION, apiId, rootId, "GET", Map.of("authorizationType", "TOKEN".equals(kind) ? "CUSTOM" : kind,
|
||||
"authorizerId", authorizer.getId(), "authorizationScopes", "TOKEN".equals(kind)
|
||||
? List.of() : List.of("widgets/read", "widgets/write")));
|
||||
service.createDeployment(REGION, apiId, Map.of("stageName", "dev"));
|
||||
return apiId;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user