Skip to content

Commit 136fd86

Browse files
fix(pyo3): default U2M scopes to ["sql","offline_access"] for the default Python client (#153)
## Summary - The pyo3 binding overrides the U2M `client_id` default to `databricks-sql-python` (to match the Thrift connector's OAuth app identity) but forwards `scopes=None` unchanged, so the kernel's intrinsic default of `["all-apis", "offline_access"]` is applied. `databricks-sql-python` is only registered for `sql` (+ `offline_access`) on the Databricks IdP, so an out-of-the-box `Session(auth_type="oauth-u2m")` from Python fails the authorization request: ``` access_denied — Scopes 'all-apis' are not assigned to the client databricks-sql-python ``` - Fix: introduce `PYSQL_OAUTH_SCOPES = ["sql", "offline_access"]` next to `PYSQL_OAUTH_CLIENT_ID` and default scopes to it when the caller passed **neither** `client_id` nor `scopes`. Matches the Thrift connector's `PYSQL_OAUTH_SCOPES` in `auth/auth.py`, restoring `use_kernel=True` drop-in parity. - A caller-supplied `client_id` still gets `scopes=None` forwarded (custom clients may legitimately request `all-apis`). An explicit `scopes` is always honoured verbatim. ## Test plan - [x] Unit tests updated/added in `pyo3/src/auth.rs`: - `u2m_defaults_client_id_and_scopes_to_python_connector` — both defaults applied together. - `u2m_explicit_client_id_overrides_default` — custom `client_id` keeps `scopes=None` (kernel default wins). - `u2m_explicit_scopes_are_honoured_verbatim` — caller-supplied scopes pass through unchanged with the default client. - [x] `cargo +stable fmt --all` + `cargo clippy --all-targets -- -D warnings` clean. - [x] `cargo test --lib --tests` — 14 passed, including all 9 auth tests. - [x] End-to-end against `e2-dogfood.staging.cloud.databricks.com`: - **Before fix:** `sql.connect(auth_type="databricks-oauth", use_kernel=True)` → `access_denied` from IdP. - **After fix:** same call (no `oauth_scopes`) opens a session, browser flow completes, `SELECT current_user()` returns the user. This pull request and its description were written by Isaac. Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
1 parent 34b4c20 commit 136fd86

2 files changed

Lines changed: 107 additions & 18 deletions

File tree

‎pyo3/NEXT_CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ changes go in the root `NEXT_CHANGELOG.md`.
1616
### Updated
1717

1818
### Fixed
19+
- `auth_type="oauth-u2m"` now defaults scopes to `["sql", "offline_access"]`
20+
when the caller passes neither `client_id` nor `scopes`. The Python binding
21+
defaults the U2M client to `databricks-sql-python` (vs. the kernel's
22+
`databricks-cli`), but that client isn't registered for the `all-apis` scope
23+
the kernel intrinsically defaults to — so an out-of-the-box
24+
`Session(auth_type="oauth-u2m")` failed the authorization request with
25+
`access_denied — Scopes 'all-apis' are not assigned to the client
26+
databricks-sql-python`. Defaults now match `PYSQL_OAUTH_SCOPES` in the
27+
standalone Thrift connector, restoring `use_kernel=True` parity. A
28+
caller-supplied `client_id` or `scopes` is still honoured verbatim.
1929

2030
---
2131
*Note: When making changes, please add your change under the appropriate

‎pyo3/src/auth.rs‎

Lines changed: 97 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ pub(crate) struct OAuthParams {
7676
/// OAuth app identity, not the on-disk cache.
7777
const PYSQL_OAUTH_CLIENT_ID: &str = "databricks-sql-python";
7878

79+
/// Default U2M scopes for the **Python** binding when the caller relies
80+
/// on the default `PYSQL_OAUTH_CLIENT_ID` above.
81+
///
82+
/// The kernel's intrinsic U2M default is `["all-apis", "offline_access"]`,
83+
/// which is correct for its bare-Rust default client (`databricks-cli`).
84+
/// But the Python binding overrides the default client to
85+
/// `databricks-sql-python` — and *that* client is only registered for
86+
/// `sql` (plus `offline_access`) on the Databricks IdP. Without a
87+
/// matching scope override here, an out-of-the-box
88+
/// `Session(auth_type="oauth-u2m")` from Python fails on the
89+
/// authorization request with
90+
/// `access_denied — Scopes 'all-apis' are not assigned to the client
91+
/// databricks-sql-python`.
92+
///
93+
/// Matches `PYSQL_OAUTH_SCOPES` in the Thrift connector
94+
/// (`databricks-sql-python` `auth/auth.py`), so `use_kernel=True` is
95+
/// drop-in compatible with the standalone Thrift path.
96+
///
97+
/// Only applied when the caller passed neither `client_id` nor `scopes`
98+
/// — a caller supplying their own `client_id` may legitimately want
99+
/// `all-apis` (or anything else), and an explicit `scopes` is always
100+
/// honoured verbatim.
101+
const PYSQL_OAUTH_SCOPES: &[&str] = &["sql", "offline_access"];
102+
79103
/// Build a kernel `AuthConfig` from the Python kwargs the `Session`
80104
/// constructor accepts.
81105
///
@@ -157,23 +181,39 @@ fn build_auth_config_inner(
157181
.to_owned(),
158182
);
159183
}
184+
// Default the U2M OAuth client to the Python connector's
185+
// own registered client (`databricks-sql-python`), NOT the
186+
// kernel's bare-Rust fallback (`databricks-cli`). This
187+
// matches the standalone Thrift connector
188+
// (`PYSQL_OAUTH_CLIENT_ID` in databricks-sql-python's
189+
// auth.py) so a user gets the same OAuth app identity — and
190+
// the same SSO token cache entry — whether they use the
191+
// Thrift backend or `use_kernel=True`. It also keeps the
192+
// kernel's per-{host,client_id,scopes} token cache distinct
193+
// from the napi (Node) binding's, which defaults to its own
194+
// client.
195+
//
196+
// When BOTH defaults are in play (caller passed no
197+
// `client_id` and no `scopes`), also default the scopes to
198+
// the Python-binding set so the default client/scopes pair
199+
// is internally consistent. The kernel's intrinsic scope
200+
// default is for its `databricks-cli` client, not for
201+
// `databricks-sql-python` — see `PYSQL_OAUTH_SCOPES` above.
202+
let caller_set_client_id = oauth.client_id.is_some();
203+
let client_id = oauth
204+
.client_id
205+
.or_else(|| Some(PYSQL_OAUTH_CLIENT_ID.to_owned()));
206+
let scopes = match oauth.scopes {
207+
Some(s) => Some(s),
208+
None if !caller_set_client_id => {
209+
Some(PYSQL_OAUTH_SCOPES.iter().map(|s| (*s).to_owned()).collect())
210+
}
211+
None => None,
212+
};
160213
Ok(AuthConfig::OAuthU2M {
161-
// Default the U2M OAuth client to the Python connector's
162-
// own registered client (`databricks-sql-python`), NOT
163-
// the kernel's bare-Rust fallback (`databricks-cli`).
164-
// This matches the standalone Thrift connector
165-
// (`PYSQL_OAUTH_CLIENT_ID` in databricks-sql-python's
166-
// auth.py) so a user gets the same OAuth app identity —
167-
// and the same SSO token cache entry — whether they use
168-
// the Thrift backend or `use_kernel=True`. It also keeps
169-
// the kernel's per-{host,client_id,scopes} token cache
170-
// distinct from the napi (Node) binding's, which defaults
171-
// to its own client.
172-
client_id: oauth
173-
.client_id
174-
.or_else(|| Some(PYSQL_OAUTH_CLIENT_ID.to_owned())),
214+
client_id,
175215
redirect_port: oauth.redirect_port,
176-
scopes: oauth.scopes,
216+
scopes,
177217
token_url_override: oauth.token_url,
178218
callback_timeout: oauth.callback_timeout_secs.map(Duration::from_secs),
179219
})
@@ -285,11 +325,16 @@ mod tests {
285325
}
286326

287327
#[test]
288-
fn u2m_defaults_client_id_to_python_connector() {
328+
fn u2m_defaults_client_id_and_scopes_to_python_connector() {
289329
// U2M takes no mandatory fields. The Python binding defaults the
290330
// OAuth client_id to `databricks-sql-python` (NOT the kernel's
291331
// bare `databricks-cli`) so it matches the standalone Thrift
292332
// connector and the napi binding gets a distinct cache key.
333+
// It also defaults the scopes to `["sql", "offline_access"]` to
334+
// match the scopes registered for that client on the Databricks
335+
// IdP — the kernel's intrinsic `all-apis` default is for its
336+
// own `databricks-cli` client, which `databricks-sql-python`
337+
// isn't allowed to request.
293338
let cfg = build_auth_config_inner("oauth-u2m", None, OAuthParams::default()).unwrap();
294339
match cfg {
295340
AuthConfig::OAuthU2M {
@@ -301,7 +346,10 @@ mod tests {
301346
} => {
302347
assert_eq!(client_id.as_deref(), Some("databricks-sql-python"));
303348
assert!(redirect_port.is_none());
304-
assert!(scopes.is_none());
349+
assert_eq!(
350+
scopes,
351+
Some(vec!["sql".to_string(), "offline_access".to_string()])
352+
);
305353
assert!(token_url_override.is_none());
306354
assert!(callback_timeout.is_none());
307355
}
@@ -321,8 +369,39 @@ mod tests {
321369
)
322370
.unwrap();
323371
match cfg {
324-
AuthConfig::OAuthU2M { client_id, .. } => {
372+
AuthConfig::OAuthU2M {
373+
client_id, scopes, ..
374+
} => {
325375
assert_eq!(client_id.as_deref(), Some("my-custom-app"));
376+
// Custom client_id without scopes: don't override the
377+
// kernel's default scopes — a custom client may
378+
// legitimately request `all-apis` (or anything else).
379+
assert!(scopes.is_none());
380+
}
381+
other => panic!("expected OAuthU2M, got {other:?}"),
382+
}
383+
}
384+
385+
#[test]
386+
fn u2m_explicit_scopes_are_honoured_verbatim() {
387+
// Caller-supplied scopes win over the Python-binding default,
388+
// both with the default client_id and with a custom one.
389+
let custom = vec!["all-apis".to_string(), "offline_access".to_string()];
390+
let cfg = build_auth_config_inner(
391+
"oauth-u2m",
392+
None,
393+
OAuthParams {
394+
scopes: Some(custom.clone()),
395+
..Default::default()
396+
},
397+
)
398+
.unwrap();
399+
match cfg {
400+
AuthConfig::OAuthU2M {
401+
client_id, scopes, ..
402+
} => {
403+
assert_eq!(client_id.as_deref(), Some("databricks-sql-python"));
404+
assert_eq!(scopes, Some(custom));
326405
}
327406
other => panic!("expected OAuthU2M, got {other:?}"),
328407
}

0 commit comments

Comments
 (0)