2015-03-18 22:30:31 +00:00
|
|
|
package vault
|
|
|
|
|
2015-03-18 22:46:07 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-03-19 16:54:57 +00:00
|
|
|
"strings"
|
2015-03-18 22:46:07 +00:00
|
|
|
|
2015-06-30 19:38:32 +00:00
|
|
|
"github.com/hashicorp/vault/helper/uuid"
|
2015-03-31 01:07:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-18 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// coreAuthConfigPath is used to store the auth configuration.
|
|
|
|
// Auth configuration is protected within the Vault itself, which means it
|
|
|
|
// can only be viewed or modified after an unseal.
|
|
|
|
coreAuthConfigPath = "core/auth"
|
|
|
|
|
|
|
|
// credentialBarrierPrefix is the prefix to the UUID used in the
|
|
|
|
// barrier view for the credential backends.
|
|
|
|
credentialBarrierPrefix = "auth/"
|
|
|
|
|
2015-03-19 16:56:39 +00:00
|
|
|
// credentialRoutePrefix is the mount prefix used for the router
|
|
|
|
credentialRoutePrefix = "auth/"
|
2015-03-18 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-09 19:42:29 +00:00
|
|
|
// errLoadAuthFailed if loadCreddentials encounters an error
|
|
|
|
errLoadAuthFailed = errors.New("failed to setup auth table")
|
2015-03-18 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
// enableCredential is used to enable a new credential backend
|
2015-03-19 16:54:57 +00:00
|
|
|
func (c *Core) enableCredential(entry *MountEntry) error {
|
2015-03-19 02:36:17 +00:00
|
|
|
c.auth.Lock()
|
|
|
|
defer c.auth.Unlock()
|
|
|
|
|
2015-04-03 21:24:00 +00:00
|
|
|
// Ensure we end the path in a slash
|
|
|
|
if !strings.HasSuffix(entry.Path, "/") {
|
|
|
|
entry.Path += "/"
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
// Ensure there is a name
|
2015-04-03 21:24:00 +00:00
|
|
|
if entry.Path == "/" {
|
2015-03-19 16:54:57 +00:00
|
|
|
return fmt.Errorf("backend path must be specified")
|
|
|
|
}
|
2015-03-19 02:36:17 +00:00
|
|
|
|
|
|
|
// Look for matching name
|
|
|
|
for _, ent := range c.auth.Entries {
|
2015-04-03 21:24:00 +00:00
|
|
|
switch {
|
|
|
|
// Existing is oauth/github/ new is oauth/ or
|
|
|
|
// existing is oauth/ and new is oauth/github/
|
|
|
|
case strings.HasPrefix(ent.Path, entry.Path):
|
|
|
|
fallthrough
|
|
|
|
case strings.HasPrefix(entry.Path, ent.Path):
|
2015-08-13 17:17:04 +00:00
|
|
|
return logical.CodedError(409, "path is already in use")
|
2015-03-19 02:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the token backend is a singleton
|
|
|
|
if entry.Type == "token" {
|
|
|
|
return fmt.Errorf("token credential backend cannot be instantiated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new UUID and view
|
2015-06-30 19:38:32 +00:00
|
|
|
entry.UUID = uuid.GenerateUUID()
|
2015-03-19 02:36:17 +00:00
|
|
|
view := NewBarrierView(c.barrier, credentialBarrierPrefix+entry.UUID+"/")
|
|
|
|
|
2015-07-01 00:30:43 +00:00
|
|
|
// Create the new backend
|
2015-09-04 20:58:12 +00:00
|
|
|
backend, err := c.newCredentialBackend(entry.Type, c.mountEntrySysView(entry), view, nil)
|
2015-07-01 00:30:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
// Update the auth table
|
2015-09-10 02:17:49 +00:00
|
|
|
newTable := c.auth.ShallowClone()
|
2015-03-19 02:36:17 +00:00
|
|
|
newTable.Entries = append(newTable.Entries, entry)
|
|
|
|
if err := c.persistAuth(newTable); err != nil {
|
|
|
|
return errors.New("failed to update auth table")
|
|
|
|
}
|
|
|
|
c.auth = newTable
|
|
|
|
|
|
|
|
// Mount the backend
|
2015-04-03 21:24:00 +00:00
|
|
|
path := credentialRoutePrefix + entry.Path
|
2015-09-04 20:58:12 +00:00
|
|
|
if err := c.router.Mount(backend, path, entry, view); err != nil {
|
2015-03-19 02:36:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-19 16:54:57 +00:00
|
|
|
c.logger.Printf("[INFO] core: enabled credential backend '%s' type: %s",
|
|
|
|
entry.Path, entry.Type)
|
2015-03-19 02:36:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// disableCredential is used to disable an existing credential backend
|
2015-03-19 16:54:57 +00:00
|
|
|
func (c *Core) disableCredential(path string) error {
|
2015-03-19 02:36:17 +00:00
|
|
|
c.auth.Lock()
|
|
|
|
defer c.auth.Unlock()
|
|
|
|
|
2015-04-03 21:24:00 +00:00
|
|
|
// Ensure we end the path in a slash
|
|
|
|
if !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
// Ensure the token backend is not affected
|
2015-04-03 21:24:00 +00:00
|
|
|
if path == "token/" {
|
2015-03-19 02:36:17 +00:00
|
|
|
return fmt.Errorf("token credential backend cannot be disabled")
|
|
|
|
}
|
|
|
|
|
2015-04-03 23:07:45 +00:00
|
|
|
// Store the view for this backend
|
|
|
|
fullPath := credentialRoutePrefix + path
|
2015-09-15 16:27:22 +00:00
|
|
|
view := c.router.MatchingStorageView(fullPath)
|
2015-04-03 23:09:06 +00:00
|
|
|
if view == nil {
|
|
|
|
return fmt.Errorf("no matching backend")
|
|
|
|
}
|
2015-04-03 23:07:45 +00:00
|
|
|
|
|
|
|
// Mark the entry as tainted
|
|
|
|
if err := c.taintCredEntry(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Taint the router path to prevent routing
|
|
|
|
if err := c.router.Taint(fullPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke credentials from this path
|
|
|
|
if err := c.expiration.RevokePrefix(fullPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount the backend
|
|
|
|
if err := c.router.Unmount(fullPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the data in the view
|
|
|
|
if view != nil {
|
|
|
|
if err := ClearView(view); err != nil {
|
|
|
|
return err
|
2015-03-19 02:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 23:07:45 +00:00
|
|
|
// Remove the mount table entry
|
|
|
|
if err := c.removeCredEntry(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.logger.Printf("[INFO] core: disabled credential backend '%s'", path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeCredEntry is used to remove an entry in the auth table
|
|
|
|
func (c *Core) removeCredEntry(path string) error {
|
|
|
|
// Taint the entry from the auth table
|
2015-09-10 02:17:49 +00:00
|
|
|
newTable := c.auth.ShallowClone()
|
2015-04-03 23:07:45 +00:00
|
|
|
newTable.Remove(path)
|
|
|
|
|
|
|
|
// Update the auth table
|
|
|
|
if err := c.persistAuth(newTable); err != nil {
|
|
|
|
return errors.New("failed to update auth table")
|
|
|
|
}
|
|
|
|
c.auth = newTable
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// taintCredEntry is used to mark an entry in the auth table as tainted
|
|
|
|
func (c *Core) taintCredEntry(path string) error {
|
|
|
|
// Taint the entry from the auth table
|
2015-09-10 02:17:49 +00:00
|
|
|
newTable := c.auth.ShallowClone()
|
2015-04-03 23:07:45 +00:00
|
|
|
found := newTable.SetTaint(path, true)
|
|
|
|
|
2015-03-19 02:36:17 +00:00
|
|
|
// Ensure there was a match
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("no matching backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the auth table
|
|
|
|
if err := c.persistAuth(newTable); err != nil {
|
|
|
|
return errors.New("failed to update auth table")
|
|
|
|
}
|
|
|
|
c.auth = newTable
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:46:07 +00:00
|
|
|
// loadCredentials is invoked as part of postUnseal to load the auth table
|
|
|
|
func (c *Core) loadCredentials() error {
|
|
|
|
// Load the existing mount table
|
|
|
|
raw, err := c.barrier.Get(coreAuthConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("[ERR] core: failed to read auth table: %v", err)
|
2015-09-09 19:42:29 +00:00
|
|
|
return errLoadAuthFailed
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
if raw != nil {
|
2015-03-19 16:54:57 +00:00
|
|
|
c.auth = &MountTable{}
|
2015-03-18 22:46:07 +00:00
|
|
|
if err := json.Unmarshal(raw.Value, c.auth); err != nil {
|
|
|
|
c.logger.Printf("[ERR] core: failed to decode auth table: %v", err)
|
2015-09-09 19:42:29 +00:00
|
|
|
return errLoadAuthFailed
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done if we have restored the auth table
|
|
|
|
if c.auth != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and persist the default auth table
|
|
|
|
c.auth = defaultAuthTable()
|
|
|
|
if err := c.persistAuth(c.auth); err != nil {
|
2015-09-09 19:42:29 +00:00
|
|
|
return errLoadAuthFailed
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistAuth is used to persist the auth table after modification
|
2015-03-19 16:54:57 +00:00
|
|
|
func (c *Core) persistAuth(table *MountTable) error {
|
2015-03-18 22:46:07 +00:00
|
|
|
// Marshal the table
|
|
|
|
raw, err := json.Marshal(table)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("[ERR] core: failed to encode auth table: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an entry
|
|
|
|
entry := &Entry{
|
|
|
|
Key: coreAuthConfigPath,
|
|
|
|
Value: raw,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the physical backend
|
|
|
|
if err := c.barrier.Put(entry); err != nil {
|
|
|
|
c.logger.Printf("[ERR] core: failed to persist auth table: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupCredentials is invoked after we've loaded the auth table to
|
2015-03-18 22:30:31 +00:00
|
|
|
// initialize the credential backends and setup the router
|
|
|
|
func (c *Core) setupCredentials() error {
|
2015-03-31 01:07:05 +00:00
|
|
|
var backend logical.Backend
|
2015-03-18 22:46:07 +00:00
|
|
|
var view *BarrierView
|
|
|
|
var err error
|
|
|
|
for _, entry := range c.auth.Entries {
|
2015-07-01 00:30:43 +00:00
|
|
|
// Create a barrier view using the UUID
|
|
|
|
view = NewBarrierView(c.barrier, credentialBarrierPrefix+entry.UUID+"/")
|
|
|
|
|
2015-03-18 22:46:07 +00:00
|
|
|
// Initialize the backend
|
2015-09-04 20:58:12 +00:00
|
|
|
backend, err = c.newCredentialBackend(entry.Type, c.mountEntrySysView(entry), view, nil)
|
2015-03-18 22:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf(
|
|
|
|
"[ERR] core: failed to create credential entry %#v: %v",
|
|
|
|
entry, err)
|
2015-09-09 19:42:29 +00:00
|
|
|
return errLoadAuthFailed
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the backend
|
2015-04-03 21:24:00 +00:00
|
|
|
path := credentialRoutePrefix + entry.Path
|
2015-09-04 20:58:12 +00:00
|
|
|
err = c.router.Mount(backend, path, entry, view)
|
2015-03-18 22:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Printf("[ERR] core: failed to mount auth entry %#v: %v", entry, err)
|
2015-09-09 19:42:29 +00:00
|
|
|
return errLoadAuthFailed
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
2015-03-23 20:41:05 +00:00
|
|
|
|
2015-04-03 23:07:45 +00:00
|
|
|
// Ensure the path is tainted if set in the mount table
|
|
|
|
if entry.Tainted {
|
|
|
|
c.router.Taint(path)
|
|
|
|
}
|
|
|
|
|
2015-03-23 20:41:05 +00:00
|
|
|
// Check if this is the token store
|
|
|
|
if entry.Type == "token" {
|
|
|
|
c.tokenStore = backend.(*TokenStore)
|
2015-09-15 16:27:22 +00:00
|
|
|
|
|
|
|
// this is loaded *after* the normal mounts, including cubbyhole
|
|
|
|
c.router.tokenStoreSalt = backend.(*TokenStore).salt
|
2015-09-15 17:49:53 +00:00
|
|
|
c.tokenStore.cubbyholeBackend = c.router.MatchingBackend("cubbyhole/").(*CubbyholeBackend)
|
2015-03-23 20:41:05 +00:00
|
|
|
}
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
2015-03-18 22:30:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// teardownCredentials is used before we seal the vault to reset the credential
|
|
|
|
// backends to their unloaded state. This is reversed by loadCredentials.
|
|
|
|
func (c *Core) teardownCredentials() error {
|
2015-03-18 22:46:07 +00:00
|
|
|
c.auth = nil
|
2015-03-23 20:41:05 +00:00
|
|
|
c.tokenStore = nil
|
2015-03-18 22:30:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-18 22:46:07 +00:00
|
|
|
|
|
|
|
// newCredentialBackend is used to create and configure a new credential backend by name
|
2015-03-31 01:07:05 +00:00
|
|
|
func (c *Core) newCredentialBackend(
|
2015-09-04 20:58:12 +00:00
|
|
|
t string, sysView logical.SystemView, view logical.Storage, conf map[string]string) (logical.Backend, error) {
|
2015-03-18 22:46:07 +00:00
|
|
|
f, ok := c.credentialBackends[t]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown backend type: %s", t)
|
|
|
|
}
|
2015-03-31 01:07:05 +00:00
|
|
|
|
2015-07-01 00:30:43 +00:00
|
|
|
config := &logical.BackendConfig{
|
2015-09-09 19:42:29 +00:00
|
|
|
StorageView: view,
|
|
|
|
Logger: c.logger,
|
|
|
|
Config: conf,
|
2015-09-10 01:58:09 +00:00
|
|
|
System: sysView,
|
2015-07-01 00:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := f(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 20:58:12 +00:00
|
|
|
|
2015-07-01 00:30:43 +00:00
|
|
|
return b, nil
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultAuthTable creates a default auth table
|
2015-03-19 16:54:57 +00:00
|
|
|
func defaultAuthTable() *MountTable {
|
|
|
|
table := &MountTable{}
|
|
|
|
tokenAuth := &MountEntry{
|
2015-04-03 21:24:00 +00:00
|
|
|
Path: "token/",
|
2015-03-18 22:46:07 +00:00
|
|
|
Type: "token",
|
|
|
|
Description: "token based credentials",
|
2015-06-30 19:38:32 +00:00
|
|
|
UUID: uuid.GenerateUUID(),
|
2015-03-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
table.Entries = append(table.Entries, tokenAuth)
|
|
|
|
return table
|
|
|
|
}
|