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

More Mount Conflict Detection #2919

Merged
merged 17 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion vault/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *Core) enableCredential(entry *MountEntry) error {
return fmt.Errorf("token credential backend cannot be instantiated")
}

if match := c.router.MatchingMount(credentialRoutePrefix + entry.Path); match != "" {
if match := c.router.MountConflict(credentialRoutePrefix + entry.Path); match != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ease of understanding, can you change match here to conflict and in the error message say "conflicting mount at ..."?

return logical.CodedError(409, fmt.Sprintf("existing mount at %s", match))
}

Expand Down
4 changes: 2 additions & 2 deletions vault/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func (c *Core) mount(entry *MountEntry) error {
c.mountsLock.Lock()
defer c.mountsLock.Unlock()

// Verify there is no conflicting mount
if match := c.router.MatchingMount(entry.Path); match != "" {
// Verify there are no conflicting mounts
if match := c.router.MountConflict(entry.Path); match != "" {
return logical.CodedError(409, fmt.Sprintf("existing mount at %s", match))
}

Expand Down
33 changes: 33 additions & 0 deletions vault/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func (r *Router) Mount(backend logical.Backend, prefix string, mountEntry *Mount
if existing, _, ok := r.root.LongestPrefix(prefix); ok && existing != "" {
return fmt.Errorf("cannot mount under existing mount '%s'", existing)
}
// If this is a secret backend, check to see if the prefix conflicts
// with an existing mountpoint
if prefix != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that we don't allow empty prefixes in the API calls or core mount function. Have you seen this? This if seems unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this check appears unnecessary.

if match := r.MatchingPrefix(prefix); match != "" {
return fmt.Errorf("cannot mount under existing mount '%s'", match)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this checking for mounting over an existing mount?

}
}

// Build the paths
paths := backend.SpecialPaths()
Expand Down Expand Up @@ -174,6 +181,32 @@ func (r *Router) MatchingMount(path string) string {
return mount
}

//MatchingPrefix returns a mount prefix that a path may be a part of
func (r *Router) MatchingPrefix(path string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't get a read lock but should, as it can be called externally (via MountConflict from the mount code). However, it is called with a lock already held from the router's Mount function.

The way to deal with this would be to have MatchingPrefix grab a read lock, then call matchingPrefixInternal which has the actual logic. The router's Mount function would then just call matchingPrefixInternal directly.

var existing string = ""
fn := func(existing_path string, _v interface{}) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function executes at all wouldn't it mean that we've found a prefix? Is the if check unnecessary here?

fmt.Println(fmt.Sprintf("comparing %v %v", path, existing_path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this was a debug line you meant to take out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How embarrassing 😬

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly it was like, um, that's going to spam logs a LOT :-)

Copy link
Contributor Author

@otakup0pe otakup0pe Jun 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why I'm feeling a touch embarrassed 👼

New commit incorporates the feedback and I added a little blurb to the docs about mount conflicts.

if strings.HasPrefix(existing_path, path) {
existing = existing_path
return true
}
return false
}
r.root.WalkPrefix(path, fn)
return existing
}

// MountConflict determines if there are potential path conflicts
func (r *Router) MountConflict(path string) string {
if exact_match := r.MatchingMount(path); exact_match != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really this function should grab a single read lock for the entire function and then call matchingMountInternal and matchingPrefixInternal. That way nothing can change between the two functions.

return exact_match
}
if prefix_match := r.MatchingPrefix(path); prefix_match != "" {
return prefix_match
}
return ""
}

// MatchingView returns the view used for a path
func (r *Router) MatchingStorageView(path string) *BarrierView {
r.l.RLock()
Expand Down
22 changes: 22 additions & 0 deletions vault/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ func TestRouter_Mount(t *testing.T) {
t.Fatalf("err: %v", err)
}

meUUID, err = uuid.GenerateUUID()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to be placed just above the definition of subMountEntry?

if err != nil {
t.Fatal(err)
}

subMountEntry := &MountEntry{
Path: "prod/",
UUID: meUUID,
}

if r.MountConflict("prod/aws/") == "" {
t.Fatalf("bad: prod/aws/")
}
if r.MountConflict("prod/") == "" {
t.Fatalf("bad: prod/")
}

err = r.Mount(n, "prod/", subMountEntry, view)
if !strings.Contains(err.Error(), "cannot mount under existing mount") {
t.Fatalf("err: %v", err)
}

if path := r.MatchingMount("prod/aws/foo"); path != "prod/aws/" {
t.Fatalf("bad: %s", path)
}
Expand Down