2015-03-06 01:23:56 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-03-06 01:23:56 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-04-08 23:43:17 +00:00
|
|
|
"time"
|
2015-03-06 01:23:56 +00:00
|
|
|
|
2015-04-08 23:43:17 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2015-03-06 01:23:56 +00:00
|
|
|
"github.com/armon/go-radix"
|
2015-06-30 21:08:21 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
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 {
|
2017-06-26 17:14:36 +00:00
|
|
|
l sync.RWMutex
|
|
|
|
root *radix.Tree
|
|
|
|
mountUUIDCache *radix.Tree
|
|
|
|
mountAccessorCache *radix.Tree
|
2018-03-08 19:21:11 +00:00
|
|
|
tokenStoreSaltFunc func(context.Context) (*salt.Salt, error)
|
2017-01-07 23:18:22 +00:00
|
|
|
// storagePrefix maps the prefix used for storage (ala the BarrierView)
|
|
|
|
// to the backend. This is used to map a key back into the backend that owns it.
|
2017-09-15 13:02:29 +00:00
|
|
|
// For example, logical/uuid1/foobar -> secrets/ (kv backend) + foobar
|
2017-01-07 23:18:22 +00:00
|
|
|
storagePrefix *radix.Tree
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouter returns a new router
|
|
|
|
func NewRouter() *Router {
|
|
|
|
r := &Router{
|
2017-06-26 17:14:36 +00:00
|
|
|
root: radix.New(),
|
|
|
|
storagePrefix: radix.New(),
|
|
|
|
mountUUIDCache: radix.New(),
|
|
|
|
mountAccessorCache: radix.New(),
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:58:12 +00:00
|
|
|
// routeEntry is used to represent a mount point in the router
|
|
|
|
type routeEntry struct {
|
2017-10-23 18:59:37 +00:00
|
|
|
tainted bool
|
|
|
|
backend logical.Backend
|
|
|
|
mountEntry *MountEntry
|
|
|
|
storageView logical.Storage
|
|
|
|
storagePrefix string
|
|
|
|
rootPaths *radix.Tree
|
|
|
|
loginPaths *radix.Tree
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
type validateMountResponse struct {
|
|
|
|
MountType string `json:"mount_type" structs:"mount_type" mapstructure:"mount_type"`
|
|
|
|
MountAccessor string `json:"mount_accessor" structs:"mount_accessor" mapstructure:"mount_accessor"`
|
|
|
|
MountPath string `json:"mount_path" structs:"mount_path" mapstructure:"mount_path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateMountByAccessor returns the mount type and ID for a given mount
|
|
|
|
// accessor
|
|
|
|
func (r *Router) validateMountByAccessor(accessor string) *validateMountResponse {
|
|
|
|
if accessor == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mountEntry := r.MatchingMountByAccessor(accessor)
|
|
|
|
if mountEntry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-14 06:31:10 +00:00
|
|
|
mountPath := mountEntry.Path
|
|
|
|
if mountEntry.Table == credentialTableType {
|
|
|
|
mountPath = credentialRoutePrefix + mountPath
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
return &validateMountResponse{
|
|
|
|
MountAccessor: mountEntry.Accessor,
|
|
|
|
MountType: mountEntry.Type,
|
2017-11-14 06:31:10 +00:00
|
|
|
MountPath: mountPath,
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// SaltID is used to apply a salt and hash to an ID to make sure its not reversible
|
2015-09-04 20:58:12 +00:00
|
|
|
func (re *routeEntry) SaltID(id string) string {
|
|
|
|
return salt.SaltID(re.mountEntry.UUID, id, salt.SHA1Hash)
|
2015-04-03 21:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount is used to expose a logical backend at a given prefix, using a unique salt,
|
|
|
|
// and the barrier view for that path.
|
2015-09-15 16:27:22 +00:00
|
|
|
func (r *Router) Mount(backend logical.Backend, prefix string, mountEntry *MountEntry, storageView *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
|
2017-10-23 18:59:37 +00:00
|
|
|
var localView logical.Storage = storageView
|
2017-09-01 05:02:03 +00:00
|
|
|
paths := new(logical.Paths)
|
|
|
|
if backend != nil {
|
|
|
|
specialPaths := backend.SpecialPaths()
|
|
|
|
if specialPaths != nil {
|
|
|
|
paths = specialPaths
|
|
|
|
}
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a mount entry
|
2015-09-04 20:58:12 +00:00
|
|
|
re := &routeEntry{
|
2017-10-23 18:59:37 +00:00
|
|
|
tainted: false,
|
|
|
|
backend: backend,
|
|
|
|
mountEntry: mountEntry,
|
|
|
|
storagePrefix: storageView.prefix,
|
|
|
|
storageView: localView,
|
|
|
|
rootPaths: pathsToRadix(paths.Root),
|
|
|
|
loginPaths: pathsToRadix(paths.Unauthenticated),
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
2017-05-05 16:54:37 +00:00
|
|
|
|
2017-06-29 21:00:13 +00:00
|
|
|
switch {
|
|
|
|
case prefix == "":
|
|
|
|
return fmt.Errorf("missing prefix to be used for router entry; mount_path: %q, mount_type: %q", re.mountEntry.Path, re.mountEntry.Type)
|
2017-10-23 18:59:37 +00:00
|
|
|
case re.storagePrefix == "":
|
2017-06-29 21:00:13 +00:00
|
|
|
return fmt.Errorf("missing storage view prefix; mount_path: %q, mount_type: %q", re.mountEntry.Path, re.mountEntry.Type)
|
|
|
|
case re.mountEntry.UUID == "":
|
|
|
|
return fmt.Errorf("missing mount identifier; mount_path: %q, mount_type: %q", re.mountEntry.Path, re.mountEntry.Type)
|
|
|
|
case re.mountEntry.Accessor == "":
|
|
|
|
return fmt.Errorf("missing mount accessor; mount_path: %q, mount_type: %q", re.mountEntry.Path, re.mountEntry.Type)
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:58:12 +00:00
|
|
|
r.root.Insert(prefix, re)
|
2017-10-23 18:59:37 +00:00
|
|
|
r.storagePrefix.Insert(re.storagePrefix, re)
|
2017-06-17 03:54:19 +00:00
|
|
|
r.mountUUIDCache.Insert(re.mountEntry.UUID, re.mountEntry)
|
2017-06-26 17:14:36 +00:00
|
|
|
r.mountAccessorCache.Insert(re.mountEntry.Accessor, re.mountEntry)
|
2015-09-10 01:58:09 +00:00
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount is used to remove a logical backend from a given prefix
|
2018-01-19 06:44:44 +00:00
|
|
|
func (r *Router) Unmount(ctx context.Context, prefix string) error {
|
2015-03-06 01:23:56 +00:00
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
2015-09-10 14:11:37 +00:00
|
|
|
|
2017-01-07 23:18:22 +00:00
|
|
|
// Fast-path out if the backend doesn't exist
|
|
|
|
raw, ok := r.root.Get(prefix)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2015-09-10 14:11:37 +00:00
|
|
|
}
|
2017-01-07 23:18:22 +00:00
|
|
|
|
|
|
|
// Call backend's Cleanup routine
|
|
|
|
re := raw.(*routeEntry)
|
2017-10-23 18:59:37 +00:00
|
|
|
if re.backend != nil {
|
2018-01-19 06:44:44 +00:00
|
|
|
re.backend.Cleanup(ctx)
|
2017-10-23 18:59:37 +00:00
|
|
|
}
|
2017-01-07 23:18:22 +00:00
|
|
|
|
|
|
|
// Purge from the radix trees
|
2015-03-06 01:23:56 +00:00
|
|
|
r.root.Delete(prefix)
|
2017-10-23 18:59:37 +00:00
|
|
|
r.storagePrefix.Delete(re.storagePrefix)
|
2017-06-17 03:54:19 +00:00
|
|
|
r.mountUUIDCache.Delete(re.mountEntry.UUID)
|
2017-06-26 17:14:36 +00:00
|
|
|
r.mountAccessorCache.Delete(re.mountEntry.Accessor)
|
2017-05-05 16:54:37 +00:00
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
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
|
2016-03-01 01:29:04 +00:00
|
|
|
// RevokeOperation requests are allowed to proceed
|
2015-04-02 18:12:13 +00:00
|
|
|
func (r *Router) Taint(path string) error {
|
|
|
|
r.l.Lock()
|
|
|
|
defer r.l.Unlock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
if ok {
|
2015-09-04 20:58:12 +00:00
|
|
|
raw.(*routeEntry).tainted = true
|
2015-04-02 18:12:13 +00:00
|
|
|
}
|
|
|
|
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 {
|
2015-09-04 20:58:12 +00:00
|
|
|
raw.(*routeEntry).tainted = false
|
2015-04-02 19:01:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 03:54:19 +00:00
|
|
|
func (r *Router) MatchingMountByUUID(mountID string) *MountEntry {
|
2017-05-05 16:54:37 +00:00
|
|
|
if mountID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.l.RLock()
|
|
|
|
defer r.l.RUnlock()
|
|
|
|
|
2017-06-17 03:54:19 +00:00
|
|
|
_, raw, ok := r.mountUUIDCache.LongestPrefix(mountID)
|
2017-05-05 16:54:37 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw.(*MountEntry)
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:18:59 +00:00
|
|
|
// MatchingMountByAccessor returns the MountEntry by accessor lookup
|
2017-06-26 17:14:36 +00:00
|
|
|
func (r *Router) MatchingMountByAccessor(mountAccessor string) *MountEntry {
|
|
|
|
if mountAccessor == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r.l.RLock()
|
|
|
|
defer r.l.RUnlock()
|
|
|
|
|
|
|
|
_, raw, ok := r.mountAccessorCache.LongestPrefix(mountAccessor)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw.(*MountEntry)
|
|
|
|
}
|
|
|
|
|
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()
|
2017-11-06 20:29:09 +00:00
|
|
|
defer r.l.RUnlock()
|
|
|
|
var mount = r.matchingMountInternal(path)
|
|
|
|
return mount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) matchingMountInternal(path string) string {
|
2015-03-12 00:56:01 +00:00
|
|
|
mount, _, ok := r.root.LongestPrefix(path)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return mount
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:29:09 +00:00
|
|
|
// matchingPrefixInternal returns a mount prefix that a path may be a part of
|
|
|
|
func (r *Router) matchingPrefixInternal(path string) string {
|
|
|
|
var existing string = ""
|
|
|
|
fn := func(existing_path string, _v interface{}) bool {
|
|
|
|
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 {
|
|
|
|
r.l.RLock()
|
|
|
|
defer r.l.RUnlock()
|
|
|
|
if exact_match := r.matchingMountInternal(path); exact_match != "" {
|
|
|
|
return exact_match
|
|
|
|
}
|
|
|
|
if prefix_match := r.matchingPrefixInternal(path); prefix_match != "" {
|
|
|
|
return prefix_match
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-10-23 18:59:37 +00:00
|
|
|
// MatchingStorageByAPIPath/StoragePath returns the storage used for
|
|
|
|
// API/Storage paths respectively
|
|
|
|
func (r *Router) MatchingStorageByAPIPath(path string) logical.Storage {
|
|
|
|
return r.matchingStorage(path, true)
|
|
|
|
}
|
|
|
|
func (r *Router) MatchingStorageByStoragePath(path string) logical.Storage {
|
|
|
|
return r.matchingStorage(path, false)
|
|
|
|
}
|
|
|
|
func (r *Router) matchingStorage(path string, apiPath bool) logical.Storage {
|
|
|
|
var raw interface{}
|
|
|
|
var ok bool
|
2015-04-02 18:03:59 +00:00
|
|
|
r.l.RLock()
|
2017-10-23 18:59:37 +00:00
|
|
|
if apiPath {
|
|
|
|
_, raw, ok = r.root.LongestPrefix(path)
|
|
|
|
} else {
|
|
|
|
_, raw, ok = r.storagePrefix.LongestPrefix(path)
|
|
|
|
}
|
2015-04-02 18:03:59 +00:00
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 16:27:22 +00:00
|
|
|
return raw.(*routeEntry).storageView
|
2015-09-04 20:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MatchingMountEntry returns the MountEntry used for a path
|
|
|
|
func (r *Router) MatchingMountEntry(path string) *MountEntry {
|
|
|
|
r.l.RLock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return raw.(*routeEntry).mountEntry
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:18:59 +00:00
|
|
|
// MatchingBackend returns the backend used for a path
|
2015-09-15 15:28:07 +00:00
|
|
|
func (r *Router) MatchingBackend(path string) logical.Backend {
|
|
|
|
r.l.RLock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return raw.(*routeEntry).backend
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:58:12 +00:00
|
|
|
// MatchingSystemView returns the SystemView used for a path
|
|
|
|
func (r *Router) MatchingSystemView(path string) logical.SystemView {
|
|
|
|
r.l.RLock()
|
|
|
|
_, raw, ok := r.root.LongestPrefix(path)
|
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return raw.(*routeEntry).backend.System()
|
2015-04-02 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 18:59:37 +00:00
|
|
|
// MatchingStoragePrefixByAPIPath/StoragePath returns the mount path matching
|
|
|
|
// and storage prefix matching the given API/Storage path respectively
|
|
|
|
func (r *Router) MatchingStoragePrefixByAPIPath(path string) (string, string, bool) {
|
|
|
|
return r.matchingStoragePrefix(path, true)
|
|
|
|
}
|
|
|
|
func (r *Router) MatchingStoragePrefixByStoragePath(path string) (string, string, bool) {
|
|
|
|
return r.matchingStoragePrefix(path, false)
|
|
|
|
}
|
|
|
|
func (r *Router) matchingStoragePrefix(path string, apiPath bool) (string, string, bool) {
|
|
|
|
var raw interface{}
|
|
|
|
var ok bool
|
2017-01-07 23:18:22 +00:00
|
|
|
r.l.RLock()
|
2017-10-23 18:59:37 +00:00
|
|
|
if apiPath {
|
|
|
|
_, raw, ok = r.root.LongestPrefix(path)
|
|
|
|
} else {
|
|
|
|
_, raw, ok = r.storagePrefix.LongestPrefix(path)
|
|
|
|
}
|
2017-01-07 23:18:22 +00:00
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the mount path and storage prefix
|
|
|
|
re := raw.(*routeEntry)
|
|
|
|
mountPath := re.mountEntry.Path
|
2017-10-23 18:59:37 +00:00
|
|
|
prefix := re.storagePrefix
|
2017-07-04 18:00:36 +00:00
|
|
|
|
|
|
|
// Add back the prefix for credential backends
|
2017-10-23 18:59:37 +00:00
|
|
|
if !apiPath && strings.HasPrefix(path, credentialBarrierPrefix) {
|
2017-07-04 18:00:36 +00:00
|
|
|
mountPath = credentialRoutePrefix + mountPath
|
|
|
|
}
|
|
|
|
|
2017-01-07 23:18:22 +00:00
|
|
|
return mountPath, prefix, true
|
|
|
|
}
|
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
// Route is used to route a given request
|
2018-01-08 18:31:38 +00:00
|
|
|
func (r *Router) Route(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
|
|
|
resp, _, _, err := r.routeCommon(ctx, req, false)
|
2016-01-07 20:10:05 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Route is used to route a given existence check request
|
2018-01-08 18:31:38 +00:00
|
|
|
func (r *Router) RouteExistenceCheck(ctx context.Context, req *logical.Request) (bool, bool, error) {
|
|
|
|
_, ok, exists, err := r.routeCommon(ctx, req, true)
|
2016-01-12 20:09:16 +00:00
|
|
|
return ok, exists, err
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (r *Router) routeCommon(ctx context.Context, req *logical.Request, existenceCheck bool) (*logical.Response, bool, bool, error) {
|
2015-03-06 01:23:56 +00:00
|
|
|
// Find the mount point
|
|
|
|
r.l.RLock()
|
2017-08-31 20:50:03 +00:00
|
|
|
adjustedPath := req.Path
|
|
|
|
mount, raw, ok := r.root.LongestPrefix(adjustedPath)
|
|
|
|
if !ok && !strings.HasSuffix(adjustedPath, "/") {
|
2015-05-07 19:18:50 +00:00
|
|
|
// Re-check for a backend by appending a slash. This lets "foo" mean
|
|
|
|
// "foo/" at the root level which is almost always what we want.
|
2017-08-31 20:50:03 +00:00
|
|
|
adjustedPath += "/"
|
|
|
|
mount, raw, ok = r.root.LongestPrefix(adjustedPath)
|
2015-05-07 19:18:50 +00:00
|
|
|
}
|
2015-03-06 01:23:56 +00:00
|
|
|
r.l.RUnlock()
|
|
|
|
if !ok {
|
2016-01-12 20:09:16 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("no handler for route '%s'", req.Path)), false, false, logical.ErrUnsupportedPath
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
2017-08-31 20:50:03 +00:00
|
|
|
req.Path = adjustedPath
|
2015-04-09 00:09:10 +00:00
|
|
|
defer metrics.MeasureSince([]string{"route", string(req.Operation),
|
|
|
|
strings.Replace(mount, "/", "-", -1)}, time.Now())
|
2015-09-04 20:58:12 +00:00
|
|
|
re := raw.(*routeEntry)
|
2015-03-06 01:23:56 +00:00
|
|
|
|
2017-10-23 18:59:37 +00:00
|
|
|
// Filtered mounts will have a nil backend
|
|
|
|
if re.backend == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("no handler for route '%s'", req.Path)), false, false, logical.ErrUnsupportedPath
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:12:13 +00:00
|
|
|
// If the path is tainted, we reject any operation except for
|
|
|
|
// Rollback and Revoke
|
2015-09-04 20:58:12 +00:00
|
|
|
if re.tainted {
|
2015-04-02 18:12:13 +00:00
|
|
|
switch req.Operation {
|
|
|
|
case logical.RevokeOperation, logical.RollbackOperation:
|
|
|
|
default:
|
2016-01-12 20:09:16 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("no handler for route '%s'", req.Path)), false, false, logical.ErrUnsupportedPath
|
2015-04-02 18:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 22:12:52 +00:00
|
|
|
// Adjust the path to exclude the routing prefix
|
2016-09-29 04:01:28 +00:00
|
|
|
originalPath := req.Path
|
2015-03-06 01:23:56 +00:00
|
|
|
req.Path = strings.TrimPrefix(req.Path, mount)
|
2015-05-27 18:46:42 +00:00
|
|
|
req.MountPoint = mount
|
2017-04-24 19:15:50 +00:00
|
|
|
req.MountType = re.mountEntry.Type
|
2015-04-04 03:48:04 +00:00
|
|
|
if req.Path == "/" {
|
|
|
|
req.Path = ""
|
|
|
|
}
|
2015-03-24 22:12:52 +00:00
|
|
|
|
|
|
|
// Attach the storage view for the request
|
2015-09-15 16:27:22 +00:00
|
|
|
req.Storage = re.storageView
|
2015-03-24 22:12:52 +00:00
|
|
|
|
2017-10-11 17:21:20 +00:00
|
|
|
originalEntityID := req.EntityID
|
|
|
|
|
|
|
|
// Allow EntityID to passthrough to the system backend. This is required to
|
|
|
|
// allow clients to generate MFA credentials in respective entity objects
|
|
|
|
// in identity store via the system backend.
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(originalPath, "sys/"):
|
|
|
|
default:
|
|
|
|
req.EntityID = ""
|
|
|
|
}
|
|
|
|
|
2017-10-23 18:59:37 +00:00
|
|
|
// Hash the request token unless the request is being routed to the token
|
|
|
|
// or system backend.
|
2015-03-24 22:12:52 +00:00
|
|
|
clientToken := req.ClientToken
|
2015-09-10 01:58:09 +00:00
|
|
|
switch {
|
2016-09-29 04:01:28 +00:00
|
|
|
case strings.HasPrefix(originalPath, "auth/token/"):
|
|
|
|
case strings.HasPrefix(originalPath, "sys/"):
|
|
|
|
case strings.HasPrefix(originalPath, "cubbyhole/"):
|
2015-09-10 01:58:09 +00:00
|
|
|
// In order for the token store to revoke later, we need to have the same
|
|
|
|
// salted ID, so we double-salt what's going to the cubbyhole backend
|
2018-03-08 19:21:11 +00:00
|
|
|
salt, err := r.tokenStoreSaltFunc(ctx)
|
2017-07-18 16:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, false, err
|
|
|
|
}
|
|
|
|
req.ClientToken = re.SaltID(salt.SaltID(req.ClientToken))
|
2015-09-10 01:58:09 +00:00
|
|
|
default:
|
2015-09-04 20:58:12 +00:00
|
|
|
req.ClientToken = re.SaltID(req.ClientToken)
|
2015-03-24 22:12:52 +00:00
|
|
|
}
|
2015-03-06 01:23:56 +00:00
|
|
|
|
2016-02-18 16:22:04 +00:00
|
|
|
// Cache the pointer to the original connection object
|
2015-03-31 03:55:01 +00:00
|
|
|
originalConn := req.Connection
|
|
|
|
|
2016-07-24 01:46:28 +00:00
|
|
|
// Cache the identifier of the request
|
|
|
|
originalReqID := req.ID
|
|
|
|
|
2017-03-08 22:36:50 +00:00
|
|
|
// Cache the client token's number of uses in the request
|
|
|
|
originalClientTokenRemainingUses := req.ClientTokenRemainingUses
|
|
|
|
req.ClientTokenRemainingUses = 0
|
|
|
|
|
2017-02-02 19:49:20 +00:00
|
|
|
// Cache the headers and hide them from backends
|
|
|
|
headers := req.Headers
|
|
|
|
req.Headers = nil
|
|
|
|
|
2017-01-04 21:44:03 +00:00
|
|
|
// Cache the wrap info of the request
|
|
|
|
var wrapInfo *logical.RequestWrapInfo
|
|
|
|
if req.WrapInfo != nil {
|
|
|
|
wrapInfo = &logical.RequestWrapInfo{
|
2017-11-09 17:47:42 +00:00
|
|
|
TTL: req.WrapInfo.TTL,
|
|
|
|
Format: req.WrapInfo.Format,
|
|
|
|
SealWrap: req.WrapInfo.SealWrap,
|
2017-01-04 21:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
|
2015-03-06 01:23:56 +00:00
|
|
|
// Reset the request before returning
|
|
|
|
defer func() {
|
2016-09-29 04:01:28 +00:00
|
|
|
req.Path = originalPath
|
2017-04-24 19:15:50 +00:00
|
|
|
req.MountPoint = mount
|
|
|
|
req.MountType = re.mountEntry.Type
|
2015-03-31 03:55:01 +00:00
|
|
|
req.Connection = originalConn
|
2016-07-24 01:46:28 +00:00
|
|
|
req.ID = originalReqID
|
2015-03-15 20:54:20 +00:00
|
|
|
req.Storage = nil
|
2015-03-24 18:09:25 +00:00
|
|
|
req.ClientToken = clientToken
|
2017-03-08 22:36:50 +00:00
|
|
|
req.ClientTokenRemainingUses = originalClientTokenRemainingUses
|
2017-01-04 21:44:03 +00:00
|
|
|
req.WrapInfo = wrapInfo
|
2017-02-02 19:49:20 +00:00
|
|
|
req.Headers = headers
|
2017-03-01 17:39:42 +00:00
|
|
|
// This is only set in one place, after routing, so should never be set
|
|
|
|
// by a backend
|
|
|
|
req.SetLastRemoteWAL(0)
|
2017-10-11 17:21:20 +00:00
|
|
|
|
|
|
|
// This will be used for attaching the mount accessor for the identities
|
|
|
|
// returned by the authentication backends
|
|
|
|
req.MountAccessor = re.mountEntry.Accessor
|
|
|
|
|
|
|
|
req.EntityID = originalEntityID
|
2015-03-06 01:23:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Invoke the backend
|
2016-01-07 20:10:05 +00:00
|
|
|
if existenceCheck {
|
2018-01-08 18:31:38 +00:00
|
|
|
ok, exists, err := re.backend.HandleExistenceCheck(ctx, req)
|
2016-01-12 20:09:16 +00:00
|
|
|
return nil, ok, exists, err
|
2016-01-07 20:10:05 +00:00
|
|
|
} else {
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := re.backend.HandleRequest(ctx, req)
|
2017-11-02 20:05:48 +00:00
|
|
|
// When a token gets renewed, the request hits this path and reaches
|
|
|
|
// token store. Token store delegates the renewal to the expiration
|
|
|
|
// manager. Expiration manager in-turn creates a different logical
|
|
|
|
// request and forwards the request to the auth backend that had
|
|
|
|
// initially authenticated the login request. The forwarding to auth
|
|
|
|
// backend will make this code path hit for the second time for the
|
|
|
|
// same renewal request. The accessors in the Alias structs should be
|
|
|
|
// of the auth backend and not of the token store. Therefore, avoiding
|
|
|
|
// the overwriting of accessors by having a check for path prefix
|
|
|
|
// having "renew". This gets applied for "renew" and "renew-self"
|
|
|
|
// requests.
|
2017-10-11 17:21:20 +00:00
|
|
|
if resp != nil &&
|
|
|
|
resp.Auth != nil &&
|
2017-11-02 20:05:48 +00:00
|
|
|
!strings.HasPrefix(req.Path, "renew") {
|
|
|
|
if resp.Auth.Alias != nil {
|
|
|
|
resp.Auth.Alias.MountAccessor = re.mountEntry.Accessor
|
|
|
|
}
|
|
|
|
for _, alias := range resp.Auth.GroupAliases {
|
|
|
|
alias.MountAccessor = re.mountEntry.Accessor
|
|
|
|
}
|
2017-10-11 17:21:20 +00:00
|
|
|
}
|
2016-01-12 20:09:16 +00:00
|
|
|
return resp, false, false, err
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
2015-03-06 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2015-09-04 20:58:12 +00:00
|
|
|
re := raw.(*routeEntry)
|
2015-03-06 01:23:56 +00:00
|
|
|
|
|
|
|
// Trim to get remaining path
|
|
|
|
remain := strings.TrimPrefix(path, mount)
|
|
|
|
|
|
|
|
// Check the rootPaths of this backend
|
2015-09-04 20:58:12 +00:00
|
|
|
match, raw, ok := re.rootPaths.LongestPrefix(remain)
|
2015-03-06 01:23:56 +00:00
|
|
|
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
|
|
|
|
}
|
2015-09-04 20:58:12 +00:00
|
|
|
re := raw.(*routeEntry)
|
2015-03-23 18:47:55 +00:00
|
|
|
|
|
|
|
// Trim to get remaining path
|
|
|
|
remain := strings.TrimPrefix(path, mount)
|
|
|
|
|
|
|
|
// Check the loginPaths of this backend
|
2015-09-04 20:58:12 +00:00
|
|
|
match, raw, ok := re.loginPaths.LongestPrefix(remain)
|
2015-03-23 18:47:55 +00:00
|
|
|
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
|
|
|
|
}
|