Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ crate-type = ["cdylib"]
databricks-sql-kernel = { path = ".." }

# pyo3 — abi3-py39 lets one wheel cover Python 3.9+.
pyo3 = { version = "0.26", features = ["extension-module", "abi3-py39"] }
#
# `extension-module` is gated behind a Cargo feature so that
# `cargo test -p databricks-sql-kernel-pyo3` can build a normal
# binary that links against libpython (needed to run unit tests
# that call `Python::attach`). Maturin enables the feature via
# pyproject.toml when building the wheel.
pyo3 = { version = "0.26", features = ["abi3-py39"] }

# Multi-thread runtime — one per Session, owned on the Rust side.
tokio = { version = "1", features = ["rt-multi-thread"] }
Expand All @@ -46,3 +52,10 @@ tokio = { version = "1", features = ["rt-multi-thread"] }
arrow-array = "57"
arrow-schema = "57"
arrow-pyarrow = "57"

# Required to implement the kernel's #[async_trait] TokenProvider on
# the Python-callable trampoline used by AuthConfig::External.
async-trait = "0.1"

[features]
extension-module = ["pyo3/extension-module"]
2 changes: 1 addition & 1 deletion pyo3/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ dependencies = ["pyarrow>=14"]

[tool.maturin]
module-name = "databricks_sql_kernel"
features = ["pyo3/extension-module"]
features = ["extension-module"]
70 changes: 70 additions & 0 deletions pyo3/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,76 @@ def main() -> None:
check("message" in attrs, "attrs has 'message'")
s2.close()

# --- External auth (Python-callable token provider) ---
#
# NOTE: the kernel today rejects `AuthConfig::External` at
# `build_auth_provider` ("reserved; v0 wires PAT + OAuthM2M +
# OAuthU2M only"). Until a follow-up kernel PR wires it into
# the HTTP client's `AuthProvider` adapter, opening a live
# session with `auth_type='external'` surfaces that kernel
# error. The smoke test verifies:
#
# 1. The wrapper accepts and validates the new kwargs.
# 2. Bad kwargs surface as `ValueError` at construction time
# (before the kernel ever sees them).
# 3. Calling `Session(auth_type='external', ...)` surfaces the
# kernel's "reserved" error verbatim — proving the path
# reaches the kernel and the trampoline is wired correctly.
#
# The trampoline itself (Python callback → Rust → kernel
# `TokenProvider`) is covered by Rust unit tests in
# `pyo3/src/auth.rs` so we know the GIL handoff works.

banner("input validation — missing auth")
try:
k.Session(host=HOST, http_path=HTTP_PATH)
check(False, "no auth should have raised ValueError")
except ValueError as e:
check(True, f"ValueError as expected: {e}")

banner("input validation — auth_type='external' without callback")
try:
k.Session(host=HOST, http_path=HTTP_PATH, auth_type="external")
check(False, "should have raised")
except ValueError as e:
check("token_callback" in str(e), f"ValueError mentions token_callback: {e}")

banner("input validation — non-callable callback")
try:
k.Session(
host=HOST,
http_path=HTTP_PATH,
auth_type="external",
token_callback="not-callable",
)
check(False, "should have raised")
except ValueError as e:
check("callable" in str(e), f"ValueError mentions callable: {e}")

banner("auth_type='external' surfaces the kernel's 'reserved' error today")

def ok_callback():
return TOKEN

try:
k.Session(
host=HOST,
http_path=HTTP_PATH,
auth_type="external",
token_callback=ok_callback,
)
check(False, "should have raised until kernel wires AuthConfig::External")
except k.KernelError as e:
attrs = e.args[1]
check(
attrs.get("code") == "InvalidArgument",
f"code=InvalidArgument, got: {attrs.get('code')}",
)
check(
"External" in attrs.get("message", ""),
f"message names External: {attrs.get('message')}",
)

print("\nAll smoke checks passed.")


