Bug: APIGatewayProxyEventModel drops the Lambda authorizer context

未关闭 适合新手
#8,801 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
aws, python
领域
backend, cloud

调研方向

阅读 APIGatewayEventAuthorizer,并检查 tests/events/apiGatewayProxyEventPrincipalId.json,将其中的 authorizer 字段与解析后的模型进行比较。首先检查解析器模型测试,并为 principalId、integrationLatency 以及 user_id 等自定义上下文键添加覆盖。完成的标准是模型保留这些值,同时现有的 Cognito authorizer 事件仍能正常解析。

由索引模型根据 Issue 内容生成。

描述

Expected Behaviour

When a REST API uses a Lambda authorizer, API Gateway puts the authorizer's principalId, the integrationLatency and every key from the authorizer's context map into requestContext.authorizer. APIGatewayProxyEventModel should keep them, so a handler can read something like a tenant ID the authorizer set. The Lambda authorizer output docs cover the context map.

Current Behaviour

APIGatewayEventAuthorizer only declares claims and scopes, so everything else in requestContext.authorizer is dropped. The repo's tests/events/apiGatewayProxyEventPrincipalId.json has this shape (principalId, integrationLatency and a custom user_id).

The APIGatewayProxyEvent data class already exposes these through principal_id, integration_latency and get_context(), and the TypeScript schema keeps them since aws-powertools/powertools-lambda-typescript#5749.

Code snippet
import json

from aws_lambda_powertools.utilities.parser import parse
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel

with open("tests/events/apiGatewayProxyEvent.json") as f:
    event = json.load(f)

# What API Gateway sends when a Lambda authorizer returns principalId "user-123"
# and context {"tenantId": "t-9"}
event["requestContext"]["authorizer"] = {
    "principalId": "user-123",
    "integrationLatency": 42,
    "tenantId": "t-9",
}

parsed = parse(event=event, model=APIGatewayProxyEventModel)
print(parsed.requestContext.authorizer)
claims=None scopes=None
Possible Solution

Declare the two fields API Gateway always sends and allow extra keys for the custom context:

class APIGatewayEventAuthorizer(BaseModel):
    model_config = ConfigDict(extra="allow")

    claims: dict[str, Any] | None = None
    scopes: list[str] | None = None
    principalId: str | None = None
    integrationLatency: int | None = None

With that, the snippet prints principalId='user-123' integrationLatency=42 tenantId='t-9' and the custom keys are in model_extra. It only adds fields, so Cognito authorizer events parse the same as before.

I'm happy to send a PR with tests if that sounds good.

Steps to Reproduce

Run the snippet above from the repo root.

Powertools for AWS Lambda (Python) version

3.40.0

AWS Lambda function runtime

3.13

Packaging format used

PyPi

主要语言
Python
星标
3.3k
派生
511
平均合并
3 天 8 小时
30 天内合并 PR
103

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws-powertools/powertools-lambda-python 的其他 Issue

查看 aws-powertools/powertools-lambda-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。