diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/databricks.yml.tmpl b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/databricks.yml.tmpl new file mode 100644 index 00000000000..18eb29b0983 --- /dev/null +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/databricks.yml.tmpl @@ -0,0 +1,14 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +workspace: + root_path: ~/.bundle/$UNIQUE_NAME + +resources: + cluster_policies: + policy: + name: test-policy-$UNIQUE_NAME + definition: + custom_tags.PolicyTag: + type: fixed + value: from-policy diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/out.test.toml b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/out.test.toml new file mode 100644 index 00000000000..a4f8dd5ef74 --- /dev/null +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/out.test.toml @@ -0,0 +1,3 @@ +Cloud = true +GOOS.windows = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt new file mode 100644 index 00000000000..784b59eb366 --- /dev/null +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt @@ -0,0 +1,47 @@ + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/[UNIQUE_NAME]/files... +Created cluster_policies.policy +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== The creator's CAN_USE is not a remote change + +>>> [CLI] bundle config-remote-sync +No changes detected. + + +=== Grant a group CAN_USE in the UI + +>>> [CLI] bundle config-remote-sync --save +Detected changes in 1 resource(s): + +Resource: resources.cluster_policies.policy + permissions: add + + + +>>> [CLI] bundle validate -o json +[{"principal":"users","level":"CAN_USE"}] + +=== Deploy applies the synced ACL + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/[UNIQUE_NAME]/files... +Created cluster_policies.policy.permissions +Files: 1 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 1 unchanged + +>>> [CLI] permissions get cluster-policies [POLICY_ID] +[{"principal":"users","level":"CAN_USE"}] + +>>> [CLI] bundle plan +Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.cluster_policies.policy + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/[UNIQUE_NAME] + +Destroy: 1 deleted diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script new file mode 100644 index 00000000000..ad422c18cda --- /dev/null +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script @@ -0,0 +1,31 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +cleanup() { + trace $CLI bundle destroy --auto-approve +} +trap cleanup EXIT + +trace $CLI bundle deploy +policy_id=$(read_id.py policy) + +title "The creator's CAN_USE is not a remote change" +trace $CLI bundle config-remote-sync + +title "Grant a group CAN_USE in the UI" +# The UI keeps the existing direct entries, including the creator's. +$CLI permissions get cluster-policies "$policy_id" | jq '{access_control_list: ( + [.access_control_list[] | select(.all_permissions[0].inherited | not) + | {user_name, service_principal_name, group_name, permission_level: .all_permissions[0].permission_level} + | with_entries(select(.value != null))] + + [{group_name: "users", permission_level: "CAN_USE"}])}' > acl.json +$CLI permissions set cluster-policies "$policy_id" --json @acl.json > /dev/null +rm acl.json + +trace $CLI bundle config-remote-sync --save +# The deployer is a user locally and a service principal on cloud, so print principals only. +trace $CLI bundle validate -o json | jq -c '[.resources.cluster_policies.policy.permissions[] | {principal: (.user_name // .service_principal_name // .group_name), level}] | sort_by(.principal)' + +title "Deploy applies the synced ACL" +trace $CLI bundle deploy +trace $CLI permissions get cluster-policies "$policy_id" | jq -c '[.access_control_list[] | select(.all_permissions[0].inherited | not) | {principal: (.user_name // .service_principal_name // .group_name), level: .all_permissions[0].permission_level}] | sort_by(.principal)' +trace $CLI bundle plan diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/test.toml b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/test.toml new file mode 100644 index 00000000000..46aa65b723a --- /dev/null +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/test.toml @@ -0,0 +1,3 @@ +Cloud = true +RecordRequests = false +Ignore = [".databricks", "databricks.yml"] diff --git a/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index 780bc52ab8b..298b54b0d6c 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -371,7 +371,7 @@ resources: }, changes) } -func TestWithoutCurrentUserManage(t *testing.T) { +func TestWithoutCurrentUserDefaults(t *testing.T) { permissions := []dresources.StatePermission{ {Level: iam.PermissionLevelIsOwner, UserName: "me@example.com"}, {Level: iam.PermissionLevelCanManage, ServicePrincipalName: "me@example.com"}, @@ -384,7 +384,25 @@ func TestWithoutCurrentUserManage(t *testing.T) { {Level: iam.PermissionLevelIsOwner, UserName: "owner@example.com"}, {Level: iam.PermissionLevelCanManage, UserName: "other@example.com"}, {Level: iam.PermissionLevelCanView, GroupName: "users"}, - }, withoutCurrentUserManage(permissions, "me@example.com")) + }, withoutCurrentUserDefaults(permissions, "me@example.com", "jobs")) +} + +func TestWithoutCurrentUserDefaultsClusterPolicy(t *testing.T) { + permissions := []dresources.StatePermission{ + {Level: iam.PermissionLevelCanUse, ServicePrincipalName: "me@example.com"}, + {Level: iam.PermissionLevelCanUse, UserName: "other@example.com"}, + {Level: iam.PermissionLevelCanUse, GroupName: "users"}, + } + + assert.Equal(t, []dresources.StatePermission{ + {Level: iam.PermissionLevelCanUse, UserName: "other@example.com"}, + {Level: iam.PermissionLevelCanUse, GroupName: "users"}, + }, withoutCurrentUserDefaults(permissions, "me@example.com", "cluster_policies")) + + creatorOnly := []dresources.StatePermission{ + {Level: iam.PermissionLevelCanUse, UserName: "me@example.com"}, + } + assert.Empty(t, withoutCurrentUserDefaults(creatorOnly, "me@example.com", "cluster_policies")) } func TestPermissionsReadSkip(t *testing.T) { diff --git a/bundle/configsync/permissions.go b/bundle/configsync/permissions.go index 87eca95a0b8..ca99b1ad31b 100644 --- a/bundle/configsync/permissions.go +++ b/bundle/configsync/permissions.go @@ -144,7 +144,7 @@ func readUnmanagedPermissions(ctx context.Context, b *bundle.Bundle, plan *deplo skipped = append(skipped, permissionsReadSkip(resourceKey, err)) return nil } - permissions = withoutCurrentUserManage(permissions, currentUser) + permissions = withoutCurrentUserDefaults(permissions, currentUser, config.GetResourceTypeFromKey(resourceKey)) if len(permissions) > 0 { result[resourceKey] = permissions } @@ -234,16 +234,27 @@ func lookupDeployedField(b *bundle.Bundle, resourceKey, field string) (any, erro return structaccess.Get(remote, path) } -// withoutCurrentUserManage drops the current user's IS_OWNER and CAN_MANAGE, one of which -// FixPermissions adds on deploy. Another principal's IS_OWNER is kept, so a transferred owner -// stays the owner after the next deploy. -func withoutCurrentUserManage(permissions []dresources.StatePermission, currentUser string) []dresources.StatePermission { +// withoutCurrentUserDefaults drops the current user's grants that exist without being declared. +// For most resources these are IS_OWNER and CAN_MANAGE, one of which FixPermissions adds on +// deploy; another principal's IS_OWNER is kept, so a transferred owner stays the owner after the +// next deploy. For a cluster policy it is the CAN_USE the backend grants its creator: only admins +// create policies and admins can use every policy, so the grant is never needed in the config. +func withoutCurrentUserDefaults(permissions []dresources.StatePermission, currentUser, resourceType string) []dresources.StatePermission { return slices.DeleteFunc(permissions, func(p dresources.StatePermission) bool { - isCurrentUser := currentUser != "" && (p.UserName == currentUser || p.ServicePrincipalName == currentUser) - return isCurrentUser && (p.Level == iam.PermissionLevelIsOwner || p.Level == iam.PermissionLevelCanManage) + if !isCurrentUserPermission(p, currentUser) { + return false + } + if resourceType == "cluster_policies" { + return p.Level == iam.PermissionLevelCanUse + } + return p.Level == iam.PermissionLevelIsOwner || p.Level == iam.PermissionLevelCanManage }) } +func isCurrentUserPermission(p dresources.StatePermission, currentUser string) bool { + return currentUser != "" && (p.UserName == currentUser || p.ServicePrincipalName == currentUser) +} + func currentUserName(b *bundle.Bundle) string { if b.Config.Workspace.CurrentUser == nil { return "" diff --git a/libs/testserver/cluster_policies.go b/libs/testserver/cluster_policies.go index 7351e357d11..44117d48f75 100644 --- a/libs/testserver/cluster_policies.go +++ b/libs/testserver/cluster_policies.go @@ -9,6 +9,7 @@ import ( "github.com/databricks/cli/libs/clusterpolicy" "github.com/databricks/databricks-sdk-go/service/compute" + "github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/jobs" ) @@ -50,6 +51,24 @@ func (s *FakeWorkspace) ClusterPoliciesCreate(req Request) any { policy.PolicyId = id s.ClusterPolicies[id] = policy + // The backend grants the creator CAN_USE on a new policy. + creatorName := userForToken(req.Token).UserName + creator := iam.AccessControlResponse{ + DisplayName: creatorName, + AllPermissions: []iam.Permission{{PermissionLevel: "CAN_USE", ForceSendFields: []string{"Inherited"}}}, + } + if creatorName == TestUserSP.UserName { + creator.ServicePrincipalName = creatorName + } else { + creator.UserName = creatorName + } + objectID := "/cluster-policies/" + id + s.Permissions[objectID] = iam.ObjectPermissions{ + ObjectId: objectID, + ObjectType: "cluster-policy", + AccessControlList: []iam.AccessControlResponse{creator}, + } + return Response{Body: compute.CreatePolicyResponse{PolicyId: id}} }