-
Notifications
You must be signed in to change notification settings - Fork 180
Added validator for folder permissions #1824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
793a836
Added validator for folder permissions
andrewnester 0872d2a
check permissions for parent folder
andrewnester f0a4e9e
refactoring + tests
andrewnester 95f45af
Update bundle/permissions/check.go
andrewnester 21799b5
Update bundle/permissions/check.go
andrewnester 020705d
fixes
andrewnester 1ba769c
fixed test
andrewnester acac028
fixes
andrewnester a559bd0
Merge branch 'main' into feature/validate-folder-permissions
andrewnester 48b9571
dont show warning twice
andrewnester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "path" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/libraries" | ||
| "github.com/databricks/cli/bundle/permissions" | ||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/databricks-sdk-go/apierr" | ||
| "github.com/databricks/databricks-sdk-go/service/workspace" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| type folderPermissions struct { | ||
|
andrewnester marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Apply implements bundle.ReadOnlyMutator. | ||
| func (f *folderPermissions) Apply(ctx context.Context, b bundle.ReadOnlyBundle) diag.Diagnostics { | ||
| if len(b.Config().Permissions) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| rootPath := b.Config().Workspace.RootPath | ||
| paths := []string{} | ||
| if !libraries.IsVolumesPath(rootPath) { | ||
| paths = append(paths, rootPath) | ||
| } | ||
|
|
||
| if !strings.HasSuffix(rootPath, "/") { | ||
| rootPath += "/" | ||
| } | ||
|
|
||
| if !strings.HasPrefix(b.Config().Workspace.ArtifactPath, rootPath) && | ||
| !libraries.IsVolumesPath(b.Config().Workspace.ArtifactPath) { | ||
| paths = append(paths, b.Config().Workspace.ArtifactPath) | ||
| } | ||
|
|
||
| if !strings.HasPrefix(b.Config().Workspace.FilePath, rootPath) && | ||
| !libraries.IsVolumesPath(b.Config().Workspace.FilePath) { | ||
| paths = append(paths, b.Config().Workspace.FilePath) | ||
| } | ||
|
|
||
| if !strings.HasPrefix(b.Config().Workspace.StatePath, rootPath) && | ||
| !libraries.IsVolumesPath(b.Config().Workspace.StatePath) { | ||
| paths = append(paths, b.Config().Workspace.StatePath) | ||
| } | ||
|
|
||
| if !strings.HasPrefix(b.Config().Workspace.ResourcePath, rootPath) && | ||
| !libraries.IsVolumesPath(b.Config().Workspace.ResourcePath) { | ||
| paths = append(paths, b.Config().Workspace.ResourcePath) | ||
| } | ||
|
andrewnester marked this conversation as resolved.
|
||
|
|
||
| var diags diag.Diagnostics | ||
| g, ctx := errgroup.WithContext(ctx) | ||
| results := make([]diag.Diagnostics, len(paths)) | ||
| for i, p := range paths { | ||
| g.Go(func() error { | ||
| results[i] = checkFolderPermission(ctx, b, p) | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| if err := g.Wait(); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| for _, r := range results { | ||
| diags = diags.Extend(r) | ||
| } | ||
|
|
||
| return diags | ||
| } | ||
|
|
||
| func checkFolderPermission(ctx context.Context, b bundle.ReadOnlyBundle, folderPath string) diag.Diagnostics { | ||
| w := b.WorkspaceClient().Workspace | ||
| obj, err := getClosestExistingObject(ctx, w, folderPath) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| objPermissions, err := w.GetPermissions(ctx, workspace.GetWorkspaceObjectPermissionsRequest{ | ||
| WorkspaceObjectId: fmt.Sprint(obj.ObjectId), | ||
| WorkspaceObjectType: "directories", | ||
|
pietern marked this conversation as resolved.
|
||
| }) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| p := permissions.NewFromWorkspaceObjectAcl(folderPath, objPermissions.AccessControlList) | ||
| return p.Compare(b.Config().Permissions) | ||
| } | ||
|
|
||
| var cache = map[string]*workspace.ObjectInfo{} | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
|
|
||
| func getClosestExistingObject(ctx context.Context, w workspace.WorkspaceInterface, folderPath string) (*workspace.ObjectInfo, error) { | ||
| if obj, ok := cache[folderPath]; ok { | ||
| return obj, nil | ||
| } | ||
|
|
||
| for folderPath != "/" { | ||
| obj, err := w.GetStatusByPath(ctx, folderPath) | ||
| if err == nil { | ||
| cache[folderPath] = obj | ||
| return obj, nil | ||
| } | ||
|
|
||
| var aerr *apierr.APIError | ||
| if !errors.As(err, &aerr) { | ||
| return nil, err | ||
| } | ||
|
|
||
| if aerr.ErrorCode != "RESOURCE_DOES_NOT_EXIST" { | ||
| return nil, err | ||
| } | ||
|
andrewnester marked this conversation as resolved.
|
||
|
|
||
| folderPath = path.Dir(folderPath) | ||
| } | ||
|
|
||
| // Check "/" root folder | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| obj, err := w.GetStatusByPath(ctx, folderPath) | ||
| if err == nil { | ||
| cache[folderPath] = obj | ||
| return obj, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("folder %s and its parent folders do not exist", folderPath) | ||
| } | ||
|
|
||
| // Name implements bundle.ReadOnlyMutator. | ||
| func (f *folderPermissions) Name() string { | ||
| return "validate:folder_permissions" | ||
| } | ||
|
|
||
| func ValidateFolderPermissions() bundle.ReadOnlyMutator { | ||
| return &folderPermissions{} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/cli/bundle/permissions" | ||
| "github.com/databricks/databricks-sdk-go/apierr" | ||
| "github.com/databricks/databricks-sdk-go/experimental/mocks" | ||
| "github.com/databricks/databricks-sdk-go/service/workspace" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidateFolderPermissions(t *testing.T) { | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| setupTest(t) | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Workspace: config.Workspace{ | ||
| RootPath: "/Workspace/Users/foo@bar.com", | ||
| ArtifactPath: "/Workspace/Users/foo@bar.com/artifacts", | ||
| FilePath: "/Workspace/Users/foo@bar.com/files", | ||
| StatePath: "/Workspace/Users/foo@bar.com/state", | ||
| ResourcePath: "/Workspace/Users/foo@bar.com/resources", | ||
| }, | ||
| Permissions: []resources.Permission{ | ||
| {Level: permissions.CAN_MANAGE, UserName: "foo@bar.com"}, | ||
| }, | ||
| }, | ||
| } | ||
| m := mocks.NewMockWorkspaceClient(t) | ||
| api := m.GetMockWorkspaceAPI() | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/Workspace/Users/foo@bar.com").Return(nil, &apierr.APIError{ | ||
| StatusCode: 404, | ||
| ErrorCode: "RESOURCE_DOES_NOT_EXIST", | ||
| }) | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/Workspace/Users").Return(nil, &apierr.APIError{ | ||
| StatusCode: 404, | ||
| ErrorCode: "RESOURCE_DOES_NOT_EXIST", | ||
| }) | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/Workspace").Return(&workspace.ObjectInfo{ | ||
| ObjectId: 1234, | ||
| }, nil) | ||
|
|
||
| api.EXPECT().GetPermissions(mock.Anything, workspace.GetWorkspaceObjectPermissionsRequest{ | ||
| WorkspaceObjectId: "1234", | ||
| WorkspaceObjectType: "directories", | ||
| }).Return(&workspace.WorkspaceObjectPermissions{ | ||
| ObjectId: "1234", | ||
| AccessControlList: []workspace.WorkspaceObjectAccessControlResponse{ | ||
| { | ||
| UserName: "foo@bar.com", | ||
| AllPermissions: []workspace.WorkspaceObjectPermission{ | ||
| {PermissionLevel: "CAN_MANAGE"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, nil) | ||
|
|
||
| b.SetWorkpaceClient(m.WorkspaceClient) | ||
| rb := bundle.ReadOnly(b) | ||
|
|
||
| diags := bundle.ApplyReadOnly(context.Background(), rb, ValidateFolderPermissions()) | ||
| require.Empty(t, diags) | ||
| } | ||
|
|
||
| func TestValidateFolderPermissionsDifferentCount(t *testing.T) { | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| setupTest(t) | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Workspace: config.Workspace{ | ||
| RootPath: "/Workspace/Users/foo@bar.com", | ||
| ArtifactPath: "/Workspace/Users/foo@bar.com/artifacts", | ||
| FilePath: "/Workspace/Users/foo@bar.com/files", | ||
| StatePath: "/Workspace/Users/foo@bar.com/state", | ||
| ResourcePath: "/Workspace/Users/foo@bar.com/resources", | ||
| }, | ||
| Permissions: []resources.Permission{ | ||
| {Level: permissions.CAN_MANAGE, UserName: "foo@bar.com"}, | ||
| }, | ||
| }, | ||
| } | ||
| m := mocks.NewMockWorkspaceClient(t) | ||
| api := m.GetMockWorkspaceAPI() | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/Workspace/Users/foo@bar.com").Return(&workspace.ObjectInfo{ | ||
| ObjectId: 1234, | ||
| }, nil) | ||
|
|
||
| api.EXPECT().GetPermissions(mock.Anything, workspace.GetWorkspaceObjectPermissionsRequest{ | ||
| WorkspaceObjectId: "1234", | ||
| WorkspaceObjectType: "directories", | ||
| }).Return(&workspace.WorkspaceObjectPermissions{ | ||
| ObjectId: "1234", | ||
| AccessControlList: []workspace.WorkspaceObjectAccessControlResponse{ | ||
| { | ||
| UserName: "foo@bar.com", | ||
| AllPermissions: []workspace.WorkspaceObjectPermission{ | ||
| {PermissionLevel: "CAN_MANAGE"}, | ||
| }, | ||
| }, | ||
| { | ||
| UserName: "foo2@bar.com", | ||
| AllPermissions: []workspace.WorkspaceObjectPermission{ | ||
| {PermissionLevel: "CAN_MANAGE"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, nil) | ||
|
|
||
| b.SetWorkpaceClient(m.WorkspaceClient) | ||
| rb := bundle.ReadOnly(b) | ||
|
|
||
| diags := bundle.ApplyReadOnly(context.Background(), rb, ValidateFolderPermissions()) | ||
| require.Len(t, diags, 1) | ||
| require.Equal(t, "permissions count mismatch", diags[0].Summary) | ||
| } | ||
|
|
||
| func TestValidateFolderPermissionsDifferentPermission(t *testing.T) { | ||
| setupTest(t) | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Workspace: config.Workspace{ | ||
| RootPath: "/Workspace/Users/foo@bar.com", | ||
| ArtifactPath: "/Workspace/Users/foo@bar.com/artifacts", | ||
| FilePath: "/Workspace/Users/foo@bar.com/files", | ||
| StatePath: "/Workspace/Users/foo@bar.com/state", | ||
| ResourcePath: "/Workspace/Users/foo@bar.com/resources", | ||
| }, | ||
| Permissions: []resources.Permission{ | ||
| {Level: permissions.CAN_MANAGE, UserName: "foo@bar.com"}, | ||
| }, | ||
| }, | ||
| } | ||
| m := mocks.NewMockWorkspaceClient(t) | ||
| api := m.GetMockWorkspaceAPI() | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/Workspace/Users/foo@bar.com").Return(&workspace.ObjectInfo{ | ||
| ObjectId: 1234, | ||
| }, nil) | ||
|
|
||
| api.EXPECT().GetPermissions(mock.Anything, workspace.GetWorkspaceObjectPermissionsRequest{ | ||
| WorkspaceObjectId: "1234", | ||
| WorkspaceObjectType: "directories", | ||
| }).Return(&workspace.WorkspaceObjectPermissions{ | ||
| ObjectId: "1234", | ||
| AccessControlList: []workspace.WorkspaceObjectAccessControlResponse{ | ||
| { | ||
| UserName: "foo2@bar.com", | ||
| AllPermissions: []workspace.WorkspaceObjectPermission{ | ||
| {PermissionLevel: "CAN_MANAGE"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, nil) | ||
|
|
||
| b.SetWorkpaceClient(m.WorkspaceClient) | ||
| rb := bundle.ReadOnly(b) | ||
|
|
||
| diags := bundle.ApplyReadOnly(context.Background(), rb, ValidateFolderPermissions()) | ||
| require.Len(t, diags, 1) | ||
| require.Equal(t, "permission not found", diags[0].Summary) | ||
| } | ||
|
|
||
| func TestNoRootFolder(t *testing.T) { | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| setupTest(t) | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Workspace: config.Workspace{ | ||
| RootPath: "/NotExisting", | ||
| ArtifactPath: "/NotExisting/artifacts", | ||
| FilePath: "/NotExisting/files", | ||
| StatePath: "/NotExisting/state", | ||
| ResourcePath: "/NotExisting/resources", | ||
| }, | ||
| Permissions: []resources.Permission{ | ||
| {Level: permissions.CAN_MANAGE, UserName: "foo@bar.com"}, | ||
| }, | ||
| }, | ||
| } | ||
| m := mocks.NewMockWorkspaceClient(t) | ||
| api := m.GetMockWorkspaceAPI() | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/NotExisting").Return(nil, &apierr.APIError{ | ||
| StatusCode: 404, | ||
| ErrorCode: "RESOURCE_DOES_NOT_EXIST", | ||
| }) | ||
| api.EXPECT().GetStatusByPath(mock.Anything, "/").Return(nil, &apierr.APIError{ | ||
| StatusCode: 404, | ||
| ErrorCode: "RESOURCE_DOES_NOT_EXIST", | ||
| }) | ||
|
|
||
| b.SetWorkpaceClient(m.WorkspaceClient) | ||
| rb := bundle.ReadOnly(b) | ||
|
|
||
| diags := bundle.ApplyReadOnly(context.Background(), rb, ValidateFolderPermissions()) | ||
| require.Len(t, diags, 1) | ||
| require.Equal(t, "folder / and its parent folders do not exist", diags[0].Summary) | ||
|
andrewnester marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func setupTest(t *testing.T) { | ||
| cache = make(map[string]*workspace.ObjectInfo) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.