Expand Down
217 changes: 217 additions & 0 deletions pyo3/src/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright (c) 2026 Databricks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Translation from Python auth kwargs to a kernel `AuthConfig`.
//!
//! Phase-1 surface covers PAT (Step 1) and `External` (this step).
//! `External` lets the connector keep ownership of its own OAuth /
//! MSAL / federation flows: it passes a Python callable that returns
//! a fresh bearer token, and the kernel calls it whenever it needs
//! one.
//!
//! Future steps add the kernel-driven OAuth variants
//! (`AuthConfig::OAuthM2M`, `OAuthU2M`, `OAuthFederation`) — useful
//! for callers that want the kernel to own the flow rather than
//! delegating back to Python.

use async_trait::async_trait;
use databricks_sql_kernel::config::TokenProvider;
use databricks_sql_kernel::{AuthConfig, Error, Result as KResult};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

/// A `TokenProvider` that delegates to a Python callable. The
/// callable is invoked once per HTTP request that needs auth, with
/// the GIL re-acquired on the calling tokio worker thread.
///
/// `Py<PyAny>` is `Send + Sync` in pyo3 (it's a reference-counted
/// pointer into the Python heap, not a borrow), so this struct
/// satisfies the kernel's `TokenProvider: Send + Sync` bound.
struct PythonTokenProvider {
callback: Py<PyAny>,
}

impl PythonTokenProvider {
fn new(callback: Py<PyAny>) -> Self {
Self { callback }
}
}

#[async_trait]
impl TokenProvider for PythonTokenProvider {
async fn token(&self) -> KResult<String> {
// The kernel calls us from a tokio worker without the GIL.
// Acquire it for the duration of one Python call, extract
// the result as a String, then drop the GIL again before
// returning the future to the kernel.
//
// Note: this is a *sync* GIL-held call to the user's Python
// callable. For typical auth providers (in-process token
// cache lookup, occasional refresh) this is fine and matches
// the connector's own auth-provider contract. If a wrapper
// ever needs to await Python coroutines here, the pattern
// is to make the callback return a token directly and own
// the async work itself.
Python::attach(|py| {
let result = self.callback.call0(py).map_err(|e| {
Error::unauthenticated().message(format!("Python token_callback raised: {e}"))
})?;
let token: String = result.extract(py).map_err(|e| {
Error::unauthenticated()
.message(format!("Python token_callback must return str: {e}"))
})?;
if token.is_empty() {
return Err(Error::unauthenticated()
.message("Python token_callback returned an empty string"));
}
Ok(token)
})
}
}

/// Build a kernel `AuthConfig` from the Python kwargs the `Session`
/// constructor accepts. Centralised here so the auth-kwarg surface
/// stays in one place.
///
/// `auth_type` is one of:
/// - `"pat"` — requires `access_token: str`.
/// - `"external"` — requires `token_callback: Callable[[], str]`.
pub(crate) fn build_auth_config(
auth_type: &str,
access_token: Option<String>,
token_callback: Option<Py<PyAny>>,
) -> PyResult<AuthConfig> {
match auth_type {
"pat" => {
let token = access_token
.ok_or_else(|| PyValueError::new_err("auth_type='pat' requires access_token"))?;
if token.is_empty() {
return Err(PyValueError::new_err("access_token must not be empty"));
}
Ok(AuthConfig::Pat { token })
}
"external" => {
let callback = token_callback.ok_or_else(|| {
PyValueError::new_err(
"auth_type='external' requires token_callback (a no-arg callable returning str)",
)
})?;
// Sanity check at construction time so a misspelled or
// non-callable arg surfaces immediately rather than on
// the first HTTP request.
Python::attach(|py| -> PyResult<()> {
if !callback.bind(py).is_callable() {
return Err(PyValueError::new_err("token_callback must be callable"));
}
Ok(())
})?;
Ok(AuthConfig::External(Box::new(PythonTokenProvider::new(
callback,
))))
}
other => Err(PyValueError::new_err(format!(
"unknown auth_type {other:?}; expected one of: pat, external"
))),
}
}

