From b523e6b5a2f0701f620b676be9ffd8d3ee84564d Mon Sep 17 00:00:00 2001 From: Steven Chen Date: Fri, 13 Nov 2026 19:27:31 +0000 Subject: [PATCH 1/3] air: skip the Me() credential probe unless GetRun is denied `air logs`, `air get`, and `air run --watch` called CurrentUser.Me before every GetRun to tell a bad credential apart from a failed request. Run the probe only when GetRun fails with 401/403, the one case where the two are ambiguous, so successful calls save a round trip and error classification is unchanged. Co-authored-by: Isaac --- cmd/air/get.go | 25 ++++++++++++++++--------- cmd/air/get_test.go | 31 +++++++++++++++++++++++++------ cmd/air/logs.go | 10 +++------- cmd/air/logs_test.go | 12 ++++++++---- cmd/air/run_watch_test.go | 4 ++-- 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/cmd/air/get.go b/cmd/air/get.go index 18967532981..10395610bc9 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/service/iam" @@ -102,6 +103,18 @@ func authError(ctx context.Context, cmd *cobra.Command, err error) error { fmt.Errorf("failed to verify authentication: %w", err)) } +// credentialError tells a bad credential apart from missing access after a call +// fails with 401/403. MustWorkspaceClient only attaches credentials (it does not +// check a PAT server-side), so the Me() probe runs here, only on failure. It +// returns the probe's error when the credentials themselves fail, or nil. +func credentialError(ctx context.Context, w *databricks.WorkspaceClient, err error) error { + if !errors.Is(err, apierr.ErrUnauthenticated) && !errors.Is(err, apierr.ErrPermissionDenied) { + return nil + } + _, meErr := w.CurrentUser.Me(ctx, iam.MeRequest{}) + return meErr +} + // newGetCommand returns the `air get JOB_RUN_ID` command, which shows status, // configuration, and timing details for a specific run. func newGetCommand() *cobra.Command { @@ -139,17 +152,11 @@ JOB_RUN_ID is the job run ID returned by "databricks air run".`, fmt.Errorf("invalid JOB_RUN_ID %q: must be a positive integer", args[0])) } - // Validate authentication against the workspace before fetching or - // rendering anything. MustWorkspaceClient's Config.Authenticate only - // attaches credentials (e.g. it does not check a PAT server-side), so - // without this a bad credential would surface as a confusing failure - // mid-render instead of a clear "not authenticated" error here. - if _, err := w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { - return authError(ctx, cmd, err) - } - run, err := w.Jobs.GetRun(ctx, jobs.GetRunRequest{RunId: runID}) if err != nil { + if credErr := credentialError(ctx, w, err); credErr != nil { + return authError(ctx, cmd, credErr) + } // The backend returns this when the run ID is unknown to the user. if errors.Is(err, apierr.ErrResourceDoesNotExist) { return renderError(ctx, cmd, "NOT_FOUND", "NOT_FOUND", false, diff --git a/cmd/air/get_test.go b/cmd/air/get_test.go index 95c2d21519d..b3bf076a66a 100644 --- a/cmd/air/get_test.go +++ b/cmd/air/get_test.go @@ -16,6 +16,7 @@ import ( "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/experimental/mocks" + "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -57,9 +58,9 @@ func TestGetRunInvalidID(t *testing.T) { assert.Contains(t, err.Error(), "invalid JOB_RUN_ID") } -// notFoundGetServer serves the auth probe plus a runs/get that reports the run -// as missing (400 INVALID_PARAMETER_VALUE, which the SDK maps to -// ErrResourceDoesNotExist for this path). +// notFoundGetServer serves a runs/get that reports the run as missing (400 +// INVALID_PARAMETER_VALUE, which the SDK maps to ErrResourceDoesNotExist for +// this path). func notFoundGetServer(t *testing.T) *httptest.Server { t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -68,7 +69,7 @@ func notFoundGetServer(t *testing.T) *httptest.Server { _, _ = w.Write([]byte(`{"error_code":"INVALID_PARAMETER_VALUE","message":"Run 5 does not exist."}`)) return } - // Me() probe and any other config discovery. + // SDK config discovery. _, _ = w.Write([]byte(`{"userName":"u@example.com"}`)) })) t.Cleanup(srv.Close) @@ -88,8 +89,9 @@ func TestGetRunNotFound(t *testing.T) { func TestGetRunAuthFailed(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // A genuine auth failure (permission denied) is validated before the run is - // fetched, so GetRun is never reached and nothing is rendered. + // GetRun's 403 is confirmed as a credential failure by the Me() probe, so + // nothing is rendered. + m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) cmd := withOutput(newGetCommand(), flags.OutputText) @@ -104,6 +106,7 @@ func TestGetRunAuthTransient(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) // A transient failure at the auth probe must not be misreported as an auth // error; it surfaces as a retryable internal error instead. + m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrUnauthenticated) m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, errors.New("connection reset")) ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) cmd := withOutput(newGetCommand(), flags.OutputText) @@ -115,6 +118,22 @@ func TestGetRunAuthTransient(t *testing.T) { assert.NotContains(t, err.Error(), "authentication was not successful") } +func TestGetRunDeniedWithValidCredentials(t *testing.T) { + m := mocks.NewMockWorkspaceClient(t) + // A 403 on the run with credentials the Me() probe accepts is missing access + // to the run, not a failed login. + m.GetMockJobsAPI().EXPECT().GetRun(mock.Anything, mock.Anything).Return(nil, apierr.ErrPermissionDenied) + m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "u@example.com"}, nil) + ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), m.WorkspaceClient) + cmd := withOutput(newGetCommand(), flags.OutputText) + cmd.SetContext(ctx) + + err := cmd.RunE(cmd, []string{"5"}) + require.Error(t, err) + assert.Contains(t, err.Error(), "failed to get status for run 5") + assert.NotContains(t, err.Error(), "authentication was not successful") +} + func TestAuthError(t *testing.T) { ctx := cmdio.MockDiscard(t.Context()) cmd := withOutput(newGetCommand(), flags.OutputText) diff --git a/cmd/air/logs.go b/cmd/air/logs.go index 9d337dd8f18..8589e5d3754 100644 --- a/cmd/air/logs.go +++ b/cmd/air/logs.go @@ -13,7 +13,6 @@ import ( "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/spf13/cobra" ) @@ -145,14 +144,11 @@ JOB_RUN_ID is the job run ID returned by "databricks air run".`, func runLogs(ctx context.Context, cmd *cobra.Command, req logRequest) error { w := cmdctx.WorkspaceClient(ctx) - // Validate credentials server-side before fetching (MustWorkspaceClient only - // attaches them), so a bad token fails clearly here. - if _, err := w.CurrentUser.Me(ctx, iam.MeRequest{}); err != nil { - return authError(ctx, cmd, err) - } - run, err := w.Jobs.GetRun(ctx, jobs.GetRunRequest{RunId: req.runID}) if err != nil { + if credErr := credentialError(ctx, w, err); credErr != nil { + return authError(ctx, cmd, credErr) + } if errors.Is(err, apierr.ErrResourceDoesNotExist) { return renderError(ctx, cmd, "NOT_FOUND", "NOT_FOUND", false, fmt.Errorf("run %d not found: check the run ID and that it is a job run ID", req.runID)) diff --git a/cmd/air/logs_test.go b/cmd/air/logs_test.go index e5b5cbc26c7..485a43e8b7f 100644 --- a/cmd/air/logs_test.go +++ b/cmd/air/logs_test.go @@ -215,8 +215,9 @@ func TestLogsSkipsNodeValidationWhenNodeCountCannotBeResolved(t *testing.T) { } } -// completedRunLogsServer serves the auth probe, a terminal runs/get, and a -// single page of Bricklens logs (newest-first, as the tail fetch requests). +// completedRunLogsServer serves a terminal runs/get and a single page of +// Bricklens logs (newest-first, as the tail fetch requests). A successful fetch +// never needs the Me() credential probe, so the server fails the test on one. func completedRunLogsServer(t *testing.T) *httptest.Server { t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -235,9 +236,12 @@ func completedRunLogsServer(t *testing.T) *httptest.Server { {"time_unix_nano": 2000000000, "body": "line two", "node_index": 0}, {"time_unix_nano": 1000000000, "body": "line one", "node_index": 0} ]}`)) + case strings.HasSuffix(r.URL.Path, "/scim/v2/Me"): + t.Errorf("unexpected Me() probe on a successful logs fetch") + w.WriteHeader(http.StatusInternalServerError) default: - // Me() probe and SDK config discovery. - _, _ = w.Write([]byte(`{"userName":"u@example.com"}`)) + // SDK config discovery. + _, _ = w.Write([]byte(`{}`)) } })) t.Cleanup(srv.Close) diff --git a/cmd/air/run_watch_test.go b/cmd/air/run_watch_test.go index 52a859de21b..2a6d0378302 100644 --- a/cmd/air/run_watch_test.go +++ b/cmd/air/run_watch_test.go @@ -18,7 +18,7 @@ import ( "github.com/stretchr/testify/require" ) -// watchServer serves submit, the auth probe, a terminal runs/get, and a single +// watchServer serves submit, the user lookup, a terminal runs/get, and a single // page of Bricklens logs — everything `air run --watch` touches after submit. // resultState is the terminal result the run reports (e.g. SUCCESS, FAILED). func watchServer(t *testing.T, resultState string) *httptest.Server { @@ -42,7 +42,7 @@ func watchServer(t *testing.T, resultState string) *httptest.Server { {"time_unix_nano": 1700000001000000000, "body": "step 1", "node_index": 0} ]}`)) default: - // Me() probe, workspace-id, SDK config discovery. + // Me() user lookup, workspace-id, SDK config discovery. _, _ = w.Write([]byte(`{"userName": "u@example.com", "workspace_id": 1}`)) } })) From 999a91b8515f510003f1cf1884df0eee9624dd58 Mon Sep 17 00:00:00 2001 From: Steven Chen Date: Fri, 13 Nov 2026 19:28:18 +0000 Subject: [PATCH 2/3] Add changelog fragment for #7613 Co-authored-by: Isaac --- .nextchanges/air/7613.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 .nextchanges/air/7613.md diff --git a/.nextchanges/air/7613.md b/.nextchanges/air/7613.md new file mode 100644 index 00000000000..37258d7f2f4 --- /dev/null +++ b/.nextchanges/air/7613.md @@ -0,0 +1 @@ +* `air logs`, `air get`, and `air run --watch` no longer make an extra credential-check request before fetching the run. ([#7613](https://github.com/databricks/cli/pull/7613)) From 310cc328063e5ec064c56ecf1f04fe037b3c3280 Mon Sep 17 00:00:00 2001 From: Steven Chen Date: Fri, 13 Nov 2026 19:42:02 +0000 Subject: [PATCH 3/3] air get: reuse the configured workspace ID for the run URL `air get` called CurrentWorkspaceID, a second /Me request, to build the run URL's ?o= hint. Use auth.ResolveWorkspaceID, as `air run` does, so the ID comes from the profile, environment, or host metadata and /Me is only called when none is set. A configured ID that is not numeric is now reported as an invalid argument. Co-authored-by: Isaac --- .nextchanges/air/7613.md | 2 +- cmd/air/get.go | 9 ++++- cmd/air/get_test.go | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/.nextchanges/air/7613.md b/.nextchanges/air/7613.md index 37258d7f2f4..842d4e43f6a 100644 --- a/.nextchanges/air/7613.md +++ b/.nextchanges/air/7613.md @@ -1 +1 @@ -* `air logs`, `air get`, and `air run --watch` no longer make an extra credential-check request before fetching the run. ([#7613](https://github.com/databricks/cli/pull/7613)) +* `air logs`, `air get`, and `air run --watch` no longer make an extra credential-check request before fetching the run, and `air get` reuses the configured workspace ID instead of looking it up. ([#7613](https://github.com/databricks/cli/pull/7613)) diff --git a/cmd/air/get.go b/cmd/air/get.go index 10395610bc9..bac778ded51 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go" @@ -166,11 +167,17 @@ JOB_RUN_ID is the job run ID returned by "databricks air run".`, fmt.Errorf("failed to get status for run %d: %w", runID, err)) } - workspaceID, err := w.CurrentWorkspaceID(ctx) + // Prefer the configured workspace ID; only look it up when it is unset. + id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { return renderError(ctx, cmd, "INTERNAL_ERROR", "TRANSIENT", true, fmt.Errorf("failed to get workspace id for run %d: %w", runID, err)) } + workspaceID, err := strconv.ParseInt(id, 10, 64) + if err != nil { + return renderError(ctx, cmd, "INVALID_ARGS", "PERMANENT", false, + fmt.Errorf("configured workspace_id %q is not a numeric workspace ID", id)) + } data := buildGetData(run) data.DashboardURL = dashboardURL(w.Config.Host, runID, workspaceID) diff --git a/cmd/air/get_test.go b/cmd/air/get_test.go index b3bf076a66a..c6e984c3b44 100644 --- a/cmd/air/get_test.go +++ b/cmd/air/get_test.go @@ -6,6 +6,8 @@ import ( "errors" "net/http" "net/http/httptest" + "strings" + "sync/atomic" "testing" "text/template" @@ -13,6 +15,7 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" "github.com/databricks/databricks-sdk-go/experimental/mocks" @@ -175,6 +178,75 @@ func TestGetRunNotFoundJSON(t *testing.T) { assert.Equal(t, jsonError{Code: "NOT_FOUND", Kind: "NOT_FOUND", Message: "run 5 not found: check the run ID and that it is a job run ID"}, got.Error) } +// getRunServer serves a terminal runs/get and a /Me that reports workspace +// 12345 in the X-Databricks-Org-Id header. *meCalls counts /Me requests. +func getRunServer(t *testing.T, meCalls *atomic.Int32) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.URL.Path == "/api/2.2/jobs/runs/get": + _, _ = w.Write([]byte(`{"run_id": 5, "state": {"life_cycle_state": "TERMINATED", "result_state": "SUCCESS"}}`)) + case strings.HasSuffix(r.URL.Path, "/scim/v2/Me"): + meCalls.Add(1) + w.Header().Set("X-Databricks-Org-Id", "12345") + _, _ = w.Write([]byte(`{"userName": "u@example.com"}`)) + default: + // SDK config discovery. + _, _ = w.Write([]byte(`{}`)) + } + })) + t.Cleanup(srv.Close) + return srv +} + +func runGetJSON(t *testing.T, w *databricks.WorkspaceClient) (string, error) { + t.Helper() + var buf bytes.Buffer + ctx := cmdctx.SetWorkspaceClient(t.Context(), w) + ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputJSON, nil, &buf, &buf, "", "")) + cmd := withOutput(newGetCommand(), flags.OutputJSON) + cmd.SetContext(ctx) + err := cmd.RunE(cmd, []string{"5"}) + return buf.String(), err +} + +func TestGetRunURLUsesConfiguredWorkspaceID(t *testing.T) { + var meCalls atomic.Int32 + srv := getRunServer(t, &meCalls) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: srv.URL, Token: "token", WorkspaceID: "67890"}) + require.NoError(t, err) + + out, err := runGetJSON(t, w) + require.NoError(t, err) + assert.Contains(t, out, "/jobs/runs/5?o=67890") + assert.Zero(t, meCalls.Load(), "a configured workspace ID needs no /Me lookup") +} + +func TestGetRunURLLooksUpUnsetWorkspaceID(t *testing.T) { + // The SDK also reads the workspace ID from the environment. + t.Setenv("DATABRICKS_WORKSPACE_ID", "") + var meCalls atomic.Int32 + srv := getRunServer(t, &meCalls) + + out, err := runGetJSON(t, newTestWorkspaceClient(t, srv.URL)) + require.NoError(t, err) + assert.Contains(t, out, "/jobs/runs/5?o=12345") + assert.Equal(t, int32(1), meCalls.Load()) +} + +func TestGetRunRejectsNonNumericWorkspaceID(t *testing.T) { + var meCalls atomic.Int32 + srv := getRunServer(t, &meCalls) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: srv.URL, Token: "token", WorkspaceID: "not-a-number"}) + require.NoError(t, err) + + out, err := runGetJSON(t, w) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + var got errorEnvelope + require.NoError(t, json.Unmarshal([]byte(out), &got)) + assert.Equal(t, jsonError{Code: "INVALID_ARGS", Kind: "PERMANENT", Message: `configured workspace_id "not-a-number" is not a numeric workspace ID`}, got.Error) +} + func TestGetTemplateSweep(t *testing.T) { out := renderGet(t, getData{ RunID: "456",