Skip to content

Commit

Permalink
Update replication state logic.
Browse files Browse the repository at this point in the history
Fixes #3727
  • Loading branch information
jefferai committed Jan 16, 2018
1 parent e28d924 commit f320f00
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion vault/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (c *Core) disableCredential(path string) error {
}

switch {
case entry.Local, !c.replicationState.HasState(consts.ReplicationPerformanceSecondary):
case entry.Local, !c.ReplicationState().HasState(consts.ReplicationPerformanceSecondary):
// Have writable storage, remove the whole thing
if err := logical.ClearView(view); err != nil {
c.logger.Error("core: failed to clear view for path being unmounted", "error", err, "path", path)
Expand Down
7 changes: 3 additions & 4 deletions vault/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ type Core struct {
atomicPrimaryFailoverAddrs *atomic.Value
// replicationState keeps the current replication state cached for quick
// lookup
replicationState consts.ReplicationState
replicationState *uint32

// uiEnabled indicates whether Vault Web UI is enabled or not
uiEnabled bool
Expand Down Expand Up @@ -490,6 +490,7 @@ func NewCore(conf *CoreConfig) (*Core, error) {
clusterPeerClusterAddrsCache: cache.New(3*heartbeatInterval, time.Second),
enableMlock: !conf.DisableMlock,
rawEnabled: conf.EnableRaw,
replicationState: new(uint32),
atomicPrimaryClusterAddrs: new(atomic.Value),
atomicPrimaryFailoverAddrs: new(atomic.Value),
}
Expand Down Expand Up @@ -2104,9 +2105,7 @@ func (c *Core) emitMetrics(stopCh chan struct{}) {
}

func (c *Core) ReplicationState() consts.ReplicationState {
c.stateLock.RLock()
defer c.stateLock.RUnlock()
return c.replicationState
return consts.ReplicationState(atomic.LoadUint32(c.replicationState))
}

func (c *Core) SealAccess() *SealAccess {
Expand Down
2 changes: 1 addition & 1 deletion vault/dynamic_system_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (d dynamicSystemView) CachingDisabled() bool {
// Checks if this is a primary Vault instance. Caller should hold the stateLock
// in read mode.
func (d dynamicSystemView) ReplicationState() consts.ReplicationState {
return d.core.replicationState
return d.core.ReplicationState()
}

// ResponseWrapData wraps the given data in a cubbyhole and returns the
Expand Down
16 changes: 8 additions & 8 deletions vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ func (b *SystemBackend) handleMountTable(ctx context.Context, req *logical.Reque

// handleMount is used to mount a new path
func (b *SystemBackend) handleMount(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()

local := data.Get("local").(bool)
if !local && repState.HasState(consts.ReplicationPerformanceSecondary) {
Expand Down Expand Up @@ -1511,7 +1511,7 @@ func (b *SystemBackend) handleUnmount(ctx context.Context, req *logical.Request,
path := data.Get("path").(string)
path = sanitizeMountPath(path)

repState := b.Core.replicationState
repState := b.Core.ReplicationState()
entry := b.Core.router.MatchingMountEntry(path)
if entry != nil && !entry.Local && repState.HasState(consts.ReplicationPerformanceSecondary) {
return logical.ErrorResponse("cannot unmount a non-local mount on a replication secondary"), nil
Expand All @@ -1535,7 +1535,7 @@ func (b *SystemBackend) handleUnmount(ctx context.Context, req *logical.Request,

// handleRemount is used to remount a path
func (b *SystemBackend) handleRemount(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()

// Get the paths
fromPath := data.Get("from").(string)
Expand Down Expand Up @@ -1641,7 +1641,7 @@ func (b *SystemBackend) handleMountTuneWrite(ctx context.Context, req *logical.R

// handleTuneWriteCommon is used to set config settings on a path
func (b *SystemBackend) handleTuneWriteCommon(path string, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()

path = sanitizeMountPath(path)

Expand Down Expand Up @@ -1904,7 +1904,7 @@ func (b *SystemBackend) handleAuthTable(ctx context.Context, req *logical.Reques

// handleEnableAuth is used to enable a new credential backend
func (b *SystemBackend) handleEnableAuth(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()
local := data.Get("local").(bool)
if !local && repState.HasState(consts.ReplicationPerformanceSecondary) {
return logical.ErrorResponse("cannot add a non-local mount to a replication secondary"), nil
Expand Down Expand Up @@ -1979,7 +1979,7 @@ func (b *SystemBackend) handleDisableAuth(ctx context.Context, req *logical.Requ

fullPath := credentialRoutePrefix + path

repState := b.Core.replicationState
repState := b.Core.ReplicationState()
entry := b.Core.router.MatchingMountEntry(fullPath)
if entry != nil && !entry.Local && repState.HasState(consts.ReplicationPerformanceSecondary) {
return logical.ErrorResponse("cannot unmount a non-local mount on a replication secondary"), nil
Expand Down Expand Up @@ -2225,7 +2225,7 @@ func (b *SystemBackend) handleAuditHash(ctx context.Context, req *logical.Reques

// handleEnableAudit is used to enable a new audit backend
func (b *SystemBackend) handleEnableAudit(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()

local := data.Get("local").(bool)
if !local && repState.HasState(consts.ReplicationPerformanceSecondary) {
Expand Down Expand Up @@ -2387,7 +2387,7 @@ func (b *SystemBackend) handleKeyStatus(ctx context.Context, req *logical.Reques

// handleRotate is used to trigger a key rotation
func (b *SystemBackend) handleRotate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
repState := b.Core.replicationState
repState := b.Core.ReplicationState()
if repState.HasState(consts.ReplicationPerformanceSecondary) {
return logical.ErrorResponse("cannot rotate on a replication secondary"), nil
}
Expand Down
4 changes: 2 additions & 2 deletions vault/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (c *Core) unmountInternal(path string) error {
}

switch {
case entry.Local, !c.replicationState.HasState(consts.ReplicationPerformanceSecondary):
case entry.Local, !c.ReplicationState().HasState(consts.ReplicationPerformanceSecondary):
// Have writable storage, remove the whole thing
if err := logical.ClearView(view); err != nil {
c.logger.Error("core: failed to clear view for path being unmounted", "error", err, "path", path)
Expand Down Expand Up @@ -610,7 +610,7 @@ func (c *Core) loadMounts() error {
// ensure this comes over. If we upgrade first, we simply don't
// create the mount, so we won't conflict when we sync. If this is
// local (e.g. cubbyhole) we do still add it.
if !foundRequired && (!c.replicationState.HasState(consts.ReplicationPerformanceSecondary) || requiredMount.Local) {
if !foundRequired && (!c.ReplicationState().HasState(consts.ReplicationPerformanceSecondary) || requiredMount.Local) {
c.mounts.Entries = append(c.mounts.Entries, requiredMount)
needPersist = true
}
Expand Down
2 changes: 1 addition & 1 deletion vault/policy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (c *Core) setupPolicyStore() error {
sysView := &dynamicSystemView{core: c}
c.policyStore = NewPolicyStore(c.systemBarrierView, sysView)

if c.replicationState.HasState(consts.ReplicationPerformanceSecondary) {
if c.ReplicationState().HasState(consts.ReplicationPerformanceSecondary) {
// Policies will sync from the primary
return nil
}
Expand Down

0 comments on commit f320f00

Please sign in to comment.