Implement clean up routine to backend as some backends may require

e.g closing database connections on unmount to avoud connection
stacking.
This commit is contained in:
Lassi Pölönen
2015-09-10 17:11:37 +03:00
parent 73416e1a0d
commit 750cf5053c
6 changed files with 38 additions and 1 deletions

View File

@@ -51,6 +51,10 @@ type Backend struct {
Rollback RollbackFunc
RollbackMinAge time.Duration
// Clean is called on unload to clean up e.g any existing connections
// to the backend, if required.
Clean CleanupFunc
// AuthRenew is the callback to call when a RenewRequest for an
// authentication comes in. By default, renewal won't be allowed.
// See the built-in AuthRenew helpers in lease.go for common callbacks.
@@ -68,6 +72,9 @@ type OperationFunc func(*logical.Request, *FieldData) (*logical.Response, error)
// RollbackFunc is the callback for rollbacks.
type RollbackFunc func(*logical.Request, string, interface{}) error
// CleanupFunc is the callback for backend unload.
type CleanupFunc func()
// logical.Backend impl.
func (b *Backend) HandleRequest(req *logical.Request) (*logical.Response, error) {
b.once.Do(b.init)
@@ -147,6 +154,11 @@ func (b *Backend) Setup(config *logical.BackendConfig) (logical.Backend, error)
return b, nil
}
func (b *Backend) Cleanup() {
if b.Clean != nil {
b.Clean()
}
}
// Logger can be used to get the logger. If no logger has been set,
// the logs will be discarded.
func (b *Backend) Logger() *log.Logger {

View File

@@ -26,6 +26,8 @@ type Backend interface {
// System provides an interface to access certain system configuration
// information, such as globally configured default and max lease TTLs.
System() SystemView
Cleanup()
}
// BackendConfig is provided to the factory to initialize the backend

View File

@@ -461,8 +461,17 @@ func (c *Core) setupMounts() error {
}
// unloadMounts is used before we seal the vault to reset the mounts to
// their unloaded state. This is reversed by load and setup mounts.
// their unloaded state, calling Cleanup if defined. This is reversed by load and setup mounts.
func (c *Core) unloadMounts() error {
if c.mounts != nil {
for _, e := range c.mounts.Entries {
prefix := e.Path
b, ok := c.router.root.Get(prefix)
if ok {
b.(*mountEntry).backend.Cleanup()
}
}
}
c.mounts = nil
c.router = NewRouter()
c.systemBarrierView = nil

View File

@@ -75,6 +75,12 @@ func (r *Router) Mount(backend logical.Backend, prefix string, mountEntry *Mount
func (r *Router) Unmount(prefix string) error {
r.l.Lock()
defer r.l.Unlock()
// Call backend's Cleanup routine
me, ok := r.root.Get(prefix)
if ok {
me.(*mountEntry).backend.Cleanup()
}
r.root.Delete(prefix)
return nil
}

View File

@@ -49,6 +49,10 @@ func (n *NoopBackend) System() logical.SystemView {
}
}
func (n *NoopBackend) Cleanup() {
// noop
}
func TestRouter_Mount(t *testing.T) {
r := NewRouter()
_, barrier, _ := mockBarrier(t)

View File

@@ -272,3 +272,7 @@ func (n *rawHTTP) System() logical.SystemView {
MaxLeaseTTLVal: time.Hour * 24 * 30,
}
}
func (n *rawHTTP) Cleanup() {
// noop
}