Skip to content

Commit a48290e

Browse files
committed
Fix shared-host token preservation, test mock semantics, and searcher coupling
- Only preserve host-keyed OAuth tokens when another U2M profile shares the host. Non-U2M profiles (PAT, M2M) never use the OAuth cache, so they should not prevent token cleanup on logout. - Add m2m token entries to test fixtures and assert they are preserved after logout, verifying the isCreatedByLogin guard works correctly. - Fix inMemoryTokenCache.Store(nil) to delete the key (matching production FileTokenCache behavior) instead of storing a nil value. - Build the promptui Searcher from the items slice directly in SelectProfile to avoid implicit coupling with the original Profiles slice.
1 parent 2a64ec5 commit a48290e

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎cmd/auth/in_memory_test.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ func (i *inMemoryTokenCache) Lookup(key string) (*oauth2.Token, error) {
2020

2121
// Store implements TokenCache.
2222
func (i *inMemoryTokenCache) Store(key string, t *oauth2.Token) error {
23-
i.Tokens[key] = t
23+
if t == nil {
24+
delete(i.Tokens, key)
25+
} else {
26+
i.Tokens[key] = t
27+
}
2428
return nil
2529
}
2630

‎cmd/auth/logout.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ func clearTokenCache(ctx context.Context, p profile.Profile, profiler profile.Pr
230230
return fmt.Errorf("failed to get host-based cache key for profile %q", p.Name)
231231
}
232232

233+
// Only preserve the host-keyed token if another U2M profile shares the
234+
// same host. Non-U2M profiles (PAT, M2M, etc.) never use the OAuth
235+
// token cache, so they should not prevent cleanup.
233236
otherProfiles, err := profiler.LoadProfiles(ctx, func(candidate profile.Profile) bool {
234-
return candidate.Name != p.Name && matchFn(candidate)
237+
return candidate.Name != p.Name && candidate.AuthType == "databricks-cli" && matchFn(candidate)
235238
})
236239
if err != nil {
237240
return fmt.Errorf("failed to load profiles for host cache key %q: %w", hostCacheKey, err)

‎cmd/auth/logout_test.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var logoutTestTokensCacheConfig = map[string]*oauth2.Token{
5151
"https://my-unique-workspace.cloud.databricks.com": {AccessToken: "unique-workspace-host-token"},
5252
"https://accounts.cloud.databricks.com/oidc/accounts/abc123": {AccessToken: "account-host-token"},
5353
"https://unified.cloud.databricks.com/oidc/accounts/def456": {AccessToken: "unified-host-token"},
54+
"my-m2m": {AccessToken: "m2m-service-token"},
55+
"https://my-m2m.cloud.databricks.com": {AccessToken: "m2m-host-token"},
5456
}
5557

5658
func copyTokens(src map[string]*oauth2.Token) map[string]*oauth2.Token {
@@ -74,6 +76,7 @@ func TestLogout(t *testing.T) {
7476
profileName string
7577
hostBasedKey string
7678
isSharedKey bool
79+
isNonU2M bool // true for profiles that are not created by login (PAT, M2M, etc.)
7780
force bool
7881
deleteProfile bool
7982
wantErr string
@@ -151,8 +154,10 @@ func TestLogout(t *testing.T) {
151154
deleteProfile: true,
152155
},
153156
{
154-
name: "do not delete m2m profile",
157+
name: "do not delete m2m profile tokens",
155158
profileName: "my-m2m",
159+
hostBasedKey: "https://my-m2m.cloud.databricks.com",
160+
isNonU2M: true,
156161
force: true,
157162
deleteProfile: false,
158163
},
@@ -191,12 +196,18 @@ func TestLogout(t *testing.T) {
191196
assert.NotEmpty(t, profiles, "expected profile %q to still exist", tc.profileName)
192197
}
193198

194-
// Verify tokens were cleaned up.
195-
assert.Nil(t, tokenCache.Tokens[tc.profileName], "expected token %q to be removed", tc.profileName)
196-
if tc.isSharedKey {
197-
assert.NotNil(t, tokenCache.Tokens[tc.hostBasedKey], "expected token %q to be preserved", tc.hostBasedKey)
199+
// Verify token cache state.
200+
if tc.isNonU2M {
201+
// Non-U2M profiles should not touch the token cache at all.
202+
assert.NotNil(t, tokenCache.Tokens[tc.profileName], "expected token %q to be preserved for non-U2M profile", tc.profileName)
203+
assert.NotNil(t, tokenCache.Tokens[tc.hostBasedKey], "expected token %q to be preserved for non-U2M profile", tc.hostBasedKey)
198204
} else {
199-
assert.Nil(t, tokenCache.Tokens[tc.hostBasedKey], "expected token %q to be removed", tc.hostBasedKey)
205+
assert.Nil(t, tokenCache.Tokens[tc.profileName], "expected token %q to be removed", tc.profileName)
206+
if tc.isSharedKey {
207+
assert.NotNil(t, tokenCache.Tokens[tc.hostBasedKey], "expected token %q to be preserved", tc.hostBasedKey)
208+
} else {
209+
assert.Nil(t, tokenCache.Tokens[tc.hostBasedKey], "expected token %q to be removed", tc.hostBasedKey)
210+
}
200211
}
201212
})
202213
}

‎libs/databrickscfg/profile/select.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"github.com/databricks/cli/libs/cmdio"
910
"github.com/manifoldco/promptui"
@@ -76,11 +77,22 @@ func SelectProfile(ctx context.Context, cfg SelectConfig) (string, error) {
7677
cfg.SelectedTemplate = defaultSelectedTemplate
7778
}
7879

80+
// Build the searcher from the items slice directly so it stays coupled
81+
// to the Items list passed to promptui (rather than the original Profiles
82+
// slice which could diverge if items were ever filtered or reordered).
83+
searcher := func(input string, index int) bool {
84+
input = strings.ToLower(input)
85+
p := items[index].Profile
86+
return strings.Contains(strings.ToLower(p.Name), input) ||
87+
strings.Contains(strings.ToLower(p.Host), input) ||
88+
strings.Contains(strings.ToLower(p.AccountID), input)
89+
}
90+
7991
i, _, err := cmdio.RunSelect(ctx, &promptui.Select{
8092
Label: cfg.Label,
8193
Items: items,
8294
StartInSearchMode: cfg.StartInSearchMode,
83-
Searcher: cfg.Profiles.SearchCaseInsensitive,
95+
Searcher: searcher,
8496
Templates: &promptui.SelectTemplates{
8597
Label: "{{ . | faint }}",
8698
Active: cfg.ActiveTemplate,

0 commit comments

Comments
 (0)