Skip to content

feat(napi): expose kernel OAuth (M2M + U2M) on openSession ConnectionOptions - #95

Closed
msrathore-db wants to merge 1 commit into
msrathore/krn-statement-row-limit-timeoutfrom
msrathore/krn-napi-oauth
Closed

msrathore-db wants to merge 1 commit into
msrathore/krn-statement-row-limit-timeoutfrom
msrathore/krn-napi-oauth

Conversation

@msrathore-db

Copy link
Copy Markdown
Collaborator

Summary

The kernel fully implements OAuth M2M (client-credentials) and U2M (auth-code + PKCE) in AuthConfig::OAuthM2M / OAuthU2M, but the napi binding only wired AuthConfig::Pat. So NodeJS SEA — and pyo3, which has the identical gap — could not offer OAuth despite the kernel supporting it. This surfaces it on the binding.

Changes

  • #[napi(string_enum)] AuthMode { Pat, OAuthM2m, OAuthU2m } — string values ('Pat'/'OAuthM2m'/'OAuthU2m') match what the NodeJS SEA adapter (SeaAuth) already expects verbatim, so the Node side wires straight onto it.
  • ConnectionOptions gains authMode (default Pat — back-compat for existing PAT callers), oauthClientId, oauthClientSecret, oauthRedirectPort, oauthScopes; token becomes optional (required only for Pat).
  • build_auth_config maps the JS options onto the kernel AuthConfig, enforcing per-mode required fields at the binding boundary (fail-fast InvalidArgument before any network). Pure + unit-tested.
  • OAuthFederation / External intentionally not exposed (kernel marks federation unimplemented; External is a Rust-trait escape hatch with no JS-callback bridge).

Layering / DRY

Auth logic stays in the kernel (AuthConfig + the OAuth flows); the binding only validates the JS option shape and selects the variant. The NodeJS SEA adapter maps its public authType: 'databricks-oauth' → authMode + the OAuth fields.

Validation

  • cargo fmt + cargo clippy --lib clean; 4 new unit tests (Pat default + empty-token reject, M2M required-field validation, U2M all-optional + override threading); 41 napi unit tests green.
  • Live: an OAuth M2M client-credentials grant against the pecotesting workspace authenticated successfully — the request reached the SEA endpoint with a minted bearer token and the server then applied warehouse-level authorization (403 PERMISSION_DENIED for an SPN not granted on that warehouse; a 401 would indicate a failed token exchange). PAT continues to work both with an explicit authMode: Pat and with authMode omitted.

This pull request and its description were written by Isaac.

@gopalldb

Copy link
Copy Markdown
Collaborator

Mismatched fields are silently ignored (build_auth_config, diff:101-126)

  • authMode: 'Pat' + oauthClientId: '...' → OAuth fields silently dropped. Friendlier to strict-reject extra fields, or at least document "ignored otherwise" for the OAuth fields like the token doc already
    does.

Empty oauth_scopes not normalized to None (diff:113, 121)

  • oauth_scopes: Some(vec![]) (JS empty array) passes through verbatim; kernel's unwrap_or_else(|| vec!["all-apis", ...]) (src/session.rs:681-683) only fires for None. Caller passing [] thinking it means
    default ends up with no scopes → cryptic server-side error.
  • Fix: options.oauth_scopes.clone().filter(|v| !v.is_empty()).

Empty-string oauthClientId on U2M bypasses kernel default (diff:119)

  • Some("") propagates verbatim; kernel's as_deref().unwrap_or("databricks-cli") (src/session.rs:702) returns "" rather than the default — fails server-side with a less helpful error than the binding could
    give.
  • Fix: empty-filter the U2M optional strings: options.oauth_client_id.clone().filter(|s| !s.is_empty()).

Test coverage gaps on boundary conditions (diff:162-260)

  • Four new tests cover happy paths + missing-field rejection well. Missing:
    • M2m with Some("") for client_id / client_secret (relies on require_field filter; not exercised).
    • PAT + extraneous OAuth fields (silent-ignore behavior is untested).
    • U2M with Some("") for oauth_client_id (per above P2).
    • U2M with oauth_redirect_port: Some(0) (kernel accepts port 0 for OS-assigned — worth pinning).

…Options

The kernel implements OAuth M2M (client-credentials) and U2M (auth-code +
PKCE) in `AuthConfig::OAuthM2M` / `OAuthU2M`, but the napi binding only
wired `AuthConfig::Pat` — so NodeJS SEA (and pyo3, which has the same gap)
could not offer OAuth even though the kernel fully supports it. This
surfaces it on the binding.

- New `#[napi(string_enum)] AuthMode { Pat, OAuthM2m, OAuthU2m }` whose
  string values (`'Pat'` / `'OAuthM2m'` / `'OAuthU2m'`) match what the
  NodeJS SEA adapter (`SeaAuth`) already expects verbatim.
- `ConnectionOptions` gains `authMode` (default Pat — back-compat for
  existing PAT callers that pass only `token`), `oauthClientId`,
  `oauthClientSecret`, `oauthRedirectPort`, `oauthScopes`. `token` is now
  optional (required only for Pat).
- `build_auth_config` maps the JS options onto the kernel `AuthConfig`,
  enforcing per-mode required fields at the binding boundary (fail-fast
  `InvalidArgument` before any network) so the kernel always receives a
  fully-formed variant. Pure + unit-tested. OAuth `Federation` / `External`
  are intentionally not exposed (kernel marks federation unimplemented;
  External is a Rust-trait escape hatch with no JS-callback bridge).

Validation: cargo fmt + clippy clean; 4 new unit tests (Pat default +
empty-token reject, M2M required-field validation, U2M all-optional with
override threading); 41 napi unit tests green. Live: OAuth M2M
client-credentials grant against pecotesting authenticated successfully
(server then applied warehouse-level authorization); PAT continues to work
both with an explicit `authMode: Pat` and with `authMode` omitted.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
@msrathore-db

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #101, which is now the main-based kernel SEA parity mega PR for agent review. The branch is preserved and this can be reopened if we decide to return to smaller stacked reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants