Skip to content
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

Remove base path checks during sync #576

Merged
merged 1 commit into from
Jul 14, 2023
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
41 changes: 0 additions & 41 deletions libs/sync/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,6 @@ import (
"github.com/databricks/databricks-sdk-go/service/workspace"
)

// Return if the child path is nested under the parent path.
func isPathNestedUnder(child, parent string) bool {
child = path.Clean(child)
parent = path.Clean(parent)

// Traverse up the tree as long as "child" is contained in "parent".
for len(child) > len(parent) && strings.HasPrefix(child, parent) {
child = path.Dir(child)
if child == parent {
return true
}
}
return false
}

// Check if the specified path is nested under one of the allowed base paths.
func checkPathNestedUnderBasePaths(me *iam.User, p string) error {
validBasePaths := []string{
path.Clean(fmt.Sprintf("/Users/%s", me.UserName)),
path.Clean(fmt.Sprintf("/Repos/%s", me.UserName)),
}

givenBasePath := path.Clean(p)
match := false
for _, basePath := range validBasePaths {
if isPathNestedUnder(givenBasePath, basePath) {
match = true
break
}
}
if !match {
return fmt.Errorf("path must be nested under %s", strings.Join(validBasePaths, " or "))
}
return nil
}

func repoPathForPath(me *iam.User, remotePath string) string {
base := path.Clean(fmt.Sprintf("/Repos/%s", me.UserName))
remotePath = path.Clean(remotePath)
Expand All @@ -66,11 +30,6 @@ func EnsureRemotePathIsUsable(ctx context.Context, wsc *databricks.WorkspaceClie
return err
}

err = checkPathNestedUnderBasePaths(me, remotePath)
if err != nil {
return err
}

// Ensure that the remote path exists.
// If it is a repo, it has to exist.
// If it is a workspace path, it may not exist.
Expand Down
31 changes: 0 additions & 31 deletions libs/sync/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPathNestedUnderBasePaths(t *testing.T) {
me := iam.User{
UserName: "[email protected]",
}

// Not nested under allowed base paths.
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/."))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/.."))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/foo"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos/"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Repos"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/."))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/.."))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/foo"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users/"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/Users"))
assert.Error(t, checkPathNestedUnderBasePaths(&me, "/"))

// Nested under allowed base paths.
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/foo"))
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/./foo"))
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Repos/[email protected]/foo/bar/qux"))
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/foo"))
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/./foo"))
assert.NoError(t, checkPathNestedUnderBasePaths(&me, "/Users/[email protected]/foo/bar/qux"))
}

func TestPathToRepoPath(t *testing.T) {
me := iam.User{
UserName: "[email protected]",
Expand Down