fix(apigateway): support integration connection patches (#6645)

* fix(apigateway): support integration connection patches

* test(apigateway): cover connection patches through AWS SDK

* fix(apigateway): accept V2 VPC links in integration patches

* fix(apigateway): resolve V2 VPC links during REST execution

* fix(apigateway): resolve VPC link stage variables at execution

* fix(apigateway): share VPC link stage variable pattern
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-26 17:05:35 -04:00
1 parent 2fd74eaf08
commit 4d0dfe9919
6 files changed
+345 -8

No files matched your search

@@ -784,25 +784,35 @@ public class ApiGatewayExecuteController {
return null;
}
String connectionId = integration.getConnectionId();
if (connectionId != null) {
Matcher stageVariable = ApiGatewayService.VPC_LINK_STAGE_VARIABLE.matcher(connectionId);
if (stageVariable.matches()) {
connectionId = scope.stage().getVariables().get(stageVariable.group(1));
}
}
if (connectionId == null || connectionId.isBlank()) {
return gatewayResponse(scope, GatewayResponseType.API_CONFIGURATION_ERROR, 500,
"VPC_LINK integration is missing a connectionId");
}
String status;
try {
io.github.hectorvent.floci.services.apigateway.model.VpcLink link =
apiGatewayService.getVpcLink(region, connectionId);
if (!"AVAILABLE".equalsIgnoreCase(link.getStatus())) {
status = apiGatewayService.getVpcLink(region, connectionId).getStatus();
} catch (AwsException e) {
try {
status = apiGatewayV2Service.getVpcLink(region, connectionId).getVpcLinkStatus();
} catch (AwsException ignored) {
LOG.warnv("VPC_LINK integration references unknown link {0} in {1}", connectionId, region);
return Response.status(502)
.entity(jsonMessage("VPC link is not available: " + link.getStatus()))
.entity(jsonMessage("Invalid VPC link identifier specified: " + connectionId))
.type(MediaType.APPLICATION_JSON).build();
}
return null;
} catch (AwsException e) {
LOG.warnv("VPC_LINK integration references unknown link {0} in {1}", connectionId, region);
}
if (!"AVAILABLE".equalsIgnoreCase(status)) {
return Response.status(502)
.entity(jsonMessage("Invalid VPC link identifier specified: " + connectionId))
.entity(jsonMessage("VPC link is not available: " + status))
.type(MediaType.APPLICATION_JSON).build();
}
return null;
}
// ──────────────────────────── Binary payloads ────────────────────────────
@@ -164,6 +164,8 @@ public class ApiGatewayService implements ResourceProvider {
"EDGE endpoint type is not supported for APIGatewayV2 domainName";
/** The characters AWS allows in an API mapping key, its level separator included, and the most it takes. */
private static final Pattern API_MAPPING_KEY = Pattern.compile("[A-Za-z0-9$\\-_.+!*'(),/]+");
static final Pattern VPC_LINK_STAGE_VARIABLE =
Pattern.compile("\\$\\{stageVariables\\.([A-Za-z0-9_]+)\\}");
private static final int MAX_API_MAPPING_KEY_LENGTH = 300;
private static final String MAPPING_KEY_SLASHES_MESSAGE =
"API mapping key should not start with a '/' or have consecutive '/'s.";
@@ -3425,6 +3427,10 @@ public class ApiGatewayService implements ResourceProvider {
if (patchOperations != null) {
String nextType = integration.getType();
String nextTransferMode = integration.getResponseTransferMode();
String nextConnectionType = integration.getConnectionType();
String nextConnectionId = integration.getConnectionId();
boolean connectionPatched = false;
boolean connectionIdPatched = false;
for (Map<String, String> op : patchOperations) {
String opType = op.get("op");
if (!"add".equals(opType) && !"replace".equals(opType)) {
@@ -3449,11 +3455,46 @@ public class ApiGatewayService implements ResourceProvider {
case "/responseTransferMode":
nextTransferMode = transferMode(value);
break;
case "/connectionType":
if (!"replace".equals(opType)) {
throw new AwsException("BadRequestException", "Invalid operation", 400);
}
nextConnectionType = value;
connectionPatched = true;
break;
case "/connectionId":
if (!"replace".equals(opType)) {
throw new AwsException("BadRequestException", "Invalid operation", 400);
}
nextConnectionId = value;
connectionPatched = true;
connectionIdPatched = true;
break;
default:
throw new AwsException("BadRequestException", "Unsupported path: " + path, 400);
}
}
validateTransferMode(nextTransferMode, nextType);
if (connectionPatched) {
if ("VPC_LINK".equals(nextConnectionType)) {
if (nextConnectionId == null || nextConnectionId.isBlank()
|| (!VPC_LINK_STAGE_VARIABLE.matcher(nextConnectionId).matches()
&& vpcLinkStore.get(vpcLinkKey(region, nextConnectionId)).isEmpty()
&& !apiGatewayV2Service.hasVpcLink(region, nextConnectionId))) {
throw new AwsException("BadRequestException",
"Invalid VPC link identifier specified: " + nextConnectionId, 400);
}
} else if ("INTERNET".equals(nextConnectionType)) {
if (connectionIdPatched) {
throw new AwsException("BadRequestException",
"Connection ID requires VPC_LINK", 400);
}
nextConnectionId = null;
} else {
throw new AwsException("BadRequestException",
"Invalid connection type: " + nextConnectionType, 400);
}
}
for (Map<String, String> op : patchOperations) {
String path = op.get("path");
String value = op.get("value");
@@ -3476,10 +3517,17 @@ public class ApiGatewayService implements ResourceProvider {
case "/responseTransferMode":
integration.setResponseTransferMode(transferMode(value));
break;
case "/connectionType":
case "/connectionId":
break;
default:
throw new IllegalStateException("Unreachable: validated above");
}
}
if (connectionPatched) {
integration.setConnectionType(nextConnectionType);
integration.setConnectionId(nextConnectionId);
}
}
resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId));
return integration;
@@ -1178,6 +1178,10 @@ public class ApiGatewayV2Service {
.orElseThrow(() -> new AwsException("NotFoundException", "VpcLink not found", 404));
}
public boolean hasVpcLink(String region, String vpcLinkId) {
return vpcLinkStore.get(vpcLinkKey(region, vpcLinkId)).isPresent();
}
public List<VpcLink> getVpcLinks(String region) {
String prefix = region + "::";
return vpcLinkStore.scan(k -> k.startsWith(prefix));