Add auth logout command with --profile and --force flags#4613
Add auth logout command with --profile and --force flags#4613mihaimitrea-db wants to merge 11 commits intomainfrom
Conversation
|
Commit: 333ea5a
27 interesting tests: 8 FAIL, 7 KNOWN, 7 SKIP, 3 flaky, 2 BUG
Top 24 slowest tests (at least 2 minutes):
|
0b2d46f to
5f039b9
Compare
simonfaltum
left a comment
There was a problem hiding this comment.
Looks good! Maybe add an extra test but can also be done in a follow up at a later stage
pietern
left a comment
There was a problem hiding this comment.
I recommend adding a few acceptance tests to test this e2e.
That would also capture some of the error messages.
| defaultConfigPath := "~/.databrickscfg" | ||
| if runtime.GOOS == "windows" { | ||
| defaultConfigPath = "%USERPROFILE%\\.databrickscfg" | ||
| } |
There was a problem hiding this comment.
This looks like it belongs (or should already exist) in libs/databrickscfg.
There was a problem hiding this comment.
This should also honor $DATABRICKS_CONFIG_FILE.
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "logout", | ||
| Short: "Log out of a Databricks profile", |
There was a problem hiding this comment.
Logging out implies (to me) only the token is removed. The long help says the profile itself is removed.
Either needs to be updated.
|
|
||
| tokenCache, err := cache.NewFileTokenCache() | ||
| if err != nil { | ||
| log.Warnf(ctx, "Failed to open token cache: %v", err) |
There was a problem hiding this comment.
When the file is corrupted
| return err | ||
| } | ||
| if !approved { | ||
| return nil |
There was a problem hiding this comment.
We should log something here as well.
| } | ||
|
|
||
| if err := tokenCache.Store(p.Name, nil); err != nil { | ||
| log.Warnf(ctx, "Failed to delete profile-keyed token for profile %q: %v", p.Name, err) |
There was a problem hiding this comment.
Is storing a nil equal to removing?
Doesn't the token cache have a native remove function?
There was a problem hiding this comment.
Yes, storing nil is equal to removing. The TokenCache interface defines the Store function as follows:
// Store stores the token with the given key, replacing any existing token.
// If t is nil, it deletes the token.
Store(key string, t *oauth2.Token) error
| // (host/oidc/accounts/<account_id>). | ||
| func clearTokenCache(ctx context.Context, p profile.Profile, profiler profile.Profiler, tokenCache cache.TokenCache) { | ||
| if tokenCache == nil { | ||
| return |
There was a problem hiding this comment.
If the token cache doesn't exist, but the profile does, should the profile still be deleted?
|
An acceptance test will also confirm the before/after state of the I would like to see if comments, ordering, etc, are retained upon profile deletion. |
b8fed18 to
0ca6e59
Compare
9cf58d8 to
66871b3
Compare
66871b3 to
1764259
Compare
Implement the initial version of databricks auth logout which removes a profile from ~/.databrickscfg and clears associated OAuth tokens from the token cache. This iteration supports explicit profile selection via --profile and a --force flag to skip the confirmation prompt. Interactive profile selection will be added in a follow-up. Token cache cleanup is best-effort: the profile-keyed token is always removed, and the host-keyed token is removed only when no other profile references the same host.
Replace plain fmt.Sprintf confirmation prompt with a structured template using cmdio.RenderWithTemplate. The warning now uses color and bold formatting to clearly highlight the profile name, config path, and consequences before prompting for confirmation.
Resolve config path from the profiler instead of hardcoding fallbacks. Delete the profile before clearing the token cache so a config write failure does not leave tokens removed. Fix token cleanup for account and unified profiles by computing the correct OIDC cache key (host/oidc/accounts/<account_id>). Drop the nil profiler guard, add a success message on logout, and extract backupConfigFile in ops.go to remove duplication. Consolidate token cleanup tests into a table-driven test covering shared hosts, unique hosts, account, and unified profiles.
Merge shared-host token deletion verification into one main parametrized test by addding the hostBasedKey and isSharedKey parameters to each case. This replaces the TestLogoutTokenCacheCleanup test with an assertion: host-based keys are preserved when another profile shares the same host, and deleted otherwise.
Rewrite the test to use inline config seeds and explicit expected state. Add cases for deleting the last non-default profile, deleting a unified host profile with multiple keys, and deleting the DEFAULT section.
- Use profiler.GetPath() to resolve config path instead of hardcoding platform-specific defaults for the help text. - Read DATABRICKS_CONFIG_FILE via env.Get(ctx, ...) instead of os.Getenv to respect context-level env overrides. - Add abort message when user declines the confirmation prompt. - Guard DeleteProfile against non-existent profiles to avoid creating unnecessary backup files. - Add TestDeleteProfile_NotFound for the error path.
Cover four scenarios: profile ordering and comments are preserved after deletion, deleting the last non-default profile leaves an empty DEFAULT section, deleting the DEFAULT profile itself clears its keys and restores the default comment, and error paths for non-existent profiles and missing --profile in non-interactive mode.
By default, Authentication related commands. For more information regarding how authentication for the Databricks CLI and SDKs work please refer to the documentation linked below. AWS: https://docs.databricks.com/dev-tools/auth/index.html Azure: https://learn.microsoft.com/azure/databricks/dev-tools/auth GCP: https://docs.gcp.databricks.com/dev-tools/auth/index.html Usage: databricks auth [command] Available Commands: describe Describes the credentials and the source of those credentials, being used by the CLI to authenticate env Get env login Log into a Databricks workspace or account profiles Lists profiles from ~/.databrickscfg token Get authentication token Flags: --account-id string Databricks Account ID --experimental-is-unified-host Flag to indicate if the host is a unified host -h, --help help for auth --host string Databricks Host --workspace-id string Databricks Workspace ID Global Flags: --debug enable debug logging -o, --output type output type: text or json (default text) -p, --profile string ~/.databrickscfg profile -t, --target string bundle target to use (if applicable) Use "databricks auth [command] --help" for more information about a command. now only clears cached OAuth tokens without removing the profile from ~/.databrickscfg. Pass --delete to also remove the profile entry from the config file.
1764259 to
e8a6f57
Compare
Existing acceptance tests that verify profile deletion now use --delete since profile removal is opt-in. Two new acceptance tests verify token-only logout: one for a unique host (both cache entries cleared) and one for a shared host (host-keyed token preserved).
🥞 Stacked PR
Use this link to review incremental changes.
Implement
databricks auth logoutwhich clears cached OAuth tokens for a profile and, optionally, removes the profile from ~/.databrickscfg.By default the command only clears tokens, requiring re-authentication on next use. Pass
--deleteto also remove the profile entry from the config file. This iteration supports explicit profile selection via--profileand a--forceflag to skip the confirmation prompt. Interactive profile selection will be added in a follow-up.Token cache cleanup is best-effort: the profile-keyed token is always removed, and the host-keyed token is removed only when no other profile references the same host.
Changes
databricks auth logoutcommand (cmd/auth/logout.go) registered under the auth command group.--profileselects the profile to log out of;--forceskips the confirmation prompt.--deleteopts in to removing the profile section from ~/.databrickscfg. Without it, only cached OAuth tokens are cleared.clearTokenCacheremoves the profile-keyed token and, if no other profile shares the same host, the host-keyed token. Host URLs are normalized (trailing slash stripped) to match token cache keys.DeleteProfileinlibs/databrickscfg/ops.goremoves a named section from ~/.databrickscfg, creating a .bak backup first.Why
There is currently no way to log out of a Databricks profile from the CLI. Users must manually edit ~/.databrickscfg and locate/delete token cache entries. This command provides a safe, single-step logout that handles token cleanup and optional profile removal.
Open issue from users requesting this feature: #1639
Tests
runLogoutcovering: successful logout, logout with--delete, non-interactive mode without--force, non-existent profile, shared-host token preservation, and logout with an empty token cache.DeleteProfilecovering: deleting an existing profile, profile not found, and custom config path.--profilein non-interactive mode.