2016-01-14 22:01:04 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2016-04-25 19:39:04 +00:00
|
|
|
"fmt"
|
2016-01-14 22:01:04 +00:00
|
|
|
"reflect"
|
2019-10-18 18:46:00 +00:00
|
|
|
"strings"
|
2016-01-14 22:01:04 +00:00
|
|
|
"testing"
|
2016-04-04 14:44:22 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
2016-01-14 22:01:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCore_Rekey_Lifecycle(t *testing.T) {
|
2019-10-18 18:46:00 +00:00
|
|
|
bc := &SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
StoredShares: 1,
|
|
|
|
}
|
2017-10-23 17:42:04 +00:00
|
|
|
c, masterKeys, _, _ := TestCoreUnsealedWithConfigs(t, bc, nil)
|
2017-01-17 20:43:10 +00:00
|
|
|
if len(masterKeys) != 1 {
|
|
|
|
t.Fatalf("expected %d keys, got %d", bc.SecretShares-bc.StoredShares, len(masterKeys))
|
|
|
|
}
|
2019-10-18 18:46:00 +00:00
|
|
|
testCore_Rekey_Lifecycle_Common(t, c, false)
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 18:46:00 +00:00
|
|
|
func testCore_Rekey_Lifecycle_Common(t *testing.T, c *Core, recovery bool) {
|
|
|
|
min, _ := c.barrier.KeyLength()
|
2016-01-14 22:01:04 +00:00
|
|
|
// Verify update not allowed
|
2019-10-18 18:46:00 +00:00
|
|
|
_, err := c.RekeyUpdate(context.Background(), make([]byte, min), "", recovery)
|
|
|
|
expected := "no barrier rekey in progress"
|
|
|
|
if recovery {
|
|
|
|
expected = "no recovery rekey in progress"
|
|
|
|
}
|
|
|
|
if err == nil || !strings.Contains(err.Error(), expected) {
|
|
|
|
t.Fatalf("no rekey should be in progress, err: %v", err)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be no progress
|
2018-05-29 22:02:13 +00:00
|
|
|
if _, _, err := c.RekeyProgress(recovery, false); err == nil {
|
|
|
|
t.Fatal("expected error from RekeyProgress")
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be no config
|
2016-04-25 19:39:04 +00:00
|
|
|
conf, err := c.RekeyConfig(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if conf != nil {
|
|
|
|
t.Fatalf("bad: %v", conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel should be idempotent
|
2016-04-04 14:44:22 +00:00
|
|
|
err = c.RekeyCancel(false)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a rekey
|
|
|
|
newConf := &SealConfig{
|
|
|
|
SecretThreshold: 3,
|
|
|
|
SecretShares: 5,
|
|
|
|
}
|
2018-01-19 08:44:06 +00:00
|
|
|
err = c.RekeyInit(newConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get config
|
2016-04-25 19:39:04 +00:00
|
|
|
conf, err = c.RekeyConfig(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
newConf.Nonce = conf.Nonce
|
|
|
|
if !reflect.DeepEqual(conf, newConf) {
|
|
|
|
t.Fatalf("bad: %v", conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel should be clear
|
2016-04-25 19:39:04 +00:00
|
|
|
err = c.RekeyCancel(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be no config
|
2016-04-25 19:39:04 +00:00
|
|
|
conf, err = c.RekeyConfig(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if conf != nil {
|
|
|
|
t.Fatalf("bad: %v", conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCore_Rekey_Init(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Run("barrier-rekey-init", func(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
testCore_Rekey_Init_Common(t, c, false)
|
|
|
|
})
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_Rekey_Init_Common(t *testing.T, c *Core, recovery bool) {
|
2016-01-14 22:01:04 +00:00
|
|
|
// Try an invalid config
|
|
|
|
badConf := &SealConfig{
|
|
|
|
SecretThreshold: 5,
|
|
|
|
SecretShares: 1,
|
|
|
|
}
|
2018-01-19 08:44:06 +00:00
|
|
|
err := c.RekeyInit(badConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a rekey
|
|
|
|
newConf := &SealConfig{
|
|
|
|
SecretThreshold: 3,
|
|
|
|
SecretShares: 5,
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
|
|
|
// If recovery key is supported, set newConf
|
|
|
|
// to be a recovery seal config
|
|
|
|
if c.seal.RecoveryKeySupported() {
|
|
|
|
newConf.Type = c.seal.RecoveryType()
|
|
|
|
}
|
|
|
|
|
2018-01-19 08:44:06 +00:00
|
|
|
err = c.RekeyInit(newConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second should fail
|
2018-01-19 08:44:06 +00:00
|
|
|
err = c.RekeyInit(newConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCore_Rekey_Update(t *testing.T) {
|
2019-10-18 18:46:00 +00:00
|
|
|
bc := &SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
}
|
2017-10-23 17:42:04 +00:00
|
|
|
c, masterKeys, _, root := TestCoreUnsealedWithConfigs(t, bc, nil)
|
2016-04-25 19:39:04 +00:00
|
|
|
testCore_Rekey_Update_Common(t, c, masterKeys, root, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_Rekey_Update_Common(t *testing.T, c *Core, keys [][]byte, root string, recovery bool) {
|
2018-05-21 19:29:41 +00:00
|
|
|
var err error
|
2016-01-14 22:01:04 +00:00
|
|
|
// Start a rekey
|
2016-04-28 17:13:03 +00:00
|
|
|
var expType string
|
|
|
|
if recovery {
|
2018-01-19 08:17:36 +00:00
|
|
|
expType = c.seal.RecoveryType()
|
2016-04-28 17:13:03 +00:00
|
|
|
} else {
|
2018-01-19 08:17:36 +00:00
|
|
|
expType = c.seal.BarrierType()
|
2016-04-28 17:13:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:01:04 +00:00
|
|
|
newConf := &SealConfig{
|
2016-04-28 17:13:03 +00:00
|
|
|
Type: expType,
|
2016-01-14 22:01:04 +00:00
|
|
|
SecretThreshold: 3,
|
|
|
|
SecretShares: 5,
|
|
|
|
}
|
2018-05-21 19:29:41 +00:00
|
|
|
hErr := c.RekeyInit(newConf, recovery)
|
|
|
|
if hErr != nil {
|
|
|
|
t.Fatalf("err: %v", hErr)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch new config with generated nonce
|
2018-05-21 19:29:41 +00:00
|
|
|
rkconf, hErr := c.RekeyConfig(recovery)
|
|
|
|
if hErr != nil {
|
|
|
|
t.Fatalf("err: %v", hErr)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
if rkconf == nil {
|
|
|
|
t.Fatalf("bad: no rekey config received")
|
|
|
|
}
|
|
|
|
|
2017-10-23 18:59:37 +00:00
|
|
|
// Provide the master/recovery keys
|
2016-04-25 19:39:04 +00:00
|
|
|
var result *RekeyResult
|
|
|
|
for _, key := range keys {
|
2018-01-19 06:44:44 +00:00
|
|
|
result, err = c.RekeyUpdate(context.Background(), key, rkconf.Nonce, recovery)
|
2016-04-25 19:39:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
if result == nil {
|
|
|
|
t.Fatal("nil result after update")
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
|
|
|
|
// Should be no progress
|
2018-05-29 22:02:13 +00:00
|
|
|
if _, _, err := c.RekeyProgress(recovery, false); err == nil {
|
|
|
|
t.Fatal("expected error from RekeyProgress")
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be no config
|
2018-05-21 19:29:41 +00:00
|
|
|
conf, hErr := c.RekeyConfig(recovery)
|
|
|
|
if hErr != nil {
|
|
|
|
t.Fatalf("rekey config error: %v", hErr)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
if conf != nil {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Fatalf("rekey config should be nil, got: %v", conf)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SealConfig should update
|
2016-04-25 19:39:04 +00:00
|
|
|
var sealConf *SealConfig
|
|
|
|
if recovery {
|
2018-01-19 06:44:44 +00:00
|
|
|
sealConf, err = c.seal.RecoveryConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
} else {
|
2018-01-19 06:44:44 +00:00
|
|
|
sealConf, err = c.seal.BarrierConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
2017-10-23 18:59:37 +00:00
|
|
|
t.Fatalf("seal config retrieval error: %v", err)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
if sealConf == nil {
|
|
|
|
t.Fatal("seal configuration is nil")
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
|
|
|
|
newConf.Nonce = rkconf.Nonce
|
2016-04-04 14:44:22 +00:00
|
|
|
if !reflect.DeepEqual(sealConf, newConf) {
|
2016-04-28 17:13:03 +00:00
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v\nexpType: %s\nrecovery: %t", newConf, sealConf, expType, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// At this point bail if we are rekeying the barrier key with recovery
|
|
|
|
// keys, since a new rekey should still be using the same set of recovery
|
|
|
|
// keys and we haven't been returned key shares in this mode.
|
|
|
|
if !recovery && c.seal.RecoveryKeySupported() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// Attempt unseal if this was not recovery mode
|
|
|
|
if !recovery {
|
|
|
|
err = c.Seal(root)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
for i := 0; i < newConf.SecretThreshold; i++ {
|
2017-02-17 04:09:39 +00:00
|
|
|
_, err = TestCoreUnseal(c, TestKeyCopy(result.SecretShares[i]))
|
2016-04-25 19:39:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 20:57:25 +00:00
|
|
|
if c.Sealed() {
|
2016-04-25 19:39:04 +00:00
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start another rekey, this time we require a quorum!
|
2017-10-23 18:59:37 +00:00
|
|
|
|
2016-01-14 22:01:04 +00:00
|
|
|
newConf = &SealConfig{
|
2016-04-28 17:13:03 +00:00
|
|
|
Type: expType,
|
2016-01-14 22:01:04 +00:00
|
|
|
SecretThreshold: 1,
|
|
|
|
SecretShares: 1,
|
|
|
|
}
|
2018-01-19 08:44:06 +00:00
|
|
|
err = c.RekeyInit(newConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch new config with generated nonce
|
2016-04-25 19:39:04 +00:00
|
|
|
rkconf, err = c.RekeyConfig(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if rkconf == nil {
|
|
|
|
t.Fatalf("bad: no rekey config received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide the parts master
|
|
|
|
oldResult := result
|
|
|
|
for i := 0; i < 3; i++ {
|
2018-01-19 06:44:44 +00:00
|
|
|
result, err = c.RekeyUpdate(context.Background(), TestKeyCopy(oldResult.SecretShares[i]), rkconf.Nonce, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be progress
|
2018-05-29 22:02:13 +00:00
|
|
|
if i < 2 {
|
|
|
|
_, num, err := c.RekeyProgress(recovery, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if num != i+1 {
|
|
|
|
t.Fatalf("bad: %d", num)
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if result == nil || len(result.SecretShares) != 1 {
|
|
|
|
t.Fatalf("Bad: %#v", result)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// Attempt unseal if this was not recovery mode
|
|
|
|
if !recovery {
|
|
|
|
err = c.Seal(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
unseal, err := TestCoreUnseal(c, result.SecretShares[0])
|
2016-04-25 19:39:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SealConfig should update
|
2016-04-25 19:39:04 +00:00
|
|
|
if recovery {
|
2018-01-19 06:44:44 +00:00
|
|
|
sealConf, err = c.seal.RecoveryConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
} else {
|
2018-01-19 06:44:44 +00:00
|
|
|
sealConf, err = c.seal.BarrierConfig(context.Background())
|
2016-04-25 19:39:04 +00:00
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
|
2016-01-14 22:01:04 +00:00
|
|
|
newConf.Nonce = rkconf.Nonce
|
2016-04-04 14:44:22 +00:00
|
|
|
if !reflect.DeepEqual(sealConf, newConf) {
|
|
|
|
t.Fatalf("bad: %#v", sealConf)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:46:00 +00:00
|
|
|
func TestCore_Rekey_Legacy(t *testing.T) {
|
|
|
|
bc := &SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
}
|
|
|
|
c, masterKeys, _, root := TestCoreUnsealedWithConfigSealOpts(t, bc, nil,
|
|
|
|
&TestSealOpts{StoredKeys: StoredKeysNotSupported})
|
|
|
|
testCore_Rekey_Update_Common(t, c, masterKeys, root, false)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
func TestCore_Rekey_Invalid(t *testing.T) {
|
2019-10-18 18:46:00 +00:00
|
|
|
bc := &SealConfig{
|
|
|
|
SecretShares: 5,
|
|
|
|
SecretThreshold: 3,
|
|
|
|
}
|
2016-04-25 19:39:04 +00:00
|
|
|
bc.StoredShares = 0
|
|
|
|
bc.SecretShares = 1
|
|
|
|
bc.SecretThreshold = 1
|
2017-10-23 17:42:04 +00:00
|
|
|
c, masterKeys, _, _ := TestCoreUnsealedWithConfigs(t, bc, nil)
|
2016-04-25 19:39:04 +00:00
|
|
|
testCore_Rekey_Invalid_Common(t, c, masterKeys, false)
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
func testCore_Rekey_Invalid_Common(t *testing.T, c *Core, keys [][]byte, recovery bool) {
|
2016-01-14 22:01:04 +00:00
|
|
|
// Start a rekey
|
|
|
|
newConf := &SealConfig{
|
|
|
|
SecretThreshold: 3,
|
|
|
|
SecretShares: 5,
|
|
|
|
}
|
2018-01-19 08:44:06 +00:00
|
|
|
err := c.RekeyInit(newConf, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch new config with generated nonce
|
2016-04-25 19:39:04 +00:00
|
|
|
rkconf, err := c.RekeyConfig(recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if rkconf == nil {
|
|
|
|
t.Fatalf("bad: no rekey config received")
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// Provide the nonce (invalid)
|
2018-01-19 06:44:44 +00:00
|
|
|
_, err = c.RekeyUpdate(context.Background(), keys[0], "abcd", recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error")
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:39:04 +00:00
|
|
|
// Provide the key (invalid)
|
|
|
|
key := keys[0]
|
|
|
|
oldkeystr := fmt.Sprintf("%#v", key)
|
|
|
|
key[0]++
|
|
|
|
newkeystr := fmt.Sprintf("%#v", key)
|
2018-01-19 06:44:44 +00:00
|
|
|
ret, err := c.RekeyUpdate(context.Background(), key, rkconf.Nonce, recovery)
|
2016-01-14 22:01:04 +00:00
|
|
|
if err == nil {
|
2016-04-25 19:39:04 +00:00
|
|
|
t.Fatalf("expected error, ret is %#v\noldkeystr: %s\nnewkeystr: %s", *ret, oldkeystr, newkeystr)
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
2018-11-20 01:03:07 +00:00
|
|
|
|
|
|
|
// Check progress has been reset
|
|
|
|
_, num, err := c.RekeyProgress(recovery, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if num != 0 {
|
|
|
|
t.Fatalf("rekey progress should be 0, got: %d", num)
|
|
|
|
}
|
2016-01-14 22:01:04 +00:00
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
|
2018-05-21 19:29:41 +00:00
|
|
|
func TestCore_Rekey_Standby(t *testing.T) {
|
2016-04-04 14:44:22 +00:00
|
|
|
// Create the first core and initialize it
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
inm, err := inmem.NewInmemHA(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
inmha, err := inmem.NewInmemHA(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
redirectOriginal := "http://127.0.0.1:8200"
|
2016-04-04 14:44:22 +00:00
|
|
|
core, err := NewCore(&CoreConfig{
|
2016-08-15 13:42:42 +00:00
|
|
|
Physical: inm,
|
2017-08-03 17:24:27 +00:00
|
|
|
HAPhysical: inmha.(physical.HABackend),
|
2016-08-15 13:42:42 +00:00
|
|
|
RedirectAddr: redirectOriginal,
|
|
|
|
DisableMlock: true,
|
2017-01-17 20:43:10 +00:00
|
|
|
DisableCache: true,
|
2016-04-04 14:44:22 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
keys, root := TestCoreInit(t, core)
|
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for core to become active
|
2016-08-15 13:42:42 +00:00
|
|
|
TestWaitActive(t, core)
|
2016-04-04 14:44:22 +00:00
|
|
|
|
|
|
|
// Create a second core, attached to same in-memory store
|
2016-08-15 13:42:42 +00:00
|
|
|
redirectOriginal2 := "http://127.0.0.1:8500"
|
2016-04-04 14:44:22 +00:00
|
|
|
core2, err := NewCore(&CoreConfig{
|
2016-08-15 13:42:42 +00:00
|
|
|
Physical: inm,
|
2017-08-03 17:24:27 +00:00
|
|
|
HAPhysical: inmha.(physical.HABackend),
|
2016-08-15 13:42:42 +00:00
|
|
|
RedirectAddr: redirectOriginal2,
|
|
|
|
DisableMlock: true,
|
2017-01-17 20:43:10 +00:00
|
|
|
DisableCache: true,
|
2016-04-04 14:44:22 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := TestCoreUnseal(core2, TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rekey the master key
|
|
|
|
newConf := &SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
|
|
|
}
|
2018-01-19 08:44:06 +00:00
|
|
|
err = core.RekeyInit(newConf, false)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Fetch new config with generated nonce
|
|
|
|
rkconf, err := core.RekeyConfig(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if rkconf == nil {
|
|
|
|
t.Fatalf("bad: no rekey config received")
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
var rekeyResult *RekeyResult
|
|
|
|
for _, key := range keys {
|
2018-01-19 06:44:44 +00:00
|
|
|
rekeyResult, err = core.RekeyUpdate(context.Background(), key, rkconf.Nonce, false)
|
2017-01-17 20:43:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
if rekeyResult == nil {
|
2016-04-04 14:44:22 +00:00
|
|
|
t.Fatalf("rekey failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal the first core, should step down
|
|
|
|
err = core.Seal(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for core2 to become active
|
2016-08-15 13:42:42 +00:00
|
|
|
TestWaitActive(t, core2)
|
2016-04-04 14:44:22 +00:00
|
|
|
|
|
|
|
// Rekey the master key again
|
2018-01-19 08:44:06 +00:00
|
|
|
err = core2.RekeyInit(newConf, false)
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Fetch new config with generated nonce
|
|
|
|
rkconf, err = core2.RekeyConfig(false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if rkconf == nil {
|
|
|
|
t.Fatalf("bad: no rekey config received")
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
var rekeyResult2 *RekeyResult
|
|
|
|
for _, key := range rekeyResult.SecretShares {
|
2018-01-19 06:44:44 +00:00
|
|
|
rekeyResult2, err = core2.RekeyUpdate(context.Background(), key, rkconf.Nonce, false)
|
2017-01-17 20:43:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if rekeyResult2 == nil {
|
|
|
|
t.Fatalf("rekey failed")
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
if rekeyResult2 == nil {
|
2016-04-04 14:44:22 +00:00
|
|
|
t.Fatalf("rekey failed")
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 18:46:00 +00:00
|
|
|
|
|
|
|
// verifies that if we are using recovery keys to force a
|
|
|
|
// rekey of a stored-shares barrier that verification is not allowed since
|
|
|
|
// the keys aren't returned
|
|
|
|
func TestSysRekey_Verification_Invalid(t *testing.T) {
|
|
|
|
core, _, _, _ := TestCoreUnsealedWithConfigSealOpts(t,
|
|
|
|
&SealConfig{StoredShares: 1, SecretShares: 1, SecretThreshold: 1},
|
|
|
|
&SealConfig{StoredShares: 1, SecretShares: 1, SecretThreshold: 1},
|
|
|
|
&TestSealOpts{StoredKeys: StoredKeysSupportedGeneric})
|
|
|
|
|
|
|
|
err := core.BarrierRekeyInit(&SealConfig{
|
|
|
|
VerificationRequired: true,
|
|
|
|
StoredShares: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "requiring verification not supported") {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|