2015-04-03 05:21:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2022-03-24 19:19:14 +00:00
|
|
|
"errors"
|
2015-04-03 05:21:33 +00:00
|
|
|
"net/http"
|
2022-03-24 19:19:14 +00:00
|
|
|
"strings"
|
2015-04-03 05:21:33 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-04-03 05:21:33 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2016-12-15 22:53:07 +00:00
|
|
|
func wrapHelpHandler(h http.Handler, core *vault.Core) http.Handler {
|
2017-05-04 20:58:50 +00:00
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
// If the help parameter is not blank, then show the help. We request
|
|
|
|
// forward because standby nodes do not have mounts and other state.
|
2016-08-15 13:42:42 +00:00
|
|
|
if v := req.URL.Query().Get("help"); v != "" || req.Method == "HELP" {
|
2017-05-04 20:58:50 +00:00
|
|
|
handleRequestForwarding(core,
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handleHelp(core, w, r)
|
|
|
|
})).ServeHTTP(writer, req)
|
2015-04-03 05:21:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:58:50 +00:00
|
|
|
h.ServeHTTP(writer, req)
|
2015-04-03 05:21:33 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
func handleHelp(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
ns, err := namespace.FromContext(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, nil)
|
2015-04-03 05:21:33 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-24 19:19:14 +00:00
|
|
|
if !strings.HasPrefix(r.URL.Path, "/v1/") {
|
|
|
|
respondError(w, http.StatusNotFound, errors.New("Missing /v1/ prefix in path. Use vault path-help command to retrieve API help for paths"))
|
|
|
|
return
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
path := ns.TrimmedPath(r.URL.Path[len("/v1/"):])
|
2015-04-03 05:21:33 +00:00
|
|
|
|
2021-06-11 17:18:16 +00:00
|
|
|
req := &logical.Request{
|
2015-06-19 00:17:18 +00:00
|
|
|
Operation: logical.HelpOperation,
|
|
|
|
Path: path,
|
2018-09-18 03:03:00 +00:00
|
|
|
Connection: getConnection(r),
|
|
|
|
}
|
2021-06-11 17:18:16 +00:00
|
|
|
requestAuth(r, req)
|
2016-10-29 21:01:49 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err := core.HandleRequest(r.Context(), req)
|
2015-04-03 05:21:33 +00:00
|
|
|
if err != nil {
|
2018-09-18 03:03:00 +00:00
|
|
|
respondErrorCommon(w, req, resp, err)
|
2015-04-03 05:21:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, resp.Data)
|
|
|
|
}
|