Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 36 additions & 0 deletions bundle/permissions/workspace_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package permissions
import (
"context"
"fmt"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
Expand All @@ -18,6 +19,12 @@ func ApplyWorkspaceRootPermissions() bundle.Mutator {

// Apply implements bundle.Mutator.
func (*workspaceRootPermissions) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {

diags := checkWorkspaceRootPermissions(b)
if len(diags) > 0 {
return diags
Comment thread
andrewnester marked this conversation as resolved.
Outdated
}

err := giveAccessForWorkspaceRoot(ctx, b)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -77,3 +84,32 @@ func getWorkspaceObjectPermissionLevel(bundlePermission string) (workspace.Works
return "", fmt.Errorf("unsupported bundle permission level %s", bundlePermission)
}
}

// checkWorkspaceRootPermissions checks that if permissions are set for the workspace root, and workspace root starts with /Workspace/Shared, then permissions should be set for group: users
func checkWorkspaceRootPermissions(b *bundle.Bundle) diag.Diagnostics {
Comment thread
andrewnester marked this conversation as resolved.
Outdated
var diags diag.Diagnostics
if len(b.Config.Permissions) == 0 {
Comment thread
andrewnester marked this conversation as resolved.
Outdated
return nil
}

if !strings.HasPrefix(b.Config.Workspace.RootPath, "/Workspace/Shared") {
Comment thread
andrewnester marked this conversation as resolved.
Outdated
return nil
}

allUsers := false
for _, p := range b.Config.Permissions {
if p.GroupName == "users" && p.Level == CAN_MANAGE {
allUsers = true
}
}

if !allUsers {
diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: "workspace_root_permissions",
Comment thread
andrewnester marked this conversation as resolved.
Outdated
Detail: "bundle is configured to /Workspace/Shared, which will give read/write access to all users. If all users should have access, add CAN_MANAGE for 'group_name: users' permission to your bundle configuration. If the deployment should be restricted, move it to a restricted folder such as /Users/<username or principal name>",
Comment thread
andrewnester marked this conversation as resolved.
Outdated
})
}

return diags
}
66 changes: 65 additions & 1 deletion bundle/permissions/workspace_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/databricks-sdk-go/service/ml"
Expand Down Expand Up @@ -70,5 +71,68 @@ func TestApplyWorkspaceRootPermissions(t *testing.T) {
}).Return(nil, nil)

diags := bundle.Apply(context.Background(), b, ApplyWorkspaceRootPermissions())
require.NoError(t, diags.Error())
require.Empty(t, diags)
}

func TestApplyWorkspaceRootPermissionsForShared(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Workspace: config.Workspace{
RootPath: "/Workspace/Shared/foo/bar",
},
Permissions: []resources.Permission{
{Level: CAN_MANAGE, GroupName: "users"},
},
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job_1": {JobSettings: &jobs.JobSettings{Name: "job_1"}},
"job_2": {JobSettings: &jobs.JobSettings{Name: "job_2"}},
},
},
},
}

m := mocks.NewMockWorkspaceClient(t)
b.SetWorkpaceClient(m.WorkspaceClient)
workspaceApi := m.GetMockWorkspaceAPI()
workspaceApi.EXPECT().GetStatusByPath(mock.Anything, "/Workspace/Shared/foo/bar").Return(&workspace.ObjectInfo{
ObjectId: 1234,
}, nil)
workspaceApi.EXPECT().UpdatePermissions(mock.Anything, workspace.WorkspaceObjectPermissionsRequest{
AccessControlList: []workspace.WorkspaceObjectAccessControlRequest{
{GroupName: "users", PermissionLevel: "CAN_MANAGE"},
},
WorkspaceObjectId: "1234",
WorkspaceObjectType: "directories",
}).Return(nil, nil)

diags := bundle.Apply(context.Background(), b, ApplyWorkspaceRootPermissions())
require.Empty(t, diags)
}

func TestApplyWorkspaceRootPermissionsForSharedError(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Workspace: config.Workspace{
RootPath: "/Workspace/Shared/foo/bar",
},
Permissions: []resources.Permission{
{Level: CAN_MANAGE, UserName: "foo@bar.com"},
},
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job_1": {JobSettings: &jobs.JobSettings{Name: "job_1"}},
"job_2": {JobSettings: &jobs.JobSettings{Name: "job_2"}},
},
},
},
}

m := mocks.NewMockWorkspaceClient(t)
b.SetWorkpaceClient(m.WorkspaceClient)

diags := bundle.Apply(context.Background(), b, ApplyWorkspaceRootPermissions())
require.Len(t, diags, 1)
require.Equal(t, "workspace_root_permissions", diags[0].Summary)
require.Equal(t, diag.Warning, diags[0].Severity)
}