From 1f2aa2430d1424fff8eea995fe8d2a7200e94c93 Mon Sep 17 00:00:00 2001 From: eric-wang-1990 Date: Fri, 21 Aug 2026 00:38:58 -0700 Subject: [PATCH 1/2] feat(napi): thread Azure service-principal M2M (AzureSpM2m) through the binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add AuthMode::AzureSpM2m to the napi binding, mirroring the pyo3 auth_type="azure-sp-m2m" surface added for Python. New ConnectionOptions fields: azureClientId + azureClientSecret (required), plus optional azureTenantId (kernel auto-discovers from the workspace /aad/auth redirect when omitted) and azureWorkspaceResourceId (when set, the kernel also sends the Azure SP management token + X-Databricks-Azure-Workspace-Resource-Id header, so an SP with an Azure RBAC role but no workspace membership can authenticate). The build_auth_config arm forwards to the kernel's AuthConfig::azure_service_principal; the kernel owns Azure resolution. Azure AD U2M needs no new mode — it rides the existing AuthMode::OAuthU2m (the kernel's workspace-federated browser flow, which Azure workspaces support). Regenerated napi/index.d.ts to match. Stacked on the pyo3/core azure-sp-m2m PR (#263), which is where AuthConfig::azure_service_principal lives. Co-authored-by: Isaac Signed-off-by: eric-wang-1990 --- napi/NEXT_CHANGELOG.md | 12 +++++ napi/index.d.ts | 41 ++++++++++++++- napi/src/database.rs | 115 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 3 deletions(-) diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index 8484cf0d..9fca102e 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -30,6 +30,18 @@ layout, or npm publishing. Pure-kernel changes go in the root `token_url_override`. - `ConnectionOptions.identityFederationClientId` selects SP-wide Workload Identity Federation during mandatory token exchange. +- `AuthMode.AzureSpM2m` — Azure Entra service-principal M2M. New + `ConnectionOptions` fields `azureClientId` (required) + `azureClientSecret` + (required), plus optional `azureTenantId` (kernel auto-discovers it from the + workspace's `/aad/auth` redirect when omitted) and `azureWorkspaceResourceId` + (when set, the kernel also sends the Azure SP management token + + `X-Databricks-Azure-Workspace-Resource-Id` header, so a service principal with + an Azure RBAC role but no workspace membership can authenticate; omitted ⇒ the + data token authenticates alone). Forwards to the kernel's + `AuthConfig::azure_service_principal`. The napi analog of the pyo3 + `auth_type="azure-sp-m2m"` surface. Azure AD **U2M** needs no new mode — it + rides `AuthMode.OAuthU2m` (the kernel's workspace-federated browser flow, + which Azure workspaces support). ### Updated diff --git a/napi/index.d.ts b/napi/index.d.ts index 4da03ecd..9c4065b8 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -100,7 +100,7 @@ export interface NamedTypedValueInput { * Authentication mode selector crossing the napi boundary. The string * literals are what napi-rs emits from this `#[napi(string_enum)]` — the * NodeJS SEA adapter (`KernelAuth`) matches them verbatim (`'Pat'`, - * `'OAuthM2m'`, `'OAuthU2m'`). + * `'OAuthM2m'`, `'OAuthU2m'`, `'AzureSpM2m'`). * * Mirrors the kernel [`AuthConfig`] variants this binding supports. * `OAuthFederation` / `External` are intentionally not exposed yet — the @@ -121,7 +121,17 @@ export const enum AuthMode { * OAuth 2.0 user-to-machine (browser flow) — optional `oauthClientId` * + `oauthRedirectPort`. */ - OAuthU2m = 'OAuthU2m' + OAuthU2m = 'OAuthU2m', + /** + * Azure Entra service-principal M2M — `azureClientId` + + * `azureClientSecret` (+ optional `azureTenantId` / + * `azureWorkspaceResourceId`). The kernel builds the Entra token + * endpoint + `.default` scope, auto-discovers the tenant from the + * workspace when `azureTenantId` is omitted, and — when + * `azureWorkspaceResourceId` is set — additionally sends the SP + * management token for an RBAC-only service principal. + */ + AzureSpM2m = 'AzureSpM2m' } /** * A single extra HTTP header as an explicit `{ name, value }` pair. @@ -165,6 +175,8 @@ export interface ProxyInput { * - `OAuthM2m` — `oauthClientId` + `oauthClientSecret` required. * - `OAuthU2m` — `oauthClientId` / `oauthRedirectPort` optional * (defaults to the `databricks-sql-connector` client on port 8030). + * - `AzureSpM2m` — `azureClientId` + `azureClientSecret` required + * (+ optional `azureTenantId` / `azureWorkspaceResourceId`). * * Catalog / schema / sessionConf are applied once at session creation * and remain in effect for every statement run on the resulting @@ -242,6 +254,31 @@ export interface ConnectionOptions { * token exchange. Omitted selects BYOT / account-wide WIF. */ identityFederationClientId?: string + /** + * Azure Entra app-registration client id. Required for + * [`AuthMode::AzureSpM2m`]; ignored otherwise. + */ + azureClientId?: string + /** + * Azure Entra app-registration client secret. Required for + * [`AuthMode::AzureSpM2m`]. + */ + azureClientSecret?: string + /** + * Azure Entra tenant id. Optional for [`AuthMode::AzureSpM2m`]; + * omitted ⇒ the kernel auto-discovers it from the workspace's + * `/aad/auth` redirect (matching the Thrift connector). + */ + azureTenantId?: string + /** + * Azure workspace ARM resource id. Optional for [`AuthMode::AzureSpM2m`]; + * when set the kernel also sends the SP management token + * (`X-Databricks-Azure-SP-Management-Token`) + the + * `X-Databricks-Azure-Workspace-Resource-Id` header, so a service + * principal with an Azure RBAC role but no workspace membership can + * authenticate. Omitted ⇒ the data token authenticates alone. + */ + azureWorkspaceResourceId?: string /** * Default catalog for statements executed on this session. * Routed through the kernel's `DefaultOpts` and onto the SEA diff --git a/napi/src/database.rs b/napi/src/database.rs index 15575a33..103d3344 100644 --- a/napi/src/database.rs +++ b/napi/src/database.rs @@ -62,7 +62,7 @@ use crate::util::guarded; /// Authentication mode selector crossing the napi boundary. The string /// literals are what napi-rs emits from this `#[napi(string_enum)]` — the /// NodeJS SEA adapter (`KernelAuth`) matches them verbatim (`'Pat'`, -/// `'OAuthM2m'`, `'OAuthU2m'`). +/// `'OAuthM2m'`, `'OAuthU2m'`, `'AzureSpM2m'`). /// /// Mirrors the kernel [`AuthConfig`] variants this binding supports. /// `OAuthFederation` / `External` are intentionally not exposed yet — the @@ -82,6 +82,14 @@ pub enum AuthMode { /// OAuth 2.0 user-to-machine (browser flow) — optional `oauthClientId` /// + `oauthRedirectPort`. OAuthU2m, + /// Azure Entra service-principal M2M — `azureClientId` + + /// `azureClientSecret` (+ optional `azureTenantId` / + /// `azureWorkspaceResourceId`). The kernel builds the Entra token + /// endpoint + `.default` scope, auto-discovers the tenant from the + /// workspace when `azureTenantId` is omitted, and — when + /// `azureWorkspaceResourceId` is set — additionally sends the SP + /// management token for an RBAC-only service principal. + AzureSpM2m, } /// A single extra HTTP header as an explicit `{ name, value }` pair. @@ -125,6 +133,8 @@ pub struct ProxyInput { /// - `OAuthM2m` — `oauthClientId` + `oauthClientSecret` required. /// - `OAuthU2m` — `oauthClientId` / `oauthRedirectPort` optional /// (defaults to the `databricks-sql-connector` client on port 8030). +/// - `AzureSpM2m` — `azureClientId` + `azureClientSecret` required +/// (+ optional `azureTenantId` / `azureWorkspaceResourceId`). /// /// Catalog / schema / sessionConf are applied once at session creation /// and remain in effect for every statement run on the resulting @@ -182,6 +192,23 @@ pub struct ConnectionOptions { /// SP-wide Workload Identity Federation client id used during mandatory /// token exchange. Omitted selects BYOT / account-wide WIF. pub identity_federation_client_id: Option, + /// Azure Entra app-registration client id. Required for + /// [`AuthMode::AzureSpM2m`]; ignored otherwise. + pub azure_client_id: Option, + /// Azure Entra app-registration client secret. Required for + /// [`AuthMode::AzureSpM2m`]. + pub azure_client_secret: Option, + /// Azure Entra tenant id. Optional for [`AuthMode::AzureSpM2m`]; + /// omitted ⇒ the kernel auto-discovers it from the workspace's + /// `/aad/auth` redirect (matching the Thrift connector). + pub azure_tenant_id: Option, + /// Azure workspace ARM resource id. Optional for [`AuthMode::AzureSpM2m`]; + /// when set the kernel also sends the SP management token + /// (`X-Databricks-Azure-SP-Management-Token`) + the + /// `X-Databricks-Azure-Workspace-Resource-Id` header, so a service + /// principal with an Azure RBAC role but no workspace membership can + /// authenticate. Omitted ⇒ the data token authenticates alone. + pub azure_workspace_resource_id: Option, /// Default catalog for statements executed on this session. /// Routed through the kernel's `DefaultOpts` and onto the SEA /// `CreateSession.catalog` wire field. @@ -424,6 +451,21 @@ fn build_auth_config(options: &ConnectionOptions) -> napi::Result { options.identity_federation_client_id.clone(), )) } + // Azure Entra service-principal M2M. The kernel owns Azure + // resolution: it builds the Entra token endpoint + `.default` scope + // and auto-discovers the tenant from the workspace when + // `azureTenantId` is omitted. The binding forwards the Entra + // credentials (+ optional tenant + optional workspace resource id for + // the management-token resource-id header). Mirrors the pyo3 + // `azure-sp-m2m` arm. + AuthMode::AzureSpM2m => AuthConfig::azure_service_principal( + options.azure_client_id.clone(), + options.azure_client_secret.clone(), + options.azure_tenant_id.clone(), + options.azure_workspace_resource_id.clone(), + options.identity_federation_client_id.clone(), + ) + .map_err(napi_err_from_kernel), } } @@ -645,6 +687,10 @@ mod tests { jwt_passphrase: None, jwt_algorithm: None, identity_federation_client_id: None, + azure_client_id: None, + azure_client_secret: None, + azure_tenant_id: None, + azure_workspace_resource_id: None, catalog: None, schema: None, session_conf: None, @@ -701,6 +747,73 @@ mod tests { )); } + #[test] + fn azure_sp_m2m_builds_azure_service_principal() { + // azure-sp-m2m forwards the Entra SP credentials (+ optional tenant + + // optional workspace resource id) to the kernel's Azure SP flow. + let mut o = base(); + o.auth_mode = Some(AuthMode::AzureSpM2m); + o.azure_client_id = Some("entra-app-id".to_owned()); + o.azure_client_secret = Some("entra-secret".to_owned()); + o.azure_tenant_id = Some("tenant-abc".to_owned()); + o.azure_workspace_resource_id = + Some("/subscriptions/s/resourceGroups/rg/workspace/w".to_owned()); + match build_auth_config(&o).unwrap() { + AuthConfig::AzureServicePrincipal { + azure_client_id, + azure_client_secret, + azure_tenant_id, + azure_workspace_resource_id, + .. + } => { + assert_eq!(azure_client_id, "entra-app-id"); + assert_eq!(azure_client_secret, "entra-secret"); + assert_eq!(azure_tenant_id.as_deref(), Some("tenant-abc")); + assert_eq!( + azure_workspace_resource_id.as_deref(), + Some("/subscriptions/s/resourceGroups/rg/workspace/w") + ); + } + other => panic!("expected AzureServicePrincipal, got {other:?}"), + } + } + + #[test] + fn azure_sp_m2m_tenant_and_resource_id_optional() { + // Omitting the tenant + resource id is valid: the kernel auto-discovers + // the tenant and sends the data token alone (no management token). + let mut o = base(); + o.auth_mode = Some(AuthMode::AzureSpM2m); + o.azure_client_id = Some("entra-app-id".to_owned()); + o.azure_client_secret = Some("entra-secret".to_owned()); + match build_auth_config(&o).unwrap() { + AuthConfig::AzureServicePrincipal { + azure_tenant_id, + azure_workspace_resource_id, + .. + } => { + assert!(azure_tenant_id.is_none()); + assert!(azure_workspace_resource_id.is_none()); + } + other => panic!("expected AzureServicePrincipal, got {other:?}"), + } + } + + #[test] + fn azure_sp_m2m_requires_client_id_and_secret() { + // The kernel ctor rejects a missing/empty client id or secret; the + // binding surfaces that as an InvalidArgument envelope. + let mut o = base(); + o.auth_mode = Some(AuthMode::AzureSpM2m); + o.azure_client_secret = Some("entra-secret".to_owned()); + assert_invalid_arg(build_auth_config(&o).unwrap_err(), "azure_client_id"); + + let mut o = base(); + o.auth_mode = Some(AuthMode::AzureSpM2m); + o.azure_client_id = Some("entra-app-id".to_owned()); + assert_invalid_arg(build_auth_config(&o).unwrap_err(), "azure_client_secret"); + } + #[test] fn pat_rejects_empty_token() { let mut o = base(); From 5153bacc9e146a6e278c38b9602af50ce875560c Mon Sep 17 00:00:00 2001 From: eric-wang-1990 Date: Fri, 21 Aug 2026 00:47:55 -0700 Subject: [PATCH 2/2] feat(c-abi): thread Azure service-principal M2M (set_auth_azure_sp) for Go/ODBC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add kernel_session_config_set_auth_azure_sp to the C ABI, so Go/ODBC consumers can use Azure Entra service-principal M2M — the C-ABI analog of the pyo3/napi azure-sp-m2m surface. Required azure_client_id + azure_client_secret; optional azure_tenant_id (NULL -> kernel auto-discovers from the workspace /aad/auth redirect) and azure_workspace_resource_id (set -> kernel also sends the Azure SP management token + X-Databricks-Azure-Workspace-Resource-Id header, for an SP with an Azure RBAC role but no workspace membership). New AuthSpec::AzureServicePrincipal variant + conversion to AuthConfig::azure_service_principal (carrying the shared federation client id). Hand-written header declaration added. Azure AD U2M needs no new setter — it rides set_auth_u2m (the kernel's workspace-federated browser flow, which Azure Databricks workspaces support). Co-authored-by: Isaac Signed-off-by: eric-wang-1990 --- NEXT_CHANGELOG.md | 11 +++ include/databricks_kernel.h | 17 ++++ src/c_abi/session.rs | 157 ++++++++++++++++++++++++++++++++++-- 3 files changed, 179 insertions(+), 6 deletions(-) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index f29a642c..b310b6dc 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -60,6 +60,17 @@ changes go in their respective binding's NEXT_CHANGELOG. `jwt_key_file` / `jwt_kid`; optional `jwt_passphrase` (encrypted PKCS#8), `jwt_algorithm` (NULL → `RS256`), `scopes` (comma-separated; NULL → `all-apis`), and `token_url` (NULL → OIDC discovery). Maps to `AuthConfig::oauth_m2m_private_key`. +- C ABI: `kernel_session_config_set_auth_azure_sp()` — expose Azure Entra + service-principal M2M over the C ABI (for ODBC / Go consumers). Required + `azure_client_id` / `azure_client_secret`; optional `azure_tenant_id` + (NULL → the kernel auto-discovers it from the workspace `/aad/auth` redirect) and + `azure_workspace_resource_id` (NULL → the data token authenticates alone; set → + the kernel also sends the Azure SP management token + + `X-Databricks-Azure-Workspace-Resource-Id` header, so an SP with an Azure RBAC + role but no workspace membership can authenticate). Maps to + `AuthConfig::azure_service_principal`. Azure AD U2M has no separate setter — it + rides `set_auth_u2m` (the kernel's workspace-federated browser flow, which Azure + Databricks workspaces support). - C ABI: `kernel_session_test()` — actively validate a session by round-tripping a lightweight `SELECT 1` test query (result discarded). The I/O-performing complement of `kernel_session_is_open()` (a local handle-state check): it reaches the server, so diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index 7161f153..26a8352b 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -264,6 +264,23 @@ KernelStatusCode kernel_session_config_set_auth_u2m(KernelSessionConfig* config, const char* client_id, uint16_t redirect_port, const char* scopes); +/* Azure Entra service-principal M2M. The kernel owns Azure resolution: it + * builds the Entra token endpoint + `{app}/.default` scope and auto-discovers + * the tenant from the workspace `/aad/auth` redirect when `azure_tenant_id` is + * NULL. `azure_client_id` and `azure_client_secret` (Entra app-registration + * credentials) are required. Optional (pass NULL to omit): `azure_tenant_id`, + * and `azure_workspace_resource_id` (workspace ARM resource id; when set, the + * kernel also sends the Azure SP management token + + * `X-Databricks-Azure-Workspace-Resource-Id` header, so a service principal + * with an Azure RBAC role but no workspace membership can authenticate; NULL → + * the data token authenticates alone). Azure AD U2M has no separate setter — + * use kernel_session_config_set_auth_u2m (the kernel's workspace-federated + * browser flow works against Azure Databricks workspaces). */ +KernelStatusCode kernel_session_config_set_auth_azure_sp(KernelSessionConfig* config, + const char* azure_client_id, + const char* azure_client_secret, + const char* azure_tenant_id, + const char* azure_workspace_resource_id); /* Optional SP-wide Workload Identity Federation client id used by mandatory * token exchange. Unset selects BYOT / account-wide WIF. */ diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index bd0592a9..2e145f61 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -14,10 +14,11 @@ //! Session FFI — config marshalling, open / close / new_statement. //! -//! Auth over the C ABI: **PAT, OAuth M2M, and OAuth U2M** (interactive -//! browser flow) are all exposed (`set_auth_pat` / `set_auth_m2m` / -//! `set_auth_u2m`). A host may also drive its own token flow and inject -//! it via PAT. +//! Auth over the C ABI: **PAT, OAuth M2M (shared-secret and JWT +//! private-key), OAuth U2M** (interactive browser flow), and **Azure Entra +//! service-principal M2M** are all exposed (`set_auth_pat` / `set_auth_m2m` / +//! `set_auth_m2m_jwt` / `set_auth_u2m` / `set_auth_azure_sp`). A host may also +//! drive its own token flow and inject it via PAT. //! //! Transaction control (autocommit / commit / rollback) is exposed via //! `kernel_session_set_autocommit` / `_get_autocommit` / `_commit` / @@ -50,8 +51,8 @@ enum ConnectionSpec { } /// Auth choice captured from the C-side setters. The C ABI wires PAT, -/// OAuth M2M (shared-secret and JWT private-key), and OAuth U2M; other -/// [`AuthConfig`] variants stay Rust-crate-only. +/// OAuth M2M (shared-secret and JWT private-key), OAuth U2M, and Azure Entra +/// service-principal M2M; other [`AuthConfig`] variants stay Rust-crate-only. /// /// NOTE (secret handling): `token` / `client_secret` / `jwt_passphrase` /// are copied out of the C string into owned `String`s and are not @@ -90,6 +91,18 @@ enum AuthSpec { /// default (`sql`, `offline_access`). scopes: Option, }, + /// Azure Entra service-principal M2M. `azure_client_id` + + /// `azure_client_secret` are required; `azure_tenant_id` (`None` ⇒ + /// kernel auto-discovers from the workspace `/aad/auth` redirect) and + /// `azure_workspace_resource_id` (`None` ⇒ data token authenticates + /// alone; `Some` ⇒ the kernel also sends the SP management token + + /// resource-id header) are optional. + AzureServicePrincipal { + azure_client_id: String, + azure_client_secret: String, + azure_tenant_id: Option, + azure_workspace_resource_id: Option, + }, } /// C-side session config builder state. @@ -427,6 +440,50 @@ pub unsafe extern "C" fn kernel_session_config_set_auth_u2m( }) } +/// Set Azure Entra service-principal M2M authentication. The kernel owns +/// Azure resolution: it builds the Entra token endpoint + `{app}/.default` +/// scope and auto-discovers the tenant from the workspace's `/aad/auth` +/// redirect when `azure_tenant_id` is omitted. Mutually exclusive with the +/// other `set_auth_*` setters — the last one called wins. +/// +/// `azure_client_id` and `azure_client_secret` (the Entra app-registration +/// credentials) are required. The rest are optional (pass NULL to omit): +/// `azure_tenant_id` (NULL → auto-discover), and `azure_workspace_resource_id` +/// (the workspace's ARM resource id; when set, the kernel additionally sends +/// the Azure SP management token + `X-Databricks-Azure-Workspace-Resource-Id` +/// header, so a service principal with an Azure RBAC role but no workspace +/// membership can authenticate; NULL → the data token authenticates alone). +/// +/// Azure AD U2M is not a separate setter — use +/// [`kernel_session_config_set_auth_u2m`], which runs the kernel's +/// workspace-federated browser flow (Azure Databricks workspaces support it). +/// +/// # Safety +/// `config` must be a live config handle; `azure_client_id` / +/// `azure_client_secret` must be NUL-terminated C strings, and the optional +/// args, if non-null, must be NUL-terminated C strings. +#[no_mangle] +pub unsafe extern "C" fn kernel_session_config_set_auth_azure_sp( + config: *mut KernelSessionConfig, + azure_client_id: *const c_char, + azure_client_secret: *const c_char, + azure_tenant_id: *const c_char, + azure_workspace_resource_id: *const c_char, +) -> KernelStatusCode { + set_on_config(config, |c| { + c.auth = Some(AuthSpec::AzureServicePrincipal { + azure_client_id: required_str(azure_client_id, "azure_client_id")?, + azure_client_secret: required_str(azure_client_secret, "azure_client_secret")?, + azure_tenant_id: optional_str(azure_tenant_id, "azure_tenant_id")?, + azure_workspace_resource_id: optional_str( + azure_workspace_resource_id, + "azure_workspace_resource_id", + )?, + }); + Ok(()) + }) +} + /// Set the optional SP-wide Workload Identity Federation client id used by /// mandatory token exchange. Leaving it unset selects BYOT / account-wide WIF. /// @@ -1102,6 +1159,18 @@ fn auth_spec_to_config( Some(token_cache), identity_federation_client_id, ), + AuthSpec::AzureServicePrincipal { + azure_client_id, + azure_client_secret, + azure_tenant_id, + azure_workspace_resource_id, + } => AuthConfig::azure_service_principal( + Some(azure_client_id), + Some(azure_client_secret), + azure_tenant_id, + azure_workspace_resource_id, + identity_federation_client_id, + )?, }; Ok(auth) } @@ -1490,6 +1559,82 @@ mod tests { } } + #[test] + fn azure_sp_builds_azure_service_principal() { + // azure-sp forwards the Entra SP credentials (+ optional tenant + + // optional workspace resource id) and the shared federation id to the + // kernel's Azure SP flow. + let auth = auth_spec_to_config( + AuthSpec::AzureServicePrincipal { + azure_client_id: "entra-app-id".to_string(), + azure_client_secret: "entra-secret".to_string(), + azure_tenant_id: Some("tenant-abc".to_string()), + azure_workspace_resource_id: Some( + "/subscriptions/s/resourceGroups/rg/workspace/w".to_string(), + ), + }, + None, + None, + Some("fed-client-id".to_string()), + TokenCacheConfig::default(), + ) + .expect("azure sp config builds"); + + match auth { + AuthConfig::AzureServicePrincipal { + azure_client_id, + azure_client_secret, + azure_tenant_id, + azure_workspace_resource_id, + identity_federation_client_id, + } => { + assert_eq!(azure_client_id, "entra-app-id"); + assert_eq!(azure_client_secret, "entra-secret"); + assert_eq!(azure_tenant_id.as_deref(), Some("tenant-abc")); + assert_eq!( + azure_workspace_resource_id.as_deref(), + Some("/subscriptions/s/resourceGroups/rg/workspace/w") + ); + assert_eq!( + identity_federation_client_id.as_deref(), + Some("fed-client-id") + ); + } + other => panic!("expected AzureServicePrincipal, got {other:?}"), + } + } + + #[test] + fn azure_sp_tenant_and_resource_id_optional() { + // Omitting tenant + resource id is valid — the kernel auto-discovers + // the tenant and sends the data token alone. + let auth = auth_spec_to_config( + AuthSpec::AzureServicePrincipal { + azure_client_id: "entra-app-id".to_string(), + azure_client_secret: "entra-secret".to_string(), + azure_tenant_id: None, + azure_workspace_resource_id: None, + }, + None, + None, + None, + TokenCacheConfig::default(), + ) + .expect("azure sp config builds"); + + match auth { + AuthConfig::AzureServicePrincipal { + azure_tenant_id, + azure_workspace_resource_id, + .. + } => { + assert!(azure_tenant_id.is_none()); + assert!(azure_workspace_resource_id.is_none()); + } + other => panic!("expected AzureServicePrincipal, got {other:?}"), + } + } + #[test] fn u2m_carries_token_endpoint_override() { let token_endpoint = "https://login.microsoftonline.com/tenant/oauth2/v2.0/token";