Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(apigateway v1): include requestContext.identity in REQUEST authorizer events (#1837)
Yos Riadyc80fee8663c5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f485cbbc..72e3e62f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **API Gateway v1 — REQUEST authorizer events carry `requestContext.identity`** — the event sent to a REQUEST Lambda authorizer had no `identity` block, so an authorizer reading `event.requestContext.identity.sourceIp` or `.userAgent` crashed and every request answered 500 or its own fallback Deny. It now reports `sourceIp` (first `X-Forwarded-For` hop) and `userAgent`, the same values the `AWS_PROXY` integration event already carries. Checked against the API Gateway Developer Guide ("Input to an API Gateway Lambda authorizer", whose REQUEST example includes `requestContext.identity.sourceIp`, and the `$context.identity.userAgent` context variable); a production REQUEST authorizer that reads `identity.userAgent` works on AWS. Not validated against a captured AWS event.- **DynamoDB — `DescribeTable` accepts a table ARN** — `TableName` given as the table's ARN answered `ResourceNotFoundException`, where AWS accepts either form. It now resolves through the same ARN normalization the other table operations use, and an ARN from another account or region still answers `ResourceNotFoundException`. This unblocks OpenSearch Data Prepper's DynamoDB source, which always describes the table by ARN.- **S3 — event notification records match S3's** — the object block carries the key URL-encoded (`red flower.jpg` → `red+flower.jpg`), the `versionId` the write made or removed, and a `sequencer` that grows with every create and delete (it was always `"0"`); records are `eventVersion` `2.6`. A delete that leaves a delete marker is `ObjectRemoved:DeleteMarkerCreated` (it was `ObjectRemoved:Delete`, so a subscription to permanent deletes received markers), and `DeleteObjects` sends an event for each object it removes (it sent none). Checked against the S3 User Guide's "Event message structure" and "Event notification types and destinations" pages; not validated against a real AWS account.- **STS — `GetCallerIdentity` resolves IAM-user callers** — keys created with `CreateAccessKey` returned the `root` ARN; they now return the user's ARN and ID in both XML and JSON protocols. Under `AUTH=true`, unknown and inactive keys are rejected instead of reported as root.diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.pyindex a544727b..91894265 100644--- a/ministack/services/apigateway_v1.py+++ b/ministack/services/apigateway_v1.py@@ -1945,6 +1945,11 @@ async def _authorize_request_v1("httpMethod": method,"apiId": api_id,"accountId": owner_account_id,+ # Same caller fields the AWS_PROXY event reports.+ "identity": {+ "sourceIp": (_header_ci(headers, "x-forwarded-for") or "127.0.0.1").split(",")[0].strip(),+ "userAgent": _header_ci(headers, "user-agent") or "",+ },},}diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.pyindex dd04d938..ea5035b2 100644--- a/tests/test_apigatewayv1.py+++ b/tests/test_apigatewayv1.py@@ -3756,6 +3756,51 @@ def test_apigwv1_authorizer_cache_is_scoped_per_method_arn(apigw_v1, lam, sqs):_auth_delete_queue(sqs, qname)+_AUTH_IDENTITY_REQUEST_AUTHORIZER = (+ "def handler(event, context):\n"+ " identity = event['requestContext']['identity']\n"+ " return {\n"+ " 'principalId': 'u1',\n"+ " 'policyDocument': {'Version': '2012-10-17', 'Statement': [\n"+ " {'Action': 'execute-api:Invoke', 'Effect': 'Allow',\n"+ " 'Resource': event['methodArn']}]},\n"+ " 'context': {'sourceIp': identity['sourceIp'],\n"+ " 'userAgent': identity['userAgent']},\n"+ " }\n"+)+++def test_apigwv1_request_authorizer_event_has_identity(apigw_v1, lam):+ """A REQUEST authorizer event carries requestContext.identity, as in the+ AWS docs' REQUEST authorizer input example. Authorizers that read the+ caller's sourceIp or userAgent from it must not crash."""+ backend = _auth_make_lambda(lam, "be", _AUTH_ECHO_BACKEND)+ authz = _auth_make_lambda(lam, "req", _AUTH_IDENTITY_REQUEST_AUTHORIZER)+ api_id, _ = _auth_build_api(+ apigw_v1, backend,+ dict(name="req", type="REQUEST", authorizerUri=_auth_lambda_uri(authz),+ identitySource="method.request.header.Authorization",+ authorizerResultTtlInSeconds=0),+ )+ try:+ status, body = _auth_http(+ _auth_execute_url(api_id, "test", "secure"),+ headers={+ "Authorization": "anything",+ "User-Agent": "identity-test/1.0",+ "X-Forwarded-For": "203.0.113.7, 10.0.0.1",+ },+ )+ assert status == 200, body+ authorizer = json.loads(body)+ assert authorizer["sourceIp"] == "203.0.113.7"+ assert authorizer["userAgent"] == "identity-test/1.0"+ finally:+ _auth_drop_api(apigw_v1, api_id)+ _auth_drop_lambda(lam, backend)+ _auth_drop_lambda(lam, authz)++def test_apigwv1_authorizer_cache_is_scoped_per_stage(apigw_v1, lam, sqs):"""API Gateway caches authorizer results per stage: a verdict computed onone stage never answers the same token on another."""