2015-04-23 18:53:31 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2016-03-11 05:41:25 +00:00
|
|
|
"strconv"
|
2016-02-22 20:40:03 +00:00
|
|
|
"time"
|
2015-04-23 18:53:31 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/vault"
|
2016-07-22 22:28:16 +00:00
|
|
|
"github.com/hashicorp/vault/version"
|
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)
|
2016-06-09 15:34:25 +00:00
|
|
|
case "HEAD":
|
|
|
|
handleSysHealthHead(core, w, r)
|
2015-04-23 18:53:31 +00:00
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:52:31 +00:00
|
|
|
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) {
|
2016-06-09 15:34:25 +00:00
|
|
|
code, body, err := getSysHealth(core, r)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
respondError(w, code, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("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().Add("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
w.WriteHeader(code)
|
|
|
|
}
|
2016-03-11 05:41:25 +00:00
|
|
|
|
2016-06-09 15:34:25 +00:00
|
|
|
func getSysHealth(core *vault.Core, r *http.Request) (int, *HealthResponse, error) {
|
2015-07-03 00:49:35 +00:00
|
|
|
// Check if being a standby is allowed for the purpose of a 200 OK
|
|
|
|
_, standbyOK := r.URL.Query()["standbyok"]
|
|
|
|
|
2016-03-11 05:41:25 +00:00
|
|
|
// FIXME: Change the sealed code to http.StatusServiceUnavailable at some
|
|
|
|
// point
|
|
|
|
sealedCode := http.StatusInternalServerError
|
2016-03-11 14:52:31 +00:00
|
|
|
if code, found, ok := fetchStatusCode(r, "sealedcode"); !ok {
|
2016-06-09 15:34:25 +00:00
|
|
|
return http.StatusBadRequest, nil, nil
|
2016-03-11 14:52:31 +00:00
|
|
|
} else if found {
|
|
|
|
sealedCode = code
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 14:52:31 +00:00
|
|
|
standbyCode := http.StatusTooManyRequests // Consul warning code
|
|
|
|
if code, found, ok := fetchStatusCode(r, "standbycode"); !ok {
|
2016-06-09 15:34:25 +00:00
|
|
|
return http.StatusBadRequest, nil, nil
|
2016-03-11 14:52:31 +00:00
|
|
|
} else if found {
|
|
|
|
standbyCode = code
|
|
|
|
}
|
2016-03-11 05:41:25 +00:00
|
|
|
|
2016-03-11 14:52:31 +00:00
|
|
|
activeCode := http.StatusOK
|
|
|
|
if code, found, ok := fetchStatusCode(r, "activecode"); !ok {
|
2016-06-09 15:34:25 +00:00
|
|
|
return http.StatusBadRequest, nil, nil
|
2016-03-11 14:52:31 +00:00
|
|
|
} else if found {
|
|
|
|
activeCode = code
|
2016-03-11 05:41:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 18:53:31 +00:00
|
|
|
// Check system status
|
|
|
|
sealed, _ := core.Sealed()
|
|
|
|
standby, _ := core.Standby()
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
2016-06-09 15:34:25 +00:00
|
|
|
return http.StatusInternalServerError, nil, err
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the status code
|
2016-03-11 05:41:25 +00:00
|
|
|
code := activeCode
|
2015-04-23 18:53:31 +00:00
|
|
|
switch {
|
|
|
|
case !init:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
case sealed:
|
2016-03-11 05:41:25 +00:00
|
|
|
code = sealedCode
|
2015-07-03 00:49:35 +00:00
|
|
|
case !standbyOK && standby:
|
2016-03-11 05:41:25 +00:00
|
|
|
code = standbyCode
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 06:25:33 +00:00
|
|
|
// Fetch the local cluster name and identifier
|
2016-07-26 14:01:35 +00:00
|
|
|
var clusterName, clusterID string
|
2016-07-26 18:05:27 +00:00
|
|
|
if !sealed {
|
|
|
|
cluster, err := core.Cluster()
|
2016-07-26 06:25:33 +00:00
|
|
|
|
2016-07-26 18:05:27 +00:00
|
|
|
// Don't set the cluster details in the health status when Vault is sealed
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, nil, err
|
|
|
|
}
|
|
|
|
if cluster != nil {
|
|
|
|
clusterName = cluster.Name
|
|
|
|
clusterID = cluster.ID
|
|
|
|
}
|
2016-07-26 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 18:53:31 +00:00
|
|
|
// Format the body
|
|
|
|
body := &HealthResponse{
|
2016-07-26 14:01:35 +00:00
|
|
|
Initialized: init,
|
|
|
|
Sealed: sealed,
|
|
|
|
Standby: standby,
|
|
|
|
ServerTimeUTC: time.Now().UTC().Unix(),
|
|
|
|
Version: version.GetVersion().String(),
|
|
|
|
ClusterName: clusterName,
|
|
|
|
ClusterID: clusterID,
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
2016-06-09 15:34:25 +00:00
|
|
|
return code, body, nil
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HealthResponse struct {
|
2016-07-26 14:01:35 +00:00
|
|
|
Initialized bool `json:"initialized"`
|
|
|
|
Sealed bool `json:"sealed"`
|
|
|
|
Standby bool `json:"standby"`
|
|
|
|
ServerTimeUTC int64 `json:"server_time_utc"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
ClusterName string `json:"cluster_name"`
|
|
|
|
ClusterID string `json:"cluster_id"`
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|