2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 09:44:44 +03:00
|
|
|
"context"
|
2016-09-14 01:42:24 +03:00
|
|
|
"encoding/base64"
|
2016-04-04 17:44:22 +03:00
|
|
|
"encoding/hex"
|
2019-01-24 00:34:34 +03:00
|
|
|
"errors"
|
2016-04-04 17:44:22 +03:00
|
|
|
"fmt"
|
2019-06-20 22:14:58 +03:00
|
|
|
"net/url"
|
2019-02-19 23:03:02 +03:00
|
|
|
"sync/atomic"
|
2016-04-04 17:44:22 +03:00
|
|
|
|
2022-08-23 22:37:16 +03:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2019-06-20 22:14:58 +03:00
|
|
|
"github.com/hashicorp/vault/physical/raft"
|
2020-01-11 04:39:52 +03:00
|
|
|
"github.com/hashicorp/vault/vault/seal"
|
2019-06-20 22:14:58 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2016-04-04 17:44:22 +03:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
|
|
|
"github.com/hashicorp/vault/shamir"
|
|
|
|
)
|
|
|
|
|
2016-09-14 01:42:24 +03: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
|
2019-10-18 21:46:00 +03:00
|
|
|
// LegacyShamirSeal should only be used in test code, we don't want to
|
|
|
|
// give the user a way to create legacy shamir seals.
|
|
|
|
LegacyShamirSeal bool
|
2016-09-14 01:42:24 +03:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03: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
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
var (
|
2021-05-19 23:07:58 +03:00
|
|
|
initPTFunc = func(c *Core) func() { return nil }
|
|
|
|
initInProgress uint32
|
|
|
|
ErrInitWithoutAutoloading = errors.New("cannot initialize storage without an autoloaded license")
|
2018-09-18 06:03:00 +03:00
|
|
|
)
|
|
|
|
|
2019-10-15 07:55:31 +03:00
|
|
|
func (c *Core) InitializeRecovery(ctx context.Context) error {
|
|
|
|
if !c.recoveryMode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raftStorage, ok := c.underlyingPhysical.(*raft.RaftBackend)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedClusterAddr, err := url.Parse(c.ClusterAddr())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.postRecoveryUnsealFuncs = append(c.postRecoveryUnsealFuncs, func() error {
|
|
|
|
return raftStorage.StartRecoveryCluster(context.Background(), raft.Peer{
|
|
|
|
ID: raftStorage.NodeID(),
|
|
|
|
Address: parsedClusterAddr.Host,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:55:34 +03:00
|
|
|
// Initialized checks if the Vault is already initialized. This means one of
|
|
|
|
// two things: either the barrier has been created (with keyring and master key)
|
|
|
|
// and the seal config written to storage, or Raft is forming a cluster and a
|
|
|
|
// join/bootstrap is in progress.
|
2018-01-19 09:44:44 +03:00
|
|
|
func (c *Core) Initialized(ctx context.Context) (bool, error) {
|
2020-12-08 21:55:34 +03:00
|
|
|
// Check the barrier first
|
|
|
|
init, err := c.InitializedLocally(ctx)
|
|
|
|
if err != nil || init {
|
|
|
|
return init, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.isRaftUnseal() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rb := c.getRaftBackend()
|
|
|
|
if rb != nil && rb.Initialized() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitializedLocally checks if the Vault is already initialized from the
|
|
|
|
// local node's perspective. This is the same thing as Initialized, unless
|
|
|
|
// using Raft, in which case Initialized may return true (because a peer
|
|
|
|
// we're joining to has been initialized) while InitializedLocally returns
|
|
|
|
// false (because we're not done bootstrapping raft on the local node).
|
|
|
|
func (c *Core) InitializedLocally(ctx context.Context) (bool, error) {
|
2016-04-04 17:44:22 +03:00
|
|
|
// Check the barrier first
|
2018-01-19 09:44:44 +03:00
|
|
|
init, err := c.barrier.Initialized(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("barrier init check failed", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !init {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Info("security barrier not initialized")
|
2016-04-04 17:44:22 +03:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the seal configuration
|
2018-01-19 09:44:44 +03:00
|
|
|
sealConf, err := c.seal.BarrierConfig(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if sealConf == nil {
|
2016-08-19 23:45:17 +03:00
|
|
|
return false, fmt.Errorf("core: barrier reports initialized but no seal configuration found")
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
2016-04-14 04:12:58 +03:00
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Core) generateShares(sc *SealConfig) ([]byte, [][]byte, error) {
|
2021-12-07 04:12:20 +03:00
|
|
|
// Generate a root key
|
|
|
|
rootKey, err := c.barrier.GenerateKey(c.secureRandomReader)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, nil, fmt.Errorf("key generation failed: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
2021-12-07 04:12:20 +03:00
|
|
|
// Return the root key if only a single key part is used
|
2016-04-04 17:44:22 +03:00
|
|
|
var unsealKeys [][]byte
|
|
|
|
if sc.SecretShares == 1 {
|
2021-12-07 04:12:20 +03:00
|
|
|
unsealKeys = append(unsealKeys, rootKey)
|
2016-04-04 17:44:22 +03:00
|
|
|
} else {
|
2021-12-07 04:12:20 +03:00
|
|
|
// Split the root key using the Shamir algorithm
|
|
|
|
shares, err := shamir.Split(rootKey, sc.SecretShares, sc.SecretThreshold)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, nil, fmt.Errorf("failed to generate barrier shares: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
unsealKeys = shares
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have PGP keys, perform the encryption
|
|
|
|
if len(sc.PGPKeys) > 0 {
|
|
|
|
hexEncodedShares := make([][]byte, len(unsealKeys))
|
2021-04-08 19:43:39 +03:00
|
|
|
for i := range unsealKeys {
|
2016-04-04 17:44:22 +03:00
|
|
|
hexEncodedShares[i] = []byte(hex.EncodeToString(unsealKeys[i]))
|
|
|
|
}
|
|
|
|
_, encryptedShares, err := pgpkeys.EncryptShares(hexEncodedShares, sc.PGPKeys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
unsealKeys = encryptedShares
|
|
|
|
}
|
|
|
|
|
2021-12-07 04:12:20 +03:00
|
|
|
return rootKey, unsealKeys, nil
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize is used to initialize the Vault with the given
|
|
|
|
// configurations.
|
2018-01-19 09:44:44 +03:00
|
|
|
func (c *Core) Initialize(ctx context.Context, initParams *InitParams) (*InitResult, error) {
|
2021-05-19 23:07:58 +03:00
|
|
|
if err := LicenseInitCheck(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:03:02 +03:00
|
|
|
atomic.StoreUint32(&initInProgress, 1)
|
|
|
|
defer atomic.StoreUint32(&initInProgress, 0)
|
2016-09-14 01:42:24 +03:00
|
|
|
barrierConfig := initParams.BarrierConfig
|
|
|
|
recoveryConfig := initParams.RecoveryConfig
|
|
|
|
|
2019-10-18 21:46:00 +03:00
|
|
|
// N.B. Although the core is capable of handling situations where some keys
|
|
|
|
// are stored and some aren't, in practice, replication + HSMs makes this
|
|
|
|
// extremely hard to reason about, to the point that it will probably never
|
2021-12-07 04:12:20 +03:00
|
|
|
// be supported. The reason is that each HSM needs to encode the root key
|
2019-10-18 21:46:00 +03:00
|
|
|
// separately, which means the shares must be generated independently,
|
|
|
|
// which means both that the shares will be different *AND* there would
|
|
|
|
// need to be a way to actually allow fetching of the generated keys by
|
|
|
|
// operators.
|
2020-01-11 04:39:52 +03:00
|
|
|
if c.SealAccess().StoredKeysSupported() == seal.StoredKeysSupportedGeneric {
|
2019-10-18 21:46:00 +03:00
|
|
|
if len(barrierConfig.PGPKeys) > 0 {
|
|
|
|
return nil, fmt.Errorf("PGP keys not supported when storing shares")
|
|
|
|
}
|
|
|
|
barrierConfig.SecretShares = 1
|
|
|
|
barrierConfig.SecretThreshold = 1
|
|
|
|
if barrierConfig.StoredShares != 1 {
|
|
|
|
c.Logger().Warn("stored keys supported on init, forcing shares/threshold to 1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if initParams.LegacyShamirSeal {
|
|
|
|
barrierConfig.StoredShares = 0
|
|
|
|
} else {
|
|
|
|
barrierConfig.StoredShares = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(barrierConfig.PGPKeys) > 0 && len(barrierConfig.PGPKeys) != barrierConfig.SecretShares {
|
|
|
|
return nil, fmt.Errorf("incorrect number of PGP keys")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SealAccess().RecoveryKeySupported() {
|
|
|
|
if len(recoveryConfig.PGPKeys) > 0 && len(recoveryConfig.PGPKeys) != recoveryConfig.SecretShares {
|
|
|
|
return nil, fmt.Errorf("incorrect number of PGP keys for recovery")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 11:44:06 +03:00
|
|
|
if c.seal.RecoveryKeySupported() {
|
2016-04-04 17:44:22 +03:00
|
|
|
if recoveryConfig == nil {
|
|
|
|
return nil, fmt.Errorf("recovery configuration must be supplied")
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:39:04 +03:00
|
|
|
if recoveryConfig.SecretShares < 1 {
|
|
|
|
return nil, fmt.Errorf("recovery configuration must specify a positive number of shares")
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-15 19:58:36 +03:00
|
|
|
// Check if the seal configuration is valid
|
2016-04-25 22:39:04 +03:00
|
|
|
if err := recoveryConfig.Validate(); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("invalid recovery configuration", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("invalid recovery configuration: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 19:58:36 +03:00
|
|
|
// Check if the seal configuration is valid
|
2016-04-04 17:44:22 +03:00
|
|
|
if err := barrierConfig.Validate(); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("invalid seal configuration", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("invalid seal configuration: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid an initialization race
|
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
|
|
|
|
|
|
|
// Check if we are initialized
|
2018-01-19 09:44:44 +03:00
|
|
|
init, err := c.Initialized(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
return nil, ErrAlreadyInit
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:04:13 +03:00
|
|
|
// Bootstrap the raft backend if that's provided as the physical or
|
|
|
|
// HA backend.
|
|
|
|
raftBackend := c.getRaftBackend()
|
|
|
|
if raftBackend != nil {
|
|
|
|
err := c.RaftBootstrap(ctx, true)
|
2019-06-20 22:14:58 +03:00
|
|
|
if err != nil {
|
2020-06-23 22:04:13 +03:00
|
|
|
c.logger.Error("failed to bootstrap raft", "error", err)
|
|
|
|
return nil, err
|
2019-06-20 22:14:58 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:04:13 +03:00
|
|
|
// Teardown cluster after bootstrap setup
|
2019-06-20 22:14:58 +03:00
|
|
|
defer func() {
|
2020-06-23 22:04:13 +03:00
|
|
|
if err := raftBackend.TeardownCluster(nil); err != nil {
|
|
|
|
c.logger.Error("failed to stop raft", "error", err)
|
2019-06-20 22:14:58 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-01-19 09:44:44 +03:00
|
|
|
err = c.seal.Init(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to initialize seal", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("error initializing seal: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 21:46:00 +03:00
|
|
|
initPTCleanup := initPTFunc(c)
|
|
|
|
if initPTCleanup != nil {
|
|
|
|
defer initPTCleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
barrierKey, barrierKeyShares, err := c.generateShares(barrierConfig)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("error generating shares", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-18 21:46:00 +03:00
|
|
|
var sealKey []byte
|
|
|
|
var sealKeyShares [][]byte
|
2022-08-23 22:37:16 +03:00
|
|
|
|
|
|
|
if barrierConfig.StoredShares == 1 && c.seal.BarrierType() == wrapping.WrapperTypeShamir {
|
2019-10-18 21:46:00 +03:00
|
|
|
sealKey, sealKeyShares, err = c.generateShares(barrierConfig)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("error generating shares", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
// Initialize the barrier
|
2019-10-18 21:46:00 +03:00
|
|
|
if err := c.barrier.Initialize(ctx, barrierKey, sealKey, c.secureRandomReader); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to initialize barrier", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to initialize barrier: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
2016-08-19 23:45:17 +03:00
|
|
|
if c.logger.IsInfo() {
|
2019-10-18 21:46:00 +03:00
|
|
|
c.logger.Info("security barrier initialized", "stored", barrierConfig.StoredShares, "shares", barrierConfig.SecretShares, "threshold", barrierConfig.SecretThreshold)
|
2016-08-19 23:45:17 +03:00
|
|
|
}
|
2016-04-04 17:44:22 +03:00
|
|
|
|
|
|
|
// Unseal the barrier
|
2018-01-19 09:44:44 +03:00
|
|
|
if err := c.barrier.Unseal(ctx, barrierKey); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to unseal barrier", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to unseal barrier: %w", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the barrier is re-sealed
|
|
|
|
defer func() {
|
2017-02-17 07:09:39 +03: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 17:44:22 +03:00
|
|
|
if err := c.barrier.Seal(); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to seal barrier", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-01-19 09:44:44 +03:00
|
|
|
err = c.seal.SetBarrierConfig(ctx, barrierConfig)
|
2017-02-17 07:09:39 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to save barrier configuration", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("barrier configuration saving failed: %w", err)
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 21:46:00 +03:00
|
|
|
results := &InitResult{
|
|
|
|
SecretShares: [][]byte{},
|
|
|
|
}
|
|
|
|
|
2017-02-17 07:09:39 +03:00
|
|
|
// If we are storing shares, pop them out of the returned results and push
|
|
|
|
// them through the seal
|
2019-10-18 21:46:00 +03:00
|
|
|
switch c.seal.StoredKeysSupported() {
|
2021-12-07 04:12:20 +03:00
|
|
|
case seal.StoredKeysSupportedShamirRoot:
|
2019-10-18 21:46:00 +03:00
|
|
|
keysToStore := [][]byte{barrierKey}
|
2023-05-04 21:22:30 +03:00
|
|
|
shamirWrapper, err := c.seal.GetShamirWrapper()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := shamirWrapper.SetAesGcmKeyBytes(sealKey); err != nil {
|
2019-10-18 21:46:00 +03:00
|
|
|
c.logger.Error("failed to set seal key", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to set seal key: %w", err)
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
2018-01-19 09:44:44 +03:00
|
|
|
if err := c.seal.SetStoredKeys(ctx, keysToStore); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to store keys", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to store keys: %w", err)
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
2019-10-18 21:46:00 +03:00
|
|
|
results.SecretShares = sealKeyShares
|
2020-01-11 04:39:52 +03:00
|
|
|
case seal.StoredKeysSupportedGeneric:
|
2019-10-18 21:46:00 +03:00
|
|
|
keysToStore := [][]byte{barrierKey}
|
|
|
|
if err := c.seal.SetStoredKeys(ctx, keysToStore); err != nil {
|
|
|
|
c.logger.Error("failed to store keys", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to store keys: %w", err)
|
2019-10-18 21:46:00 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// We don't support initializing an old-style Shamir seal anymore, so
|
|
|
|
// this case is only reachable by tests.
|
|
|
|
results.SecretShares = barrierKeyShares
|
2017-02-17 07:09:39 +03:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
// Perform initial setup
|
2018-01-19 09:44:44 +03:00
|
|
|
if err := c.setupCluster(ctx); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("cluster setup failed during init", "error", err)
|
2016-08-15 16:42:42 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-01 22:07:37 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
// Start tracking
|
|
|
|
if initPTCleanup != nil {
|
|
|
|
initPTCleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCtx, ctxCancel := context.WithCancel(namespace.RootContext(nil))
|
|
|
|
if err := c.postUnseal(activeCtx, ctxCancel, standardUnsealStrategy{}); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("post-unseal setup failed during init", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-15 20:04:29 +03: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 11:44:06 +03:00
|
|
|
if c.seal.RecoveryKeySupported() {
|
2018-01-19 09:44:44 +03:00
|
|
|
err = c.seal.SetRecoveryConfig(ctx, recoveryConfig)
|
2016-04-15 20:04:29 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to save recovery configuration", "error", err)
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("recovery configuration saving failed: %w", err)
|
2016-04-15 20:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if recoveryConfig.SecretShares > 0 {
|
|
|
|
recoveryKey, recoveryUnsealKeys, err := c.generateShares(recoveryConfig)
|
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("failed to generate recovery shares", "error", err)
|
2016-04-15 20:04:29 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 09:44:44 +03:00
|
|
|
err = c.seal.SetRecoveryKey(ctx, recoveryKey)
|
2016-04-15 20:04:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
results.RecoveryShares = recoveryUnsealKeys
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
// Generate a new root token
|
2018-01-19 09:44:44 +03:00
|
|
|
rootToken, err := c.tokenStore.rootToken(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("root token generation failed", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-17 22:43:07 +03:00
|
|
|
results.RootToken = rootToken.ExternalID
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Info("root token generated")
|
2016-04-04 17:44:22 +03:00
|
|
|
|
2016-09-14 01:42:24 +03:00
|
|
|
if initParams.RootTokenPGPKey != "" {
|
|
|
|
_, encryptedVals, err := pgpkeys.EncryptShares([][]byte{[]byte(results.RootToken)}, []string{initParams.RootTokenPGPKey})
|
|
|
|
if err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("root token encryption failed", "error", err)
|
2016-09-14 01:42:24 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results.RootToken = base64.StdEncoding.EncodeToString(encryptedVals[0])
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:04:13 +03:00
|
|
|
if raftBackend != nil {
|
|
|
|
if _, err := c.raftCreateTLSKeyring(ctx); err != nil {
|
|
|
|
c.logger.Error("failed to create raft TLS keyring", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-20 22:14:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
// Prepare to re-seal
|
|
|
|
if err := c.preSeal(); err != nil {
|
2018-04-03 03:46:59 +03:00
|
|
|
c.logger.Error("pre-seal teardown failed", "error", err)
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:42:03 +03:00
|
|
|
if c.serviceRegistration != nil {
|
|
|
|
if err := c.serviceRegistration.NotifyInitializedStateChange(true); err != nil {
|
|
|
|
if c.logger.IsWarn() {
|
|
|
|
c.logger.Warn("notification of initialization failed", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:34:34 +03:00
|
|
|
// UnsealWithStoredKeys performs auto-unseal using stored keys. An error
|
|
|
|
// return value of "nil" implies the Vault instance is unsealed.
|
|
|
|
//
|
2019-06-20 22:14:58 +03:00
|
|
|
// Callers should attempt to retry any NonFatalErrors. Callers should
|
2019-01-24 00:34:34 +03:00
|
|
|
// not re-attempt fatal errors.
|
2018-01-19 09:44:44 +03:00
|
|
|
func (c *Core) UnsealWithStoredKeys(ctx context.Context) error {
|
2019-01-24 00:34:34 +03:00
|
|
|
c.unsealWithStoredKeysLock.Lock()
|
|
|
|
defer c.unsealWithStoredKeysLock.Unlock()
|
|
|
|
|
2022-08-23 22:37:16 +03:00
|
|
|
if c.seal.BarrierType() == wrapping.WrapperTypeShamir {
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:34:02 +03:00
|
|
|
// Disallow auto-unsealing when migrating
|
2023-10-27 17:47:12 +03:00
|
|
|
if c.IsInSealMigrationMode(true) && !c.IsSealMigrated(true) {
|
2019-01-24 00:34:34 +03:00
|
|
|
return NewNonFatalError(errors.New("cannot auto-unseal during seal migration"))
|
2018-10-23 09:34:02 +03:00
|
|
|
}
|
|
|
|
|
2020-10-23 21:16:04 +03:00
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
|
|
|
|
2018-07-24 23:57:25 +03:00
|
|
|
sealed := c.Sealed()
|
2016-04-14 04:12:58 +03:00
|
|
|
if !sealed {
|
2019-01-24 00:34:34 +03:00
|
|
|
c.Logger().Warn("attempted unseal with stored keys, but vault is already unsealed")
|
2016-04-14 04:12:58 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:34:34 +03:00
|
|
|
c.Logger().Info("stored unseal keys supported, attempting fetch")
|
2018-01-19 09:44:44 +03:00
|
|
|
keys, err := c.seal.GetStoredKeys(ctx)
|
2016-04-04 17:44:22 +03:00
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return NewNonFatalError(fmt.Errorf("fetching stored unseal keys failed: %w", err))
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
2019-01-24 00:34:34 +03:00
|
|
|
|
|
|
|
// This usually happens when auto-unseal is configured, but the servers have
|
|
|
|
// not been initialized yet.
|
2016-04-04 17:44:22 +03:00
|
|
|
if len(keys) == 0 {
|
2019-01-24 00:34:34 +03:00
|
|
|
return NewNonFatalError(errors.New("stored unseal keys are supported, but none were found"))
|
|
|
|
}
|
2020-10-23 21:16:04 +03:00
|
|
|
if len(keys) != 1 {
|
|
|
|
return NewNonFatalError(errors.New("expected exactly one stored key"))
|
|
|
|
}
|
2019-01-24 00:34:34 +03:00
|
|
|
|
2020-10-23 21:16:04 +03:00
|
|
|
err = c.unsealInternal(ctx, keys[0])
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return NewNonFatalError(fmt.Errorf("unseal with stored key failed: %w", err))
|
2016-04-04 17:44:22 +03:00
|
|
|
}
|
2017-10-23 23:49:46 +03:00
|
|
|
|
2020-10-23 21:16:04 +03:00
|
|
|
if c.Sealed() {
|
2019-01-24 00:34:34 +03:00
|
|
|
// This most likely means that the user configured Vault to only store a
|
|
|
|
// subset of the required threshold of keys. We still consider this a
|
|
|
|
// "success", since trying again would yield the same result.
|
2020-10-23 21:16:04 +03:00
|
|
|
c.Logger().Warn("vault still sealed after using stored unseal key")
|
2019-01-24 00:34:34 +03:00
|
|
|
} else {
|
2020-10-23 21:16:04 +03:00
|
|
|
c.Logger().Info("unsealed with stored key")
|
2019-01-24 00:34:34 +03:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:44:22 +03:00
|
|
|
return nil
|
|
|
|
}
|