2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-05-28 21:28:50 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2016-07-06 16:25:40 +00:00
|
|
|
"encoding/json"
|
2017-01-17 20:43:10 +00:00
|
|
|
"fmt"
|
2015-05-28 21:28:50 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-20 02:04:45 +00:00
|
|
|
"github.com/go-test/deep"
|
2023-02-22 17:46:46 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/testhelpers/schema"
|
2015-05-28 21:28:50 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2017-01-12 05:05:41 +00:00
|
|
|
// Test to check if the API errors out when wrong number of PGP keys are
|
|
|
|
// supplied for rekey
|
2017-10-23 21:39:21 +00:00
|
|
|
func TestSysRekey_Init_pgpKeysEntriesForRekey(t *testing.T) {
|
2023-02-22 17:46:46 +00:00
|
|
|
cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
RequestResponseCallback: schema.ResponseValidatingCallback(t),
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cl := cluster.Cores[0].Client
|
2017-01-12 05:05:41 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
_, err := cl.Logical().Write("sys/rekey/init", map[string]interface{}{
|
2017-01-12 05:05:41 +00:00
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
|
|
|
"pgp_keys": []string{"pgpkey1"},
|
|
|
|
})
|
2023-02-22 17:46:46 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have failed to write pgp key entry due to mismatched keys", err)
|
|
|
|
}
|
2017-01-12 05:05:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:39:21 +00:00
|
|
|
func TestSysRekey_Init_Status(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Run("status-barrier-default", func(t *testing.T) {
|
2023-02-22 17:46:46 +00:00
|
|
|
cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
RequestResponseCallback: schema.ResponseValidatingCallback(t),
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cl := cluster.Cores[0].Client
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
resp, err := cl.Logical().Read("sys/rekey/init")
|
2018-09-18 03:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
actual := resp.Data
|
2018-09-18 03:03:00 +00:00
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": false,
|
|
|
|
"t": json.Number("0"),
|
|
|
|
"n": json.Number("0"),
|
|
|
|
"progress": json.Number("0"),
|
|
|
|
"required": json.Number("3"),
|
|
|
|
"pgp_fingerprints": interface{}(nil),
|
|
|
|
"backup": false,
|
|
|
|
"nonce": "",
|
|
|
|
"verification_required": false,
|
|
|
|
}
|
2023-02-22 17:46:46 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-05-28 21:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:39:21 +00:00
|
|
|
func TestSysRekey_Init_Setup(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Run("init-barrier-barrier-key", func(t *testing.T) {
|
2023-02-22 17:46:46 +00:00
|
|
|
cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
RequestResponseCallback: schema.ResponseValidatingCallback(t),
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cl := cluster.Cores[0].Client
|
2018-09-18 03:03:00 +00:00
|
|
|
|
|
|
|
// Start rekey
|
2023-02-22 17:46:46 +00:00
|
|
|
resp, err := cl.Logical().Write("sys/rekey/init", map[string]interface{}{
|
2018-09-18 03:03:00 +00:00
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
|
|
|
})
|
2023-02-22 17:46:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
actual := resp.Data
|
2018-09-18 03:03:00 +00:00
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": true,
|
|
|
|
"t": json.Number("3"),
|
|
|
|
"n": json.Number("5"),
|
|
|
|
"progress": json.Number("0"),
|
|
|
|
"required": json.Number("3"),
|
|
|
|
"pgp_fingerprints": interface{}(nil),
|
|
|
|
"backup": false,
|
|
|
|
"verification_required": false,
|
|
|
|
}
|
2023-02-22 17:46:46 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
|
|
|
expected["nonce"] = actual["nonce"]
|
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
// Get rekey status
|
2023-02-22 17:46:46 +00:00
|
|
|
resp, err = cl.Logical().Read("sys/rekey/init")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
actual = resp.Data
|
2018-09-18 03:03:00 +00:00
|
|
|
expected = map[string]interface{}{
|
|
|
|
"started": true,
|
|
|
|
"t": json.Number("3"),
|
|
|
|
"n": json.Number("5"),
|
|
|
|
"progress": json.Number("0"),
|
|
|
|
"required": json.Number("3"),
|
|
|
|
"pgp_fingerprints": interface{}(nil),
|
|
|
|
"backup": false,
|
|
|
|
"verification_required": false,
|
|
|
|
}
|
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
|
|
|
expected["nonce"] = actual["nonce"]
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-05-28 21:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:39:21 +00:00
|
|
|
func TestSysRekey_Init_Cancel(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Run("cancel-barrier-barrier-key", func(t *testing.T) {
|
2023-02-22 17:46:46 +00:00
|
|
|
cluster := vault.NewTestCluster(t, nil, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: Handler,
|
|
|
|
RequestResponseCallback: schema.ResponseValidatingCallback(t),
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
cl := cluster.Cores[0].Client
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
_, err := cl.Logical().Write("sys/rekey/init", map[string]interface{}{
|
2018-09-18 03:03:00 +00:00
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
|
|
|
})
|
2023-02-22 17:46:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
_, err = cl.Logical().Delete("sys/rekey/init")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
resp, err := cl.Logical().Read("sys/rekey/init")
|
2018-09-18 03:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-05-28 21:28:50 +00:00
|
|
|
|
2023-02-22 17:46:46 +00:00
|
|
|
actual := resp.Data
|
2018-09-18 03:03:00 +00:00
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": false,
|
|
|
|
"t": json.Number("0"),
|
|
|
|
"n": json.Number("0"),
|
|
|
|
"progress": json.Number("0"),
|
|
|
|
"required": json.Number("3"),
|
|
|
|
"pgp_fingerprints": interface{}(nil),
|
|
|
|
"backup": false,
|
|
|
|
"nonce": "",
|
|
|
|
"verification_required": false,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2015-05-28 21:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSysRekey_badKey(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2015-08-22 00:36:19 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/rekey/update", map[string]interface{}{
|
2015-05-28 21:28:50 +00:00
|
|
|
"key": "0123",
|
|
|
|
})
|
|
|
|
testResponseStatus(t, resp, 400)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSysRekey_Update(t *testing.T) {
|
2018-09-18 03:03:00 +00:00
|
|
|
t.Run("rekey-barrier-barrier-key", func(t *testing.T) {
|
|
|
|
core, keys, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/rekey/init", map[string]interface{}{
|
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
2017-01-17 20:43:10 +00:00
|
|
|
})
|
2018-09-18 03:03:00 +00:00
|
|
|
var rekeyStatus map[string]interface{}
|
2017-01-17 20:43:10 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2018-09-18 03:03:00 +00:00
|
|
|
testResponseBody(t, resp, &rekeyStatus)
|
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
var expected map[string]interface{}
|
|
|
|
|
|
|
|
for i, key := range keys {
|
|
|
|
resp = testHttpPut(t, token, addr+"/v1/sys/rekey/update", map[string]interface{}{
|
|
|
|
"nonce": rekeyStatus["nonce"].(string),
|
|
|
|
"key": hex.EncodeToString(key),
|
|
|
|
})
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"started": true,
|
|
|
|
"nonce": rekeyStatus["nonce"].(string),
|
|
|
|
"backup": false,
|
|
|
|
"pgp_fingerprints": interface{}(nil),
|
|
|
|
"required": json.Number("3"),
|
|
|
|
"t": json.Number("3"),
|
|
|
|
"n": json.Number("5"),
|
|
|
|
"progress": json.Number(fmt.Sprintf("%d", i+1)),
|
|
|
|
"verification_required": false,
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
|
|
|
if i+1 == len(keys) {
|
|
|
|
delete(expected, "started")
|
|
|
|
delete(expected, "required")
|
|
|
|
delete(expected, "t")
|
|
|
|
delete(expected, "n")
|
|
|
|
delete(expected, "progress")
|
|
|
|
expected["complete"] = true
|
|
|
|
expected["keys"] = actual["keys"]
|
|
|
|
expected["keys_base64"] = actual["keys_base64"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if i+1 < len(keys) && (actual["nonce"] == nil || actual["nonce"].(string) == "") {
|
|
|
|
t.Fatalf("expected a nonce, i is %d, actual is %#v", i, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: \n%#v\nactual: \n%#v", expected, actual)
|
|
|
|
}
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
retKeys := actual["keys"].([]interface{})
|
|
|
|
if len(retKeys) != 5 {
|
|
|
|
t.Fatalf("bad: %#v", retKeys)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
keysB64 := actual["keys_base64"].([]interface{})
|
|
|
|
if len(keysB64) != 5 {
|
|
|
|
t.Fatalf("bad: %#v", keysB64)
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
})
|
2015-05-28 21:28:50 +00:00
|
|
|
}
|
2015-12-16 21:56:15 +00:00
|
|
|
|
|
|
|
func TestSysRekey_ReInitUpdate(t *testing.T) {
|
2017-01-17 20:43:10 +00:00
|
|
|
core, keys, token := vault.TestCoreUnsealed(t)
|
2015-12-16 21:56:15 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/rekey/init", map[string]interface{}{
|
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
|
|
|
})
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2015-12-16 21:56:15 +00:00
|
|
|
|
|
|
|
resp = testHttpDelete(t, token, addr+"/v1/sys/rekey/init")
|
|
|
|
testResponseStatus(t, resp, 204)
|
|
|
|
|
|
|
|
resp = testHttpPut(t, token, addr+"/v1/sys/rekey/init", map[string]interface{}{
|
|
|
|
"secret_shares": 5,
|
|
|
|
"secret_threshold": 3,
|
|
|
|
})
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2015-12-16 21:56:15 +00:00
|
|
|
|
|
|
|
resp = testHttpPut(t, token, addr+"/v1/sys/rekey/update", map[string]interface{}{
|
2017-01-17 20:43:10 +00:00
|
|
|
"key": hex.EncodeToString(keys[0]),
|
2015-12-16 21:56:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
testResponseStatus(t, resp, 400)
|
|
|
|
}
|