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
7 changes: 7 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ changes go in their respective binding's NEXT_CHANGELOG.
with an optional partner entry from `adbc.databricks.user_agent_entry`
(`adbc.spark.user_agent_entry` is a compatibility alias). `User-Agent` in
`adbc.databricks.http.headers` is now rejected; use the dedicated entry option.
- **Breaking (Rust API):** `ClientCredentialsProvider`,
`JwtClientCredentialsProvider` and `AuthorizationCodeProvider` constructors
take `&Arc<DatabricksHttpClient>` instead of `Arc<DatabricksHttpClient>` and
hold it weakly, matching `FederationProvider`. Callers must keep the client
alive for as long as they use the provider; a token fetch after the client is
dropped fails with `invalid_state`.

### Updated

### Fixed

- `Session::open` authenticates once before resolving feature flags. A failed OAuth token request now fails the open immediately instead of being sent a second time by CreateSession.
- A timed-out or reset CloudFetch download no longer writes the presigned URL's signature (`sig=`, `X-Amz-Signature`) to the log or the returned error. The request URL in those messages now ends at the path, and debug request lines stop at `?`.
- Closing an OAuth session (M2M, M2M private key, U2M or Azure service principal) now frees its HTTP client and auth provider. The provider held the HTTP client strongly while the client owned the provider, so the pair and the provider's tokio runtime handle leaked for every closed session.

