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(apigatewayv2): resolve JWKS via OIDC discovery for non-Cognito issuers (#802) (#813)
Pratham Israni6d0141032240
diff --git a/ministack/services/apigateway.py b/ministack/services/apigateway.pyindex 4505dc50..0f3a62bb 100644--- a/ministack/services/apigateway.py+++ b/ministack/services/apigateway.py@@ -87,6 +87,8 @@ async def _urlopen_async(request_or_url, timeout_seconds: float):REGION = os.environ.get("MINISTACK_REGION", "us-east-1")_PROXY_TIMEOUT_SECONDS = _timeout_from_env("MINISTACK_APIGW_PROXY_TIMEOUT_SECONDS", 30.0)_JWKS_TIMEOUT_SECONDS = _timeout_from_env("MINISTACK_APIGW_JWKS_TIMEOUT_SECONDS", 5.0)+_OIDC_TIMEOUT_SECONDS = _timeout_from_env("MINISTACK_APIGW_OIDC_TIMEOUT_SECONDS", 5.0)+_OIDC_CACHE_TTL_SECONDS = _timeout_from_env("MINISTACK_APIGW_OIDC_CACHE_TTL_SECONDS", 7200.0)# ---- Module-level state ----_apis = AccountScopedDict() # api_id -> api object@@ -102,6 +104,10 @@ _integration_responses = AccountScopedDict() # api_id -> {integration_id -> {i# to per-account local Cognito user pools — the same URL string may legitimately# serve different keys in different accounts._jwks_cache = AccountScopedDict()+# OIDC discovery documents ({issuer}/.well-known/openid-configuration) are cached+# by issuer (account-scoped, like _jwks_cache) so the jwks_uri lookup does not+# hit the network on every request.+_oidc_config_cache = AccountScopedDict()# WebSocket connection registry — connections are not per-account-scoped at the store level# because the @connections management API may arrive on any host/account; instead we store@@ -493,7 +499,30 @@ def _extract_token_from_identity_source(identity_source, headers: dict, query_pareturn None-def _resolve_jwks_url(authorizer: dict) -> str | None:+async def _fetch_oidc_jwks_uri(issuer: str) -> str | None:+ """Resolve an issuer's jwks_uri via OIDC discovery, cached per issuer.++ Reads {issuer}/.well-known/openid-configuration and returns its jwks_uri.+ Returns None (and caches the miss) if discovery is unavailable so callers+ can fall back to the conventional path.+ """+ issuer = issuer.rstrip("/")+ cached = _oidc_config_cache.get(issuer)+ now = time.time()+ if cached and cached.get("expiresAt", 0) > now:+ return cached.get("jwks_uri")+ jwks_uri = None+ try:+ url = f"{issuer}/.well-known/openid-configuration"+ _, _, body = await _urlopen_async(url, _OIDC_TIMEOUT_SECONDS)+ jwks_uri = (json.loads(body or b"{}") or {}).get("jwks_uri")+ except (OSError, ValueError, json.JSONDecodeError):+ jwks_uri = None+ _oidc_config_cache[issuer] = {"jwks_uri": jwks_uri, "expiresAt": now + _OIDC_CACHE_TTL_SECONDS}+ return jwks_uri+++async def _resolve_jwks_url(authorizer: dict) -> str | None:jwt_cfg = authorizer.get("jwtConfiguration") or {}issuer = jwt_cfg.get("issuer") or jwt_cfg.get("Issuer")if not issuer:@@ -502,7 +531,8 @@ def _resolve_jwks_url(authorizer: dict) -> str | None:if issuer.startswith("https://cognito-idp.") and ".amazonaws.com/" in issuer:pool_id = issuer.rsplit("/", 1)[-1]return f"http://{_HOST}:{_PORT}/{pool_id}/.well-known/jwks.json"- return f"{issuer}/.well-known/jwks.json"+ discovered = await _fetch_oidc_jwks_uri(issuer)+ return discovered or f"{issuer}/.well-known/jwks.json"async def _fetch_jwks(url: str) -> dict:@@ -572,7 +602,7 @@ async def _validate_jwt_authorizer(route: dict, authorizer: dict, headers: dict,return None, None, _jwt_unauthorized()kid = header.get("kid")- jwks_url = _resolve_jwks_url(authorizer)+ jwks_url = await _resolve_jwks_url(authorizer)if not kid or not jwks_url:return None, None, _jwt_unauthorized()diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.pyindex da7deeea..c4a519e5 100644--- a/tests/test_apigatewayv2.py+++ b/tests/test_apigatewayv2.py@@ -1305,6 +1305,79 @@ def test_apigw_request_mapping_claims_to_headers(apigw, cognito_idp):cognito_idp.delete_user_pool(UserPoolId=pool_id)+def test_apigw_resolve_jwks_url_uses_oidc_discovery(monkeypatch):+ """Non-Cognito issuers: jwks_uri is read from OIDC discovery, not hardcoded.++ Regression test for issuers like Salesforce that publish their keys at a path+ other than {issuer}/.well-known/jwks.json (e.g. /id/keys).+ """+ import asyncio++ from ministack.services import apigateway as apigw_mod++ issuer = "https://example-idp.test"+ discovery_url = f"{issuer}/.well-known/openid-configuration"+ real_jwks_uri = f"{issuer}/id/keys"+ calls = {"urls": []}++ async def _fake_urlopen(request_or_url, _timeout_seconds):+ calls["urls"].append(request_or_url)+ body = json.dumps({"jwks_uri": real_jwks_uri}).encode("utf-8")+ return 200, {"Content-Type": "application/json"}, body++ monkeypatch.setattr(apigw_mod, "_urlopen_async", _fake_urlopen)+ monkeypatch.setattr(apigw_mod, "_oidc_config_cache", apigw_mod.AccountScopedDict())++ authorizer = {"jwtConfiguration": {"Issuer": issuer, "Audience": ["ms-client"]}}+ resolved = asyncio.run(apigw_mod._resolve_jwks_url(authorizer))++ assert resolved == real_jwks_uri+ assert calls["urls"] == [discovery_url]+++def test_apigw_resolve_jwks_url_falls_back_when_discovery_unavailable(monkeypatch):+ """If OIDC discovery fails, fall back to the conventional jwks.json path."""+ import asyncio++ from ministack.services import apigateway as apigw_mod++ issuer = "https://unreachable-idp.test"++ async def _failing_urlopen(_request_or_url, _timeout_seconds):+ raise OSError("connection refused")++ monkeypatch.setattr(apigw_mod, "_urlopen_async", _failing_urlopen)+ monkeypatch.setattr(apigw_mod, "_oidc_config_cache", apigw_mod.AccountScopedDict())++ authorizer = {"jwtConfiguration": {"Issuer": issuer, "Audience": ["ms-client"]}}+ resolved = asyncio.run(apigw_mod._resolve_jwks_url(authorizer))++ assert resolved == f"{issuer}/.well-known/jwks.json"+++def test_apigw_resolve_jwks_url_cognito_skips_discovery(monkeypatch):+ """Cognito issuers keep using the local pool JWKS — no discovery call."""+ import asyncio++ from ministack.services import apigateway as apigw_mod++ issuer = "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_pool123"+ called = {"hit": False}++ async def _should_not_be_called(_request_or_url, _timeout_seconds):+ called["hit"] = True+ return 200, {}, b"{}"++ monkeypatch.setattr(apigw_mod, "_urlopen_async", _should_not_be_called)+ monkeypatch.setattr(apigw_mod, "_oidc_config_cache", apigw_mod.AccountScopedDict())++ authorizer = {"jwtConfiguration": {"Issuer": issuer, "Audience": ["ms-client"]}}+ resolved = asyncio.run(apigw_mod._resolve_jwks_url(authorizer))++ assert resolved.endswith("/us-east-1_pool123/.well-known/jwks.json")+ assert called["hit"] is False++def test_apigw_http_proxy_does_not_block_parallel_ddb(monkeypatch):import asyncio