Skip to content

Commit 53fc45e

Browse files
feat(bindings): thread Azure service-principal M2M through the napi + C ABI bindings (#280)
## What Threads **Azure Entra service-principal M2M** through the **napi (Node.js)** and **C ABI (Go / ODBC)** bindings — the analogs of the pyo3 `auth_type="azure-sp-m2m"` surface that #263 added for Python. #263 wired azure-sp-m2m through pyo3 + the core `AuthConfig`, but left the other two bindings untouched. > **Stacked on #263** — targets `eric-wang/azure-service-principal-design` because it calls `AuthConfig::azure_service_principal`, which lives in #263, not `main`. Rebase onto `main` once #263 merges. ## How **napi** (`napi/src/database.rs`, `napi/index.d.ts`): - New `AuthMode::AzureSpM2m` + `ConnectionOptions` fields `azureClientId` / `azureClientSecret` (required), `azureTenantId?`, `azureWorkspaceResourceId?`. - `build_auth_config` arm → `AuthConfig::azure_service_principal(...)`. `index.d.ts` regenerated to match. **C ABI** (`src/c_abi/session.rs`, `include/databricks_kernel.h`): - New `kernel_session_config_set_auth_azure_sp(config, azure_client_id, azure_client_secret, azure_tenant_id, azure_workspace_resource_id)` alongside `set_auth_pat` / `_m2m` / `_m2m_jwt` / `_u2m`. - New `AuthSpec::AzureServicePrincipal` variant + conversion to `AuthConfig::azure_service_principal` (carrying the shared federation client id). Hand-written header declaration added. Common semantics across both: `azureClientId` + `azureClientSecret` required; `azureTenantId` optional (kernel auto-discovers from the workspace `/aad/auth` redirect when omitted); `azureWorkspaceResourceId` optional (set ⇒ the 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; omitted ⇒ the data token authenticates alone). The bindings stay thin — the kernel owns all Azure resolution (Entra token endpoint, `{app}/.default` scope, tenant discovery, and the management token's v1.0 `resource=` grant). **Azure AD U2M needs no new mode/setter** on either binding — it rides the existing napi `AuthMode::OAuthU2m` / C-ABI `set_auth_u2m`, i.e. the kernel's in-house workspace-federated browser flow, which Azure Databricks workspaces support. The connectors map their Azure-U2M selectors onto that (separate connector-repo PRs). ## Tests - **napi** (`cargo test --features dyn-symbols`): `azure_sp_m2m_builds_azure_service_principal`, `azure_sp_m2m_tenant_and_resource_id_optional`, `azure_sp_m2m_requires_client_id_and_secret`. - **C ABI** (`cargo test`): `azure_sp_builds_azure_service_principal` (incl. federation id passthrough), `azure_sp_tenant_and_resource_id_optional`. - All green; `clippy -D warnings` clean; napi loader drift check passes; TDD throughout (stub → RED → real arm → GREEN on both bindings). ## Related - Kernel core + pyo3: #263. - Connector bridges (Node `KernelAuth.ts` + Python already merged/in-flight): separate connector-repo PRs. - JIRA: PECOBLR-4141 (azure-sp-m2m), PECOBLR-4120 (Azure U2M). This pull request and its description were written by Isaac. --------- Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
1 parent ad78a5b commit 53fc45e

6 files changed

Lines changed: 344 additions & 9 deletions

File tree

‎NEXT_CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ changes go in their respective binding's NEXT_CHANGELOG.
6060
`jwt_key_file` / `jwt_kid`; optional `jwt_passphrase` (encrypted PKCS#8),
6161
`jwt_algorithm` (NULL → `RS256`), `scopes` (comma-separated; NULL → `all-apis`),
6262
and `token_url` (NULL → OIDC discovery). Maps to `AuthConfig::oauth_m2m_private_key`.
63+
- C ABI: `kernel_session_config_set_auth_azure_sp()` — expose Azure Entra
64+
service-principal M2M over the C ABI (for ODBC / Go consumers). Required
65+
`azure_client_id` / `azure_client_secret`; optional `azure_tenant_id`
66+
(NULL → the kernel auto-discovers it from the workspace `/aad/auth` redirect) and
67+
`azure_workspace_resource_id` (NULL → the data token authenticates alone; set →
68+
the kernel also sends the Azure SP management token +
69+
`X-Databricks-Azure-Workspace-Resource-Id` header, so an SP with an Azure RBAC
70+
role but no workspace membership can authenticate). Maps to
71+
`AuthConfig::azure_service_principal`. Azure AD U2M has no separate setter — it
72+
rides `set_auth_u2m` (the kernel's workspace-federated browser flow, which Azure
73+
Databricks workspaces support).
6374
- C ABI: `kernel_session_test()` — actively validate a session by round-tripping a
6475
lightweight `SELECT 1` test query (result discarded). The I/O-performing complement
6576
of `kernel_session_is_open()` (a local handle-state check): it reaches the server, so

‎include/databricks_kernel.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ KernelStatusCode kernel_session_config_set_auth_u2m(KernelSessionConfig* config,
264264
const char* client_id,
265265
uint16_t redirect_port,
266266
const char* scopes);
267+
/* Azure Entra service-principal M2M. The kernel owns Azure resolution: it
268+
* builds the Entra token endpoint + `{app}/.default` scope and auto-discovers
269+
* the tenant from the workspace `/aad/auth` redirect when `azure_tenant_id` is
270+
* NULL. `azure_client_id` and `azure_client_secret` (Entra app-registration
271+
* credentials) are required. Optional (pass NULL to omit): `azure_tenant_id`,
272+
* and `azure_workspace_resource_id` (workspace ARM resource id; when set, the
273+
* kernel also sends the Azure SP management token +
274+
* `X-Databricks-Azure-Workspace-Resource-Id` header, so a service principal
275+
* with an Azure RBAC role but no workspace membership can authenticate; NULL →
276+
* the data token authenticates alone). Azure AD U2M has no separate setter —
277+
* use kernel_session_config_set_auth_u2m (the kernel's workspace-federated
278+
* browser flow works against Azure Databricks workspaces). */
279+
KernelStatusCode kernel_session_config_set_auth_azure_sp(KernelSessionConfig* config,
280+
const char* azure_client_id,
281+
const char* azure_client_secret,
282+
const char* azure_tenant_id,
283+
const char* azure_workspace_resource_id);
267284

268285
/* Optional SP-wide Workload Identity Federation client id used by mandatory
269286
* token exchange. Unset selects BYOT / account-wide WIF. */

‎napi/NEXT_CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ layout, or npm publishing. Pure-kernel changes go in the root
3030
`token_url_override`.
3131
- `ConnectionOptions.identityFederationClientId` selects SP-wide Workload
3232
Identity Federation during mandatory token exchange.
33+
- `AuthMode.AzureSpM2m` — Azure Entra service-principal M2M. New
34+
`ConnectionOptions` fields `azureClientId` (required) + `azureClientSecret`
35+
(required), plus optional `azureTenantId` (kernel auto-discovers it from the
36+
workspace's `/aad/auth` redirect when omitted) and `azureWorkspaceResourceId`
37+
(when set, the kernel also sends the Azure SP management token +
38+
`X-Databricks-Azure-Workspace-Resource-Id` header, so a service principal with
39+
an Azure RBAC role but no workspace membership can authenticate; omitted ⇒ the
40+
data token authenticates alone). Forwards to the kernel's
41+
`AuthConfig::azure_service_principal`. The napi analog of the pyo3
42+
`auth_type="azure-sp-m2m"` surface. Azure AD **U2M** needs no new mode — it
43+
rides `AuthMode.OAuthU2m` (the kernel's workspace-federated browser flow,
44+
which Azure workspaces support).
3345

3446
### Updated
3547

‎napi/index.d.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export interface NamedTypedValueInput {
100100
* Authentication mode selector crossing the napi boundary. The string
101101
* literals are what napi-rs emits from this `#[napi(string_enum)]` — the
102102
* NodeJS SEA adapter (`KernelAuth`) matches them verbatim (`'Pat'`,
103-
* `'OAuthM2m'`, `'OAuthU2m'`).
103+
* `'OAuthM2m'`, `'OAuthU2m'`, `'AzureSpM2m'`).
104104
*
105105
* Mirrors the kernel [`AuthConfig`] variants this binding supports.
106106
* `OAuthFederation` / `External` are intentionally not exposed yet — the
@@ -121,7 +121,17 @@ export const enum AuthMode {
121121
* OAuth 2.0 user-to-machine (browser flow) — optional `oauthClientId`
122122
* + `oauthRedirectPort`.
123123
*/
124-
OAuthU2m = 'OAuthU2m'
124+
OAuthU2m = 'OAuthU2m',
125+
/**
126+
* Azure Entra service-principal M2M — `azureClientId` +
127+
* `azureClientSecret` (+ optional `azureTenantId` /
128+
* `azureWorkspaceResourceId`). The kernel builds the Entra token
129+
* endpoint + `.default` scope, auto-discovers the tenant from the
130+
* workspace when `azureTenantId` is omitted, and — when
131+
* `azureWorkspaceResourceId` is set — additionally sends the SP
132+
* management token for an RBAC-only service principal.
133+
*/
134+
AzureSpM2m = 'AzureSpM2m'
125135
}
126136
/**
127137
* A single extra HTTP header as an explicit `{ name, value }` pair.
@@ -165,6 +175,8 @@ export interface ProxyInput {
165175
* - `OAuthM2m` — `oauthClientId` + `oauthClientSecret` required.
166176
* - `OAuthU2m` — `oauthClientId` / `oauthRedirectPort` optional
167177
* (defaults to the `databricks-sql-connector` client on port 8030).
178+
* - `AzureSpM2m` — `azureClientId` + `azureClientSecret` required
179+
* (+ optional `azureTenantId` / `azureWorkspaceResourceId`).
168180
*
169181
* Catalog / schema / sessionConf are applied once at session creation
170182
* and remain in effect for every statement run on the resulting
@@ -242,6 +254,31 @@ export interface ConnectionOptions {
242254
* token exchange. Omitted selects BYOT / account-wide WIF.
243255
*/
244256
identityFederationClientId?: string
257+
/**
258+
* Azure Entra app-registration client id. Required for
259+
* [`AuthMode::AzureSpM2m`]; ignored otherwise.
260+
*/
261+
azureClientId?: string
262+
/**
263+
* Azure Entra app-registration client secret. Required for
264+
* [`AuthMode::AzureSpM2m`].
265+
*/
266+
azureClientSecret?: string
267+
/**
268+
* Azure Entra tenant id. Optional for [`AuthMode::AzureSpM2m`];
269+
* omitted ⇒ the kernel auto-discovers it from the workspace's
270+
* `/aad/auth` redirect (matching the Thrift connector).
271+
*/
272+
azureTenantId?: string
273+
/**
274+
* Azure workspace ARM resource id. Optional for [`AuthMode::AzureSpM2m`];
275+
* when set the kernel also sends the SP management token
276+
* (`X-Databricks-Azure-SP-Management-Token`) + the
277+
* `X-Databricks-Azure-Workspace-Resource-Id` header, so a service
278+
* principal with an Azure RBAC role but no workspace membership can
279+
* authenticate. Omitted ⇒ the data token authenticates alone.
280+
*/
281+
azureWorkspaceResourceId?: string
245282
/**
246283
* Default catalog for statements executed on this session.
247284
* Routed through the kernel's `DefaultOpts` and onto the SEA

‎napi/src/database.rs‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::util::guarded;
6262
/// Authentication mode selector crossing the napi boundary. The string
6363
/// literals are what napi-rs emits from this `#[napi(string_enum)]` — the
6464
/// NodeJS SEA adapter (`KernelAuth`) matches them verbatim (`'Pat'`,
65-
/// `'OAuthM2m'`, `'OAuthU2m'`).
65+
/// `'OAuthM2m'`, `'OAuthU2m'`, `'AzureSpM2m'`).
6666
///
6767
/// Mirrors the kernel [`AuthConfig`] variants this binding supports.
6868
/// `OAuthFederation` / `External` are intentionally not exposed yet — the
@@ -82,6 +82,14 @@ pub enum AuthMode {
8282
/// OAuth 2.0 user-to-machine (browser flow) — optional `oauthClientId`
8383
/// + `oauthRedirectPort`.
8484
OAuthU2m,
85+
/// Azure Entra service-principal M2M — `azureClientId` +
86+
/// `azureClientSecret` (+ optional `azureTenantId` /
87+
/// `azureWorkspaceResourceId`). The kernel builds the Entra token
88+
/// endpoint + `.default` scope, auto-discovers the tenant from the
89+
/// workspace when `azureTenantId` is omitted, and — when
90+
/// `azureWorkspaceResourceId` is set — additionally sends the SP
91+
/// management token for an RBAC-only service principal.
92+
AzureSpM2m,
8593
}
8694

8795
/// A single extra HTTP header as an explicit `{ name, value }` pair.
@@ -125,6 +133,8 @@ pub struct ProxyInput {
125133
/// - `OAuthM2m` — `oauthClientId` + `oauthClientSecret` required.
126134
/// - `OAuthU2m` — `oauthClientId` / `oauthRedirectPort` optional
127135
/// (defaults to the `databricks-sql-connector` client on port 8030).
136+
/// - `AzureSpM2m` — `azureClientId` + `azureClientSecret` required
137+
/// (+ optional `azureTenantId` / `azureWorkspaceResourceId`).
128138
///
129139
/// Catalog / schema / sessionConf are applied once at session creation
130140
/// and remain in effect for every statement run on the resulting
@@ -182,6 +192,23 @@ pub struct ConnectionOptions {
182192
/// SP-wide Workload Identity Federation client id used during mandatory
183193
/// token exchange. Omitted selects BYOT / account-wide WIF.
184194
pub identity_federation_client_id: Option<String>,
195+
/// Azure Entra app-registration client id. Required for
196+
/// [`AuthMode::AzureSpM2m`]; ignored otherwise.
197+
pub azure_client_id: Option<String>,
198+
/// Azure Entra app-registration client secret. Required for
199+
/// [`AuthMode::AzureSpM2m`].
200+
pub azure_client_secret: Option<String>,
201+
/// Azure Entra tenant id. Optional for [`AuthMode::AzureSpM2m`];
202+
/// omitted ⇒ the kernel auto-discovers it from the workspace's
203+
/// `/aad/auth` redirect (matching the Thrift connector).
204+
pub azure_tenant_id: Option<String>,
205+
/// Azure workspace ARM resource id. Optional for [`AuthMode::AzureSpM2m`];
206+
/// when set the kernel also sends the SP management token
207+
/// (`X-Databricks-Azure-SP-Management-Token`) + the
208+
/// `X-Databricks-Azure-Workspace-Resource-Id` header, so a service
209+
/// principal with an Azure RBAC role but no workspace membership can
210+
/// authenticate. Omitted ⇒ the data token authenticates alone.
211+
pub azure_workspace_resource_id: Option<String>,
185212
/// Default catalog for statements executed on this session.
186213
/// Routed through the kernel's `DefaultOpts` and onto the SEA
187214
/// `CreateSession.catalog` wire field.
@@ -424,6 +451,21 @@ fn build_auth_config(options: &ConnectionOptions) -> napi::Result<AuthConfig> {
424451
options.identity_federation_client_id.clone(),
425452
))
426453
}
454+
// Azure Entra service-principal M2M. The kernel owns Azure
455+
// resolution: it builds the Entra token endpoint + `.default` scope
456+
// and auto-discovers the tenant from the workspace when
457+
// `azureTenantId` is omitted. The binding forwards the Entra
458+
// credentials (+ optional tenant + optional workspace resource id for
459+
// the management-token resource-id header). Mirrors the pyo3
460+
// `azure-sp-m2m` arm.
461+
AuthMode::AzureSpM2m => AuthConfig::azure_service_principal(
462+
options.azure_client_id.clone(),
463+
options.azure_client_secret.clone(),
464+
options.azure_tenant_id.clone(),
465+
options.azure_workspace_resource_id.clone(),
466+
options.identity_federation_client_id.clone(),
467+
)
468+
.map_err(napi_err_from_kernel),
427469
}
428470
}
429471

@@ -645,6 +687,10 @@ mod tests {
645687
jwt_passphrase: None,
646688
jwt_algorithm: None,
647689
identity_federation_client_id: None,
690+
azure_client_id: None,
691+
azure_client_secret: None,
692+
azure_tenant_id: None,
693+
azure_workspace_resource_id: None,
648694
catalog: None,
649695
schema: None,
650696
session_conf: None,
@@ -701,6 +747,73 @@ mod tests {
701747
));
702748
}
703749

750+
#[test]
751+
fn azure_sp_m2m_builds_azure_service_principal() {
752+
// azure-sp-m2m forwards the Entra SP credentials (+ optional tenant +
753+
// optional workspace resource id) to the kernel's Azure SP flow.
754+
let mut o = base();
755+
o.auth_mode = Some(AuthMode::AzureSpM2m);
756+
o.azure_client_id = Some("entra-app-id".to_owned());
757+
o.azure_client_secret = Some("entra-secret".to_owned());
758+
o.azure_tenant_id = Some("tenant-abc".to_owned());
759+
o.azure_workspace_resource_id =
760+
Some("/subscriptions/s/resourceGroups/rg/workspace/w".to_owned());
761+
match build_auth_config(&o).unwrap() {
762+
AuthConfig::AzureServicePrincipal {
763+
azure_client_id,
764+
azure_client_secret,
765+
azure_tenant_id,
766+
azure_workspace_resource_id,
767+
..
768+
} => {
769+
assert_eq!(azure_client_id, "entra-app-id");
770+
assert_eq!(azure_client_secret, "entra-secret");
771+
assert_eq!(azure_tenant_id.as_deref(), Some("tenant-abc"));
772+
assert_eq!(
773+
azure_workspace_resource_id.as_deref(),
774+
Some("/subscriptions/s/resourceGroups/rg/workspace/w")
775+
);
776+
}
777+
other => panic!("expected AzureServicePrincipal, got {other:?}"),
778+
}
779+
}
780+
781+
#[test]
782+
fn azure_sp_m2m_tenant_and_resource_id_optional() {
783+
// Omitting the tenant + resource id is valid: the kernel auto-discovers
784+
// the tenant and sends the data token alone (no management token).
785+
let mut o = base();
786+
o.auth_mode = Some(AuthMode::AzureSpM2m);
787+
o.azure_client_id = Some("entra-app-id".to_owned());
788+
o.azure_client_secret = Some("entra-secret".to_owned());
789+
match build_auth_config(&o).unwrap() {
790+
AuthConfig::AzureServicePrincipal {
791+
azure_tenant_id,
792+
azure_workspace_resource_id,
793+
..
794+
} => {
795+
assert!(azure_tenant_id.is_none());
796+
assert!(azure_workspace_resource_id.is_none());
797+
}
798+
other => panic!("expected AzureServicePrincipal, got {other:?}"),
799+
}
800+
}
801+
802+
#[test]
803+
fn azure_sp_m2m_requires_client_id_and_secret() {
804+
// The kernel ctor rejects a missing/empty client id or secret; the
805+
// binding surfaces that as an InvalidArgument envelope.
806+
let mut o = base();
807+
o.auth_mode = Some(AuthMode::AzureSpM2m);
808+
o.azure_client_secret = Some("entra-secret".to_owned());
809+
assert_invalid_arg(build_auth_config(&o).unwrap_err(), "azure_client_id");
810+
811+
let mut o = base();
812+
o.auth_mode = Some(AuthMode::AzureSpM2m);
813+
o.azure_client_id = Some("entra-app-id".to_owned());
814+
assert_invalid_arg(build_auth_config(&o).unwrap_err(), "azure_client_secret");
815+
}
816+
704817
#[test]
705818
fn pat_rejects_empty_token() {
706819
let mut o = base();

0 commit comments

Comments
 (0)