mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(apigateway): validate and normalize imported integration types (#5543)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
eba058266b
commit
63e0302c12
9 files changed
+489
-3
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/** Integration types generated from Botocore's public API Gateway service model. */
|
||||
final class ApiGatewayIntegrationTypes {
|
||||
|
||||
private static final String RESOURCE_NAME = "aws/apigateway-integration-types.json";
|
||||
private static final Map<String, String> TYPES = load();
|
||||
|
||||
private ApiGatewayIntegrationTypes() {
|
||||
}
|
||||
|
||||
static String canonical(String value) {
|
||||
if (value.chars().anyMatch(character -> character > 0x7f)) {
|
||||
return null;
|
||||
}
|
||||
return TYPES.get(value.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
private static Map<String, String> load() {
|
||||
try (InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(RESOURCE_NAME)) {
|
||||
if (input == null) {
|
||||
throw new IllegalStateException("Missing API Gateway integration types: " + RESOURCE_NAME);
|
||||
}
|
||||
JsonNode values = new ObjectMapper().readTree(input).path("integrationTypes");
|
||||
if (!values.isArray() || values.isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries no integrationTypes");
|
||||
}
|
||||
Map<String, String> types = new HashMap<>();
|
||||
for (JsonNode value : values) {
|
||||
if (!value.isTextual() || value.asText().isBlank()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries an invalid integration type");
|
||||
}
|
||||
types.put(value.asText().toLowerCase(Locale.ROOT), value.asText());
|
||||
}
|
||||
return Map.copyOf(types);
|
||||
} catch (IOException error) {
|
||||
throw new IllegalStateException("Failed to read " + RESOURCE_NAME, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
-3
@@ -3675,21 +3675,38 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
if (openAPI.getPaths() != null) {
|
||||
for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
|
||||
PathItem pathItem = entry.getValue();
|
||||
for (Map.Entry<PathItem.HttpMethod, Operation> operation : pathItem.readOperationsMap().entrySet()) {
|
||||
addImportedIntegrationWarning(entry.getKey(), operation.getKey().name(),
|
||||
operation.getValue(), warnings);
|
||||
}
|
||||
Object anyMethod = pathItem.getExtensions() != null
|
||||
? pathItem.getExtensions().get("x-amazon-apigateway-any-method") : null;
|
||||
if (anyMethod != null) {
|
||||
OpenAPIDeserializer.ParseResult anyResult = new OpenAPIDeserializer.ParseResult();
|
||||
parseAnyMethodOperation(entry.getKey(), anyMethod, anyResult);
|
||||
Operation operation = parseAnyMethodOperation(entry.getKey(), anyMethod, anyResult);
|
||||
warnings.addAll(anyResult.getMessages());
|
||||
addImportedIntegrationWarning(entry.getKey(), "ANY", operation, warnings);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failOnWarnings && !warnings.isEmpty()) {
|
||||
throw new AwsException("BadRequestException", String.join("; ", warnings), 400);
|
||||
throw new AwsException("BadRequestException",
|
||||
"Warnings found during import:\n\t" + String.join("\n\t", warnings), 400);
|
||||
}
|
||||
return new ParsedOpenApi(openAPI, List.copyOf(warnings));
|
||||
}
|
||||
|
||||
private static void addImportedIntegrationWarning(String path, String httpMethod,
|
||||
Operation operation, List<String> warnings) {
|
||||
Object extension = operation.getExtensions() == null
|
||||
? null : operation.getExtensions().get("x-amazon-apigateway-integration");
|
||||
if (extension instanceof Map<?, ?> integration
|
||||
&& integration.get("type") instanceof String type
|
||||
&& ApiGatewayIntegrationTypes.canonical(type) == null) {
|
||||
warnings.add("Unknown integration type '" + type + "' for '" + httpMethod + " " + path + "'. Ignoring.");
|
||||
}
|
||||
}
|
||||
|
||||
private void validateImportedAuthorizers(OpenAPI openAPI) {
|
||||
if (openAPI.getComponents() == null || openAPI.getComponents().getSecuritySchemes() == null) {
|
||||
return;
|
||||
@@ -4395,7 +4412,13 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
String httpMethod, Map<String, Object> integrationExt) {
|
||||
Map<String, Object> integrationRequest = new HashMap<>();
|
||||
Object integrationType = integrationExt.get("type");
|
||||
integrationRequest.put("type", integrationType instanceof String type ? type.toUpperCase(Locale.ROOT) : integrationType);
|
||||
if (integrationType instanceof String type) {
|
||||
integrationType = ApiGatewayIntegrationTypes.canonical(type);
|
||||
if (integrationType == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
integrationRequest.put("type", integrationType);
|
||||
integrationRequest.put("httpMethod", integrationExt.get("httpMethod"));
|
||||
integrationRequest.put("uri", integrationExt.get("uri"));
|
||||
Object passthrough = integrationExt.get("passthroughBehavior");
|
||||
|
||||
Reference in new issue
Block a user