From 86b4aa895b5049c18ecc347d6cca186570e257c6 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Tue, 17 Nov 2026 11:17:42 +0000 Subject: [PATCH 1/3] Ignore the cluster policy creator's CAN_USE in config-remote-sync The backend grants the creator CAN_USE on a new cluster policy, so config-remote-sync imported it as a permissions block for policies that declare none. Drop it like the deployer's IS_OWNER/CAN_MANAGE, and seed the grant in the testserver so local runs cover it. cloud: TestAccept/bundle/config-remote-sync/cluster_policy_cross_target TestAccept/bundle/config-remote-sync/cluster_policy_remote_addition TestAccept/bundle/config-remote-sync/policy_injected_cluster_fields Co-authored-by: Isaac --- bundle/configsync/diff_test.go | 17 +++++++++++++++-- bundle/configsync/permissions.go | 19 +++++++++++++------ libs/testserver/cluster_policies.go | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index 780bc52ab8b..89cd7c583d2 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,20 @@ 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")) } func TestPermissionsReadSkip(t *testing.T) { diff --git a/bundle/configsync/permissions.go b/bundle/configsync/permissions.go index 87eca95a0b8..cfda613e64c 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,13 +234,20 @@ 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 are there without being +// declared: IS_OWNER or CAN_MANAGE, one of which FixPermissions adds on deploy, and the CAN_USE +// that the backend grants the creator of a cluster policy, which supports no other level. Another +// principal's IS_OWNER is kept, so a transferred owner stays the owner after the next deploy. +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 !isCurrentUser { + return false + } + if resourceType == "cluster_policies" { + return p.Level == iam.PermissionLevelCanUse + } + return p.Level == iam.PermissionLevelIsOwner || p.Level == iam.PermissionLevelCanManage }) } diff --git a/libs/testserver/cluster_policies.go b/libs/testserver/cluster_policies.go index 7351e357d11..bef37ac7e61 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,23 @@ 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. + creator := iam.AccessControlResponse{ + DisplayName: s.CurrentUser().UserName, + AllPermissions: []iam.Permission{{PermissionLevel: "CAN_USE", ForceSendFields: []string{"Inherited"}}}, + } + if s.isServicePrincipal { + creator.ServicePrincipalName = s.CurrentUser().UserName + } else { + creator.UserName = s.CurrentUser().UserName + } + 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}} } From 95224c454653c3331a81852d50834bc7a61eac78 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Tue, 17 Nov 2026 11:27:43 +0000 Subject: [PATCH 2/3] Keep the creator's CAN_USE when other principals are imported FixPermissions does not add a cluster policy grant back, so dropping the creator's CAN_USE next to other imported grants would revoke it on the next deploy. Drop it only when it is the whole ACL, credit the creator by token in the testserver, and cover sync and deploy with a remote group grant. cloud: TestAccept/bundle/config-remote-sync/cluster_policy_cross_target TestAccept/bundle/config-remote-sync/cluster_policy_remote_addition TestAccept/bundle/config-remote-sync/policy_injected_cluster_fields TestAccept/bundle/config-remote-sync/cluster_policy_permissions Co-authored-by: Isaac --- .../databricks.yml.tmpl | 14 ++++++ .../cluster_policy_permissions/out.test.toml | 3 ++ .../cluster_policy_permissions/output.txt | 47 +++++++++++++++++++ .../cluster_policy_permissions/script | 31 ++++++++++++ .../cluster_policy_permissions/test.toml | 3 ++ bundle/configsync/diff_test.go | 16 +++++-- bundle/configsync/permissions.go | 29 +++++++----- libs/testserver/cluster_policies.go | 9 ++-- 8 files changed, 131 insertions(+), 21 deletions(-) create mode 100644 acceptance/bundle/config-remote-sync/cluster_policy_permissions/databricks.yml.tmpl create mode 100644 acceptance/bundle/config-remote-sync/cluster_policy_permissions/out.test.toml create mode 100644 acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt create mode 100644 acceptance/bundle/config-remote-sync/cluster_policy_permissions/script create mode 100644 acceptance/bundle/config-remote-sync/cluster_policy_permissions/test.toml 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..6a59ad3e076 --- /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":"[USERNAME]","level":"CAN_USE"},{"principal":"users","level":"CAN_USE"}] + +=== Deploy keeps the creator's CAN_USE + +>>> [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":"[USERNAME]","level":"CAN_USE"},{"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..f52d9078740 --- /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 keeps the creator's CAN_USE" +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 89cd7c583d2..6a5d1d0fecf 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -388,16 +388,22 @@ func TestWithoutCurrentUserDefaults(t *testing.T) { } func TestWithoutCurrentUserDefaultsClusterPolicy(t *testing.T) { - permissions := []dresources.StatePermission{ + creatorOnly := []dresources.StatePermission{ + {Level: iam.PermissionLevelCanUse, ServicePrincipalName: "me@example.com"}, + } + assert.Empty(t, withoutCurrentUserDefaults(creatorOnly, "me@example.com", "cluster_policies")) + + // With other grants, the current user's CAN_USE is kept, or the next deploy would revoke it. + withOthers := []dresources.StatePermission{ {Level: iam.PermissionLevelCanUse, ServicePrincipalName: "me@example.com"}, - {Level: iam.PermissionLevelCanUse, UserName: "other@example.com"}, {Level: iam.PermissionLevelCanUse, GroupName: "users"}, } + assert.Equal(t, withOthers, withoutCurrentUserDefaults(slices.Clone(withOthers), "me@example.com", "cluster_policies")) - assert.Equal(t, []dresources.StatePermission{ + otherCreator := []dresources.StatePermission{ {Level: iam.PermissionLevelCanUse, UserName: "other@example.com"}, - {Level: iam.PermissionLevelCanUse, GroupName: "users"}, - }, withoutCurrentUserDefaults(permissions, "me@example.com", "cluster_policies")) + } + assert.Equal(t, otherCreator, withoutCurrentUserDefaults(slices.Clone(otherCreator), "me@example.com", "cluster_policies")) } func TestPermissionsReadSkip(t *testing.T) { diff --git a/bundle/configsync/permissions.go b/bundle/configsync/permissions.go index cfda613e64c..c0355664e97 100644 --- a/bundle/configsync/permissions.go +++ b/bundle/configsync/permissions.go @@ -234,20 +234,25 @@ func lookupDeployedField(b *bundle.Bundle, resourceKey, field string) (any, erro return structaccess.Get(remote, path) } -// withoutCurrentUserDefaults drops the current user's grants that are there without being -// declared: IS_OWNER or CAN_MANAGE, one of which FixPermissions adds on deploy, and the CAN_USE -// that the backend grants the creator of a cluster policy, which supports no other level. Another -// principal's IS_OWNER is kept, so a transferred owner stays the owner after the next deploy. +// withoutCurrentUserDefaults 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. +// +// A cluster policy supports only CAN_USE, which the backend grants its creator and FixPermissions +// does not add. The current user's CAN_USE is dropped only when it is the whole ACL: once other +// principals are imported, it is kept so that the next deploy does not revoke it. 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) - if !isCurrentUser { - return false - } - if resourceType == "cluster_policies" { - return p.Level == iam.PermissionLevelCanUse + isCurrentUser := func(p dresources.StatePermission) bool { + return currentUser != "" && (p.UserName == currentUser || p.ServicePrincipalName == currentUser) + } + if resourceType == "cluster_policies" { + if len(permissions) == 1 && isCurrentUser(permissions[0]) && permissions[0].Level == iam.PermissionLevelCanUse { + return nil } - return p.Level == iam.PermissionLevelIsOwner || p.Level == iam.PermissionLevelCanManage + return permissions + } + return slices.DeleteFunc(permissions, func(p dresources.StatePermission) bool { + return isCurrentUser(p) && (p.Level == iam.PermissionLevelIsOwner || p.Level == iam.PermissionLevelCanManage) }) } diff --git a/libs/testserver/cluster_policies.go b/libs/testserver/cluster_policies.go index bef37ac7e61..44117d48f75 100644 --- a/libs/testserver/cluster_policies.go +++ b/libs/testserver/cluster_policies.go @@ -52,14 +52,15 @@ func (s *FakeWorkspace) ClusterPoliciesCreate(req Request) any { s.ClusterPolicies[id] = policy // The backend grants the creator CAN_USE on a new policy. + creatorName := userForToken(req.Token).UserName creator := iam.AccessControlResponse{ - DisplayName: s.CurrentUser().UserName, + DisplayName: creatorName, AllPermissions: []iam.Permission{{PermissionLevel: "CAN_USE", ForceSendFields: []string{"Inherited"}}}, } - if s.isServicePrincipal { - creator.ServicePrincipalName = s.CurrentUser().UserName + if creatorName == TestUserSP.UserName { + creator.ServicePrincipalName = creatorName } else { - creator.UserName = s.CurrentUser().UserName + creator.UserName = creatorName } objectID := "/cluster-policies/" + id s.Permissions[objectID] = iam.ObjectPermissions{ From 1d52db99ff6b2a140c6629d1fd4cc78b2020b8c9 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 18 Nov 2026 05:15:48 +0000 Subject: [PATCH 3/3] Always drop the policy creator's CAN_USE in config-remote-sync Only admins create cluster policies and admins can use every policy, so the creator's CAN_USE is not needed in the config, and writing it there would apply one deployer's grant to other targets and deployers. cloud: TestAccept/bundle/config-remote-sync/cluster_policy_cross_target TestAccept/bundle/config-remote-sync/cluster_policy_remote_addition TestAccept/bundle/config-remote-sync/policy_injected_cluster_fields TestAccept/bundle/config-remote-sync/cluster_policy_permissions Co-authored-by: Isaac --- .../cluster_policy_permissions/output.txt | 6 ++-- .../cluster_policy_permissions/script | 2 +- bundle/configsync/diff_test.go | 19 +++++------ bundle/configsync/permissions.go | 33 +++++++++---------- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt index 6a59ad3e076..784b59eb366 100644 --- a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/output.txt @@ -22,9 +22,9 @@ Resource: resources.cluster_policies.policy >>> [CLI] bundle validate -o json -[{"principal":"[USERNAME]","level":"CAN_USE"},{"principal":"users","level":"CAN_USE"}] +[{"principal":"users","level":"CAN_USE"}] -=== Deploy keeps the creator's CAN_USE +=== Deploy applies the synced ACL >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/[UNIQUE_NAME]/files... @@ -33,7 +33,7 @@ Files: 1 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 1 unchanged >>> [CLI] permissions get cluster-policies [POLICY_ID] -[{"principal":"[USERNAME]","level":"CAN_USE"},{"principal":"users","level":"CAN_USE"}] +[{"principal":"users","level":"CAN_USE"}] >>> [CLI] bundle plan Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged diff --git a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script index f52d9078740..ad422c18cda 100644 --- a/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script +++ b/acceptance/bundle/config-remote-sync/cluster_policy_permissions/script @@ -25,7 +25,7 @@ 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 keeps the creator's CAN_USE" +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/bundle/configsync/diff_test.go b/bundle/configsync/diff_test.go index 6a5d1d0fecf..298b54b0d6c 100644 --- a/bundle/configsync/diff_test.go +++ b/bundle/configsync/diff_test.go @@ -388,22 +388,21 @@ func TestWithoutCurrentUserDefaults(t *testing.T) { } func TestWithoutCurrentUserDefaultsClusterPolicy(t *testing.T) { - creatorOnly := []dresources.StatePermission{ - {Level: iam.PermissionLevelCanUse, ServicePrincipalName: "me@example.com"}, - } - assert.Empty(t, withoutCurrentUserDefaults(creatorOnly, "me@example.com", "cluster_policies")) - - // With other grants, the current user's CAN_USE is kept, or the next deploy would revoke it. - withOthers := []dresources.StatePermission{ + 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, withOthers, withoutCurrentUserDefaults(slices.Clone(withOthers), "me@example.com", "cluster_policies")) - otherCreator := []dresources.StatePermission{ + 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.Equal(t, otherCreator, withoutCurrentUserDefaults(slices.Clone(otherCreator), "me@example.com", "cluster_policies")) + 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 c0355664e97..ca99b1ad31b 100644 --- a/bundle/configsync/permissions.go +++ b/bundle/configsync/permissions.go @@ -234,28 +234,27 @@ func lookupDeployedField(b *bundle.Bundle, resourceKey, field string) (any, erro return structaccess.Get(remote, path) } -// withoutCurrentUserDefaults 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. -// -// A cluster policy supports only CAN_USE, which the backend grants its creator and FixPermissions -// does not add. The current user's CAN_USE is dropped only when it is the whole ACL: once other -// principals are imported, it is kept so that the next deploy does not revoke it. +// 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 { - isCurrentUser := func(p dresources.StatePermission) bool { - return currentUser != "" && (p.UserName == currentUser || p.ServicePrincipalName == currentUser) - } - if resourceType == "cluster_policies" { - if len(permissions) == 1 && isCurrentUser(permissions[0]) && permissions[0].Level == iam.PermissionLevelCanUse { - return nil - } - return permissions - } return slices.DeleteFunc(permissions, func(p dresources.StatePermission) bool { - return isCurrentUser(p) && (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 ""