2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-01-09 02:21:02 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-02-06 16:56:37 +00:00
|
|
|
"context"
|
2016-01-09 02:21:02 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2016-07-06 16:25:40 +00:00
|
|
|
"encoding/json"
|
2017-01-17 20:43:10 +00:00
|
|
|
"fmt"
|
2020-02-06 16:56:37 +00:00
|
|
|
"net"
|
2016-01-09 02:21:02 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-15 16:56:24 +00:00
|
|
|
"github.com/go-test/deep"
|
2023-02-01 13:33:16 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2020-02-06 16:56:37 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2016-01-09 02:21:02 +00:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
2023-02-01 13:33:16 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
|
2021-12-01 13:05:49 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/xor"
|
2020-02-06 16:56:37 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-01-09 02:21:02 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2022-02-17 19:43:07 +00:00
|
|
|
var tokenLength string = fmt.Sprintf("%d", vault.TokenLength+vault.TokenPrefixLength)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRootAttempt_Status(t *testing.T) {
|
2016-01-09 02:21:02 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp, err := http.Get(addr + "/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": false,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-01-09 02:21:02 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-01-09 02:21:02 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "",
|
2016-02-12 20:35:26 +00:00
|
|
|
"nonce": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2018-10-23 06:34:02 +00:00
|
|
|
expected["otp"] = actual["otp"]
|
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRootAttempt_Setup_OTP(t *testing.T) {
|
2016-01-09 02:21:02 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", nil)
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-01-09 02:21:02 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-01-09 02:21:02 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-02-12 20:35:26 +00:00
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
expected["nonce"] = actual["nonce"]
|
2018-10-23 06:34:02 +00:00
|
|
|
expected["otp"] = actual["otp"]
|
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
2016-02-12 19:24:36 +00:00
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/generate-root/attempt")
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"started": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-02-12 19:24:36 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-02-12 19:24:36 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "",
|
2018-09-18 03:03:00 +00:00
|
|
|
"otp": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-02-12 19:24:36 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-02-12 20:35:26 +00:00
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
2016-02-12 19:24:36 +00:00
|
|
|
expected["nonce"] = actual["nonce"]
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRootAttempt_Setup_PGP(t *testing.T) {
|
2016-01-09 02:21:02 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", map[string]interface{}{
|
2016-01-09 02:21:02 +00:00
|
|
|
"pgp_key": pgpkeys.TestPubKey1,
|
|
|
|
})
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-01-09 02:21:02 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-01-09 02:21:02 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "816938b8a29146fbe245dd29e7cbaf8e011db793",
|
2018-09-18 03:03:00 +00:00
|
|
|
"otp": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-02-12 20:35:26 +00:00
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
expected["nonce"] = actual["nonce"]
|
2018-10-23 06:34:02 +00:00
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRootAttempt_Cancel(t *testing.T) {
|
2016-01-09 02:21:02 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", nil)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-02-12 19:24:36 +00:00
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"started": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-02-12 19:24:36 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-02-12 19:24:36 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-02-12 19:24:36 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-02-12 20:35:26 +00:00
|
|
|
if actual["nonce"].(string) == "" {
|
|
|
|
t.Fatalf("nonce was empty")
|
|
|
|
}
|
2016-02-12 19:24:36 +00:00
|
|
|
expected["nonce"] = actual["nonce"]
|
2018-10-23 06:34:02 +00:00
|
|
|
expected["otp"] = actual["otp"]
|
|
|
|
if diff := deep.Equal(actual, expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2016-02-12 19:24:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp = testHttpDelete(t, token, addr+"/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
testResponseStatus(t, resp, 204)
|
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
resp, err := http.Get(addr + "/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:24:36 +00:00
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2016-01-09 02:21:02 +00:00
|
|
|
"started": false,
|
2016-07-06 16:25:40 +00:00
|
|
|
"progress": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
"required": json.Number("3"),
|
2016-01-09 02:21:02 +00:00
|
|
|
"complete": false,
|
2017-11-13 20:44:26 +00:00
|
|
|
"encoded_token": "",
|
2016-01-09 02:21:02 +00:00
|
|
|
"encoded_root_token": "",
|
|
|
|
"pgp_fingerprint": "",
|
2016-02-12 20:35:26 +00:00
|
|
|
"nonce": "",
|
2018-09-18 03:03:00 +00:00
|
|
|
"otp": "",
|
2022-02-17 19:43:07 +00:00
|
|
|
"otp_length": json.Number(tokenLength),
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:56:37 +00:00
|
|
|
func enableNoopAudit(t *testing.T, token string, core *vault.Core) {
|
|
|
|
t.Helper()
|
|
|
|
auditReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
ClientToken: token,
|
|
|
|
Path: "sys/audit/noop",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": "noop",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := core.HandleRequest(namespace.RootContext(context.Background()), auditReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCoreUnsealedWithAudit(t *testing.T, records **[][]byte) (*vault.Core, [][]byte, string) {
|
|
|
|
conf := &vault.CoreConfig{
|
2023-02-01 13:33:16 +00:00
|
|
|
BuiltinRegistry: corehelpers.NewMockBuiltinRegistry(),
|
|
|
|
AuditBackends: map[string]audit.Factory{
|
|
|
|
"noop": corehelpers.NoopAuditFactory(records),
|
|
|
|
},
|
2020-02-06 16:56:37 +00:00
|
|
|
}
|
|
|
|
core, keys, token := vault.TestCoreUnsealedWithConfig(t, conf)
|
|
|
|
return core, keys, token
|
|
|
|
}
|
|
|
|
|
|
|
|
func testServerWithAudit(t *testing.T, records **[][]byte) (net.Listener, string, string, [][]byte) {
|
|
|
|
core, keys, token := testCoreUnsealedWithAudit(t, records)
|
2016-01-09 02:21:02 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
TestServerAuth(t, addr, token)
|
2020-02-06 16:56:37 +00:00
|
|
|
enableNoopAudit(t, token, core)
|
|
|
|
return ln, addr, token, keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSysGenerateRoot_badKey(t *testing.T) {
|
|
|
|
var records *[][]byte
|
|
|
|
ln, addr, token, _ := testServerWithAudit(t, &records)
|
|
|
|
defer ln.Close()
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/update", map[string]interface{}{
|
2016-01-09 02:21:02 +00:00
|
|
|
"key": "0123",
|
|
|
|
})
|
|
|
|
testResponseStatus(t, resp, 400)
|
2020-02-06 16:56:37 +00:00
|
|
|
|
|
|
|
if len(*records) < 3 {
|
|
|
|
// One record for enabling the noop audit device, two for generate root attempt
|
|
|
|
t.Fatalf("expected at least 3 audit records, got %d", len(*records))
|
|
|
|
}
|
|
|
|
t.Log(string((*records)[2]))
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRoot_ReAttemptUpdate(t *testing.T) {
|
2016-01-09 02:21:02 +00:00
|
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", nil)
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp = testHttpDelete(t, token, addr+"/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
testResponseStatus(t, resp, 204)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp = testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", map[string]interface{}{
|
2016-01-09 02:21:02 +00:00
|
|
|
"pgp_key": pgpkeys.TestPubKey1,
|
|
|
|
})
|
|
|
|
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRoot_Update_OTP(t *testing.T) {
|
2020-02-06 16:56:37 +00:00
|
|
|
var records *[][]byte
|
|
|
|
ln, addr, token, keys := testServerWithAudit(t, &records)
|
2016-01-09 02:21:02 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", map[string]interface{}{})
|
2016-01-09 02:21:02 +00:00
|
|
|
var rootGenerationStatus map[string]interface{}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &rootGenerationStatus)
|
2018-09-18 03:03:00 +00:00
|
|
|
otp := rootGenerationStatus["otp"].(string)
|
2016-01-09 02:21:02 +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/generate-root/update", map[string]interface{}{
|
|
|
|
"nonce": rootGenerationStatus["nonce"].(string),
|
|
|
|
"key": hex.EncodeToString(key),
|
|
|
|
})
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"complete": false,
|
|
|
|
"nonce": rootGenerationStatus["nonce"].(string),
|
|
|
|
"progress": json.Number(fmt.Sprintf("%d", i+1)),
|
|
|
|
"required": json.Number(fmt.Sprintf("%d", len(keys))),
|
|
|
|
"started": true,
|
|
|
|
"pgp_fingerprint": "",
|
2018-09-18 03:03:00 +00:00
|
|
|
"otp": "",
|
|
|
|
"otp_length": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
if i+1 == len(keys) {
|
|
|
|
expected["complete"] = true
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 20:44:26 +00:00
|
|
|
if actual["encoded_token"] == nil || actual["encoded_token"] == "" {
|
|
|
|
t.Fatalf("no encoded token found in response")
|
|
|
|
}
|
|
|
|
if actual["encoded_root_token"] == nil || actual["encoded_root-token"] == "" {
|
2016-01-09 02:21:02 +00:00
|
|
|
t.Fatalf("no encoded root token found in response")
|
|
|
|
}
|
2017-11-13 20:44:26 +00:00
|
|
|
expected["encoded_token"] = actual["encoded_token"]
|
2016-01-09 02:21:02 +00:00
|
|
|
expected["encoded_root_token"] = actual["encoded_root_token"]
|
2018-09-18 03:03:00 +00:00
|
|
|
expected["encoded_token"] = actual["encoded_token"]
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
tokenBytes, err := base64.RawStdEncoding.DecodeString(expected["encoded_token"].(string))
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
|
|
|
tokenBytes, err = xor.XORBytes(tokenBytes, []byte(otp))
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
newRootToken := string(tokenBytes)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2016-05-11 20:51:18 +00:00
|
|
|
"id": newRootToken,
|
|
|
|
"display_name": "root",
|
|
|
|
"meta": interface{}(nil),
|
2016-07-06 16:25:40 +00:00
|
|
|
"num_uses": json.Number("0"),
|
2016-05-11 20:51:18 +00:00
|
|
|
"policies": []interface{}{"root"},
|
|
|
|
"orphan": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"creation_ttl": json.Number("0"),
|
|
|
|
"ttl": json.Number("0"),
|
2016-05-11 20:51:18 +00:00
|
|
|
"path": "auth/token/root",
|
2016-07-06 16:25:40 +00:00
|
|
|
"explicit_max_ttl": json.Number("0"),
|
2017-05-04 02:03:42 +00:00
|
|
|
"expire_time": nil,
|
2017-10-11 17:21:20 +00:00
|
|
|
"entity_id": "",
|
2018-10-15 16:56:24 +00:00
|
|
|
"type": "service",
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp = testHttpGet(t, newRootToken, addr+"/v1/auth/token/lookup-self")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
|
|
|
expected["creation_time"] = actual["data"].(map[string]interface{})["creation_time"]
|
2016-03-09 18:45:36 +00:00
|
|
|
expected["accessor"] = actual["data"].(map[string]interface{})["accessor"]
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual["data"], expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual["data"])
|
|
|
|
}
|
2020-02-06 16:56:37 +00:00
|
|
|
|
|
|
|
for _, r := range *records {
|
|
|
|
t.Log(string(r))
|
|
|
|
}
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
func TestSysGenerateRoot_Update_PGP(t *testing.T) {
|
2017-01-17 20:43:10 +00:00
|
|
|
core, keys, token := vault.TestCoreUnsealed(t)
|
2016-01-09 02:21:02 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
2016-01-15 15:55:35 +00:00
|
|
|
resp := testHttpPut(t, token, addr+"/v1/sys/generate-root/attempt", map[string]interface{}{
|
2016-01-09 02:21:02 +00:00
|
|
|
"pgp_key": pgpkeys.TestPubKey1,
|
|
|
|
})
|
2016-02-12 19:24:36 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
// We need to get the nonce first before we update
|
2016-01-15 15:55:35 +00:00
|
|
|
resp, err := http.Get(addr + "/v1/sys/generate-root/attempt")
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
var rootGenerationStatus map[string]interface{}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &rootGenerationStatus)
|
|
|
|
|
|
|
|
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/generate-root/update", map[string]interface{}{
|
|
|
|
"nonce": rootGenerationStatus["nonce"].(string),
|
|
|
|
"key": hex.EncodeToString(key),
|
|
|
|
})
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
|
|
|
"complete": false,
|
|
|
|
"nonce": rootGenerationStatus["nonce"].(string),
|
|
|
|
"progress": json.Number(fmt.Sprintf("%d", i+1)),
|
|
|
|
"required": json.Number(fmt.Sprintf("%d", len(keys))),
|
|
|
|
"started": true,
|
|
|
|
"pgp_fingerprint": "816938b8a29146fbe245dd29e7cbaf8e011db793",
|
2018-09-18 03:03:00 +00:00
|
|
|
"otp": "",
|
|
|
|
"otp_length": json.Number("0"),
|
2017-01-17 20:43:10 +00:00
|
|
|
}
|
|
|
|
if i+1 == len(keys) {
|
|
|
|
expected["complete"] = true
|
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 20:44:26 +00:00
|
|
|
if actual["encoded_token"] == nil || actual["encoded_token"] == "" {
|
|
|
|
t.Fatalf("no encoded token found in response")
|
|
|
|
}
|
|
|
|
if actual["encoded_root_token"] == nil || actual["encoded_root-token"] == "" {
|
2016-01-09 02:21:02 +00:00
|
|
|
t.Fatalf("no encoded root token found in response")
|
|
|
|
}
|
2017-11-13 20:44:26 +00:00
|
|
|
expected["encoded_token"] = actual["encoded_token"]
|
2016-01-09 02:21:02 +00:00
|
|
|
expected["encoded_root_token"] = actual["encoded_root_token"]
|
2018-09-18 03:03:00 +00:00
|
|
|
expected["encoded_token"] = actual["encoded_token"]
|
2016-01-09 02:21:02 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("\nexpected: %#v\nactual: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
decodedTokenBuf, err := pgpkeys.DecryptBytes(actual["encoded_token"].(string), pgpkeys.TestPrivKey1)
|
2016-01-09 02:21:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if decodedTokenBuf == nil {
|
|
|
|
t.Fatal("decoded root token buffer is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
newRootToken := decodedTokenBuf.String()
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2016-05-11 20:51:18 +00:00
|
|
|
"id": newRootToken,
|
|
|
|
"display_name": "root",
|
|
|
|
"meta": interface{}(nil),
|
2016-07-06 16:25:40 +00:00
|
|
|
"num_uses": json.Number("0"),
|
2016-05-11 20:51:18 +00:00
|
|
|
"policies": []interface{}{"root"},
|
|
|
|
"orphan": true,
|
2016-07-06 16:25:40 +00:00
|
|
|
"creation_ttl": json.Number("0"),
|
|
|
|
"ttl": json.Number("0"),
|
2016-05-11 20:51:18 +00:00
|
|
|
"path": "auth/token/root",
|
2016-07-06 16:25:40 +00:00
|
|
|
"explicit_max_ttl": json.Number("0"),
|
2017-05-04 02:03:42 +00:00
|
|
|
"expire_time": nil,
|
2017-10-11 17:21:20 +00:00
|
|
|
"entity_id": "",
|
2018-10-15 16:56:24 +00:00
|
|
|
"type": "service",
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp = testHttpGet(t, newRootToken, addr+"/v1/auth/token/lookup-self")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
|
|
|
expected["creation_time"] = actual["data"].(map[string]interface{})["creation_time"]
|
2016-03-09 18:45:36 +00:00
|
|
|
expected["accessor"] = actual["data"].(map[string]interface{})["accessor"]
|
2016-01-09 02:21:02 +00:00
|
|
|
|
2018-10-15 16:56:24 +00:00
|
|
|
if diff := deep.Equal(actual["data"], expected); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2016-01-09 02:21:02 +00:00
|
|
|
}
|
|
|
|
}
|