2015-03-06 01:23:56 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/armon/go-radix"
|
2015-03-15 21:53:41 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-06 01:23:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Router is used to do prefix based routing of a request to a logical backend
|
|
|
|
type Router struct {
|
|
|
|
l sync.RWMutex
|
|
|
|
root *radix.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouter returns a new router
|
|
|
|
func NewRouter() *Router {
|
|
|
|
r := &Router{
|
|
|
|
root: radix.New(),
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// mountEntry is used to represent a mount point
|
|
|
|
type mountEntry struct {
|
2015-04-02 18:12:13 +00:00
|
|
|
tainted bool
|
2015-03-23 18:47:55 +00:00
|
|
|
backend logical.Backend
|
|
|
|
view *BarrierView
|
|
|
|
rootPaths *radix.Tree
|
|
|
|
loginPaths *radix.Tree
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount is used to expose a logical backend at a given prefix
|
2015-03-18 22:48:14 +00:00
|
|
|
func (r *Router) Mount(backend logical.Backend, prefix string, view *BarrierView) error {
|
2015-03-06 01:23:56 +00:00
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
|
|
|
|
// Check if this is a nested mount
|
|
|
|
if existing, _, ok := r.root.LongestPrefix(prefix); ok && existing != "" {
|
|
|
|
return fmt.Errorf("cannot mount under existing mount '%s'", existing)
|
|
|
|
}
|
|
|
|
|
2015-03-31 00:46:18 +00:00
|
|
|
// Build the paths
|
|
|
|
paths := backend.SpecialPaths()
|
|
|
|
if paths == nil {
|
|
|
|
paths = new(logical.Paths)
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a mount entry
|
|
|
|
me := &mountEntry{
|
2015-04-02 18:12:13 +00:00
|
|
|
tainted: false,
|
2015-03-23 18:47:55 +00:00
|
|
|
backend: backend,
|
|
|
|
view: view,
|
2015-03-31 00:46:18 +00:00
|
|
|
rootPaths: pathsToRadix(paths.Root),
|
|
|
|
loginPaths: pathsToRadix(paths.Unauthenticated),
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
r.root.Insert(prefix, me)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount is used to remove a logical backend from a given prefix
|
|
|
|
func (r *Router) Unmount(prefix string) error {
|
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
r.root.Delete(prefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remount is used to change the mount location of a logical backend
|
|
|
|
func (r *Router) Remount(src, dst string) error {
|
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
|
|
|
|
// Check for existing mount
|
|
|
|
raw, ok := r.root.Get(src)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("no mount at '%s'", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the mount point
|
|
|
|
r.root.Delete(src)
|
|
|
|
r.root.Insert(dst, raw)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:12:13 +00:00
|
|
|
// Taint is used to mark a path as tainted. This means only RollbackOperation
|
|
|
|
// RenewOperation requests are allowed to proceed
|
|
|
|
func (r *Router) Taint(path string) error {
|
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
if ok {
|
|
|
|
raw.(*mountEntry).tainted = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 19:01:53 +00:00
|
|
|
// Untaint is used to unmark a path as tainted.
|
|
|
|
func (r *Router) Untaint(path string) error {
|
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
if ok {
|
|
|
|
raw.(*mountEntry).tainted = false
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:03:59 +00:00
|
|
|
// MatchingMount returns the mount prefix that would be used for a path
|
2015-03-12 00:56:01 +00:00
|
|
|
func (r *Router) MatchingMount(path string) string {
|
|
|
|
r.l.RLock()
|
|
|
|
mount, _, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return mount
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:03:59 +00:00
|
|
|
// MatchingView returns the view used for a path
|
|
|
|
func (r *Router) MatchingView(path string) *BarrierView {
|
|
|
|
r.l.RLock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return raw.(*mountEntry).view
|
|
|
|
}
|
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
// Route is used to route a given request
|
2015-03-15 21:53:41 +00:00
|
|
|
func (r *Router) Route(req *logical.Request) (*logical.Response, error) {
|
2015-03-06 01:23:56 +00:00
|
|
|
// Find the mount point
|
|
|
|
r.l.RLock()
|
|
|
|
mount, raw, ok := r.root.LongestPrefix(req.Path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no handler for route '%s'", req.Path)
|
|
|
|
}
|
|
|
|
me := raw.(*mountEntry)
|
|
|
|
|
2015-04-02 18:12:13 +00:00
|
|
|
// If the path is tainted, we reject any operation except for
|
|
|
|
// Rollback and Revoke
|
|
|
|
if me.tainted {
|
|
|
|
switch req.Operation {
|
|
|
|
case logical.RevokeOperation, logical.RollbackOperation:
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("no handler for route '%s'", req.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 03:55:01 +00:00
|
|
|
// Determine if this path is an unauthenticated path before we modify it
|
|
|
|
loginPath := r.LoginPath(req.Path)
|
|
|
|
|
2015-03-24 22:12:52 +00:00
|
|
|
// Adjust the path to exclude the routing prefix
|
2015-03-06 01:23:56 +00:00
|
|
|
original := req.Path
|
|
|
|
req.Path = strings.TrimPrefix(req.Path, mount)
|
2015-03-24 22:12:52 +00:00
|
|
|
|
|
|
|
// Attach the storage view for the request
|
2015-03-15 20:54:20 +00:00
|
|
|
req.Storage = me.view
|
2015-03-24 22:12:52 +00:00
|
|
|
|
|
|
|
// Clear the request token unless this is the token backend
|
|
|
|
clientToken := req.ClientToken
|
|
|
|
if !strings.HasPrefix(original, "auth/token/") {
|
|
|
|
req.ClientToken = ""
|
|
|
|
}
|
2015-03-06 01:23:56 +00:00
|
|
|
|
2015-03-31 03:55:01 +00:00
|
|
|
// If the request is not a login path, then clear the connection
|
|
|
|
originalConn := req.Connection
|
|
|
|
if !loginPath {
|
|
|
|
req.Connection = nil
|
|
|
|
}
|
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
// Reset the request before returning
|
|
|
|
defer func() {
|
|
|
|
req.Path = original
|
2015-03-31 03:55:01 +00:00
|
|
|
req.Connection = originalConn
|
2015-03-15 20:54:20 +00:00
|
|
|
req.Storage = nil
|
2015-03-24 18:09:25 +00:00
|
|
|
req.ClientToken = clientToken
|
2015-03-06 01:23:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Invoke the backend
|
|
|
|
return me.backend.HandleRequest(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootPath checks if the given path requires root privileges
|
|
|
|
func (r *Router) RootPath(path string) bool {
|
|
|
|
r.l.RLock()
|
|
|
|
mount, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
me := raw.(*mountEntry)
|
|
|
|
|
|
|
|
// Trim to get remaining path
|
|
|
|
remain := strings.TrimPrefix(path, mount)
|
|
|
|
|
|
|
|
// Check the rootPaths of this backend
|
|
|
|
match, raw, ok := me.rootPaths.LongestPrefix(remain)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
prefixMatch := raw.(bool)
|
|
|
|
|
|
|
|
// Handle the prefix match case
|
|
|
|
if prefixMatch {
|
|
|
|
return strings.HasPrefix(remain, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the exact match case
|
|
|
|
return match == remain
|
|
|
|
}
|
2015-03-23 18:47:55 +00:00
|
|
|
|
|
|
|
// LoginPath checks if the given path is used for logins
|
|
|
|
func (r *Router) LoginPath(path string) bool {
|
|
|
|
r.l.RLock()
|
|
|
|
mount, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
me := raw.(*mountEntry)
|
|
|
|
|
|
|
|
// Trim to get remaining path
|
|
|
|
remain := strings.TrimPrefix(path, mount)
|
|
|
|
|
|
|
|
// Check the loginPaths of this backend
|
|
|
|
match, raw, ok := me.loginPaths.LongestPrefix(remain)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
prefixMatch := raw.(bool)
|
|
|
|
|
|
|
|
// Handle the prefix match case
|
|
|
|
if prefixMatch {
|
|
|
|
return strings.HasPrefix(remain, match)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the exact match case
|
|
|
|
return match == remain
|
|
|
|
}
|
|
|
|
|
2015-03-31 00:46:18 +00:00
|
|
|
// pathsToRadix converts a the mapping of special paths to a mapping
|
|
|
|
// of special paths to radix trees.
|
2015-03-23 18:47:55 +00:00
|
|
|
func pathsToRadix(paths []string) *radix.Tree {
|
|
|
|
tree := radix.New()
|
|
|
|
for _, path := range paths {
|
|
|
|
// Check if this is a prefix or exact match
|
|
|
|
prefixMatch := len(path) >= 1 && path[len(path)-1] == '*'
|
|
|
|
if prefixMatch {
|
|
|
|
path = path[:len(path)-1]
|
|
|
|
}
|
2015-03-31 00:46:18 +00:00
|
|
|
|
2015-03-23 18:47:55 +00:00
|
|
|
tree.Insert(path, prefixMatch)
|
|
|
|
}
|
2015-03-31 00:46:18 +00:00
|
|
|
|
2015-03-23 18:47:55 +00:00
|
|
|
return tree
|
|
|
|
}
|