#[cfg(test)]
mod tests {
//! End-to-end coverage for the Python→Rust→kernel auth bridge.
//!
//! These tests exercise the `TokenProvider` trampoline without
//! needing a live workspace — we construct a `PythonTokenProvider`
//! directly, run its async `token()` method on a tokio runtime,
//! and check the value flows back correctly. This is the layer
//! the rest of Step 5 builds on; if it breaks we want to catch
//! it without spinning a Session against dogfood.
//!
//! `Python::initialize()` initialises the Python interpreter on
//! first call; subsequent calls are no-ops.

use super::*;
use databricks_sql_kernel::ErrorCode;
use pyo3::ffi::c_str;

fn rt() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
}

#[test]
fn python_token_provider_returns_callback_value() {
Python::initialize();
let callback: Py<PyAny> = Python::attach(|py| {
py.eval(c_str!("lambda: 'token-xyz'"), None, None)
.unwrap()
.unbind()
});
let provider = PythonTokenProvider::new(callback);
let got = rt().block_on(provider.token()).unwrap();
assert_eq!(got, "token-xyz");
}

#[test]
fn python_token_provider_maps_python_exception_to_unauthenticated() {
Python::initialize();
let callback: Py<PyAny> = Python::attach(|py| {
// Lambda raising RuntimeError. Done as exec+lookup so we
// can carry a statement (`raise ...`) instead of an
// expression.
let globals = pyo3::types::PyDict::new(py);
py.run(
c_str!("def cb():\n raise RuntimeError('simulated MSAL failure')\n"),
Some(&globals),
None,
)
.unwrap();
globals.get_item("cb").unwrap().unwrap().unbind()
});
let provider = PythonTokenProvider::new(callback);
let err = rt().block_on(provider.token()).unwrap_err();
assert_eq!(err.code, ErrorCode::Unauthenticated);
assert!(
err.message.contains("simulated MSAL failure"),
"message should preserve the Python exception text: {}",
err.message
);
}

#[test]
fn python_token_provider_rejects_non_string_return() {
Python::initialize();
let callback: Py<PyAny> = Python::attach(|py| {
py.eval(c_str!("lambda: 12345"), None, None)
.unwrap()
.unbind()
});
let provider = PythonTokenProvider::new(callback);
let err = rt().block_on(provider.token()).unwrap_err();
assert_eq!(err.code, ErrorCode::Unauthenticated);
assert!(err.message.contains("must return str"), "{}", err.message);
}

#[test]
fn python_token_provider_rejects_empty_string() {
Python::initialize();
let callback: Py<PyAny> =
Python::attach(|py| py.eval(c_str!("lambda: ''"), None, None).unwrap().unbind());
let provider = PythonTokenProvider::new(callback);
let err = rt().block_on(provider.token()).unwrap_err();
assert_eq!(err.code, ErrorCode::Unauthenticated);
assert!(err.message.contains("empty"), "{}", err.message);
}
}
7 changes: 5 additions & 2 deletions pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
//! Python bindings for the Databricks SQL Kernel.
//!
//! Phase 1 surface today: `Session`, `Statement`, `ExecutedStatement`,
//! `ExecutedAsyncStatement`, `Metadata`, `ResultStream`. PAT auth
//! only. Both synchronous `execute()` and async `submit()` paths
//! `ExecutedAsyncStatement`, `Metadata`, `ResultStream`. Auth: PAT
//! plus `External` (Python-callable token provider — used by the
//! connector to delegate OAuth back to its own auth provider).
//! Both synchronous `execute()` and async `submit()` paths
//! supported. Built as the `databricks_sql_kernel` extension module
//! via `maturin develop` / `maturin build`.
//!
//! The kernel's API is async — every method call from Python detaches
//! the GIL and `block_on`s on the session-owned tokio runtime.

mod auth;
mod errors;
mod executed_async;
mod fetch;
Expand Down
Loading