Skip to content

Commit e712e7d

Browse files
authored
Fix "cache: token not found" for auth token command (#3447)
## Changes Update the "auth token" command to return the same error as CLI v0.263.0. ## Why Older versions of Databricks SDKs call this command to retrieve an OAuth token. They perform a substring match on the returned error to determine if the CLI experienced a real error or if OAuth is not an applicable authentication mechanism in the user's context. By changing the error message (see databricks/databricks-sdk-go#1250), these older SDKs no longer fall through but return an error `cache: no token found` when checking if they can use OAuth to authenticate. ## Tests Adds an acceptance test. It passes with the previous version of the CLI and passes with this commit.
1 parent 6430e79 commit e712e7d

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### CLI
1212

13+
* Fix "cache: token not found" for auth token command ([#3447](https://github.com/databricks/cli/pull/3447))
14+
1315
### Bundles
1416
* Add support for Lakebase database instances in DABs ([#3283](https://github.com/databricks/cli/pull/3283))
1517
* Add support for Lakebase database catalogs in DABs ([#3436](https://github.com/databricks/cli/pull/3436))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
>>> [CLI] auth token --host [DATABRICKS_URL]
3+
Error: cache: databricks OAuth is not configured for this host. Try logging in again with `databricks auth login --host [DATABRICKS_URL]` before retrying. If this fails, please report this issue to the Databricks CLI maintainers at https://github.com/databricks/cli/issues/new
4+
5+
Exit code: 1

acceptance/cmd/auth/token/script

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mkdir -p home
2+
export HOME="$PWD/home"
3+
4+
export DATABRICKS_HOST_ORIG="$DATABRICKS_HOST"
5+
export DATABRICKS_TOKEN_ORIG="$DATABRICKS_TOKEN"
6+
7+
unset DATABRICKS_HOST
8+
unset DATABRICKS_TOKEN
9+
10+
# This is expected to fail because there is no token cache.
11+
# We need to assert that the returned error message does not change,
12+
# because older SDK versions check for a particular substring.
13+
trace $CLI auth token --host $DATABRICKS_HOST_ORIG
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ignore = [
2+
"home"
3+
]

cmd/auth/token.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/databricks/cli/libs/auth"
1111
"github.com/databricks/cli/libs/databrickscfg/profile"
1212
"github.com/databricks/databricks-sdk-go/credentials/u2m"
13+
"github.com/databricks/databricks-sdk-go/credentials/u2m/cache"
1314
"github.com/spf13/cobra"
1415
"golang.org/x/oauth2"
1516
)
@@ -116,6 +117,18 @@ func loadToken(ctx context.Context, args loadTokenArgs) (*oauth2.Token, error) {
116117
}
117118
t, err := persistentAuth.Token()
118119
if err != nil {
120+
if errors.Is(err, cache.ErrNotFound) {
121+
// The error returned by the SDK when the token cache doesn't exist or doesn't contain a token
122+
// for the given host changed in SDK v0.77.0: https://github.com/databricks/databricks-sdk-go/pull/1250.
123+
// This was released as part of CLI v0.264.0.
124+
//
125+
// Older SDK versions check for a particular substring to determine if
126+
// the OAuth authentication type can fall through or if it is a real error.
127+
// This means we need to keep this error message constant for backwards compatibility.
128+
//
129+
// This is captured in an acceptance test under "cmd/auth/token".
130+
err = errors.New("cache: databricks OAuth is not configured for this host")
131+
}
119132
if err, ok := auth.RewriteAuthError(ctx, args.authArguments.Host, args.authArguments.AccountID, args.profileName, err); ok {
120133
return nil, err
121134
}

0 commit comments

Comments
 (0)