mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): build shared $context values in one helper and match HTTP API extendedRequestId to requestId (#8191)
This commit is contained in:
1 parent
b07c49082e
commit
39e9cd9d1e
6 files changed
+174
-44
No files matched your search
@@ -1399,8 +1399,9 @@ public class ApiGatewayExecuteController {
|
||||
Map<String, String> stageVars = stage != null ? stage.getVariables() : null;
|
||||
putStageVariables(node, stageVars);
|
||||
|
||||
GatewayContextValues values = gatewayContextValues(apiId, region, headers);
|
||||
ObjectNode ctx = node.putObject("requestContext");
|
||||
ctx.put("accountId", regionResolver.getAccountId());
|
||||
ctx.put("accountId", values.accountId());
|
||||
ctx.put("apiId", apiId);
|
||||
ctx.put("resourceId", resourceId != null ? resourceId : "");
|
||||
ctx.put("resourcePath", resourcePath);
|
||||
@@ -1408,12 +1409,11 @@ public class ApiGatewayExecuteController {
|
||||
ctx.put("httpMethod", httpMethod);
|
||||
ctx.put("stage", stageName);
|
||||
ctx.put("requestId", routeContext.requestId(headers));
|
||||
ctx.put("requestTimeEpoch", routeContext.requestTime().toEpochMilli());
|
||||
ctx.put("requestTime", GATEWAY_REQUEST_TIME.format(routeContext.requestTime().atZone(ZoneOffset.UTC)));
|
||||
ctx.put("extendedRequestId", routeContext.extendedRequestId());
|
||||
String domain = restDomainName(apiId, region, headers);
|
||||
ctx.put("domainName", domain);
|
||||
ctx.put("domainPrefix", domain != null ? domain.split("\\.")[0] : null);
|
||||
ctx.put("requestTimeEpoch", values.requestTimeEpoch());
|
||||
ctx.put("requestTime", values.requestTime());
|
||||
ctx.put("extendedRequestId", values.extendedRequestId());
|
||||
ctx.put("domainName", values.domainName());
|
||||
ctx.put("domainPrefix", values.domainPrefix());
|
||||
ctx.put("protocol", "HTTP/1.1");
|
||||
|
||||
// identity.apiKey / identity.apiKeyId: resolve from usage plans linked to this (apiId, stage)
|
||||
@@ -1526,22 +1526,20 @@ public class ApiGatewayExecuteController {
|
||||
putStageVariables(event, stageVars);
|
||||
|
||||
String arnRegion = region != null ? region : regionResolver.getDefaultRegion();
|
||||
String domainName = restDomainName(apiId, arnRegion, headers);
|
||||
long nowMillis = routeContext.requestTime().toEpochMilli();
|
||||
String requestTime = GATEWAY_REQUEST_TIME.format(routeContext.requestTime().atZone(ZoneOffset.UTC));
|
||||
GatewayContextValues values = gatewayContextValues(apiId, arnRegion, headers);
|
||||
|
||||
ObjectNode ctx = event.putObject("requestContext");
|
||||
ctx.put("accountId", regionResolver.getAccountId());
|
||||
ctx.put("accountId", values.accountId());
|
||||
ctx.put("apiId", apiId);
|
||||
ctx.put("domainName", domainName);
|
||||
ctx.put("domainPrefix", domainName.split("\\.")[0]);
|
||||
ctx.put("extendedRequestId", routeContext.extendedRequestId());
|
||||
ctx.put("domainName", values.domainName());
|
||||
ctx.put("domainPrefix", values.domainPrefix());
|
||||
ctx.put("extendedRequestId", values.extendedRequestId());
|
||||
ctx.put("httpMethod", httpMethod);
|
||||
ctx.put("path", requestPath);
|
||||
ctx.put("protocol", "HTTP/1.1");
|
||||
ctx.put("requestId", requestId);
|
||||
ctx.put("requestTime", requestTime);
|
||||
ctx.put("requestTimeEpoch", nowMillis);
|
||||
ctx.put("requestTime", values.requestTime());
|
||||
ctx.put("requestTimeEpoch", values.requestTimeEpoch());
|
||||
ctx.put("resourceId", resourceId != null ? resourceId : "");
|
||||
ctx.put("resourcePath", resourcePath);
|
||||
ctx.put("stage", stageName);
|
||||
@@ -2306,20 +2304,20 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
private Map<String, Object> restRequestContext(GatewayResponseScope scope, String requestId) {
|
||||
String region = scope.region() != null ? scope.region() : regionResolver.getDefaultRegion();
|
||||
String domain = restDomainName(scope.apiId(), region, scope.headers());
|
||||
GatewayContextValues values = gatewayContextValues(scope.apiId(), region, scope.headers());
|
||||
Map<String, Object> context = new LinkedHashMap<>();
|
||||
context.put("accountId", regionResolver.getAccountId());
|
||||
context.put("accountId", values.accountId());
|
||||
context.put("apiId", scope.apiId());
|
||||
context.put("deploymentId", scope.stage() != null ? scope.stage().getDeploymentId() : null);
|
||||
context.put("domainName", domain);
|
||||
context.put("domainPrefix", domain != null ? domain.split("\\.")[0] : null);
|
||||
context.put("extendedRequestId", routeContext.extendedRequestId());
|
||||
context.put("domainName", values.domainName());
|
||||
context.put("domainPrefix", values.domainPrefix());
|
||||
context.put("extendedRequestId", values.extendedRequestId());
|
||||
context.put("httpMethod", scope.httpMethod());
|
||||
context.put("path", "/" + scope.stageName() + scope.path());
|
||||
context.put("protocol", "HTTP/1.1");
|
||||
context.put("requestId", requestId);
|
||||
context.put("requestTime", GATEWAY_REQUEST_TIME.format(routeContext.requestTime().atZone(ZoneOffset.UTC)));
|
||||
context.put("requestTimeEpoch", routeContext.requestTime().toEpochMilli());
|
||||
context.put("requestTime", values.requestTime());
|
||||
context.put("requestTimeEpoch", values.requestTimeEpoch());
|
||||
context.put("resourceId", scope.resource() != null ? scope.resource().getId() : "");
|
||||
context.put("resourcePath", scope.resource() != null ? scope.resource().getPath() : "");
|
||||
context.put("stage", scope.stageName());
|
||||
@@ -2327,6 +2325,22 @@ public class ApiGatewayExecuteController {
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code $context} values REST and HTTP APIs build the same way: the account, the domain the caller
|
||||
* addressed, the gateway-generated request id and the request time. An HTTP API reports its request id as
|
||||
* {@code extendedRequestId} instead.
|
||||
*/
|
||||
private record GatewayContextValues(String accountId, String domainName, String domainPrefix,
|
||||
String extendedRequestId, String requestTime, long requestTimeEpoch) {}
|
||||
|
||||
private GatewayContextValues gatewayContextValues(String apiId, String region, HttpHeaders headers) {
|
||||
Instant requestTime = routeContext.requestTime();
|
||||
String domain = restDomainName(apiId, region, headers);
|
||||
return new GatewayContextValues(regionResolver.getAccountId(), domain, domain.split("\\.")[0],
|
||||
routeContext.extendedRequestId(), GATEWAY_REQUEST_TIME.format(requestTime.atZone(ZoneOffset.UTC)),
|
||||
requestTime.toEpochMilli());
|
||||
}
|
||||
|
||||
private String restDomainName(String apiId, String region, HttpHeaders headers) {
|
||||
String host = headers != null ? headers.getHeaderString("Host") : null;
|
||||
if (host != null) {
|
||||
@@ -3018,7 +3032,7 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
String hostRegion = region != null ? region : regionResolver.getDefaultRegion();
|
||||
Map<String, String> contextVariables =
|
||||
httpApiContextVariables(apiId, stageName, httpMethod, path, route, hostRegion, headers);
|
||||
httpApiContextVariables(apiId, stageName, httpMethod, path, route, requestId, hostRegion, headers);
|
||||
|
||||
LOG.debugv("execute-api v2: {0} {1}/{2}{3} → {4}", httpMethod, apiId, stageName, path,
|
||||
subtype.subtypeName());
|
||||
@@ -3049,21 +3063,20 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
/** The {@code $context.<name>} values an HTTP API parameter mapping can read for this request. */
|
||||
private Map<String, String> httpApiContextVariables(String apiId, String stageName, String httpMethod,
|
||||
String path, Route route, String region,
|
||||
HttpHeaders headers) {
|
||||
Instant requestTime = routeContext.requestTime();
|
||||
String domain = restDomainName(apiId, region, headers);
|
||||
String path, Route route, String requestId,
|
||||
String region, HttpHeaders headers) {
|
||||
GatewayContextValues values = gatewayContextValues(apiId, region, headers);
|
||||
Map<String, String> contextVariables = new LinkedHashMap<>();
|
||||
contextVariables.put("accountId", regionResolver.getAccountId());
|
||||
contextVariables.put("accountId", values.accountId());
|
||||
contextVariables.put("apiId", apiId);
|
||||
contextVariables.put("domainName", domain);
|
||||
contextVariables.put("domainPrefix", domain.split("\\.")[0]);
|
||||
contextVariables.put("extendedRequestId", routeContext.extendedRequestId());
|
||||
contextVariables.put("domainName", values.domainName());
|
||||
contextVariables.put("domainPrefix", values.domainPrefix());
|
||||
contextVariables.put("extendedRequestId", requestId);
|
||||
contextVariables.put("httpMethod", httpMethod);
|
||||
contextVariables.put("path", ("$default".equals(stageName) ? "" : "/" + stageName) + path);
|
||||
contextVariables.put("protocol", "HTTP/1.1");
|
||||
contextVariables.put("requestTime", GATEWAY_REQUEST_TIME.format(requestTime.atZone(ZoneOffset.UTC)));
|
||||
contextVariables.put("requestTimeEpoch", Long.toString(requestTime.toEpochMilli()));
|
||||
contextVariables.put("requestTime", values.requestTime());
|
||||
contextVariables.put("requestTimeEpoch", Long.toString(values.requestTimeEpoch()));
|
||||
contextVariables.put("routeKey", route.getRouteKey());
|
||||
contextVariables.put("stage", stageName);
|
||||
return contextVariables;
|
||||
@@ -3176,15 +3189,17 @@ public class ApiGatewayExecuteController {
|
||||
// $request.header.* mappings keep the caller's values; only what reaches the backend changes.
|
||||
Map<String, List<String>> backendHeaders = HttpApiForwardedHeaders.apply(multiValueHeaders, sourceIp,
|
||||
routeContext.localIp(), headers.getHeaderString("Host"), isSecureTransport(uriInfo) ? "https" : "http");
|
||||
String requestId = routeContext.requestId(headers);
|
||||
io.floci.aws.services.apigatewayv2.proxy.RequestContext ctx =
|
||||
new io.floci.aws.services.apigatewayv2.proxy.RequestContext(
|
||||
apiId, stageName, httpMethod, path,
|
||||
pathParams.getOrDefault("proxy", ""), route.getRouteKey(),
|
||||
UUID.randomUUID().toString(), sourceIp,
|
||||
requestId, sourceIp,
|
||||
requestHeaders, queryParams, pathParams, body,
|
||||
claims, authorizerContext, backendHeaders, multiValueQueryParams, rawPathParams)
|
||||
.withVariables(stageVariables, httpApiContextVariables(apiId, stageName, httpMethod, path,
|
||||
route, region != null ? region : regionResolver.getDefaultRegion(), headers));
|
||||
route, requestId, region != null ? region : regionResolver.getDefaultRegion(),
|
||||
headers));
|
||||
|
||||
LOG.debugv("execute-api v2: {0} {1}/{2}{3} → HTTP_PROXY {4}",
|
||||
httpMethod, apiId, stageName, path, effective.getIntegrationUri());
|
||||
@@ -3196,12 +3211,15 @@ public class ApiGatewayExecuteController {
|
||||
if (result.body() != null) rb.entity(result.body());
|
||||
if (result.headers() != null) {
|
||||
for (Map.Entry<String, List<String>> e : result.headers().entrySet()) {
|
||||
if ("x-amzn-RequestId".equalsIgnoreCase(e.getKey())) {
|
||||
continue;
|
||||
}
|
||||
for (String value : e.getValue()) {
|
||||
rb.header(e.getKey(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return rb.build();
|
||||
return rb.header("x-amzn-RequestId", requestId).build();
|
||||
}
|
||||
|
||||
private static boolean isCloudMapIntegration(
|
||||
|
||||
Reference in new issue
Block a user