Skip to content

feat(auth): U2M token-cache control on the kernel path (PECOBLR-4146) - #283

Merged
eric-wang-1990 merged 12 commits into
mainfrom
eric-wang/kernel-oauth-token-persistence
Aug 25, 2026
Merged

eric-wang-1990 merged 12 commits into
mainfrom
eric-wang/kernel-oauth-token-persistence

Conversation

@eric-wang-1990

@eric-wang-1990 eric-wang-1990 commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Adds U2M on-disk token-cache control on the kernel path (PECOBLR-4146, PuPr scope). The kernel already owns the U2M token lifetime and, by default, persists the refresh token to an encrypted on-disk cache (~/.config/databricks-sql-kernel/oauth/, 0600, matching JDBC EnableTokenCache=1). This PR lets a caller enable/disable that cache and set its passphrase from the two bindings — the piece needed for the connectors to reconcile their own defaults (Thrift Python/Node default to no on-disk persistence) with the kernel's on-disk default.

Caller-supplied custom token stores (DB/keychain/vault via a callback bridge) are deferred post-GA — see docs/designs/PECOBLR-4146-oauth-token-persistence-design.md for the rationale (no named customer, the connector ships no prod store, the kernel disk cache already covers the common case better, and the callback path is the untested higher-risk surface). The custom-store design is fully worked out and prototyped in this branch's earlier commits.

How

  • No kernel-core change. TokenCacheConfig (enable + passphrase) already exists and is reachable over the C-ABI; this PR is purely binding surface.
  • napi — ConnectionOptions gains tokenCacheEnabled?: boolean / tokenCachePassphrase?: string; build_auth_config maps them to TokenCacheConfig on the U2M arm. Omitted ⇒ kernel default; a passphrase with no flag implies enabled. Native contract regenerated with the crate-correct @napi-rs/cli@3.8.2 (which also migrates the stale committed v2-CLI loader to v3 — the bulk of the index.* diff).
  • pyo3 — Session(…) gains token_cache_enabled / token_cache_passphrase kwargs, carried on OAuthParams and mapped to TokenCacheConfig in the U2M arm (the GIL-less build_auth_config_inner stays unit-testable).

The default the connector adapters must choose

The kernel defaults on-disk; Thrift defaults in-memory / none. The connector adapters (databricks-sql-nodejs#457, databricks-sql-python#913) should consciously pick per binding — adopt the kernel/JDBC default (fewer logins; token now at rest) or disable-by-default to match Thrift's conservative posture. This PR's enable/disable knob is what makes either choice expressible.

Testing

  • napi: cargo test --features dyn-symbols (81, incl. a new u2m_token_cache_control_maps_to_config) + clippy, green.
  • pyo3: clippy (--lib + --tests) green + maturin-linked build; the new mapping test runs under Linux CI (cargo test from pyo3/).
  • Kernel U2M itself was verified E2E on this branch (open + a two-run disk-cache round-trip that skips the browser on the second run).

This pull request and its description were written by Isaac.

…R-4146)

Introduce a TokenPersistence trait so callers can supply a custom OAuth U2M
token store (DB, keychain, in-memory) instead of the kernel's built-in
encrypted disk cache. The built-in cache becomes the default impl
(DiskTokenCache), so behavior is unchanged when no store is supplied.

- New src/auth/oauth/persistence.rs: TokenPersistence (async load/save) +
  DiskTokenCache default impl wrapping the existing TokenCache.
- AuthConfig::OAuthU2M gains token_store: Option<Arc<dyn TokenPersistence>>;
  when Some it overrides the token_cache disk policy. Threaded through the
  oauth_u2m constructor (bindings must use it -- AuthConfig is non_exhaustive).
- AuthorizationCodeProvider swaps cache_passphrase for the store; load errors
  are treated as a cache miss, save errors logged and swallowed.
- session.rs resolves the effective store: caller store wins, else derive
  DiskTokenCache from the token_cache policy.
- C-ABI passes None (no callback surface; out of scope for Go).

Kernel-only enabler; napi/pyo3 binding hooks follow in later commits.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
…COBLR-4146)

Thread the kernel's TokenPersistence through the napi binding so a Node caller
can supply a custom OAuth U2M token store (its OAuthPersistence) on the kernel
path, instead of the built-in disk cache.

- openSession gains two optional callbacks (oauthPersistenceRead /
  oauthPersistencePersist). When both are supplied, a NapiTokenStore wraps them
  as ThreadsafeFunctions and is installed as the U2M token_store; load/save
  call the JS store and await its returned Promise. The token crosses as the
  kernel's OAuthToken JSON (lossless, same bytes the disk cache stores) via the
  KernelPersistArgs object.
- build_auth_config takes the resolved store and forwards it in the U2M arm.
- add async-trait dep (to impl the kernel's #[async_trait] TokenPersistence).

Contract regeneration note: index.js/index.d.ts are regenerated with
@napi-rs/cli@3.8.2 (the version bin/build-native.sh selects for the napi 3.x
crate). The committed contract was stale @napi-rs/cli v2-style output, so the
diff also migrates the loader to v3 (adds NAPI_RS_ENFORCE_VERSION_CHECK, the
per-package version-consistency checks, WASI fallback) — same regeneration the
nodejs driver landed in #505. The persistence-specific delta is KernelPersistArgs
+ the two openSession params.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
…R-4146)

