Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = true
GOOS.windows = false
EnvMatrix.DMS = ["", "true"]
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = true
RecordRequests = false
Ignore = [".databricks", "databricks.yml"]
22 changes: 20 additions & 2 deletions bundle/configsync/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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) {
Expand Down
25 changes: 18 additions & 7 deletions bundle/configsync/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 ""
Expand Down
19 changes: 19 additions & 0 deletions libs/testserver/cluster_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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}}
}

Expand Down
Loading