open-vault/http/sys_health.go

203 lines
5.5 KiB
Go
Raw Normal View History

2015-04-23 18:53:31 +00:00
package http
import (
"context"
2015-04-23 18:53:31 +00:00
"encoding/json"
2016-08-10 19:22:12 +00:00
"fmt"
2015-04-23 18:53:31 +00:00
"net/http"
"strconv"
"time"
2015-04-23 18:53:31 +00:00
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/version"
"github.com/hashicorp/vault/vault"
2015-04-23 18:53:31 +00:00
)
func handleSysHealth(core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handleSysHealthGet(core, w, r)
case "HEAD":
handleSysHealthHead(core, w, r)
2015-04-23 18:53:31 +00:00
default:
respondError(w, http.StatusMethodNotAllowed, nil)
}
})
}
func fetchStatusCode(r *http.Request, field string) (int, bool, bool) {
var err error
statusCode := http.StatusOK
if statusCodeStr, statusCodeOk := r.URL.Query()[field]; statusCodeOk {
statusCode, err = strconv.Atoi(statusCodeStr[0])
if err != nil || len(statusCodeStr) < 1 {
return http.StatusBadRequest, false, false
}
return statusCode, true, true
}
return statusCode, false, true
}
2015-04-23 18:53:31 +00:00
func handleSysHealthGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {
code, body, err := getSysHealth(core, r)
if err != nil {
2018-01-20 00:59:58 +00:00
core.Logger().Error("error checking health", "error", err)
respondError(w, http.StatusInternalServerError, nil)
return
}
if body == nil {
respondError(w, code, nil)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
// Generate the response
enc := json.NewEncoder(w)
enc.Encode(body)
}
func handleSysHealthHead(core *vault.Core, w http.ResponseWriter, r *http.Request) {
code, body, err := getSysHealth(core, r)
if err != nil {
code = http.StatusInternalServerError
}
if body != nil {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(code)
}
func getSysHealth(core *vault.Core, r *http.Request) (int, *HealthResponse, error) {
// Check if being a standby is allowed for the purpose of a 200 OK
_, standbyOK := r.URL.Query()["standbyok"]
2018-08-27 22:13:41 +00:00
_, perfStandbyOK := r.URL.Query()["perfstandbyok"]
uninitCode := http.StatusNotImplemented
if code, found, ok := fetchStatusCode(r, "uninitcode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
uninitCode = code
}
sealedCode := http.StatusServiceUnavailable
if code, found, ok := fetchStatusCode(r, "sealedcode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
sealedCode = code
}
standbyCode := http.StatusTooManyRequests // Consul warning code
if code, found, ok := fetchStatusCode(r, "standbycode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
standbyCode = code
}
activeCode := http.StatusOK
if code, found, ok := fetchStatusCode(r, "activecode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
activeCode = code
}
drSecondaryCode := 472 // unofficial 4xx status code
if code, found, ok := fetchStatusCode(r, "drsecondarycode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
drSecondaryCode = code
}
2018-08-27 22:13:41 +00:00
perfStandbyCode := 473 // unofficial 4xx status code
if code, found, ok := fetchStatusCode(r, "performancestandbycode"); !ok {
return http.StatusBadRequest, nil, nil
} else if found {
perfStandbyCode = code
}
ctx := context.Background()
2015-04-23 18:53:31 +00:00
// Check system status
sealed := core.Sealed()
2015-04-23 18:53:31 +00:00
standby, _ := core.Standby()
2018-08-27 22:13:41 +00:00
perfStandby := core.PerfStandby()
var replicationState consts.ReplicationState
if standby {
replicationState = core.ActiveNodeReplicationState()
} else {
replicationState = core.ReplicationState()
}
init, err := core.Initialized(ctx)
2015-04-23 18:53:31 +00:00
if err != nil {
return http.StatusInternalServerError, nil, err
2015-04-23 18:53:31 +00:00
}
// Determine the status code
code := activeCode
2015-04-23 18:53:31 +00:00
switch {
case !init:
code = uninitCode
2015-04-23 18:53:31 +00:00
case sealed:
code = sealedCode
case replicationState.HasState(consts.ReplicationDRSecondary):
code = drSecondaryCode
2018-08-27 22:13:41 +00:00
case !perfStandbyOK && perfStandby:
code = perfStandbyCode
case !standbyOK && standby:
code = standbyCode
2015-04-23 18:53:31 +00:00
}
// Fetch the local cluster name and identifier
var clusterName, clusterID string
2016-07-26 18:05:27 +00:00
if !sealed {
cluster, err := core.Cluster(ctx)
2016-07-26 18:05:27 +00:00
if err != nil {
return http.StatusInternalServerError, nil, err
}
2016-08-10 19:22:12 +00:00
if cluster == nil {
return http.StatusInternalServerError, nil, fmt.Errorf("failed to fetch cluster details")
}
clusterName = cluster.Name
clusterID = cluster.ID
}
2015-04-23 18:53:31 +00:00
// Format the body
body := &HealthResponse{
2018-01-23 02:44:38 +00:00
Initialized: init,
Sealed: sealed,
Standby: standby,
2018-08-27 22:13:41 +00:00
PerformanceStandby: perfStandby,
2018-01-23 02:44:38 +00:00
ReplicationPerformanceMode: replicationState.GetPerformanceString(),
ReplicationDRMode: replicationState.GetDRString(),
ServerTimeUTC: time.Now().UTC().Unix(),
Version: version.GetVersion().VersionNumber(),
ClusterName: clusterName,
ClusterID: clusterID,
2015-04-23 18:53:31 +00:00
}
if init && !sealed && !standby {
body.LastWAL = vault.LastWAL(core)
}
return code, body, nil
2015-04-23 18:53:31 +00:00
}
type HealthResponse struct {
2018-01-23 02:44:38 +00:00
Initialized bool `json:"initialized"`
Sealed bool `json:"sealed"`
Standby bool `json:"standby"`
2018-08-27 22:13:41 +00:00
PerformanceStandby bool `json:"performance_standby"`
2018-01-23 02:44:38 +00:00
ReplicationPerformanceMode string `json:"replication_performance_mode"`
ReplicationDRMode string `json:"replication_dr_mode"`
ServerTimeUTC int64 `json:"server_time_utc"`
Version string `json:"version"`
ClusterName string `json:"cluster_name,omitempty"`
ClusterID string `json:"cluster_id,omitempty"`
LastWAL uint64 `json:"last_wal,omitempty"`
2015-04-23 18:53:31 +00:00
}