Skip to content
Merged
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
3 changes: 3 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ changes go in their respective binding's NEXT_CHANGELOG.
`identity_federation_client_id` fields on the PAT, M2M, and U2M `AuthConfig`
variants select SP-wide WIF; unset means BYOT / account-wide WIF. Follows the
ADBC wire contract (`grant_type=jwt-bearer`, `assertion`, `scope=sql`).
- Auth constructors now accept the optional federation client id directly;
the C ABI exposes it through
`kernel_session_config_set_identity_federation_client_id()`.

### Updated

Expand Down
6 changes: 6 additions & 0 deletions include/databricks_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ KernelStatusCode kernel_session_config_set_auth_u2m(KernelSessionConfig* config,
uint16_t redirect_port,
const char* scopes);

/* Optional SP-wide Workload Identity Federation client id used by mandatory
* token exchange. Unset selects BYOT / account-wide WIF. */
KernelStatusCode
kernel_session_config_set_identity_federation_client_id(KernelSessionConfig* config,
const char* client_id);

/* Add (or overwrite) one session-conf entry. Keys are normally server SET
* parameters forwarded on the SEA wire (allowlist-filtered). A small set of
* reserved keys are instead CLIENT-ONLY: they tune the kernel locally, are
Expand Down
3 changes: 3 additions & 0 deletions napi/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ layout, or npm publishing. Pure-kernel changes go in the root

### Added

- `ConnectionOptions.identityFederationClientId` selects SP-wide Workload
Identity Federation during mandatory token exchange.

### Updated

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export interface ConnectionOptions {
* (`["all-apis"]` for M2M; `["sql", "offline_access"]` for U2M).
*/
oauthScopes?: Array<string>
/**
* SP-wide Workload Identity Federation client id used during mandatory
* token exchange. Omitted selects BYOT / account-wide WIF.
*/
identityFederationClientId?: string
/**
* Default catalog for statements executed on this session.
* Routed through the kernel's `DefaultOpts` and onto the SEA
Expand Down
42 changes: 41 additions & 1 deletion napi/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ pub struct ConnectionOptions {
/// OAuth scopes override (M2M / U2M). Omitted ⇒ kernel defaults
/// (`["all-apis"]` for M2M; `["sql", "offline_access"]` for U2M).
pub oauth_scopes: Option<Vec<String>>,
/// SP-wide Workload Identity Federation client id used during mandatory
/// token exchange. Omitted selects BYOT / account-wide WIF.
pub identity_federation_client_id: Option<String>,
/// Default catalog for statements executed on this session.
/// Routed through the kernel's `DefaultOpts` and onto the SEA
/// `CreateSession.catalog` wire field.
Expand Down Expand Up @@ -334,12 +337,17 @@ pub struct ConnectionOptions {
/// session.
fn build_auth_config(options: &ConnectionOptions) -> napi::Result<AuthConfig> {
match options.auth_mode.unwrap_or_default() {
AuthMode::Pat => AuthConfig::pat(options.token.clone()).map_err(napi_err_from_kernel),
AuthMode::Pat => AuthConfig::pat(
options.token.clone(),
options.identity_federation_client_id.clone(),
)
.map_err(napi_err_from_kernel),
AuthMode::OAuthM2m => AuthConfig::oauth_m2m(
options.oauth_client_id.clone(),
options.oauth_client_secret.clone(),
options.oauth_scopes.clone(),
None,
options.identity_federation_client_id.clone(),
)
.map_err(napi_err_from_kernel),
// napi injects no client_id/scope default: it rides the kernel's
Expand Down Expand Up @@ -369,6 +377,7 @@ fn build_auth_config(options: &ConnectionOptions) -> napi::Result<AuthConfig> {
// binding exposes no passphrase option. Surfacing one through
// `options` would strengthen it further.
None,
options.identity_federation_client_id.clone(),
))
}
}
Expand Down Expand Up @@ -586,6 +595,7 @@ mod tests {
oauth_client_secret: None,
oauth_redirect_port: None,
oauth_scopes: None,
identity_federation_client_id: None,
catalog: None,
schema: None,
session_conf: None,
Expand Down Expand Up @@ -653,6 +663,24 @@ mod tests {
);
}

#[test]
fn federation_client_id_reaches_auth_config() {
let mut o = base();
o.token = Some("dapi-xxx".to_owned());
o.identity_federation_client_id = Some("federation-client".to_owned());

match build_auth_config(&o).unwrap() {
AuthConfig::Pat {
identity_federation_client_id,
..
} => assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
),
other => panic!("expected Pat, got {other:?}"),
}
}

#[test]
fn m2m_requires_client_id_and_secret() {
let mut o = base();
Expand All @@ -662,16 +690,22 @@ mod tests {
assert_invalid_arg(build_auth_config(&o).unwrap_err(), "client_secret");
o.oauth_client_secret = Some("sec".to_owned());
o.oauth_scopes = Some(vec!["all-apis".to_owned()]);
o.identity_federation_client_id = Some("federation-client".to_owned());
match build_auth_config(&o).unwrap() {
AuthConfig::OAuthM2M {
client_id,
client_secret,
scopes,
identity_federation_client_id,
..
} => {
assert_eq!(client_id, "cid");
assert_eq!(client_secret, "sec");
assert_eq!(scopes, Some(vec!["all-apis".to_owned()]));
assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
);
}
other => panic!("expected OAuthM2M, got {other:?}"),
}
Expand Down Expand Up @@ -701,14 +735,20 @@ mod tests {
// Explicit overrides thread through.
o.oauth_client_id = Some("custom-cli".to_owned());
o.oauth_redirect_port = Some(9000);
o.identity_federation_client_id = Some("federation-client".to_owned());
match build_auth_config(&o).unwrap() {
AuthConfig::OAuthU2M {
client_id,
redirect_port,
identity_federation_client_id,
..
} => {
assert_eq!(client_id.as_deref(), Some("custom-cli"));
assert_eq!(redirect_port, Some(9000));
assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
);
}
other => panic!("expected OAuthU2M, got {other:?}"),
}
Expand Down
3 changes: 3 additions & 0 deletions pyo3/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ changes go in the root `NEXT_CHANGELOG.md`.

### Added

- `Session(..., identity_federation_client_id=...)` selects SP-wide Workload
Identity Federation during mandatory token exchange.

### Updated

### Fixed
Expand Down
65 changes: 44 additions & 21 deletions pyo3/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub(crate) struct OAuthParams {
pub token_url: Option<String>,
pub redirect_port: Option<u16>,
pub callback_timeout_secs: Option<u64>,
pub identity_federation_client_id: Option<String>,
}

/// Default U2M OAuth client id for the **Python** binding.
Expand Down Expand Up @@ -144,10 +145,8 @@ fn build_auth_config_inner(
if token.is_empty() {
return Err("access_token must not be empty".to_owned());
}
Ok(AuthConfig::Pat {
token,
identity_federation_client_id: None,
})
AuthConfig::pat(Some(token), oauth.identity_federation_client_id)
.map_err(|err| err.to_string())
}
"oauth-m2m" => {
// Client credentials grant: both halves are mandatory.
Expand All @@ -156,13 +155,14 @@ fn build_auth_config_inner(
oauth.client_secret,
"auth_type='oauth-m2m' requires client_secret",
)?;
Ok(AuthConfig::OAuthM2M {
client_id,
client_secret,
scopes: oauth.scopes,
token_url_override: oauth.token_url,
identity_federation_client_id: None,
})
AuthConfig::oauth_m2m(
Some(client_id),
Some(client_secret),
oauth.scopes,
oauth.token_url,
oauth.identity_federation_client_id,
)
.map_err(|err| err.to_string())
}
"oauth-u2m" => {
// Browser flow: every field is optional and `None` lets the
Expand Down Expand Up @@ -240,22 +240,22 @@ fn build_auth_config_inner(
None if !caller_set_client_id => Some(PYSQL_OAUTH_REDIRECT_PORT),
None => None,
};
Ok(AuthConfig::OAuthU2M {
Ok(AuthConfig::oauth_u2m(
client_id,
redirect_port,
scopes,
token_url_override: oauth.token_url,
callback_timeout: oauth.callback_timeout_secs.map(Duration::from_secs),
oauth.token_url,
oauth.callback_timeout_secs.map(Duration::from_secs),
// `None` takes the kernel default: on-disk cache ENABLED,
// encrypted under a machine-local derived key since this
// binding exposes no passphrase option. That preserves
// cross-connection SSO reuse (the point of the cache) without
// ever writing the refresh token in the clear. Surfacing a
// passphrase through `OAuthParams` — the equivalent of JDBC's
// TokenCachePassPhrase — would strengthen it further.
token_cache: None,
identity_federation_client_id: None,
})
None,
oauth.identity_federation_client_id,
))
}
other => Err(format!(
"unsupported auth_type {other:?}; expected one of 'pat', 'oauth-m2m', 'oauth-u2m'"
Expand Down Expand Up @@ -288,9 +288,22 @@ mod tests {

#[test]
fn pat_builds() {
let cfg =
build_auth_config_inner("pat", Some("dapi123".into()), OAuthParams::default()).unwrap();
assert!(matches!(cfg, AuthConfig::Pat { token, .. } if token == "dapi123"));
let cfg = build_auth_config_inner(
"pat",
Some("dapi123".into()),
OAuthParams {
identity_federation_client_id: Some("federation-client".into()),
..Default::default()
},
)
.unwrap();
assert!(matches!(
cfg,
AuthConfig::Pat {
token,
identity_federation_client_id: Some(client_id),
} if token == "dapi123" && client_id == "federation-client"
));
}

#[test]
Expand Down Expand Up @@ -343,6 +356,7 @@ mod tests {
client_secret: Some("sec".into()),
scopes: Some(vec!["all-apis".into()]),
token_url: Some("https://example/token".into()),
identity_federation_client_id: Some("federation-client".into()),
..Default::default()
},
)
Expand All @@ -359,7 +373,10 @@ mod tests {
assert_eq!(client_secret, "sec");
assert_eq!(scopes, Some(vec!["all-apis".to_string()]));
assert_eq!(token_url_override.as_deref(), Some("https://example/token"));
assert!(identity_federation_client_id.is_none());
assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
);
}
other => panic!("expected OAuthM2M, got {other:?}"),
}
Expand Down Expand Up @@ -471,6 +488,7 @@ mod tests {
OAuthParams {
redirect_port: Some(9999),
callback_timeout_secs: Some(300),
identity_federation_client_id: Some("federation-client".into()),
..Default::default()
},
)
Expand All @@ -479,10 +497,15 @@ mod tests {
AuthConfig::OAuthU2M {
redirect_port,
callback_timeout,
identity_federation_client_id,
..
} => {
assert_eq!(redirect_port, Some(9999));
assert_eq!(callback_timeout, Some(Duration::from_secs(300)));
assert_eq!(
identity_federation_client_id.as_deref(),
Some("federation-client")
);
}
other => panic!("expected OAuthU2M, got {other:?}"),
}
Expand Down
7 changes: 7 additions & 0 deletions pyo3/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ impl Session {
/// OAuth is kernel-owned: the kernel acquires and transparently
/// refreshes tokens itself.
///
/// `identity_federation_client_id` selects SP-wide Workload Identity
/// Federation during mandatory token exchange. Omit it for BYOT /
/// account-wide WIF.
///
/// TLS: all knobs are optional relaxations / additions on top of
/// the secure default (validate system trust store, verify chain
/// + hostname). `tls_ca_cert` adds a PEM CA bundle (bytes);
Expand Down Expand Up @@ -118,6 +122,7 @@ impl Session {
token_url=None,
redirect_port=None,
oauth_callback_timeout_secs=None,
identity_federation_client_id=None,
tls_ca_cert=None,
tls_skip_verify=false,
tls_skip_hostname_verify=false,
Expand Down Expand Up @@ -147,6 +152,7 @@ impl Session {
token_url: Option<String>,
redirect_port: Option<u16>,
oauth_callback_timeout_secs: Option<u64>,
identity_federation_client_id: Option<String>,
tls_ca_cert: Option<Vec<u8>>,
tls_skip_verify: bool,
tls_skip_hostname_verify: bool,
Expand Down Expand Up @@ -193,6 +199,7 @@ impl Session {
token_url,
redirect_port,
callback_timeout_secs: oauth_callback_timeout_secs,
identity_federation_client_id,
};
let auth_config = build_auth_config(&resolved_auth_type, access_token, oauth)?;

Expand Down
9 changes: 9 additions & 0 deletions pyo3/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ def test_unknown_auth_type_raises_value_error():
auth_type="something-else",
access_token="x",
)


def test_identity_federation_client_id_is_accepted_kwarg():
with pytest.raises(ValueError, match="access_token"):
k.Session(
host="x.databricks.com",
http_path="/sql/1.0/warehouses/abc",
identity_federation_client_id="federation-client",
)
Loading
Loading