Skip to content

Honor profile.authType in default credential chain and delegate name()#182

Draft
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug6-authtype-name
Draft

Honor profile.authType in default credential chain and delegate name()#182
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug6-authtype-name

Conversation

@parthban-db
Copy link
Copy Markdown
Contributor

@parthban-db parthban-db commented Jun 3, 2026

🥞 Stacked PR

Use this link to review incremental changes.


Summary

The default credential chain now honors profile.authType, and DefaultCredentials.name() reports the strategy that actually resolved instead of always returning default.

Why

The chain previously tried strategies in a fixed order (PAT, OAuth M2M, Databricks CLI) and stopped at the first applicable one, ignoring profile.authType even though it is parsed from auth_type / DATABRICKS_AUTH_TYPE. A profile that set auth_type = databricks-cli but also carried a token would silently use PAT, with no way to pin a method. Separately, name() was hardcoded to default, so logging and future User-Agent auth= telemetry could never tell which strategy won.

What changed

  • Strategy is now an interface {name, configure} instead of a bare function, so the chain can match a strategy against authType by name (pat, oauth-m2m, databricks-cli) before building it. Converted patStrategy, m2mStrategy, and u2mStrategy to this shape with unchanged config logic.
  • When authType is set, the chain configures only the matching strategy with no fallback: it throws NO_AUTH_CONFIGURED if that strategy cannot configure, and the new AUTH_TYPE_NOT_FOUND code if no strategy matches. When authType is unset, ordered fall-through behavior is unchanged.
  • name() returns default before resolution and the resolved strategy's name afterward.
  • These types are internal (not exported from the package barrel), so this is not a public API change.
  • Mirrors the Go SDK's DefaultCredentials (config/auth_default.go); the JS strategy set is smaller, so fewer authType names are accepted — an existing scope difference. Composing the User-Agent auth= segment is out of scope (JS does not emit one yet).

Validated: npm run build, npm test, npm run test:browser, npm run typecheck, npm run lint, and npm run format:check all pass for @databricks/sdk-auth.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
If this is not necessary for your PR, please include the following in your PR description:
NO_CHANGELOG=true
and rerun the job.

1 similar comment
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
If this is not necessary for your PR, please include the following in your PR description:
NO_CHANGELOG=true
and rerun the job.

## Summary

Make the default credential chain honor `profile.authType` and have `DefaultCredentials.name()` report the strategy that actually resolved, instead of always trying a fixed order and always returning `default`.

## Why

The default credential chain previously tried strategies in a fixed priority order (PAT, then OAuth M2M, then Databricks CLI) and stopped at the first one whose required fields were present. It never consulted `profile.authType`, even though that field is fully parsed from `auth_type` in the config file and `DATABRICKS_AUTH_TYPE` in the environment (see `packages/core/src/profiles/profile.ts`). As a result, a profile that explicitly set `auth_type = databricks-cli` but also happened to carry a `token` would silently authenticate with PAT, because PAT comes first in the fixed order and a token was present. Users had no way to pin a specific authentication method when their environment was compatible with more than one.

A second, related problem: `DefaultCredentials.name()` was hardcoded to return `default`. Because that name is the identifier the chain exposes for the winning strategy, callers (logging and, in the future, the User-Agent `auth=` telemetry segment) could not tell which strategy was actually used. The reported name was always wrong once a real strategy resolved.

This PR addresses both: when `authType` is set, the chain selects the strategy whose name matches it; and `name()` now delegates to the resolved strategy so it reflects the method that actually authenticated.

## What changed

### Interface changes

- **`Strategy`** is now an interface — `{readonly name: string; readonly configure: (profile: Profile) => Credentials | undefined}` — rather than a bare `(profile) => Credentials | undefined` function. The `name` field (`pat`, `oauth-m2m`, `databricks-cli`) matches the `name()` of the credentials the strategy produces, which is what lets the chain match a strategy against `profile.authType` without first building it. `Strategy`, `DefaultCredentials`, `patStrategy`, and `m2mStrategy` are internal (not exported from the package barrel), so this is not a public API change for SDK consumers.
- **`DefaultCredentialsErrorCode`** gains a new discriminant, `AUTH_TYPE_NOT_FOUND`, alongside the existing `NO_AUTH_CONFIGURED`.

### Behavioral changes

- When `profile.authType` is set, the chain selects the strategy whose name equals it and configures only that strategy. It does not fall back to any other strategy. If the named strategy exists but cannot configure itself from the profile (e.g. `auth_type = pat` with no token), the chain throws `NO_AUTH_CONFIGURED`. If no strategy has that name, it throws `AUTH_TYPE_NOT_FOUND` with a message pointing at the supported auth types.
- When `profile.authType` is not set, behavior is unchanged: strategies are tried in order and the first configured one wins.
- `DefaultCredentials.name()` returns `default` before the chain resolves (i.e. before the first `authHeaders()` call) and the resolved strategy's name afterwards. Previously it always returned `default`.

### Internal changes

- `patStrategy`, `m2mStrategy`, and `u2mStrategy` were converted from bare functions to the new named-object `Strategy` shape; their configuration logic is unchanged.
- `DefaultCredentials.resolveChain()` now branches on `profile.authType`, with the authType path factored into a `resolveByAuthType` helper. The resolved `Credentials` is cached as before, so the profile loader and strategy configuration still run at most once.

## Go-parity note

This mirrors the Go SDK's `DefaultCredentials` (`config/auth_default.go`): when `AuthType` is set, Go iterates its strategies, picks the one whose `Name()` matches, records that name, and returns it (or errors with `auth type %q not found` when none matches); `Name()` returns `default` only until a strategy is recorded. The JS chain has a smaller strategy set (PAT, OAuth M2M, Databricks CLI) than Go, so the set of names accepted for `authType` is correspondingly smaller — that is an existing scope difference, not introduced here.

Deliberate scope limitation: the finding mentions composing the User-Agent `auth=` segment lazily once the chain resolves. The JS SDK does not yet emit an `auth=` segment in its User-Agent (only the product/agent segment in `packages/core/src/clientinfo`), so there is nothing to compose into today. This PR fixes the root cause — `name()` now reports the resolved strategy — so a future `auth=` segment can read the correct value. Adding the segment itself is left out of scope.

## How is this tested?

Unit tests in `packages/auth/tests/credentials/default/chain.test.ts`, run under both the Node and browser vitest runners. The previous test that locked in `name()` always returning `default` was updated to assert it returns `default` before resolution and the resolved strategy name (`pat`) after. New coverage was added for: authType selecting a non-first strategy (PAT configured and first, but `authType = oauth-m2m` wins); `AUTH_TYPE_NOT_FOUND` when authType names no strategy; `NO_AUTH_CONFIGURED` when the authType-named strategy cannot configure itself (no fallback); and `name()` reporting the authType-selected strategy. `npm run build`, `npm test`, `npm run test:browser`, `npm run typecheck`, `npm run lint`, and `npm run format:check` all pass for `@databricks/sdk-auth` (and `@databricks/sdk-core` builds clean as the upstream dependency).

Co-authored-by: Isaac
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug6-authtype-name branch from b69fa83 to 520820d Compare June 4, 2026 13:03
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.

1 participant