cosmos: handle HTTP 403/sub-status 5300 (AAD_REQUEST_NOT_AUTHORIZED) by refreshing bearer token and retrying#46167
cosmos: handle HTTP 403/sub-status 5300 (AAD_REQUEST_NOT_AUTHORIZED) by refreshing bearer token and retrying#46167
Conversation
…and async) When Cosmos DB returns HTTP 403 with sub-status 5300 (AAD_REQUEST_NOT_AUTHORIZED), the cached bearer token is now cleared and the request is retried with a fresh token. This mirrors how the base class handles HTTP 401, and resolves the issue where long-running services using managed identity would permanently fail after token expiry. - Added send() override to CosmosBearerTokenCredentialPolicy (_auth_policy.py) - Added send() override to AsyncCosmosBearerTokenCredentialPolicy (_auth_policy_async.py) - Added unit tests for both sync and async policies Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/a5381531-6292-4e5e-be43-586d3267d980 Co-authored-by: bambriz <8497145+bambriz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/a5381531-6292-4e5e-be43-586d3267d980 Co-authored-by: bambriz <8497145+bambriz@users.noreply.github.com>
| from azure.cosmos.http_constants import HttpHeaders, SubStatusCodes | ||
|
|
||
|
|
||
| def _make_request(): |
There was a problem hiding this comment.
@copilot request headers might be needed. Is there not a better way to test this? Something more expansive and using the sdk code more realistically to mimic potential issues that may occur.
There was a problem hiding this comment.
Rewrote both test files (commit cb77da60) to use a realistic Pipeline/AsyncPipeline backed by a MockTransport that extends HttpTransport/AsyncHttpTransport. The transport returns proper requests.Response objects — including the x-ms-substatus header on the 403 — and records every outgoing request so tests can assert on the exact Authorization header value (verifying the Cosmos AAD format type=aad&ver=1.0&sig=<token> and that a fresh token is used on the retry). This mirrors the pattern used in azure-core's own authentication test suite.
Replace MagicMock-based policy isolation tests with full azure-core Pipeline/AsyncPipeline tests that use a real MockTransport (extending HttpTransport/AsyncHttpTransport). This approach: - Returns proper requests.Response objects with real header dicts, including x-ms-substatus for the 403/5300 trigger - Records all outgoing requests so tests can assert on the exact Authorization header value (type=aad&ver=1.0&sig=<token>) - Verifies both the Cosmos AAD header format and token freshness end-to-end through the full policy pipeline, matching the pattern used in azure-core's own authentication tests Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/a67aa68c-a3b6-42ef-824f-aa45415226d6 Co-authored-by: bambriz <8497145+bambriz@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/6a43abe4-336d-48bb-9181-24523f9dc925 Co-authored-by: bambriz <8497145+bambriz@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot update changelog to mention the pr and the fix applied. |
Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/50ccc6e6-b671-434d-97cc-9469276b13da Co-authored-by: bambriz <8497145+bambriz@users.noreply.github.com>
Done in commit Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
AsyncCosmosBearerTokenCredentialPolicydoes not handle HTTP 403 with sub-status 5300 (AAD_REQUEST_NOT_AUTHORIZED) - only 401 is handledsend()override toAsyncCosmosBearerTokenCredentialPolicyin_auth_policy_async.pyto clear cached token and retry on 403/5300send()override to syncCosmosBearerTokenCredentialPolicyin_auth_policy.pyfor the same fixPipeline/AsyncPipelinewithMockTransportthat returns properrequests.Responseobjects with headersAuthorizationheader format (type=aad&ver=1.0&sig=<token>) in both initial and retry requests