2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
|
|
|
|
2020-10-13 23:38:21 +00:00
|
|
|
"github.com/hashicorp/vault/helper/metricsutil"
|
2019-10-04 07:29:51 +00:00
|
|
|
"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)
|
2020-04-21 22:30:36 +00:00
|
|
|
return
|
2020-02-07 08:30:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-10-13 15:06:33 +00:00
|
|
|
status := resp.Data[logical.HTTPStatusCode].(int)
|
2019-10-04 07:29:51 +00:00
|
|
|
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:
|
2021-10-13 15:06:33 +00:00
|
|
|
w.WriteHeader(status)
|
2019-10-08 12:55:25 +00:00
|
|
|
w.Write([]byte(v))
|
|
|
|
case []byte:
|
2021-10-13 15:06:33 +00:00
|
|
|
w.WriteHeader(status)
|
2019-10-08 12:55:25 +00:00
|
|
|
w.Write(v)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusInternalServerError, fmt.Errorf("wrong response returned"))
|
|
|
|
}
|
2019-10-04 07:29:51 +00:00
|
|
|
})
|
|
|
|
}
|