diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 9bb33ab3b3..4ab8095272 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -867,6 +867,10 @@ query names are case-sensitive. No configured sources produces an empty array or string. A missing or empty configured value returns `401` without invoking Lambda in either format. Repeated request values use each event's delivered representation: last value in format `1.0`, comma-joined values in format `2.0`. +The authorizer event's source IP (`requestContext.http.sourceIp` in format `2.0`, +`requestContext.identity.sourceIp` in format `1.0`) and the Lambda proxy event's +`requestContext.http.sourceIp` are the address of the TCP connection to Floci, as in +`$context.identity.sourceIp`; a client's `X-Forwarded-For` header does not change them. HTTP API `HTTP_PROXY` integrations can map values from a successful Lambda authorizer's context into request headers, query strings, and paths. For a returned diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java index d4b1660d86..f6689da074 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java @@ -3712,7 +3712,7 @@ public class ApiGatewayExecuteController { ctx.put("domainName", AwsEndpoints.executeApiHost(apiId, arnRegion)); ctx.put("domainPrefix", apiId); ObjectNode identity = ctx.putObject("identity"); - identity.put("sourceIp", "127.0.0.1"); + identity.put("sourceIp", routeContext.sourceIp()); identity.put("userAgent", headers.getHeaderString("User-Agent") != null ? headers.getHeaderString("User-Agent") : ""); return event; @@ -3762,7 +3762,7 @@ public class ApiGatewayExecuteController { http.put("method", httpMethod); http.put("path", preservedPath); http.put("protocol", "HTTP/1.1"); - http.put("sourceIp", "127.0.0.1"); + http.put("sourceIp", routeContext.sourceIp()); http.put("userAgent", headers.getHeaderString("User-Agent") != null ? headers.getHeaderString("User-Agent") : ""); @@ -4131,7 +4131,7 @@ public class ApiGatewayExecuteController { http.put("method", httpMethod); http.put("path", preservedPath); http.put("protocol", "HTTP/1.1"); - http.put("sourceIp", "127.0.0.1"); + http.put("sourceIp", routeContext.sourceIp()); http.put("userAgent", headers.getHeaderString("User-Agent") != null ? headers.getHeaderString("User-Agent") : ""); diff --git a/src/test/java/io/floci/aws/services/apigateway/HttpApiAuthorizerPolicyTest.java b/src/test/java/io/floci/aws/services/apigateway/HttpApiAuthorizerPolicyTest.java index 889801cbc4..4abf16c9ed 100644 --- a/src/test/java/io/floci/aws/services/apigateway/HttpApiAuthorizerPolicyTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/HttpApiAuthorizerPolicyTest.java @@ -32,6 +32,7 @@ import org.junit.jupiter.params.provider.ValueSource; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -118,6 +119,38 @@ class HttpApiAuthorizerPolicyTest { assertPolicy("2.0", statements, 403); } + @ParameterizedTest + @CsvSource({ + "1.0, 203.0.113.42, /requestContext/identity/sourceIp", + "1.0, 2001:db8::42, /requestContext/identity/sourceIp", + "2.0, 203.0.113.42, /requestContext/http/sourceIp", + "2.0, 2001:db8::42, /requestContext/http/sourceIp" + }) + void authorizerAndProxyEventsCarryTransportAddress(String version, String sourceIp, String authorizerField) { + when(remoteAddress.hostAddress()).thenReturn(sourceIp); + when(headers.getHeaderString("X-Forwarded-For")).thenReturn("198.51.100.42"); + AtomicReference authorizerEvent = new AtomicReference<>(); + AtomicReference backendEvent = new AtomicReference<>(); + when(lambda.invokeTarget(eq("us-east-1"), eq("arn:aws:lambda:us-east-1:000000000000:function:" + "authorizer"), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenAnswer(invocation -> { + authorizerEvent.set(mapper.readTree((byte[]) invocation.getArgument(2))); + return result("{\"principalId\":\"user\",\"policyDocument\":{\"Version\":\"2012-10-17\"," + + "\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}}"); + }); + when(lambda.invokeTarget(eq("us-east-1"), eq("arn:aws:lambda:us-east-1:000000000000:function:" + "backend"), any(byte[].class), eq(InvocationType.RequestResponse))) + .thenAnswer(invocation -> { + backendEvent.set(mapper.readTree((byte[]) invocation.getArgument(2))); + return result("{\"statusCode\":200,\"body\":\"allowed\"}"); + }); + + try (Response response = controller(version, false).handleGet(headers, uri, "api", "test", "items")) { + assertEquals(200, response.getStatus(), "payload version " + version); + } + + assertEquals(sourceIp, authorizerEvent.get().at(authorizerField).asText()); + assertEquals(sourceIp, backendEvent.get().at("/requestContext/http/sourceIp").asText()); + } + @ParameterizedTest @CsvSource({ "1.0, 2001:db8::42, 403",