Bug: APIGatewayProxyEventModel drops the Lambda authorizer context

Aperta Adatta ai principianti
#8,801 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
aws, python
Ambito
backend, cloud

Direzione di ricerca

Leggi APIGatewayEventAuthorizer ed esamina tests/events/apiGatewayProxyEventPrincipalId.json per confrontare i suoi campi dell’authorizer con il modello analizzato. Inizia controllando i test del modello del parser e aggiungi copertura per principalId, integrationLatency e le chiavi di contesto personalizzate come user_id. Il lavoro è completato quando il modello conserva questi valori e gli eventi Cognito esistenti dell’authorizer continuano a essere analizzati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
3.3k
Fork
511
Merge medio
3g 8h
PR unite (30g)
103

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di aws-powertools/powertools-lambda-python

Tutte le issue di aws-powertools/powertools-lambda-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.