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
2 changes: 1 addition & 1 deletion .github/workflows/rust-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:

- name: Run v0 E2E tests
run: |
for suite in v0_session_e2e v0_execute_e2e v0_async_execute_e2e v0_metadata_e2e; do
for suite in v0_session_e2e v0_execute_e2e v0_async_execute_e2e v0_metadata_e2e v0_query_tags_headers_e2e; do
echo "::group::cargo test --test ${suite}"
cargo test --test "${suite}" -- --ignored --nocapture
echo "::endgroup::"
Expand Down
9 changes: 7 additions & 2 deletions napi/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ pub struct ExecuteOptions {
/// raises `InvalidArgument` — the caller's intent is ambiguous
/// so we refuse to silently pick one over the other.
///
/// A **`null`** value emits a **bare key** (no colon) — e.g.
/// `{ production: null }` → `"production"` — matching the
/// connectors' `key`-only tag form.
///
/// See the struct-level "Tag-order caveat" for the
/// HashMap-iteration-order vs `Object.keys`-iteration-order
/// divergence and the byte-identical-Thrift-parity workaround.
pub query_tags: Option<HashMap<String, String>>,
pub query_tags: Option<HashMap<String, Option<String>>>,
/// Server-side cap on the number of rows this statement returns
/// (SEA `row_limit`), independent of any SQL `LIMIT`. Maps to
/// `StatementSpec.row_limit`. Omitted ⇒ no driver-imposed cap.
Expand Down Expand Up @@ -199,7 +203,7 @@ fn build_statement(
session: &Session,
sql: String,
statement_conf: Option<HashMap<String, String>>,
query_tags: Option<HashMap<String, String>>,
query_tags: Option<HashMap<String, Option<String>>>,
row_limit: Option<i64>,
query_timeout_secs: Option<u32>,
positional_params: Vec<TypedValue>,
Expand All @@ -220,6 +224,7 @@ fn build_statement(
}
// Fold the `queryTags` convenience map into the conf overlay. Empty map is
// a no-op; a dual definition with `statementConf["query_tags"]` rejects.
// A `null` JS value maps to `None` → a bare key on the wire.
if let Some(tags) = query_tags {
stmt.spec().query_tags(tags).map_err(napi_err_from_kernel)?;
}
Expand Down
24 changes: 22 additions & 2 deletions pyo3/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ impl Session {
/// CloudFetch requests; token-endpoint (auth) calls keep a fixed
/// fast-fail policy.
///
/// `http_headers` is a list of `(name, value)` pairs sent on every
/// request — used to forward the connector's caller headers (which
/// also carry the composed `User-Agent`). The kernel applies them
/// before its own `Authorization` / `x-databricks-org-id` headers,
/// which always take precedence; a caller header for either of those
/// names is ignored.
///
/// `session_conf` is the server-bound session configuration map
/// — passed through to SEA `session_confs` verbatim. Use for
/// `ANSI_MODE`, `TIMEZONE`, `STATEMENT_TIMEOUT`, query-tag
Expand Down Expand Up @@ -119,6 +126,7 @@ impl Session {
retry_max_wait_secs=None,
retry_max_attempts=None,
retry_overall_timeout_secs=None,
http_headers=None,
catalog=None,
schema=None,
session_conf=None,
Expand Down Expand Up @@ -147,6 +155,7 @@ impl Session {
retry_max_wait_secs: Option<u64>,
retry_max_attempts: Option<u32>,
retry_overall_timeout_secs: Option<u64>,
http_headers: Option<Vec<(String, String)>>,
catalog: Option<String>,
schema: Option<String>,
session_conf: Option<HashMap<String, String>>,
Expand Down Expand Up @@ -199,8 +208,9 @@ impl Session {
None
};

// Build an HttpConfig only when the caller tuned retry/backoff;
// otherwise leave the builder on its defaults (1s/60s/5-retries).
// Build an HttpConfig only when the caller tuned retry/backoff
// or supplied custom headers; otherwise leave the builder on its
// defaults (1s/60s/5-retries, no extra headers).
//
// `retry_max_attempts` is the **total** number of attempts
// (matching the connector's `_retry_stop_after_attempts_count`
Expand All @@ -209,10 +219,17 @@ impl Session {
// `max(0, attempts - 1)` here — centralising the off-by-one so
// wrappers can pass an intuitive total. `retry_max_attempts = 0`
// or `1` both mean "one attempt, no retries".
//
// `http_headers` are extra headers sent on every request (the
// connector forwards its `http_headers`, which also carries the
// composed `User-Agent`). The kernel applies them before its own
// `Authorization` / `x-databricks-org-id`, which always win.
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
let http_config = if retry_min_wait_secs.is_some()
|| retry_max_wait_secs.is_some()
|| retry_max_attempts.is_some()
|| retry_overall_timeout_secs.is_some()
|| has_http_headers
{
let mut http = databricks_sql_kernel::HttpConfig::default();
if let Some(s) = retry_min_wait_secs {
Expand All @@ -227,6 +244,9 @@ impl Session {
if let Some(s) = retry_overall_timeout_secs {
http.overall_timeout = std::time::Duration::from_secs(s);
}
if let Some(headers) = http_headers {
http.custom_headers = headers;
}
Some(http)
} else {
None
Expand Down
27 changes: 27 additions & 0 deletions pyo3/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use databricks_sql_kernel::{
Statement as KStatement,
};
use pyo3::prelude::*;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -65,6 +66,32 @@ impl Statement {
Ok(())
}

/// Set per-statement query tags.
///
/// ``tags`` is a ``dict[str, str | None]`` — a ``None`` value emits
/// a bare key (no colon) on the wire, matching the connector's
/// ``Dict[str, Optional[str]]`` contract. The kernel serialises to
/// the SEA ``query_tags`` conf shape and merges it into the
/// per-statement conf overlay.
///
/// Raises ``KernelError(code='InvalidArgument')`` if a ``query_tags``
/// entry was already set explicitly via the statement conf (the
/// kernel refuses an ambiguous double-definition). An empty ``tags``
/// dict is a no-op.
fn set_query_tags(
&mut self,
py: Python<'_>,
tags: HashMap<String, Option<String>>,
) -> PyResult<()> {
let inner = alive_mut!(self, "Statement");
inner
.stmt
.spec()
.query_tags(tags)
.map_err(|e| to_py_err(py, e))?;
Ok(())
}

/// Bind a positional parameter.
///
/// ``ordinal`` is 1-based on the SEA wire. ``value_str`` is the
Expand Down
183 changes: 182 additions & 1 deletion src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ pub struct HttpClientConfig {
/// Databricks organization ID for multi-tenant workspace routing.
/// When set, sent as the `x-databricks-org-id` header on all authenticated requests.
pub org_id: Option<String>,
/// Extra headers applied to every request, before the kernel's own
/// `Authorization` / `x-databricks-org-id` (which are added last and
/// therefore win). A caller `User-Agent` here is folded into the
/// effective UA at construction (appended to the base UA, then
/// removed from this list). Populated from
/// [`crate::config::HttpConfig::custom_headers`]; see its docs for
/// the full precedence / reserved-name rules.
pub custom_headers: Vec<(String, String)>,
}

impl Default for HttpClientConfig {
Expand All @@ -224,6 +232,7 @@ impl Default for HttpClientConfig {
proxy: ProxyConfig::default(),
tls: TlsConfig::default(),
org_id: None,
custom_headers: Vec::new(),
}
}
}
Expand Down Expand Up @@ -309,9 +318,66 @@ impl DatabricksHttpClient {
/// calling `execute()`. Use `execute_without_auth()` for requests that
/// don't need authentication (e.g., OAuth token endpoint calls).
pub fn new(
config: HttpClientConfig,
mut config: HttpClientConfig,
retry_configs: HashMap<RequestCategory, RetryConfig>,
) -> Result<Self> {
// Fold any caller-supplied `User-Agent` custom header into the
// effective UA, and remove it from `custom_headers` so it isn't
// re-applied per-request. **`User-Agent` is never replaced** —
// the kernel's base UA carries the `DatabricksJDBCDriverOSS/...`
// token the SEA server keys on to enable the
// `INLINE_OR_EXTERNAL_LINKS` result disposition; a caller UA that
// replaced it would break result fetching (HTTP 400
// "not a supported disposition"). Instead we APPEND the caller's
// UA as a suffix: `"<base> <caller>"`. This preserves the
// disposition gating and honors partner attribution (the
// connector's composed `PyDatabricksSqlConnector/x (entry)` UA
// rides along after the base token).
// Last UA wins if a caller somehow set several.
if let Some(caller_ua) = config
.custom_headers
.iter()
.rfind(|(n, _)| n.eq_ignore_ascii_case("user-agent"))
.map(|(_, v)| v.clone())
{
config
.custom_headers
.retain(|(n, _)| !n.eq_ignore_ascii_case("user-agent"));
let caller_ua = caller_ua.trim();
// Guard against a pathologically large UA on every request.
// 1 KiB is far above any legitimate `product/version (entry)`
// string; truncate (don't reject) so an over-long entry
// degrades gracefully rather than failing the session.
const MAX_UA_SUFFIX: usize = 1024;
if caller_ua.len() > MAX_UA_SUFFIX {
warn!(
len = caller_ua.len(),
"caller User-Agent suffix exceeds {MAX_UA_SUFFIX} bytes; truncating"
);
}
let caller_ua = &caller_ua[..caller_ua.len().min(MAX_UA_SUFFIX)];
if !caller_ua.is_empty() {
config.user_agent = format!("{} {}", config.user_agent, caller_ua);
}
}

// Validate the remaining custom headers up front so a malformed
// name/value fails at session-open rather than on every request
// build (where it would surface as a confusing per-request
// error). Reserved names are still dropped at request time; we
// only reject structurally-invalid headers here.
for (name, value) in &config.custom_headers {
reqwest::header::HeaderName::try_from(name.as_str()).map_err(|e| {
Error::invalid_argument()
.message(format!("invalid custom HTTP header name {name:?}: {e}"))
})?;
reqwest::header::HeaderValue::try_from(value.as_str()).map_err(|e| {
Error::invalid_argument().message(format!(
"invalid custom HTTP header value for {name:?}: {e}"
))
})?;
}

let mut builder = Client::builder()
.connect_timeout(config.connect_timeout)
.timeout(config.read_timeout)
Expand Down Expand Up @@ -616,6 +682,38 @@ impl DatabricksHttpClient {
// Build a fresh request for this attempt
let mut req_builder = self.client.request(method.clone(), url.clone());

// Caller-supplied custom headers go on FIRST, so the
// kernel's own auth / routing headers (set last, below) take
// precedence — matching the Thrift connector, where
// `auth_provider.add_headers` runs after the custom-header
// dict. We SKIP any custom header named `Authorization` or
// `x-databricks-org-id` (case-insensitive) so a stray caller
// header can't sit alongside the kernel's (reqwest appends
// same-named headers rather than replacing, which for those
// two would send a duplicate and could confuse the server).
// `User-Agent` is NOT in this map — it was folded into the
// client's base UA in `new()` (appended, never replacing,
// to preserve the disposition-gating JDBC token).
//
// Note: this loop runs for BOTH `execute` and
// `execute_without_auth`, so custom headers (and the merged
// User-Agent) also ride OAuth token-endpoint requests. This
// is intentional and matches the Thrift connector, where
// `setCustomHeaders` applies to every request on the
// transport, IdP calls included.
for (name, value) in &self.config.custom_headers {
let lname = name.to_ascii_lowercase();
if lname == "authorization" || lname == "x-databricks-org-id" {
warn!(
header = %name,
"ignoring caller-supplied custom header; this name is \
managed by the kernel and cannot be overridden"
);
continue;
}
req_builder = req_builder.header(name, value);
}

for (name, value) in headers.iter() {
req_builder = req_builder.header(name, value);
}
Expand Down Expand Up @@ -830,6 +928,89 @@ mod tests {
assert_eq!(header, "Bearer test-token");
}

#[tokio::test]
async fn test_custom_headers_applied_with_auth_winning() {
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

let server = MockServer::start().await;

// Expect: our benign custom header present; the caller's
// User-Agent is APPENDED to the kernel's base UA (never
// replacing it — the base token gates the SEA result
// disposition); Authorization is the kernel's real token (NOT
// the caller's bogus one).
Mock::given(method("GET"))
.and(path("/probe"))
.and(header("x-custom-header", "custom-value"))
.and(header(
"user-agent",
"KernelBase/9.9 MyConnector/1.0 (entry)",
))
.and(header("authorization", "Bearer real-token"))
.respond_with(ResponseTemplate::new(200))
.mount(&server)
.await;

let config = HttpClientConfig {
// Fixed base UA so the merged-UA assertion is deterministic
// (the real default embeds the crate version).
user_agent: "KernelBase/9.9".to_string(),
custom_headers: vec![
("x-custom-header".to_string(), "custom-value".to_string()),
(
"User-Agent".to_string(),
"MyConnector/1.0 (entry)".to_string(),
),
// Caller attempt to override auth — must be ignored; the
// kernel's real token wins.
(
"Authorization".to_string(),
"Bearer caller-bogus".to_string(),
),
],
..HttpClientConfig::default()
};
let client = DatabricksHttpClient::new(config, default_retry_configs()).unwrap();
client.set_auth_provider(Arc::new(PersonalAccessToken::new("real-token".to_string())));

let req = client
.inner()
.request(reqwest::Method::GET, format!("{}/probe", server.uri()))
.build()
.unwrap();
let resp = client
.execute(req, RequestType::GetStatementStatus)
.await
.expect("request should succeed against the mock");
assert_eq!(resp.status().as_u16(), 200);
// If the matchers above didn't all match, wiremock returns the
// default 404 and the status assert fails — so a 200 confirms
// custom header + caller UA present AND auth = real-token.
}

#[test]
fn test_malformed_custom_header_rejected_at_construction() {
// A structurally-invalid header name must fail at `new()`
// (session-open) rather than on the first request build.
let bad_name = HttpClientConfig {
custom_headers: vec![("Bad Header Name".to_string(), "v".to_string())],
..HttpClientConfig::default()
};
let err = DatabricksHttpClient::new(bad_name, default_retry_configs()).unwrap_err();
assert_eq!(err.code, crate::kernel_error::ErrorCode::InvalidArgument);
assert!(err.message.contains("header name"), "{}", err.message);

// An invalid header value (control char) is likewise rejected.
let bad_value = HttpClientConfig {
custom_headers: vec![("X-Custom".to_string(), "bad\nvalue".to_string())],
..HttpClientConfig::default()
};
let err = DatabricksHttpClient::new(bad_value, default_retry_configs()).unwrap_err();
assert_eq!(err.code, crate::kernel_error::ErrorCode::InvalidArgument);
assert!(err.message.contains("header value"), "{}", err.message);
}

#[tokio::test]
#[should_panic(expected = "Auth provider can only be set once")]
async fn test_set_auth_provider_twice_panics() {
Expand Down
Loading
Loading