2018-10-23 06:34:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-17 17:33:00 +00:00
|
|
|
"crypto/rand"
|
2018-10-23 06:34:02 +00:00
|
|
|
"fmt"
|
2019-10-17 17:33:00 +00:00
|
|
|
"io"
|
2018-10-23 06:34:02 +00:00
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2020-01-11 01:39:52 +00:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping"
|
|
|
|
aeadwrapper "github.com/hashicorp/go-kms-wrapping/wrappers/aead"
|
2019-10-17 17:33:00 +00:00
|
|
|
"github.com/hashicorp/vault/command/server"
|
2018-10-23 06:34:02 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
2018-10-23 08:12:23 +00:00
|
|
|
vaultseal "github.com/hashicorp/vault/vault/seal"
|
2018-10-23 06:34:02 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-10-17 17:33:00 +00:00
|
|
|
onEnterprise = false
|
|
|
|
createSecureRandomReaderFunc = createSecureRandomReader
|
2020-02-15 00:39:13 +00:00
|
|
|
adjustCoreConfigForEnt = adjustCoreConfigForEntNoop
|
2018-10-23 06:34:02 +00:00
|
|
|
)
|
|
|
|
|
2020-02-15 00:39:13 +00:00
|
|
|
func adjustCoreConfigForEntNoop(config *server.Config, coreConfig *vault.CoreConfig) {
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:33:00 +00:00
|
|
|
func createSecureRandomReader(config *server.Config, seal *vault.Seal) (io.Reader, error) {
|
|
|
|
return rand.Reader, nil
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:14:58 +00:00
|
|
|
func adjustCoreForSealMigration(logger log.Logger, core *vault.Core, barrierSeal, unwrapSeal vault.Seal) error {
|
2018-10-23 06:34:02 +00:00
|
|
|
existBarrierSealConfig, existRecoverySealConfig, err := core.PhysicalSealConfigs(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error checking for existing seal: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:11:56 +00:00
|
|
|
// If we don't have an existing config or if it's the deprecated auto seal
|
|
|
|
// which needs an upgrade, skip out
|
2020-01-11 01:39:52 +00:00
|
|
|
if existBarrierSealConfig == nil || existBarrierSealConfig.Type == wrapping.HSMAutoDeprecated {
|
2019-03-04 22:11:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if unwrapSeal == nil {
|
|
|
|
// We have the same barrier type and the unwrap seal is nil so we're not
|
|
|
|
// migrating from same to same, IOW we assume it's not a migration
|
|
|
|
if existBarrierSealConfig.Type == barrierSeal.BarrierType() {
|
2018-10-23 06:34:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:11:56 +00:00
|
|
|
// If we're not coming from Shamir, and the existing type doesn't match
|
|
|
|
// the barrier type, we need both the migration seal and the new seal
|
2020-01-11 01:39:52 +00:00
|
|
|
if existBarrierSealConfig.Type != wrapping.Shamir && barrierSeal.BarrierType() != wrapping.Shamir {
|
2019-03-04 22:11:56 +00:00
|
|
|
return errors.New(`Trying to migrate from auto-seal to auto-seal but no "disabled" seal stanza found`)
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-11 01:39:52 +00:00
|
|
|
if unwrapSeal.BarrierType() == wrapping.Shamir {
|
2019-03-04 22:11:56 +00:00
|
|
|
return errors.New("Shamir seals cannot be set disabled (they should simply not be set)")
|
2018-10-23 06:34:02 +00:00
|
|
|
}
|
2019-03-04 22:11:56 +00:00
|
|
|
}
|
2018-10-23 06:34:02 +00:00
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
if existBarrierSealConfig.Type != wrapping.Shamir && existRecoverySealConfig == nil {
|
2019-03-04 22:11:56 +00:00
|
|
|
return errors.New(`Recovery seal configuration not found for existing seal`)
|
|
|
|
}
|
2018-10-23 06:34:02 +00:00
|
|
|
|
2020-01-21 17:24:33 +00:00
|
|
|
if onEnterprise && barrierSeal.BarrierType() == wrapping.Shamir {
|
|
|
|
return errors.New("Migrating from autoseal to Shamir seal is not currently supported on Vault Enterprise")
|
|
|
|
}
|
|
|
|
|
|
|
|
var migrationSeal vault.Seal
|
|
|
|
var newSeal vault.Seal
|
|
|
|
|
|
|
|
// Determine the migrationSeal. This is either going to be an instance of
|
|
|
|
// shamir or the unwrapSeal.
|
2019-03-04 22:11:56 +00:00
|
|
|
switch existBarrierSealConfig.Type {
|
2020-01-11 01:39:52 +00:00
|
|
|
case wrapping.Shamir:
|
2019-03-04 22:11:56 +00:00
|
|
|
// The value reflected in config is what we're going to
|
2020-01-21 17:24:33 +00:00
|
|
|
migrationSeal = vault.NewDefaultSeal(&vaultseal.Access{
|
2020-01-11 01:39:52 +00:00
|
|
|
Wrapper: aeadwrapper.NewWrapper(&wrapping.WrapperOptions{
|
|
|
|
Logger: logger.Named("shamir"),
|
|
|
|
}),
|
|
|
|
})
|
2020-01-21 17:24:33 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// If we're not coming from Shamir we expect the previous seal to be
|
|
|
|
// in the config and disabled.
|
|
|
|
migrationSeal = unwrapSeal
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSeal will be the barrierSeal
|
|
|
|
newSeal = barrierSeal
|
|
|
|
|
2020-02-13 21:27:31 +00:00
|
|
|
if migrationSeal != nil && newSeal != nil && migrationSeal.BarrierType() == newSeal.BarrierType() {
|
|
|
|
return errors.New("Migrating between same seal types is currently not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
if unwrapSeal != nil && existBarrierSealConfig.Type == barrierSeal.BarrierType() {
|
|
|
|
// In this case our migration seal is set so we are using it
|
|
|
|
// (potentially) for unwrapping. Set it on core for that purpose then
|
|
|
|
// exit.
|
|
|
|
core.SetSealsForMigration(nil, nil, unwrapSeal)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:24:33 +00:00
|
|
|
// Set the appropriate barrier and recovery configs.
|
|
|
|
switch {
|
|
|
|
case migrationSeal.RecoveryKeySupported() && newSeal.RecoveryKeySupported():
|
|
|
|
// Migrating from auto->auto, copy the configs over
|
|
|
|
newSeal.SetCachedBarrierConfig(existBarrierSealConfig)
|
|
|
|
newSeal.SetCachedRecoveryConfig(existRecoverySealConfig)
|
|
|
|
case migrationSeal.RecoveryKeySupported():
|
|
|
|
// Migrating from auto->shamir, clone auto's recovery config and set
|
|
|
|
// stored keys to 1.
|
|
|
|
newSealConfig := existRecoverySealConfig.Clone()
|
|
|
|
newSealConfig.StoredShares = 1
|
|
|
|
newSeal.SetCachedBarrierConfig(newSealConfig)
|
|
|
|
case newSeal.RecoveryKeySupported():
|
|
|
|
// Migrating from shamir->auto, set a new barrier config and set
|
|
|
|
// recovery config to a clone of shamir's barrier config with stored
|
|
|
|
// keys set to 0.
|
2019-03-04 22:11:56 +00:00
|
|
|
newBarrierSealConfig := &vault.SealConfig{
|
|
|
|
Type: newSeal.BarrierType(),
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
StoredShares: 1,
|
2018-10-23 06:34:02 +00:00
|
|
|
}
|
2019-03-04 22:11:56 +00:00
|
|
|
newSeal.SetCachedBarrierConfig(newBarrierSealConfig)
|
|
|
|
|
2020-01-21 17:24:33 +00:00
|
|
|
newRecoveryConfig := existBarrierSealConfig.Clone()
|
|
|
|
newRecoveryConfig.StoredShares = 0
|
|
|
|
newSeal.SetCachedRecoveryConfig(newRecoveryConfig)
|
2018-10-23 06:34:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:24:33 +00:00
|
|
|
core.SetSealsForMigration(migrationSeal, newSeal, unwrapSeal)
|
2019-03-04 22:11:56 +00:00
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
return nil
|
|
|
|
}
|