2015-03-17 23:16:04 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-08 23:43:17 +00:00
|
|
|
"strings"
|
2015-04-02 05:11:48 +00:00
|
|
|
"sync"
|
2015-03-17 23:16:04 +00:00
|
|
|
"time"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2015-04-08 23:43:17 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2015-03-17 23:16:04 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
const (
|
|
|
|
// rollbackPeriod is how often we attempt rollbacks for all the backends
|
|
|
|
rollbackPeriod = time.Minute
|
|
|
|
)
|
|
|
|
|
2015-03-17 23:16:04 +00:00
|
|
|
// RollbackManager is responsible for performing rollbacks of partial
|
|
|
|
// secrets within logical backends.
|
|
|
|
//
|
|
|
|
// During normal operations, it is possible for logical backends to
|
|
|
|
// error partially through an operation. These are called "partial secrets":
|
|
|
|
// they are never sent back to a user, but they do need to be cleaned up.
|
|
|
|
// This manager handles that by periodically (on a timer) requesting that the
|
|
|
|
// backends clean up.
|
|
|
|
//
|
2015-04-02 05:11:48 +00:00
|
|
|
// The RollbackManager periodically initiates a logical.RollbackOperation
|
|
|
|
// on every mounted logical backend. It ensures that only one rollback operation
|
|
|
|
// is in-flight at any given time within a single seal/unseal phase.
|
2015-03-17 23:16:04 +00:00
|
|
|
type RollbackManager struct {
|
2016-08-19 20:45:17 +00:00
|
|
|
logger log.Logger
|
2015-11-11 16:44:07 +00:00
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
// This gives the current mount table of both logical and credential backends,
|
|
|
|
// plus a RWMutex that is locked for reading. It is up to the caller to RUnlock
|
|
|
|
// it when done with the mount table.
|
|
|
|
backends func() []*MountEntry
|
2015-11-11 16:44:07 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
router *Router
|
|
|
|
period time.Duration
|
|
|
|
|
|
|
|
inflightAll sync.WaitGroup
|
|
|
|
inflight map[string]*rollbackState
|
2016-09-13 15:50:14 +00:00
|
|
|
inflightLock sync.RWMutex
|
2015-04-02 05:11:48 +00:00
|
|
|
|
|
|
|
doneCh chan struct{}
|
|
|
|
shutdown bool
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
shutdownLock sync.Mutex
|
2018-01-08 18:31:38 +00:00
|
|
|
quitContext context.Context
|
2018-07-31 01:55:43 +00:00
|
|
|
|
|
|
|
core *Core
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
2015-03-18 01:46:10 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// rollbackState is used to track the state of a single rollback attempt
|
|
|
|
type rollbackState struct {
|
|
|
|
lastError error
|
|
|
|
sync.WaitGroup
|
|
|
|
}
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// NewRollbackManager is used to create a new rollback manager
|
2018-07-31 01:55:43 +00:00
|
|
|
func NewRollbackManager(ctx context.Context, logger log.Logger, backendsFunc func() []*MountEntry, router *Router, core *Core) *RollbackManager {
|
2015-04-02 05:11:48 +00:00
|
|
|
r := &RollbackManager{
|
2018-01-08 18:31:38 +00:00
|
|
|
logger: logger,
|
|
|
|
backends: backendsFunc,
|
|
|
|
router: router,
|
|
|
|
period: rollbackPeriod,
|
|
|
|
inflight: make(map[string]*rollbackState),
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
quitContext: ctx,
|
2018-07-31 01:55:43 +00:00
|
|
|
core: core,
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
|
|
|
return r
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// Start starts the rollback manager
|
2015-03-17 23:16:04 +00:00
|
|
|
func (m *RollbackManager) Start() {
|
2015-04-02 05:11:48 +00:00
|
|
|
go m.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the running manager. This will wait for any in-flight
|
|
|
|
// rollbacks to complete.
|
|
|
|
func (m *RollbackManager) Stop() {
|
|
|
|
m.shutdownLock.Lock()
|
|
|
|
defer m.shutdownLock.Unlock()
|
|
|
|
if !m.shutdown {
|
|
|
|
m.shutdown = true
|
|
|
|
close(m.shutdownCh)
|
|
|
|
<-m.doneCh
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
m.inflightAll.Wait()
|
|
|
|
}
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// run is a long running routine to periodically invoke rollback
|
|
|
|
func (m *RollbackManager) run() {
|
2018-04-03 00:46:59 +00:00
|
|
|
m.logger.Info("starting rollback manager")
|
2015-04-02 05:11:48 +00:00
|
|
|
tick := time.NewTicker(m.period)
|
2015-03-17 23:16:04 +00:00
|
|
|
defer tick.Stop()
|
2015-04-02 05:11:48 +00:00
|
|
|
defer close(m.doneCh)
|
2015-03-17 23:16:04 +00:00
|
|
|
for {
|
2015-04-02 05:11:48 +00:00
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
m.triggerRollbacks()
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
case <-m.shutdownCh:
|
2018-04-03 00:46:59 +00:00
|
|
|
m.logger.Info("stopping rollback manager")
|
2015-03-17 23:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// triggerRollbacks is used to trigger the rollbacks across all the backends
|
|
|
|
func (m *RollbackManager) triggerRollbacks() {
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
backends := m.backends()
|
2015-11-11 16:44:07 +00:00
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
for _, e := range backends {
|
2016-05-26 18:29:41 +00:00
|
|
|
path := e.Path
|
|
|
|
if e.Table == credentialTableType {
|
2017-07-12 20:12:55 +00:00
|
|
|
path = credentialRoutePrefix + path
|
2016-05-26 18:29:41 +00:00
|
|
|
}
|
2017-07-12 20:12:55 +00:00
|
|
|
|
|
|
|
// When the mount is filtered, the backend will be nil
|
|
|
|
backend := m.router.MatchingBackend(path)
|
|
|
|
if backend == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:50:14 +00:00
|
|
|
m.inflightLock.RLock()
|
|
|
|
_, ok := m.inflight[path]
|
|
|
|
m.inflightLock.RUnlock()
|
|
|
|
if !ok {
|
2018-08-08 00:48:43 +00:00
|
|
|
m.startRollback(path, true)
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// startRollback is used to start an async rollback attempt.
|
|
|
|
// This must be called with the inflightLock held.
|
2018-08-08 00:48:43 +00:00
|
|
|
func (m *RollbackManager) startRollback(path string, grabStatelock bool) *rollbackState {
|
2015-04-02 05:11:48 +00:00
|
|
|
rs := &rollbackState{}
|
|
|
|
rs.Add(1)
|
|
|
|
m.inflightAll.Add(1)
|
2016-09-13 15:50:14 +00:00
|
|
|
m.inflightLock.Lock()
|
2015-04-02 05:11:48 +00:00
|
|
|
m.inflight[path] = rs
|
2016-09-13 15:50:14 +00:00
|
|
|
m.inflightLock.Unlock()
|
2018-08-08 00:48:43 +00:00
|
|
|
go m.attemptRollback(m.quitContext, path, rs, grabStatelock)
|
2015-04-02 05:11:48 +00:00
|
|
|
return rs
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// attemptRollback invokes a RollbackOperation for the given path
|
2018-08-08 00:48:43 +00:00
|
|
|
func (m *RollbackManager) attemptRollback(ctx context.Context, path string, rs *rollbackState, grabStatelock bool) (err error) {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"rollback", "attempt", strings.Replace(path, "/", "-", -1)}, time.Now())
|
2018-04-03 00:46:59 +00:00
|
|
|
if m.logger.IsDebug() {
|
|
|
|
m.logger.Debug("attempting rollback", "path", path)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2015-11-11 16:44:07 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
defer func() {
|
|
|
|
rs.lastError = err
|
|
|
|
rs.Done()
|
|
|
|
m.inflightAll.Done()
|
|
|
|
m.inflightLock.Lock()
|
|
|
|
delete(m.inflight, path)
|
|
|
|
m.inflightLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Invoke a RollbackOperation
|
2015-03-17 23:16:04 +00:00
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.RollbackOperation,
|
|
|
|
Path: path,
|
|
|
|
}
|
2018-07-24 21:50:49 +00:00
|
|
|
var cancelFunc context.CancelFunc
|
|
|
|
ctx, cancelFunc = context.WithTimeout(ctx, DefaultMaxRequestDuration)
|
2018-08-08 00:48:43 +00:00
|
|
|
if grabStatelock {
|
|
|
|
m.core.stateLock.RLock()
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
_, err = m.router.Route(ctx, req)
|
2018-08-08 00:48:43 +00:00
|
|
|
if grabStatelock {
|
|
|
|
m.core.stateLock.RUnlock()
|
|
|
|
}
|
2018-07-24 21:50:49 +00:00
|
|
|
cancelFunc()
|
2015-03-17 23:24:42 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// If the error is an unsupported operation, then it doesn't
|
|
|
|
// matter, the backend doesn't support it.
|
|
|
|
if err == logical.ErrUnsupportedOperation {
|
|
|
|
err = nil
|
|
|
|
}
|
2018-02-03 01:28:25 +00:00
|
|
|
// If we failed due to read-only storage, we can't do anything; ignore
|
|
|
|
if err != nil && strings.Contains(err.Error(), logical.ErrReadOnly.Error()) {
|
|
|
|
err = nil
|
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
m.logger.Error("error rolling back", "path", path, "error", err)
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
return
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
2015-03-17 23:23:58 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// Rollback is used to trigger an immediate rollback of the path,
|
2018-08-08 00:48:43 +00:00
|
|
|
// or to join an existing rollback operation if in flight. Caller should have
|
|
|
|
// core's statelock held
|
2015-04-02 05:11:48 +00:00
|
|
|
func (m *RollbackManager) Rollback(path string) error {
|
|
|
|
// Check for an existing attempt and start one if none
|
2016-09-13 15:50:14 +00:00
|
|
|
m.inflightLock.RLock()
|
2015-04-02 05:11:48 +00:00
|
|
|
rs, ok := m.inflight[path]
|
2016-09-13 15:50:14 +00:00
|
|
|
m.inflightLock.RUnlock()
|
2015-04-02 05:11:48 +00:00
|
|
|
if !ok {
|
2018-08-08 00:48:43 +00:00
|
|
|
rs = m.startRollback(path, false)
|
2015-03-17 23:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// Wait for the attempt to finish
|
|
|
|
rs.Wait()
|
|
|
|
|
|
|
|
// Return the last error
|
|
|
|
return rs.lastError
|
|
|
|
}
|
2015-03-17 23:23:58 +00:00
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// The methods below are the hooks from core that are called pre/post seal.
|
|
|
|
|
|
|
|
// startRollback is used to start the rollback manager after unsealing
|
|
|
|
func (c *Core) startRollback() error {
|
2016-04-19 01:06:26 +00:00
|
|
|
backendsFunc := func() []*MountEntry {
|
2015-11-15 22:32:57 +00:00
|
|
|
ret := []*MountEntry{}
|
|
|
|
c.mountsLock.RLock()
|
|
|
|
defer c.mountsLock.RUnlock()
|
2016-07-19 02:02:39 +00:00
|
|
|
// During teardown/setup after a leader change or unseal there could be
|
|
|
|
// something racy here so make sure the table isn't nil
|
|
|
|
if c.mounts != nil {
|
|
|
|
for _, entry := range c.mounts.Entries {
|
|
|
|
ret = append(ret, entry)
|
|
|
|
}
|
2015-11-15 22:32:57 +00:00
|
|
|
}
|
2016-05-26 18:29:41 +00:00
|
|
|
c.authLock.RLock()
|
|
|
|
defer c.authLock.RUnlock()
|
2016-07-19 02:02:39 +00:00
|
|
|
// During teardown/setup after a leader change or unseal there could be
|
|
|
|
// something racy here so make sure the table isn't nil
|
|
|
|
if c.auth != nil {
|
|
|
|
for _, entry := range c.auth.Entries {
|
|
|
|
ret = append(ret, entry)
|
|
|
|
}
|
2016-05-26 18:29:41 +00:00
|
|
|
}
|
2015-11-15 22:32:57 +00:00
|
|
|
return ret
|
2015-11-11 16:44:07 +00:00
|
|
|
}
|
2018-07-31 01:55:43 +00:00
|
|
|
c.rollback = NewRollbackManager(c.activeContext, c.logger.ResetNamed("rollback"), backendsFunc, c.router, c)
|
2015-04-02 05:11:48 +00:00
|
|
|
c.rollback.Start()
|
2015-03-17 23:23:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
// stopRollback is used to stop running the rollback manager before sealing
|
2015-03-17 23:23:58 +00:00
|
|
|
func (c *Core) stopRollback() error {
|
|
|
|
if c.rollback != nil {
|
|
|
|
c.rollback.Stop()
|
|
|
|
c.rollback = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|