GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: surface the token-exchange error instead of KeyError('access_token') by TangoEnSkai · Pull Request #928 · databricks/databricks-sql-python

Conversation

@TangoEnSkai

@TangoEnSkai TangoEnSkai commented Aug 23, 2026

Copy link
Copy Markdown

Context

TokenFederationProvider wraps every credentials provider unconditionally
(auth.py:68, "Always wrap with token federation"), and _should_exchange_token
returns True whenever the token's issuer host differs from the workspace host —
for example a Microsoft Entra ID token against an Azure Databricks workspace. An
exchange is therefore attempted on every such connection.

When the exchange endpoint refuses, it replies with an OAuth error body
({"error": ..., "error_description": ...}) rather than a token.
_exchange_token read token_response["access_token"] unconditionally, so that
reply raised KeyError('access_token'). The handler in _get_token logs the
exception, and str(KeyError("access_token")) renders as the key name:

Token exchange failed, using external token: 'access_token'

The endpoint's own error and error_description were never read, so there was
no way to tell whether the exchange was misconfigured, unauthorized, or simply
unsupported for that workspace. Note token_type on the very next line is
already read defensively with .get().

What

  • _exchange_token checks for access_token before reading it. If it is
    missing, it raises a ValueError naming the endpoint, the HTTP status, and the
    returned error / error_description.
  • A non-JSON or truncated body now raises a ValueError naming the endpoint and
    status instead of surfacing a bare JSONDecodeError.
  • tests/unit/test_token_federation.py: test_exchange_token_failure previously
    asserted pytest.raises(KeyError) — it pinned the behaviour this issue is
    about — and is updated to assert the endpoint's reason is surfaced. Three tests
    are added for the no-error-fields body, the non-JSON body, and the unchanged
    fallback to the external token.

The response carries no token on the failure paths, so nothing sensitive is
placed in the message.

Why

This is diagnostic only — the fallback to the external token works and queries
succeed, which is exactly why it is worth fixing: the warning fires on every
connection and tells the operator nothing actionable. The fix is contained to the
one function and preserves the graceful degradation, since _get_token still
catches the exception and falls through to the external token.

Completion Criteria

  • _exchange_token surfaces the endpoint's error / error_description
  • Non-JSON responses report the endpoint and status, not a JSONDecodeError
  • Fallback to the external token is unchanged, covered by a new test
  • pytest tests/unit — 531 passed, 217 skipped, 185 subtests passed
  • Reverting the source change fails exactly the three new/updated tests and
    nothing else
  • black --check src clean
  • CHANGELOG entry under # Unreleased
  • Commit signed off (DCO)

close #904

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@TangoEnSkai
fix: surface the token-exchange error instead of KeyError('access_tok…
3265132
…en')

When token federation exchange fails, `_exchange_token` read
`token_response["access_token"]` unconditionally. An OAuth error body
(`{"error": ..., "error_description": ...}`) therefore raised
`KeyError('access_token')`, and the handler in `_get_token` logged the
name of the missing key:

    Token exchange failed, using external token: 'access_token'

The endpoint's own reason was never read. Since `TokenFederationProvider`
wraps every provider and `_should_exchange_token` returns True whenever
the issuer host differs from the workspace host, this warning fires on
every connection for cross-issuer tokens with no way to tell whether the
exchange was misconfigured, unauthorized, or unsupported.

Check for `access_token` before reading it and raise a ValueError that
names the endpoint, the HTTP status, and the returned `error` /
`error_description`. A non-JSON body now reports the endpoint and status
rather than surfacing a JSONDecodeError. The response carries no token in
either case, so nothing sensitive is exposed.

The fallback to the external token is unchanged: `_get_token` still
catches the exception and connections keep working.

Resolves databricks#904

Signed-off-by: TangoEnSkai <[email protected]>
@TangoEnSkai
TangoEnSkai force-pushed the fix/token-exchange-error-message branch from 5c96d89 to 3265132 Compare September 9, 2026 14:32
@TangoEnSkai
Merge main and resolve release history conflict
be39535
Signed-off-by: TangoEnSkai <[email protected]>
@TangoEnSkai

TangoEnSkai commented Sep 23, 2026

Copy link
Copy Markdown
Author

Merged current main into this branch and resolved the CHANGELOG conflict, preserving both the upstream entries and this fix. The PR is mergeable again at be39535.

Validation after the merge:

  • Token federation tests: 34 passed.
  • Full unit suite: 586 passed, 237 skipped, 185 subtests passed.
  • Black check on src: passed (64 files unchanged).

The new GitHub Actions runs are awaiting maintainer approval (action_required), including Code Quality, DCO, and E2E/coverage. Could a maintainer approve those runs and review the fix? The existing external Python Integration Tests check is green.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jay-xiao446
jay-xiao446 approved these changes Sep 29, 2026
View reviewed changes

@jay-xiao446 jay-xiao446 left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lorogno

lorogno commented Oct 11, 2026

Copy link
Copy Markdown

Is there an expected Release date for this Fix?
Looking forward to it because it has effects to out dbt-databricks Project

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vuanhphung vuanhphung mentioned this pull request Oct 30, 2026
Auth token warning using oauth M2M #702
Closed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jay-xiao446 jay-xiao446 jay-xiao446 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Token federation: _exchange_token raises KeyError('access_token') on error responses, discarding the real failure reason

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@TangoEnSkai @lorogno @jay-xiao446
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.