From 59052069bc0fb099d65206ec51330b67d3399478 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 11 Mar 2015 17:56:01 -0700 Subject: [PATCH] vault: Router can check for matching mounts --- vault/router.go | 11 +++++++++++ vault/router_test.go | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/vault/router.go b/vault/router.go index 7b899bed5..edb685acb 100644 --- a/vault/router.go +++ b/vault/router.go @@ -91,6 +91,17 @@ func (r *Router) Remount(src, dst string) error { return nil } +// MatchingMOunt returns the mount prefix that would be used for a path +func (r *Router) MatchingMount(path string) string { + r.l.RLock() + mount, _, ok := r.root.LongestPrefix(path) + r.l.RUnlock() + if !ok { + return "" + } + return mount +} + // Route is used to route a given request func (r *Router) Route(req *Request) (*Response, error) { // Find the mount point diff --git a/vault/router_test.go b/vault/router_test.go index f975267d5..050f9af6a 100644 --- a/vault/router_test.go +++ b/vault/router_test.go @@ -39,6 +39,14 @@ func TestRouter_Mount(t *testing.T) { t.Fatalf("err: %v", err) } + if path := r.MatchingMount("prod/aws/foo"); path != "prod/aws/" { + t.Fatalf("bad: %s", path) + } + + if path := r.MatchingMount("stage/aws/foo"); path != "" { + t.Fatalf("bad: %s", path) + } + req := &Request{ Path: "prod/aws/foo", }