mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
86c4904bcc
commit
0383ecf53e
4 files changed
+327
-5
No files matched your search
+26
-5
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user