Thread the kernel's TokenPersistence through the Python binding so a caller can
supply a custom OAuth U2M token store on the kernel path.

- Session(...) gains an oauth_persistence kwarg: a Python object exposing
  read(host)->Optional[str] / persist(host, token_json). It is wrapped as a
  PyTokenStore (impl TokenPersistence) that calls the object under the GIL and
  exchanges the kernel OAuthToken as an opaque JSON string (lossless; the same
  bytes the disk cache stores; consistent with the napi binding).
- Carried on OAuthParams.token_store so build_auth_config's signature and its
  GIL-less unit-testable inner stay unchanged; the U2M arm forwards it.
- add async-trait + serde_json deps.

The databricks-sql-python connector adapts its experimental_oauth_persistence
onto this in a follow-up (#913).

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
…ence (PECOBLR-4146)

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: 1 Medium

Solid, well-contained feature — the kernel-side TokenPersistence trait + DiskTokenCache default preserve existing behavior, session.rs correctly resolves caller-store-wins-else-disk, and the U2M load/save rewrite keeps the prior control flow with lenient (miss-on-load-error, swallow-on-save-error) semantics as documented. One medium concern: both bindings key the caller store on host alone, dropping the client_id/scopes the kernel trait (and design doc) say form the key, which diverges from the triple-keyed disk cache. The kernel core carries unit coverage; NapiTokenStore/PyTokenStore are only exercised via connector E2E (acceptable given TSFN/GIL are hard to unit-test).

Comment thread napi/src/database.rs Resolved
…m store post-GA

PuPr ships kernel-owned persistence: the kernel owns the token lifetime + the
built-in encrypted disk cache, and the caller can enable / disable / locate it
via TokenCacheConfig exposed through napi/pyo3. A caller-supplied custom token
store (via napi/pyo3 callback bridges) is deferred post-GA, built only when a
named consumer needs a non-file sink.

Records the rationale (no named customer, no prod store in the connector today,
kernel disk cache already covers the common case better, no telemetry signal,
the custom-store path is the untested higher-risk surface) and keeps the
custom-store design + prototype status under Post-GA.

NOTE: the code on this branch still carries the custom-store prototype; scoping
the implementation down to the PuPr surface is the follow-up.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
… control

Revert the custom-store surface (the TokenPersistence trait + napi/pyo3 callback
bridges) added earlier on this branch, and ship the PuPr surface instead: expose
the kernel's existing TokenCacheConfig (enable/disable + passphrase) through the
napi and pyo3 bindings.

- napi: openSession ConnectionOptions gains tokenCacheEnabled /
  tokenCachePassphrase; build_auth_config maps them to TokenCacheConfig on the
  U2M arm. Native contract regenerated with @napi-rs/cli@3.8.2 (also migrates
  the stale v2-CLI loader to v3).
- pyo3: Session gains token_cache_enabled / token_cache_passphrase kwargs,
  carried on OAuthParams and mapped to TokenCacheConfig in the U2M arm.
- Kernel core is unchanged from main (no trait, no callbacks) — TokenCacheConfig
  already exists; this is purely binding surface.
- Caller-supplied custom stores are deferred post-GA; the prototype remains in
  this branch's history (see docs/designs/PECOBLR-4146-...).

Unit tests added for the enable/disable/passphrase mapping in both bindings.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

…PECOBLR-4146 token-cache entries alongside main v0.2.1 sections)

Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
The pyo3 dep enabled `extension-module` unconditionally, which tells the
linker NOT to link libpython (symbols are supplied by the host interpreter
at wheel load time). That is correct for the built extension but breaks the
`cargo test` step in pyo3-checks.yml: the unit-test binary must link
libpython, so it failed on Linux CI with a wall of undefined `Py*` symbols
(PyEval_RestoreThread, PyGILState_Ensure, ...). main is currently red for
the same reason.

Make `extension-module` an opt-in crate feature and have maturin turn it on
via [tool.maturin] features, so plain `cargo test` links libpython while the
wheel build keeps the dynamic-lookup behaviour. Same fix as the in-flight
#request-timeout binding PR; verified locally (22 pyo3 unit tests pass, incl.
u2m_token_cache_control_maps_to_config).

Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

…lve pyo3/pyproject.toml (keep extension-module comment; #288 landed the identical fix + request-timeout kwargs, auto-merged in Cargo.toml/session.rs)

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

@eric-wang-1990
eric-wang-1990 added this pull request to the merge queue Aug 25, 2026
Merged via the queue into main with commit 628abd6 Aug 25, 2026
23 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — e72b3662 Deployed Aug 25, 2026 by eric-wang-1990 via review #441
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engineer-bot Apply to an issue to trigger the engineer-bot; on a PR to enable engineer follow-up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants