Skip to content

Deprecate auth env + ensure the cmd uses resolved auth context#4904

Closed
simonfaltum wants to merge 12 commits intomainfrom
simonfaltum/auth-env-refactor
Closed

Deprecate auth env + ensure the cmd uses resolved auth context#4904
simonfaltum wants to merge 12 commits intomainfrom
simonfaltum/auth-env-refactor

Conversation

@simonfaltum
Copy link
Copy Markdown
Member

@simonfaltum simonfaltum commented Apr 6, 2026

Why

auth env had its own custom auth resolution logic (local --host/--profile flags, manual profile-to-host matching via ini file scanning). It didn't go through the standard CLI auth chain, so it couldn't resolve auth from bundle context, default profiles, or environment variables. It was effectively a standalone tool that only worked with explicit profile or host arguments.

The command should return the environment variables needed to authenticate as the exact same identity the CLI is currently authenticated as.

Changes

Refactored to use the CLI's standard auth resolution (root.MustAnyClient + cmdctx.ConfigUsed), same pattern as auth describe. Uses auth.Env(cfg) from libs/auth/env.go to generate env vars. This means it now works with bundle context, env var auth, default profiles, and all other standard auth paths.

Before: databricks auth env --host https://my-workspace.cloud.databricks.com with custom profile matching.
Now: databricks auth env returns the env vars for whatever identity the CLI resolved through its normal auth chain.

Also adds --output text support for KEY=VALUE lines with proper shell quoting, and hides the command from databricks auth --help listings (still invokable, --help still works).

JSON shape is preserved ({"env": {...}}) to avoid breaking downstream scripts.

Breaking changes:

  • Removed command-specific --host and --profile flags (the inherited flags from the parent auth and root commands cover this)
  • Only the primary env var per attribute is emitted (via auth.Env, consistent with bundle run)

Structure:

  • Formatting extracted into a pure writeEnvOutput(w, envVars, textMode) function; the Cobra RunE is a thin adapter. Follows the decoupling patterns from go-code-structure.md (principles 1 and 5).

Removed ~120 lines of dead code: canonicalHost, resolveSection, loadFromDatabricksCfg, collectEnvVars, ErrNoMatchingProfiles.

Test plan

  • Unit tests for text and JSON output modes (via writeEnvOutput directly, no Cobra setup required)
  • Unit tests for shell quoting: spaces, special chars, single quotes, newlines
  • NEXT_CHANGELOG.md entry added
  • Existing cmd/auth tests pass; make checks and full golangci-lint clean
  • Manually verified against --profile e2-dogfood: JSON shape matches prior v0.296.0 behavior, all expected env vars present (DATABRICKS_HOST, DATABRICKS_ACCOUNT_ID, DATABRICKS_CONFIG_PROFILE, DATABRICKS_WORKSPACE_ID, etc.), text mode emits sorted shell-quoted KEY=VALUE lines
  • Verified env no longer appears under databricks auth --help, but databricks auth env --help still works

Use root.MustAnyClient and auth.Env instead of custom profile/host
resolution logic. auth env now returns the environment variables for the
exact identity the CLI is authenticated as, including bundle context and
all standard auth resolution paths.

Also adds --output text support for KEY=VALUE lines with proper shell
quoting.

Breaking changes:
- Removed command-specific --host and --profile flags (use inherited flags)
- JSON output is a flat map instead of wrapped in {"env": ...}
- Only the primary env var per attribute is emitted (via auth.Env)

Co-authored-by: Isaac
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

Approval status: pending

/cmd/auth/ - needs approval

Files: cmd/auth/env.go, cmd/auth/env_test.go
Suggested: @mihaimitrea-db
Also eligible: @tanmay-db, @tejaskochar-db, @hectorcast-db, @renaudhartert-db, @parthban-db, @Divyansh-db, @chrisst, @rauchy

General files (require maintainer)

Files: NEXT_CHANGELOG.md
Based on git history:

  • @denik -- recent work in ./, cmd/auth/

Any maintainer (@andrewnester, @anton-107, @denik, @pietern, @shreyas-goenka, @renaudhartert-db) can approve all areas.
See OWNERS for ownership rules.

simonfaltum and others added 9 commits April 17, 2026 14:56
Applies go-code-structure.md principles to the auth env command:

- Extracts formatting into a pure writeEnvOutput(w, envVars, textMode)
  function so the business logic does not depend on *cobra.Command.
- Replaces the end-to-end Cobra-based test with direct bytes.Buffer
  tests of writeEnvOutput. No more env var mocking or parent command
  setup just to verify output formatting.
- Fixes the quoteEnvValue doc comment typo (plain ASCII single quotes).
- Adds a trailing newline to JSON output so the terminal prompt does
  not land on the closing brace.
- Adds a NEXT_CHANGELOG entry calling out the breaking flag/JSON-shape
  changes from the parent refactor.

Co-authored-by: Isaac
gofmt 1.19+ reformats godoc comments and treats the literal POSIX
escape sequence (three adjacent single quotes) as smart-quote
candidates, mangling the comment and failing CI.

Co-authored-by: Isaac
Keep it invokable (with --help) but out of `databricks auth` and
`databricks auth --help` listings.

Co-authored-by: Isaac
Reverts the flat-map change to preserve the pre-refactor JSON shape
for downstream scripts that may consume `databricks auth env`.

Co-authored-by: Isaac
Prints a deprecation warning to stderr when the command is invoked,
notes the deprecation in the Long help, and updates the changelog
entry. The command remains hidden from the auth command listing.

Co-authored-by: Isaac
Re-run to work around transient zipball fetch failure for astral-sh/ruff-action.

Co-authored-by: Isaac
@simonfaltum simonfaltum changed the title Refactor auth env to use CLI's resolved auth context Deprecate auth env + ensure the cmd uses resolved auth context Apr 21, 2026
@simonfaltum simonfaltum mentioned this pull request Apr 21, 2026
5 tasks
@simonfaltum
Copy link
Copy Markdown
Member Author

Closing in favor of #5049, which is a much smaller, deprecation-only change (9 lines added across 2 files) without the refactor and new output modes that muddied this PR.

bernardo-rodriguez pushed a commit to bernardo-rodriguez/b-cli that referenced this pull request Apr 21, 2026
## Why

`auth env` has never really fit the rest of the CLI. It has its own
custom auth resolution (local `--host`/`--profile` flags, manual ini
scanning) instead of going through the standard auth chain, and it
overlaps with what callers can get from the SDK directly. Rather than
invest more in maintaining it, we want to signal that it's on its way
out so users stop depending on it.

## Changes

Deprecate the command:

- `Hidden: true` — no longer shows up under `databricks auth --help`
(still invokable, `--help` still works).
- Prints `Warning: 'databricks auth env' is deprecated and will be
removed in a future release.` to stderr on every invocation.
- Long help and `Short` mention the deprecation.

Behavior is otherwise unchanged. JSON output shape, flags, and
resolution logic all stay identical so anything currently scripting
against the command keeps working until the removal PR.

Replaces databricks#4904, which mixed this deprecation with a larger refactor.

## Test plan

- [x] `make checks` clean
- [x] `go test ./cmd/auth/...` passes
- [x] `databricks auth env --help` still works and shows the
"Deprecated" note
- [x] `databricks auth env` no longer appears under `databricks auth
--help`
- [x] Running the command prints the warning to stderr and the JSON env
output to stdout unchanged
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