From 5d8950f02c98c07fed0e4491ebbe614e267cf74a Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:51 +0000 Subject: [PATCH 01/23] testserver: serve identity/me and group lookups from the workspace fake Tests and acceptance runs need identity/me, the transitive parent groups of the current user, the account group list and group lookups by ID. The fake describes the caller as a service principal for SP tokens and as a user otherwise. Group membership is a set that tests join explicitly (Server.JoinGroup), the parent-groups endpoint lists only those groups, and the group list is paged on page_token, as the account endpoint is. Co-authored-by: Isaac --- libs/testserver/fake_workspace.go | 31 +++++++++++- libs/testserver/groups.go | 83 +++++++++++++++++++++++++++++++ libs/testserver/handlers.go | 17 +++++++ 3 files changed, 130 insertions(+), 1 deletion(-) diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 5e8068b6d02..e96b9874409 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -78,10 +78,16 @@ func isGuestToken(token string) bool { return strings.HasPrefix(token, GuestServicePrincipalTokenPrefix) } +// isServicePrincipalToken reports whether a token authenticates a service principal +// (primary or guest). +func isServicePrincipalToken(token string) bool { + return strings.HasPrefix(token, ServicePrincipalTokenPrefix) || isGuestToken(token) +} + // userForToken returns the identity behind a token: any service-principal token // (primary or guest) is the SP, otherwise the user. func userForToken(token string) iam.User { - if strings.HasPrefix(token, ServicePrincipalTokenPrefix) || isGuestToken(token) { + if isServicePrincipalToken(token) { return TestUserSP } return TestUser @@ -97,6 +103,25 @@ func (s *FakeWorkspace) MeUser(token string) iam.User { return user } +// IdentityMe returns the GET /api/2.0/identity/me body for a token. It describes the +// same identity as MeUser, as a user or a service principal. +func (s *FakeWorkspace) IdentityMe(token string) any { + me := s.MeUser(token) + if isServicePrincipalToken(token) { + return map[string]any{"service_principal": map[string]any{ + "service_principal_id": me.Id, + "application_id": me.UserName, + "display_name": me.DisplayName, + "account_sp_status": "ACTIVE", + }} + } + return map[string]any{"user": map[string]any{ + "user_id": me.Id, + "username": me.UserName, + "account_user_status": "ACTIVE", + }} +} + var ( idMutex sync.Mutex lastNowNano int64 @@ -280,6 +305,9 @@ type FakeWorkspace struct { Permissions map[string]iam.ObjectPermissions Groups map[string]iam.Group + // groupMembers holds the IDs of the groups the caller belongs to; the parent-groups + // endpoint lists exactly these. + groupMembers map[string]bool Repos map[string]workspace.RepoInfo @@ -570,6 +598,7 @@ func NewFakeWorkspace(url, token string) *FakeWorkspace { WorkspaceSettings: map[string]settingsv2.Setting{}, Permissions: map[string]iam.ObjectPermissions{}, Groups: map[string]iam.Group{}, + groupMembers: map[string]bool{}, DatabaseInstances: map[string]database.DatabaseInstance{}, DatabaseCatalogs: map[string]database.DatabaseCatalog{}, SyncedDatabaseTables: map[string]database.SyncedDatabaseTable{}, diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index 5074708657e..197730893b1 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -3,6 +3,9 @@ package testserver import ( "encoding/json" "fmt" + "maps" + "net/http" + "slices" "strconv" "github.com/databricks/databricks-sdk-go/service/iam" @@ -37,3 +40,83 @@ func (s *FakeWorkspace) GroupsCreate(req Request) Response { Body: group, } } + +// IdentityMeTransitiveParentGroups answers GET /api/2.0/identity/me/transitive-parent-groups +// with the groups the caller belongs to. +func (s *FakeWorkspace) IdentityMeTransitiveParentGroups() any { + defer s.LockUnlock()() + + ids := slices.Sorted(maps.Keys(s.groupMembers)) + parents := make([]map[string]any, 0, len(ids)) + for _, id := range ids { + parents = append(parents, map[string]any{"group_id": id}) + } + return map[string]any{"me_transitive_parent_groups": parents} +} + +// AddGroups creates a group for each name in member and each name in nonMember in the +// workspace behind token, and makes that identity a member of the groups in member. +func (s *Server) AddGroups(token string, member, nonMember []string) { + ws := s.getWorkspaceForToken(token) + defer ws.LockUnlock()() + for _, name := range slices.Concat(member, nonMember) { + id := strconv.FormatInt(nextID(), 10) + ws.Groups[id] = iam.Group{Id: id, DisplayName: name} + } + for _, name := range member { + for id, group := range ws.Groups { + if group.DisplayName == name { + ws.groupMembers[id] = true + } + } + } +} + +// JoinGroup makes the identity behind token a member of the group with groupID. +// The ID need not name an existing group, which lets tests make a group lookup fail. +func (s *Server) JoinGroup(token, groupID string) { + ws := s.getWorkspaceForToken(token) + defer ws.LockUnlock()() + ws.groupMembers[groupID] = true +} + +// IdentityGroup answers GET /api/2.0/identity/groups/{group_id}. +func (s *FakeWorkspace) IdentityGroup(groupID string) any { + defer s.LockUnlock()() + + group, ok := s.Groups[groupID] + if !ok { + return Response{ + StatusCode: http.StatusNotFound, + Body: map[string]any{"error_code": "RESOURCE_DOES_NOT_EXIST", "message": "Group " + groupID + " does not exist."}, + } + } + return map[string]any{ + "name": "groups/" + groupID, + "group_id": group.Id, + "group_name": group.DisplayName, + } +} + +// IdentityGroups answers GET /api/2.0/identity/groups, one page of all groups in +// the account. page_token is the offset of the next page, as the server's opaque token. +func (s *FakeWorkspace) IdentityGroups(pageSize int, pageToken string) any { + defer s.LockUnlock()() + + if pageSize <= 0 { + pageSize = 1000 + } + ids := slices.Sorted(maps.Keys(s.Groups)) + start, _ := strconv.Atoi(pageToken) + end := min(start+pageSize, len(ids)) + groups := make([]map[string]any, 0, end-start) + for _, id := range ids[start:end] { + group := s.Groups[id] + groups = append(groups, map[string]any{"group_id": group.Id, "group_name": group.DisplayName}) + } + resp := map[string]any{"groups": groups} + if end < len(ids) { + resp["next_page_token"] = strconv.Itoa(end) + } + return resp +} diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 20aac63e2b7..61b4deeb781 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -251,6 +251,23 @@ func AddDefaultHandlers(server *Server) { } }) + server.Handle("GET", "/api/2.0/identity/me", func(req Request) any { + return req.Workspace.IdentityMe(req.Token) + }) + + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req Request) any { + return req.Workspace.IdentityMeTransitiveParentGroups() + }) + + server.Handle("GET", "/api/2.0/identity/groups", func(req Request) any { + pageSize, _ := strconv.Atoi(req.URL.Query().Get("page_size")) + return req.Workspace.IdentityGroups(pageSize, req.URL.Query().Get("page_token")) + }) + + server.Handle("GET", "/api/2.0/identity/groups/{group_id}", func(req Request) any { + return req.Workspace.IdentityGroup(req.Vars["group_id"]) + }) + server.Handle("GET", "/api/2.0/workspace/get-status", func(req Request) any { path := req.URL.Query().Get("path") returnGitInfo := req.URL.Query().Get("return_git_info") == "true" From 50c80e7f399cdec5cc1b911474711f0b5438847b Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:53 +0000 Subject: [PATCH 02/23] iamutil: read the current user from identity/me Callers used SCIM Me, which does not resolve an assumed group. GetCurrentUser now calls GET /api/2.0/identity/me and returns its error when it fails. It retries HTTP 500 responses for up to 30 seconds, as the SCIM call did before; GetCurrentUserOnce makes a single attempt for probes that should fail fast. The principal is mapped onto the iam.User fields that callers already read. identity/me returns no groups. CurrentUserGroups lists the caller's parent group IDs from identity/me/transitive-parent-groups, reads all account groups in one paged list, and maps the IDs to names. That is one list call per 1000 account groups. A group ID missing from the account list is skipped, and a failed call fails the lookup so no partial list is taken as complete. Co-authored-by: Isaac --- libs/iamutil/current_user.go | 162 ++++++++++++++++++-- libs/iamutil/current_user_test.go | 243 ++++++++++++++++++++++++++++-- 2 files changed, 378 insertions(+), 27 deletions(-) diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index 2908c8e19a2..e3a6c9844f8 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -4,29 +4,45 @@ import ( "context" "errors" "net/http" + "slices" + "strings" "time" + "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" + databricksclient "github.com/databricks/databricks-sdk-go/client" "github.com/databricks/databricks-sdk-go/retries" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/databricks-sdk-go/service/iamv2" ) -// meRetryTimeout bounds how long GetCurrentUser retries transient 500s. -const meRetryTimeout = 30 * time.Second +const ( + // meRetryTimeout bounds how long GetCurrentUser retries transient 500s. + meRetryTimeout = 30 * time.Second -// GetCurrentUser returns the current user, retrying on HTTP 500 responses. + identityMePath = "/api/2.0/identity/me" + identityMeTransitiveParentGroupsPath = "/api/2.0/identity/me/transitive-parent-groups" +) + +// identityMe is the principal returned by GET /api/2.0/identity/me. Exactly one +// of the three fields is set. Groups and parent groups are not returned inline. +type identityMe struct { + User *iamv2.User `json:"user,omitempty"` + Group *iamv2.Group `json:"group,omitempty"` + ServicePrincipal *iamv2.ServicePrincipal `json:"service_principal,omitempty"` +} + +// GetCurrentUser returns the authorizing principal of the current request as an +// iam.User, read from GET /api/2.0/identity/me, which also resolves an assumed group. +// Groups, Roles and Entitlements are not set; CurrentUserGroups reads the groups. // -// CurrentUser.Me is a read-only endpoint, so retrying any 500 is safe. The -// Databricks SCIM API intermittently returns "500 TEMPORARILY_UNAVAILABLE" (a -// condition it otherwise serves as 503), and the SDK retries 503 but not 500 — -// see the "some API's recommend retries on HTTP 500, but we'll add that later" -// comment on (*apierr.APIError).IsRetriable in databricks-sdk-go/apierr. This -// call runs at the start of every bundle command, so an unretried 500 fails the -// whole command; we close that gap here. +// The call retries HTTP 500 responses for up to meRetryTimeout. It runs at the +// start of most CLI commands, and an unretried 500 would fail the whole command. func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { return retries.Poll(ctx, meRetryTimeout, func() (*iam.User, *retries.Err) { - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := getIdentityMe(ctx, w) if err == nil { return user, nil } @@ -36,3 +52,127 @@ func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.Us return nil, retries.Halt(err) }) } + +// GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should +// fail fast rather than wait out transient 500s. +func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + return getIdentityMe(ctx, w) +} + +// identityGet sends a GET to path with the workspace's auth headers and decodes the +// response into out. +func identityGet(ctx context.Context, w *databricks.WorkspaceClient, path string, query map[string]any, out any) error { + c, err := databricksclient.New(w.Config) + if err != nil { + return err + } + return c.Do(ctx, http.MethodGet, path, auth.WorkspaceIDHeaders(w.Config), query, nil, out) +} + +func getIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + var me identityMe + if err := identityGet(ctx, w, identityMePath, nil, &me); err != nil { + return nil, err + } + return meToUser(&me) +} + +// meToUser maps a principal from identity/me onto an iam.User, setting Id, UserName, +// DisplayName, Active, ExternalId and Emails. A service principal's UserName is its +// application ID, not its name. +func meToUser(me *identityMe) (*iam.User, error) { + switch { + case me.User != nil: + u := me.User + return &iam.User{ + Id: u.UserId, + UserName: u.Username, + DisplayName: strings.TrimSpace(u.FullName.GivenName + " " + u.FullName.FamilyName), + Active: u.AccountUserStatus == iamv2.StateActive, + ExternalId: u.ExternalId, + Emails: []iam.ComplexValue{{Value: u.Username, Primary: true}}, + }, nil + case me.ServicePrincipal != nil: + sp := me.ServicePrincipal + return &iam.User{ + Id: sp.ServicePrincipalId, + UserName: sp.ApplicationId, + DisplayName: sp.DisplayName, + Active: sp.AccountSpStatus == iamv2.StateActive, + ExternalId: sp.ExternalId, + }, nil + case me.Group != nil: + g := me.Group + return &iam.User{ + Id: g.GroupId, + UserName: g.GroupName, + DisplayName: g.GroupName, + }, nil + default: + return nil, errors.New("identity/me returned no principal") + } +} + +// listCurrentUserParentGroupIDs returns the IDs of the groups the current principal +// belongs to, directly or through parent groups, following every page. The keys follow +// ListMeTransitiveParentGroupsResponse in the IAM v2 proto, not the generic SDK response, +// which decodes a different key. +func listCurrentUserParentGroupIDs(ctx context.Context, w *databricks.WorkspaceClient) ([]string, error) { + var ids []string + pageToken := "" + for { + query := map[string]any{} + if pageToken != "" { + query["page_token"] = pageToken + } + var page struct { + Groups []struct { + GroupId string `json:"group_id"` + } `json:"me_transitive_parent_groups"` + NextPageToken string `json:"next_page_token,omitempty"` + } + if err := identityGet(ctx, w, identityMeTransitiveParentGroupsPath, query, &page); err != nil { + return nil, err + } + for _, g := range page.Groups { + ids = append(ids, g.GroupId) + } + if page.NextPageToken == "" { + return ids, nil + } + pageToken = page.NextPageToken + } +} + +// CurrentUserGroups returns the groups the current principal belongs to, directly or +// through parent groups, as iam.ComplexValue entries with the group ID as Value and the +// group name as Display, sorted by Display. Names come from one list of all account +// groups, so the cost is one list call per 1000 groups rather than one lookup per group. +// It fails when either call fails, so a caller never sees a partial list as complete. +func CurrentUserGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]iam.ComplexValue, error) { + ids, err := listCurrentUserParentGroupIDs(ctx, w) + if err != nil { + return nil, err + } + all, err := w.WorkspaceIamV2.ListGroupsProxyAll(ctx, iamv2.ListGroupsProxyRequest{}) + if err != nil { + return nil, err + } + names := make(map[string]string, len(all)) + for _, g := range all { + names[g.GroupId] = g.GroupName + } + var groups []iam.ComplexValue + for _, id := range ids { + name, ok := names[id] + if !ok { + log.Debugf(ctx, "Group %s of the current user is not in the account group list; skipping it", id) + continue + } + groups = append(groups, iam.ComplexValue{Value: id, Display: name}) + } + slices.SortFunc(groups, func(a, b iam.ComplexValue) int { + return strings.Compare(a.Display, b.Display) + }) + return groups, nil +} diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index decf8eef936..4c3ee6af913 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -2,38 +2,249 @@ package iamutil import ( "net/http" + "sync/atomic" "testing" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" - "github.com/databricks/databricks-sdk-go/experimental/mocks" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/databricks-sdk-go/service/iamv2" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) +// newIdentityMeWorkspace returns a workspace client whose identity/me is answered by handler. +func newIdentityMeWorkspace(t *testing.T, handler func(req testserver.Request) testserver.Response) *databricks.WorkspaceClient { + t.Helper() + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMePath, func(req testserver.Request) any { + return handler(req) + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + return w +} + +func TestMeToUser(t *testing.T) { + tests := []struct { + name string + me identityMe + want *iam.User + wantErr bool + }{ + { + name: "user", + me: identityMe{User: &iamv2.User{ + UserId: "1000012345", + Username: "alice@example.com", + FullName: iamv2.UserFullName{GivenName: "Alice", FamilyName: "Smith"}, + AccountUserStatus: iamv2.StateActive, + ExternalId: "ext-1", + }}, + want: &iam.User{ + Id: "1000012345", + UserName: "alice@example.com", + DisplayName: "Alice Smith", + Active: true, + ExternalId: "ext-1", + Emails: []iam.ComplexValue{{Value: "alice@example.com", Primary: true}}, + }, + }, + { + name: "user without full name and inactive", + me: identityMe{User: &iamv2.User{ + UserId: "1000012345", + Username: "alice@example.com", + AccountUserStatus: iamv2.StateInactive, + }}, + want: &iam.User{ + Id: "1000012345", + UserName: "alice@example.com", + Emails: []iam.ComplexValue{{Value: "alice@example.com", Primary: true}}, + }, + }, + { + name: "service principal reports application id as user name", + me: identityMe{ServicePrincipal: &iamv2.ServicePrincipal{ + ServicePrincipalId: "2000022222", + ApplicationId: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee", + DisplayName: "deploy-sp", + AccountSpStatus: iamv2.StateActive, + }}, + want: &iam.User{ + Id: "2000022222", + UserName: "aaaaaaaa-bbbb-4ccc-dddd-eeeeeeeeeeee", + DisplayName: "deploy-sp", + Active: true, + }, + }, + { + name: "assumed group", + me: identityMe{Group: &iamv2.Group{GroupId: "3000033333", GroupName: "data-engineers"}}, + want: &iam.User{ + Id: "3000033333", + UserName: "data-engineers", + DisplayName: "data-engineers", + }, + }, + { + name: "no principal", + me: identityMe{}, + wantErr: true, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got, err := meToUser(&tc.me) + if tc.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + func TestGetCurrentUserRetriesOn500(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - me := m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything) - me.Return(nil, &apierr.APIError{StatusCode: http.StatusInternalServerError, ErrorCode: "TEMPORARILY_UNAVAILABLE"}).Once() - me.Return(&iam.User{UserName: "test-user"}, nil).Once() + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + if calls.Add(1) == 1 { + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + } + return testserver.Response{Body: map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}} + }) - user, err := GetCurrentUser(t.Context(), m.WorkspaceClient) + user, err := GetCurrentUser(t.Context(), w) require.NoError(t, err) assert.Equal(t, "test-user", user.UserName) + assert.Equal(t, int32(2), calls.Load()) } func TestGetCurrentUserDoesNotRetryOn403(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - // A non-500 error must fail immediately, without a retry (Once enforces it). - m.GetMockCurrentUserAPI().EXPECT(). - Me(mock.Anything, mock.Anything). - Return(nil, &apierr.APIError{StatusCode: http.StatusForbidden, Message: "denied"}). - Once() - - _, err := GetCurrentUser(t.Context(), m.WorkspaceClient) - require.Error(t, err) + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + calls.Add(1) + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + + _, err := GetCurrentUser(t.Context(), w) var apiErr *apierr.APIError require.ErrorAs(t, err, &apiErr) assert.Equal(t, http.StatusForbidden, apiErr.StatusCode) + // A non-500 error must fail immediately, without a retry. + assert.Equal(t, int32(1), calls.Load()) +} + +func TestListCurrentUserParentGroupIDsFollowsPages(t *testing.T) { + var pages []string + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(req testserver.Request) any { + token := req.URL.Query().Get("page_token") + pages = append(pages, token) + // The fixture bodies use the proto keys; a wrong key decodes to no groups. + if token == "" { + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "11"}, {"group_id": "12"}}, + "next_page_token": "page2", + } + } + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "13"}}, + } + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + ids, err := listCurrentUserParentGroupIDs(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []string{"11", "12", "13"}, ids) + assert.Equal(t, []string{"", "page2"}, pages) +} + +func TestCurrentUserGroupsResolvesNamesSorted(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + server.AddGroups("test-token", []string{"zeta", "alpha"}, nil) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + require.Len(t, groups, 2) + assert.Equal(t, "alpha", groups[0].Display) + assert.Equal(t, "zeta", groups[1].Display) + assert.NotEmpty(t, groups[0].Value) +} + +// A parent ID that is missing from the account group list is skipped, not reported as +// an error: the other memberships are still known. +func TestCurrentUserGroupsSkipsParentIDMissingFromAccountList(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + server.AddGroups("test-token", []string{"testgroup"}, nil) + server.JoinGroup("test-token", "missing-group") + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + require.Len(t, groups, 1) + assert.Equal(t, "testgroup", groups[0].Display) + assert.Equal(t, "testgroup", groups[0].Display) +} + +// The account group list is paged; every page is read, and names are matched by ID +// across pages. +func TestCurrentUserGroupsReadsEveryPageOfGroupList(t *testing.T) { + var pages []string + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11"}, {"group_id": "12"}, {"group_id": "13"}}} + }) + server.Handle("GET", "/api/2.0/identity/groups", func(req testserver.Request) any { + token := req.URL.Query().Get("page_token") + pages = append(pages, token) + if token == "" { + return map[string]any{ + "groups": []map[string]any{{"group_id": "11", "group_name": "b"}, {"group_id": "99", "group_name": "unrelated"}}, + "next_page_token": "page2", + } + } + return map[string]any{"groups": []map[string]any{{"group_id": "12", "group_name": "a"}, {"group_id": "13", "group_name": "c"}}} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []iam.ComplexValue{ + {Value: "12", Display: "a"}, + {Value: "11", Display: "b"}, + {Value: "13", Display: "c"}, + }, groups) + assert.Equal(t, []string{"", "page2"}, pages) +} + +// A failed account group list fails the lookup, so no partial list is returned as the +// complete membership. +func TestCurrentUserGroupsFailsWhenGroupListFails(t *testing.T) { + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11"}}} + }) + server.Handle("GET", "/api/2.0/identity/groups", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.ErrorContains(t, err, "denied") + assert.Nil(t, groups) } From 3750e339b69f8eaca811dc3c350cf824a40b4ec9 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:55 +0000 Subject: [PATCH 03/23] cli: read the current user through iamutil.GetCurrentUser Every non-test call site that needed the current user now calls the helper, so they all read identity/me and resolve an assumed group the same way. The account-config probe in auth describe uses GetCurrentUserOnce, so a 500 fails at once instead of waiting out the retry window. Co-authored-by: Isaac --- cmd/air/cancel.go | 4 ++-- cmd/air/get.go | 4 ++-- cmd/air/runlaunch.go | 5 ++--- cmd/apps/import.go | 4 ++-- cmd/apps/run_local.go | 4 ++-- cmd/auth/describe.go | 6 ++++-- cmd/auth/profiles.go | 4 ++-- cmd/psql/psql_autoscaling.go | 4 ++-- cmd/psql/psql_provisioned.go | 4 ++-- cmd/sync/completion.go | 4 ++-- experimental/postgres/cmd/targeting.go | 6 +++--- experimental/ssh/internal/client/client.go | 4 ++-- experimental/ssh/internal/keys/secrets.go | 4 ++-- experimental/ssh/internal/server/fuse.go | 4 ++-- experimental/ssh/internal/workspace/workspace.go | 4 ++-- libs/databrickscfg/cfgpickers/clusters.go | 3 ++- libs/sync/path.go | 3 ++- libs/template/helpers.go | 6 +++--- 18 files changed, 40 insertions(+), 37 deletions(-) diff --git a/cmd/air/cancel.go b/cmd/air/cancel.go index 0ca93c793cd..47dce8a5f39 100644 --- a/cmd/air/cancel.go +++ b/cmd/air/cancel.go @@ -13,9 +13,9 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "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" ) @@ -86,7 +86,7 @@ all active runs submitted by the current user. if all { data.All = true - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return renderError(ctx, cmd, "INTERNAL_ERROR", "TRANSIENT", true, fmt.Errorf("failed to resolve current user: %w", err)) diff --git a/cmd/air/get.go b/cmd/air/get.go index 7a4de046fef..3f78b97477a 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -13,11 +13,11 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "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" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/spf13/cobra" ) @@ -151,7 +151,7 @@ func credentialError(ctx context.Context, w *databricks.WorkspaceClient, err err if !errors.Is(err, apierr.ErrUnauthenticated) && !errors.Is(err, apierr.ErrPermissionDenied) { return nil } - _, meErr := w.CurrentUser.Me(ctx, iam.MeRequest{}) + _, meErr := iamutil.GetCurrentUser(ctx, w) return meErr } diff --git a/cmd/air/runlaunch.go b/cmd/air/runlaunch.go index a888d80d85d..a2288d21e16 100644 --- a/cmd/air/runlaunch.go +++ b/cmd/air/runlaunch.go @@ -9,9 +9,9 @@ import ( "uuid" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "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/workspace" ) @@ -21,8 +21,7 @@ const userWorkspaceDirEnv = "DATABRICKS_INTERNAL_USER_WORKSPACE_DIR" // currentUserEmail returns the authenticated user's email (works for any domain). func currentUserEmail(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - // Only userName is needed; the full response also resolves groups and entitlements. - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{Attributes: "userName"}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return "", fmt.Errorf("failed to resolve current user: %w", err) } diff --git a/cmd/apps/import.go b/cmd/apps/import.go index 7df83287871..7455163ca6a 100644 --- a/cmd/apps/import.go +++ b/cmd/apps/import.go @@ -30,11 +30,11 @@ import ( "github.com/databricks/cli/libs/dyn/convert" "github.com/databricks/cli/libs/dyn/yamlsaver" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/logdiag" "github.com/databricks/cli/libs/textutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/apps" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -90,7 +90,7 @@ Examples: w := cmdctx.WorkspaceClient(ctx) // Get current user to filter apps - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/apps/run_local.go b/cmd/apps/run_local.go index d9671276f0f..10521b27734 100644 --- a/cmd/apps/run_local.go +++ b/cmd/apps/run_local.go @@ -17,8 +17,8 @@ import ( "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" ) @@ -117,7 +117,7 @@ func setupProxy(ctx context.Context, cmd *cobra.Command, config *runlocal.Config return err } - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return err } diff --git a/cmd/auth/describe.go b/cmd/auth/describe.go index a58e964de27..73e87af71a3 100644 --- a/cmd/auth/describe.go +++ b/cmd/auth/describe.go @@ -13,6 +13,7 @@ import ( "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" @@ -138,6 +139,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try // Workspaces.List can fail for reasons other than bad credentials // (e.g. it requires account admin), so verify against the workspace // endpoint with the same resolved config before reporting failure. + // Account-only hosts reject this probe. me, meErr := verifyWorkspaceAuth(ctx, a.Config) if meErr == nil { return successAuthStatus(ctx, cmd, a.Config, showSensitive, me.UserName, ""), nil @@ -146,7 +148,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try } w := cmdctx.WorkspaceClient(ctx) - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err == nil { return successAuthStatus(ctx, cmd, w.Config, showSensitive, me.UserName, ""), nil } @@ -174,7 +176,7 @@ func verifyWorkspaceAuth(ctx context.Context, cfg *config.Config) (*iam.User, er if err != nil { return nil, err } - return w.CurrentUser.Me(ctx, iam.MeRequest{}) + return iamutil.GetCurrentUserOnce(ctx, w) } // verifyAccountAuth checks whether cfg's credentials are accepted by the diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index e65b11371b7..95af06f7b36 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -13,10 +13,10 @@ import ( "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" "gopkg.in/ini.v1" ) @@ -110,7 +110,7 @@ func (c *profileMetadata) Load(ctx context.Context, configFilePath string, skipV if err != nil { return } - _, err = w.CurrentUser.Me(callCtx, iam.MeRequest{}) + _, err = iamutil.GetCurrentUser(callCtx, w) c.Host = cfg.Host c.AuthType = cfg.AuthType if err != nil { diff --git a/cmd/psql/psql_autoscaling.go b/cmd/psql/psql_autoscaling.go index 331e05b55b5..ca50bdba6aa 100644 --- a/cmd/psql/psql_autoscaling.go +++ b/cmd/psql/psql_autoscaling.go @@ -8,9 +8,9 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" libpsql "github.com/databricks/cli/libs/psql" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/postgres" ) @@ -38,7 +38,7 @@ func connectAutoscaling(ctx context.Context, projectID, branchID, endpointID str return err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/psql/psql_provisioned.go b/cmd/psql/psql_provisioned.go index 272dfd7d687..f3934e86157 100644 --- a/cmd/psql/psql_provisioned.go +++ b/cmd/psql/psql_provisioned.go @@ -8,10 +8,10 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" libpsql "github.com/databricks/cli/libs/psql" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/database" - "github.com/databricks/databricks-sdk-go/service/iam" ) // provisionedDefaultDatabase is the default database for Lakebase Provisioned instances. @@ -27,7 +27,7 @@ func connectProvisioned(ctx context.Context, instanceName string, retryConfig li return err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/sync/completion.go b/cmd/sync/completion.go index c82d85a1c38..9f79c0892f6 100644 --- a/cmd/sync/completion.go +++ b/cmd/sync/completion.go @@ -5,8 +5,8 @@ import ( "path" "strings" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -45,7 +45,7 @@ func completeRemotePath( wsc *databricks.WorkspaceClient, toComplete string, ) ([]string, cobra.ShellCompDirective) { - me, err := wsc.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, wsc) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/experimental/postgres/cmd/targeting.go b/experimental/postgres/cmd/targeting.go index cf1290ad578..59b3d95dcd3 100644 --- a/experimental/postgres/cmd/targeting.go +++ b/experimental/postgres/cmd/targeting.go @@ -7,9 +7,9 @@ import ( "github.com/databricks/cli/experimental/postgres/cmd/internal/target" "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/database" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/postgres" ) @@ -114,7 +114,7 @@ func resolveProvisioned(ctx context.Context, w *databricks.WorkspaceClient, inst return nil, fmt.Errorf("database instance %q is not ready for accepting connections (state: %s)", instance.Name, instance.State) } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } @@ -175,7 +175,7 @@ func resolveAutoscaling(ctx context.Context, w *databricks.WorkspaceClient, spec return nil, err } - user, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + user, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/client/client.go b/experimental/ssh/internal/client/client.go index 5afd212131f..f96c8ec8043 100644 --- a/experimental/ssh/internal/client/client.go +++ b/experimental/ssh/internal/client/client.go @@ -30,6 +30,7 @@ import ( "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" @@ -37,7 +38,6 @@ import ( "github.com/databricks/databricks-sdk-go/retries" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/environments" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/settingsv2" "github.com/databricks/databricks-sdk-go/service/workspace" @@ -598,7 +598,7 @@ func runIDE(ctx context.Context, client *databricks.WorkspaceClient, userName, k } // Get Databricks user name for the workspace path - currentUser, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + currentUser, err := iamutil.GetCurrentUser(ctx, client) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/keys/secrets.go b/experimental/ssh/internal/keys/secrets.go index 6409770a4d7..4a6ec325c11 100644 --- a/experimental/ssh/internal/keys/secrets.go +++ b/experimental/ssh/internal/keys/secrets.go @@ -6,15 +6,15 @@ import ( "errors" "fmt" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/workspace" ) // CreateKeysSecretScope creates or retrieves the secret scope for SSH keys. // sessionID is the unique identifier for the session (cluster ID for dedicated clusters, connection name for serverless). func CreateKeysSecretScope(ctx context.Context, client *databricks.WorkspaceClient, sessionID string) (string, error) { - me, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/server/fuse.go b/experimental/ssh/internal/server/fuse.go index 23366728ab0..90f7b19fe59 100644 --- a/experimental/ssh/internal/server/fuse.go +++ b/experimental/ssh/internal/server/fuse.go @@ -9,9 +9,9 @@ import ( "time" "github.com/databricks/cli/experimental/ssh/internal/fuse" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" ) func startFuseRegistration(ctx context.Context, client *databricks.WorkspaceClient, serverless bool) { @@ -44,7 +44,7 @@ func fuseUserInfo(ctx context.Context, client *databricks.WorkspaceClient) (user // User metadata is optional. Limit its lookup before publishing the server port. userCtx, cancel := context.WithTimeout(ctx, 2*time.Second) defer cancel() - me, err := client.CurrentUser.Me(userCtx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(userCtx, client) if err != nil { log.Debugf(ctx, "Registering filesystem credentials without user metadata: %v", err) return "", "" diff --git a/experimental/ssh/internal/workspace/workspace.go b/experimental/ssh/internal/workspace/workspace.go index a6a29ca7bb2..64e32e80c71 100644 --- a/experimental/ssh/internal/workspace/workspace.go +++ b/experimental/ssh/internal/workspace/workspace.go @@ -9,8 +9,8 @@ import ( "path/filepath" "github.com/databricks/cli/libs/filer" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" ) const metadataFileName = "metadata.json" @@ -31,7 +31,7 @@ type WorkspaceMetadata struct { // GetWorkspaceHomeDir returns the current user's workspace home folder. func GetWorkspaceHomeDir(ctx context.Context, client *databricks.WorkspaceClient) (string, error) { - me, err := client.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/libs/databrickscfg/cfgpickers/clusters.go b/libs/databrickscfg/cfgpickers/clusters.go index 0c752f3a4b1..d5b77691908 100644 --- a/libs/databrickscfg/cfgpickers/clusters.go +++ b/libs/databrickscfg/cfgpickers/clusters.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/iam" @@ -140,7 +141,7 @@ func loadInteractiveClusters(ctx context.Context, w *databricks.WorkspaceClient, if err != nil { return nil, fmt.Errorf("list clusters: %w", err) } - me, err := w.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err := iamutil.GetCurrentUser(ctx, w) if err != nil { return nil, fmt.Errorf("current user: %w", err) } diff --git a/libs/sync/path.go b/libs/sync/path.go index d85699cf55c..48a6ba580c5 100644 --- a/libs/sync/path.go +++ b/libs/sync/path.go @@ -6,6 +6,7 @@ import ( "path" "strings" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/retry" "github.com/databricks/databricks-sdk-go" @@ -41,7 +42,7 @@ func EnsureRemotePathIsUsable(ctx context.Context, wsc *databricks.WorkspaceClie // TODO: we should cache CurrentUser.Me at the SDK level // for now we let clients pass in any existing user they might already have if me == nil { - me, err = wsc.CurrentUser.Me(ctx, iam.MeRequest{}) + me, err = iamutil.GetCurrentUser(ctx, wsc) if err != nil { return err } diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 7330a129b41..e2db3ec4fea 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -172,7 +172,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "user_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetCurrentUser(ctx, w) if err != nil { return "", err } @@ -186,7 +186,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "short_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetCurrentUser(ctx, w) if err != nil { return "", err } @@ -217,7 +217,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { } if cachedUser == nil { var err error - cachedUser, err = w.CurrentUser.Me(ctx, iam.MeRequest{}) + cachedUser, err = iamutil.GetCurrentUser(ctx, w) if err != nil { return false, err } From 2506b1b68f677646f2d57dedc3e5afa529351b4c Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:56 +0000 Subject: [PATCH 04/23] bundle: resolve permission group membership through IAM-v2 APIs identity/me returns no groups, so PopulateCurrentUser reads the current user's groups with iamutil.CurrentUserGroups and sets current_user.groups, once per command. A failed read is a Warning and leaves groups empty. current_user.roles and current_user.entitlements are no longer set, as no IAM-v2 source provides them. Permission checks match a configured CAN_MANAGE group by name against the groups already on current_user, so they make no group requests. Production code no longer reads SCIM for group membership. Co-authored-by: Isaac --- .../config/mutator/populate_current_user.go | 23 +++- .../mutator/populate_current_user_test.go | 52 ++++++- bundle/permissions/permission_diagnostics.go | 25 ++-- .../permission_diagnostics_test.go | 129 ++++++++++++++++-- bundle/permissions/permission_report.go | 2 +- bundle/permissions/permission_report_test.go | 10 +- 6 files changed, 207 insertions(+), 34 deletions(-) diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index a00234912fa..61c8d58ab2b 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -2,6 +2,7 @@ package mutator import ( "context" + "fmt" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" @@ -37,14 +38,28 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. return diag.FromErr(err) } + // Groups are not part of the cached principal: membership can change while the + // fingerprint stays the same, so they are read on every command. + var diags diag.Diagnostics + groups, err := iamutil.CurrentUserGroups(ctx, w) + if err != nil { + diags = diag.Diagnostics{{ + Severity: diag.Warning, + Summary: fmt.Sprintf("Could not read the groups of the current user: %v", err), + }} + } + // Copy the cached principal so the groups are not written back into the cache entry. + user := *me + user.Groups = groups + b.Config.Workspace.CurrentUser = &config.User{ - ShortName: iamutil.GetShortUserName(me), - DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(me), - User: me, + ShortName: iamutil.GetShortUserName(&user), + DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(&user), + User: &user, } // Configure tagging object now that we know we have a valid client. b.Tagging = tags.ForCloud(w.Config) - return nil + return diags } diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index 1475055dc35..eeffb0539ce 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -1,9 +1,57 @@ package mutator import ( + "net/http" "testing" + "time" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/cache" + "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestPopulateCurrentUser(t *testing.T) { - // We need to implement workspace client mocking to implement this test. +// newPopulateBundle returns a bundle whose workspace serves the default identity and +// membership in the groups named by groups. The handlers registered on server before +// this call take precedence, so a test can override one endpoint. +func newPopulateBundle(t *testing.T, server *testserver.Server, groups ...string) *bundle.Bundle { + testserver.AddDefaultHandlers(server) + server.AddGroups("test-token", groups, nil) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + b := &bundle.Bundle{Config: config.Root{}} + b.Cache = cache.NewCache(t.Context(), "user", time.Minute, &b.Metrics) + b.SetWorkpaceClient(w) + return b +} + +func TestPopulateCurrentUserSetsGroups(t *testing.T) { + b := newPopulateBundle(t, testserver.New(t), "zeta", "alpha") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + require.Len(t, b.Config.Workspace.CurrentUser.Groups, 2) + assert.Equal(t, "alpha", b.Config.Workspace.CurrentUser.Groups[0].Display) + assert.Equal(t, "zeta", b.Config.Workspace.CurrentUser.Groups[1].Display) +} + +// A failed group list leaves the groups empty and warns, and the current user is still set. +func TestPopulateCurrentUserWarnsWhenGroupListFails(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/groups", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} + }) + b := newPopulateBundle(t, server, "testgroup") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.Len(t, diags, 1) + assert.Equal(t, diag.Warning, diags[0].Severity) + assert.Contains(t, diags[0].Summary, "Could not read the groups of the current user") + assert.Nil(t, b.Config.Workspace.CurrentUser.Groups) + assert.Equal(t, "tester@databricks.com", b.Config.Workspace.CurrentUser.UserName) } diff --git a/bundle/permissions/permission_diagnostics.go b/bundle/permissions/permission_diagnostics.go index 0e01f72a0f4..027797c9905 100644 --- a/bundle/permissions/permission_diagnostics.go +++ b/bundle/permissions/permission_diagnostics.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/set" + "github.com/databricks/databricks-sdk-go/service/iam" ) type permissionDiagnostics struct{} @@ -34,7 +35,7 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia return nil } - canManageBundle, _ := analyzeBundlePermissions(b) + canManageBundle, _ := analyzeBundlePermissions(ctx, b) if canManageBundle { return nil } @@ -70,7 +71,7 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia // Returns: // - canManageBundle: true if the current user or one of their groups can manage the bundle resources. // - assistance: advice on who to contact as to manage this project -func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { +func analyzeBundlePermissions(ctx context.Context, b *bundle.Bundle) (bool, string) { canManageBundle := false otherManagers := set.NewSet[string]() if b.Config.RunAs != nil && b.Config.RunAs.UserName != "" && b.Config.RunAs.UserName != b.Config.Workspace.CurrentUser.UserName { @@ -80,8 +81,7 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { } currentUser := b.Config.Workspace.CurrentUser.UserName - targetPermissions := b.Config.Permissions - for _, p := range targetPermissions { + for _, p := range b.Config.Permissions { if p.Level != CAN_MANAGE { continue } @@ -91,7 +91,7 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { continue } - if isGroupOfCurrentUser(b, p.GroupName) { + if p.GroupName != "" && isMemberOfGroup(b, p.GroupName) { canManageBundle = true continue } @@ -120,13 +120,10 @@ func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { return canManageBundle, assistance } -func isGroupOfCurrentUser(b *bundle.Bundle, groupName string) bool { - currentUserGroups := b.Config.Workspace.CurrentUser.Groups - - for _, g := range currentUserGroups { - if g.Display == groupName { - return true - } - } - return false +// isMemberOfGroup reports whether the current user belongs to the group named name, +// by matching the name against the groups read in PopulateCurrentUser. +func isMemberOfGroup(b *bundle.Bundle, name string) bool { + return slices.ContainsFunc(b.Config.Workspace.CurrentUser.Groups, func(g iam.ComplexValue) bool { + return g.Display == name + }) } diff --git a/bundle/permissions/permission_diagnostics_test.go b/bundle/permissions/permission_diagnostics_test.go index e5a9214f437..d368b032f52 100644 --- a/bundle/permissions/permission_diagnostics_test.go +++ b/bundle/permissions/permission_diagnostics_test.go @@ -1,6 +1,7 @@ package permissions_test import ( + "sync/atomic" "testing" "github.com/databricks/cli/bundle" @@ -8,12 +9,16 @@ import ( "github.com/databricks/cli/bundle/config/resources" "github.com/databricks/cli/bundle/permissions" "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestPermissionDiagnosticsApplySuccess(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", UserName: "testuser@databricks.com"}, }) @@ -22,14 +27,14 @@ func TestPermissionDiagnosticsApplySuccess(t *testing.T) { } func TestPermissionDiagnosticsEmpty(t *testing.T) { - b := mockBundle(nil) + b := mockBundle(t, nil) diags := bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics()) require.NoError(t, diags.Error()) } func TestPermissionDiagnosticsApplyFail(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, }) @@ -48,21 +53,129 @@ func TestPermissionDiagnosticsApplyFail(t *testing.T) { require.Contains(t, diags[0].Summary, expectedMsg) } -func mockBundle(permissions []resources.Permission) *bundle.Bundle { - return &bundle.Bundle{ +// groupCalls counts the group requests the workspace serves. +type groupCalls struct { + list atomic.Int32 // GET /api/2.0/identity/groups + get atomic.Int32 // GET /api/2.0/identity/groups/{group_id} +} + +// mockBundle returns a bundle whose workspace has one group, testgroup, that the +// current user belongs to. +func mockBundle(t *testing.T, permissions []resources.Permission) *bundle.Bundle { + b, _ := mockBundleWithGroups(t, permissions, []string{"testgroup"}, nil) + return b +} + +// mockBundleWithGroups returns a bundle whose workspace creates the groups in member, +// which the current user belongs to, and the groups in nonMember, which it does not. +// The bundle's current user carries the groups resolved from the membership, as the +// populate mutator would set them. The returned counts cover only the group requests +// the bundle makes after this call returns. +func mockBundleWithGroups(t *testing.T, permissions []resources.Permission, member, nonMember []string) (*bundle.Bundle, *groupCalls) { + server := testserver.New(t) + calls := &groupCalls{} + // Handlers registered before AddDefaultHandlers take precedence, so they count and + // then delegate to the fake. + server.Handle("GET", "/api/2.0/identity/groups", func(req testserver.Request) any { + calls.list.Add(1) + return req.Workspace.IdentityGroups(0, req.URL.Query().Get("page_token")) + }) + server.Handle("GET", "/api/2.0/identity/groups/{group_id}", func(req testserver.Request) any { + calls.get.Add(1) + return req.Workspace.IdentityGroup(req.Vars["group_id"]) + }) + testserver.AddDefaultHandlers(server) + server.AddGroups("test-token", member, nonMember) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + groups, err := iamutil.CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + calls.list.Store(0) + calls.get.Store(0) + + b := &bundle.Bundle{ Config: config.Root{ Workspace: config.Workspace{ CurrentUser: &config.User{ User: &iam.User{ UserName: "testuser@databricks.com", DisplayName: "Test User", - Groups: []iam.ComplexValue{ - {Display: "testgroup"}, - }, + Groups: groups, }, }, }, Permissions: permissions, }, } + b.SetWorkpaceClient(w) + return b, calls +} + +// A group permission the current user cannot match, with no parent groups in the +// workspace, is reported as not manageable and names the group as a contact. +func TestPermissionDiagnosticsNoGroupMembership(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, nil, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: testgroup.") +} + +// A group that exists in the workspace but that the current user is not a member of +// does not grant CAN_MANAGE, and is still named as a contact. +func TestPermissionDiagnosticsGroupExistsNotMember(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "otherteam"}, + }, nil, []string{"otherteam"}) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: otherteam.") +} + +// A configured group the workspace does not have is named as a contact, not matched. +func TestPermissionDiagnosticsGroupDoesNotExist(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "nosuchgroup"}, + }, nil, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: nosuchgroup.") +} + +// A group the current user belongs to, matched by ID, grants CAN_MANAGE. The report +// is then a stale permission set, not a missing grant. +func TestPermissionDiagnosticsGroupMember(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, []string{"testgroup"}, nil) + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.Equal(t, diag.CannotChangePathPermissions, diags[0].ID) +} + +// Matching a configured group is a comparison with the groups already on the current +// user, so a report makes no group request, whatever the number of configured groups. +func TestPermissionDiagnosticsMembershipMakesNoGroupRequests(t *testing.T) { + b, calls := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + {Level: "CAN_MANAGE", GroupName: "otherteam"}, + {Level: "CAN_MANAGE", GroupName: "nosuchgroup"}, + }, []string{"testgroup", "joined"}, []string{"otherteam"}) + + permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + assert.Equal(t, int32(0), calls.list.Load()) + assert.Equal(t, int32(0), calls.get.Load()) +} + +// A user whose groups were not read, for example because the groups list failed, has +// no group matches, so the configured group is named as a contact. +func TestPermissionDiagnosticsNoGroupsMatchesNothing(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: "testgroup"}, + }, nil, nil) + b.Config.Workspace.CurrentUser.Groups = nil + + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.ErrorContains(t, diags.Error(), "may include: testgroup.") } diff --git a/bundle/permissions/permission_report.go b/bundle/permissions/permission_report.go index 36526eeeba9..eb92dab1c61 100644 --- a/bundle/permissions/permission_report.go +++ b/bundle/permissions/permission_report.go @@ -22,7 +22,7 @@ func ReportPossiblePermissionDenied(ctx context.Context, b *bundle.Bundle, path if iamutil.IsServicePrincipal(me) { userName = me.DisplayName } - canManageBundle, assistance := analyzeBundlePermissions(b) + canManageBundle, assistance := analyzeBundlePermissions(ctx, b) if !canManageBundle { return diag.Diagnostics{{ diff --git a/bundle/permissions/permission_report_test.go b/bundle/permissions/permission_report_test.go index 52437aacdf8..781f845614f 100644 --- a/bundle/permissions/permission_report_test.go +++ b/bundle/permissions/permission_report_test.go @@ -9,7 +9,7 @@ import ( ) func TestPermissionsReportPermissionDeniedWithGroup(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "testgroup"}, }) @@ -22,7 +22,7 @@ func TestPermissionsReportPermissionDeniedWithGroup(t *testing.T) { } func TestPermissionsReportPermissionDeniedWithOtherGroup(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "othergroup"}, }) @@ -36,7 +36,7 @@ func TestPermissionsReportPermissionDeniedWithOtherGroup(t *testing.T) { } func TestPermissionsReportPermissionDeniedWithoutPermission(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, }) @@ -50,7 +50,7 @@ func TestPermissionsReportPermissionDeniedWithoutPermission(t *testing.T) { } func TestPermissionsReportPermissionDeniedNilPermission(t *testing.T) { - b := mockBundle(nil) + b := mockBundle(t, nil) diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") expected := "EPERM1: unable to deploy to testpath as testuser@databricks.com.\n" + @@ -62,7 +62,7 @@ func TestPermissionsReportPermissionDeniedNilPermission(t *testing.T) { } func TestPermissionsReportFindOtherOwners(t *testing.T) { - b := mockBundle([]resources.Permission{ + b := mockBundle(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "testgroup"}, {Level: "CAN_MANAGE", UserName: "alice@databricks.com"}, }) From bf34837491279647ef2f7bf43aaf211b7f45ae4c Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:58 +0000 Subject: [PATCH 05/23] tests: adapt unit test fixtures to identity/me Fixtures that stubbed scim/v2/Me now answer identity/me with the same user, and tests that mocked CurrentUser.Me use a testserver workspace for the call that now goes to identity/me. The air tests share withAirIdentityMe for servers that do not assert on identity/me. TestGetWorkspaceAuthStatus asserts a successful status and the test user name against a testserver with auth type pat. Its AuthTypeMismatch check for DATABRICKS_AUTH_TYPE=azure-cli stays in TestGetWorkspaceAuthStatusError, because the SDK keeps an environment auth type over the resolved one, so the pat call would fail and azure-cli cannot authenticate in a unit test. Co-authored-by: Isaac --- cmd/air/cancel_test.go | 15 +- cmd/air/get_test.go | 29 ++-- cmd/air/list_index_test.go | 4 +- cmd/air/list_test.go | 17 ++- cmd/air/logs_test.go | 4 +- cmd/air/run_test.go | 4 +- cmd/air/run_watch_test.go | 10 +- cmd/air/runlaunch_test.go | 11 +- cmd/air/runsubmit_test.go | 4 +- cmd/apps/run_local_test.go | 14 +- cmd/auth/describe_test.go | 132 +++++++++++------- cmd/auth/profiles_test.go | 18 +-- cmd/labs/project/installer_test.go | 7 - experimental/ssh/internal/server/fuse_test.go | 4 +- .../databrickscfg/cfgpickers/clusters_test.go | 26 ++-- 15 files changed, 172 insertions(+), 127 deletions(-) diff --git a/cmd/air/cancel_test.go b/cmd/air/cancel_test.go index 6f9166d1db3..54ddee151c5 100644 --- a/cmd/air/cancel_test.go +++ b/cmd/air/cancel_test.go @@ -18,7 +18,6 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "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/spf13/cobra" "github.com/stretchr/testify/assert" @@ -288,18 +287,22 @@ func TestCancelAllConfirmReadError(t *testing.T) { } func TestCancelAllMeError(t *testing.T) { - m := mocks.NewMockWorkspaceClient(t) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, iam.MeRequest{}).Return(nil, errors.New("nope")) + // identity/me fails with a non-retriable 403. + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"error_code":"PERMISSION_DENIED","message":"nope"}`)) + })) + t.Cleanup(srv.Close) var buf bytes.Buffer - err := runCancelAll(t, m.WorkspaceClient, flags.OutputText, nil, &buf) + err := runCancelAll(t, newTestWorkspaceClient(t, srv.URL), flags.OutputText, nil, &buf) require.Error(t, err) assert.Contains(t, err.Error(), "failed to resolve current user") } func TestCancelAllListError(t *testing.T) { - // Me succeeds (default empty user), but listing active runs fails. - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // The current user resolves, but listing active runs fails. + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/2.2/jobs/runs/list" { w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(`{"error_code":"INTERNAL","message":"boom"}`)) diff --git a/cmd/air/get_test.go b/cmd/air/get_test.go index dc6eb3636c0..deba18eb8e8 100644 --- a/cmd/air/get_test.go +++ b/cmd/air/get_test.go @@ -16,11 +16,11 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/testserver" "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" - "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" @@ -91,12 +91,23 @@ func TestGetRunNotFound(t *testing.T) { assert.Contains(t, err.Error(), "run 5 not found") } +// identityMeConfig returns a config whose identity/me call answers with resp. +func identityMeConfig(t *testing.T, resp testserver.Response) *config.Config { + t.Helper() + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return resp + }) + return &config.Config{Host: server.URL, Token: "token"} +} + func TestGetRunAuthFailed(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // GetRun's 403 is confirmed as a credential failure by the Me() probe, so + m.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}}) + // GetRun's 403 is confirmed as a credential failure by the current-user 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) cmd.SetContext(ctx) @@ -108,10 +119,10 @@ func TestGetRunAuthFailed(t *testing.T) { func TestGetRunAuthTransient(t *testing.T) { m := mocks.NewMockWorkspaceClient(t) - // A transient failure at the auth probe must not be misreported as an auth + // A probe failure that is not a 401/403 must not be misreported as an auth // error; it surfaces as a retryable internal error instead. + m.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusBadRequest, Body: map[string]any{"error_code": "BAD_REQUEST", "message": "unavailable"}}) 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) cmd.SetContext(ctx) @@ -124,10 +135,10 @@ func TestGetRunAuthTransient(t *testing.T) { 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.WorkspaceClient.Config = identityMeConfig(t, testserver.Response{StatusCode: http.StatusOK, Body: map[string]any{"user": map[string]any{"user_id": "1000012345", "username": "me@example.com"}}}) + // A 403 on the run with credentials the current-user 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) @@ -187,7 +198,7 @@ const succeededRun = `{"run_id": 5, "state": {"life_cycle_state": "TERMINATED", // requests. func getRunServer(t *testing.T, meCalls *atomic.Int32, run string, outputs map[string]string) *httptest.Server { t.Helper() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case r.URL.Path == "/api/2.2/jobs/runs/get": _, _ = w.Write([]byte(run)) diff --git a/cmd/air/list_index_test.go b/cmd/air/list_index_test.go index 0d4f37887c0..ea57c7a2283 100644 --- a/cmd/air/list_index_test.go +++ b/cmd/air/list_index_test.go @@ -329,10 +329,8 @@ func TestRegularScanKeepsFullCap(t *testing.T) { func TestListSelfUserFilterUsesIndex(t *testing.T) { var indexHits, listHits atomic.Int32 run := indexRun(7, 3000_000) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { - case "/api/2.0/preview/scim/v2/Me": - _, _ = w.Write([]byte(`{"userName":"me@example.com"}`)) case aiTrainingWorkflowsPath: indexHits.Add(1) _, _ = w.Write([]byte(`{"training_workflows":[{"job_run_id":"7","submit_time":{"seconds":3000}}]}`)) diff --git a/cmd/air/list_test.go b/cmd/air/list_test.go index f3af7fd9aa1..867ee9dd0b9 100644 --- a/cmd/air/list_test.go +++ b/cmd/air/list_test.go @@ -52,12 +52,27 @@ func runsListBody(t *testing.T, nextToken string, runs ...jobs.BaseRun) string { return string(b) } +// airTestUserBody is the identity/me response for the user that runs are listed for. +const airTestUserBody = `{"name": "accounts/acct-123/me", "user": {"user_id": "1000012345", "username": "me@example.com", "account_user_status": "ACTIVE"}}` + +// withAirIdentityMe answers identity/me with airTestUserBody, the user the air runs are +// listed for, and passes every other request to next. +func withAirIdentityMe(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/identity/me" { + _, _ = w.Write([]byte(airTestUserBody)) + return + } + next(w, r) + } +} + // runsServer serves one runs/list response body per call, repeating the last // once exhausted, and a stub for any other request (the SDK config probe). func runsServer(t *testing.T, bodies ...string) *httptest.Server { t.Helper() call := 0 - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/2.2/jobs/runs/list" { body := bodies[min(call, len(bodies)-1)] call++ diff --git a/cmd/air/logs_test.go b/cmd/air/logs_test.go index f89b1b8b9d8..abfb224e771 100644 --- a/cmd/air/logs_test.go +++ b/cmd/air/logs_test.go @@ -236,8 +236,8 @@ 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") + case strings.HasSuffix(r.URL.Path, "/identity/me"): + t.Errorf("unexpected current-user probe on a successful logs fetch") w.WriteHeader(http.StatusInternalServerError) default: // SDK config discovery. diff --git a/cmd/air/run_test.go b/cmd/air/run_test.go index cedae60e672..0242f64f7e4 100644 --- a/cmd/air/run_test.go +++ b/cmd/air/run_test.go @@ -31,7 +31,7 @@ func submitServer(t *testing.T) (*httptest.Server, *submitRequestCounts) { t.Helper() counts := &submitRequestCounts{} runGet := `{"run_id": 555, "tasks": [{"run_id": 556, "attempt_number": 0}]}` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 555}`)) @@ -193,7 +193,7 @@ func TestRunDryRunValidatesBackendWithoutMutation(t *testing.T) { if request.path == validateConfigPath { validations = append(validations, request) } - assert.Contains(t, []string{"/.well-known/databricks-config", "/api/2.0/preview/scim/v2/Me", validateConfigPath}, request.path, + assert.Contains(t, []string{"/.well-known/databricks-config", "/api/2.0/identity/me", validateConfigPath}, request.path, "dry-run must not make requests beyond authentication, user lookup, and validation") assert.True(t, request.method == http.MethodGet || request.path == validateConfigPath, "dry-run made unexpected %s request to %s", request.method, request.path) diff --git a/cmd/air/run_watch_test.go b/cmd/air/run_watch_test.go index 13c21923057..62de87c938f 100644 --- a/cmd/air/run_watch_test.go +++ b/cmd/air/run_watch_test.go @@ -36,7 +36,7 @@ func watchServer(t *testing.T, resultState string) *httptest.Server { "state": {"life_cycle_state": "TERMINATED", "result_state": "` + resultState + `"}, "tasks": [{"run_id": 778, "attempt_number": 0}] }` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 777}`)) @@ -54,7 +54,7 @@ func watchServer(t *testing.T, resultState string) *httptest.Server { } _, _ = fmt.Fprintf(w, `{"log_records":[%s,%s]}`, first, second) default: - // Me() user lookup, workspace-id, SDK config discovery. + // Workspace-id and SDK config discovery. _, _ = w.Write([]byte(`{"userName": "u@example.com", "workspace_id": 1}`)) } })) @@ -75,7 +75,7 @@ func watchServerMLflow(t *testing.T, resultState string) *httptest.Server { "state": {"life_cycle_state": "TERMINATED", "result_state": "` + resultState + `"}, "tasks": [{"run_id": 778, "attempt_number": 0}] }` - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch { case strings.HasSuffix(r.URL.Path, "/jobs/runs/submit"): _, _ = w.Write([]byte(`{"run_id": 777}`)) @@ -231,6 +231,10 @@ func TestRunWatchDryRunSkipsSubmit(t *testing.T) { var got []string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { got = append(got, r.URL.Path) + if r.URL.Path == "/api/2.0/identity/me" { + _, _ = w.Write([]byte(airTestUserBody)) + return + } _, _ = w.Write([]byte(`{}`)) })) t.Cleanup(srv.Close) diff --git a/cmd/air/runlaunch_test.go b/cmd/air/runlaunch_test.go index edb4803ba65..4434c2ff29d 100644 --- a/cmd/air/runlaunch_test.go +++ b/cmd/air/runlaunch_test.go @@ -9,7 +9,6 @@ import ( "github.com/databricks/cli/libs/filer" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -46,14 +45,9 @@ func TestUserWorkspaceDir(t *testing.T) { assert.Equal(t, "/Workspace/custom", dir) } -func TestUserWorkspaceDirRequestsOnlyUserName(t *testing.T) { +func TestUserWorkspaceDirUsesCurrentUser(t *testing.T) { server := testserver.New(t) t.Cleanup(server.Close) - var attributes []string - server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any { - attributes = append(attributes, req.URL.Query().Get("attributes")) - return iam.User{UserName: "me@example.com"} - }) testserver.AddDefaultHandlers(server) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) require.NoError(t, err) @@ -61,8 +55,7 @@ func TestUserWorkspaceDirRequestsOnlyUserName(t *testing.T) { dir, err := userWorkspaceDir(t.Context(), w) require.NoError(t, err) - assert.Equal(t, "/Workspace/Users/me@example.com", dir) - assert.Equal(t, []string{"userName"}, attributes) + assert.Equal(t, "/Workspace/Users/tester@databricks.com", dir) } func TestUserWorkspaceDirProfilePrecedence(t *testing.T) { diff --git a/cmd/air/runsubmit_test.go b/cmd/air/runsubmit_test.go index e101bbc28af..ad851cc390c 100644 --- a/cmd/air/runsubmit_test.go +++ b/cmd/air/runsubmit_test.go @@ -769,9 +769,9 @@ func TestSubmitWorkloadWithCodeSource(t *testing.T) { return jobs.SubmitRunResponse{RunId: 555} }) var meCalls atomic.Int32 - server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any { + server.Handle("GET", "/api/2.0/identity/me", func(req testserver.Request) any { meCalls.Add(1) - return req.Workspace.MeUser(req.Token) + return req.Workspace.IdentityMe(req.Token) }) stubValidateConfig(server) testserver.AddDefaultHandlers(server) diff --git a/cmd/apps/run_local_test.go b/cmd/apps/run_local_test.go index 94a0b56ec70..de5d57eaa7e 100644 --- a/cmd/apps/run_local_test.go +++ b/cmd/apps/run_local_test.go @@ -8,10 +8,9 @@ import ( "time" "github.com/databricks/cli/libs/apps/runlocal" - "github.com/databricks/databricks-sdk-go/experimental/mocks" - "github.com/databricks/databricks-sdk-go/service/iam" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" "github.com/spf13/cobra" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -21,11 +20,14 @@ func TestSetupProxyPortInUse(t *testing.T) { defer ln.Close() port := ln.Addr().(*net.TCPAddr).Port - m := mocks.NewMockWorkspaceClient(t) - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "test-user"}, nil) + server := testserver.New(t) + t.Cleanup(server.Close) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) config := runlocal.NewConfig("https://workspace.databricks.test", "123", t.TempDir(), runlocal.DEFAULT_HOST, runlocal.DEFAULT_PORT) - err = setupProxy(t.Context(), &cobra.Command{}, config, m.WorkspaceClient, port, false) + err = setupProxy(t.Context(), &cobra.Command{}, config, w, port, false) require.ErrorContains(t, err, "failed to start app proxy") } diff --git a/cmd/auth/describe_test.go b/cmd/auth/describe_test.go index 3d7bd62a0a1..3ad0b3092a6 100644 --- a/cmd/auth/describe_test.go +++ b/cmd/auth/describe_test.go @@ -7,15 +7,17 @@ import ( "net/http/httptest" "os" "path/filepath" + "sync/atomic" "testing" "github.com/databricks/cli/libs/auth/storage" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/testserver" + "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" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -91,51 +93,46 @@ func TestGetWorkspaceAuthStatus(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - showSensitive := false - - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{ - UserName: "test-user", - }, nil) - cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") err := cmd.Flag("profile").Value.Set("my-profile") require.NoError(t, err) cmd.Flag("profile").Changed = true + // Only identity/me is served, so the username comes from this fixture rather than the default user. + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}} + }) + cfg := &config.Config{ Profile: "my-profile", } m.WorkspaceClient.Config = cfg - t.Setenv("DATABRICKS_AUTH_TYPE", "azure-cli") + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), ".databrickscfg")) err = config.ConfigAttributes.Configure(cfg) require.NoError(t, err) - status, err := getAuthStatus(cmd, []string{}, showSensitive, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { + status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { err := config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", + "host": server.URL, "token": "test-token", - "auth_type": "azure-cli", + "auth_type": "pat", }) require.NoError(t, err) return cfg, false, nil }) require.NoError(t, err) require.NotNil(t, status) + require.NoError(t, status.Error) require.Equal(t, "success", status.Status) require.Equal(t, "test-user", status.Username) - require.Equal(t, "https://test.test", status.Details.Host) - require.Equal(t, "azure-cli", status.Details.AuthType) + require.Equal(t, server.URL, status.Details.Host) + require.Equal(t, "pat", status.Details.AuthType) - require.Equal(t, "azure-cli", status.Details.Configuration["auth_type"].Value) - require.Equal(t, "DATABRICKS_AUTH_TYPE environment variable", status.Details.Configuration["auth_type"].Source.String()) + require.Equal(t, "pat", status.Details.Configuration["auth_type"].Value) require.False(t, status.Details.Configuration["auth_type"].AuthTypeMismatch) - require.Equal(t, "********", status.Details.Configuration["token"].Value) - require.Equal(t, "dynamic configuration", status.Details.Configuration["token"].Source.String()) - require.True(t, status.Details.Configuration["token"].AuthTypeMismatch) - require.Equal(t, "my-profile", status.Details.Configuration["profile"].Value) require.Equal(t, "--profile flag", status.Details.Configuration["profile"].Source.String()) require.False(t, status.Details.Configuration["profile"].AuthTypeMismatch) @@ -274,6 +271,7 @@ func TestGetAccountAuthStatus(t *testing.T) { }) require.NoError(t, err) require.NotNil(t, status) + require.NoError(t, status.Error) require.Equal(t, "success", status.Status) require.Equal(t, "test-user", status.Username) @@ -372,8 +370,13 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "u2m-user"}, nil) + // A databricks-cli token cannot be minted in a unit test, so the check fails; + // token storage is reported for the status either way. + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusUnauthorized, Body: map[string]any{"error_code": "UNAUTHENTICATED", "message": "no token"}} + }) cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") @@ -388,8 +391,9 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { require.NoError(t, config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", - "auth_type": authTypeDatabricksCLI, + "host": server.URL, + "auth_type": authTypeDatabricksCLI, + "databricks_cli_path": "/nonexistent/databricks", })) return cfg, false, nil }) @@ -401,10 +405,10 @@ func TestGetWorkspaceAuthStatus_U2M_PopulatesTokenStorage(t *testing.T) { assert.Equal(t, "DATABRICKS_AUTH_STORAGE environment variable", status.TokenStorage.Source) } -// describeVerifyServer simulates the host that describe's secondary -// verification calls hit: CurrentUser.Me and account Workspaces.List. -// Statuses other than 200 return an error body with that status code; a zero -// status marks an endpoint that must not be called at all. +// describeVerifyServer simulates the host that describe's verification calls +// hit: identity/me (the workspace check) and account Workspaces.List. Statuses +// other than 200 return an error body with that status code; a zero status marks +// an endpoint that must not be called at all. func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus int) *httptest.Server { t.Helper() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -421,8 +425,8 @@ func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus i require.NoError(t, json.NewEncoder(w).Encode(okBody)) } switch r.URL.Path { - case "/api/2.0/preview/scim/v2/Me": - respond(meStatus, map[string]any{"userName": "fallback-user"}) + case "/api/2.0/identity/me": + respond(meStatus, map[string]any{"user": map[string]any{"user_id": "1", "username": "fallback-user"}}) case "/api/2.0/accounts/" + accountID + "/workspaces": respond(listStatus, []map[string]any{}) default: @@ -433,15 +437,12 @@ func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus i return server } -// newDescribeWorkspaceCmd wires a mocked workspace client whose Me call fails -// with meErr, with cfg as its resolved configuration. -func newDescribeWorkspaceCmd(t *testing.T, cfg *config.Config, meErr error) *cobra.Command { +// newDescribeWorkspaceCmd wires a workspace client over cfg, the resolved +// configuration, so its identity/me call goes to the configured host. +func newDescribeWorkspaceCmd(t *testing.T, cfg *config.Config) *cobra.Command { t.Helper() - m := mocks.NewMockWorkspaceClient(t) - m.WorkspaceClient.Config = cfg - m.GetMockCurrentUserAPI().EXPECT().Me(mock.Anything, mock.Anything).Return(nil, meErr) cmd := newHostProfileCmd(t) - cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), m.WorkspaceClient)) + cmd.SetContext(cmdctx.SetWorkspaceClient(t.Context(), &databricks.WorkspaceClient{Config: cfg})) t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), ".databrickscfg")) return cmd } @@ -469,16 +470,17 @@ func resolveCfg(t *testing.T, cfg *config.Config, attrs map[string]string, isAcc } } -// TestGetAuthStatusVerificationFallback covers the fallback when the primary -// verification call (mocked client) fails: describe tries the other endpoint -// over HTTP against describeVerifyServer, and only when both fail does it -// report the primary error. Error rows leave wantUsername/wantAccountID empty -// because errorAuthStatus never sets them. +// TestGetAuthStatusVerificationFallback covers the fallback when the primary check +// fails. The account branch tries identity/me after Workspaces.List fails, and the +// workspace branch tries the account Workspaces list after identity/me fails. Only when +// both fail is the primary error reported. describeVerifyServer answers the HTTP +// fallback endpoint. Error rows leave wantUsername/wantAccountID empty because +// errorAuthStatus never sets them. func TestGetAuthStatusVerificationFallback(t *testing.T) { tests := []struct { name string isAccount bool - primaryErr *apierr.APIError + primaryErr *apierr.APIError // account branch only: the primary Workspaces.List error meStatus int listStatus int accountID string @@ -488,7 +490,6 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }{ { name: "workspace check fails, account check succeeds", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, meStatus: http.StatusNotFound, listStatus: http.StatusOK, accountID: "test-acct", @@ -497,7 +498,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "no second call without an account id", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, + meStatus: http.StatusBadRequest, wantStatus: "error", }, { @@ -512,7 +513,6 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "workspace branch, both checks fail", - primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "Unable to load OAuth Config"}, meStatus: http.StatusNotFound, listStatus: http.StatusUnauthorized, accountID: "test-acct", @@ -538,7 +538,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { if tc.isAccount { cmd = newDescribeAccountCmd(t, cfg, tc.primaryErr) } else { - cmd = newDescribeWorkspaceCmd(t, cfg, tc.primaryErr) + cmd = newDescribeWorkspaceCmd(t, cfg) } attrs := map[string]string{ "host": server.URL, @@ -554,7 +554,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { require.Equal(t, tc.wantStatus, status.Status) assert.Equal(t, tc.wantUsername, status.Username) assert.Equal(t, tc.wantAccountID, status.AccountID) - if tc.wantStatus == "error" { + if tc.wantStatus == "error" && tc.primaryErr != nil { assert.ErrorIs(t, status.Error, tc.primaryErr) } }) @@ -569,8 +569,9 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { cmd := &cobra.Command{} cmd.SetContext(ctx) - currentUserApi := m.GetMockCurrentUserAPI() - currentUserApi.EXPECT().Me(mock.Anything, mock.Anything).Return(&iam.User{UserName: "pat-user"}, nil) + server := testserver.New(t) + t.Cleanup(server.Close) + testserver.AddDefaultHandlers(server) cmd.Flags().String("host", "", "") cmd.Flags().String("profile", "", "") @@ -581,7 +582,7 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { status, err := getAuthStatus(cmd, []string{}, false, func(cmd *cobra.Command, args []string) (*config.Config, bool, error) { require.NoError(t, config.ConfigAttributes.ResolveFromStringMap(cfg, map[string]string{ - "host": "https://test.test", + "host": server.URL, "token": "pat-token", "auth_type": "pat", })) @@ -591,3 +592,32 @@ func TestGetWorkspaceAuthStatus_NonU2M_OmitsTokenStorage(t *testing.T) { require.NotNil(t, status) assert.Nil(t, status.TokenStorage) } + +// The account branch probes identity/me once. A 500 is not retried, so describe does not +// wait out the workspace retry window before reporting the failure. +func TestGetAuthStatusAccountProbeDoesNotRetryOn500(t *testing.T) { + t.Setenv("DATABRICKS_ACCOUNT_ID", "") + var meCalls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/2.0/identity/me" { + meCalls.Add(1) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(`{"error_code":"INTERNAL_ERROR","message":"boom"}`)) + })) + t.Cleanup(server.Close) + + cfg := &config.Config{} + cmd := newDescribeAccountCmd(t, cfg, &apierr.APIError{StatusCode: http.StatusForbidden, Message: "not an account admin"}) + attrs := map[string]string{ + "host": server.URL, + "token": "test-token", + "auth_type": "pat", + "account_id": "test-acct", + } + status, err := getAuthStatus(cmd, []string{}, false, resolveCfg(t, cfg, attrs, true)) + require.NoError(t, err) + assert.Equal(t, "error", status.Status) + assert.Equal(t, int32(1), meCalls.Load()) +} diff --git a/cmd/auth/profiles_test.go b/cmd/auth/profiles_test.go index 90efa2a99bd..48fafd74ae8 100644 --- a/cmd/auth/profiles_test.go +++ b/cmd/auth/profiles_test.go @@ -112,7 +112,7 @@ func TestProfilesDefaultMarker(t *testing.T) { // newSPOGServer creates a mock SPOG server that returns account-scoped OIDC. // It serves both validation endpoints since SPOG workspace profiles (with a -// real workspace_id) need CurrentUser.Me, while account profiles need +// real workspace_id) need identity/me, while account profiles need // Workspaces.List. The workspace-only newWorkspaceServer omits the account // endpoint to prove routing correctness for non-SPOG hosts. func newSPOGServer(t *testing.T, accountID string) *httptest.Server { @@ -127,9 +127,9 @@ func newSPOGServer(t *testing.T, accountID string) *httptest.Server { }) case "/api/2.0/accounts/" + accountID + "/workspaces": _ = json.NewEncoder(w).Encode([]map[string]any{}) - case "/api/2.0/preview/scim/v2/Me": - // SPOG workspace profiles also need CurrentUser.Me to succeed. - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + // SPOG workspace profiles also need the current user to resolve. + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } @@ -151,8 +151,8 @@ func newWorkspaceServer(t *testing.T, accountID string) *httptest.Server { "account_id": accountID, "oidc_endpoint": r.Host + "/oidc", }) - case "/api/2.0/preview/scim/v2/Me": - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } @@ -250,14 +250,14 @@ func TestClassicAccountsHostConfigType(t *testing.T) { func TestProfileLoadNoDiscoveryStaysWorkspace(t *testing.T) { // When .well-known returns 404 and the unified-host fallback is false, // the SPOG override should NOT trigger even if account_id is set. The - // profile should stay WorkspaceConfig and validate via CurrentUser.Me. + // profile should stay WorkspaceConfig and validate via identity/me. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.URL.Path { case "/.well-known/databricks-config": w.WriteHeader(http.StatusNotFound) - case "/api/2.0/preview/scim/v2/Me": - _ = json.NewEncoder(w).Encode(map[string]any{"userName": "test-user"}) + case "/api/2.0/identity/me": + _ = json.NewEncoder(w).Encode(map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}) default: w.WriteHeader(http.StatusNotFound) } diff --git a/cmd/labs/project/installer_test.go b/cmd/labs/project/installer_test.go index 266d42dbeb8..6a5dc19b1bd 100644 --- a/cmd/labs/project/installer_test.go +++ b/cmd/labs/project/installer_test.go @@ -24,7 +24,6 @@ import ( "github.com/databricks/cli/libs/process" "github.com/databricks/cli/libs/python" "github.com/databricks/databricks-sdk-go/service/compute" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/sql" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -327,12 +326,6 @@ func TestInstallerWorksForDevelopment(t *testing.T) { }) return } - if r.URL.Path == "/api/2.0/preview/scim/v2/Me" { - respondWithJSON(t, w, iam.User{ - UserName: "serge", - }) - return - } if r.URL.Path == "/api/2.1/clusters/spark-versions" { respondWithJSON(t, w, compute.GetSparkVersionsResponse{ Versions: []compute.SparkVersion{ diff --git a/experimental/ssh/internal/server/fuse_test.go b/experimental/ssh/internal/server/fuse_test.go index 393c5ddaa35..eee3fa5a74a 100644 --- a/experimental/ssh/internal/server/fuse_test.go +++ b/experimental/ssh/internal/server/fuse_test.go @@ -89,7 +89,7 @@ func TestFuseUserInfo(t *testing.T) { wantUserID string wantNotebookDir string }{ - {name: "available", status: http.StatusOK, body: `{"id":"12345","userName":"user@example.test"}`, wantUserID: "12345", wantNotebookDir: "/Users/user@example.test"}, + {name: "available", status: http.StatusOK, body: `{"user":{"user_id":"12345","username":"user@example.test"}}`, wantUserID: "12345", wantNotebookDir: "/Users/user@example.test"}, {name: "forbidden", status: http.StatusForbidden, body: `{"message":"forbidden"}`}, {name: "timeout"}, } { @@ -98,7 +98,7 @@ func TestFuseUserInfo(t *testing.T) { c, err := databricks.NewWorkspaceClient(&databricks.Config{ Host: "https://workspace.test", HostMetadataResolver: noHostMetadata, Token: "test-token", HTTPTransport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - assert.Equal(t, "/api/2.0/preview/scim/v2/Me", r.URL.Path) + assert.Equal(t, "/api/2.0/identity/me", r.URL.Path) if tc.status == 0 { <-r.Context().Done() return nil, r.Context().Err() diff --git a/libs/databrickscfg/cfgpickers/clusters_test.go b/libs/databrickscfg/cfgpickers/clusters_test.go index 1c462eace05..b57b3157edc 100644 --- a/libs/databrickscfg/cfgpickers/clusters_test.go +++ b/libs/databrickscfg/cfgpickers/clusters_test.go @@ -7,7 +7,6 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/qa" "github.com/databricks/databricks-sdk-go/service/compute" - "github.com/databricks/databricks-sdk-go/service/iam" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -116,13 +115,7 @@ func TestFirstCompatibleCluster(t *testing.T) { }, }, }, - { - Method: "GET", - Resource: "/api/2.0/preview/scim/v2/Me?", - Response: iam.User{ - UserName: "serge", - }, - }, + identityMeFixture(), { Method: "GET", Resource: "/api/2.1/clusters/spark-versions", @@ -163,13 +156,7 @@ func TestNoCompatibleClusters(t *testing.T) { }, }, }, - { - Method: "GET", - Resource: "/api/2.0/preview/scim/v2/Me?", - Response: iam.User{ - UserName: "serge", - }, - }, + identityMeFixture(), { Method: "GET", Resource: "/api/2.1/clusters/spark-versions", @@ -190,3 +177,12 @@ func TestNoCompatibleClusters(t *testing.T) { _, err := AskForCluster(ctx, w, WithDatabricksConnect("13.1")) require.Equal(t, ErrNoCompatibleClusters, err) } + +// identityMeFixture answers the current-user probe as user serge. +func identityMeFixture() qa.HTTPFixture { + return qa.HTTPFixture{ + Method: "GET", + Resource: "/api/2.0/identity/me", + Response: map[string]any{"user": map[string]string{"user_id": "1000012345", "username": "serge"}}, + } +} From d840e707d0662057dd176ad89550d934b0627172 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:50:59 +0000 Subject: [PATCH 06/23] acceptance: cover identity/me principals and cloud checks Adds fake-server tests for a group returned by identity/me, and cloud-enabled tests for auth describe and for bundle validate reading the current user's groups. Request logs now show identity/me where they showed scim/v2/Me, plus GET /api/2.0/identity/me/transitive-parent-groups and GET /api/2.0/identity/groups for bundle commands: one account group list per command, with no per-group lookups. The liteswap check follows the bundle's first principal request to identity/me. Golden changes that are not a request-log substitution: - bundle summary and validate current_user gains active and emails, because the mapped iam.User carries them. Real SCIM Me already returned both, so production output is unchanged; only the testserver's SCIM fake lacked them. - bundle/python experimental-compatibility byte offsets shift by the length of those added fields. Co-authored-by: Isaac --- .../auth/bundle_default_profile/output.txt | 4 +- .../auth/bundle_default_profile/test.toml | 4 ++ .../url_ref/out.destroy-requests.txt | 3 -- .../bind/pipelines/update/out.summary.json | 7 ++++ .../output.txt | 4 +- .../experimental-compatibility/output.txt | 4 +- .../resources/jobs/request-summary/output.txt | 10 ++++- .../target-is-passed/default/output.txt | 20 +++++++++- .../target-is-passed/from_flag/output.txt | 20 +++++++++- .../profile-is-passed/from_flag/output.txt | 20 +++++++++- .../target-is-passed/default/output.txt | 40 ++++++++++++++++++- .../target-is-passed/from_flag/output.txt | 40 ++++++++++++++++++- .../helpers-error/test.toml | 5 ++- acceptance/bundle/user_agent/output.txt | 28 +++++++++---- .../simple/out.requests.deploy.json | 20 +++++++++- .../simple/out.requests.destroy.json | 20 +++++++++- .../user_agent/simple/out.requests.plan.json | 20 +++++++++- .../user_agent/simple/out.requests.plan2.json | 20 +++++++++- .../user_agent/simple/out.requests.run.json | 20 +++++++++- .../simple/out.requests.summary.json | 20 +++++++++- .../simple/out.requests.validate.json | 20 +++++++++- .../validate/identity-me-cloud/databricks.yml | 2 + .../validate/identity-me-cloud/out.test.toml | 2 + .../validate/identity-me-cloud/output.txt | 3 ++ .../bundle/validate/identity-me-cloud/script | 5 +++ .../validate/identity-me-cloud/test.toml | 11 +++++ .../immutable_workspace_paths/output.txt | 7 ++++ .../bundle/validate/job-references/output.txt | 7 ++++ .../out.with_empty_prefix.json | 7 ++++ .../out.with_nil_prefix.json | 7 ++++ .../out.with_static_prefix.json | 7 ++++ .../out.without_presets.json | 7 ++++ .../account-host-with-workspace-id/test.toml | 2 +- .../describe/identity-me-cloud/out.test.toml | 1 + .../describe/identity-me-cloud/output.txt | 8 ++++ .../auth/describe/identity-me-cloud/script | 2 + .../auth/describe/identity-me-cloud/test.toml | 6 +++ .../describe/identity-me-group/out.test.toml | 1 + .../describe/identity-me-group/output.txt | 18 +++++++++ .../auth/describe/identity-me-group/script | 2 + .../auth/describe/identity-me-group/test.toml | 19 +++++++++ acceptance/cmd/liteswap-target/output.txt | 13 +++++- acceptance/cmd/liteswap-target/script | 2 +- acceptance/internal/prepare_server.go | 2 +- 44 files changed, 452 insertions(+), 38 deletions(-) create mode 100644 acceptance/bundle/validate/identity-me-cloud/databricks.yml create mode 100644 acceptance/bundle/validate/identity-me-cloud/out.test.toml create mode 100644 acceptance/bundle/validate/identity-me-cloud/output.txt create mode 100644 acceptance/bundle/validate/identity-me-cloud/script create mode 100644 acceptance/bundle/validate/identity-me-cloud/test.toml create mode 100644 acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml create mode 100644 acceptance/cmd/auth/describe/identity-me-cloud/output.txt create mode 100644 acceptance/cmd/auth/describe/identity-me-cloud/script create mode 100644 acceptance/cmd/auth/describe/identity-me-cloud/test.toml create mode 100644 acceptance/cmd/auth/describe/identity-me-group/out.test.toml create mode 100644 acceptance/cmd/auth/describe/identity-me-group/output.txt create mode 100644 acceptance/cmd/auth/describe/identity-me-group/script create mode 100644 acceptance/cmd/auth/describe/identity-me-group/test.toml diff --git a/acceptance/auth/bundle_default_profile/output.txt b/acceptance/auth/bundle_default_profile/output.txt index f8e8a3c0062..557b4f3eaac 100644 --- a/acceptance/auth/bundle_default_profile/output.txt +++ b/acceptance/auth/bundle_default_profile/output.txt @@ -11,7 +11,7 @@ >>> errcode [CLI] bundle validate -p other -o json Warn: [hostmetadata] failed to fetch host metadata for https://other.test, will skip for 1m0s -Error: Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted) +Error: Get "https://other.test/api/2.0/identity/me": (redacted) Exit code: 1 @@ -31,7 +31,7 @@ Exit code: 1 === Bundle with workspace.profile: pinned profile wins over default_profile >>> errcode [CLI] bundle validate -o json -Error: Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted) +Error: Get "https://other.test/api/2.0/identity/me": (redacted) Exit code: 1 diff --git a/acceptance/auth/bundle_default_profile/test.toml b/acceptance/auth/bundle_default_profile/test.toml index 5d5b0283f42..31a933f5d06 100644 --- a/acceptance/auth/bundle_default_profile/test.toml +++ b/acceptance/auth/bundle_default_profile/test.toml @@ -9,3 +9,7 @@ Ignore = [ [[Repls]] Old = 'Get "https://other.test/api/2.0/preview/scim/v2/Me\??": .*' New = 'Get "https://other.test/api/2.0/preview/scim/v2/Me": (redacted)' + +[[Repls]] +Old = 'Get "https://other.test/api/2.0/identity/me": .*' +New = 'Get "https://other.test/api/2.0/identity/me": (redacted)' diff --git a/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt b/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt index 9ae96b91891..e69de29bb2d 100644 --- a/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt +++ b/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt @@ -1,3 +0,0 @@ -File [OUTPUT_DIR]/out.requests.txt not found - -Exit code: 1 diff --git a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json index 6d538af432a..85aea659837 100644 --- a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json +++ b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json @@ -38,7 +38,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/test-pipeline-recreate/default/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt index a649ac03b5f..e18cea7f6dc 100644 --- a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt +++ b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt @@ -57,14 +57,14 @@ [ 0, 1, - 880 + 961 ] ], "resources.jobs": [ [ 0, 1, - 889 + 970 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/python/experimental-compatibility/output.txt b/acceptance/bundle/python/experimental-compatibility/output.txt index 40a5754e118..42036a6ae2a 100644 --- a/acceptance/bundle/python/experimental-compatibility/output.txt +++ b/acceptance/bundle/python/experimental-compatibility/output.txt @@ -50,14 +50,14 @@ [ 0, 1, - 825 + 906 ] ], "resources.jobs": [ [ 0, 1, - 834 + 915 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/resources/jobs/request-summary/output.txt b/acceptance/bundle/resources/jobs/request-summary/output.txt index a05f10b342f..b1f6fb5e4a6 100644 --- a/acceptance/bundle/resources/jobs/request-summary/output.txt +++ b/acceptance/bundle/resources/jobs/request-summary/output.txt @@ -6,7 +6,9 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> print_requests GET /.well-known/databricks-config -GET /api/2.0/preview/scim/v2/Me +GET /api/2.0/identity/groups +GET /api/2.0/identity/me +GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/resources.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/terraform.tfstate @@ -18,6 +20,8 @@ Files: 1 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged >>> print_requests +GET /api/2.0/identity/groups +GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json @@ -47,6 +51,8 @@ Files: 0 uploaded, 0 deleted Resources: 0 created, 0 changed, 0 deleted, 1 unchanged >>> print_requests +GET /api/2.0/identity/groups +GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json @@ -74,6 +80,8 @@ All files and directories at the following location will be deleted: /Workspace/ Destroy: 1 deleted >>> print_requests +GET /api/2.0/identity/groups +GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/resources.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/terraform.tfstate diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt index 067706823b6..f23e3763f62 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt @@ -14,7 +14,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "headers": { diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt index dcc90ffb748..cb7b726acf8 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt @@ -42,7 +42,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "headers": { diff --git a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt index 2d9c8b6521d..55929e120f9 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt @@ -31,7 +31,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "method": "GET", diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt index 64073379aa6..80c30b1d6e4 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt @@ -14,7 +14,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "method": "GET", @@ -27,7 +45,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer [DATABRICKS_TOKEN]" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "headers": { diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt index 91d2b39a321..baeff49be46 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt @@ -28,7 +28,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "method": "GET", @@ -55,7 +73,25 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} +{ + "headers": { + "Authorization": [ + "Bearer oauth-token" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" } { "headers": { diff --git a/acceptance/bundle/templates-machinery/helpers-error/test.toml b/acceptance/bundle/templates-machinery/helpers-error/test.toml index 3839635db08..faa704eb91b 100644 --- a/acceptance/bundle/templates-machinery/helpers-error/test.toml +++ b/acceptance/bundle/templates-machinery/helpers-error/test.toml @@ -1,6 +1,7 @@ Badness = '''(minor) error message is not great: executing "" at : error calling user_name:''' +# The 400 keeps the golden error. The 500 retry path is not covered by this test. [[Server]] -Pattern = "GET /api/2.0/preview/scim/v2/Me" +Pattern = "GET /api/2.0/identity/me" Response.Body = '{}' -Response.StatusCode = 500 +Response.StatusCode = 400 diff --git a/acceptance/bundle/user_agent/output.txt b/acceptance/bundle/user_agent/output.txt index bb7b5c94d3d..7656fadc0c3 100644 --- a/acceptance/bundle/user_agent/output.txt +++ b/acceptance/bundle/user_agent/output.txt @@ -1,4 +1,5 @@ -MISS deploy /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat' +MISS deploy /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat' +MISS deploy /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat' MISS deploy /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat' MISS deploy /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat' OK deploy /api/2.0/workspace/export engine/direct @@ -19,8 +20,10 @@ OK deploy /api/2.0/workspace/delete engine/direct OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.1/unity-catalog/schemas engine/direct +MISS deploy /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat' MISS deploy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS destroy /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' +MISS destroy /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' +MISS destroy /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' MISS destroy /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' MISS destroy /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' OK destroy /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct @@ -32,29 +35,40 @@ OK destroy /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct OK destroy /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deploy.lock engine/direct OK destroy /api/2.0/workspace/delete engine/direct OK destroy /api/2.0/workspace/delete engine/direct +MISS destroy /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat' MISS destroy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS plan /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' +MISS plan /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' +MISS plan /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' OK plan /api/2.0/workspace/export engine/direct +MISS plan /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS plan /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS plan2 /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' +MISS plan2 /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' +MISS plan2 /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan2 /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan2 /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' OK plan2 /api/2.0/workspace/export engine/direct OK plan2 /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct +MISS plan2 /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS plan2 /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS run /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' +MISS run /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' +MISS run /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' MISS run /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' MISS run /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' +MISS run /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS run /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS summary /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' +MISS summary /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' +MISS summary /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' MISS summary /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' MISS summary /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' OK summary /api/2.0/preview/scim/v2/Me engine/direct +MISS summary /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS summary /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' -MISS validate /api/2.0/preview/scim/v2/Me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' +MISS validate /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' +MISS validate /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/mkdirs 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' +MISS validate /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS validate /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' diff --git a/acceptance/bundle/user_agent/simple/out.requests.deploy.json b/acceptance/bundle/user_agent/simple/out.requests.deploy.json index 028011cb20c..7ad2f901434 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.deploy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.deploy.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -318,6 +327,15 @@ "name": "myschema" } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.destroy.json b/acceptance/bundle/user_agent/simple/out.requests.destroy.json index a77a7a20300..0e59e40efe3 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.destroy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.destroy.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -148,6 +157,15 @@ "recursive": true } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan.json b/acceptance/bundle/user_agent/simple/out.requests.plan.json index 8658a9f41f4..f011777f301 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -46,6 +55,15 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deployment.json" } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan2.json b/acceptance/bundle/user_agent/simple/out.requests.plan2.json index 27810cf9e61..6fe827d5469 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan2.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan2.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -55,6 +64,15 @@ "method": "GET", "path": "/api/2.1/unity-catalog/schemas/mycatalog.myschema" } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.run.json b/acceptance/bundle/user_agent/simple/out.requests.run.json index ace799d7c7e..aeaab13122b 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.run.json +++ b/acceptance/bundle/user_agent/simple/out.requests.run.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -33,6 +42,15 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/terraform.tfstate" } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.summary.json b/acceptance/bundle/user_agent/simple/out.requests.summary.json index 6182e907b70..073c9804cda 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.summary.json +++ b/acceptance/bundle/user_agent/simple/out.requests.summary.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -45,6 +54,15 @@ "excludedAttributes": "entitlements" } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.validate.json b/acceptance/bundle/user_agent/simple/out.requests.validate.json index 5750f79f473..197b4bcce56 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.validate.json +++ b/acceptance/bundle/user_agent/simple/out.requests.validate.json @@ -5,7 +5,16 @@ ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } { "headers": { @@ -43,6 +52,15 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files" } } +{ + "headers": { + "User-Agent": [ + "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/groups" +} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/validate/identity-me-cloud/databricks.yml b/acceptance/bundle/validate/identity-me-cloud/databricks.yml new file mode 100644 index 00000000000..26d7f37b53f --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: identity-me-cloud diff --git a/acceptance/bundle/validate/identity-me-cloud/out.test.toml b/acceptance/bundle/validate/identity-me-cloud/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/identity-me-cloud/output.txt b/acceptance/bundle/validate/identity-me-cloud/output.txt new file mode 100644 index 00000000000..3599e606bbe --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/output.txt @@ -0,0 +1,3 @@ + +=== bundle validate reads the current user's groups +"array" diff --git a/acceptance/bundle/validate/identity-me-cloud/script b/acceptance/bundle/validate/identity-me-cloud/script new file mode 100644 index 00000000000..0032af02956 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/script @@ -0,0 +1,5 @@ +title "bundle validate reads the current user's groups" +err=$(mktemp) +$CLI bundle validate -o json 2>"$err" | jq '.workspace.current_user.groups | type' +grep -q "Could not read the groups" "$err" && { echo "unexpected groups warning"; exit 1; } || true +rm -f "$err" diff --git a/acceptance/bundle/validate/identity-me-cloud/test.toml b/acceptance/bundle/validate/identity-me-cloud/test.toml new file mode 100644 index 00000000000..e5f6c883ff9 --- /dev/null +++ b/acceptance/bundle/validate/identity-me-cloud/test.toml @@ -0,0 +1,11 @@ +# Runs on a real workspace too: the groups of the caller are read from the account group +# list there. The stubs below give the local run a membership to read; cloud runs ignore them. +Cloud = true + +[[Server]] +Pattern = "GET /api/2.0/identity/me/transitive-parent-groups" +Response.Body = '{"me_transitive_parent_groups": [{"group_id": "42"}]}' + +[[Server]] +Pattern = "GET /api/2.0/identity/groups" +Response.Body = '{"groups": [{"group_id": "42", "group_name": "data-engineers"}]}' diff --git a/acceptance/bundle/validate/immutable_workspace_paths/output.txt b/acceptance/bundle/validate/immutable_workspace_paths/output.txt index e7ed88a8087..2246eb4323c 100644 --- a/acceptance/bundle/validate/immutable_workspace_paths/output.txt +++ b/acceptance/bundle/validate/immutable_workspace_paths/output.txt @@ -4,7 +4,14 @@ "workspace": { "artifact_path": "${resources.internal_immutable_snapshots.immutable.full_path}/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/job-references/output.txt b/acceptance/bundle/validate/job-references/output.txt index 8f88ccee391..1dbb5f82d9f 100644 --- a/acceptance/bundle/validate/job-references/output.txt +++ b/acceptance/bundle/validate/job-references/output.txt @@ -55,7 +55,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/foobar/default/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json index 258b6b938ab..21a5ccc17ed 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json @@ -76,7 +76,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json index 258b6b938ab..21a5ccc17ed 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json @@ -76,7 +76,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json index 3fe9aeea34a..bc5dc30ad30 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json @@ -70,7 +70,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json index 258b6b938ab..21a5ccc17ed 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json @@ -76,7 +76,14 @@ "workspace": { "artifact_path": "/Workspace/Users/[USERNAME]/.bundle/BUNDLE/dev/artifacts", "current_user": { + "active": true, "domain_friendly_name": "[USERNAME]", + "emails": [ + { + "primary": true, + "value": "[USERNAME]" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml b/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml index 55541c52a5d..8a921507856 100644 --- a/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml +++ b/acceptance/cmd/auth/describe/account-host-with-workspace-id/test.toml @@ -5,7 +5,7 @@ Ignore = [ # Simulate an account console host: it cannot serve workspace APIs, so the # workspace-side verification call fails even though the credentials are valid. [[Server]] -Pattern = "GET /api/2.0/preview/scim/v2/Me" +Pattern = "GET /api/2.0/identity/me" Response.Body = 'Unable to load OAuth Config' Response.StatusCode = 400 diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml b/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml new file mode 100644 index 00000000000..c7c6f58ed6e --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/out.test.toml @@ -0,0 +1 @@ +Cloud = true diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/output.txt b/acceptance/cmd/auth/describe/identity-me-cloud/output.txt new file mode 100644 index 00000000000..27a03fe6a21 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/output.txt @@ -0,0 +1,8 @@ + +=== auth describe resolves the current user through identity/me + +>>> [CLI] auth describe -o json +{ + "status": "success", + "username": "[USERNAME]" +} diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/script b/acceptance/cmd/auth/describe/identity-me-cloud/script new file mode 100644 index 00000000000..39590e7c461 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/script @@ -0,0 +1,2 @@ +title "auth describe resolves the current user through identity/me" +trace $CLI auth describe -o json | jq '{status, username}' diff --git a/acceptance/cmd/auth/describe/identity-me-cloud/test.toml b/acceptance/cmd/auth/describe/identity-me-cloud/test.toml new file mode 100644 index 00000000000..c585363eb38 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-cloud/test.toml @@ -0,0 +1,6 @@ +# Runs on a real workspace too: auth describe must resolve the caller through +# identity/me there, not only against the testserver. +Cloud = true +Ignore = [ + "home" +] diff --git a/acceptance/cmd/auth/describe/identity-me-group/out.test.toml b/acceptance/cmd/auth/describe/identity-me-group/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/auth/describe/identity-me-group/output.txt b/acceptance/cmd/auth/describe/identity-me-group/output.txt new file mode 100644 index 00000000000..4076eb6af18 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/output.txt @@ -0,0 +1,18 @@ + +=== Describe succeeds with an assumed group and shows the group as the user + +>>> [CLI] auth describe +Host: [DATABRICKS_URL] +User: data-engineers +Authenticated with: pat +----- +Current configuration: + ✓ host: [DATABRICKS_URL] (from DATABRICKS_HOST environment variable) + ✓ workspace_id: [WORKSPACE_ID] + ✓ token: ******** (from DATABRICKS_TOKEN environment variable) + ✓ profile: default + ✓ databricks_cli_path: [CLI] + ✓ auth_type: pat + ✓ rate_limit: 1000000000 (from DATABRICKS_RATE_LIMIT environment variable) + ✓ cloud: AWS + ✓ discovery_url: [DATABRICKS_URL]/oidc/.well-known/oauth-authorization-server diff --git a/acceptance/cmd/auth/describe/identity-me-group/script b/acceptance/cmd/auth/describe/identity-me-group/script new file mode 100644 index 00000000000..0cd8e6235a1 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/script @@ -0,0 +1,2 @@ +title "Describe succeeds with an assumed group and shows the group as the user" +trace $CLI auth describe diff --git a/acceptance/cmd/auth/describe/identity-me-group/test.toml b/acceptance/cmd/auth/describe/identity-me-group/test.toml new file mode 100644 index 00000000000..52106194401 --- /dev/null +++ b/acceptance/cmd/auth/describe/identity-me-group/test.toml @@ -0,0 +1,19 @@ +Ignore = [ + "home" +] + +# The caller is an assumed group, which identity/me returns as the principal. +[[Server]] +Pattern = "GET /api/2.0/identity/me" +Response.Body = ''' +{ + "name": "accounts/acct-123/me", + "group": { + "group_id": "42", + "group_name": "data-engineers", + "name": "accounts/acct-123/groups/42", + "parent": "accounts/acct-123" + }, + "identity_attributes": {} +} +''' diff --git a/acceptance/cmd/liteswap-target/output.txt b/acceptance/cmd/liteswap-target/output.txt index b8b59b78545..e20c89613b4 100644 --- a/acceptance/cmd/liteswap-target/output.txt +++ b/acceptance/cmd/liteswap-target/output.txt @@ -46,7 +46,7 @@ Workspace: Validation OK! ->>> print_requests.py --get //scim/v2/Me +>>> print_requests.py --get //identity/me { "headers": { "X-Databricks-Traffic-Id": [ @@ -54,7 +54,16 @@ Validation OK! ] }, "method": "GET", - "path": "/api/2.0/preview/scim/v2/Me" + "path": "/api/2.0/identity/me" +} +{ + "headers": { + "X-Databricks-Traffic-Id": [ + "testenv://liteswap/dms" + ] + }, + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" } === No header when unset diff --git a/acceptance/cmd/liteswap-target/script b/acceptance/cmd/liteswap-target/script index f69acefb698..c0003285c0c 100644 --- a/acceptance/cmd/liteswap-target/script +++ b/acceptance/cmd/liteswap-target/script @@ -8,7 +8,7 @@ trace print_requests.py --get //network-policies title "Bundle command" DATABRICKS_LITESWAP_TARGET=dms trace $CLI bundle validate -trace print_requests.py --get //scim/v2/Me +trace print_requests.py --get //identity/me title "No header when unset" trace $CLI clusters list diff --git a/acceptance/internal/prepare_server.go b/acceptance/internal/prepare_server.go index 6fca1f6f0ec..d33fd1ea1d4 100644 --- a/acceptance/internal/prepare_server.go +++ b/acceptance/internal/prepare_server.go @@ -216,7 +216,7 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o } // For the purposes of replacements, use testUser for local runs. - // Note, users might have overridden /api/2.0/preview/scim/v2/Me but that should not affect the replacement: + // Note, users might have overridden /api/2.0/identity/me, but that should not affect the replacement: return cfg, testUser, strconv.Itoa(testserver.TestWorkspaceID) } From c476b418fff46f2317248b6e66f78062fc7a4396 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Mon, 16 Nov 2026 16:51:01 +0000 Subject: [PATCH 07/23] changelog: note identity/me for the current user Names the cloud-enabled acceptance tests so the PR's cloud run covers the auth describe path, bundle validate's group read, and two existing bundle tests that read the current user. cloud: TestAccept/cmd/auth/describe/identity-me-cloud TestAccept/bundle/validate/identity-me-cloud TestAccept/bundle/resources/models/basic TestAccept/bundle/local_state_staleness Co-authored-by: Isaac --- .nextchanges/bundles/7714.md | 1 + .nextchanges/cli/7714.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 .nextchanges/bundles/7714.md create mode 100644 .nextchanges/cli/7714.md diff --git a/.nextchanges/bundles/7714.md b/.nextchanges/bundles/7714.md new file mode 100644 index 00000000000..6a522c34b3e --- /dev/null +++ b/.nextchanges/bundles/7714.md @@ -0,0 +1 @@ +* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements` in their output. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/cli/7714.md b/.nextchanges/cli/7714.md new file mode 100644 index 00000000000..4a3297d1b47 --- /dev/null +++ b/.nextchanges/cli/7714.md @@ -0,0 +1 @@ +* `databricks auth describe` and the current-user lookups now work when the caller is an assumed group. ([#7714](https://github.com/databricks/cli/pull/7714)) From 2d0673c28cb9df9ab1a193b38cbd363e5605aed5 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 10:59:31 +0000 Subject: [PATCH 08/23] iamutil: name parent groups from identity/me The parent-groups response already names each group, including workspace-local groups such as admins and users. Reading names from the account group list missed workspace-local groups, so a CAN_MANAGE grant to them did not match. Current user groups now come from that one paged response, with no account group list read. Co-authored-by: Isaac --- .../resources/jobs/request-summary/output.txt | 4 - .../target-is-passed/default/output.txt | 9 -- .../target-is-passed/from_flag/output.txt | 9 -- .../profile-is-passed/from_flag/output.txt | 9 -- .../target-is-passed/default/output.txt | 18 --- .../target-is-passed/from_flag/output.txt | 18 --- .../simple/out.requests.deploy.json | 9 -- .../simple/out.requests.destroy.json | 9 -- .../user_agent/simple/out.requests.plan.json | 9 -- .../user_agent/simple/out.requests.plan2.json | 9 -- .../user_agent/simple/out.requests.run.json | 9 -- .../simple/out.requests.summary.json | 9 -- .../simple/out.requests.validate.json | 9 -- .../validate/identity-me-cloud/test.toml | 10 +- .../workspace-local-groups/databricks.yml | 8 ++ .../workspace-local-groups/out.test.toml | 2 + .../workspace-local-groups/output.txt | 15 +++ .../validate/workspace-local-groups/script | 2 + .../validate/workspace-local-groups/test.toml | 5 + .../mutator/populate_current_user_test.go | 20 ++- .../permission_diagnostics_test.go | 54 ++++---- libs/iamutil/current_user.go | 118 +++++++++++------- libs/iamutil/current_user_test.go | 94 +++++++------- libs/testserver/groups.go | 46 +------ libs/testserver/handlers.go | 9 -- 25 files changed, 206 insertions(+), 307 deletions(-) create mode 100644 acceptance/bundle/validate/workspace-local-groups/databricks.yml create mode 100644 acceptance/bundle/validate/workspace-local-groups/out.test.toml create mode 100644 acceptance/bundle/validate/workspace-local-groups/output.txt create mode 100644 acceptance/bundle/validate/workspace-local-groups/script create mode 100644 acceptance/bundle/validate/workspace-local-groups/test.toml diff --git a/acceptance/bundle/resources/jobs/request-summary/output.txt b/acceptance/bundle/resources/jobs/request-summary/output.txt index b1f6fb5e4a6..e8f18926229 100644 --- a/acceptance/bundle/resources/jobs/request-summary/output.txt +++ b/acceptance/bundle/resources/jobs/request-summary/output.txt @@ -6,7 +6,6 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> print_requests GET /.well-known/databricks-config -GET /api/2.0/identity/groups GET /api/2.0/identity/me GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json @@ -20,7 +19,6 @@ Files: 1 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged >>> print_requests -GET /api/2.0/identity/groups GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock @@ -51,7 +49,6 @@ Files: 0 uploaded, 0 deleted Resources: 0 created, 0 changed, 0 deleted, 1 unchanged >>> print_requests -GET /api/2.0/identity/groups GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock @@ -80,7 +77,6 @@ All files and directories at the following location will be deleted: /Workspace/ Destroy: 1 deleted >>> print_requests -GET /api/2.0/identity/groups GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/resources.json diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt index f23e3763f62..df938243ff7 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/default/output.txt @@ -25,15 +25,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer [DATABRICKS_TOKEN]" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "Authorization": [ diff --git a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt index cb7b726acf8..9162d8cd412 100644 --- a/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/inline-script/databricks-cli/target-is-passed/from_flag/output.txt @@ -53,15 +53,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer oauth-token" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "Authorization": [ diff --git a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt index 55929e120f9..937c71a3259 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/profile-is-passed/from_flag/output.txt @@ -42,15 +42,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer oauth-token" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "method": "GET", "path": "/.well-known/databricks-config" diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt index 80c30b1d6e4..78850a81445 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/default/output.txt @@ -25,15 +25,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer [DATABRICKS_TOKEN]" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "method": "GET", "path": "/.well-known/databricks-config" @@ -56,15 +47,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer [DATABRICKS_TOKEN]" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "Authorization": [ diff --git a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt index baeff49be46..ef0b8f0eb7d 100644 --- a/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt +++ b/acceptance/bundle/run/scripts/databricks-cli/target-is-passed/from_flag/output.txt @@ -39,15 +39,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer oauth-token" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "method": "GET", "path": "/.well-known/databricks-config" @@ -84,15 +75,6 @@ "method": "GET", "path": "/api/2.0/identity/me/transitive-parent-groups" } -{ - "headers": { - "Authorization": [ - "Bearer oauth-token" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "Authorization": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.deploy.json b/acceptance/bundle/user_agent/simple/out.requests.deploy.json index 7ad2f901434..a9c06951182 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.deploy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.deploy.json @@ -327,15 +327,6 @@ "name": "myschema" } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.destroy.json b/acceptance/bundle/user_agent/simple/out.requests.destroy.json index 0e59e40efe3..a20a1f02458 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.destroy.json +++ b/acceptance/bundle/user_agent/simple/out.requests.destroy.json @@ -157,15 +157,6 @@ "recursive": true } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan.json b/acceptance/bundle/user_agent/simple/out.requests.plan.json index f011777f301..22cbb623838 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan.json @@ -55,15 +55,6 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deployment.json" } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.plan2.json b/acceptance/bundle/user_agent/simple/out.requests.plan2.json index 6fe827d5469..92477634970 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.plan2.json +++ b/acceptance/bundle/user_agent/simple/out.requests.plan2.json @@ -64,15 +64,6 @@ "method": "GET", "path": "/api/2.1/unity-catalog/schemas/mycatalog.myschema" } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.run.json b/acceptance/bundle/user_agent/simple/out.requests.run.json index aeaab13122b..d90309aca07 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.run.json +++ b/acceptance/bundle/user_agent/simple/out.requests.run.json @@ -42,15 +42,6 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/terraform.tfstate" } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.summary.json b/acceptance/bundle/user_agent/simple/out.requests.summary.json index 073c9804cda..d159019477c 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.summary.json +++ b/acceptance/bundle/user_agent/simple/out.requests.summary.json @@ -54,15 +54,6 @@ "excludedAttributes": "entitlements" } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/user_agent/simple/out.requests.validate.json b/acceptance/bundle/user_agent/simple/out.requests.validate.json index 197b4bcce56..514e222fb97 100644 --- a/acceptance/bundle/user_agent/simple/out.requests.validate.json +++ b/acceptance/bundle/user_agent/simple/out.requests.validate.json @@ -52,15 +52,6 @@ "path": "/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files" } } -{ - "headers": { - "User-Agent": [ - "cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat" - ] - }, - "method": "GET", - "path": "/api/2.0/identity/groups" -} { "headers": { "User-Agent": [ diff --git a/acceptance/bundle/validate/identity-me-cloud/test.toml b/acceptance/bundle/validate/identity-me-cloud/test.toml index e5f6c883ff9..77cfc1b6619 100644 --- a/acceptance/bundle/validate/identity-me-cloud/test.toml +++ b/acceptance/bundle/validate/identity-me-cloud/test.toml @@ -1,11 +1,7 @@ -# Runs on a real workspace too: the groups of the caller are read from the account group -# list there. The stubs below give the local run a membership to read; cloud runs ignore them. +# Runs on a real workspace too: the groups of the caller are read from the parent-groups +# list there. The stub below gives the local run a membership to read; cloud runs ignore it. Cloud = true [[Server]] Pattern = "GET /api/2.0/identity/me/transitive-parent-groups" -Response.Body = '{"me_transitive_parent_groups": [{"group_id": "42"}]}' - -[[Server]] -Pattern = "GET /api/2.0/identity/groups" -Response.Body = '{"groups": [{"group_id": "42", "group_name": "data-engineers"}]}' +Response.Body = '{"me_transitive_parent_groups": [{"group_id": "42", "group_name": "data-engineers"}]}' diff --git a/acceptance/bundle/validate/workspace-local-groups/databricks.yml b/acceptance/bundle/validate/workspace-local-groups/databricks.yml new file mode 100644 index 00000000000..dda6e6d0f88 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/databricks.yml @@ -0,0 +1,8 @@ +bundle: + name: workspace-local-groups + +permissions: + - group_name: admins + level: CAN_MANAGE + - group_name: users + level: CAN_VIEW diff --git a/acceptance/bundle/validate/workspace-local-groups/out.test.toml b/acceptance/bundle/validate/workspace-local-groups/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/workspace-local-groups/output.txt b/acceptance/bundle/validate/workspace-local-groups/output.txt new file mode 100644 index 00000000000..22baf404710 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/output.txt @@ -0,0 +1,15 @@ + +>>> [CLI] bundle validate -o json +[ + "admins", + "users" +] + +>>> [CLI] bundle validate +Name: workspace-local-groups +Target: default +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/workspace-local-groups/default + +Validation OK! diff --git a/acceptance/bundle/validate/workspace-local-groups/script b/acceptance/bundle/validate/workspace-local-groups/script new file mode 100644 index 00000000000..48f8ee0d0c3 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/script @@ -0,0 +1,2 @@ +trace $CLI bundle validate -o json | jq '.workspace.current_user.groups | map(.display)' +trace $CLI bundle validate diff --git a/acceptance/bundle/validate/workspace-local-groups/test.toml b/acceptance/bundle/validate/workspace-local-groups/test.toml new file mode 100644 index 00000000000..6d37644a704 --- /dev/null +++ b/acceptance/bundle/validate/workspace-local-groups/test.toml @@ -0,0 +1,5 @@ +# The current user belongs to the workspace-local groups admins and users. The +# parent-groups response names them, so their CAN_MANAGE grant counts as manageable. +[[Server]] +Pattern = "GET /api/2.0/identity/me/transitive-parent-groups" +Response.Body = '{"me_transitive_parent_groups": [{"group_id": "1", "group_name": "admins"}, {"group_id": "2", "group_name": "users"}]}' diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index eeffb0539ce..d4e597562f0 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -40,10 +40,24 @@ func TestPopulateCurrentUserSetsGroups(t *testing.T) { assert.Equal(t, "zeta", b.Config.Workspace.CurrentUser.Groups[1].Display) } -// A failed group list leaves the groups empty and warns, and the current user is still set. -func TestPopulateCurrentUserWarnsWhenGroupListFails(t *testing.T) { +// Workspace-local groups are named by the parent-groups response, so admins and users +// resolve like account groups. +func TestPopulateCurrentUserSetsWorkspaceLocalGroups(t *testing.T) { + b := newPopulateBundle(t, testserver.New(t), "admins", "users") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + var names []string + for _, g := range b.Config.Workspace.CurrentUser.Groups { + names = append(names, g.Display) + } + assert.Equal(t, []string{"admins", "users"}, names) +} + +// A failed parent-groups read leaves the groups empty and warns, and the current user is still set. +func TestPopulateCurrentUserWarnsWhenGroupsFail(t *testing.T) { server := testserver.New(t) - server.Handle("GET", "/api/2.0/identity/groups", func(testserver.Request) any { + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(testserver.Request) any { return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} }) b := newPopulateBundle(t, server, "testgroup") diff --git a/bundle/permissions/permission_diagnostics_test.go b/bundle/permissions/permission_diagnostics_test.go index d368b032f52..eaae7cfaa0b 100644 --- a/bundle/permissions/permission_diagnostics_test.go +++ b/bundle/permissions/permission_diagnostics_test.go @@ -53,10 +53,9 @@ func TestPermissionDiagnosticsApplyFail(t *testing.T) { require.Contains(t, diags[0].Summary, expectedMsg) } -// groupCalls counts the group requests the workspace serves. -type groupCalls struct { - list atomic.Int32 // GET /api/2.0/identity/groups - get atomic.Int32 // GET /api/2.0/identity/groups/{group_id} +// parentGroupCalls counts the reads of the current user's parent groups. +type parentGroupCalls struct { + parents atomic.Int32 // GET /api/2.0/identity/me/transitive-parent-groups } // mockBundle returns a bundle whose workspace has one group, testgroup, that the @@ -69,20 +68,16 @@ func mockBundle(t *testing.T, permissions []resources.Permission) *bundle.Bundle // mockBundleWithGroups returns a bundle whose workspace creates the groups in member, // which the current user belongs to, and the groups in nonMember, which it does not. // The bundle's current user carries the groups resolved from the membership, as the -// populate mutator would set them. The returned counts cover only the group requests -// the bundle makes after this call returns. -func mockBundleWithGroups(t *testing.T, permissions []resources.Permission, member, nonMember []string) (*bundle.Bundle, *groupCalls) { +// populate mutator would set them. The returned counts cover only the parent-group +// reads the bundle makes after this call returns. +func mockBundleWithGroups(t *testing.T, permissions []resources.Permission, member, nonMember []string) (*bundle.Bundle, *parentGroupCalls) { server := testserver.New(t) - calls := &groupCalls{} - // Handlers registered before AddDefaultHandlers take precedence, so they count and - // then delegate to the fake. - server.Handle("GET", "/api/2.0/identity/groups", func(req testserver.Request) any { - calls.list.Add(1) - return req.Workspace.IdentityGroups(0, req.URL.Query().Get("page_token")) - }) - server.Handle("GET", "/api/2.0/identity/groups/{group_id}", func(req testserver.Request) any { - calls.get.Add(1) - return req.Workspace.IdentityGroup(req.Vars["group_id"]) + calls := &parentGroupCalls{} + // Handlers registered before AddDefaultHandlers take precedence, so this one counts + // and then delegates to the fake. + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req testserver.Request) any { + calls.parents.Add(1) + return req.Workspace.IdentityMeTransitiveParentGroups() }) testserver.AddDefaultHandlers(server) server.AddGroups("test-token", member, nonMember) @@ -90,8 +85,7 @@ func mockBundleWithGroups(t *testing.T, permissions []resources.Permission, memb require.NoError(t, err) groups, err := iamutil.CurrentUserGroups(t.Context(), w) require.NoError(t, err) - calls.list.Store(0) - calls.get.Store(0) + calls.parents.Store(0) b := &bundle.Bundle{ Config: config.Root{ @@ -155,7 +149,8 @@ func TestPermissionDiagnosticsGroupMember(t *testing.T) { } // Matching a configured group is a comparison with the groups already on the current -// user, so a report makes no group request, whatever the number of configured groups. +// user, so a report reads no group from the workspace, whatever the number of +// configured groups. func TestPermissionDiagnosticsMembershipMakesNoGroupRequests(t *testing.T) { b, calls := mockBundleWithGroups(t, []resources.Permission{ {Level: "CAN_MANAGE", GroupName: "testgroup"}, @@ -164,8 +159,23 @@ func TestPermissionDiagnosticsMembershipMakesNoGroupRequests(t *testing.T) { }, []string{"testgroup", "joined"}, []string{"otherteam"}) permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") - assert.Equal(t, int32(0), calls.list.Load()) - assert.Equal(t, int32(0), calls.get.Load()) + assert.Equal(t, int32(0), calls.parents.Load()) +} + +// Workspace-local groups are matched by name like account groups: a CAN_MANAGE grant +// to admins or users that the current user belongs to counts as manageable. +func TestPermissionDiagnosticsWorkspaceLocalGroupGrantsCanManage(t *testing.T) { + for _, name := range []string{"admins", "users"} { + t.Run(name, func(t *testing.T) { + b, _ := mockBundleWithGroups(t, []resources.Permission{ + {Level: "CAN_MANAGE", GroupName: name}, + }, []string{name}, nil) + + require.Empty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) + diags := permissions.ReportPossiblePermissionDenied(t.Context(), b, "testpath") + require.Equal(t, diag.CannotChangePathPermissions, diags[0].ID) + }) + } } // A user whose groups were not read, for example because the groups list failed, has diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index e3a6c9844f8..7ff1514b2e4 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -9,7 +9,6 @@ import ( "time" "github.com/databricks/cli/libs/auth" - "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" databricksclient "github.com/databricks/databricks-sdk-go/client" @@ -26,6 +25,10 @@ const ( identityMeTransitiveParentGroupsPath = "/api/2.0/identity/me/transitive-parent-groups" ) +// ErrGroupPrincipal is returned by GetUserPrincipal when the current request is +// authorized by an assumed group, which has no user identity to deploy as. +var ErrGroupPrincipal = errors.New("the current identity is an assumed group, but this command needs a user or service principal") + // identityMe is the principal returned by GET /api/2.0/identity/me. Exactly one // of the three fields is set. Groups and parent groups are not returned inline. type identityMe struct { @@ -34,6 +37,12 @@ type identityMe struct { ServicePrincipal *iamv2.ServicePrincipal `json:"service_principal,omitempty"` } +// currentPrincipal is identity/me as an iam.User, with group set for an assumed group. +type currentPrincipal struct { + user *iam.User + group bool +} + // GetCurrentUser returns the authorizing principal of the current request as an // iam.User, read from GET /api/2.0/identity/me, which also resolves an assumed group. // Groups, Roles and Entitlements are not set; CurrentUserGroups reads the groups. @@ -41,10 +50,43 @@ type identityMe struct { // The call retries HTTP 500 responses for up to meRetryTimeout. It runs at the // start of most CLI commands, and an unretried 500 would fail the whole command. func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { - return retries.Poll(ctx, meRetryTimeout, func() (*iam.User, *retries.Err) { - user, err := getIdentityMe(ctx, w) + p, err := pollIdentityMe(ctx, w) + if err != nil { + return nil, err + } + return p.user, nil +} + +// GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should +// fail fast rather than wait out transient 500s. +func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := getIdentityMe(ctx, w) + if err != nil { + return nil, err + } + return p.user, nil +} + +// GetUserPrincipal is GetCurrentUser for commands that act as the current user, such +// as bundle deployments and paths under /Workspace/Users. It returns ErrGroupPrincipal +// when the caller is an assumed group. +func GetUserPrincipal(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := pollIdentityMe(ctx, w) + if err != nil { + return nil, err + } + if p.group { + return nil, ErrGroupPrincipal + } + return p.user, nil +} + +// pollIdentityMe is getIdentityMe that retries HTTP 500 responses for meRetryTimeout. +func pollIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { + return retries.Poll(ctx, meRetryTimeout, func() (*currentPrincipal, *retries.Err) { + p, err := getIdentityMe(ctx, w) if err == nil { - return user, nil + return p, nil } if apiErr, ok := errors.AsType[*apierr.APIError](err); ok && apiErr.StatusCode == http.StatusInternalServerError { return nil, retries.Continue(err) @@ -53,12 +95,6 @@ func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.Us }) } -// GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should -// fail fast rather than wait out transient 500s. -func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { - return getIdentityMe(ctx, w) -} - // identityGet sends a GET to path with the workspace's auth headers and decodes the // response into out. func identityGet(ctx context.Context, w *databricks.WorkspaceClient, path string, query map[string]any, out any) error { @@ -69,12 +105,16 @@ func identityGet(ctx context.Context, w *databricks.WorkspaceClient, path string return c.Do(ctx, http.MethodGet, path, auth.WorkspaceIDHeaders(w.Config), query, nil, out) } -func getIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { +func getIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { var me identityMe if err := identityGet(ctx, w, identityMePath, nil, &me); err != nil { return nil, err } - return meToUser(&me) + user, err := meToUser(&me) + if err != nil { + return nil, err + } + return ¤tPrincipal{user: user, group: me.Group != nil}, nil } // meToUser maps a principal from identity/me onto an iam.User, setting Id, UserName, @@ -113,12 +153,18 @@ func meToUser(me *identityMe) (*iam.User, error) { } } -// listCurrentUserParentGroupIDs returns the IDs of the groups the current principal -// belongs to, directly or through parent groups, following every page. The keys follow -// ListMeTransitiveParentGroupsResponse in the IAM v2 proto, not the generic SDK response, -// which decodes a different key. -func listCurrentUserParentGroupIDs(ctx context.Context, w *databricks.WorkspaceClient) ([]string, error) { - var ids []string +// parentGroup is one entry of ListMeTransitiveParentGroupsResponse. The keys follow the +// IAM v2 proto, not the generic SDK response, which decodes a different key. +type parentGroup struct { + GroupId string `json:"group_id"` + GroupName string `json:"group_name"` +} + +// listCurrentUserParentGroups returns the groups the current principal belongs to, +// directly or through parent groups, following every page. The response also names +// workspace-local groups such as admins and users, which the account group list lacks. +func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]parentGroup, error) { + var groups []parentGroup pageToken := "" for { query := map[string]any{} @@ -126,19 +172,15 @@ func listCurrentUserParentGroupIDs(ctx context.Context, w *databricks.WorkspaceC query["page_token"] = pageToken } var page struct { - Groups []struct { - GroupId string `json:"group_id"` - } `json:"me_transitive_parent_groups"` - NextPageToken string `json:"next_page_token,omitempty"` + Groups []parentGroup `json:"me_transitive_parent_groups"` + NextPageToken string `json:"next_page_token,omitempty"` } if err := identityGet(ctx, w, identityMeTransitiveParentGroupsPath, query, &page); err != nil { return nil, err } - for _, g := range page.Groups { - ids = append(ids, g.GroupId) - } + groups = append(groups, page.Groups...) if page.NextPageToken == "" { - return ids, nil + return groups, nil } pageToken = page.NextPageToken } @@ -146,30 +188,16 @@ func listCurrentUserParentGroupIDs(ctx context.Context, w *databricks.WorkspaceC // CurrentUserGroups returns the groups the current principal belongs to, directly or // through parent groups, as iam.ComplexValue entries with the group ID as Value and the -// group name as Display, sorted by Display. Names come from one list of all account -// groups, so the cost is one list call per 1000 groups rather than one lookup per group. -// It fails when either call fails, so a caller never sees a partial list as complete. +// group name as Display, sorted by Display. It is one paged call and never reads the +// account group list. func CurrentUserGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]iam.ComplexValue, error) { - ids, err := listCurrentUserParentGroupIDs(ctx, w) + parents, err := listCurrentUserParentGroups(ctx, w) if err != nil { return nil, err } - all, err := w.WorkspaceIamV2.ListGroupsProxyAll(ctx, iamv2.ListGroupsProxyRequest{}) - if err != nil { - return nil, err - } - names := make(map[string]string, len(all)) - for _, g := range all { - names[g.GroupId] = g.GroupName - } var groups []iam.ComplexValue - for _, id := range ids { - name, ok := names[id] - if !ok { - log.Debugf(ctx, "Group %s of the current user is not in the account group list; skipping it", id) - continue - } - groups = append(groups, iam.ComplexValue{Value: id, Display: name}) + for _, g := range parents { + groups = append(groups, iam.ComplexValue{Value: g.GroupId, Display: g.GroupName}) } slices.SortFunc(groups, func(a, b iam.ComplexValue) int { return strings.Compare(a.Display, b.Display) diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index 4c3ee6af913..40b94202b17 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -138,7 +138,7 @@ func TestGetCurrentUserDoesNotRetryOn403(t *testing.T) { assert.Equal(t, int32(1), calls.Load()) } -func TestListCurrentUserParentGroupIDsFollowsPages(t *testing.T) { +func TestListCurrentUserParentGroupsFollowsPages(t *testing.T) { var pages []string server := testserver.New(t) t.Cleanup(server.Close) @@ -148,20 +148,24 @@ func TestListCurrentUserParentGroupIDsFollowsPages(t *testing.T) { // The fixture bodies use the proto keys; a wrong key decodes to no groups. if token == "" { return map[string]any{ - "me_transitive_parent_groups": []map[string]any{{"group_id": "11"}, {"group_id": "12"}}, + "me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}, {"group_id": "12", "group_name": "ops"}}, "next_page_token": "page2", } } return map[string]any{ - "me_transitive_parent_groups": []map[string]any{{"group_id": "13"}}, + "me_transitive_parent_groups": []map[string]any{{"group_id": "13", "group_name": "users"}}, } }) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) require.NoError(t, err) - ids, err := listCurrentUserParentGroupIDs(t.Context(), w) + groups, err := listCurrentUserParentGroups(t.Context(), w) require.NoError(t, err) - assert.Equal(t, []string{"11", "12", "13"}, ids) + assert.Equal(t, []parentGroup{ + {GroupId: "11", GroupName: "eng"}, + {GroupId: "12", GroupName: "ops"}, + {GroupId: "13", GroupName: "users"}, + }, groups) assert.Equal(t, []string{"", "page2"}, pages) } @@ -180,42 +184,17 @@ func TestCurrentUserGroupsResolvesNamesSorted(t *testing.T) { assert.NotEmpty(t, groups[0].Value) } -// A parent ID that is missing from the account group list is skipped, not reported as -// an error: the other memberships are still known. -func TestCurrentUserGroupsSkipsParentIDMissingFromAccountList(t *testing.T) { - server := testserver.New(t) - testserver.AddDefaultHandlers(server) - w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) - require.NoError(t, err) - server.AddGroups("test-token", []string{"testgroup"}, nil) - server.JoinGroup("test-token", "missing-group") - - groups, err := CurrentUserGroups(t.Context(), w) - require.NoError(t, err) - require.Len(t, groups, 1) - assert.Equal(t, "testgroup", groups[0].Display) - assert.Equal(t, "testgroup", groups[0].Display) -} - -// The account group list is paged; every page is read, and names are matched by ID -// across pages. -func TestCurrentUserGroupsReadsEveryPageOfGroupList(t *testing.T) { - var pages []string +// Workspace-local groups such as admins and users are named by the parent-groups +// response itself, so they resolve without any account group read. +func TestCurrentUserGroupsIncludesWorkspaceLocalGroups(t *testing.T) { server := testserver.New(t) t.Cleanup(server.Close) server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { - return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11"}, {"group_id": "12"}, {"group_id": "13"}}} - }) - server.Handle("GET", "/api/2.0/identity/groups", func(req testserver.Request) any { - token := req.URL.Query().Get("page_token") - pages = append(pages, token) - if token == "" { - return map[string]any{ - "groups": []map[string]any{{"group_id": "11", "group_name": "b"}, {"group_id": "99", "group_name": "unrelated"}}, - "next_page_token": "page2", - } - } - return map[string]any{"groups": []map[string]any{{"group_id": "12", "group_name": "a"}, {"group_id": "13", "group_name": "c"}}} + return map[string]any{"me_transitive_parent_groups": []map[string]any{ + {"group_id": "86546749069356", "group_name": "users"}, + {"group_id": "79621542766305", "group_name": "account users"}, + {"group_id": "90000000000001", "group_name": "admins"}, + }} }) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) require.NoError(t, err) @@ -223,22 +202,18 @@ func TestCurrentUserGroupsReadsEveryPageOfGroupList(t *testing.T) { groups, err := CurrentUserGroups(t.Context(), w) require.NoError(t, err) assert.Equal(t, []iam.ComplexValue{ - {Value: "12", Display: "a"}, - {Value: "11", Display: "b"}, - {Value: "13", Display: "c"}, + {Value: "79621542766305", Display: "account users"}, + {Value: "90000000000001", Display: "admins"}, + {Value: "86546749069356", Display: "users"}, }, groups) - assert.Equal(t, []string{"", "page2"}, pages) } -// A failed account group list fails the lookup, so no partial list is returned as the +// A failed parent-groups read fails the lookup, so no partial list is returned as the // complete membership. -func TestCurrentUserGroupsFailsWhenGroupListFails(t *testing.T) { +func TestCurrentUserGroupsFailsWhenParentGroupsFail(t *testing.T) { server := testserver.New(t) t.Cleanup(server.Close) server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { - return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11"}}} - }) - server.Handle("GET", "/api/2.0/identity/groups", func(testserver.Request) any { return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} }) w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) @@ -248,3 +223,28 @@ func TestCurrentUserGroupsFailsWhenGroupListFails(t *testing.T) { require.ErrorContains(t, err, "denied") assert.Nil(t, groups) } + +func TestGetUserPrincipalRejectsAssumedGroup(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{Body: map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}}} + }) + + user, err := GetUserPrincipal(t.Context(), w) + require.ErrorIs(t, err, ErrGroupPrincipal) + assert.Nil(t, user) + + // Commands that only identify the caller still get the group. + group, err := GetCurrentUser(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "data-engineers", group.UserName) +} + +func TestGetUserPrincipalReturnsUser(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{Body: map[string]any{"user": map[string]any{"user_id": "1", "username": "test-user"}}} + }) + + user, err := GetUserPrincipal(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "test-user", user.UserName) +} diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index 197730893b1..b7c82e0ae7b 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "maps" - "net/http" "slices" "strconv" @@ -42,14 +41,14 @@ func (s *FakeWorkspace) GroupsCreate(req Request) Response { } // IdentityMeTransitiveParentGroups answers GET /api/2.0/identity/me/transitive-parent-groups -// with the groups the caller belongs to. +// with the groups the caller belongs to, named as the server names them. func (s *FakeWorkspace) IdentityMeTransitiveParentGroups() any { defer s.LockUnlock()() ids := slices.Sorted(maps.Keys(s.groupMembers)) parents := make([]map[string]any, 0, len(ids)) for _, id := range ids { - parents = append(parents, map[string]any{"group_id": id}) + parents = append(parents, map[string]any{"group_id": id, "group_name": s.Groups[id].DisplayName}) } return map[string]any{"me_transitive_parent_groups": parents} } @@ -79,44 +78,3 @@ func (s *Server) JoinGroup(token, groupID string) { defer ws.LockUnlock()() ws.groupMembers[groupID] = true } - -// IdentityGroup answers GET /api/2.0/identity/groups/{group_id}. -func (s *FakeWorkspace) IdentityGroup(groupID string) any { - defer s.LockUnlock()() - - group, ok := s.Groups[groupID] - if !ok { - return Response{ - StatusCode: http.StatusNotFound, - Body: map[string]any{"error_code": "RESOURCE_DOES_NOT_EXIST", "message": "Group " + groupID + " does not exist."}, - } - } - return map[string]any{ - "name": "groups/" + groupID, - "group_id": group.Id, - "group_name": group.DisplayName, - } -} - -// IdentityGroups answers GET /api/2.0/identity/groups, one page of all groups in -// the account. page_token is the offset of the next page, as the server's opaque token. -func (s *FakeWorkspace) IdentityGroups(pageSize int, pageToken string) any { - defer s.LockUnlock()() - - if pageSize <= 0 { - pageSize = 1000 - } - ids := slices.Sorted(maps.Keys(s.Groups)) - start, _ := strconv.Atoi(pageToken) - end := min(start+pageSize, len(ids)) - groups := make([]map[string]any, 0, end-start) - for _, id := range ids[start:end] { - group := s.Groups[id] - groups = append(groups, map[string]any{"group_id": group.Id, "group_name": group.DisplayName}) - } - resp := map[string]any{"groups": groups} - if end < len(ids) { - resp["next_page_token"] = strconv.Itoa(end) - } - return resp -} diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 61b4deeb781..a3580b815a0 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -259,15 +259,6 @@ func AddDefaultHandlers(server *Server) { return req.Workspace.IdentityMeTransitiveParentGroups() }) - server.Handle("GET", "/api/2.0/identity/groups", func(req Request) any { - pageSize, _ := strconv.Atoi(req.URL.Query().Get("page_size")) - return req.Workspace.IdentityGroups(pageSize, req.URL.Query().Get("page_token")) - }) - - server.Handle("GET", "/api/2.0/identity/groups/{group_id}", func(req Request) any { - return req.Workspace.IdentityGroup(req.Vars["group_id"]) - }) - server.Handle("GET", "/api/2.0/workspace/get-status", func(req Request) any { path := req.URL.Query().Get("path") returnGitInfo := req.URL.Query().Get("return_git_info") == "true" From 38651b1a41d493129fe814ccb9ae19467d8232bb Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 11:16:54 +0000 Subject: [PATCH 09/23] bundle: reject group principals and set the current user's name An assumed group has no user identity, so bundle commands fail early with a clear error instead of using the group name as a user name. air run uses the same check. Other commands, including auth describe, still identify the group. identity/me returns the given and family names, so current_user.user.name is set from them. Co-authored-by: Isaac --- .nextchanges/bundles/7714-group-principal.md | 1 + .../validate/group-principal/databricks.yml | 2 ++ .../validate/group-principal/out.test.toml | 2 ++ .../validate/group-principal/output.txt | 6 +++++ .../bundle/validate/group-principal/script | 1 + .../bundle/validate/group-principal/test.toml | 5 ++++ .../config/mutator/populate_current_user.go | 2 +- .../mutator/populate_current_user_test.go | 17 ++++++++++++ cmd/air/runlaunch.go | 3 ++- libs/iamutil/current_user.go | 26 ++++++++++++++----- libs/iamutil/current_user_test.go | 2 ++ 11 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 .nextchanges/bundles/7714-group-principal.md create mode 100644 acceptance/bundle/validate/group-principal/databricks.yml create mode 100644 acceptance/bundle/validate/group-principal/out.test.toml create mode 100644 acceptance/bundle/validate/group-principal/output.txt create mode 100644 acceptance/bundle/validate/group-principal/script create mode 100644 acceptance/bundle/validate/group-principal/test.toml diff --git a/.nextchanges/bundles/7714-group-principal.md b/.nextchanges/bundles/7714-group-principal.md new file mode 100644 index 00000000000..d6ea48d01d0 --- /dev/null +++ b/.nextchanges/bundles/7714-group-principal.md @@ -0,0 +1 @@ +* Bundle commands fail with a clear error when the caller is an assumed group, because they deploy as a user or service principal. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/acceptance/bundle/validate/group-principal/databricks.yml b/acceptance/bundle/validate/group-principal/databricks.yml new file mode 100644 index 00000000000..98a9ce6d2b2 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/databricks.yml @@ -0,0 +1,2 @@ +bundle: + name: group-principal diff --git a/acceptance/bundle/validate/group-principal/out.test.toml b/acceptance/bundle/validate/group-principal/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/group-principal/output.txt b/acceptance/bundle/validate/group-principal/output.txt new file mode 100644 index 00000000000..d3f20ba3dff --- /dev/null +++ b/acceptance/bundle/validate/group-principal/output.txt @@ -0,0 +1,6 @@ +Error: the current identity is an assumed group, but this command needs a user or service principal + +Name: group-principal +Target: default + +Found 1 error diff --git a/acceptance/bundle/validate/group-principal/script b/acceptance/bundle/validate/group-principal/script new file mode 100644 index 00000000000..26b424b480e --- /dev/null +++ b/acceptance/bundle/validate/group-principal/script @@ -0,0 +1 @@ +musterr $CLI bundle validate diff --git a/acceptance/bundle/validate/group-principal/test.toml b/acceptance/bundle/validate/group-principal/test.toml new file mode 100644 index 00000000000..3a70d6933e8 --- /dev/null +++ b/acceptance/bundle/validate/group-principal/test.toml @@ -0,0 +1,5 @@ +# The caller is an assumed group. Bundle commands act as a user, so validate fails +# early instead of using the group name as a user name. +[[Server]] +Pattern = "GET /api/2.0/identity/me" +Response.Body = '{"group": {"group_id": "42", "group_name": "data-engineers"}}' diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index 61c8d58ab2b..99cb4593630 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -32,7 +32,7 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. fingerprint := b.GetUserFingerprint(ctx) me, err := cache.GetOrCompute(ctx, b.Cache, fingerprint, func(ctx context.Context) (*iam.User, error) { - return iamutil.GetCurrentUser(ctx, w) + return iamutil.GetUserPrincipal(ctx, w) }) if err != nil { return diag.FromErr(err) diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index d4e597562f0..86edbb1ceb3 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -9,6 +9,7 @@ import ( "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/cache" "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/testserver" "github.com/databricks/databricks-sdk-go" "github.com/stretchr/testify/assert" @@ -69,3 +70,19 @@ func TestPopulateCurrentUserWarnsWhenGroupsFail(t *testing.T) { assert.Nil(t, b.Config.Workspace.CurrentUser.Groups) assert.Equal(t, "tester@databricks.com", b.Config.Workspace.CurrentUser.UserName) } + +// An assumed group is not a user: bundle commands that deploy as the current user fail +// early with a clear error rather than using the group name as a user name. +func TestPopulateCurrentUserRejectsGroupPrincipal(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}} + }) + b := newPopulateBundle(t, server) + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.Len(t, diags, 1) + assert.Equal(t, diag.Error, diags[0].Severity) + assert.Equal(t, iamutil.ErrGroupPrincipal.Error(), diags[0].Summary) + assert.Nil(t, b.Config.Workspace.CurrentUser) +} diff --git a/cmd/air/runlaunch.go b/cmd/air/runlaunch.go index a2288d21e16..3d65136b0ca 100644 --- a/cmd/air/runlaunch.go +++ b/cmd/air/runlaunch.go @@ -21,7 +21,8 @@ const userWorkspaceDirEnv = "DATABRICKS_INTERNAL_USER_WORKSPACE_DIR" // currentUserEmail returns the authenticated user's email (works for any domain). func currentUserEmail(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - me, err := iamutil.GetCurrentUser(ctx, w) + // Only userName is needed; the groups are read separately. + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", fmt.Errorf("failed to resolve current user: %w", err) } diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index 7ff1514b2e4..c58e5ef7c54 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -43,12 +43,17 @@ type currentPrincipal struct { group bool } -// GetCurrentUser returns the authorizing principal of the current request as an -// iam.User, read from GET /api/2.0/identity/me, which also resolves an assumed group. -// Groups, Roles and Entitlements are not set; CurrentUserGroups reads the groups. +// GetCurrentUser returns the current user, retrying on HTTP 500 responses. It reads the +// authorizing principal of the current request from GET /api/2.0/identity/me, which also +// resolves an assumed group. Groups, Roles and Entitlements are not set; CurrentUserGroups +// reads the groups. // -// The call retries HTTP 500 responses for up to meRetryTimeout. It runs at the -// start of most CLI commands, and an unretried 500 would fail the whole command. +// identity/me is a read-only endpoint, so retrying any 500 is safe. The Databricks SCIM +// API intermittently returns "500 TEMPORARILY_UNAVAILABLE" (a condition it otherwise +// serves as 503), and the SDK retries 503 but not 500 — see the "some API's recommend +// retries on HTTP 500, but we'll add that later" comment on (*apierr.APIError).IsRetriable +// in databricks-sdk-go/apierr. This call runs at the start of every bundle command, so an +// unretried 500 fails the whole command; we close that gap here. func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { p, err := pollIdentityMe(ctx, w) if err != nil { @@ -118,15 +123,22 @@ func getIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*current } // meToUser maps a principal from identity/me onto an iam.User, setting Id, UserName, -// DisplayName, Active, ExternalId and Emails. A service principal's UserName is its -// application ID, not its name. +// Name, DisplayName, Active, ExternalId and Emails. identity/me returns the given and +// family names separately, so Name is taken as is (nil when both are empty) and +// DisplayName joins them. A service principal's UserName is its application ID, not its +// name. func meToUser(me *identityMe) (*iam.User, error) { switch { case me.User != nil: u := me.User + var name *iam.Name + if u.FullName.GivenName != "" || u.FullName.FamilyName != "" { + name = &iam.Name{GivenName: u.FullName.GivenName, FamilyName: u.FullName.FamilyName} + } return &iam.User{ Id: u.UserId, UserName: u.Username, + Name: name, DisplayName: strings.TrimSpace(u.FullName.GivenName + " " + u.FullName.FamilyName), Active: u.AccountUserStatus == iamv2.StateActive, ExternalId: u.ExternalId, diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index 40b94202b17..d759f8f88cc 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -46,6 +46,7 @@ func TestMeToUser(t *testing.T) { want: &iam.User{ Id: "1000012345", UserName: "alice@example.com", + Name: &iam.Name{GivenName: "Alice", FamilyName: "Smith"}, DisplayName: "Alice Smith", Active: true, ExternalId: "ext-1", @@ -248,3 +249,4 @@ func TestGetUserPrincipalReturnsUser(t *testing.T) { require.NoError(t, err) assert.Equal(t, "test-user", user.UserName) } + From 4135519c09c0eb3b15690c4a194dc58167d70a95 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 11:16:55 +0000 Subject: [PATCH 10/23] cmd: probe the current user once in auth, air and sync These probes only decide a fast answer, so a transient 500 should fail them at once instead of waiting out the 30 second retry used for command startup. Co-authored-by: Isaac --- cmd/air/get.go | 2 +- cmd/auth/profiles.go | 2 +- cmd/sync/completion.go | 2 +- libs/iamutil/current_user_test.go | 12 ++++++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/air/get.go b/cmd/air/get.go index 3f78b97477a..7ce99bf7ea4 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -151,7 +151,7 @@ func credentialError(ctx context.Context, w *databricks.WorkspaceClient, err err if !errors.Is(err, apierr.ErrUnauthenticated) && !errors.Is(err, apierr.ErrPermissionDenied) { return nil } - _, meErr := iamutil.GetCurrentUser(ctx, w) + _, meErr := iamutil.GetCurrentUserOnce(ctx, w) return meErr } diff --git a/cmd/auth/profiles.go b/cmd/auth/profiles.go index 95af06f7b36..4da2cd9e088 100644 --- a/cmd/auth/profiles.go +++ b/cmd/auth/profiles.go @@ -110,7 +110,7 @@ func (c *profileMetadata) Load(ctx context.Context, configFilePath string, skipV if err != nil { return } - _, err = iamutil.GetCurrentUser(callCtx, w) + _, err = iamutil.GetCurrentUserOnce(callCtx, w) c.Host = cfg.Host c.AuthType = cfg.AuthType if err != nil { diff --git a/cmd/sync/completion.go b/cmd/sync/completion.go index 9f79c0892f6..f4090921188 100644 --- a/cmd/sync/completion.go +++ b/cmd/sync/completion.go @@ -45,7 +45,7 @@ func completeRemotePath( wsc *databricks.WorkspaceClient, toComplete string, ) ([]string, cobra.ShellCompDirective) { - me, err := iamutil.GetCurrentUser(ctx, wsc) + me, err := iamutil.GetCurrentUserOnce(ctx, wsc) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index d759f8f88cc..301f8f5c424 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -250,3 +250,15 @@ func TestGetUserPrincipalReturnsUser(t *testing.T) { assert.Equal(t, "test-user", user.UserName) } +// GetCurrentUserOnce makes a single attempt, so a probe fails fast on a transient 500. +func TestGetCurrentUserOnceDoesNotRetryOn500(t *testing.T) { + var calls atomic.Int32 + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + calls.Add(1) + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + }) + + _, err := GetCurrentUserOnce(t.Context(), w) + require.Error(t, err) + assert.Equal(t, int32(1), calls.Load()) +} From a6b9187d08fc25bf4123a25b12d22fb9ebd90a0b Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 11:16:57 +0000 Subject: [PATCH 11/23] bundle/permissions: drop the unused context from analyzeBundlePermissions Co-authored-by: Isaac --- bundle/permissions/permission_diagnostics.go | 4 ++-- bundle/permissions/permission_report.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bundle/permissions/permission_diagnostics.go b/bundle/permissions/permission_diagnostics.go index 027797c9905..ef193de7cab 100644 --- a/bundle/permissions/permission_diagnostics.go +++ b/bundle/permissions/permission_diagnostics.go @@ -35,7 +35,7 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia return nil } - canManageBundle, _ := analyzeBundlePermissions(ctx, b) + canManageBundle, _ := analyzeBundlePermissions(b) if canManageBundle { return nil } @@ -71,7 +71,7 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia // Returns: // - canManageBundle: true if the current user or one of their groups can manage the bundle resources. // - assistance: advice on who to contact as to manage this project -func analyzeBundlePermissions(ctx context.Context, b *bundle.Bundle) (bool, string) { +func analyzeBundlePermissions(b *bundle.Bundle) (bool, string) { canManageBundle := false otherManagers := set.NewSet[string]() if b.Config.RunAs != nil && b.Config.RunAs.UserName != "" && b.Config.RunAs.UserName != b.Config.Workspace.CurrentUser.UserName { diff --git a/bundle/permissions/permission_report.go b/bundle/permissions/permission_report.go index eb92dab1c61..36526eeeba9 100644 --- a/bundle/permissions/permission_report.go +++ b/bundle/permissions/permission_report.go @@ -22,7 +22,7 @@ func ReportPossiblePermissionDenied(ctx context.Context, b *bundle.Bundle, path if iamutil.IsServicePrincipal(me) { userName = me.DisplayName } - canManageBundle, assistance := analyzeBundlePermissions(ctx, b) + canManageBundle, assistance := analyzeBundlePermissions(b) if !canManageBundle { return diag.Diagnostics{{ From 0b79be33303031fe6e3a3ec72352b46dcd9113a5 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 11:16:58 +0000 Subject: [PATCH 12/23] acceptance: drop the removed groups list from the user agent golden Co-authored-by: Isaac --- acceptance/bundle/user_agent/output.txt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/acceptance/bundle/user_agent/output.txt b/acceptance/bundle/user_agent/output.txt index 7656fadc0c3..fe1347ad6a7 100644 --- a/acceptance/bundle/user_agent/output.txt +++ b/acceptance/bundle/user_agent/output.txt @@ -20,7 +20,6 @@ OK deploy /api/2.0/workspace/delete engine/direct OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.0/workspace/mkdirs engine/direct OK deploy /api/2.1/unity-catalog/schemas engine/direct -MISS deploy /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat' MISS deploy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS destroy /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' MISS destroy /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none auth/pat' @@ -35,14 +34,12 @@ OK destroy /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct OK destroy /api/2.0/workspace-files/import-file/Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deploy.lock engine/direct OK destroy /api/2.0/workspace/delete engine/direct OK destroy /api/2.0/workspace/delete engine/direct -MISS destroy /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_destroy cmd-exec-id/[UUID][0] interactive/none sdk-feature/pagination auth/pat' MISS destroy /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS plan /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' OK plan /api/2.0/workspace/export engine/direct -MISS plan /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS plan /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS plan2 /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' MISS plan2 /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' @@ -50,25 +47,21 @@ MISS plan2 /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_V MISS plan2 /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat' OK plan2 /api/2.0/workspace/export engine/direct OK plan2 /api/2.1/unity-catalog/schemas/mycatalog.myschema engine/direct -MISS plan2 /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS plan2 /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS run /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' MISS run /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' MISS run /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' MISS run /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none auth/pat' -MISS run /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_run cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS run /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS summary /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' MISS summary /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' MISS summary /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' MISS summary /api/2.0/workspace/export 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none auth/pat' OK summary /api/2.0/preview/scim/v2/Me engine/direct -MISS summary /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_summary cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS summary /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' MISS validate /api/2.0/identity/me 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/identity/me/transitive-parent-groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/get-status 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' MISS validate /api/2.0/workspace/mkdirs 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none auth/pat' -MISS validate /api/2.0/identity/groups 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_validate cmd-exec-id/[UUID] interactive/none sdk-feature/pagination auth/pat' MISS validate /.well-known/databricks-config 'cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS]' From 411ef591991d261f8cbe65308c3669ab6defa230 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Wed, 18 Nov 2026 12:32:29 +0000 Subject: [PATCH 13/23] testserver: drop the unused JoinGroup helper Co-authored-by: Isaac --- libs/testserver/groups.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index b7c82e0ae7b..fb779df0f2e 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -70,11 +70,3 @@ func (s *Server) AddGroups(token string, member, nonMember []string) { } } } - -// JoinGroup makes the identity behind token a member of the group with groupID. -// The ID need not name an existing group, which lets tests make a group lookup fail. -func (s *Server) JoinGroup(token, groupID string) { - ws := s.getWorkspaceForToken(token) - defer ws.LockUnlock()() - ws.groupMembers[groupID] = true -} From 5fc2207e54d3e73fb89d8c614986010831bc44e6 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 11:50:30 +0000 Subject: [PATCH 14/23] iamutil: retry the parent-groups call on HTTP 500 and reuse one identity client The parent-groups call now retries a 500 the same way identity/me does. Each command builds one HTTP client and reuses it for all retries and pages. Co-authored-by: Isaac --- libs/iamutil/current_user.go | 61 +++++++++++++++++++++++++------ libs/iamutil/current_user_test.go | 20 ++++++++++ 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index c58e5ef7c54..a1786c0496f 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -65,7 +65,11 @@ func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.Us // GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should // fail fast rather than wait out transient 500s. func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { - p, err := getIdentityMe(ctx, w) + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + p, err := getIdentityMe(ctx, c) if err != nil { return nil, err } @@ -88,31 +92,57 @@ func GetUserPrincipal(ctx context.Context, w *databricks.WorkspaceClient) (*iam. // pollIdentityMe is getIdentityMe that retries HTTP 500 responses for meRetryTimeout. func pollIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { - return retries.Poll(ctx, meRetryTimeout, func() (*currentPrincipal, *retries.Err) { - p, err := getIdentityMe(ctx, w) + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + return retryOn500(ctx, func() (*currentPrincipal, error) { + return getIdentityMe(ctx, c) + }) +} + +// retryOn500 runs get, retrying HTTP 500 responses for meRetryTimeout. Other errors fail at once. +func retryOn500[T any](ctx context.Context, get func() (T, error)) (T, error) { + res, err := retries.Poll(ctx, meRetryTimeout, func() (*T, *retries.Err) { + v, err := get() if err == nil { - return p, nil + return &v, nil } if apiErr, ok := errors.AsType[*apierr.APIError](err); ok && apiErr.StatusCode == http.StatusInternalServerError { return nil, retries.Continue(err) } return nil, retries.Halt(err) }) + if err != nil { + var zero T + return zero, err + } + return *res, nil +} + +// identityClient sends GETs to the identity endpoints. Build one per command and reuse it +// across retries and pages. +type identityClient struct { + client *databricksclient.DatabricksClient + headers map[string]string } -// identityGet sends a GET to path with the workspace's auth headers and decodes the -// response into out. -func identityGet(ctx context.Context, w *databricks.WorkspaceClient, path string, query map[string]any, out any) error { +func newIdentityClient(w *databricks.WorkspaceClient) (*identityClient, error) { c, err := databricksclient.New(w.Config) if err != nil { - return err + return nil, err } - return c.Do(ctx, http.MethodGet, path, auth.WorkspaceIDHeaders(w.Config), query, nil, out) + return &identityClient{client: c, headers: auth.WorkspaceIDHeaders(w.Config)}, nil } -func getIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { +// get sends a GET to path and decodes the response into out. +func (c *identityClient) get(ctx context.Context, path string, query map[string]any, out any) error { + return c.client.Do(ctx, http.MethodGet, path, c.headers, query, nil, out) +} + +func getIdentityMe(ctx context.Context, c *identityClient) (*currentPrincipal, error) { var me identityMe - if err := identityGet(ctx, w, identityMePath, nil, &me); err != nil { + if err := c.get(ctx, identityMePath, nil, &me); err != nil { return nil, err } user, err := meToUser(&me) @@ -176,6 +206,10 @@ type parentGroup struct { // directly or through parent groups, following every page. The response also names // workspace-local groups such as admins and users, which the account group list lacks. func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceClient) ([]parentGroup, error) { + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } var groups []parentGroup pageToken := "" for { @@ -187,7 +221,10 @@ func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceCli Groups []parentGroup `json:"me_transitive_parent_groups"` NextPageToken string `json:"next_page_token,omitempty"` } - if err := identityGet(ctx, w, identityMeTransitiveParentGroupsPath, query, &page); err != nil { + _, err = retryOn500(ctx, func() (struct{}, error) { + return struct{}{}, c.get(ctx, identityMeTransitiveParentGroupsPath, query, &page) + }) + if err != nil { return nil, err } groups = append(groups, page.Groups...) diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index 301f8f5c424..86d45a2e2c7 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -225,6 +225,26 @@ func TestCurrentUserGroupsFailsWhenParentGroupsFail(t *testing.T) { assert.Nil(t, groups) } +// A transient 500 on the parent-groups call is retried, as the identity/me call is. +func TestCurrentUserGroupsRetriesOn500(t *testing.T) { + var calls atomic.Int32 + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(testserver.Request) any { + if calls.Add(1) == 1 { + return testserver.Response{StatusCode: http.StatusInternalServerError, Body: map[string]any{"error_code": "TEMPORARILY_UNAVAILABLE"}} + } + return map[string]any{"me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}}} + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + groups, err := CurrentUserGroups(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, []iam.ComplexValue{{Value: "11", Display: "eng"}}, groups) + assert.Equal(t, int32(2), calls.Load()) +} + func TestGetUserPrincipalRejectsAssumedGroup(t *testing.T) { w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { return testserver.Response{Body: map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}}} From e00aa912e1c3f752b245694aee2783693eb5b88c Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 11:52:04 +0000 Subject: [PATCH 15/23] experimental, sync, template: reject an assumed group where a user folder is derived Paths under /Workspace/Users and user names used as Postgres or VS Code identities need a user principal. These callers now use GetUserPrincipal, so an assumed group fails with the same error as the bundle commands instead of building a folder named after the group. Co-authored-by: Isaac --- experimental/postgres/cmd/targeting.go | 4 +- experimental/ssh/internal/client/client.go | 2 +- experimental/ssh/internal/keys/secrets.go | 2 +- experimental/ssh/internal/server/fuse.go | 2 +- .../ssh/internal/workspace/workspace.go | 2 +- .../ssh/internal/workspace/workspace_test.go | 38 +++++++++++++++++++ libs/sync/path.go | 2 +- libs/template/helpers.go | 6 +-- 8 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 experimental/ssh/internal/workspace/workspace_test.go diff --git a/experimental/postgres/cmd/targeting.go b/experimental/postgres/cmd/targeting.go index 59b3d95dcd3..a7b2d971795 100644 --- a/experimental/postgres/cmd/targeting.go +++ b/experimental/postgres/cmd/targeting.go @@ -114,7 +114,7 @@ func resolveProvisioned(ctx context.Context, w *databricks.WorkspaceClient, inst return nil, fmt.Errorf("database instance %q is not ready for accepting connections (state: %s)", instance.Name, instance.State) } - user, err := iamutil.GetCurrentUser(ctx, w) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } @@ -175,7 +175,7 @@ func resolveAutoscaling(ctx context.Context, w *databricks.WorkspaceClient, spec return nil, err } - user, err := iamutil.GetCurrentUser(ctx, w) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return nil, fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/client/client.go b/experimental/ssh/internal/client/client.go index f96c8ec8043..c1c7b3f6772 100644 --- a/experimental/ssh/internal/client/client.go +++ b/experimental/ssh/internal/client/client.go @@ -598,7 +598,7 @@ func runIDE(ctx context.Context, client *databricks.WorkspaceClient, userName, k } // Get Databricks user name for the workspace path - currentUser, err := iamutil.GetCurrentUser(ctx, client) + currentUser, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/keys/secrets.go b/experimental/ssh/internal/keys/secrets.go index 4a6ec325c11..45c95e82370 100644 --- a/experimental/ssh/internal/keys/secrets.go +++ b/experimental/ssh/internal/keys/secrets.go @@ -14,7 +14,7 @@ import ( // CreateKeysSecretScope creates or retrieves the secret scope for SSH keys. // sessionID is the unique identifier for the session (cluster ID for dedicated clusters, connection name for serverless). func CreateKeysSecretScope(ctx context.Context, client *databricks.WorkspaceClient, sessionID string) (string, error) { - me, err := iamutil.GetCurrentUser(ctx, client) + me, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/server/fuse.go b/experimental/ssh/internal/server/fuse.go index 90f7b19fe59..3e911306331 100644 --- a/experimental/ssh/internal/server/fuse.go +++ b/experimental/ssh/internal/server/fuse.go @@ -44,7 +44,7 @@ func fuseUserInfo(ctx context.Context, client *databricks.WorkspaceClient) (user // User metadata is optional. Limit its lookup before publishing the server port. userCtx, cancel := context.WithTimeout(ctx, 2*time.Second) defer cancel() - me, err := iamutil.GetCurrentUser(userCtx, client) + me, err := iamutil.GetUserPrincipal(userCtx, client) if err != nil { log.Debugf(ctx, "Registering filesystem credentials without user metadata: %v", err) return "", "" diff --git a/experimental/ssh/internal/workspace/workspace.go b/experimental/ssh/internal/workspace/workspace.go index 64e32e80c71..e8936a2b8f1 100644 --- a/experimental/ssh/internal/workspace/workspace.go +++ b/experimental/ssh/internal/workspace/workspace.go @@ -31,7 +31,7 @@ type WorkspaceMetadata struct { // GetWorkspaceHomeDir returns the current user's workspace home folder. func GetWorkspaceHomeDir(ctx context.Context, client *databricks.WorkspaceClient) (string, error) { - me, err := iamutil.GetCurrentUser(ctx, client) + me, err := iamutil.GetUserPrincipal(ctx, client) if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } diff --git a/experimental/ssh/internal/workspace/workspace_test.go b/experimental/ssh/internal/workspace/workspace_test.go new file mode 100644 index 00000000000..6752730115c --- /dev/null +++ b/experimental/ssh/internal/workspace/workspace_test.go @@ -0,0 +1,38 @@ +package workspace + +import ( + "testing" + + "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetWorkspaceHomeDirReturnsUserFolder(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + home, err := GetWorkspaceHomeDir(t.Context(), w) + require.NoError(t, err) + assert.Equal(t, "/Workspace/Users/tester@databricks.com", home) +} + +// An assumed group has no /Workspace/Users folder, so the home lookup fails with the +// same error as the bundle commands. +func TestGetWorkspaceHomeDirRejectsAssumedGroup(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return map[string]any{"group": map[string]any{"group_id": "3000033333", "group_name": "data-engineers"}} + }) + testserver.AddDefaultHandlers(server) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + + home, err := GetWorkspaceHomeDir(t.Context(), w) + require.ErrorIs(t, err, iamutil.ErrGroupPrincipal) + assert.Empty(t, home) +} diff --git a/libs/sync/path.go b/libs/sync/path.go index 48a6ba580c5..977ed6b1899 100644 --- a/libs/sync/path.go +++ b/libs/sync/path.go @@ -42,7 +42,7 @@ func EnsureRemotePathIsUsable(ctx context.Context, wsc *databricks.WorkspaceClie // TODO: we should cache CurrentUser.Me at the SDK level // for now we let clients pass in any existing user they might already have if me == nil { - me, err = iamutil.GetCurrentUser(ctx, wsc) + me, err = iamutil.GetUserPrincipal(ctx, wsc) if err != nil { return err } diff --git a/libs/template/helpers.go b/libs/template/helpers.go index e2db3ec4fea..ad2d9b7160e 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -172,7 +172,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "user_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = iamutil.GetCurrentUser(ctx, w) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", err } @@ -186,7 +186,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { "short_name": func() (string, error) { if cachedUser == nil { var err error - cachedUser, err = iamutil.GetCurrentUser(ctx, w) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", err } @@ -217,7 +217,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { } if cachedUser == nil { var err error - cachedUser, err = iamutil.GetCurrentUser(ctx, w) + cachedUser, err = iamutil.GetUserPrincipal(ctx, w) if err != nil { return false, err } From f8acf5e4f154747f42e3af177faf742f40e7ea1c Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 11:55:09 +0000 Subject: [PATCH 16/23] bundle: cache the current user's groups with the principal A cache hit now makes no identity/me or parent-groups request. Groups are cached under the same fingerprint as the principal, and a failed groups read is not cached. When the groups could not be read, the permission recommendation to add the identity is withheld, since a grant through a group may already apply. The warning about the groups is the only message. Co-authored-by: Isaac --- bundle/bundle.go | 5 ++ .../config/mutator/populate_current_user.go | 55 ++++++++++++------- .../mutator/populate_current_user_test.go | 33 +++++++++++ bundle/permissions/permission_diagnostics.go | 6 ++ .../permission_diagnostics_test.go | 14 ++++- 5 files changed, 91 insertions(+), 22 deletions(-) diff --git a/bundle/bundle.go b/bundle/bundle.go index 9f686a7e466..3c0f9966fa3 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -229,6 +229,11 @@ type Bundle struct { // By default, caching is enabled. Set DATABRICKS_CACHE_ENABLED=false to disable caching. Cache *cache.Cache + // CurrentUserGroupsUnknown is set when PopulateCurrentUser could not read the groups of + // the current user, so a grant through a group cannot be ruled out. It lives here rather + // than on the config because config is converted to and from its dynamic form between mutators. + CurrentUserGroupsUnknown bool + Metrics Metrics } diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index 99cb4593630..af57ea0806d 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -15,6 +15,13 @@ import ( type populateCurrentUser struct{} +// cachedCurrentUser is the principal and its groups under one cache entry. Entries +// written without groups decode with a nil User and are read as a miss. +type cachedCurrentUser struct { + User *iam.User `json:"user"` + Groups []iam.ComplexValue `json:"groups"` +} + // PopulateCurrentUser sets the `current_user` property on the workspace. func PopulateCurrentUser() bundle.Mutator { return &populateCurrentUser{} @@ -29,33 +36,39 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. return nil } w := b.WorkspaceClient(ctx) - fingerprint := b.GetUserFingerprint(ctx) - me, err := cache.GetOrCompute(ctx, b.Cache, fingerprint, func(ctx context.Context) (*iam.User, error) { - return iamutil.GetUserPrincipal(ctx, w) - }) - if err != nil { - return diag.FromErr(err) - } - // Groups are not part of the cached principal: membership can change while the - // fingerprint stays the same, so they are read on every command. + // A cache hit makes no identity/me or parent-groups request. Groups are cached with + // the principal, so a stale membership lasts as long as the principal entry does. + var user *iam.User var diags diag.Diagnostics - groups, err := iamutil.CurrentUserGroups(ctx, w) - if err != nil { - diags = diag.Diagnostics{{ - Severity: diag.Warning, - Summary: fmt.Sprintf("Could not read the groups of the current user: %v", err), - }} + if cached, ok := cache.Get[cachedCurrentUser](ctx, b.Cache, fingerprint); ok && cached.User != nil { + user = cached.User + user.Groups = cached.Groups + } else { + me, err := iamutil.GetUserPrincipal(ctx, w) + if err != nil { + return diag.FromErr(err) + } + user = me + groups, err := iamutil.CurrentUserGroups(ctx, w) + if err != nil { + // Not cached, so the next command reads the groups again. + diags = diag.Diagnostics{{ + Severity: diag.Warning, + Summary: fmt.Sprintf("Could not read the groups of the current user: %v", err), + }} + b.CurrentUserGroupsUnknown = true + } else { + user.Groups = groups + cache.Put(ctx, b.Cache, fingerprint, cachedCurrentUser{User: user, Groups: groups}) + } } - // Copy the cached principal so the groups are not written back into the cache entry. - user := *me - user.Groups = groups b.Config.Workspace.CurrentUser = &config.User{ - ShortName: iamutil.GetShortUserName(&user), - DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(&user), - User: &user, + ShortName: iamutil.GetShortUserName(user), + DomainFriendlyName: iamutil.GetShortUserDomainFriendlyName(user), + User: user, } // Configure tagging object now that we know we have a valid client. diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index 86edbb1ceb3..b129661c6a7 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -2,6 +2,7 @@ package mutator import ( "net/http" + "sync/atomic" "testing" "time" @@ -68,9 +69,41 @@ func TestPopulateCurrentUserWarnsWhenGroupsFail(t *testing.T) { assert.Equal(t, diag.Warning, diags[0].Severity) assert.Contains(t, diags[0].Summary, "Could not read the groups of the current user") assert.Nil(t, b.Config.Workspace.CurrentUser.Groups) + assert.True(t, b.CurrentUserGroupsUnknown) assert.Equal(t, "tester@databricks.com", b.Config.Workspace.CurrentUser.UserName) } +// The groups are cached with the principal, so a second command with the same +// fingerprint reads neither identity/me nor the parent groups. +func TestPopulateCurrentUserCachesGroupsWithPrincipal(t *testing.T) { + server := testserver.New(t) + var meCalls, parentCalls atomic.Int32 + server.Handle("GET", "/api/2.0/identity/me", func(req testserver.Request) any { + meCalls.Add(1) + return req.Workspace.IdentityMe(req.Token) + }) + server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(req testserver.Request) any { + parentCalls.Add(1) + return req.Workspace.IdentityMeTransitiveParentGroups() + }) + first := newPopulateBundle(t, server, "alpha") + + diags := bundle.Apply(t.Context(), first, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + assert.Equal(t, int32(1), meCalls.Load()) + assert.Equal(t, int32(1), parentCalls.Load()) + + second := &bundle.Bundle{Config: config.Root{}} + second.Cache = first.Cache + second.SetWorkpaceClient(first.WorkspaceClient(t.Context())) + diags = bundle.Apply(t.Context(), second, PopulateCurrentUser()) + require.NoError(t, diags.Error()) + assert.Equal(t, int32(1), meCalls.Load()) + assert.Equal(t, int32(1), parentCalls.Load()) + require.Len(t, second.Config.Workspace.CurrentUser.Groups, 1) + assert.Equal(t, "alpha", second.Config.Workspace.CurrentUser.Groups[0].Display) +} + // An assumed group is not a user: bundle commands that deploy as the current user fail // early with a clear error rather than using the group name as a user name. func TestPopulateCurrentUserRejectsGroupPrincipal(t *testing.T) { diff --git a/bundle/permissions/permission_diagnostics.go b/bundle/permissions/permission_diagnostics.go index ef193de7cab..f81935ec8fb 100644 --- a/bundle/permissions/permission_diagnostics.go +++ b/bundle/permissions/permission_diagnostics.go @@ -40,6 +40,12 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia return nil } + // Without the groups, a grant through a group may already apply, so suggesting the + // identity could be wrong. The populate step has already warned about the groups. + if b.CurrentUserGroupsUnknown { + return nil + } + me := b.Config.Workspace.CurrentUser.User identityType := "user_name" if iamutil.IsServicePrincipal(me) { diff --git a/bundle/permissions/permission_diagnostics_test.go b/bundle/permissions/permission_diagnostics_test.go index eaae7cfaa0b..3276b0bdb2d 100644 --- a/bundle/permissions/permission_diagnostics_test.go +++ b/bundle/permissions/permission_diagnostics_test.go @@ -26,6 +26,18 @@ func TestPermissionDiagnosticsApplySuccess(t *testing.T) { require.NoError(t, diags.Error()) } +// When the groups could not be read, a grant through a group may already apply, so the +// recommendation to add the identity is withheld. +func TestPermissionDiagnosticsSkipsRecommendationWhenGroupsUnknown(t *testing.T) { + b := mockBundle(t, []resources.Permission{ + {Level: "CAN_VIEW", UserName: "testuser@databricks.com"}, + }) + require.NotEmpty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) + + b.CurrentUserGroupsUnknown = true + require.Empty(t, bundle.Apply(t.Context(), b, permissions.PermissionDiagnostics())) +} + func TestPermissionDiagnosticsEmpty(t *testing.T) { b := mockBundle(t, nil) @@ -137,7 +149,7 @@ func TestPermissionDiagnosticsGroupDoesNotExist(t *testing.T) { require.ErrorContains(t, diags.Error(), "may include: nosuchgroup.") } -// A group the current user belongs to, matched by ID, grants CAN_MANAGE. The report +// A group the current user belongs to, matched by name, grants CAN_MANAGE. The report // is then a stale permission set, not a missing grant. func TestPermissionDiagnosticsGroupMember(t *testing.T) { b, _ := mockBundleWithGroups(t, []resources.Permission{ From 403358176c5f7013331da1cba2abec7fdf3422db Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 11:58:14 +0000 Subject: [PATCH 17/23] testserver: set group membership from the members of a created group GroupsCreate makes the caller a member of a new group that lists the caller, so the acceptance tests set workspace-local group membership without a per-test route stub. The identity-me-cloud script now checks the warning with contains.py and traces its command. Co-authored-by: Isaac --- .../bundle/validate/identity-me-cloud/output.txt | 4 +--- acceptance/bundle/validate/identity-me-cloud/script | 7 ++----- .../bundle/validate/identity-me-cloud/test.toml | 8 ++------ .../validate/workspace-local-groups/output.txt | 12 ++++++++++++ .../bundle/validate/workspace-local-groups/script | 9 +++++++++ .../bundle/validate/workspace-local-groups/test.toml | 8 +++----- libs/testserver/groups.go | 8 ++++++++ 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/acceptance/bundle/validate/identity-me-cloud/output.txt b/acceptance/bundle/validate/identity-me-cloud/output.txt index 3599e606bbe..85dc4186b1a 100644 --- a/acceptance/bundle/validate/identity-me-cloud/output.txt +++ b/acceptance/bundle/validate/identity-me-cloud/output.txt @@ -1,3 +1 @@ - -=== bundle validate reads the current user's groups -"array" +[USERNAME] diff --git a/acceptance/bundle/validate/identity-me-cloud/script b/acceptance/bundle/validate/identity-me-cloud/script index 0032af02956..c63380e30cb 100644 --- a/acceptance/bundle/validate/identity-me-cloud/script +++ b/acceptance/bundle/validate/identity-me-cloud/script @@ -1,5 +1,2 @@ -title "bundle validate reads the current user's groups" -err=$(mktemp) -$CLI bundle validate -o json 2>"$err" | jq '.workspace.current_user.groups | type' -grep -q "Could not read the groups" "$err" && { echo "unexpected groups warning"; exit 1; } || true -rm -f "$err" +trace $CLI bundle validate -o json 2>LOG.validate | jq -r '.workspace.current_user.userName' +contains.py '!Could not read the groups' < LOG.validate > /dev/null diff --git a/acceptance/bundle/validate/identity-me-cloud/test.toml b/acceptance/bundle/validate/identity-me-cloud/test.toml index 77cfc1b6619..24e7350afc6 100644 --- a/acceptance/bundle/validate/identity-me-cloud/test.toml +++ b/acceptance/bundle/validate/identity-me-cloud/test.toml @@ -1,7 +1,3 @@ -# Runs on a real workspace too: the groups of the caller are read from the parent-groups -# list there. The stub below gives the local run a membership to read; cloud runs ignore it. +# Runs on a real workspace too, where the caller's own parent groups are read. The local +# run reads whatever the fake holds; the check is that the read does not fail. Cloud = true - -[[Server]] -Pattern = "GET /api/2.0/identity/me/transitive-parent-groups" -Response.Body = '{"me_transitive_parent_groups": [{"group_id": "42", "group_name": "data-engineers"}]}' diff --git a/acceptance/bundle/validate/workspace-local-groups/output.txt b/acceptance/bundle/validate/workspace-local-groups/output.txt index 22baf404710..23460476982 100644 --- a/acceptance/bundle/validate/workspace-local-groups/output.txt +++ b/acceptance/bundle/validate/workspace-local-groups/output.txt @@ -1,10 +1,20 @@ +>>> [CLI] api post /api/2.0/preview/scim/v2/Groups --json {"displayName": "admins", "members": [{"value": "[USERID]"}]} + +>>> [CLI] api post /api/2.0/preview/scim/v2/Groups --json {"displayName": "users", "members": [{"value": "[USERID]"}]} + >>> [CLI] bundle validate -o json [ "admins", "users" ] +>>> print_requests.py --get //api/2.0/identity/me/transitive-parent-groups +{ + "method": "GET", + "path": "/api/2.0/identity/me/transitive-parent-groups" +} + >>> [CLI] bundle validate Name: workspace-local-groups Target: default @@ -13,3 +23,5 @@ Workspace: Path: /Workspace/Users/[USERNAME]/.bundle/workspace-local-groups/default Validation OK! + +>>> print_requests.py --get //api/2.0/identity/me/transitive-parent-groups diff --git a/acceptance/bundle/validate/workspace-local-groups/script b/acceptance/bundle/validate/workspace-local-groups/script index 48f8ee0d0c3..2304c2b6710 100644 --- a/acceptance/bundle/validate/workspace-local-groups/script +++ b/acceptance/bundle/validate/workspace-local-groups/script @@ -1,2 +1,11 @@ +ME=$($CLI current-user me | jq -r .id) +for name in admins users; do + MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.0/preview/scim/v2/Groups --json "{\"displayName\": \"$name\", \"members\": [{\"value\": \"$ME\"}]}" > LOG.groups.$name +done + trace $CLI bundle validate -o json | jq '.workspace.current_user.groups | map(.display)' +trace print_requests.py --get //api/2.0/identity/me/transitive-parent-groups + +# The groups come from the cache, so the second command reads no parent groups. trace $CLI bundle validate +trace print_requests.py --get //api/2.0/identity/me/transitive-parent-groups diff --git a/acceptance/bundle/validate/workspace-local-groups/test.toml b/acceptance/bundle/validate/workspace-local-groups/test.toml index 6d37644a704..8f0f7957ef6 100644 --- a/acceptance/bundle/validate/workspace-local-groups/test.toml +++ b/acceptance/bundle/validate/workspace-local-groups/test.toml @@ -1,5 +1,3 @@ -# The current user belongs to the workspace-local groups admins and users. The -# parent-groups response names them, so their CAN_MANAGE grant counts as manageable. -[[Server]] -Pattern = "GET /api/2.0/identity/me/transitive-parent-groups" -Response.Body = '{"me_transitive_parent_groups": [{"group_id": "1", "group_name": "admins"}, {"group_id": "2", "group_name": "users"}]}' +# The current user belongs to the workspace-local groups admins and users, set up by the +# script. Their CAN_MANAGE grant counts as manageable. +RecordRequests = true diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index fb779df0f2e..d13feda6cfd 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -35,6 +35,14 @@ func (s *FakeWorkspace) GroupsCreate(req Request) Response { s.Groups[groupId] = group + // A member that is the caller makes the caller a member of the new group, so acceptance + // tests can set group membership declaratively. + for _, member := range request.Members { + if member.Value == userForToken(req.Token).Id { + s.groupMembers[groupId] = true + } + } + return Response{ Body: group, } From 2492aaf045e2be24fd99a2034b7154b687a48abb Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 11:58:15 +0000 Subject: [PATCH 18/23] changelog: name the new current_user fields and narrow the auth describe note Co-authored-by: Isaac --- .nextchanges/bundles/7714.md | 2 +- .nextchanges/cli/7714.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.nextchanges/bundles/7714.md b/.nextchanges/bundles/7714.md index 6a522c34b3e..0065d333dee 100644 --- a/.nextchanges/bundles/7714.md +++ b/.nextchanges/bundles/7714.md @@ -1 +1 @@ -* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements` in their output. ([#7714](https://github.com/databricks/cli/pull/7714)) +* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements` in their output. `current_user` now includes `active` and `emails`. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/cli/7714.md b/.nextchanges/cli/7714.md index 4a3297d1b47..d77a4f3156f 100644 --- a/.nextchanges/cli/7714.md +++ b/.nextchanges/cli/7714.md @@ -1 +1 @@ -* `databricks auth describe` and the current-user lookups now work when the caller is an assumed group. ([#7714](https://github.com/databricks/cli/pull/7714)) +* `databricks auth describe` works when the caller is an assumed group. ([#7714](https://github.com/databricks/cli/pull/7714)) From 86c4ff537c41b8787199a679786bbb22cc4f4225 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 12:24:36 +0000 Subject: [PATCH 19/23] bundle: read the current user's groups through the cache's compute path cache.Get bypassed the local cache telemetry (attempt, hit, miss), which changed the telemetry of every bundle command. The groups are now read inside GetOrCompute, which keeps the telemetry, and a failed groups read returns an error from the compute function so nothing is stored. The cache key carries a version, so entries written before the groups were cached are not read. Acceptance goldens drop the parent-groups request of a cached command. url_ref has no request left to record for destroy, so print_requests reports the missing log. Co-authored-by: Isaac --- .../url_ref/out.destroy-requests.txt | 3 + .../resources/jobs/request-summary/output.txt | 3 - .../config/mutator/populate_current_user.go | 61 ++++++++++++------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt b/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt index e69de29bb2d..9ae96b91891 100644 --- a/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt +++ b/acceptance/bundle/bundle_tag/url_ref/out.destroy-requests.txt @@ -0,0 +1,3 @@ +File [OUTPUT_DIR]/out.requests.txt not found + +Exit code: 1 diff --git a/acceptance/bundle/resources/jobs/request-summary/output.txt b/acceptance/bundle/resources/jobs/request-summary/output.txt index e8f18926229..8a934a53bcf 100644 --- a/acceptance/bundle/resources/jobs/request-summary/output.txt +++ b/acceptance/bundle/resources/jobs/request-summary/output.txt @@ -19,7 +19,6 @@ Files: 1 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged >>> print_requests -GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json @@ -49,7 +48,6 @@ Files: 0 uploaded, 0 deleted Resources: 0 created, 0 changed, 0 deleted, 1 unchanged >>> print_requests -GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deployment.json @@ -77,7 +75,6 @@ All files and directories at the following location will be deleted: /Workspace/ Destroy: 1 deleted >>> print_requests -GET /api/2.0/identity/me/transitive-parent-groups GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/deploy.lock GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/resources.json GET /api/2.0/workspace/export direct_download=true path=/Workspace/Users/[USERNAME]/.bundle/request-summary/default/state/terraform.tfstate diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index af57ea0806d..6de3c79b598 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -2,6 +2,7 @@ package mutator import ( "context" + "errors" "fmt" "github.com/databricks/cli/bundle" @@ -15,8 +16,16 @@ import ( type populateCurrentUser struct{} -// cachedCurrentUser is the principal and its groups under one cache entry. Entries -// written without groups decode with a nil User and are read as a miss. +// currentUserCacheKey keys the cached principal and groups. Version keeps entries +// written before the groups were cached, which have another shape, from being read. +type currentUserCacheKey struct { + Fingerprint bundle.UserFingerprint + Version int +} + +const currentUserCacheVersion = 2 + +// cachedCurrentUser is the principal with its groups, cached as one entry. type cachedCurrentUser struct { User *iam.User `json:"user"` Groups []iam.ComplexValue `json:"groups"` @@ -36,33 +45,41 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. return nil } w := b.WorkspaceClient(ctx) - fingerprint := b.GetUserFingerprint(ctx) + key := currentUserCacheKey{Fingerprint: b.GetUserFingerprint(ctx), Version: currentUserCacheVersion} - // A cache hit makes no identity/me or parent-groups request. Groups are cached with - // the principal, so a stale membership lasts as long as the principal entry does. - var user *iam.User - var diags diag.Diagnostics - if cached, ok := cache.Get[cachedCurrentUser](ctx, b.Cache, fingerprint); ok && cached.User != nil { - user = cached.User - user.Groups = cached.Groups - } else { + // The groups are part of the cached value, so a cache hit makes no identity/me or + // parent-groups request. A failed groups read is not cached: it returns an error from + // the compute function, and the principal is kept for the warning below. + var principal *iam.User + var groupsErr error + cached, err := cache.GetOrCompute(ctx, b.Cache, key, func(ctx context.Context) (cachedCurrentUser, error) { me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { - return diag.FromErr(err) + return cachedCurrentUser{}, err } - user = me + principal = me groups, err := iamutil.CurrentUserGroups(ctx, w) if err != nil { - // Not cached, so the next command reads the groups again. - diags = diag.Diagnostics{{ - Severity: diag.Warning, - Summary: fmt.Sprintf("Could not read the groups of the current user: %v", err), - }} - b.CurrentUserGroupsUnknown = true - } else { - user.Groups = groups - cache.Put(ctx, b.Cache, fingerprint, cachedCurrentUser{User: user, Groups: groups}) + groupsErr = err + return cachedCurrentUser{}, err } + return cachedCurrentUser{User: me, Groups: groups}, nil + }) + + var diags diag.Diagnostics + user := cached.User + switch { + case err == nil: + user.Groups = cached.Groups + case groupsErr != nil && errors.Is(err, groupsErr): + user = principal + diags = diag.Diagnostics{{ + Severity: diag.Warning, + Summary: fmt.Sprintf("Could not read the groups of the current user: %v", groupsErr), + }} + b.CurrentUserGroupsUnknown = true + default: + return diag.FromErr(err) } b.Config.Workspace.CurrentUser = &config.User{ From 8f7143805b3f670a19239406ef1d025d4312e7a7 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Thu, 19 Nov 2026 12:54:33 +0000 Subject: [PATCH 20/23] acceptance: read the bundle skills user agent from the identity/me request Co-authored-by: Isaac --- acceptance/cmd/skills-invoked/output.txt | 2 +- acceptance/cmd/skills-invoked/script | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance/cmd/skills-invoked/output.txt b/acceptance/cmd/skills-invoked/output.txt index 50d0025864a..9370602eced 100644 --- a/acceptance/cmd/skills-invoked/output.txt +++ b/acceptance/cmd/skills-invoked/output.txt @@ -15,7 +15,7 @@ >>> [CLI] bundle validate ->>> print_requests.py --get //scim/v2/Me +>>> print_requests.py --get //identity/me === Invalid entries are dropped and the rest is normalized diff --git a/acceptance/cmd/skills-invoked/script b/acceptance/cmd/skills-invoked/script index 918f4141abd..16316447641 100644 --- a/acceptance/cmd/skills-invoked/script +++ b/acceptance/cmd/skills-invoked/script @@ -14,7 +14,7 @@ contains.py " skill/databricks-core" < LOG.api > /dev/null title "Env var adds the skills from bundle commands" DATABRICKS_SKILLS_INVOKED=databricks-dabs trace $CLI bundle validate > /dev/null -trace print_requests.py --get //scim/v2/Me > LOG.bundle +trace print_requests.py --get //identity/me > LOG.bundle contains.py " skill/databricks-dabs" < LOG.bundle > /dev/null title "Invalid entries are dropped and the rest is normalized" From 4dd82460ddefe007208b534045681292edde19b5 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Fri, 20 Nov 2026 14:41:11 +0000 Subject: [PATCH 21/23] iamutil: address review on identity/me current user - Report a workspace without identity/me with an actionable error that says to use an older CLI version for now, in the bundle path and the other callers. - A failed parent-groups read only logs; it no longer warns, so `bundle validate --strict` does not fail on it. The permission recommendation stays withheld. - Reject a repeated next_page_token in the parent-groups paging loop. - Reject assumed-group principals in psql, apps import, air cancel --all, apps run locally and shell completion for remote paths. - auth describe makes a single attempt for the identity/me check. - The fake lists the built-in users group for every identity and can act as an assumed group, so acceptance tests set group membership through the fake instead of per-test stubs. - Strengthen the auth describe fallback test and the cloud group check. cloud: TestAccept/cmd/auth/describe/identity-me-cloud TestAccept/bundle/validate/identity-me-cloud TestAccept/bundle/resources/models/basic TestAccept/bundle/local_state_staleness Co-authored-by: Isaac --- acceptance/air/cancel/test.toml | 2 +- acceptance/air/list/test.toml | 2 +- .../bind/pipelines/update/out.summary.json | 6 ++ .../output.txt | 4 +- .../experimental-compatibility/output.txt | 4 +- .../output.txt | 12 ---- .../bundle/validate/group-principal/test.toml | 4 +- .../validate/identity-me-cloud/output.txt | 1 + .../bundle/validate/identity-me-cloud/script | 2 +- .../validate/identity-me-cloud/test.toml | 4 +- .../immutable_workspace_paths/output.txt | 6 ++ .../bundle/validate/job-references/output.txt | 6 ++ .../out.with_empty_prefix.json | 6 ++ .../out.with_nil_prefix.json | 6 ++ .../out.with_static_prefix.json | 6 ++ .../out.without_presets.json | 6 ++ .../workspace-local-groups/output.txt | 2 - .../validate/workspace-local-groups/script | 4 +- .../validate/workspace-local-groups/test.toml | 5 +- .../auth/describe/identity-me-group/test.toml | 15 +---- acceptance/internal/config.go | 3 + acceptance/internal/prepare_server.go | 3 + .../config/mutator/populate_current_user.go | 19 +++--- .../mutator/populate_current_user_test.go | 38 ++++++++--- bundle/permissions/permission_diagnostics.go | 2 +- cmd/air/cancel.go | 2 +- cmd/air/cancel_test.go | 2 +- cmd/air/runlaunch.go | 2 +- cmd/apps/import.go | 2 +- cmd/apps/run_local.go | 2 +- cmd/auth/describe.go | 2 +- cmd/auth/describe_test.go | 13 +++- cmd/psql/psql_autoscaling.go | 2 +- cmd/psql/psql_provisioned.go | 2 +- cmd/sync/completion.go | 2 +- libs/iamutil/current_user.go | 66 +++++++++++++++---- libs/iamutil/current_user_test.go | 53 ++++++++++++++- libs/testserver/fake_workspace.go | 22 +++++-- libs/testserver/groups.go | 7 +- 39 files changed, 250 insertions(+), 97 deletions(-) diff --git a/acceptance/air/cancel/test.toml b/acceptance/air/cancel/test.toml index ac19a88d6ad..09bb7d57a3a 100644 --- a/acceptance/air/cancel/test.toml +++ b/acceptance/air/cancel/test.toml @@ -22,7 +22,7 @@ Pattern = "POST /api/2.2/jobs/runs/cancel" Response.Body = '{}' # Jobs runs/list backs `cancel --all`: two active AIR runs for the current user -# (tester@databricks.com, from the built-in scim/v2/Me handler). +# (tester@databricks.com, from the built-in identity/me handler). [[Server]] Pattern = "GET /api/2.2/jobs/runs/list" Response.Body = ''' diff --git a/acceptance/air/list/test.toml b/acceptance/air/list/test.toml index 009458a44e4..81b9e306314 100644 --- a/acceptance/air/list/test.toml +++ b/acceptance/air/list/test.toml @@ -10,7 +10,7 @@ Response.Body = '' # `air list` reads Jobs runs/list directly (expand_tasks), filtering to AIR runs. # The current runs carry an ai_runtime_task (not modeled by the typed SDK), which # is why the CLI parses the response raw. Both runs belong to the default user -# (tester@databricks.com, from the built-in scim/v2/Me handler). The second run +# (tester@databricks.com, from the built-in identity/me handler). The second run # has the shape of the official multi-task DAB (a notebook prep task before the # ai_runtime_task) and must be listed from its AIR task. The third run has two # AI Runtime tasks that ended differently: it lists as two rows, each naming its diff --git a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json index 85aea659837..81747e50a45 100644 --- a/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json +++ b/acceptance/bundle/deployment/bind/pipelines/update/out.summary.json @@ -46,6 +46,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt index e18cea7f6dc..31aea7e2616 100644 --- a/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt +++ b/acceptance/bundle/python/experimental-compatibility-both-equal/output.txt @@ -57,14 +57,14 @@ [ 0, 1, - 961 + 1009 ] ], "resources.jobs": [ [ 0, 1, - 970 + 1018 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/python/experimental-compatibility/output.txt b/acceptance/bundle/python/experimental-compatibility/output.txt index 42036a6ae2a..a65ebdef2dc 100644 --- a/acceptance/bundle/python/experimental-compatibility/output.txt +++ b/acceptance/bundle/python/experimental-compatibility/output.txt @@ -50,14 +50,14 @@ [ 0, 1, - 906 + 954 ] ], "resources.jobs": [ [ 0, 1, - 915 + 963 ] ], "resources.jobs.my_job": [ diff --git a/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt b/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt index 32b813560a0..c45fd8fd743 100644 --- a/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt +++ b/acceptance/bundle/telemetry/deploy-workspace-folder-permissions/output.txt @@ -97,18 +97,6 @@ state_path_is_shared false state_path_other true >>> [CLI] bundle deploy -t shared_users_can_manage -Recommendation: permissions section should explicitly include the current deployment identity '[USERNAME]' or one of its groups -If it is not included, CAN_MANAGE permissions are only applied if the present identity is used to deploy. - -Consider using a adding a top-level permissions section such as the following: - - permissions: - - user_name: [USERNAME] - level: CAN_MANAGE - -See https://docs.databricks.com/dev-tools/bundles/permissions.html to learn more about permission configuration. - in databricks.yml:47:7 - Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/shared_users_can_manage/files... Files: 1 uploaded, 0 deleted Resources: 0 created, 0 changed, 0 deleted, 0 unchanged diff --git a/acceptance/bundle/validate/group-principal/test.toml b/acceptance/bundle/validate/group-principal/test.toml index 3a70d6933e8..ac4175e5ee6 100644 --- a/acceptance/bundle/validate/group-principal/test.toml +++ b/acceptance/bundle/validate/group-principal/test.toml @@ -1,5 +1,3 @@ # The caller is an assumed group. Bundle commands act as a user, so validate fails # early instead of using the group name as a user name. -[[Server]] -Pattern = "GET /api/2.0/identity/me" -Response.Body = '{"group": {"group_id": "42", "group_name": "data-engineers"}}' +IsAssumedGroup = true diff --git a/acceptance/bundle/validate/identity-me-cloud/output.txt b/acceptance/bundle/validate/identity-me-cloud/output.txt index 85dc4186b1a..458096907c0 100644 --- a/acceptance/bundle/validate/identity-me-cloud/output.txt +++ b/acceptance/bundle/validate/identity-me-cloud/output.txt @@ -1 +1,2 @@ [USERNAME] +true diff --git a/acceptance/bundle/validate/identity-me-cloud/script b/acceptance/bundle/validate/identity-me-cloud/script index c63380e30cb..0073d671602 100644 --- a/acceptance/bundle/validate/identity-me-cloud/script +++ b/acceptance/bundle/validate/identity-me-cloud/script @@ -1,2 +1,2 @@ -trace $CLI bundle validate -o json 2>LOG.validate | jq -r '.workspace.current_user.userName' +trace $CLI bundle validate -o json 2>LOG.validate | jq -r '.workspace.current_user | .userName, (.groups | map(.display) | contains(["users"]))' contains.py '!Could not read the groups' < LOG.validate > /dev/null diff --git a/acceptance/bundle/validate/identity-me-cloud/test.toml b/acceptance/bundle/validate/identity-me-cloud/test.toml index 24e7350afc6..03e36f135d4 100644 --- a/acceptance/bundle/validate/identity-me-cloud/test.toml +++ b/acceptance/bundle/validate/identity-me-cloud/test.toml @@ -1,3 +1,3 @@ -# Runs on a real workspace too, where the caller's own parent groups are read. The local -# run reads whatever the fake holds; the check is that the read does not fail. +# Runs on a real workspace too. The check is that the caller's parent groups are read without +# a warning and include the workspace users group, which every workspace identity belongs to. Cloud = true diff --git a/acceptance/bundle/validate/immutable_workspace_paths/output.txt b/acceptance/bundle/validate/immutable_workspace_paths/output.txt index 2246eb4323c..ff3b19dd6b6 100644 --- a/acceptance/bundle/validate/immutable_workspace_paths/output.txt +++ b/acceptance/bundle/validate/immutable_workspace_paths/output.txt @@ -12,6 +12,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/job-references/output.txt b/acceptance/bundle/validate/job-references/output.txt index 1dbb5f82d9f..22a6b7ebbc2 100644 --- a/acceptance/bundle/validate/job-references/output.txt +++ b/acceptance/bundle/validate/job-references/output.txt @@ -63,6 +63,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json index 21a5ccc17ed..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_empty_prefix.json @@ -84,6 +84,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json index 21a5ccc17ed..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_nil_prefix.json @@ -84,6 +84,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json index bc5dc30ad30..285b887bbfd 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.with_static_prefix.json @@ -78,6 +78,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json index 21a5ccc17ed..637af50b367 100644 --- a/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json +++ b/acceptance/bundle/validate/presets_name_prefix_dev/out.without_presets.json @@ -84,6 +84,12 @@ "value": "[USERNAME]" } ], + "groups": [ + { + "display": "users", + "value": "1" + } + ], "id": "[USERID]", "short_name": "[USERNAME]", "userName": "[USERNAME]" diff --git a/acceptance/bundle/validate/workspace-local-groups/output.txt b/acceptance/bundle/validate/workspace-local-groups/output.txt index 23460476982..5ad04d57180 100644 --- a/acceptance/bundle/validate/workspace-local-groups/output.txt +++ b/acceptance/bundle/validate/workspace-local-groups/output.txt @@ -1,8 +1,6 @@ >>> [CLI] api post /api/2.0/preview/scim/v2/Groups --json {"displayName": "admins", "members": [{"value": "[USERID]"}]} ->>> [CLI] api post /api/2.0/preview/scim/v2/Groups --json {"displayName": "users", "members": [{"value": "[USERID]"}]} - >>> [CLI] bundle validate -o json [ "admins", diff --git a/acceptance/bundle/validate/workspace-local-groups/script b/acceptance/bundle/validate/workspace-local-groups/script index 2304c2b6710..ed8ee16a355 100644 --- a/acceptance/bundle/validate/workspace-local-groups/script +++ b/acceptance/bundle/validate/workspace-local-groups/script @@ -1,7 +1,5 @@ ME=$($CLI current-user me | jq -r .id) -for name in admins users; do - MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.0/preview/scim/v2/Groups --json "{\"displayName\": \"$name\", \"members\": [{\"value\": \"$ME\"}]}" > LOG.groups.$name -done +MSYS_NO_PATHCONV=1 trace $CLI api post /api/2.0/preview/scim/v2/Groups --json "{\"displayName\": \"admins\", \"members\": [{\"value\": \"$ME\"}]}" > LOG.groups.admins trace $CLI bundle validate -o json | jq '.workspace.current_user.groups | map(.display)' trace print_requests.py --get //api/2.0/identity/me/transitive-parent-groups diff --git a/acceptance/bundle/validate/workspace-local-groups/test.toml b/acceptance/bundle/validate/workspace-local-groups/test.toml index 8f0f7957ef6..afc30ab11e4 100644 --- a/acceptance/bundle/validate/workspace-local-groups/test.toml +++ b/acceptance/bundle/validate/workspace-local-groups/test.toml @@ -1,3 +1,4 @@ -# The current user belongs to the workspace-local groups admins and users, set up by the -# script. Their CAN_MANAGE grant counts as manageable. +# The current user belongs to the workspace-local groups admins and users. The script creates +# admins; the fake lists users for every identity, as a workspace does. Their CAN_MANAGE grant +# counts as manageable. RecordRequests = true diff --git a/acceptance/cmd/auth/describe/identity-me-group/test.toml b/acceptance/cmd/auth/describe/identity-me-group/test.toml index 52106194401..ff2551b71d2 100644 --- a/acceptance/cmd/auth/describe/identity-me-group/test.toml +++ b/acceptance/cmd/auth/describe/identity-me-group/test.toml @@ -3,17 +3,4 @@ Ignore = [ ] # The caller is an assumed group, which identity/me returns as the principal. -[[Server]] -Pattern = "GET /api/2.0/identity/me" -Response.Body = ''' -{ - "name": "accounts/acct-123/me", - "group": { - "group_id": "42", - "group_name": "data-engineers", - "name": "accounts/acct-123/groups/42", - "parent": "accounts/acct-123" - }, - "identity_attributes": {} -} -''' +IsAssumedGroup = true diff --git a/acceptance/internal/config.go b/acceptance/internal/config.go index 6ea79a9578a..8dc3ccda4bf 100644 --- a/acceptance/internal/config.go +++ b/acceptance/internal/config.go @@ -59,6 +59,9 @@ type TestConfig struct { // If set, current user will be set to a service principal-like UUID instead of email (default is false) IsServicePrincipal *bool + // If set, the caller is an assumed group: identity/me returns the group as the principal (default is false) + IsAssumedGroup *bool + // List of additional replacements to apply on this test. // Old is a regexp, New is a replacement expression. Repls []testdiff.Replacement diff --git a/acceptance/internal/prepare_server.go b/acceptance/internal/prepare_server.go index 6feb41483a2..52d89d29548 100644 --- a/acceptance/internal/prepare_server.go +++ b/acceptance/internal/prepare_server.go @@ -145,6 +145,9 @@ func PrepareServerAndClient(t *testing.T, config TestConfig, logRequests bool, o if isTruePtr(config.IsServicePrincipal) { token = testserver.ServicePrincipalTokenPrefix + tokenSuffix testUser = testserver.TestUserSP + } else if isTruePtr(config.IsAssumedGroup) { + token = testserver.AssumedGroupTokenPrefix + tokenSuffix + testUser = testserver.TestUser } else if staleOnceEnabled(testEnv) { // Use the eventual-consistency token so the testserver returns 404 on the // first GET after a create, matching real cloud propagation delays. diff --git a/bundle/config/mutator/populate_current_user.go b/bundle/config/mutator/populate_current_user.go index 6de3c79b598..800829563c0 100644 --- a/bundle/config/mutator/populate_current_user.go +++ b/bundle/config/mutator/populate_current_user.go @@ -3,13 +3,13 @@ package mutator import ( "context" "errors" - "fmt" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/cache" "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/iamutil" + "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/tags" "github.com/databricks/databricks-sdk-go/service/iam" ) @@ -48,8 +48,8 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. key := currentUserCacheKey{Fingerprint: b.GetUserFingerprint(ctx), Version: currentUserCacheVersion} // The groups are part of the cached value, so a cache hit makes no identity/me or - // parent-groups request. A failed groups read is not cached: it returns an error from - // the compute function, and the principal is kept for the warning below. + // parent-groups request. A failed groups read is not cached, so the next command reads + // them again; the principal is kept for the fallback below. var principal *iam.User var groupsErr error cached, err := cache.GetOrCompute(ctx, b.Cache, key, func(ctx context.Context) (cachedCurrentUser, error) { @@ -66,18 +66,19 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. return cachedCurrentUser{User: me, Groups: groups}, nil }) - var diags diag.Diagnostics user := cached.User switch { case err == nil: user.Groups = cached.Groups case groupsErr != nil && errors.Is(err, groupsErr): + // Not a warning: `bundle validate --strict` would fail on it. The permission + // recommendation is withheld through CurrentUserGroupsUnknown instead. + log.Infof(ctx, "Could not read the groups of the current user: %v", groupsErr) user = principal - diags = diag.Diagnostics{{ - Severity: diag.Warning, - Summary: fmt.Sprintf("Could not read the groups of the current user: %v", groupsErr), - }} b.CurrentUserGroupsUnknown = true + case errors.Is(err, iamutil.ErrMeNotEnabled): + // diag.FromErr would show only the API fields, which drop this message. + return diag.Errorf("%v", err) default: return diag.FromErr(err) } @@ -91,5 +92,5 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag. // Configure tagging object now that we know we have a valid client. b.Tagging = tags.ForCloud(w.Config) - return diags + return nil } diff --git a/bundle/config/mutator/populate_current_user_test.go b/bundle/config/mutator/populate_current_user_test.go index b129661c6a7..515f59475c0 100644 --- a/bundle/config/mutator/populate_current_user_test.go +++ b/bundle/config/mutator/populate_current_user_test.go @@ -37,15 +37,18 @@ func TestPopulateCurrentUserSetsGroups(t *testing.T) { diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) require.NoError(t, diags.Error()) - require.Len(t, b.Config.Workspace.CurrentUser.Groups, 2) - assert.Equal(t, "alpha", b.Config.Workspace.CurrentUser.Groups[0].Display) - assert.Equal(t, "zeta", b.Config.Workspace.CurrentUser.Groups[1].Display) + var names []string + for _, g := range b.Config.Workspace.CurrentUser.Groups { + names = append(names, g.Display) + } + // The fake lists the built-in users group for every identity. + assert.Equal(t, []string{"alpha", "users", "zeta"}, names) } // Workspace-local groups are named by the parent-groups response, so admins and users // resolve like account groups. func TestPopulateCurrentUserSetsWorkspaceLocalGroups(t *testing.T) { - b := newPopulateBundle(t, testserver.New(t), "admins", "users") + b := newPopulateBundle(t, testserver.New(t), "admins") diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) require.NoError(t, diags.Error()) @@ -56,18 +59,18 @@ func TestPopulateCurrentUserSetsWorkspaceLocalGroups(t *testing.T) { assert.Equal(t, []string{"admins", "users"}, names) } -// A failed parent-groups read leaves the groups empty and warns, and the current user is still set. -func TestPopulateCurrentUserWarnsWhenGroupsFail(t *testing.T) { +// A failed parent-groups read leaves the groups empty and only logs, and the current user is still set. +func TestPopulateCurrentUserSkipsGroupsWhenReadFails(t *testing.T) { server := testserver.New(t) server.Handle("GET", "/api/2.0/identity/me/transitive-parent-groups", func(testserver.Request) any { return testserver.Response{StatusCode: http.StatusForbidden, Body: map[string]any{"error_code": "PERMISSION_DENIED", "message": "denied"}} }) b := newPopulateBundle(t, server, "testgroup") + // The failure is logged, not a diagnostic, so `bundle validate --strict` still passes. diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) - require.Len(t, diags, 1) - assert.Equal(t, diag.Warning, diags[0].Severity) - assert.Contains(t, diags[0].Summary, "Could not read the groups of the current user") + require.NoError(t, diags.Error()) + assert.Empty(t, diags) assert.Nil(t, b.Config.Workspace.CurrentUser.Groups) assert.True(t, b.CurrentUserGroupsUnknown) assert.Equal(t, "tester@databricks.com", b.Config.Workspace.CurrentUser.UserName) @@ -100,8 +103,9 @@ func TestPopulateCurrentUserCachesGroupsWithPrincipal(t *testing.T) { require.NoError(t, diags.Error()) assert.Equal(t, int32(1), meCalls.Load()) assert.Equal(t, int32(1), parentCalls.Load()) - require.Len(t, second.Config.Workspace.CurrentUser.Groups, 1) + require.Len(t, second.Config.Workspace.CurrentUser.Groups, 2) assert.Equal(t, "alpha", second.Config.Workspace.CurrentUser.Groups[0].Display) + assert.Equal(t, "users", second.Config.Workspace.CurrentUser.Groups[1].Display) } // An assumed group is not a user: bundle commands that deploy as the current user fail @@ -119,3 +123,17 @@ func TestPopulateCurrentUserRejectsGroupPrincipal(t *testing.T) { assert.Equal(t, iamutil.ErrGroupPrincipal.Error(), diags[0].Summary) assert.Nil(t, b.Config.Workspace.CurrentUser) } + +// A workspace without identity/me fails the command with the actionable message, not the raw server text. +func TestPopulateCurrentUserReportsIdentityMeNotEnabled(t *testing.T) { + server := testserver.New(t) + server.Handle("GET", "/api/2.0/identity/me", func(testserver.Request) any { + return testserver.Response{StatusCode: http.StatusNotFound, Body: map[string]any{"error_code": "ENDPOINT_NOT_FOUND", "message": "Me API is not enabled."}} + }) + b := newPopulateBundle(t, server, "testgroup") + + diags := bundle.Apply(t.Context(), b, PopulateCurrentUser()) + require.Error(t, diags.Error()) + assert.ErrorContains(t, diags.Error(), "the identity/me API is not enabled in this workspace") + assert.Nil(t, b.Config.Workspace.CurrentUser) +} diff --git a/bundle/permissions/permission_diagnostics.go b/bundle/permissions/permission_diagnostics.go index f81935ec8fb..1ed3525092d 100644 --- a/bundle/permissions/permission_diagnostics.go +++ b/bundle/permissions/permission_diagnostics.go @@ -41,7 +41,7 @@ func (m *permissionDiagnostics) Apply(ctx context.Context, b *bundle.Bundle) dia } // Without the groups, a grant through a group may already apply, so suggesting the - // identity could be wrong. The populate step has already warned about the groups. + // identity could be wrong. The populate step has already logged that the groups could not be read. if b.CurrentUserGroupsUnknown { return nil } diff --git a/cmd/air/cancel.go b/cmd/air/cancel.go index 4dae554ea29..d6faa6aabe3 100644 --- a/cmd/air/cancel.go +++ b/cmd/air/cancel.go @@ -89,7 +89,7 @@ any other task such as a notebook that prepares data. if all { data.All = true - me, err := iamutil.GetCurrentUser(ctx, w) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return renderError(ctx, cmd, "INTERNAL_ERROR", "TRANSIENT", true, fmt.Errorf("failed to resolve current user: %w", err)) diff --git a/cmd/air/cancel_test.go b/cmd/air/cancel_test.go index ce95205c79d..47c2936914e 100644 --- a/cmd/air/cancel_test.go +++ b/cmd/air/cancel_test.go @@ -355,7 +355,7 @@ func TestCancelAllCancelsAMultiTaskRunOnce(t *testing.T) { single := airBaseRun(222, "me@example.com", "GPU_1xA10", 1, "/Users/me@example.com/exp-c") list := runsListBody(t, "", multi, single) var cancelled []int64 - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + srv := httptest.NewServer(withAirIdentityMe(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/2.2/jobs/runs/list": _, _ = w.Write([]byte(list)) diff --git a/cmd/air/runlaunch.go b/cmd/air/runlaunch.go index 3d65136b0ca..846b68dfb89 100644 --- a/cmd/air/runlaunch.go +++ b/cmd/air/runlaunch.go @@ -21,7 +21,7 @@ const userWorkspaceDirEnv = "DATABRICKS_INTERNAL_USER_WORKSPACE_DIR" // currentUserEmail returns the authenticated user's email (works for any domain). func currentUserEmail(ctx context.Context, w *databricks.WorkspaceClient) (string, error) { - // Only userName is needed; the groups are read separately. + // Only userName is needed, so the groups are not read. me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return "", fmt.Errorf("failed to resolve current user: %w", err) diff --git a/cmd/apps/import.go b/cmd/apps/import.go index 7455163ca6a..2f76f3215d6 100644 --- a/cmd/apps/import.go +++ b/cmd/apps/import.go @@ -90,7 +90,7 @@ Examples: w := cmdctx.WorkspaceClient(ctx) // Get current user to filter apps - me, err := iamutil.GetCurrentUser(ctx, w) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/apps/run_local.go b/cmd/apps/run_local.go index 10521b27734..4ba97f9d0a0 100644 --- a/cmd/apps/run_local.go +++ b/cmd/apps/run_local.go @@ -117,7 +117,7 @@ func setupProxy(ctx context.Context, cmd *cobra.Command, config *runlocal.Config return err } - me, err := iamutil.GetCurrentUser(ctx, w) + me, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return err } diff --git a/cmd/auth/describe.go b/cmd/auth/describe.go index 73e87af71a3..a2ef92cca14 100644 --- a/cmd/auth/describe.go +++ b/cmd/auth/describe.go @@ -148,7 +148,7 @@ func getAuthStatus(cmd *cobra.Command, args []string, showSensitive bool, fn try } w := cmdctx.WorkspaceClient(ctx) - me, err := iamutil.GetCurrentUser(ctx, w) + me, err := iamutil.GetCurrentUserOnce(ctx, w) if err == nil { return successAuthStatus(ctx, cmd, w.Config, showSensitive, me.UserName, ""), nil } diff --git a/cmd/auth/describe_test.go b/cmd/auth/describe_test.go index 3ad0b3092a6..1136e3e1025 100644 --- a/cmd/auth/describe_test.go +++ b/cmd/auth/describe_test.go @@ -420,7 +420,7 @@ func describeVerifyServer(t *testing.T, accountID string, meStatus, listStatus i w.Header().Set("Content-Type", "application/json") if status != http.StatusOK { w.WriteHeader(status) - okBody = map[string]any{"error_code": "TEST_ERROR", "message": "secondary check failed"} + okBody = map[string]any{"error_code": "TEST_ERROR", "message": r.URL.Path + " failed"} } require.NoError(t, json.NewEncoder(w).Encode(okBody)) } @@ -480,7 +480,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { tests := []struct { name string isAccount bool - primaryErr *apierr.APIError // account branch only: the primary Workspaces.List error + primaryErr *apierr.APIError // the primary check error that is reported when both checks fail meStatus int listStatus int accountID string @@ -498,6 +498,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "no second call without an account id", + primaryErr: &apierr.APIError{StatusCode: http.StatusBadRequest, Message: "/api/2.0/identity/me failed"}, meStatus: http.StatusBadRequest, wantStatus: "error", }, @@ -513,6 +514,7 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { }, { name: "workspace branch, both checks fail", + primaryErr: &apierr.APIError{StatusCode: http.StatusNotFound, Message: "/api/2.0/identity/me failed"}, meStatus: http.StatusNotFound, listStatus: http.StatusUnauthorized, accountID: "test-acct", @@ -555,7 +557,12 @@ func TestGetAuthStatusVerificationFallback(t *testing.T) { assert.Equal(t, tc.wantUsername, status.Username) assert.Equal(t, tc.wantAccountID, status.AccountID) if tc.wantStatus == "error" && tc.primaryErr != nil { - assert.ErrorIs(t, status.Error, tc.primaryErr) + // The reported error is the primary one, found by status and message: an HTTP + // error from the server is a new value, so ErrorIs cannot match it by identity. + var apiErr *apierr.APIError + require.ErrorAs(t, status.Error, &apiErr) + assert.Equal(t, tc.primaryErr.StatusCode, apiErr.StatusCode) + assert.Equal(t, tc.primaryErr.Message, apiErr.Message) } }) } diff --git a/cmd/psql/psql_autoscaling.go b/cmd/psql/psql_autoscaling.go index ca50bdba6aa..b43a8787827 100644 --- a/cmd/psql/psql_autoscaling.go +++ b/cmd/psql/psql_autoscaling.go @@ -38,7 +38,7 @@ func connectAutoscaling(ctx context.Context, projectID, branchID, endpointID str return err } - user, err := iamutil.GetCurrentUser(ctx, w) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/psql/psql_provisioned.go b/cmd/psql/psql_provisioned.go index f3934e86157..feffd66b80c 100644 --- a/cmd/psql/psql_provisioned.go +++ b/cmd/psql/psql_provisioned.go @@ -27,7 +27,7 @@ func connectProvisioned(ctx context.Context, instanceName string, retryConfig li return err } - user, err := iamutil.GetCurrentUser(ctx, w) + user, err := iamutil.GetUserPrincipal(ctx, w) if err != nil { return fmt.Errorf("failed to get current user: %w", err) } diff --git a/cmd/sync/completion.go b/cmd/sync/completion.go index f4090921188..04d7184d653 100644 --- a/cmd/sync/completion.go +++ b/cmd/sync/completion.go @@ -45,7 +45,7 @@ func completeRemotePath( wsc *databricks.WorkspaceClient, toComplete string, ) ([]string, cobra.ShellCompDirective) { - me, err := iamutil.GetCurrentUserOnce(ctx, wsc) + me, err := iamutil.GetUserPrincipalOnce(ctx, wsc) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/libs/iamutil/current_user.go b/libs/iamutil/current_user.go index a1786c0496f..87d71d4675d 100644 --- a/libs/iamutil/current_user.go +++ b/libs/iamutil/current_user.go @@ -3,6 +3,7 @@ package iamutil import ( "context" "errors" + "fmt" "net/http" "slices" "strings" @@ -25,7 +26,15 @@ const ( identityMeTransitiveParentGroupsPath = "/api/2.0/identity/me/transitive-parent-groups" ) -// ErrGroupPrincipal is returned by GetUserPrincipal when the current request is +// ErrMeNotEnabled is wrapped by the identity/me errors of a workspace that does not serve the +// API yet. Callers check it with errors.Is to show the message as the diagnostic. +var ErrMeNotEnabled = errors.New("the identity/me API is not enabled in this workspace, so the current user cannot be read; use an older Databricks CLI version for now") + +// meNotEnabledCodes are the error codes identity/me returns in a workspace that does not serve +// the API yet. The 404 and 501 statuses are matched through apierr.ErrNotFound and ErrNotImplemented. +var meNotEnabledCodes = []string{"ENDPOINT_NOT_FOUND", "FEATURE_DISABLED", "NOT_IMPLEMENTED"} + +// ErrGroupPrincipal is returned by GetUserPrincipal and GetUserPrincipalOnce when the current request is // authorized by an assumed group, which has no user identity to deploy as. var ErrGroupPrincipal = errors.New("the current identity is an assumed group, but this command needs a user or service principal") @@ -48,12 +57,8 @@ type currentPrincipal struct { // resolves an assumed group. Groups, Roles and Entitlements are not set; CurrentUserGroups // reads the groups. // -// identity/me is a read-only endpoint, so retrying any 500 is safe. The Databricks SCIM -// API intermittently returns "500 TEMPORARILY_UNAVAILABLE" (a condition it otherwise -// serves as 503), and the SDK retries 503 but not 500 — see the "some API's recommend -// retries on HTTP 500, but we'll add that later" comment on (*apierr.APIError).IsRetriable -// in databricks-sdk-go/apierr. This call runs at the start of every bundle command, so an -// unretried 500 fails the whole command; we close that gap here. +// identity/me is read-only, so retrying a 500 is safe. The SDK retries 503 but not 500, and +// this call runs at the start of every bundle command, so an unretried 500 would fail it. func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { p, err := pollIdentityMe(ctx, w) if err != nil { @@ -65,11 +70,7 @@ func GetCurrentUser(ctx context.Context, w *databricks.WorkspaceClient) (*iam.Us // GetCurrentUserOnce is GetCurrentUser with a single attempt, for probes that should // fail fast rather than wait out transient 500s. func GetCurrentUserOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { - c, err := newIdentityClient(w) - if err != nil { - return nil, err - } - p, err := getIdentityMe(ctx, c) + p, err := getIdentityMeOnce(ctx, w) if err != nil { return nil, err } @@ -90,6 +91,19 @@ func GetUserPrincipal(ctx context.Context, w *databricks.WorkspaceClient) (*iam. return p.user, nil } +// GetUserPrincipalOnce is GetUserPrincipal with a single attempt, for probes that should +// fail fast rather than wait out transient 500s. +func GetUserPrincipalOnce(ctx context.Context, w *databricks.WorkspaceClient) (*iam.User, error) { + p, err := getIdentityMeOnce(ctx, w) + if err != nil { + return nil, err + } + if p.group { + return nil, ErrGroupPrincipal + } + return p.user, nil +} + // pollIdentityMe is getIdentityMe that retries HTTP 500 responses for meRetryTimeout. func pollIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { c, err := newIdentityClient(w) @@ -101,6 +115,15 @@ func pollIdentityMe(ctx context.Context, w *databricks.WorkspaceClient) (*curren }) } +// getIdentityMeOnce is getIdentityMe with a single attempt. +func getIdentityMeOnce(ctx context.Context, w *databricks.WorkspaceClient) (*currentPrincipal, error) { + c, err := newIdentityClient(w) + if err != nil { + return nil, err + } + return getIdentityMe(ctx, c) +} + // retryOn500 runs get, retrying HTTP 500 responses for meRetryTimeout. Other errors fail at once. func retryOn500[T any](ctx context.Context, get func() (T, error)) (T, error) { res, err := retries.Poll(ctx, meRetryTimeout, func() (*T, *retries.Err) { @@ -120,6 +143,16 @@ func retryOn500[T any](ctx context.Context, get func() (T, error)) (T, error) { return *res, nil } +// meNotEnabled reports whether err means the workspace does not serve identity/me yet. +func meNotEnabled(err error) bool { + apiErr, ok := errors.AsType[*apierr.APIError](err) + if !ok { + return false + } + return errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrNotImplemented) || + slices.Contains(meNotEnabledCodes, apiErr.ErrorCode) +} + // identityClient sends GETs to the identity endpoints. Build one per command and reuse it // across retries and pages. type identityClient struct { @@ -143,6 +176,9 @@ func (c *identityClient) get(ctx context.Context, path string, query map[string] func getIdentityMe(ctx context.Context, c *identityClient) (*currentPrincipal, error) { var me identityMe if err := c.get(ctx, identityMePath, nil, &me); err != nil { + if meNotEnabled(err) { + return nil, fmt.Errorf("%w: %w", ErrMeNotEnabled, err) + } return nil, err } user, err := meToUser(&me) @@ -212,6 +248,7 @@ func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceCli } var groups []parentGroup pageToken := "" + seenTokens := map[string]bool{} for { query := map[string]any{} if pageToken != "" { @@ -231,6 +268,11 @@ func listCurrentUserParentGroups(ctx context.Context, w *databricks.WorkspaceCli if page.NextPageToken == "" { return groups, nil } + // Following a token the server already sent would loop until the context ends. + if seenTokens[page.NextPageToken] { + return nil, errors.New("identity/me returned a repeated page token for the parent groups") + } + seenTokens[page.NextPageToken] = true pageToken = page.NextPageToken } } diff --git a/libs/iamutil/current_user_test.go b/libs/iamutil/current_user_test.go index 86d45a2e2c7..7ed8629efee 100644 --- a/libs/iamutil/current_user_test.go +++ b/libs/iamutil/current_user_test.go @@ -179,9 +179,10 @@ func TestCurrentUserGroupsResolvesNamesSorted(t *testing.T) { groups, err := CurrentUserGroups(t.Context(), w) require.NoError(t, err) - require.Len(t, groups, 2) + require.Len(t, groups, 3) assert.Equal(t, "alpha", groups[0].Display) - assert.Equal(t, "zeta", groups[1].Display) + assert.Equal(t, "users", groups[1].Display) + assert.Equal(t, "zeta", groups[2].Display) assert.NotEmpty(t, groups[0].Value) } @@ -282,3 +283,51 @@ func TestGetCurrentUserOnceDoesNotRetryOn500(t *testing.T) { require.Error(t, err) assert.Equal(t, int32(1), calls.Load()) } + +func TestGetCurrentUserReportsIdentityMeNotEnabled(t *testing.T) { + tests := []struct { + name string + status int + errorCode string + wantIs error + }{ + {name: "not found", status: http.StatusNotFound, errorCode: "ENDPOINT_NOT_FOUND", wantIs: apierr.ErrNotFound}, + {name: "not implemented", status: http.StatusNotImplemented, errorCode: "NOT_IMPLEMENTED", wantIs: apierr.ErrNotImplemented}, + {name: "feature disabled", status: http.StatusBadRequest, errorCode: "FEATURE_DISABLED"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + w := newIdentityMeWorkspace(t, func(testserver.Request) testserver.Response { + return testserver.Response{StatusCode: tc.status, Body: map[string]any{"error_code": tc.errorCode, "message": "Me API is not enabled."}} + }) + + _, err := GetCurrentUser(t.Context(), w) + assert.ErrorContains(t, err, "the identity/me API is not enabled in this workspace") + assert.ErrorContains(t, err, "use an older Databricks CLI version") + assert.ErrorContains(t, err, "Me API is not enabled.") + if tc.wantIs != nil { + assert.ErrorIs(t, err, tc.wantIs) + } + }) + } +} + +func TestListCurrentUserParentGroupsRejectsRepeatedPageToken(t *testing.T) { + var calls atomic.Int32 + server := testserver.New(t) + t.Cleanup(server.Close) + server.Handle("GET", identityMeTransitiveParentGroupsPath, func(req testserver.Request) any { + calls.Add(1) + // A server bug that keeps returning the same next page token must not loop forever. + return map[string]any{ + "me_transitive_parent_groups": []map[string]any{{"group_id": "11", "group_name": "eng"}}, + "next_page_token": "page2", + } + }) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + + _, err = listCurrentUserParentGroups(t.Context(), w) + assert.ErrorContains(t, err, "repeated page token") + assert.Equal(t, int32(2), calls.Load()) +} diff --git a/libs/testserver/fake_workspace.go b/libs/testserver/fake_workspace.go index 6979474bbfe..b2320fbb453 100644 --- a/libs/testserver/fake_workspace.go +++ b/libs/testserver/fake_workspace.go @@ -52,10 +52,13 @@ const ( // (not yet visible), and a deleted synced table stays in DELETING instead of // disappearing (so the direct engine's post-delete poll has a teardown to wait out). EventualConsistencyTokenPrefix = "dbapi3" - UserID = "1000012345" - TestDefaultClusterId = "0123-456789-cluster0" - TestDefaultWarehouseId = "8ec9edc1-db0c-40df-af8d-7580020fe61e" - TestDefaultInstancePoolId = "0123-456789-pool0" + // AssumedGroupTokenPrefix marks a caller authorized by an assumed group: identity/me + // returns that group as the principal. + AssumedGroupTokenPrefix = "dbapi4" + UserID = "1000012345" + TestDefaultClusterId = "0123-456789-cluster0" + TestDefaultWarehouseId = "8ec9edc1-db0c-40df-af8d-7580020fe61e" + TestDefaultInstancePoolId = "0123-456789-pool0" ) var TestUser = iam.User{ @@ -103,9 +106,18 @@ func (s *FakeWorkspace) MeUser(token string) iam.User { return user } +// AssumedGroupId and AssumedGroupName identify the group an AssumedGroupTokenPrefix token acts as. +const ( + AssumedGroupId = "42" + AssumedGroupName = "data-engineers" +) + // IdentityMe returns the GET /api/2.0/identity/me body for a token. It describes the -// same identity as MeUser, as a user or a service principal. +// same identity as MeUser, as a user, a service principal or an assumed group. func (s *FakeWorkspace) IdentityMe(token string) any { + if strings.HasPrefix(token, AssumedGroupTokenPrefix) { + return map[string]any{"group": map[string]any{"group_id": AssumedGroupId, "group_name": AssumedGroupName}} + } me := s.MeUser(token) if isServicePrincipalToken(token) { return map[string]any{"service_principal": map[string]any{ diff --git a/libs/testserver/groups.go b/libs/testserver/groups.go index d13feda6cfd..78207254638 100644 --- a/libs/testserver/groups.go +++ b/libs/testserver/groups.go @@ -48,13 +48,18 @@ func (s *FakeWorkspace) GroupsCreate(req Request) Response { } } +// usersGroupId is the workspace-local users group. Every workspace identity is a member of it, +// so the fake lists it for every caller, as a real workspace does. +const usersGroupId = "1" + // IdentityMeTransitiveParentGroups answers GET /api/2.0/identity/me/transitive-parent-groups // with the groups the caller belongs to, named as the server names them. func (s *FakeWorkspace) IdentityMeTransitiveParentGroups() any { defer s.LockUnlock()() ids := slices.Sorted(maps.Keys(s.groupMembers)) - parents := make([]map[string]any, 0, len(ids)) + parents := make([]map[string]any, 0, len(ids)+1) + parents = append(parents, map[string]any{"group_id": usersGroupId, "group_name": "users"}) for _, id := range ids { parents = append(parents, map[string]any{"group_id": id, "group_name": s.Groups[id].DisplayName}) } From 82dcb5a6fdea25029c869ebf78774f862f396c6c Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Fri, 20 Nov 2026 14:41:12 +0000 Subject: [PATCH 22/23] changelog: list the current_user fields that change and the air group error Co-authored-by: Isaac --- .nextchanges/air/7714.md | 1 + .nextchanges/bundles/7714.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .nextchanges/air/7714.md diff --git a/.nextchanges/air/7714.md b/.nextchanges/air/7714.md new file mode 100644 index 00000000000..0e2678dbc1b --- /dev/null +++ b/.nextchanges/air/7714.md @@ -0,0 +1 @@ +* `databricks air run`, `air list` and `air cancel --all` fail with a clear error when the caller is an assumed group, since they act as a user. ([#7714](https://github.com/databricks/cli/pull/7714)) diff --git a/.nextchanges/bundles/7714.md b/.nextchanges/bundles/7714.md index 0065d333dee..32ce445d901 100644 --- a/.nextchanges/bundles/7714.md +++ b/.nextchanges/bundles/7714.md @@ -1 +1 @@ -* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements` in their output. `current_user` now includes `active` and `emails`. ([#7714](https://github.com/databricks/cli/pull/7714)) +* `bundle validate` and `bundle summary` no longer include `current_user.roles` or `current_user.entitlements`. In `current_user`, `name` now holds the given and family names, `displayName` is those names joined with a space, `emails` holds the user name, `active` comes from the account status, and `groups` lists the transitive parent groups with only `value` and `display`. ([#7714](https://github.com/databricks/cli/pull/7714)) From 7300c24ee20f8d7d9e71aeeafc6357a1249fded9 Mon Sep 17 00:00:00 2001 From: "andrew.nester" Date: Fri, 20 Nov 2026 14:53:25 +0000 Subject: [PATCH 23/23] acceptance: run the identity/me cloud tests on this head The merge of main became the head commit, so the tests named for the cloud run were no longer selected. cloud: TestAccept/cmd/auth/describe/identity-me-cloud TestAccept/bundle/validate/identity-me-cloud TestAccept/bundle/resources/models/basic TestAccept/bundle/local_state_staleness Co-authored-by: Isaac