Port over some work to make the system views a bit nicer

This commit is contained in:
Jeff Mitchell 2017-01-13 14:51:10 -05:00
parent 1d7ded02b4
commit 252e1f1e84
6 changed files with 68 additions and 22 deletions

View File

@ -68,6 +68,10 @@ type Backend struct {
// to the backend, if required.
Clean CleanupFunc
// Initialize is called after a backend is created. Storage should not be
// written to before this function is called.
Init InitializeFunc
// Invalidate is called when a keys is modified if required
Invalidate InvalidateFunc
@ -95,6 +99,9 @@ type WALRollbackFunc func(*logical.Request, string, interface{}) error
// CleanupFunc is the callback for backend unload.
type CleanupFunc func()
// InitializeFunc is the callback for backend creation.
type InitializeFunc func() error
// InvalidateFunc is the callback for backend key invalidation.
type InvalidateFunc func(string)
@ -231,6 +238,14 @@ func (b *Backend) Cleanup() {
}
}
func (b *Backend) Initialize() error {
if b.Init != nil {
return b.Init()
}
return nil
}
// InvalidateKey is used to clear caches and reset internal state on key changes
func (b *Backend) InvalidateKey(key string) {
if b.Invalidate != nil {

View File

@ -39,6 +39,10 @@ type Backend interface {
// handle any cleanup like connection closing or releasing of file handles.
Cleanup()
// Initialize is invoked after a backend is created. It is the place to run
// any operations requiring storage; these should not be in the factory.
Initialize() error
// InvalidateKey may be invoked when an object is modified that belongs
// to the backend. The backend can use this to clear any caches or reset
// internal state as needed.
@ -71,4 +75,27 @@ type Paths struct {
// Unauthenticated are the paths that can be accessed without any auth.
Unauthenticated []string
// LocalStorage are paths (prefixes) that are local to this instance; this
// indicates that these paths should not be replicated
LocalStorage []string
}
type ReplicationState uint32
const (
ReplicationDisabled ReplicationState = iota
ReplicationPrimary
ReplicationSecondary
)
func (r ReplicationState) String() string {
switch r {
case ReplicationSecondary:
return "secondary"
case ReplicationPrimary:
return "primary"
}
return "disabled"
}

View File

@ -31,19 +31,18 @@ type SystemView interface {
// despite known slowdowns.
CachingDisabled() bool
// IsPrimary checks if this is a primary Vault instance. This
// can be used to avoid writes on secondaries and to avoid doing
// lazy upgrades which may cause writes.
IsPrimary() bool
// ReplicationState indicates the state of cluster replication
ReplicationState() ReplicationState
}
type StaticSystemView struct {
DefaultLeaseTTLVal time.Duration
MaxLeaseTTLVal time.Duration
SudoPrivilegeVal bool
TaintedVal bool
CachingDisabledVal bool
Primary bool
DefaultLeaseTTLVal time.Duration
MaxLeaseTTLVal time.Duration
SudoPrivilegeVal bool
TaintedVal bool
CachingDisabledVal bool
Primary bool
ReplicationStateVal ReplicationState
}
func (d StaticSystemView) DefaultLeaseTTL() time.Duration {
@ -66,6 +65,6 @@ func (d StaticSystemView) CachingDisabled() bool {
return d.CachingDisabledVal
}
func (d StaticSystemView) IsPrimary() bool {
return d.Primary
func (d StaticSystemView) ReplicationState() ReplicationState {
return d.ReplicationStateVal
}

View File

@ -299,6 +299,10 @@ type Core struct {
rpcClientConn *grpc.ClientConn
// The grpc forwarding client
rpcForwardingClient RequestForwardingClient
// replicationState keeps the current replication state cached for quick
// lookup
replicationState logical.ReplicationState
}
// CoreConfig is used to parameterize a core

View File

@ -6,14 +6,6 @@ import (
"github.com/hashicorp/vault/logical"
)
var (
sysViewIsPrimaryFunc = func(c *Core) func() bool {
return func() bool {
return true
}
}
)
type dynamicSystemView struct {
core *Core
mountEntry *MountEntry
@ -84,6 +76,10 @@ func (d dynamicSystemView) CachingDisabled() bool {
}
// Checks if this is a primary Vault instance.
func (d dynamicSystemView) IsPrimary() bool {
return sysViewIsPrimaryFunc(d.core)()
func (d dynamicSystemView) ReplicationState() logical.ReplicationState {
var state logical.ReplicationState
d.core.clusterParamsLock.RLock()
state = d.core.replicationState
d.core.clusterParamsLock.RUnlock()
return state
}

View File

@ -387,6 +387,11 @@ func (n *rawHTTP) Cleanup() {
// noop
}
func (n *rawHTTP) Initialize() error {
// noop
return nil
}
func (n *rawHTTP) InvalidateKey(string) {
// noop
}