2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 09:44:44 +03:00
|
|
|
"context"
|
2017-08-08 07:18:59 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2022-09-23 13:19:38 +03:00
|
|
|
"github.com/hashicorp/vault/helper/versions"
|
2018-09-18 06:03:00 +03:00
|
|
|
|
2022-09-21 22:25:04 +03:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2021-07-16 03:17:31 +03:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2022-09-23 13:19:38 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2019-04-13 00:54:35 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2022-08-30 05:42:26 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/plugin"
|
2017-08-08 07:18:59 +03:00
|
|
|
)
|
|
|
|
|
2022-05-25 21:37:42 +03:00
|
|
|
// reloadMatchingPluginMounts reloads provided mounts, regardless of
|
2017-08-08 07:18:59 +03:00
|
|
|
// plugin name, as long as the backend type is plugin.
|
2020-06-30 01:36:22 +03:00
|
|
|
func (c *Core) reloadMatchingPluginMounts(ctx context.Context, mounts []string) error {
|
2018-09-18 06:03:00 +03:00
|
|
|
c.mountsLock.RLock()
|
|
|
|
defer c.mountsLock.RUnlock()
|
|
|
|
c.authLock.RLock()
|
|
|
|
defer c.authLock.RUnlock()
|
|
|
|
|
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 07:18:59 +03:00
|
|
|
|
|
|
|
var errors error
|
|
|
|
for _, mount := range mounts {
|
2022-05-25 21:37:42 +03:00
|
|
|
var isAuth bool
|
|
|
|
// allow any of
|
|
|
|
// - sys/auth/foo/
|
|
|
|
// - sys/auth/foo
|
|
|
|
// - auth/foo/
|
|
|
|
// - auth/foo
|
|
|
|
if strings.HasPrefix(mount, credentialRoutePrefix) {
|
|
|
|
isAuth = true
|
2023-10-16 14:38:11 +03:00
|
|
|
} else if strings.HasPrefix(mount, mountPathSystem+credentialRoutePrefix) {
|
2022-05-25 21:37:42 +03:00
|
|
|
isAuth = true
|
2023-10-16 14:38:11 +03:00
|
|
|
mount = strings.TrimPrefix(mount, mountPathSystem)
|
2022-05-25 21:37:42 +03:00
|
|
|
}
|
|
|
|
if !strings.HasSuffix(mount, "/") {
|
|
|
|
mount += "/"
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
entry := c.router.MatchingMountEntry(ctx, mount)
|
2017-08-08 07:18:59 +03:00
|
|
|
if entry == nil {
|
2018-04-05 18:49:21 +03:00
|
|
|
errors = multierror.Append(errors, fmt.Errorf("cannot fetch mount entry on %q", mount))
|
2017-08-08 07:18:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
// We dont reload mounts that are not in the same namespace
|
|
|
|
if ns.ID != entry.Namespace().ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-07 04:21:24 +03:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, isAuth)
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
errors = multierror.Append(errors, fmt.Errorf("cannot reload plugin on %q: %w", mount, err))
|
2018-11-07 04:21:24 +03:00
|
|
|
continue
|
2017-08-08 07:18:59 +03:00
|
|
|
}
|
2022-10-06 14:54:27 +03:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", entry.Accessor, "path", entry.Path, "version", entry.Version)
|
2017-08-08 07:18:59 +03:00
|
|
|
}
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
// reloadPlugin reloads all mounted backends that are of
|
|
|
|
// plugin pluginName (name of the plugin as registered in
|
|
|
|
// the plugin catalog).
|
2020-06-30 01:36:22 +03:00
|
|
|
func (c *Core) reloadMatchingPlugin(ctx context.Context, pluginName string) error {
|
2018-09-18 06:03:00 +03:00
|
|
|
c.mountsLock.RLock()
|
|
|
|
defer c.mountsLock.RUnlock()
|
|
|
|
c.authLock.RLock()
|
|
|
|
defer c.authLock.RUnlock()
|
|
|
|
|
|
|
|
ns, err := namespace.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 07:18:59 +03:00
|
|
|
|
|
|
|
// Filter mount entries that only matches the plugin name
|
|
|
|
for _, entry := range c.mounts.Entries {
|
2018-09-18 06:03:00 +03:00
|
|
|
// We dont reload mounts that are not in the same namespace
|
|
|
|
if ns.ID != entry.Namespace().ID {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-30 01:36:22 +03:00
|
|
|
if entry.Type == pluginName || (entry.Type == "plugin" && entry.Config.PluginName == pluginName) {
|
2018-03-21 22:04:27 +03:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, false)
|
2017-08-08 07:18:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 14:54:27 +03:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", pluginName, "path", entry.Path, "version", entry.Version)
|
2017-08-08 07:18:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter auth mount entries that ony matches the plugin name
|
|
|
|
for _, entry := range c.auth.Entries {
|
2018-09-18 06:03:00 +03:00
|
|
|
// We dont reload mounts that are not in the same namespace
|
|
|
|
if ns.ID != entry.Namespace().ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:23:48 +03:00
|
|
|
if entry.Type == pluginName || (entry.Type == "plugin" && entry.Config.PluginName == pluginName) {
|
2018-03-21 22:04:27 +03:00
|
|
|
err := c.reloadBackendCommon(ctx, entry, true)
|
2017-08-08 07:18:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 14:54:27 +03:00
|
|
|
c.logger.Info("successfully reloaded plugin", "plugin", entry.Accessor, "path", entry.Path, "version", entry.Version)
|
2017-08-08 07:18:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:04:27 +03: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 {
|
2019-04-23 17:22:56 +03:00
|
|
|
// Make sure our cache is up-to-date. Since some singleton mounts can be
|
|
|
|
// tuned, we do this before the below check.
|
|
|
|
entry.SyncCache()
|
|
|
|
|
2018-05-21 21:05:04 +03:00
|
|
|
// We don't want to reload the singleton mounts. They often have specific
|
|
|
|
// inmemory elements and we don't want to touch them here.
|
|
|
|
if strutil.StrListContains(singletonMounts, entry.Type) {
|
2018-09-18 06:03:00 +03:00
|
|
|
c.logger.Debug("skipping reload of singleton mount", "type", entry.Type)
|
2018-05-21 21:05:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
path := entry.Path
|
|
|
|
|
2017-12-15 21:31:57 +03:00
|
|
|
if isAuth {
|
|
|
|
path = credentialRoutePrefix + path
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
// Fast-path out if the backend doesn't exist
|
2018-12-12 04:21:23 +03:00
|
|
|
raw, ok := c.router.root.Get(entry.Namespace().Path + path)
|
2017-08-08 07:18:59 +03:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
re := raw.(*routeEntry)
|
2017-12-15 21:31:57 +03:00
|
|
|
|
2018-03-21 22:04:27 +03:00
|
|
|
// Grab the lock, this allows requests to drain before we cleanup the
|
|
|
|
// client.
|
|
|
|
re.l.Lock()
|
|
|
|
defer re.l.Unlock()
|
|
|
|
|
2017-12-15 21:31:57 +03:00
|
|
|
// Only call Cleanup if backend is initialized
|
|
|
|
if re.backend != nil {
|
2022-08-30 05:42:26 +03:00
|
|
|
// Pass a context value so that the plugin client will call the
|
|
|
|
// appropriate cleanup method for reloading
|
|
|
|
reloadCtx := context.WithValue(ctx, plugin.ContextKeyPluginReload, "reload")
|
2017-12-15 21:31:57 +03:00
|
|
|
// Call backend's Cleanup routine
|
2022-08-30 05:42:26 +03:00
|
|
|
re.backend.Cleanup(reloadCtx)
|
2017-12-15 21:31:57 +03:00
|
|
|
}
|
2017-08-08 07:18:59 +03:00
|
|
|
|
|
|
|
view := re.storageView
|
2018-09-18 06:03:00 +03:00
|
|
|
viewPath := entry.UUID + "/"
|
|
|
|
switch entry.Table {
|
|
|
|
case mountTableType:
|
|
|
|
viewPath = backendBarrierPrefix + viewPath
|
|
|
|
case credentialTableType:
|
|
|
|
viewPath = credentialBarrierPrefix + viewPath
|
|
|
|
}
|
|
|
|
|
|
|
|
removePathCheckers(c, entry, viewPath)
|
2017-08-08 07:18:59 +03:00
|
|
|
|
|
|
|
sysView := c.mountEntrySysView(entry)
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
nilMount, err := preprocessMount(c, entry, view.(*BarrierView))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
var backend logical.Backend
|
2022-09-21 23:32:00 +03:00
|
|
|
oldSha := entry.RunningSha256
|
2017-08-08 07:18:59 +03:00
|
|
|
if !isAuth {
|
|
|
|
// Dispense a new backend
|
2022-09-21 23:32:00 +03:00
|
|
|
backend, entry.RunningSha256, err = c.newLogicalBackend(ctx, entry, sysView, view)
|
2017-08-08 07:18:59 +03:00
|
|
|
} else {
|
2022-09-21 23:32:00 +03:00
|
|
|
backend, entry.RunningSha256, err = c.newCredentialBackend(ctx, entry, sysView, view)
|
2017-08-08 07:18:59 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if backend == nil {
|
|
|
|
return fmt.Errorf("nil backend of type %q returned from creation function", entry.Type)
|
|
|
|
}
|
|
|
|
|
2022-09-23 13:19:38 +03:00
|
|
|
// update the entry running version with the configured version, which was verified during registration.
|
|
|
|
entry.RunningVersion = entry.Version
|
|
|
|
if entry.RunningVersion == "" {
|
|
|
|
// don't set the running version to a builtin if it is running as an external plugin
|
2023-04-12 20:34:35 +03:00
|
|
|
if entry.RunningSha256 == "" {
|
2022-09-23 13:19:38 +03:00
|
|
|
if isAuth {
|
|
|
|
entry.RunningVersion = versions.GetBuiltinVersion(consts.PluginTypeCredential, entry.Type)
|
|
|
|
} else {
|
|
|
|
entry.RunningVersion = versions.GetBuiltinVersion(consts.PluginTypeSecrets, entry.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:25:04 +03:00
|
|
|
// update the mount table since we changed the runningSha
|
2022-09-21 23:32:00 +03:00
|
|
|
if oldSha != entry.RunningSha256 && MountTableUpdateStorage {
|
2022-09-21 22:25:04 +03:00
|
|
|
if isAuth {
|
|
|
|
err = c.persistAuth(ctx, c.auth, &entry.Local)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = c.persistMounts(ctx, c.mounts, &entry.Local)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 06:03:00 +03:00
|
|
|
addPathCheckers(c, entry, backend, viewPath)
|
|
|
|
|
|
|
|
if nilMount {
|
|
|
|
backend.Cleanup(ctx)
|
|
|
|
backend = nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
// Set the backend back
|
|
|
|
re.backend = backend
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
if backend != nil {
|
2022-09-22 20:16:21 +03:00
|
|
|
// Initialize the backend after reload. This is a no-op for backends < v5 which
|
|
|
|
// rely on lazy loading for initialization. v5 backends do not rely on lazy loading
|
|
|
|
// for initialization unless the plugin process is killed. Reload of a v5 backend
|
|
|
|
// results in a new plugin process, so we must initialize the backend here.
|
|
|
|
err := backend.Initialize(ctx, &logical.InitializationRequest{Storage: view})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
// Set paths as well
|
|
|
|
paths := backend.SpecialPaths()
|
|
|
|
if paths != nil {
|
|
|
|
re.rootPaths.Store(pathsToRadix(paths.Root))
|
2021-10-13 19:51:20 +03:00
|
|
|
loginPathsEntry, err := parseUnauthenticatedPaths(paths.Unauthenticated)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
re.loginPaths.Store(loginPathsEntry)
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
2018-03-16 20:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-08-08 07:18:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-30 21:33:30 +03:00
|
|
|
|
|
|
|
func (c *Core) setupPluginReload() error {
|
|
|
|
return handleSetupPluginReload(c)
|
|
|
|
}
|