Skip to content

feat: add WithTokenCache to control kernel U2M on-disk token cache - #458

Merged
eric-wang-1990 merged 9 commits into
mainfrom
eric-wang/kernel-token-cache-control
Aug 26, 2026
Merged

feat: add WithTokenCache to control kernel U2M on-disk token cache#458
eric-wang-1990 merged 9 commits into
mainfrom
eric-wang/kernel-token-cache-control

Conversation

@eric-wang-1990

@eric-wang-1990 eric-wang-1990 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

What & why

Adds WithTokenCache(enabled bool) (and a tokenCache=true|false DSN param) to control the kernel backend's U2M on-disk OAuth token cache.

On the kernel (SEA) path with OAuth U2M, the Rust kernel can persist the refresh token to an AES-256 encrypted on-disk cache (the OS config dir — ~/Library/Application Support/databricks-sql-kernel/oauth/ on macOS, ~/.config/databricks-sql-kernel/oauth/ on Linux), so a later process skips the interactive browser login. This exposes a driver-level switch for that, mapping to the kernel C-ABI setter kernel_session_config_set_u2m_token_cache_config.

Behavior

  • U2M-only. No effect on PAT/M2M (M2M has no refresh token to cache).
  • Disabled by default. When unset, the driver explicitly disables the cache — matching the driver's historical no-persistence posture. Enabling on-disk token persistence is opt-in via WithTokenCache(true) / tokenCache=true.
  • Enable flag only for now (no passphrase surface); when enabled the kernel encrypts at rest with a machine-local derived key. The Thrift path is unaffected (it has no token cache).

Dependency / why this is a draft

