Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpContext is null for AWS Lambda Function Handler (Executable assembly) #1665

Closed
vladyslav-sosnov opened this issue Jan 29, 2024 · 7 comments
Labels
bug This issue is a bug. module/aspnetcore-support needs-reproduction This issue needs reproduction.

Comments

@vladyslav-sosnov
Copy link

Describe the bug

HttpContext is always null after it has been injected into AWS Lambda Function Handler. For comparison, it is populated while using the MinimalAPI approach.

Expected Behavior

HttpContext is populated with Items (and Session data when AWS DynamoDB session provider is used)

Current Behavior

HttpContext object is always null when injected in the AWS Lambda Function Handler.

Reproduction Steps

Code sample illustrating the absence of HttpContext.

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Microsoft.AspNetCore.DataProtection;

var authorizerSettings = new SessionAuthorizerSettings();
var serviceCollection = new ServiceCollection();

serviceCollection.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

serviceCollection.AddSingleton<ISessionAuthorizerSettings, SessionAuthorizerSettings>();

serviceCollection
    .AddDataProtection()
    .SetApplicationName(authorizerSettings.DataProtectionAppName)
    .DisableAutomaticKeyGeneration();

serviceCollection
    .AddHttpContextAccessor();

serviceCollection
    .AddAWSDynamoDBDistributedCache(options =>
    {
        options.TableName = authorizerSettings.CacheTableName;
        options.PartitionKeyName = authorizerSettings.PartitionKeyName;
        options.TTLAttributeName = authorizerSettings.TTLAttributeName;
    })
    .AddSession(options =>
    {
        options.Cookie.Name = ".Cookie.Session";
        options.IdleTimeout = TimeSpan.FromMinutes(30);
        options.Cookie.HttpOnly = true;
        options.Cookie.IsEssential = true;
    });

var serviceProvider = serviceCollection.BuildServiceProvider();

var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();

var httpContext = httpContextAccessor?.HttpContext;

var handler = (APIGatewayHttpApiV2ProxyRequest request, ILambdaContext lambdaContext) =>
{
    var userId = "12345";

    // ISSUE: 'httpContext' is always null and therefore, the session can not be accessed.
    var session = httpContext.Session;
    var sessionData = ObjectSerializer.Deserialize<SessionPermissions>(session.GetString(userId));

    // An example of a typical response from the authorizer to invoke the requested Lambda.
    var response = new APIGatewayCustomAuthorizerResponse
    {
        PolicyDocument = new APIGatewayCustomAuthorizerPolicy
        {
            Version = "2012-10-17",
            Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
            {
                 new()
                 {
                     Action = new HashSet<string> {"execute-api:Invoke"},
                     Effect = "Allow",
                     Resource = new HashSet<string> { $"arn:aws:execute-api:{lambdaContext.InvokedFunctionArn}:{request.RequestContext.AccountId}:{request.RequestContext.ApiId}/$default/*" }
                 }
            },
        },
        PrincipalID = userId,
        Context = new APIGatewayCustomAuthorizerContextOutput
        {
            ["data"] = sessionData.Data
        }
    };
};

await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

Possible Solution

No response

Additional Information/Context

Alternatively, a Minimal API approach was trialed where HttpContext was populated with Items and Session data, however, it
returned a response that is incorrect for a Custom Authorizer (Raised in a separate issue)

Context
HttpContext is required to access session data.

For this purpose, AWS DynamoDB session provider has been integrated and API Gateway HTTP Payload v2.0 was used.

Apart from the use of Function Handler, the Lambda Entry Point approach was used. This approach faces the same issue as the Function Handler, where HttpContext is null.
Amazon.Lambda.AspNetCoreServer

AWS .NET SDK and/or Package version used

  • Amazon.Lambda.AspNetCoreServer.Hosting, Version="1.6.1"
  • AWS.AspNetCore.DistributedCacheProvider, Version="0.9.2"
  • AWSSDK.S3, Version="3.7.103.37"

Targeted .NET Platform

.Net 6

Operating System and version

AWS Lambda

@vladyslav-sosnov vladyslav-sosnov added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 29, 2024
@bhoradc bhoradc added needs-reproduction This issue needs reproduction. module/aspnetcore-support and removed needs-triage This issue or PR still needs to be triaged. labels Mar 5, 2024
@ashishdhingra
Copy link
Contributor

@vladyslav-sosnov Good afternoon. I'm unsure if I understand your code, per my knowledge, HttpContext by default is available only in the context of web applications. Although your code calls serviceCollection.AddHttpContextAccessor();, this per HttpServiceCollectionExtensions.AddHttpContextAccessor, adds a default implementation for the IHttpContextAccessor service, which provides access to the current HttpContext, if one is available. The handler you are configuring is a simple Lambda function handler. Refer ASP.NET Core minimal APIs section at https://aws.amazon.com/blogs/compute/introducing-the-net-6-runtime-for-aws-lambda/ on how to use Amazon.Lambda.AspNetCoreServer.Hosting package.

CCing @normj for any inputs.

Thanks,
Ashish

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. label Mar 13, 2024
Copy link
Contributor

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 24, 2024
@vladyslav-sosnov
Copy link
Author

Hello, @ashishdhingra. Indeed, within this Lambda function handler, accessing HttpContext is not possible, which is the exact challenge I'm facing. I understand this might be by design, but I'm curious if there are any intentions to introduce HttpContext support. Alternatively, is there a method to inject it effectively so the context becomes available?

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. labels Mar 27, 2024
@normj
Copy link
Member

normj commented Mar 28, 2024

@vladyslav-sosnov There are no plans to add HttpContext support into the basic Lambda programming model. That is an ASP.NET Core concept that brings with it a lot of dependencies and concepts that we would not introduce into the basic Lambda programming model. What is it you need from HttpContext that isn't available in the APIGatewayHttpApiV2ProxyRequest.

@vladyslav-sosnov
Copy link
Author

@normj Hello! We need to access the session from AWS Dynamo DB Distributed Cache. Alternatively, if we use the Minimal API approach as described in #1666, we are already able to retrieve the session from the HttpContext but Minimal API does not return the correct Custom Authorizer response (as it wraps the response in the body property).

For this issue, the response is in the correct format (for a Custom Authorizer) but the session can not be retrieved.

Our preference, if possible, is to be able to use the Minimal API approach (#1666) if the response could be fixed to return the correct response for a Custom Authorizer.

@ashishdhingra
Copy link
Contributor

Per @normj in #1665 (comment), there are no plans to add HttpContext support into the basic Lambda programming model. Hence, closing this issue.

Copy link
Contributor

github-actions bot commented May 3, 2024

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/aspnetcore-support needs-reproduction This issue needs reproduction.
Projects
None yet
Development

No branches or pull requests

4 participants