Commit 61a1131
authored
Enrich 401/403 errors with auth identity context (#4576)
## Why
When CLI commands fail with 401 (unauthorized) or 403 (forbidden), the
error message shows only the raw API error (e.g., `PERMISSION_DENIED:
...`) with no indication of which profile, host, or auth method was
used. This makes debugging auth issues unnecessarily difficult — the
first thing users and support ask is "which identity am I using?"
## Changes
- **`libs/auth/error.go`** — New `EnrichAuthError` function that detects
`apierr.APIError` with status 401 or 403 and appends:
- Identity context: profile, host, auth type (each omitted if empty)
- Auth-type-aware remediation steps (OAuth → `auth login`, PAT →
regenerate token, Azure CLI → `az login`, M2M → check credentials, etc.)
- `auth describe` command with correct flags for
workspace/account/unified contexts (including `--workspace-id`)
- Profile nudge when using env-var-based auth
- **`libs/auth/error.go`** — New `BuildDescribeCommand` that builds
`databricks auth describe` flags directly from `*config.Config` (avoids
information loss from OAuthArgument conversion, e.g., workspace-id)
- **`cmd/root/root.go`** — Call `EnrichAuthError` in the centralized
`Execute` error path, gated on `cmdctx.HasConfigUsed`. Covers both
client creation errors (PersistentPreRunE) and command execution errors
(RunE)
- **`acceptance/workspace/jobs/create-error/output.txt`** — Regenerated
golden file
### Scope limitations
Commands that bypass `MustWorkspaceClient`/`MustAccountClient` and don't
call `SetConfigUsed` (e.g., `databricks api`) won't get enrichment. This
can be addressed in a follow-up.
### Example output
<img width="1354" height="173" alt="image"
src="https://github.com/user-attachments/assets/901687e6-d7dc-4837-9715-bb1405bd1280"
/>
https://github.com/user-attachments/assets/2fa2445f-c15b-4dfa-b785-255c4e621f6d
**403 with profile:**
```
Error: PERMISSION_DENIED: User does not have permission.
Profile: my-profile
Host: https://myworkspace.cloud.databricks.com
Auth type: pat
Next steps:
- Verify you have the required permissions for this operation
- Check your identity: databricks auth describe --profile my-profile
```
**401 without profile (env var auth):**
```
Error: UNAUTHENTICATED: Token expired.
Host: https://myworkspace.cloud.databricks.com
Auth type: pat
Next steps:
- Regenerate your access token
- Check your identity: databricks auth describe --host https://myworkspace.cloud.databricks.com
- Consider configuring a profile: databricks configure --profile <name>
```
## Test plan
### Automated
- [x] `go test ./libs/auth/...` — unit tests for `EnrichAuthError`,
`BuildDescribeCommand`, error preservation, all auth types, unified host
workspace-id
- [x] `go test ./cmd/root/...` — integration tests for Execute wiring
(with ConfigUsed, without, ErrAlreadyPrinted)
- [x] `go test ./acceptance -run TestAccept/workspace/jobs/create-error`
— acceptance test with updated golden file
- [x] `make checks` — whitespace, tidy, links
### Manual testing
**Force a 401 (expired/invalid token):**
```bash
# Option A: Set a bogus token via env vars (no profile)
DATABRICKS_HOST=https://<your-workspace>.cloud.databricks.com \
DATABRICKS_TOKEN=invalid-token-abc123 \
/tmp/databricks-test clusters list
# Option B: Create a profile with a bad token
cat >> ~/.databrickscfg << 'EOF'
[bad-token-test]
host = https://<your-workspace>.cloud.databricks.com
token = dapi_invalid_token_for_testing
EOF
/tmp/databricks-test clusters list --profile bad-token-test
# Clean up after:
# Remove the [bad-token-test] section from ~/.databrickscfg
```
**Verify non-auth errors are unaffected:**
```bash
# A 404 should show no enrichment:
/tmp/databricks-test clusters get --cluster-id nonexistent-id --profile <your-profile>
```1 parent 794469d commit 61a1131
File tree
5 files changed
+524
-0
lines changed- acceptance/workspace/jobs/create-error
- cmd/root
- libs/auth
5 files changed
+524
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
150 | 155 | | |
151 | 156 | | |
152 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
| 10 | + | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
11 | 54 | | |
12 | 55 | | |
13 | 56 | | |
| |||
27 | 70 | | |
28 | 71 | | |
29 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
30 | 169 | | |
31 | 170 | | |
32 | 171 | | |
| |||
48 | 187 | | |
49 | 188 | | |
50 | 189 | | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
0 commit comments