---
*Note: When making changes, please add your change under the appropriate
Expand Down
10 changes: 5 additions & 5 deletions src/auth/oauth/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl AzureServicePrincipalProvider {
azure_client_secret: &str,
tenant: &str,
workspace_resource_id: Option<String>,
http_client: std::sync::Arc<crate::client::http::DatabricksHttpClient>,
http_client: &std::sync::Arc<crate::client::http::DatabricksHttpClient>,
) -> crate::kernel_error::Result<Self> {
Self::new_with_token_url(
host,
Expand All @@ -185,13 +185,13 @@ impl AzureServicePrincipalProvider {
azure_client_secret: &str,
token_url: String,
workspace_resource_id: Option<String>,
http_client: std::sync::Arc<crate::client::http::DatabricksHttpClient>,
http_client: &std::sync::Arc<crate::client::http::DatabricksHttpClient>,
) -> crate::kernel_error::Result<Self> {
let data = super::m2m::ClientCredentialsProvider::new_with_full_config(
host,
azure_client_id,
azure_client_secret,
http_client.clone(),
http_client,
vec![sp_m2m_scope(host)],
Some(token_url.clone()),
)
Expand Down Expand Up @@ -586,7 +586,7 @@ mod tests {
"sp-client-secret",
token_url,
Some("/subscriptions/s/resourceGroups/rg/workspace/w".to_string()),
http_client,
&http_client,
)
.await
.expect("provider builds");
Expand Down Expand Up @@ -619,7 +619,7 @@ mod tests {
"sp-client-secret",
token_url,
None,
http_client,
&http_client,
)
.await
.expect("provider builds");
Expand Down
4 changes: 1 addition & 3 deletions src/auth/oauth/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ impl FederationProvider {
/// Performs the token exchange and returns the in-house token. Errors here
/// are turned into the pass-through fallback by the caller.
async fn exchange(&self, source_token: &str) -> Result<OAuthToken> {
let http_client = self.http_client.upgrade().ok_or_else(|| {
Error::invalid_state().message("Token federation HTTP client is no longer available")
})?;
let http_client = super::upgrade_http_client(&self.http_client)?;
let mut form: Vec<(&str, &str)> = vec![
("grant_type", GRANT_TYPE_JWT_BEARER),
("assertion", source_token),
Expand Down
70 changes: 55 additions & 15 deletions src/auth/oauth/jwt_m2m.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! M2M tokens are not cached to disk (same rationale as [`super::m2m`]):
//! the private key is always available for re-authentication.

use std::sync::Arc;
use std::sync::{Arc, Weak};

use oauth2::basic::BasicTokenType;
use oauth2::{EmptyExtraTokenFields, StandardTokenResponse, TokenResponse as _};
Expand Down Expand Up @@ -112,8 +112,9 @@ pub struct JwtClientCredentialsProvider {
signing_key: Arc<JwtSigningKey>,
/// Thread-safe token store managing the token lifecycle.
token_store: TokenStore,
/// HTTP client for token endpoint requests.
http_client: Arc<DatabricksHttpClient>,
/// HTTP client for token endpoint requests. Weak; see
/// [`super::upgrade_http_client`].
http_client: Weak<DatabricksHttpClient>,
/// OAuth scopes to request.
scopes: Vec<String>,
/// Tokio runtime handle captured at construction so the sync
Expand Down Expand Up @@ -152,7 +153,8 @@ impl JwtClientCredentialsProvider {
/// * `jwt_kid` — key id for the JWT header.
/// * `jwt_passphrase` — passphrase for an encrypted PKCS#8 key, else `None`.
/// * `jwt_algorithm` — JWT algorithm name (`None` → `RS256`).
/// * `http_client` — shared HTTP client.
/// * `http_client` — shared HTTP client. The caller must keep it alive;
/// the provider holds only a weak reference.
/// * `scopes` — OAuth scopes to request.
/// * `token_endpoint_override` — token endpoint URL, or `None` to discover via OIDC.
#[allow(clippy::too_many_arguments)]
Expand All @@ -163,7 +165,7 @@ impl JwtClientCredentialsProvider {
jwt_kid: &str,
jwt_passphrase: Option<&str>,
jwt_algorithm: Option<&str>,
http_client: Arc<DatabricksHttpClient>,
http_client: &Arc<DatabricksHttpClient>,
scopes: Vec<String>,
token_endpoint_override: Option<String>,
) -> Result<Self> {
Expand All @@ -178,7 +180,7 @@ impl JwtClientCredentialsProvider {
let token_endpoint = match token_endpoint_override {
Some(endpoint) => endpoint,
None => {
OidcEndpoints::discover(host, &http_client)
OidcEndpoints::discover(host, http_client)
.await?
.token_endpoint
}
Expand All @@ -190,7 +192,7 @@ impl JwtClientCredentialsProvider {
token_endpoint,
signing_key,
token_store: TokenStore::new(),
http_client,
http_client: Arc::downgrade(http_client),
scopes,
runtime_handle: RuntimeHandle::current(),
})
Expand Down Expand Up @@ -246,6 +248,7 @@ impl AuthProvider for JwtClientCredentialsProvider {
let jwt_kid = self.jwt_kid.clone();

let token = self.token_store.get_or_refresh(move || {
let http_client = super::upgrade_http_client(&http_client)?;
// Sign a fresh assertion only now that a refresh is actually
// running (fresh-token calls return before reaching here).
let client_assertion =
Expand Down Expand Up @@ -379,14 +382,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
Some("RS256"),
http_client(),
&client,
vec!["all-apis".to_string()],
None,
)
Expand Down Expand Up @@ -423,14 +427,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
Some("RS256"),
http_client(),
&client,
vec!["all-apis".to_string()],
None,
)
Expand Down Expand Up @@ -491,14 +496,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
None,
http_client(),
&client,
vec!["all-apis".to_string()],
None,
)
Expand Down Expand Up @@ -527,14 +533,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
None,
http_client(),
&client,
vec!["all-apis".to_string()],
Some(token_url),
)
Expand All @@ -560,7 +567,7 @@ mod tests {
"test-kid",
None,
Some("ES512"),
http_client(),
&http_client(),
vec!["all-apis".to_string()],
None,
)
Expand All @@ -582,7 +589,7 @@ mod tests {
"test-kid",
None,
None,
http_client(),
&http_client(),
vec!["all-apis".to_string()],
None,
)
Expand All @@ -605,14 +612,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
None,
http_client(),
&client,
vec!["all-apis".to_string()],
None,
)
Expand Down Expand Up @@ -650,14 +658,15 @@ mod tests {
.await;

let (_dir, key_path) = write_test_key();
let client = http_client();
let provider = JwtClientCredentialsProvider::new(
&server.uri(),
"test-client-id",
&key_path,
"test-kid",
None,
None,
http_client(),
&client,
vec!["all-apis".to_string()],
None,
)
Expand All @@ -668,4 +677,35 @@ mod tests {
assert_eq!(err.code, crate::kernel_error::ErrorCode::Unauthenticated);
assert_eq!(err.http_status, Some(400));
}

/// The HTTP client owns its auth provider, so a provider holding the client
/// strongly would leak both, plus the provider's tokio runtime handle.
#[tokio::test(flavor = "multi_thread")]
async fn provider_does_not_retain_http_client() {
let (_dir, key_path) = write_test_key();
let client = http_client();
let weak_client = Arc::downgrade(&client);
let provider = Arc::new(
JwtClientCredentialsProvider::new(
"https://ws.cloud.databricks.com",
"test-client-id",
&key_path,
"test-kid",
None,
None,
&client,
vec!["all-apis".to_string()],
Some("https://ws.cloud.databricks.com/oidc/v1/token".to_string()),
)
.await
.expect("provider"),
);
let weak_provider = Arc::downgrade(&provider);
client.set_auth_provider(provider);

drop(client);

assert!(weak_client.upgrade().is_none());
assert!(weak_provider.upgrade().is_none());
}
}
Loading
Loading