Skip to content

Commit

Permalink
Continuously attempt to unseal if sealed keys are supported
Browse files Browse the repository at this point in the history
This fixes a bug that occurs on bootstrapping an initial cluster. Given a collection of Vault nodes and an initialized storage backend, they will all go into standby waiting for initialization. After one node is initialized, the other nodes had no mechanism by which they "re-check" to see if unseal keys are present. This adds a goroutine to the server command which continually waits for unseal keys to exist. It exits in the following conditions:

- the node is unsealed
- the node does not support stored keys
- a fatal error occurs (as defined by Vault)
- the server is shutting down

In all other situations, the routine wakes up at the specified interval and attempts to unseal with the stored keys.
  • Loading branch information
sethvargo committed Jan 23, 2019
1 parent 5ad512c commit 9e646ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,31 @@ CLUSTER_SYNTHESIS_COMPLETE:
return 1
}

if err := core.UnsealWithStoredKeys(context.Background()); err != nil {
if vault.IsFatalError(err) {
c.UI.Error(fmt.Sprintf("Error initializing core: %s", err))
return 1
// Attempt unsealing in a background goroutine. This is needed for when a
// Vault cluster with multiple servers is configured with auto-unseal but is
// uninitialized. Once one server initializes the storage backend, this
// goroutine will pick up the unseal keys and unseal this instance.
go func() {
for {
err := core.UnsealWithStoredKeys(context.Background())
if err == nil {
return
}

if vault.IsFatalError(err) {
c.logger.Error(fmt.Sprintf("Error unsealing core", "error", err))
return
} else {
c.logger.Warn(fmt.Sprintf("Failed to unseal core", "error" err))
}

select {
case <-c.ShutdownCh:
return
case <-time.After(5 * time.Second):
}
}
}
}()

// Perform service discovery registrations and initialization of
// HTTP server after the verifyOnly check.
Expand Down
2 changes: 1 addition & 1 deletion vault/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (c *Core) UnsealWithStoredKeys(ctx context.Context) error {
defer c.unsealWithStoredKeysLock.Unlock()

if !c.seal.StoredKeysSupported() {
return errors.New("stored keys are not supported")
return nil
}

// Disallow auto-unsealing when migrating
Expand Down

0 comments on commit 9e646ee

Please sign in to comment.