fix(apigateway): expose VTL authorizer and parameter maps (#3538)

* fix(apigateway): expose VTL authorizer and parameter maps

* fix(apigateway): match AWS authorizer VTL values

* fix(apigateway): pass authorizer context to MOCK templates

---------

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-13 15:37:17 -04:00
1 parent a5045d3aef
commit 2290247802
8 files changed
+486 -25

No files matched your search

@@ -413,8 +413,9 @@ public class ApiGatewayExecuteController {
matched, stage, integration, headers, uriInfo, body, authorizerResult, resolvedApiKey,
iamIdentity);
case "AWS" -> invokeAwsIntegration(region, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body);
case "MOCK" -> invokeMock(region, httpMethod, path, stageName, matched, integration, headers, uriInfo, body);
matched, integration, headers, uriInfo, body, authorizerResult);
case "MOCK" -> invokeMock(region, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body, authorizerResult);
default -> Response.status(500)
.entity(jsonMessage("Unsupported integration type: " + integration.getType()))
.type(MediaType.APPLICATION_JSON).build();
@@ -993,10 +994,27 @@ public class ApiGatewayExecuteController {
return params;
}
static Map<String, Object> vtlAuthorizerContext(
String principalId, Map<String, Object> authorizerContext) {
Map<String, Object> result = new HashMap<>();
if (authorizerContext != null) {
authorizerContext.forEach((key, value) -> {
if (value != null) {
result.put(key, value.toString());
}
});
}
if (principalId != null) {
result.put("principalId", principalId);
}
return result.isEmpty() ? null : result;
}
private Response invokeAwsIntegration(String region, String httpMethod, String path,
String stageName, ApiGatewayResource resource,
Integration integration, HttpHeaders headers,
UriInfo uriInfo, byte[] body) {
UriInfo uriInfo, byte[] body,
AuthorizerResult authorizerResult) {
AwsServiceRouter.IntegrationTarget target = serviceRouter.parseIntegrationUri(integration.getUri());
if (target == null) {
return Response.status(500)
@@ -1022,10 +1040,13 @@ public class ApiGatewayExecuteController {
String incomingContentType = headerMap.getOrDefault("Content-Type",
headerMap.getOrDefault("content-type", "application/json"));
Map<String, Object> vtlAuthorizerContext = vtlAuthorizerContext(
authorizerResult.principalId(), authorizerResult.context());
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
bodyStr, headerMap, queryMap, pathMap, stageName, httpMethod,
resource.getPath(), requestId, regionResolver.getAccountId(), null);
resource.getPath(), requestId, regionResolver.getAccountId(), null,
vtlAuthorizerContext);
// AWS selects the request template by the *incoming* request Content-Type. Capture it
// before parameter mapping runs, since an integration.request.header.Content-Type
@@ -1244,7 +1265,8 @@ public class ApiGatewayExecuteController {
if (responseTemplate != null && !responseTemplate.isEmpty()) {
VtlTemplateEngine.VtlContext responseMappingCtx = new VtlTemplateEngine.VtlContext(
responseBodyStr, headerMap, queryMap, pathMap, stageName, httpMethod,
resource.getPath(), requestId, regionResolver.getAccountId(), null);
resource.getPath(), requestId, regionResolver.getAccountId(), null,
vtlAuthorizerContext);
templateResult = vtlEngine.evaluate(responseTemplate, responseMappingCtx);
finalBody = templateResult.body();
} else {
@@ -1359,7 +1381,8 @@ public class ApiGatewayExecuteController {
private Response invokeMock(String region, String httpMethod, String path, String stageName,
ApiGatewayResource resource, Integration integration,
HttpHeaders headers, UriInfo uriInfo, byte[] body) {
HttpHeaders headers, UriInfo uriInfo, byte[] body,
AuthorizerResult authorizerResult) {
String requestId = UUID.randomUUID().toString();
String bodyStr = body != null && body.length > 0 ? new String(body) : null;
@@ -1380,7 +1403,8 @@ public class ApiGatewayExecuteController {
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
bodyStr, headerMap, queryMap, pathMap, stageName, httpMethod,
resource.getPath(), requestId, regionResolver.getAccountId(), null);
resource.getPath(), requestId, regionResolver.getAccountId(), null,
vtlAuthorizerContext(authorizerResult.principalId(), authorizerResult.context()));
// A MOCK has no backend: the request template *is* the integration response, and the
// "statusCode" it renders is what the integration responses' selectionPatterns are
@@ -31,9 +31,11 @@ import java.io.StringWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Evaluates AWS API Gateway VTL (Velocity Template Language) mapping templates.
@@ -44,6 +46,7 @@ import java.util.Map;
@ApplicationScoped
@RegisterForReflection(targets = {
VtlTemplateEngine.InputVariable.class,
VtlTemplateEngine.ParameterMap.class,
VtlTemplateEngine.UtilVariable.class,
VtlTemplateEngine.ResponseOverride.class,
UberspectImpl.class,
@@ -131,6 +134,10 @@ public class VtlTemplateEngine {
identity.put("sourceIp", "127.0.0.1");
map.put("identity", identity);
if (ctx.authorizer() != null) {
map.put("authorizer", ctx.authorizer());
}
map.put("responseOverride", responseOverride);
return map;
@@ -181,7 +188,8 @@ public class VtlTemplateEngine {
String resourcePath,
String requestId,
String accountId,
Map<String, String> stageVariables
Map<String, String> stageVariables,
Map<String, Object> authorizer
) {}
/**
@@ -252,9 +260,9 @@ public class VtlTemplateEngine {
/** Returns request parameters organized by type. */
public Map<String, Map<String, String>> params() {
Map<String, Map<String, String>> params = new HashMap<>();
params.put("querystring", ctx.queryParams() != null ? ctx.queryParams() : Map.of());
params.put("path", ctx.pathParams() != null ? ctx.pathParams() : Map.of());
params.put("header", ctx.headers() != null ? ctx.headers() : Map.of());
params.put("querystring", new ParameterMap(ctx.queryParams()));
params.put("path", new ParameterMap(ctx.pathParams()));
params.put("header", new ParameterMap(ctx.headers()));
return params;
}
@@ -289,6 +297,31 @@ public class VtlTemplateEngine {
}
}
/** Reflection-safe map exposed to Velocity templates in native images. */
public static class ParameterMap extends AbstractMap<String, String> {
private final Map<String, String> values;
public ParameterMap(Map<String, String> values) {
this.values = values != null ? values : Map.of();
}
@Override
public String get(Object key) {
return values.get(key);
}
@Override
public Set<String> keySet() {
return values.keySet();
}
@Override
public Set<Entry<String, String>> entrySet() {
return values.entrySet();
}
}
/**
* The {@code $util} variable available in API Gateway VTL templates.
*/
@@ -410,7 +410,7 @@ public class WebSocketIntegrationInvoker {
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
eventJson, Map.of(), Map.of(), Map.of(), "", "", "", "", "000000000000",
stageVariables != null ? stageVariables : Map.of());
stageVariables != null ? stageVariables : Map.of(), null);
VtlTemplateEngine.EvaluateResult result = vtlEngine.evaluate(template, vtlCtx);
return result.body();
@@ -450,7 +450,7 @@ public class WebSocketIntegrationInvoker {
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
responseBody, Map.of(), Map.of(), Map.of(), "", "", "", "", "000000000000",
stageVariables != null ? stageVariables : Map.of());
stageVariables != null ? stageVariables : Map.of(), null);
VtlTemplateEngine.EvaluateResult result = vtlEngine.evaluate(template, vtlCtx);
return result.body();