2015-03-12 06:05:16 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-04-01 04:24:20 +00:00
|
|
|
"fmt"
|
2015-08-26 14:03:33 +00:00
|
|
|
"io"
|
2015-03-12 06:05:16 +00:00
|
|
|
"net/http"
|
2015-04-19 20:18:09 +00:00
|
|
|
"net/url"
|
2015-04-03 05:21:33 +00:00
|
|
|
"strings"
|
2015-03-12 06:05:16 +00:00
|
|
|
|
2016-03-08 23:27:03 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-07-11 18:19:35 +00:00
|
|
|
"github.com/hashicorp/vault/helper/duration"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-03-29 23:14:54 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-12 06:05:16 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2016-05-02 02:39:45 +00:00
|
|
|
const (
|
|
|
|
// AuthHeaderName is the name of the header containing the token.
|
|
|
|
AuthHeaderName = "X-Vault-Token"
|
|
|
|
|
|
|
|
// WrapHeaderName is the name of the header containing a directive to wrap the
|
|
|
|
// response.
|
2016-05-02 04:08:07 +00:00
|
|
|
WrapTTLHeaderName = "X-Vault-Wrap-TTL"
|
2016-05-02 02:39:45 +00:00
|
|
|
)
|
2015-05-11 17:56:41 +00:00
|
|
|
|
2015-03-12 06:05:16 +00:00
|
|
|
// Handler returns an http.Handler for the API. This can be used on
|
|
|
|
// its own to mount the Vault API within another web server.
|
|
|
|
func Handler(core *vault.Core) http.Handler {
|
2015-04-03 05:21:33 +00:00
|
|
|
// Create the muxer to handle the actual endpoints
|
2015-03-12 06:05:16 +00:00
|
|
|
mux := http.NewServeMux()
|
2015-03-12 19:37:41 +00:00
|
|
|
mux.Handle("/v1/sys/init", handleSysInit(core))
|
2015-03-12 17:47:31 +00:00
|
|
|
mux.Handle("/v1/sys/seal-status", handleSysSealStatus(core))
|
|
|
|
mux.Handle("/v1/sys/seal", handleSysSeal(core))
|
2016-02-27 00:43:55 +00:00
|
|
|
mux.Handle("/v1/sys/step-down", handleSysStepDown(core))
|
2015-03-12 17:47:31 +00:00
|
|
|
mux.Handle("/v1/sys/unseal", handleSysUnseal(core))
|
2016-08-08 22:00:44 +00:00
|
|
|
mux.Handle("/v1/sys/renew", handleLogical(core, false, nil))
|
2016-08-09 00:01:08 +00:00
|
|
|
mux.Handle("/v1/sys/renew/", handleLogical(core, false, nil))
|
2015-04-20 18:59:24 +00:00
|
|
|
mux.Handle("/v1/sys/leader", handleSysLeader(core))
|
2015-04-23 18:53:31 +00:00
|
|
|
mux.Handle("/v1/sys/health", handleSysHealth(core))
|
2016-01-15 15:55:35 +00:00
|
|
|
mux.Handle("/v1/sys/generate-root/attempt", handleSysGenerateRootAttempt(core))
|
|
|
|
mux.Handle("/v1/sys/generate-root/update", handleSysGenerateRootUpdate(core))
|
2016-04-04 14:44:22 +00:00
|
|
|
mux.Handle("/v1/sys/rekey/init", handleSysRekeyInit(core, false))
|
|
|
|
mux.Handle("/v1/sys/rekey/update", handleSysRekeyUpdate(core, false))
|
|
|
|
mux.Handle("/v1/sys/rekey-recovery-key/init", handleSysRekeyInit(core, true))
|
|
|
|
mux.Handle("/v1/sys/rekey-recovery-key/update", handleSysRekeyUpdate(core, true))
|
2016-03-18 03:01:28 +00:00
|
|
|
mux.Handle("/v1/sys/capabilities-self", handleLogical(core, true, sysCapabilitiesSelfCallback))
|
2016-03-18 02:29:53 +00:00
|
|
|
mux.Handle("/v1/sys/", handleLogical(core, true, nil))
|
|
|
|
mux.Handle("/v1/", handleLogical(core, false, nil))
|
2015-04-03 05:21:33 +00:00
|
|
|
|
|
|
|
// Wrap the handler in another handler to trigger all help paths.
|
|
|
|
handler := handleHelpHandler(mux, core)
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2016-03-18 03:01:28 +00:00
|
|
|
// ClientToken is required in the handler of sys/capabilities-self endpoint in
|
|
|
|
// system backend. But the ClientToken gets obfuscated before the request gets
|
|
|
|
// forwarded to any logical backend. So, setting the ClientToken in the data
|
|
|
|
// field for this request.
|
|
|
|
func sysCapabilitiesSelfCallback(req *logical.Request) error {
|
|
|
|
if req == nil || req.Data == nil {
|
|
|
|
return fmt.Errorf("invalid request")
|
|
|
|
}
|
2016-03-18 02:52:03 +00:00
|
|
|
req.Data["token"] = req.ClientToken
|
2016-03-18 02:29:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-03 05:21:33 +00:00
|
|
|
// stripPrefix is a helper to strip a prefix from the path. It will
|
|
|
|
// return false from the second return value if it the prefix doesn't exist.
|
|
|
|
func stripPrefix(prefix, path string) (string, bool) {
|
|
|
|
if !strings.HasPrefix(path, prefix) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path[len(prefix):]
|
|
|
|
if path == "" {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, true
|
2015-03-12 06:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseRequest(r *http.Request, out interface{}) error {
|
2016-07-06 16:25:40 +00:00
|
|
|
err := jsonutil.DecodeJSONFromReader(r.Body, out)
|
2015-08-26 14:03:33 +00:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return fmt.Errorf("Failed to parse JSON input: %s", err)
|
|
|
|
}
|
|
|
|
return err
|
2015-03-12 06:05:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:19:03 +00:00
|
|
|
// request is a helper to perform a request and properly exit in the
|
|
|
|
// case of an error.
|
2015-04-19 21:36:50 +00:00
|
|
|
func request(core *vault.Core, w http.ResponseWriter, rawReq *http.Request, r *logical.Request) (*logical.Response, bool) {
|
2015-04-08 18:19:03 +00:00
|
|
|
resp, err := core.HandleRequest(r)
|
2016-05-16 20:11:33 +00:00
|
|
|
if errwrap.Contains(err, vault.ErrStandby.Error()) {
|
2015-04-19 21:36:50 +00:00
|
|
|
respondStandby(core, w, rawReq.URL)
|
2015-04-19 20:18:09 +00:00
|
|
|
return resp, false
|
|
|
|
}
|
2016-06-22 21:47:05 +00:00
|
|
|
if respondErrorCommon(w, resp, err) {
|
2015-04-08 18:19:03 +00:00
|
|
|
return resp, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, true
|
|
|
|
}
|
|
|
|
|
2015-04-19 20:18:09 +00:00
|
|
|
// respondStandby is used to trigger a redirect in the case that this Vault is currently a hot standby
|
|
|
|
func respondStandby(core *vault.Core, w http.ResponseWriter, reqURL *url.URL) {
|
|
|
|
// Request the leader address
|
|
|
|
_, advertise, err := core.Leader()
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no leader, generate a 503 error
|
|
|
|
if advertise == "" {
|
|
|
|
err = fmt.Errorf("no active Vault instance found")
|
|
|
|
respondError(w, http.StatusServiceUnavailable, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the advertise location
|
|
|
|
advertiseURL, err := url.Parse(advertise)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a redirect URL
|
|
|
|
redirectURL := url.URL{
|
|
|
|
Scheme: advertiseURL.Scheme,
|
|
|
|
Host: advertiseURL.Host,
|
|
|
|
Path: reqURL.Path,
|
|
|
|
RawQuery: reqURL.RawQuery,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is a scheme, default to https
|
|
|
|
if redirectURL.Scheme == "" {
|
|
|
|
redirectURL.Scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an address, redirect! We use a 307 code
|
|
|
|
// because we don't actually know if its permanent and
|
|
|
|
// the request method should be preserved.
|
|
|
|
w.Header().Set("Location", redirectURL.String())
|
|
|
|
w.WriteHeader(307)
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:14:54 +00:00
|
|
|
// requestAuth adds the token to the logical.Request if it exists.
|
|
|
|
func requestAuth(r *http.Request, req *logical.Request) *logical.Request {
|
2015-05-11 17:56:41 +00:00
|
|
|
// Attach the header value if we have it
|
|
|
|
if v := r.Header.Get(AuthHeaderName); v != "" {
|
|
|
|
req.ClientToken = v
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:14:54 +00:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2016-05-02 04:08:07 +00:00
|
|
|
// requestWrapTTL adds the WrapTTL value to the logical.Request if it
|
2016-05-02 02:39:45 +00:00
|
|
|
// exists.
|
2016-05-02 04:08:07 +00:00
|
|
|
func requestWrapTTL(r *http.Request, req *logical.Request) (*logical.Request, error) {
|
2016-05-02 02:39:45 +00:00
|
|
|
// First try for the header value
|
2016-05-02 04:08:07 +00:00
|
|
|
wrapTTL := r.Header.Get(WrapTTLHeaderName)
|
|
|
|
if wrapTTL == "" {
|
2016-05-02 02:39:45 +00:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it has an allowed suffix parse as a duration string
|
2016-07-11 18:19:35 +00:00
|
|
|
dur, err := duration.ParseDurationSecond(wrapTTL)
|
|
|
|
if err != nil {
|
|
|
|
return req, err
|
2016-05-02 02:39:45 +00:00
|
|
|
}
|
2016-07-11 18:19:35 +00:00
|
|
|
if int64(dur) < 0 {
|
2016-06-07 19:00:35 +00:00
|
|
|
return req, fmt.Errorf("requested wrap ttl cannot be negative")
|
|
|
|
}
|
2016-07-11 18:19:35 +00:00
|
|
|
req.WrapTTL = dur
|
2016-05-02 02:39:45 +00:00
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2015-03-12 06:05:16 +00:00
|
|
|
func respondError(w http.ResponseWriter, status int, err error) {
|
2015-05-19 07:59:19 +00:00
|
|
|
// Adjust status code when sealed
|
2016-05-16 20:11:33 +00:00
|
|
|
if errwrap.Contains(err, vault.ErrSealed.Error()) {
|
2015-05-19 07:59:19 +00:00
|
|
|
status = http.StatusServiceUnavailable
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:27:25 +00:00
|
|
|
// Allow HTTPCoded error passthrough to specify a code
|
|
|
|
if t, ok := err.(logical.HTTPCodedError); ok {
|
|
|
|
status = t.Code()
|
|
|
|
}
|
|
|
|
|
2015-03-12 06:05:16 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
|
|
|
|
resp := &ErrorResponse{Errors: make([]string, 0, 1)}
|
|
|
|
if err != nil {
|
|
|
|
resp.Errors = append(resp.Errors, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.Encode(resp)
|
|
|
|
}
|
|
|
|
|
2016-06-22 21:47:05 +00:00
|
|
|
func respondErrorCommon(w http.ResponseWriter, resp *logical.Response, err error) bool {
|
|
|
|
// If there are no errors return
|
|
|
|
if err == nil && (resp == nil || !resp.IsError()) {
|
2015-04-01 04:29:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-22 21:47:05 +00:00
|
|
|
// Start out with internal server error since in most of these cases there
|
|
|
|
// won't be a response so this won't be overridden
|
|
|
|
statusCode := http.StatusInternalServerError
|
|
|
|
// If we actually have a response, start out with bad request
|
|
|
|
if resp != nil {
|
|
|
|
statusCode = http.StatusBadRequest
|
|
|
|
}
|
2016-05-16 20:11:33 +00:00
|
|
|
|
2016-06-22 21:47:05 +00:00
|
|
|
// Now, check the error itself; if it has a specific logical error, set the
|
|
|
|
// appropriate code
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errwrap.ContainsType(err, new(vault.StatusBadRequest)):
|
|
|
|
statusCode = http.StatusBadRequest
|
|
|
|
case errwrap.Contains(err, logical.ErrPermissionDenied.Error()):
|
|
|
|
statusCode = http.StatusForbidden
|
|
|
|
case errwrap.Contains(err, logical.ErrUnsupportedOperation.Error()):
|
|
|
|
statusCode = http.StatusMethodNotAllowed
|
|
|
|
case errwrap.Contains(err, logical.ErrUnsupportedPath.Error()):
|
|
|
|
statusCode = http.StatusNotFound
|
|
|
|
case errwrap.Contains(err, logical.ErrInvalidRequest.Error()):
|
|
|
|
statusCode = http.StatusBadRequest
|
2015-06-19 20:56:44 +00:00
|
|
|
}
|
2016-06-22 21:47:05 +00:00
|
|
|
}
|
2015-06-19 20:56:44 +00:00
|
|
|
|
2016-08-08 22:00:44 +00:00
|
|
|
if resp != nil && resp.IsError() {
|
2016-06-22 21:47:05 +00:00
|
|
|
err = fmt.Errorf("%s", resp.Data["error"].(string))
|
2015-04-01 04:29:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 21:47:05 +00:00
|
|
|
respondError(w, statusCode, err)
|
|
|
|
return true
|
2015-04-01 04:24:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 06:05:16 +00:00
|
|
|
func respondOk(w http.ResponseWriter, body interface{}) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.Encode(body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Errors []string `json:"errors"`
|
|
|
|
}
|