open-vault/http/sys_rekey_test.go

263 lines
7.2 KiB
Go
Raw Normal View History

2015-05-28 21:28:50 +00:00
package http
import (
"encoding/hex"
"encoding/json"
2017-01-17 20:43:10 +00:00
"fmt"
2015-05-28 21:28:50 +00:00
"net/http"
"reflect"
"testing"
"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) {
2017-01-12 05:05:41 +00:00
core, _, 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,
"pgp_keys": []string{"pgpkey1"},
})
testResponseStatus(t, resp, 400)
}
2017-10-23 21:39:21 +00:00
func TestSysRekey_Init_Status(t *testing.T) {
2015-05-28 21:28:50 +00:00
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := TestServer(t, core)
defer ln.Close()
TestServerAuth(t, addr, token)
resp, err := http.Get(addr + "/v1/sys/rekey/init")
if err != nil {
t.Fatalf("err: %s", err)
}
var actual map[string]interface{}
expected := map[string]interface{}{
2015-12-16 21:56:15 +00:00
"started": false,
"t": json.Number("0"),
"n": json.Number("0"),
"progress": json.Number("0"),
2017-01-17 20:43:10 +00:00
"required": json.Number("3"),
2015-12-16 21:56:15 +00:00
"pgp_fingerprints": interface{}(nil),
"backup": false,
"nonce": "",
2015-05-28 21:28:50 +00:00
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if !reflect.DeepEqual(actual, expected) {
2015-12-16 21:56:15 +00:00
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) {
2015-05-28 21:28:50 +00:00
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := TestServer(t, core)
defer ln.Close()
TestServerAuth(t, addr, token)
2017-10-23 21:41:44 +00:00
// Start rekey
2015-08-22 00:36:19 +00:00
resp := testHttpPut(t, token, addr+"/v1/sys/rekey/init", map[string]interface{}{
2015-05-28 21:28:50 +00:00
"secret_shares": 5,
"secret_threshold": 3,
})
testResponseStatus(t, resp, 200)
2015-05-28 21:28:50 +00:00
var actual map[string]interface{}
expected := map[string]interface{}{
2015-12-16 21:56:15 +00:00
"started": true,
"t": json.Number("3"),
"n": json.Number("5"),
"progress": json.Number("0"),
2017-01-17 20:43:10 +00:00
"required": json.Number("3"),
2015-12-16 21:56:15 +00:00
"pgp_fingerprints": interface{}(nil),
"backup": false,
2015-05-28 21:28:50 +00:00
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if actual["nonce"].(string) == "" {
t.Fatalf("nonce was empty")
}
2015-12-16 21:56:15 +00:00
expected["nonce"] = actual["nonce"]
2015-05-28 21:28:50 +00:00
if !reflect.DeepEqual(actual, expected) {
2015-12-16 21:56:15 +00:00
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
2015-05-28 21:28:50 +00:00
}
2017-10-23 21:41:44 +00:00
// Get rekey status
resp = testHttpGet(t, token, addr+"/v1/sys/rekey/init")
actual = map[string]interface{}{}
expected = map[string]interface{}{
"started": true,
"t": json.Number("3"),
"n": json.Number("5"),
"progress": json.Number("0"),
2017-01-17 20:43:10 +00:00
"required": json.Number("3"),
"pgp_fingerprints": interface{}(nil),
"backup": false,
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
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) {
2015-05-28 21:28:50 +00:00
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/init", map[string]interface{}{
2015-05-28 21:28:50 +00:00
"secret_shares": 5,
"secret_threshold": 3,
})
testResponseStatus(t, resp, 200)
2015-05-28 21:28:50 +00:00
2015-08-22 00:36:19 +00:00
resp = testHttpDelete(t, token, addr+"/v1/sys/rekey/init")
2015-05-28 21:28:50 +00:00
testResponseStatus(t, resp, 204)
resp, err := http.Get(addr + "/v1/sys/rekey/init")
if err != nil {
t.Fatalf("err: %s", err)
}
var actual map[string]interface{}
expected := map[string]interface{}{
2015-12-16 21:56:15 +00:00
"started": false,
"t": json.Number("0"),
"n": json.Number("0"),
"progress": json.Number("0"),
2017-01-17 20:43:10 +00:00
"required": json.Number("3"),
2015-12-16 21:56:15 +00:00
"pgp_fingerprints": interface{}(nil),
"backup": false,
"nonce": "",
2015-05-28 21:28:50 +00:00
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if !reflect.DeepEqual(actual, expected) {
2015-12-16 21:56:15 +00:00
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) {
2017-01-17 20:43:10 +00:00
core, keys, token := vault.TestCoreUnsealed(t)
2015-05-28 21:28:50 +00:00
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/init", map[string]interface{}{
2015-05-28 21:28:50 +00:00
"secret_shares": 5,
"secret_threshold": 3,
})
2015-12-16 21:56:15 +00:00
var rekeyStatus map[string]interface{}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &rekeyStatus)
2015-05-28 21:28:50 +00:00
var actual map[string]interface{}
2017-01-17 20:43:10 +00:00
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)),
}
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)
}
2015-05-28 21:28:50 +00:00
}
2017-01-17 20:43:10 +00:00
retKeys := actual["keys"].([]interface{})
if len(retKeys) != 5 {
t.Fatalf("bad: %#v", retKeys)
2015-05-28 21:28:50 +00:00
}
keysB64 := actual["keys_base64"].([]interface{})
if len(keysB64) != 5 {
t.Fatalf("bad: %#v", keysB64)
}
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,
})
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,
})
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)
}