From 9954bf18273fb3d161100be9a613dbd2fff518ae Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Tue, 3 Nov 2026 21:17:02 +0000 Subject: [PATCH] fix(session): authenticate once before resolving feature flags The feature-flag prefetch was the first caller of the auth provider and swallows its errors, so a failed token request was sent again by CreateSession with no backoff. Authenticate once right after the provider is attached; the prefetch and CreateSession reuse the cached token. Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- NEXT_CHANGELOG.md | 2 + src/session.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 2ebce050..95b5af07 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -17,6 +17,8 @@ changes go in their respective binding's NEXT_CHANGELOG. ### 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. + --- *Note: When making changes, please add your change under the appropriate section with a brief description. At release time, move the entries here diff --git a/src/session.rs b/src/session.rs index 72d63d64..1a64a209 100644 --- a/src/session.rs +++ b/src/session.rs @@ -378,6 +378,11 @@ impl Session { let auth_provider: Arc = build_auth_provider(&config.auth, &host, http_client.clone()).await?; http_client.set_auth_provider(auth_provider); + // Authenticate before the feature-flag prefetch, which swallows its + // errors: otherwise a failed token request is sent again by + // CreateSession. Both reuse the token cached here. + http_client.auth_header()?; + http_client.auth_extra_headers()?; // 3. Transport. Two-phase: SeaRestTransport needs an // Arc to build the result-reader factory, @@ -2583,6 +2588,105 @@ mod is_open_tests { assert_eq!(create_session_requests, 2, "each open creates a session"); } + fn m2m_session_builder( + server: &MockServer, + workspace_id: &str, + driver_version: &str, + ) -> SessionBuilder { + Session::builder() + .connection( + ConnectionConfig::from_http_path( + server.uri(), + &format!("/sql/1.0/warehouses/test_wh?o={workspace_id}"), + ) + .expect("valid http_path"), + ) + .auth(AuthConfig::OAuthM2M { + client_id: "test-client-id".into(), + client_secret: "test-client-secret".into(), + scopes: None, + token_url_override: Some(format!("{}/oidc/v1/token", server.uri())), + identity_federation_client_id: None, + }) + .driver_system_configuration(DriverSystemConfiguration { + driver_name: Some("databricks-sql-go".to_string()), + driver_version: Some(driver_version.to_string()), + ..DriverSystemConfiguration::default() + }) + } + + #[tokio::test(flavor = "multi_thread")] + async fn failed_m2m_token_request_is_sent_once_when_feature_flags_are_enabled() { + // Test-specific workspace id keeps the process-wide flag cache cold. + const WORKSPACE_ID: &str = "3762431"; + let server = MockServer::start().await; + // apiproxy's auth-shedding reply. Retry-After exceeds the auth retry + // budget, so this request must not be retried by anyone. + Mock::given(method("POST")) + .and(path("/oidc/v1/token")) + .respond_with(ResponseTemplate::new(503).insert_header("retry-after", "60")) + .expect(1) + .mount(&server) + .await; + Mock::given(method("GET")) + .respond_with(ResponseTemplate::new(500)) + .expect(0) + .mount(&server) + .await; + Mock::given(method("POST")) + .and(path("/api/2.0/sql/warehouses/test_wh/sessions")) + .respond_with(ResponseTemplate::new(500)) + .expect(0) + .mount(&server) + .await; + + let err = m2m_session_builder(&server, WORKSPACE_ID, "m2m-token-failure") + .open() + .await + .expect_err("open must fail when the token request fails"); + + assert_eq!(err.http_status, Some(503), "{}", err.message); + server.verify().await; + } + + #[tokio::test(flavor = "multi_thread")] + async fn m2m_session_open_uses_one_token_for_feature_flags_and_create_session() { + const WORKSPACE_ID: &str = "3762432"; + const DRIVER_VERSION: &str = "m2m-single-token"; + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/oidc/v1/token")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "access_token": "m2m-token", + "token_type": "Bearer", + "expires_in": 3600 + }))) + .expect(1) + .mount(&server) + .await; + Mock::given(method("GET")) + .and(path(format!( + "/api/2.0/connector-service/feature-flags/GOLANG/{DRIVER_VERSION}" + ))) + .and(header("authorization", "Bearer m2m-token")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "flags": [], + "ttl_seconds": 300 + }))) + .expect(1) + .mount(&server) + .await; + mount_session_create_delete(&server, "sess-m2m-single-token").await; + + let session = m2m_session_builder(&server, WORKSPACE_ID, DRIVER_VERSION) + .open() + .await + .expect("session must open"); + session.close().await.expect("close succeeds"); + + server.verify().await; + } + #[tokio::test] async fn session_without_driver_identity_skips_feature_flag_resolution() { let server = MockServer::start().await;