The C-ABI setter this calls is already on databricks-sql-kernel main (added in kernel #227), so this is not blocked on kernel PR #283 (that PR is the napi/pyo3 surface). It's a draft pending final review; the live E2E below is already done.

Testing

  • Pure-Go unit tests — the WithTokenCache option → config wiring and tokenCache DSN parsing.
  • Kernel-tagged unit testgo test -tags "cgo databricks_kernel" ./internal/backend/kernel/... exercises the real kernel_session_config_set_u2m_token_cache_config cgo setter for U2M enabled/disabled and the non-U2M no-op. (A prior revision failed to compile under the tag due to an unused var in the test seam; fixed in this PR.)

Live E2E (manual, interactive U2M)

Validated end-to-end against a live Azure workspace on the kernel U2M path, with DBSQL_KERNEL_DEBUG=1 RUST_LOG=databricks::sql::kernel=debug confirming each step. Run on both entry points — the functional-options path (NewConnector(WithTokenCache(...))) and the DSN path (sql.Open("databricks", "…?authType=OauthU2M&useKernel=true&tokenCache=true")):

Scenario enabled + empty cache enabled + cached disabled
Functional options miss → browser login → Token cached successfully (file written) Token cache hitno browser, reused browser login → Token cache disabled; not persisting (no file)
DSN (tokenCache=…) same (tokenCache=true) same, hit → no browser omit tokenCache → disabled → no file

This confirms both code paths, including the DSN carrier + bridge (tokenCache=true enables; omitting it yields the disabled default). The interactive U2M browser step means this suite is not runnable in CI — it's a manual E2E; the automated coverage above is what CI runs.

Note: the cache-hit "second run skips the browser", the encrypted on-disk round-trip, file permissions (0600), and JDBC wire-compat are also covered by the kernel's own deterministic unit tests in databricks-sql-kernel (no browser needed).

Related: databricks-sql-kernel PR #283 (napi/pyo3 surface for the same control).

This pull request and its description were written by Isaac.

Adds WithTokenCache(enabled bool) and a tokenCache DSN param that map to the kernel C-ABI setter kernel_session_config_set_u2m_token_cache_config on the OAuth U2M path. U2M-only; disabled by default (explicit opt-in preserves the driver's no-persistence posture); enable-flag only.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
@eric-wang-1990
eric-wang-1990 marked this pull request as ready for review August 25, 2026 23:47
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:47

@peco-review-bot peco-review-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 High

One blocking issue: the new trySetTokenCacheConfig test seam declares a local k that is never used, which is a Go compile error and breaks the cgo && databricks_kernel build (the very tag the seam's test runs under). Rest of the wiring (config field, DeepCopy, DSN parse, connector bridge, forwarding) looks consistent and well-tested.

Comment thread internal/backend/kernel/backend.go
Replaces the now-inaccurate 'token caching is owned by the kernel (no driver config)' note with the WithTokenCache option (U2M-only, disabled by default).

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

Copilot AI 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.

Pull request overview

This PR adds an opt-in control to enable/disable the Databricks SQL kernel backend’s on-disk OAuth U2M token cache, exposed both as a connector option (WithTokenCache(enabled bool)) and as a DSN parameter (tokenCache=true|false). This fits into the connector’s kernel backend configuration surface by forwarding the new flag through config assembly into the kernel C-ABI session config setter.

Changes:

  • Introduces TokenCacheEnabled in kernel experimental config and wires it through kernel config assembly into the kernel backend.
  • Adds DSN parsing for tokenCache and bridges it into kernel experimental config during connector construction.
  • Adds unit tests and a tagged cgo test seam to exercise the real kernel C-ABI setter.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
kernel_experimental_test.go Extends kernel experimental option classification + DeepCopy tests to include token cache flag.
kernel_config.go Forwards KernelExperimental.TokenCacheEnabled into kernel.Config.
kernel_config_test.go Adds coverage that TokenCacheEnabled is forwarded and defaults to false.
internal/config/config.go Adds TokenCacheEnabled to KernelExperimentalConfig and DSN carrier TokenCacheEnabledDSN; parses tokenCache from DSN.
internal/config/config_test.go Adds DSN parsing tests for `tokenCache=true
internal/backend/kernel/kernel_test.go Adds tagged test for the token cache C-ABI setter via a test seam.
internal/backend/kernel/config.go Adds TokenCacheEnabled to kernel backend config struct with documentation.
internal/backend/kernel/backend.go Calls the kernel C-ABI setter to configure U2M token cache; adds test seam trySetTokenCacheConfig.
connector.go Adds WithTokenCache option and bridges DSN carrier into kernel experimental config.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/backend/kernel/backend.go Outdated
Comment on lines +540 to +542
// Set auth first; technically we just need to set U2M, but setAuth is the real path.
k := &KernelBackend{cfg: Config{Auth: auth, TokenCacheEnabled: enabled}}
// Call the setter directly: setAuth would set auth first, but we want to test just the setter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Already satisfied in the current tree. In internal/backend/kernel/backend.go the trySetTokenCacheConfig function (lines ~532-548) no longer declares the unused k := &KernelBackend{...} variable and no longer carries the "Set auth first" comments the reviewer flagged. It now calls C.kernel_session_config_set_u2m_token_cache_config directly using the enabled parameter, so there's no unused-variable compile error in tagged kernel builds and the comment matches the behavior. No code change warranted (this cgo-tagged file is outside the pure-Go make test suite).

Comment on lines +83 to +87
// TokenCacheEnabled controls the kernel's on-disk OAuth U2M token-cache persistence
// (WithTokenCache / tokenCache DSN param). When false (the default), tokens are held
// in memory only; when true, the refresh token is persisted encrypted to
// ~/.config/databricks-sql-kernel/oauth/. U2M-only: PAT and M2M ignore this.
// Maps to kernel_session_config_set_u2m_token_cache_config.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expanded the TokenCacheEnabled doc comment to capture the reviewer's nuance — the zero value isn't "not applied": on the U2M path false is still forwarded to the setter to explicitly disable on-disk persistence. This matches the actual code at backend.go:436, which unconditionally calls set_u2m_token_cache_config with C.bool(k.cfg.TokenCacheEnabled). make test passes (1122 tests, 2 e2e skipped for lack of credentials, as expected).

Pushed 65fb21e (bundled with 2 other thread(s)).

@peco-review-bot peco-review-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 High · 1 Low

Clean, well-tested wiring of the new WithTokenCache / tokenCache DSN option — one blocking issue: the trySetTokenCacheConfig test seam declares an unused k variable, which is a compile error in the cgo && databricks_kernel build (F1). Also a minor Thrift-path rejection asymmetry between the option and the DSN param (F2).

Comment thread internal/backend/kernel/backend.go
Comment thread connector.go
…cks_kernel build compiles

The test seam declared an unused KernelBackend and failed 'go build -tags databricks_kernel'; only the pure-Go suite had been run. Verified: kernel-tagged tests now pass (go test -tags databricks_kernel ./internal/backend/kernel/...).

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

@peco-review-bot peco-review-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 Low

Looks good — clean, well-tested addition of WithTokenCache / tokenCache DSN wiring with matching config plumbing, disposition-map entries, DeepCopy coverage, and a cgo test seam. One low-severity note about an intentional (but slightly asymmetric) precedence in the DSN→KernelExperimental bridge; no correctness or security concerns found.

Comment thread connector.go
@eric-wang-1990 eric-wang-1990 added the engineer-bot Maintainer-only: triggers the autonomous engineer-bot (bug-fix author on issues; follow-up on PRs) label Aug 26, 2026
… columns)

Replaces the prose bullet with a table row matching the other kernel knobs; corrects the cache path to the platform config dir.

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

@peco-review-bot peco-review-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 Low

Looks good — clean, well-tested addition of WithTokenCache / tokenCache DSN param wired through to the kernel U2M token-cache setter. The conditional DSN bridge (guarding KernelExperimental allocation) is correctly asymmetric vs. the unconditional arrow bridge, since a non-nil KernelExperimental would trip the Thrift-path rejection. One Low: the new withUserConfig DSN bridge lacks the propagation test its arrow sibling has.

Comment thread connector.go
Addresses:
  - #3858505031 at internal/backend/kernel/config.go:87
  - #3858530397 at connector.go:296
  - #3858652595 at connector.go:296

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
Addresses:
  - #3858780340 at connector.go:308

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@peco-review-bot peco-review-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 Low

Looks good — clean, well-tested wiring of WithTokenCache / tokenCache DSN param through to the kernel U2M cgo setter, with matching deep-copy, field-disposition, and test-seam coverage. One low-severity doc/UX gap: tokenCache=true in a DSN without useKernel=true fails at connect with ErrRequiresKernelBackend, which isn't documented.

Comment thread connector.go
Addresses:
  - #3858817262 at connector.go:308

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@peco-review-bot peco-review-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

Looks solid overall — clean wiring of the new option/DSN param through UserConfig → KernelExperimental → kernel.Config, with good unit coverage. One medium doc/behavior mismatch: the WithTokenCache godoc says disabling is "a no-op on any backend," but WithTokenCache(false) allocates KernelExperimental and is rejected on the Thrift path — the no-op claim only holds for the tokenCache=false DSN carrier.

Comment thread connector.go
Addresses:
  - #3858847874 at connector.go:806

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@peco-review-bot peco-review-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 Low

Looks good — clean, well-documented feature with broad coverage; one low test-effectiveness note. The DSN carrier → KernelExperimental bridge, the WithTokenCache(false) opt-into-kernel behavior, and the U2M-only setter call are all consistent with the docs and the Connect-time ErrRequiresKernelBackend gate.

Comment thread internal/backend/kernel/backend.go Outdated
Addresses:
  - #3858870669 at internal/backend/kernel/backend.go:534

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@peco-review-bot peco-review-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 26, 2026
Merged via the queue into main with commit f38cebe Aug 26, 2026
14 of 15 checks passed
@eric-wang-1990
eric-wang-1990 deleted the eric-wang/kernel-token-cache-control branch August 26, 2026 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engineer-bot Maintainer-only: triggers the autonomous engineer-bot (bug-fix author on issues; follow-up on PRs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants