2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-23 18:53:31 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2016-06-09 15:34:25 +00:00
|
|
|
"io/ioutil"
|
2015-04-23 18:53:31 +00:00
|
|
|
"net/http"
|
2016-03-11 05:41:25 +00:00
|
|
|
"net/url"
|
2015-04-23 18:53:31 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2015-04-23 18:53:31 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSysHealth_get(t *testing.T) {
|
2016-08-18 16:10:23 +00:00
|
|
|
core := vault.TestCore(t)
|
2015-04-23 18:53:31 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(addr + "/v1/sys/health")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
|
|
|
|
"initialized": false,
|
|
|
|
"sealed": true,
|
|
|
|
"standby": true,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
testResponseStatus(t, resp, 501)
|
2015-04-23 18:53:31 +00:00
|
|
|
testResponseBody(t, resp, &actual)
|
2016-07-12 20:28:27 +00:00
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
2016-07-22 22:28:16 +00:00
|
|
|
expected["version"] = actual["version"]
|
2016-07-26 20:53:59 +00:00
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2015-04-23 18:53:31 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-07-07 21:44:14 +00:00
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
2016-03-11 05:41:25 +00:00
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
keys, _ := vault.TestCoreInit(t, core)
|
2016-03-11 05:41:25 +00:00
|
|
|
resp, err = http.Get(addr + "/v1/sys/health")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
|
|
|
|
"initialized": true,
|
|
|
|
"sealed": true,
|
|
|
|
"standby": true,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2016-08-18 16:10:23 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 503)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
|
|
|
expected["version"] = actual["version"]
|
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2016-08-18 16:10:23 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := vault.TestCoreUnseal(core, vault.TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
}
|
|
|
|
resp, err = http.Get(addr + "/v1/sys/health")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationPerformanceDisabled.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationDRDisabled.GetDRString(),
|
|
|
|
"initialized": true,
|
|
|
|
"sealed": false,
|
|
|
|
"standby": false,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
testResponseStatus(t, resp, 200)
|
2016-03-11 05:41:25 +00:00
|
|
|
testResponseBody(t, resp, &actual)
|
2016-07-12 20:28:27 +00:00
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
2016-07-22 22:28:16 +00:00
|
|
|
expected["version"] = actual["version"]
|
2016-07-26 20:53:59 +00:00
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2016-03-11 05:41:25 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-07-07 21:44:14 +00:00
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSysHealth_customcodes(t *testing.T) {
|
2016-08-18 16:10:23 +00:00
|
|
|
core := vault.TestCore(t)
|
2016-03-11 05:41:25 +00:00
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
2016-08-18 16:10:23 +00:00
|
|
|
queryurl, err := url.Parse(addr + "/v1/sys/health?uninitcode=581&sealedcode=523&activecode=202")
|
2016-03-11 05:41:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
resp, err := http.Get(queryurl.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual map[string]interface{}
|
|
|
|
expected := map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
|
|
|
|
"initialized": false,
|
|
|
|
"sealed": true,
|
|
|
|
"standby": true,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
testResponseStatus(t, resp, 581)
|
2016-03-11 05:41:25 +00:00
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
2016-07-12 20:28:27 +00:00
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
2016-07-22 22:28:16 +00:00
|
|
|
expected["version"] = actual["version"]
|
2016-07-26 20:53:59 +00:00
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2016-03-11 05:41:25 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-07-07 21:44:14 +00:00
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
keys, _ := vault.TestCoreInit(t, core)
|
2016-08-18 16:10:23 +00:00
|
|
|
resp, err = http.Get(queryurl.String())
|
2016-03-11 05:41:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationUnknown.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationUnknown.GetDRString(),
|
|
|
|
"initialized": true,
|
|
|
|
"sealed": true,
|
|
|
|
"standby": true,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2016-08-18 16:10:23 +00:00
|
|
|
}
|
|
|
|
testResponseStatus(t, resp, 523)
|
|
|
|
testResponseBody(t, resp, &actual)
|
|
|
|
|
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
|
|
|
expected["version"] = actual["version"]
|
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2016-08-18 16:10:23 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:43:10 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, err := vault.TestCoreUnseal(core, vault.TestKeyCopy(key)); err != nil {
|
|
|
|
t.Fatalf("unseal err: %s", err)
|
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
}
|
2016-03-11 05:41:25 +00:00
|
|
|
resp, err = http.Get(queryurl.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = map[string]interface{}{}
|
|
|
|
expected = map[string]interface{}{
|
2018-01-23 02:44:38 +00:00
|
|
|
"replication_performance_mode": consts.ReplicationPerformanceDisabled.GetPerformanceString(),
|
|
|
|
"replication_dr_mode": consts.ReplicationDRDisabled.GetDRString(),
|
|
|
|
"initialized": true,
|
|
|
|
"sealed": false,
|
|
|
|
"standby": false,
|
2018-08-27 22:13:41 +00:00
|
|
|
"performance_standby": false,
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
2016-08-18 16:10:23 +00:00
|
|
|
testResponseStatus(t, resp, 202)
|
2016-03-11 05:41:25 +00:00
|
|
|
testResponseBody(t, resp, &actual)
|
2016-07-12 20:28:27 +00:00
|
|
|
expected["server_time_utc"] = actual["server_time_utc"]
|
2016-07-22 22:28:16 +00:00
|
|
|
expected["version"] = actual["version"]
|
2016-07-26 20:53:59 +00:00
|
|
|
if actual["cluster_name"] == nil {
|
|
|
|
delete(expected, "cluster_name")
|
|
|
|
} else {
|
|
|
|
expected["cluster_name"] = actual["cluster_name"]
|
|
|
|
}
|
|
|
|
if actual["cluster_id"] == nil {
|
|
|
|
delete(expected, "cluster_id")
|
|
|
|
} else {
|
|
|
|
expected["cluster_id"] = actual["cluster_id"]
|
|
|
|
}
|
2021-05-20 17:32:15 +00:00
|
|
|
delete(actual, "license")
|
2016-03-11 05:41:25 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-07-07 21:44:14 +00:00
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
2016-06-09 15:34:25 +00:00
|
|
|
|
|
|
|
func TestSysHealth_head(t *testing.T) {
|
|
|
|
core, _, _ := vault.TestCoreUnsealed(t)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
|
2016-07-07 21:44:14 +00:00
|
|
|
testData := []struct {
|
2016-06-09 15:34:25 +00:00
|
|
|
uri string
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{"", 200},
|
|
|
|
{"?activecode=503", 503},
|
|
|
|
{"?activecode=notacode", 400},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testData {
|
|
|
|
queryurl, err := url.Parse(addr + "/v1/sys/health" + tt.uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err on %v: %s", queryurl, err)
|
|
|
|
}
|
|
|
|
resp, err := http.Head(queryurl.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err on %v: %s", queryurl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != tt.code {
|
|
|
|
t.Fatalf("HEAD %v expected code %d, got %d.", queryurl, tt.code, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err on %v: %s", queryurl, err)
|
|
|
|
}
|
|
|
|
if len(data) > 0 {
|
|
|
|
t.Fatalf("HEAD %v expected no body, received \"%v\".", queryurl, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|