Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/auth-keyring-large-tokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix `databricks auth login` failing with `store token: data passed to Set was too big` on macOS and Windows when the OAuth access token is too large for one OS keyring item. Such a token is now stored across several keyring items. ([#7845](https://github.com/databricks/cli/pull/7845))
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Error: --confirmation-code requires --use-device-code
>>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test#device-code --use-device-code
Error: profile name "test#device-code" must not end with #device-code, which marks pending device code logins

=== Profile name that marks an access token part

>>> musterr [CLI] auth login --host [DATABRICKS_URL] --profile test#access-token-0 --use-device-code
Error: profile name "test#access-token-0" must not contain #access-token-, which marks the parts of large access tokens

=== Account host without an account ID

>>> musterr [CLI] auth login --host https://accounts.cloud.databricks.test --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ trace musterr $CLI auth login --host $DATABRICKS_HOST --profile test --user-code
title "Profile name that marks a pending login"
trace musterr $CLI auth login --host $DATABRICKS_HOST --profile 'test#device-code' --use-device-code

title "Profile name that marks an access token part"
trace musterr $CLI auth login --host $DATABRICKS_HOST --profile 'test#access-token-0' --use-device-code

# The non-interactive login cannot prompt for the account ID.
title "Account host without an account ID"
trace musterr $CLI auth login --host https://accounts.cloud.databricks.test --use-device-code --user-code WDJB-0001 --confirmation-code FTFA-QQKY
Expand Down
3 changes: 3 additions & 0 deletions cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func storeLoginToken(ctx context.Context, tokenStore storage.Store, mode storage
tokenStore = storage.WrapForOAuthArgument(ctx, tokenStore, mode, arg)
key := arg.GetCacheKey()
if err := tokenStore.Put(key, storage.Entry{Token: token}); err != nil {
// This includes storage.ErrAccessTokenNotStored, which a refresh treats
// as stored: a failed login can be run again, while a refresh has to
// keep the refresh token that the server just replaced.
storeErr := fmt.Errorf("store token: %w", err)
// The profile is already saved, but the old token may still be present.
// Delete it so later commands cannot use it with the updated profile.
Expand Down
8 changes: 8 additions & 0 deletions cmd/auth/login_device_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ func pendingLoginKey(cacheKey string) string {

// validateLoginProfileName returns an error if the token of profile name would
// share its token store key with the pending login of another profile.
// It also rejects a name that would share a key with a part of another
// profile's access token.
func validateLoginProfileName(name string) error {
if strings.HasSuffix(name, pendingLoginKeySuffix) {
return fmt.Errorf("profile name %q must not end with %s, which marks pending device code logins", name, pendingLoginKeySuffix)
}
// Windows Credential Manager ignores case in credential names, so a
// profile named "dev#Access-Token-0" would share the item of a part of dev.
// https://learn.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentialw
if strings.Contains(strings.ToLower(name), storage.AccessTokenPartMarker) {
return fmt.Errorf("profile name %q must not contain %s, which marks the parts of large access tokens", name, storage.AccessTokenPartMarker)
}
return nil
}

Expand Down
36 changes: 36 additions & 0 deletions cmd/auth/login_device_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,39 @@ func TestLoginDeviceCodeWrongConfirmationCode(t *testing.T) {
})
}
}

func TestValidateLoginProfileName(t *testing.T) {
cases := []struct {
name string
profile string
wantErr string
}{
{name: "plain name", profile: "dev"},
{
name: "key of a pending device code login",
profile: "dev#device-code",
wantErr: `profile name "dev#device-code" must not end with #device-code, which marks pending device code logins`,
},
{
name: "key of an access token part",
profile: "dev#access-token-0",
wantErr: `profile name "dev#access-token-0" must not contain #access-token-, which marks the parts of large access tokens`,
},
{
name: "key of an access token part in another letter case",
profile: "dev#Access-Token-0",
wantErr: `profile name "dev#Access-Token-0" must not contain #access-token-, which marks the parts of large access tokens`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := validateLoginProfileName(tc.profile)
if tc.wantErr == "" {
require.NoError(t, err)
return
}
assert.EqualError(t, err, tc.wantErr)
})
}
}
4 changes: 3 additions & 1 deletion cmd/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,9 @@ func TestPromptForProfileRejectsTakenName(t *testing.T) {
tm.WaitFor(`>> profile name "__settings__" is reserved for internal use`)
tm.Type(strings.Repeat(termtest.KeyBackspace, len("__settings__")) + "take#device-code" + termtest.KeyEnter)
tm.WaitFor(`>> profile name "take#device-code" must not end with #device-code, which marks pending device code logins`)
tm.Type(strings.Repeat(termtest.KeyBackspace, len("#device-code")) + termtest.KeyEnter)
tm.Type(strings.Repeat(termtest.KeyBackspace, len("#device-code")) + "#access-token-0" + termtest.KeyEnter)
tm.WaitFor(`>> profile name "take#access-token-0" must not contain #access-token-, which marks the parts of large access tokens`)
tm.Type(strings.Repeat(termtest.KeyBackspace, len("#access-token-0")) + termtest.KeyEnter)
name, err := tm.Result()
require.NoError(t, err)
assert.Equal(t, "take", name)
Expand Down
71 changes: 71 additions & 0 deletions cmd/auth/logout_keyring_os_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build darwin || windows

package auth

import (
"os"
"strings"
"testing"
"uuid"

"github.com/databricks/cli/libs/auth/storage"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/databrickscfg/profile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zalando/go-keyring"
"golang.org/x/oauth2"
)

// TestLogoutRemovesSplitTokenFromOSKeyring logs out of a profile whose token
// the real OS keyring holds in several items, because it is too large for
// one, and checks that none of them is left. It uses the store that logout
// uses, with its 3-second timeout per keyring operation.
//
// It writes to the real OS keyring and therefore runs on CI only (GitHub
// Actions sets CI=true).
func TestLogoutRemovesSplitTokenFromOSKeyring(t *testing.T) {
if os.Getenv("CI") == "" {
t.Skip("touches the real OS keyring; runs on CI only")
}

// The keyring store keeps every item in this service.
const service = "databricks-cli"
profileName := "__probe_" + uuid.New().String()
items := []string{
profileName,
profileName + storage.AccessTokenPartMarker + "0",
profileName + storage.AccessTokenPartMarker + "1",
profileName + storage.AccessTokenPartMarker + "2",
}
t.Cleanup(func() {
for _, account := range items {
_ = keyring.Delete(service, account)
}
})

ctx := cmdio.MockDiscard(t.Context())
configPath := writeTempConfig(t, "["+profileName+"]\nhost = https://keyring-logout.cloud.databricks.test\nauth_type = databricks-cli\n")
t.Setenv("DATABRICKS_CONFIG_FILE", configPath)

tokenStore := storage.NewKeyringStore()
// The size of the access tokens in https://github.com/databricks/cli/issues/7707.
tok := &oauth2.Token{AccessToken: strings.Repeat("a", 3250), RefreshToken: "refresh-token"}
require.NoError(t, tokenStore.Put(profileName, storage.Entry{Token: tok}))
_, err := keyring.Get(service, items[1])
require.NoError(t, err, "the token was not split")

err = runLogout(ctx, logoutArgs{
profileName: profileName,
autoApprove: true,
profiler: profile.DefaultProfiler,
tokenStore: tokenStore,
configFilePath: configPath,
})
require.NoError(t, err)

for _, account := range items {
_, err := keyring.Get(service, account)
assert.ErrorIs(t, err, keyring.ErrNotFound, "item %q is left in the OS keyring", account)
}
}
Loading
Loading