diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs index 455fcf26a..2bda4d328 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Reflection; using System.Text; using System.Text.Encodings.Web; @@ -214,13 +215,14 @@ protected string ErrorReport(Exception e) } /// - /// Convert the JSON document received from API Gateway in the ASP.NET Core IHttpRequestFeature object. - /// IHttpRequestFeature is then passed into IHttpApplication to create the ASP.NET Core request objects. + /// Convert the JSON document received from API Gateway into the InvokeFeatures object. + /// InvokeFeatures is then passed into IHttpApplication to create the ASP.NET Core request objects. /// - /// + /// /// - protected void MarshallRequest(IHttpRequestFeature requestFeatures, APIGatewayProxyRequest apiGatewayRequest) + protected void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest) { + var requestFeatures = (IHttpRequestFeature)features; requestFeatures.Scheme = "https"; requestFeatures.Path = apiGatewayRequest.Path; requestFeatures.Method = apiGatewayRequest.HttpMethod; @@ -273,6 +275,13 @@ protected void MarshallRequest(IHttpRequestFeature requestFeatures, APIGatewayPr } requestFeatures.Body = new MemoryStream(binaryBody); } + // set up connection features + var connectionFeatures = (IHttpConnectionFeature)features; + connectionFeatures.RemoteIpAddress = IPAddress.Parse(apiGatewayRequest.RequestContext.Identity.SourceIp); + if (apiGatewayRequest.Headers?.ContainsKey("X-Forwarded-Port") == true) + { + connectionFeatures.RemotePort = int.Parse(apiGatewayRequest.Headers["X-Forwarded-Port"]); + } } /// diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/InvokeFeatures.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/InvokeFeatures.cs index a2ab35833..017b91237 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/InvokeFeatures.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/InvokeFeatures.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -14,11 +15,11 @@ namespace Amazon.Lambda.AspNetCoreServer.Internal { public class InvokeFeatures : IFeatureCollection, IHttpRequestFeature, - IHttpResponseFeature + IHttpResponseFeature, + IHttpConnectionFeature /* , IHttpUpgradeFeature, - IHttpConnectionFeature, IHttpRequestLifetimeFeature*/ { @@ -26,6 +27,7 @@ public InvokeFeatures() { _features[typeof(IHttpRequestFeature)] = this; _features[typeof(IHttpResponseFeature)] = this; + _features[typeof(IHttpConnectionFeature)] = this; } #region IFeatureCollection @@ -146,5 +148,18 @@ void IHttpResponseFeature.OnCompleted(Func callback, object state) } #endregion + #region IHttpConnectionFeature + + string IHttpConnectionFeature.ConnectionId { get; set; } + + IPAddress IHttpConnectionFeature.RemoteIpAddress { get; set; } + + IPAddress IHttpConnectionFeature.LocalIpAddress { get; set; } + + int IHttpConnectionFeature.RemotePort { get; set; } + + int IHttpConnectionFeature.LocalPort { get; set; } + + #endregion } } diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/swagger-get-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/swagger-get-apigatway-request.json index 5d3b6939d..8c9de0dc8 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/swagger-get-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/swagger-get-apigatway-request.json @@ -19,7 +19,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-aggregateerror-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-aggregateerror-apigatway-request.json index 811a992ae..9e7bb6500 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-aggregateerror-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-aggregateerror-apigatway-request.json @@ -21,7 +21,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-all-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-all-apigatway-request.json index 8a097aa80..de3494856 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-all-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-all-apigatway-request.json @@ -19,7 +19,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json index 14295ebcb..e03c1bf2a 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json @@ -19,7 +19,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-error-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-error-apigatway-request.json index e058b0ab1..7277bdce1 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-error-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-error-apigatway-request.json @@ -21,7 +21,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-querystring-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-querystring-apigatway-request.json index 5700b8573..48caafb53 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-querystring-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-querystring-apigatway-request.json @@ -22,7 +22,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-single-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-single-apigatway-request.json index 25fe05b72..b7273e2b3 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-single-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-single-apigatway-request.json @@ -19,7 +19,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-typeloaderror-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-typeloaderror-apigatway-request.json index 0b0c57256..42abac228 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-typeloaderror-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-typeloaderror-apigatway-request.json @@ -21,7 +21,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-put-withbody-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-put-withbody-apigatway-request.json index 707484561..7c67040f3 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-put-withbody-apigatway-request.json +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-put-withbody-apigatway-request.json @@ -19,7 +19,7 @@ "cognitoIdentityId": null, "caller": "BBBBBBBBBBBB", "apiKey": "test-invoke-api-key", - "sourceIp": "test-invoke-source-ip", + "sourceIp": "127.0.0.1", "cognitoAuthenticationType": null, "cognitoAuthenticationProvider": null, "userArn": "arn:aws:iam::AAAAAAAAAAAA:root",