2017-08-08 04:18:59 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-08-08 04:18:59 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
// reloadPluginMounts reloads provided mounts, regardless of
|
|
|
|
// plugin name, as long as the backend type is plugin.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) reloadMatchingPluginMounts(ctx context.Context, mounts []string) error {
|
2017-08-08 04:18:59 +00:00
|
|
|
c.mountsLock.Lock()
|
|
|
|
defer c.mountsLock.Unlock()
|
|
|
|
|
|
|
|
var errors error
|
|
|
|
for _, mount := range mounts {
|
|
|
|
entry := c.router.MatchingMountEntry(mount)
|
|
|
|
if entry == nil {
|
|
|
|
errors = multierror.Append(errors, fmt.Errorf("cannot fetch mount entry on %s", mount))
|
|
|
|
continue
|
|
|
|
// return fmt.Errorf("cannot fetch mount entry on %s", mount)
|
|
|
|
}
|
|
|
|
|
|
|
|
var isAuth bool
|
|
|
|
fullPath := c.router.MatchingMount(mount)
|
|
|
|
if strings.HasPrefix(fullPath, credentialRoutePrefix) {
|
|
|
|
isAuth = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.Type == "plugin" {
|
2018-03-21 19:04:27 +00:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, isAuth)
|
2017-08-08 04:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = multierror.Append(errors, fmt.Errorf("cannot reload plugin on %s: %v", mount, err))
|
|
|
|
continue
|
|
|
|
}
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", entry.Config.PluginName, "path", entry.Path)
|
2017-08-08 04:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// reloadPlugin reloads all mounted backends that are of
|
|
|
|
// plugin pluginName (name of the plugin as registered in
|
|
|
|
// the plugin catalog).
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) reloadMatchingPlugin(ctx context.Context, pluginName string) error {
|
2017-08-08 04:18:59 +00:00
|
|
|
c.mountsLock.Lock()
|
|
|
|
defer c.mountsLock.Unlock()
|
|
|
|
|
|
|
|
// Filter mount entries that only matches the plugin name
|
|
|
|
for _, entry := range c.mounts.Entries {
|
|
|
|
if entry.Config.PluginName == pluginName && entry.Type == "plugin" {
|
2018-03-21 19:04:27 +00:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, false)
|
2017-08-08 04:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", pluginName, "path", entry.Path)
|
2017-08-08 04:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter auth mount entries that ony matches the plugin name
|
|
|
|
for _, entry := range c.auth.Entries {
|
|
|
|
if entry.Config.PluginName == pluginName && entry.Type == "plugin" {
|
2018-03-21 19:04:27 +00:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, true)
|
2017-08-08 04:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", pluginName, "path", entry.Path)
|
2017-08-08 04:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:04:27 +00:00
|
|
|
// reloadBackendCommon is a generic method to reload a backend provided a
|
|
|
|
// MountEntry.
|
|
|
|
func (c *Core) reloadBackendCommon(ctx context.Context, entry *MountEntry, isAuth bool) error {
|
2017-08-08 04:18:59 +00:00
|
|
|
path := entry.Path
|
|
|
|
|
2017-12-15 18:31:57 +00:00
|
|
|
if isAuth {
|
|
|
|
path = credentialRoutePrefix + path
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:18:59 +00:00
|
|
|
// Fast-path out if the backend doesn't exist
|
|
|
|
raw, ok := c.router.root.Get(path)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
re := raw.(*routeEntry)
|
2017-12-15 18:31:57 +00:00
|
|
|
|
2018-03-21 19:04:27 +00:00
|
|
|
// Grab the lock, this allows requests to drain before we cleanup the
|
|
|
|
// client.
|
|
|
|
re.l.Lock()
|
|
|
|
defer re.l.Unlock()
|
|
|
|
|
2017-12-15 18:31:57 +00:00
|
|
|
// Only call Cleanup if backend is initialized
|
|
|
|
if re.backend != nil {
|
|
|
|
// Call backend's Cleanup routine
|
2018-01-19 06:44:44 +00:00
|
|
|
re.backend.Cleanup(ctx)
|
2017-12-15 18:31:57 +00:00
|
|
|
}
|
2017-08-08 04:18:59 +00:00
|
|
|
|
|
|
|
view := re.storageView
|
|
|
|
|
|
|
|
sysView := c.mountEntrySysView(entry)
|
|
|
|
|
|
|
|
var backend logical.Backend
|
|
|
|
var err error
|
|
|
|
if !isAuth {
|
|
|
|
// Dispense a new backend
|
2018-03-21 19:04:27 +00:00
|
|
|
backend, err = c.newLogicalBackend(ctx, entry, sysView, view)
|
2017-08-08 04:18:59 +00:00
|
|
|
} else {
|
2018-03-21 19:04:27 +00:00
|
|
|
backend, err = c.newCredentialBackend(ctx, entry, sysView, view)
|
2017-08-08 04:18:59 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if backend == nil {
|
|
|
|
return fmt.Errorf("nil backend of type %q returned from creation function", entry.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the backend back
|
|
|
|
re.backend = backend
|
|
|
|
|
2018-03-16 17:35:19 +00:00
|
|
|
// Set paths as well
|
|
|
|
paths := backend.SpecialPaths()
|
|
|
|
if paths != nil {
|
|
|
|
re.rootPaths.Store(pathsToRadix(paths.Root))
|
|
|
|
re.loginPaths.Store(pathsToRadix(paths.Unauthenticated))
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:18:59 +00:00
|
|
|
return nil
|
|
|
|
}
|