Skip to content

[Go] Add external-IdP token federation - #1106

Open
anilmenon14 wants to merge 1 commit into
mainfrom
feature/go-federated-token-auth
Open

anilmenon14 wants to merge 1 commit into
mainfrom
feature/go-federated-token-auth

Conversation

@anilmenon14

@anilmenon14 anilmenon14 commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Adds first-class external-IdP token federation (for example Microsoft Entra ID) to the Go (cgo) SDK, through two new C FFI entry points. This is the Go half of #1069. The Java half is #1105. The two PRs touch disjoint files: this one changes rust/ffi/ and go/, #1105 changes rust/jni/ and java/.

WHY: Customers whose workloads already carry an IdP identity (Entra ID managed identity, workload identity, and so on) should not have to provision and rotate a Databricks OAuth client secret just to use Zerobus. The Rust core already performs the RFC 8693 token exchange, caching and proactive refresh (#740 / #760). The Go SDK only needs a way to hand it an IdP token.

WHAT:

  • New Go API (github.com/databricks/zerobus-sdk/go):
    • IdpTokenSupplier: func() (string, error). There is no context.Context parameter, because the SDK calls the supplier during background refresh, when no caller context exists.
    • NewFederatedToken(supplier, opts ...FederatedTokenOption): account-level federation by default. WithClientID(spClientID) selects workload identity federation.
    • ZerobusSdk.CreateStreamWithFederatedToken and ZerobusSdk.CreateArrowStreamWithFederatedToken, alongside the existing ...WithHeadersProvider methods.
    token, err := zerobus.NewFederatedToken(fetchEntraIDToken, zerobus.WithClientID(spClientID))
    stream, err := sdk.CreateStreamWithFederatedToken(
        zerobus.TableProperties{TableName: "catalog.schema.table"}, token, options)
  • Token caching: each FederatedToken gets a random 128-bit cache identity (go:<hex>) when it is built. It is passed to the core's IdpTokenSupplier::with_identity. Exchanged tokens are cached per table. For account-level federation the cache is per instance: streams created from the same instance share one exchanged token, and different instances never share one. For workload identity federation the core keys the cache by the service principal's client ID, so instances with the same client ID share a token. The identity is never derived from an address.
  • Retryability: a supplier error is retried like a failed OAuth request. A *ZerobusError with IsRetryable: false (found with errors.As, so it may be wrapped) is permanent and stops recovery. A panicking supplier is recovered in Go and reported as a permanent failure, so it never unwinds into Rust or crashes the process.
  • New C FFI (additive; zerobus.h gains 96 lines and loses none, so C++ and .NET are unaffected):
    • zerobus_sdk_create_stream_with_federated_token and zerobus_sdk_create_arrow_stream_with_federated_token.
    • They take an IdpTokenSupplierCallback that returns a CIdpToken (token, or error_message plus is_retryable), user_data and free_user_data, a nullable client_id (null means account-level), and a cache_identity.
    • Ownership of user_data follows the headers-provider entry points: the FFI owns it once the call is made, and frees it exactly once on every path, after any in-flight callback has returned.
    • New rust/ffi/src/federated.rs adapts the callback into the core's IdpTokenSupplier. It runs the callback on spawn_blocking, so a slow IdP round-trip never stalls a Tokio worker. A blocking call can't be cancelled, so it holds a lock for the whole call: a hung callback holds one thread per stream, and retries or refreshes wait for it instead of starting new calls.
    • Both entry points keep the typed ZerobusError, so a transient IdP failure stays retryable in CResult.is_retryable, including on the Arrow path.
  • Go cgo binding: follows the existing HeadersProvider pattern. Each create hands a new cgo.Handle to the FFI, which releases it through goFreeIdpTokenSupplier. Go never deletes it itself.
  • Docs: godoc, a README section on external-IdP federation plus API reference entries, a runnable examples/json/federated (Entra ID client-credentials grant, both modes, wired into make examples/lint/fmt), go/NEXT_CHANGELOG.md, rust/ffi/NEXT_CHANGELOG.md and go/CLAUDE.md.

Release note for maintainers: as agreed, this PR does not commit rebuilt go/lib/*.a archives. CI builds the FFI from source (make build-rust). The archives get committed on the release version-bump branch through release-go.yml.

How is this tested?

  • Rust FFI (rust/ffi/src/federated.rs tests):
    • Callback replies map correctly: token, retryable error, permanent error, empty token, neither set, and both set (the error wins).
    • Argument validation (null or blank cache_identity, blank client_id), with user_data freed exactly once in every case.
    • Both entry points free user_data exactly once on failure.
    • A real federated stream create runs the callback through FederatedTokenProvider and spawn_blocking, and surfaces its error as non-retryable, in both modes.
    • Full FFI suite: 95 passed. cargo clippy --all-targets -D warnings and cargo fmt --check are clean.
  • Go unit tests (go/federated_test.go): both modes and String(), option validation, unique cache identities, retryability classification (including a wrapped *ZerobusError), panic recovery, handle release, and nil-token rejection.
  • Go end-to-end (go/tests/federated_test.go): real JSON and Arrow streams run against the existing mock gRPC server. The SDK's Unity Catalog URL points at a new loopback mock token endpoint (go/tests/mock_token_server.go), which records every exchange request and replies from a script. The tests check:
    • The exchange request, in both modes:
      • 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.
      • The minted token reaches the gRPC stream as the Bearer token, with the table-name header.
    • Caching:
      • Streams created from one FederatedToken share one exchanged token: the supplier is called once and the token endpoint hit once.
      • Two account-level tokens never share an exchanged token, and each stream gets its own bearer.
      • Workload identity tokens with the same client_id share one, and different client_ids don't.
      • Exchanged tokens are per table.
      • 8 concurrent creates on one token still make only one supplier call and one exchange.
    • Supplier errors:
      • A plain error is retryable, isn't cached (the next create calls the supplier again), and its message never contains the IdP token.
      • A non-retryable *ZerobusError, even wrapped, stays permanent.
      • An empty token is rejected as permanent and never sent.
      • A panicking supplier is contained, and the same SDK keeps working.
      • A hung supplier is called once even when the stream retries, and the token works again once the call returns.
    • Refresh: when the exchanged token is due for refresh, the SDK calls the supplier again and exchanges the new token, not a cached one.
    • Exchange failures: HTTP 401/400 from the token endpoint is permanent and carries the server's reason; 500 is retryable.
    • Arrow streams: the Arrow entry point sends the same exact subject_token and correct client_id in both modes, and shares the token across streams.
    • Handle release: after streams close, and after a failed create, the cgo.Handle is released, so the FederatedToken is garbage-collected.
    • The suite runs under go test -race with no data races.
    • The mock gRPC server now also records each stream's authorization and table-name headers.
  • Each test can fail: I broke the code locally in each of these ways, confirmed that the matching test failed, and reverted:
    • A fresh cache identity per stream, or a constant one.
    • client_id dropped for workload identity, or always omitted on the Arrow path.
    • The token truncated by one byte.
    • The supplier's token memoised in the binding.
    • Supplier errors mapped to non-retryable.
    • The recover() removed. This crashes the test process, which shows the recovery is load-bearing.
    • The handle never freed.
    • The lock around the callback removed (the supplier was then called 3 times instead of once).
  • Full checks: make test passes (95 Rust FFI tests, 82 Go tests), and make lint passes, including all examples.
  • Live smoke (Azure Databricks workspace, Entra ID app registration): account-level, workload identity and OAuth all ingested and flushed. The batches in each run shared a single token exchange.
  • 70-minute soak per mode, run in parallel against the Azure workspace, using one ZerobusSdk and one FederatedToken per mode:
    • Workload: every 5 minutes a fresh stream ingested 200 records, then flushed and closed.
    • Token exchanges: the IdP supplier ran exactly twice per mode, a cold mint at t=0 and a proactive refresh at t=55 min (300 s before the ~60-min expiry). Every other stream creation hit the cache.
    • Outcome: both modes PASSed with 14/14 batches, 0 failures and 2,800 records each.

Add external-IdP (for example Microsoft Entra ID) token federation to
the Go SDK through two new, additive C FFI entry points.

Go: IdpTokenSupplier, NewFederatedToken (WithClientID selects workload
identity federation), CreateStreamWithFederatedToken and
CreateArrowStreamWithFederatedToken. Each FederatedToken has a random
cache identity, so streams on one token share its exchanged token and
two tokens never share one. A non-retryable *ZerobusError from the
supplier is permanent; other errors are retried; a panic is recovered
and reported as permanent.

FFI: zerobus_sdk_create_stream_with_federated_token and
zerobus_sdk_create_arrow_stream_with_federated_token take an
IdpTokenSupplierCallback returning a CIdpToken. user_data ownership
follows the headers-provider entry points. The callback runs on a
blocking thread. zerobus.h changes are additive.

Tests run real streams against the mock gRPC server and a new mock
token endpoint that records every exchange request. go/lib archives
are left for the release branch.

Refs #1069

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Anil Menon <anil.menon@databricks.com>
@anilmenon14
anilmenon14 force-pushed the feature/go-federated-token-auth branch from 517bee7 to 78985e0 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