2019-10-04 07:29:51 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2019-10-08 12:55:25 +00:00
|
|
|
"fmt"
|
2019-10-04 07:29:51 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/metricsutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleMetricsUnauthenticated(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &logical.Request{Headers: r.Header}
|
2019-10-08 12:55:25 +00:00
|
|
|
|
2020-02-07 08:30:25 +00:00
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:55:25 +00:00
|
|
|
// Parse form
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:29:51 +00:00
|
|
|
format := r.Form.Get("format")
|
|
|
|
if format == "" {
|
|
|
|
format = metricsutil.FormatFromRequest(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define response
|
2019-10-08 12:55:25 +00:00
|
|
|
resp := core.MetricsHelper().ResponseForFormat(format)
|
2019-10-04 07:29:51 +00:00
|
|
|
|
|
|
|
// Manually extract the logical response and send back the information
|
|
|
|
w.WriteHeader(resp.Data[logical.HTTPStatusCode].(int))
|
|
|
|
w.Header().Set("Content-Type", resp.Data[logical.HTTPContentType].(string))
|
2019-10-08 12:55:25 +00:00
|
|
|
switch v := resp.Data[logical.HTTPRawBody].(type) {
|
|
|
|
case string:
|
|
|
|
w.Write([]byte(v))
|
|
|
|
case []byte:
|
|
|
|
w.Write(v)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusInternalServerError, fmt.Errorf("wrong response returned"))
|
|
|
|
}
|
2019-10-04 07:29:51 +00:00
|
|
|
})
|
|
|
|
}
|