2016-04-04 14:44:22 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-09-13 22:42:24 +00:00
|
|
|
"encoding/base64"
|
2016-04-04 14:44:22 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-04-04 14:44:22 +00:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
|
|
|
"github.com/hashicorp/vault/shamir"
|
|
|
|
)
|
|
|
|
|
2016-09-13 22:42:24 +00:00
|
|
|
// InitParams keeps the init function from being littered with too many
|
|
|
|
// params, that's it!
|
|
|
|
type InitParams struct {
|
|
|
|
BarrierConfig *SealConfig
|
|
|
|
RecoveryConfig *SealConfig
|
|
|
|
RootTokenPGPKey string
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
// InitResult is used to provide the key parts back after
|
|
|
|
// they are generated as part of the initialization.
|
|
|
|
type InitResult struct {
|
|
|
|
SecretShares [][]byte
|
|
|
|
RecoveryShares [][]byte
|
|
|
|
RootToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialized checks if the Vault is already initialized
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) Initialized(ctx context.Context) (bool, error) {
|
2016-04-04 14:44:22 +00:00
|
|
|
// Check the barrier first
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err := c.barrier.Initialized(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("barrier init check failed", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !init {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("security barrier not initialized")
|
2016-04-04 14:44:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the seal configuration
|
2018-01-19 06:44:44 +00:00
|
|
|
sealConf, err := c.seal.BarrierConfig(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if sealConf == nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
return false, fmt.Errorf("core: barrier reports initialized but no seal configuration found")
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2016-04-14 01:12:58 +00:00
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Core) generateShares(sc *SealConfig) ([]byte, [][]byte, error) {
|
|
|
|
// Generate a master key
|
|
|
|
masterKey, err := c.barrier.GenerateKey()
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, nil, errwrap.Wrapf("key generation failed: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the master key if only a single key part is used
|
|
|
|
var unsealKeys [][]byte
|
|
|
|
if sc.SecretShares == 1 {
|
|
|
|
unsealKeys = append(unsealKeys, masterKey)
|
|
|
|
} else {
|
|
|
|
// Split the master key using the Shamir algorithm
|
|
|
|
shares, err := shamir.Split(masterKey, sc.SecretShares, sc.SecretThreshold)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, nil, errwrap.Wrapf("failed to generate barrier shares: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
unsealKeys = shares
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have PGP keys, perform the encryption
|
|
|
|
if len(sc.PGPKeys) > 0 {
|
|
|
|
hexEncodedShares := make([][]byte, len(unsealKeys))
|
|
|
|
for i, _ := range unsealKeys {
|
|
|
|
hexEncodedShares[i] = []byte(hex.EncodeToString(unsealKeys[i]))
|
|
|
|
}
|
|
|
|
_, encryptedShares, err := pgpkeys.EncryptShares(hexEncodedShares, sc.PGPKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
unsealKeys = encryptedShares
|
|
|
|
}
|
|
|
|
|
|
|
|
return masterKey, unsealKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize is used to initialize the Vault with the given
|
|
|
|
// configurations.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) Initialize(ctx context.Context, initParams *InitParams) (*InitResult, error) {
|
2016-09-13 22:42:24 +00:00
|
|
|
barrierConfig := initParams.BarrierConfig
|
|
|
|
recoveryConfig := initParams.RecoveryConfig
|
|
|
|
|
2018-01-19 08:44:06 +00:00
|
|
|
if c.seal.RecoveryKeySupported() {
|
2016-04-04 14:44:22 +00:00
|
|
|
if recoveryConfig == nil {
|
|
|
|
return nil, fmt.Errorf("recovery configuration must be supplied")
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
if recoveryConfig.SecretShares < 1 {
|
|
|
|
return nil, fmt.Errorf("recovery configuration must specify a positive number of shares")
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// Check if the seal configuration is valid
|
2016-04-25 19:39:04 +00:00
|
|
|
if err := recoveryConfig.Validate(); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("invalid recovery configuration", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("invalid recovery configuration: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// Check if the seal configuration is valid
|
2016-04-04 14:44:22 +00:00
|
|
|
if err := barrierConfig.Validate(); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("invalid seal configuration", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("invalid seal configuration: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid an initialization race
|
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
|
|
|
|
|
|
|
// Check if we are initialized
|
2018-01-19 06:44:44 +00:00
|
|
|
init, err := c.Initialized(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
return nil, ErrAlreadyInit
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = c.seal.Init(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to initialize seal", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error initializing seal: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
barrierKey, barrierUnsealKeys, err := c.generateShares(barrierConfig)
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("error generating shares", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the barrier
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.barrier.Initialize(ctx, barrierKey); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to initialize barrier", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to initialize barrier: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsInfo() {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("security barrier initialized", "shares", barrierConfig.SecretShares, "threshold", barrierConfig.SecretThreshold)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
|
|
|
|
// Unseal the barrier
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.barrier.Unseal(ctx, barrierKey); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to unseal barrier", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to unseal barrier: {{err}}", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the barrier is re-sealed
|
|
|
|
defer func() {
|
2017-02-17 04:09:39 +00:00
|
|
|
// Defers are LIFO so we need to run this here too to ensure the stop
|
|
|
|
// happens before sealing. preSeal also stops, so we just make the
|
|
|
|
// stopping safe against multiple calls.
|
2016-04-04 14:44:22 +00:00
|
|
|
if err := c.barrier.Seal(); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to seal barrier", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = c.seal.SetBarrierConfig(ctx, barrierConfig)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to save barrier configuration", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("barrier configuration saving failed: {{err}}", err)
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are storing shares, pop them out of the returned results and push
|
|
|
|
// them through the seal
|
|
|
|
if barrierConfig.StoredShares > 0 {
|
|
|
|
var keysToStore [][]byte
|
|
|
|
for i := 0; i < barrierConfig.StoredShares; i++ {
|
|
|
|
keysToStore = append(keysToStore, barrierUnsealKeys[0])
|
|
|
|
barrierUnsealKeys = barrierUnsealKeys[1:]
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.seal.SetStoredKeys(ctx, keysToStore); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to store keys", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("failed to store keys: {{err}}", err)
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results := &InitResult{
|
|
|
|
SecretShares: barrierUnsealKeys,
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
// Perform initial setup
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := c.setupCluster(ctx); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("cluster setup failed during init", "error", err)
|
2016-08-15 13:42:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
if err := c.postUnseal(); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("post-unseal setup failed during init", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 17:04:29 +00:00
|
|
|
// Save the configuration regardless, but only generate a key if it's not
|
|
|
|
// disabled. When using recovery keys they are stored in the barrier, so
|
|
|
|
// this must happen post-unseal.
|
2018-01-19 08:44:06 +00:00
|
|
|
if c.seal.RecoveryKeySupported() {
|
2018-01-19 06:44:44 +00:00
|
|
|
err = c.seal.SetRecoveryConfig(ctx, recoveryConfig)
|
2016-04-15 17:04:29 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to save recovery configuration", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("recovery configuration saving failed: {{err}}", err)
|
2016-04-15 17:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if recoveryConfig.SecretShares > 0 {
|
|
|
|
recoveryKey, recoveryUnsealKeys, err := c.generateShares(recoveryConfig)
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("failed to generate recovery shares", "error", err)
|
2016-04-15 17:04:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = c.seal.SetRecoveryKey(ctx, recoveryKey)
|
2016-04-15 17:04:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
results.RecoveryShares = recoveryUnsealKeys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
// Generate a new root token
|
2018-01-19 06:44:44 +00:00
|
|
|
rootToken, err := c.tokenStore.rootToken(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("root token generation failed", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results.RootToken = rootToken.ID
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("root token generated")
|
2016-04-04 14:44:22 +00:00
|
|
|
|
2016-09-13 22:42:24 +00:00
|
|
|
if initParams.RootTokenPGPKey != "" {
|
|
|
|
_, encryptedVals, err := pgpkeys.EncryptShares([][]byte{[]byte(results.RootToken)}, []string{initParams.RootTokenPGPKey})
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("root token encryption failed", "error", err)
|
2016-09-13 22:42:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results.RootToken = base64.StdEncoding.EncodeToString(encryptedVals[0])
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
// Prepare to re-seal
|
|
|
|
if err := c.preSeal(); err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("pre-seal teardown failed", "error", err)
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2017-10-23 20:03:36 +00:00
|
|
|
// UnsealWithStoredKeys performs auto-unseal using stored keys.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (c *Core) UnsealWithStoredKeys(ctx context.Context) error {
|
2018-01-19 08:44:06 +00:00
|
|
|
if !c.seal.StoredKeysSupported() {
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 20:57:25 +00:00
|
|
|
sealed := c.Sealed()
|
2016-04-14 01:12:58 +00:00
|
|
|
if !sealed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("stored unseal keys supported, attempting fetch")
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := c.seal.GetStoredKeys(ctx)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("fetching stored unseal keys failed", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return &NonFatalError{Err: errwrap.Wrapf("fetching stored unseal keys failed: {{err}}", err)}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
if len(keys) == 0 {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Warn("stored unseal key(s) supported but none found")
|
2016-04-04 14:44:22 +00:00
|
|
|
} else {
|
|
|
|
unsealed := false
|
|
|
|
keysUsed := 0
|
|
|
|
for _, key := range keys {
|
|
|
|
unsealed, err = c.Unseal(key)
|
|
|
|
if err != nil {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Error("unseal with stored unseal key failed", "error", err)
|
2018-04-05 15:49:21 +00:00
|
|
|
return &NonFatalError{Err: errwrap.Wrapf("unseal with stored key failed: {{err}}", err)}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
keysUsed += 1
|
|
|
|
if unsealed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !unsealed {
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsWarn() {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Warn("stored unseal key(s) used but Vault not unsealed yet", "stored_keys_used", keysUsed)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
} else {
|
2016-08-19 20:45:17 +00:00
|
|
|
if c.logger.IsInfo() {
|
2018-04-03 00:46:59 +00:00
|
|
|
c.logger.Info("successfully unsealed with stored key(s)", "stored_keys_used", keysUsed)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 20:49:46 +00:00
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
return nil
|
|
|
|
}
|