databricks-cli/bundle/config/validate/folder_permissions.go

136 lines
3.7 KiB
Go
Raw Normal View History

2024-10-10 13:16:06 +00:00
package validate
import (
"context"
"fmt"
2024-10-14 10:33:47 +00:00
"path"
2024-10-10 13:16:06 +00:00
"strings"
"github.com/databricks/cli/bundle"
2024-10-14 10:33:47 +00:00
"github.com/databricks/cli/bundle/libraries"
2024-10-10 13:16:06 +00:00
"github.com/databricks/cli/bundle/permissions"
"github.com/databricks/cli/libs/diag"
2024-10-14 10:33:47 +00:00
"github.com/databricks/databricks-sdk-go/apierr"
2024-10-10 13:16:06 +00:00
"github.com/databricks/databricks-sdk-go/service/workspace"
"golang.org/x/sync/errgroup"
2024-10-16 12:32:20 +00:00
"golang.org/x/sync/singleflight"
2024-10-10 13:16:06 +00:00
)
type folderPermissions struct {
}
// Apply implements bundle.ReadOnlyMutator.
func (f *folderPermissions) Apply(ctx context.Context, b bundle.ReadOnlyBundle) diag.Diagnostics {
if len(b.Config().Permissions) == 0 {
return nil
}
2024-10-14 10:33:47 +00:00
rootPath := b.Config().Workspace.RootPath
paths := []string{}
if !libraries.IsVolumesPath(rootPath) {
paths = append(paths, rootPath)
}
if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}
2024-10-10 13:16:06 +00:00
2024-10-14 10:33:47 +00:00
if !strings.HasPrefix(b.Config().Workspace.ArtifactPath, rootPath) &&
!libraries.IsVolumesPath(b.Config().Workspace.ArtifactPath) {
2024-10-10 13:16:06 +00:00
paths = append(paths, b.Config().Workspace.ArtifactPath)
}
2024-10-14 10:33:47 +00:00
if !strings.HasPrefix(b.Config().Workspace.FilePath, rootPath) &&
!libraries.IsVolumesPath(b.Config().Workspace.FilePath) {
2024-10-10 13:16:06 +00:00
paths = append(paths, b.Config().Workspace.FilePath)
}
2024-10-14 10:33:47 +00:00
if !strings.HasPrefix(b.Config().Workspace.StatePath, rootPath) &&
!libraries.IsVolumesPath(b.Config().Workspace.StatePath) {
2024-10-10 13:16:06 +00:00
paths = append(paths, b.Config().Workspace.StatePath)
}
2024-10-14 10:33:47 +00:00
if !strings.HasPrefix(b.Config().Workspace.ResourcePath, rootPath) &&
!libraries.IsVolumesPath(b.Config().Workspace.ResourcePath) {
2024-10-10 13:16:06 +00:00
paths = append(paths, b.Config().Workspace.ResourcePath)
}
var diags diag.Diagnostics
2024-10-14 10:33:47 +00:00
g, ctx := errgroup.WithContext(ctx)
results := make([]diag.Diagnostics, len(paths))
2024-10-16 12:32:20 +00:00
syncGroup := new(singleflight.Group)
2024-10-14 10:33:47 +00:00
for i, p := range paths {
g.Go(func() error {
2024-10-16 12:32:20 +00:00
diags, err, _ := syncGroup.Do(p, func() (any, error) {
diags := checkFolderPermission(ctx, b, p)
return diags, nil
})
results[i] = diags.(diag.Diagnostics)
return err
2024-10-10 13:16:06 +00:00
})
}
2024-10-14 10:33:47 +00:00
if err := g.Wait(); err != nil {
2024-10-10 13:16:06 +00:00
return diag.FromErr(err)
}
2024-10-14 10:33:47 +00:00
for _, r := range results {
diags = diags.Extend(r)
}
2024-10-10 13:16:06 +00:00
return diags
}
func checkFolderPermission(ctx context.Context, b bundle.ReadOnlyBundle, folderPath string) diag.Diagnostics {
w := b.WorkspaceClient().Workspace
2024-10-14 10:33:47 +00:00
obj, err := getClosestExistingObject(ctx, w, folderPath)
2024-10-10 13:16:06 +00:00
if err != nil {
return diag.FromErr(err)
}
objPermissions, err := w.GetPermissions(ctx, workspace.GetWorkspaceObjectPermissionsRequest{
WorkspaceObjectId: fmt.Sprint(obj.ObjectId),
WorkspaceObjectType: "directories",
})
if err != nil {
return diag.FromErr(err)
}
2024-10-16 12:32:20 +00:00
p := permissions.ObjectAclToResourcePermissions(folderPath, objPermissions.AccessControlList)
2024-10-14 10:33:47 +00:00
return p.Compare(b.Config().Permissions)
}
func getClosestExistingObject(ctx context.Context, w workspace.WorkspaceInterface, folderPath string) (*workspace.ObjectInfo, error) {
2024-10-16 12:32:20 +00:00
for {
2024-10-14 10:33:47 +00:00
obj, err := w.GetStatusByPath(ctx, folderPath)
if err == nil {
return obj, nil
2024-10-10 13:16:06 +00:00
}
2024-10-16 12:32:20 +00:00
if !apierr.IsMissing(err) {
2024-10-14 10:33:47 +00:00
return nil, err
2024-10-10 13:16:06 +00:00
}
2024-10-16 12:32:20 +00:00
parent := path.Dir(folderPath)
// If the parent is the same as the current folder, then we have reached the root
if folderPath == parent {
break
2024-10-10 13:16:06 +00:00
}
2024-10-16 12:32:20 +00:00
folderPath = parent
2024-10-10 13:16:06 +00:00
}
2024-10-14 10:33:47 +00:00
return nil, fmt.Errorf("folder %s and its parent folders do not exist", folderPath)
2024-10-10 13:16:06 +00:00
}
// Name implements bundle.ReadOnlyMutator.
func (f *folderPermissions) Name() string {
return "validate:folder_permissions"
}
2024-10-16 12:32:20 +00:00
// ValidateFolderPermissions validates that permissions for the folders in Workspace file system matches
// the permissions in the top-level permissions section of the bundle.
2024-10-10 13:16:06 +00:00
func ValidateFolderPermissions() bundle.ReadOnlyMutator {
return &folderPermissions{}
}