diff --git a/command/server.go b/command/server.go index 8760e55ddd..5927b1b6db 100644 --- a/command/server.go +++ b/command/server.go @@ -114,47 +114,9 @@ func (c *ServerCommand) Run(args []string) int { return 1 } - var advertiseAddr string = config.Backend.AdvertiseAddr - - // Note that "habackend" is a backend that *may* support HA; - // it defaults to the same backend as normal operations - var habackend physical.Backend = backend - - // Initialize the separate HA physical backend, if it exists - if config.HABackend != nil { - habackend, err = physical.NewBackend( - config.HABackend.Type, config.HABackend.Config) - if err != nil { - c.Ui.Error(fmt.Sprintf( - "Error initializing backend of type %s: %s", - config.HABackend.Type, err)) - return 1 - } - if _, ok := habackend.(physical.HABackend); !ok { - c.Ui.Error("Specified HA backend does not support HA") - return 1 - } - - advertiseAddr = config.HABackend.AdvertiseAddr - } - - // Attempt to detect the advertise address possible - if detect, ok := habackend.(physical.AdvertiseDetect); ok && advertiseAddr == "" { - advertise, err := c.detectAdvertise(detect, config) - if err != nil { - c.Ui.Error(fmt.Sprintf("Error detecting advertise address: %s", err)) - } else if advertise == "" { - c.Ui.Error("Failed to detect advertise address.") - } else { - advertiseAddr = advertise - } - } - - // Initialize the core - core, err := vault.NewCore(&vault.CoreConfig{ - AdvertiseAddr: advertiseAddr, + coreConfig := &vault.CoreConfig{ Physical: backend, - HAPhysical: habackend, + HAPhysical: nil, AuditBackends: c.AuditBackends, CredentialBackends: c.CredentialBackends, LogicalBackends: c.LogicalBackends, @@ -163,7 +125,41 @@ func (c *ServerCommand) Run(args []string) int { DisableMlock: config.DisableMlock, MaxLeaseTTL: config.MaxLeaseTTL, DefaultLeaseTTL: config.DefaultLeaseTTL, - }) + } + + // Initialize the separate HA physical backend, if it exists + if config.HABackend != nil { + habackend, err := physical.NewBackend( + config.HABackend.Type, config.HABackend.Config) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error initializing backend of type %s: %s", + config.HABackend.Type, err)) + return 1 + } + + var ok bool + if coreConfig.HAPhysical, ok = habackend.(physical.HABackend); !ok { + c.Ui.Error("Specified HA backend does not support HA") + return 1 + } + coreConfig.AdvertiseAddr = config.HABackend.AdvertiseAddr + } + + // Attempt to detect the advertise address possible + if detect, ok := coreConfig.HAPhysical.(physical.AdvertiseDetect); ok && coreConfig.AdvertiseAddr == "" { + advertise, err := c.detectAdvertise(detect, config) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error detecting advertise address: %s", err)) + } else if advertise == "" { + c.Ui.Error("Failed to detect advertise address.") + } else { + coreConfig.AdvertiseAddr = advertise + } + } + + // Initialize the core + core, err := vault.NewCore(coreConfig) if err != nil { c.Ui.Error(fmt.Sprintf("Error initializing core: %s", err)) return 1 @@ -214,13 +210,13 @@ func (c *ServerCommand) Run(args []string) int { if config.HABackend != nil { info["HA backend"] = config.HABackend.Type - info["advertise address"] = advertiseAddr + info["advertise address"] = coreConfig.AdvertiseAddr infoKeys = append(infoKeys, "HA backend", "advertise address") } else { // If the backend supports HA, then note it - if _, ok := habackend.(physical.HABackend); ok { + if coreConfig.HAPhysical != nil { info["backend"] += " (HA available)" - info["advertise address"] = advertiseAddr + info["advertise address"] = coreConfig.AdvertiseAddr infoKeys = append(infoKeys, "advertise address") } } diff --git a/http/logical_test.go b/http/logical_test.go index 369a768747..8ae7f2a65c 100644 --- a/http/logical_test.go +++ b/http/logical_test.go @@ -70,10 +70,8 @@ func TestLogical_StandbyRedirect(t *testing.T) { // Create an HA Vault inm := physical.NewInmem() - inmha := physical.NewInmemHA() conf := &vault.CoreConfig{ Physical: inm, - HAPhysical: inmha, AdvertiseAddr: addr1, DisableMlock: true, } @@ -89,7 +87,6 @@ func TestLogical_StandbyRedirect(t *testing.T) { // Create a second HA Vault conf2 := &vault.CoreConfig{ Physical: inm, - HAPhysical: inmha, AdvertiseAddr: addr2, DisableMlock: true, } diff --git a/vault/core.go b/vault/core.go index f87fcd9cf3..b536bec0e6 100644 --- a/vault/core.go +++ b/vault/core.go @@ -277,7 +277,7 @@ type CoreConfig struct { // Defaults to the same backend as Physical. This is not a backend that // necessarily supports HA; it is merely the one that will be attempted // for HA operations - HAPhysical physical.Backend + HAPhysical physical.HABackend Logger *log.Logger DisableCache bool // Disables the LRU cache on the physical backend @@ -290,12 +290,7 @@ type CoreConfig struct { // NewCore is used to construct a new core func NewCore(conf *CoreConfig) (*Core, error) { - // Check if this backend supports an HA configuraiton - var haBackend physical.HABackend - if ha, ok := conf.HAPhysical.(physical.HABackend); ok { - haBackend = ha - } - if haBackend != nil && conf.AdvertiseAddr == "" { + if conf.HAPhysical != nil && conf.AdvertiseAddr == "" { return nil, fmt.Errorf("missing advertisement address") } @@ -305,7 +300,6 @@ func NewCore(conf *CoreConfig) (*Core, error) { if conf.MaxLeaseTTL == 0 { conf.MaxLeaseTTL = maxLeaseTTL } - if conf.DefaultLeaseTTL > conf.MaxLeaseTTL { return nil, fmt.Errorf("cannot have DefaultLeaseTTL larger than MaxLeaseTTL") } @@ -361,7 +355,7 @@ func NewCore(conf *CoreConfig) (*Core, error) { // Setup the core c := &Core{ - ha: haBackend, + ha: conf.HAPhysical, advertiseAddr: conf.AdvertiseAddr, physical: conf.Physical, barrier: barrier, diff --git a/vault/core_test.go b/vault/core_test.go index 6155986439..6a99b5d56c 100644 --- a/vault/core_test.go +++ b/vault/core_test.go @@ -1258,9 +1258,16 @@ func TestCore_CleanLeaderPrefix(t *testing.T) { } func TestCore_Standby(t *testing.T) { - // Create the first core and initialize it - inm := physical.NewInmem() inmha := physical.NewInmemHA() + testCore_Standby_Common(t, inmha, inmha) +} + +func TestCore_Standby_SeparateHA(t *testing.T) { + testCore_Standby_Common(t, physical.NewInmemHA(), physical.NewInmemHA()) +} + +func testCore_Standby_Common(t *testing.T, inm physical.Backend, inmha physical.HABackend) { + // Create the first core and initialize it advertiseOriginal := "http://127.0.0.1:8200" core, err := NewCore(&CoreConfig{ Physical: inm,