fix: report a rejected PKCE token exchange as an auth error (FLYTE-SDK-7D) - #1432
Open
EngHabu wants to merge 2 commits into
Open
fix: report a rejected PKCE token exchange as an auth error (FLYTE-SDK-7D)#1432EngHabu wants to merge 2 commits into
EngHabu wants to merge 2 commits into
Conversation
…K-7D)
The authorization-code -> access-token exchange raised a bare RuntimeError
carrying the raw response bytes whenever the identity provider answered with
anything other than 200:
RuntimeError: Failed to request access token with response: [400]
b'{\n "error": "invalid_request",\n "error_description":
"client_secret is missing."\n}'
That is a deployment configuration problem -- the OAuth2 application backing
browser login requires a client secret that the PKCE flow does not send -- but
RuntimeError is unclassified, so it escaped to Sentry as an SDK crash and the
user got a Python bytes repr instead of an explanation.
`_token_client.get_token` already raises `AuthenticationError` for exactly this
condition (_token_client.py:157); the PKCE authorization client just never got
the same treatment. `AuthenticationError` is on `_sentry._is_user_error`'s
allow-list and still subclasses RuntimeError, so no caller changes.
Also decode the RFC 6749 section 5.2 error body -- `error`, `error_description`,
`error_uri` -- rather than dumping the raw bytes. The description is the only
part of the exchange that says why the login was rejected. Non-JSON bodies (an
HTML page from a proxy answering the token endpoint) are quoted and truncated.
The sibling `Expected "access_token" in response` ValueError becomes an
AuthenticationError for the same reason. The state-parameter mismatch check is
deliberately left as a ValueError: it guards against a forged callback, and a
genuine SDK bug could also trip it, so it should keep reporting.
fixes FLYTE-SDK-7D
Signed-off-by: Haytham Abuelfutuh <haytham@afutuh.com>
Signed-off-by: Haytham Abuelfutuh <haytham@afutuh.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
FLYTE-SDK-7D —
flyte runagainst a deployment whose OAuth2 application is misconfigured.Reading the whole exception chain rather than the reported title, the event holds two independent problems. The
ConnectError: invalid content-type: 'text/html'half is already handled by #1411 (merged yesterday; the event is from release 2.5.18, which predates it). This PR is the other half:The identity provider is telling us the OAuth2 application backing browser login is registered as a confidential client and wants a
client_secretthe PKCE flow doesn't send. That is a deployment configuration problem, but a bareRuntimeErroris unclassified, so:_token_client.get_tokenalready raisesAuthenticationErrorfor the identical condition (_token_client.py:157) — the PKCE authorization client simply never got the same treatment.What
_request_access_tokenraisesAuthenticationErrorinstead ofRuntimeErroron a non-200 from the token endpoint.AuthenticationErroris on_sentry._is_user_error's allow-list, so it stops being crash-reported, and it still subclassesRuntimeError, so no caller changes.error,error_description,error_uri) rather than dumping raw bytes. Non-JSON bodies — an HTML page from a proxy answering the token endpoint — are quoted and truncated to 200 chars.Expected "access_token" in response from oauth serverValueErrorbecomes anAuthenticationErrorfor the same reason.Before / after for the reported event:
Deliberately not changed
The state-parameter mismatch (
Unexpected state parameter [...] passed) stays aValueError. It guards against a forged callback and a genuine SDK bug could also trip it, so it should keep reporting. A test pins that.Testing
8 tests in
tests/flyte/remote/test_pkce_token_request.py. 6 verified failing onmain; the 2 that pass there are the controls (success path, state mismatch).tests/flyte/remote+tests/flyte/keyring+tests/flyte/test_sentry.py= 684 passed, plus one failure (test_retrieve_degrades_when_keyring_not_installed) that also fails on cleanmainin my sandbox.fixes FLYTE-SDK-7D