Skip to content

[Rust][Python] Add federation end-to-end tests - #1107

Open
anilmenon14 wants to merge 1 commit into
mainfrom
feature/federation-e2e-tests
Open

anilmenon14 wants to merge 1 commit into
mainfrom
feature/federation-e2e-tests

Conversation

@anilmenon14

@anilmenon14 anilmenon14 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Adds end-to-end tests for external-IdP token federation (#740 / #760) in the Rust core and the Python SDK, and fixes one bug those tests found. This is the Rust and Python part of #1069. Java (#1105) and Go (#1106) add the same coverage for their bindings.

WHY: the existing federation tests check the core's logic when it's called directly, but not the wiring between layers. No Rust test built a stream with .federated_auth(), and no Python test reached a working token endpoint: every Python bridge test was a failure path. So a regression in the builder's federation arm, the shared token cache, or the Python bridge would pass CI. I checked this by breaking the code: on main, nothing fails when the federation arm swaps workspace_id and the Unity Catalog URL, gets a fresh token cache instead of the SDK's shared one, or drops the refresh timeout.

WHAT:

  • Bug fix (Rust core, so it also reaches Python): an IdP token supplier that returns an empty or whitespace-only string now fails with a non-retryable ZerobusError::InvalidArgument (NonRetriableException in Python). Before, the SDK posted an empty subject_token to the token endpoint, and the failure looked like a federation-policy problem. Java and Go reject an empty token in their bridges ([Java] Add external-IdP token federation #1105, [Go] Add external-IdP token federation #1106); this check in FederatedTokenProvider covers Rust and Python, and backs up the other two.
  • New rust/tests/src/federated_token_tests.rs (16 tests). Each builds real streams with StreamBuilder::federated_auth against the mock token endpoint and mock gRPC server.
  • Test-support changes:
    • mock_oauth.rs now records each request's method, path, headers and decoded form.
    • The tests check each stream's bearer with the mock gRPC server's get_auth_headers(), added on main by [Rust] Add personal access token auth #1071.
    • Existing tests are unaffected.
  • Flaky-test fix: oauth_token_tests::hung_refresh_falls_back_to_cached_token gave its first stream, which does a cold mint, a 1-second budget. A loopback mint takes about 600 ms and up to about 2 s on a loaded machine (measured on main), so the test failed once in a full make test run here. Only the second stream, which never mints, needs the tight budget. The first now uses the default timeout, and the new federated version of the test does the same. What each test checks hasn't changed.
  • New python/tests/test_federated_exchange.py (15 tests). Each calls the real create_stream(auth=FederatedToken(...)) against a loopback mock token endpoint (ThreadingHTTPServer). The gRPC endpoint is a refused port with recovery off; the SDK exchanges the token before it connects, so each create reaches the token endpoint.
  • Docs:
    • Fixed the module docstring of python/tests/test_federated_auth.py, which said the bridge was already tested end to end.
    • Documented the empty-token rule on IdpTokenCallback and FederatedToken.
    • Added NEXT_CHANGELOG.md entries for Rust and Python.

How is this tested?

  • Rust (federated_token_tests.rs), in both federation modes where it applies:
    • The exchange request:
      • It is POST /oidc/v1/token, form-encoded, with no Authorization header.
      • subject_token is byte-identical to the supplier's token (a realistic ~1.5 KB JWT).
      • grant_type and subject_token_type are the token-exchange values.
      • client_id is sent only for workload identity federation.
      • scope, resource and authorization_details match the OAuth grant for the same table.
      • The exchanged token reaches the gRPC stream as the Bearer token, with the table-name header.
    • Caching:
      • Streams sharing one supplier share one exchange: the supplier is called once and the endpoint hit once.
      • Two account-level suppliers never share a token, and each stream gets its own identity's bearer.
      • Workload identity is keyed by client_id.
      • Tokens are per table.
      • OAuth and federation on the same table never share a token.
      • 8 concurrent creates with a slow (500 ms) supplier make one supplier call and one exchange.
    • Supplier errors:
      • A supplier error is retryable and isn't cached.
      • An empty or whitespace-only token is permanent and never sent.
    • Refresh: when the exchanged token is due for refresh, the supplier is called again and its new token is exchanged and used by later streams.
    • Auth rejection: an UNAUTHENTICATED rejection re-exchanges once and succeeds. Repeated rejection stops after one re-exchange.
    • Hung refresh: a refresh that hangs falls back to the still-valid cached token.
    • Token-endpoint errors:
      • HTTP 400/401 are permanent, carry the server's reason, and aren't retried.
      • HTTP 500 is retryable and isn't cached.
      • No error message contains the IdP token.
  • Python (test_federated_exchange.py):
    • The exchange request, in both modes: the same request checks as the Rust tests (method, path, headers, byte-identical subject_token, grant parameters, and client_id only for workload identity).
    • Caching:
      • One FederatedToken shares one exchange across streams.
      • Two FederatedTokens never share a token.
      • Workload identity is keyed by client_id.
      • Tokens are per table.
    • Supplier errors:
      • A raised exception is retryable (not NonRetriableException) and isn't cached.
      • A non-string or empty return is NonRetriableException and is never sent.
    • Refresh: the supplier is called again and its new token is exchanged.
    • Token-endpoint errors: HTTP 400/401 are NonRetriableException with the server's reason, and HTTP 500 is retryable. No error contains the IdP token.
    • Async SDK: the async SDK with an async def supplier, in both modes, sends the exact token and caches it.
  • Each test can fail. I broke the code locally in each of these ways, confirmed that a test failed, and reverted:
    • Rust core:

      • The federation arm swaps workspace_id and the Unity Catalog URL.
      • The federation arm gets a fresh token cache instead of the SDK's shared one.
      • The federation arm drops client_id.
      • The federation arm drops the refresh timeout.
      • A constant account-level cache identity.
      • subject_token truncated by one byte.
      • invalidate() does nothing.
      • Client-credentials Basic auth sent on the exchange.

      The first, second and fourth weren't caught by any existing test.

    • Python:

      • A fresh cache identity per stream, or a constant one.
      • client_id dropped on the sync path, or on the async path.
      • The supplier's result remembered per FederatedToken.
      • The token truncated by one character in the bridge.
      • A supplier exception mapped to non-retryable.
  • What the tests found: the empty-token bug above, in both Rust and Python.
  • Results:
    • Rust: make test (the full feature matrix) passes. make lint and cargo fmt --check are clean.
    • Python: the full suite passes (318 tests, run on Python 3.12). make lint, black and isort are clean.

@anilmenon14

anilmenon14 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

python / fmt (and so CI / gate) fails because python/rust/Cargo.lock on main still pins the core at 3.1.1 instead of 3.2.0. #1102 fixes that, so I'm not adding a lockfile change to this branch. I'll rebase onto main once #1102 merges.

Edit: #1071/#1072 fixed the lockfile on main, so the branch is now rebased and above fix in #1102 isn't needed.

Add end-to-end tests for external-IdP token federation that go through
the public API. The Rust tests build streams with
StreamBuilder::federated_auth against the mock token endpoint and mock
gRPC server. The Python tests call create_stream(auth=FederatedToken)
against a loopback mock token endpoint. They check the exchange request
in both modes, caching and isolation, refresh, supplier errors, auth
rejection, a hung refresh, and token-endpoint errors.

The mock token endpoint now records each request, and the mock gRPC
server records each stream's authorization and table-name headers.

Fix a bug the tests found: a supplier returning an empty token caused an
empty subject_token to be sent. FederatedTokenProvider now rejects an
empty or whitespace-only token as a non-retryable InvalidArgument.

Give the cold first stream in the OAuth hung-refresh test the default
timeout; a loopback mint can take over a second on a loaded machine.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Anil Menon <anil.menon@databricks.com>
@anilmenon14
anilmenon14 force-pushed the feature/federation-e2e-tests branch from 7f64292 to b14a140 Compare November 21, 2026 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant