2015-03-12 06:05:16 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-04-01 04:24:20 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2015-03-29 23:14:54 +00:00
|
|
|
// AuthCookieName is the name of the cookie containing the token.
|
|
|
|
const AuthCookieName = "token"
|
|
|
|
|
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))
|
|
|
|
mux.Handle("/v1/sys/unseal", handleSysUnseal(core))
|
2015-03-16 17:51:13 +00:00
|
|
|
mux.Handle("/v1/sys/mounts/", handleSysMounts(core))
|
2015-04-07 17:54:58 +00:00
|
|
|
mux.Handle("/v1/sys/remount", handleSysRemount(core))
|
2015-04-02 00:43:58 +00:00
|
|
|
mux.Handle("/v1/sys/policy", handleSysListPolicies(core))
|
2015-04-02 00:52:55 +00:00
|
|
|
mux.Handle("/v1/sys/policy/", handleSysPolicy(core))
|
2015-04-14 00:21:31 +00:00
|
|
|
mux.Handle("/v1/sys/renew/", handleSysRenew(core))
|
2015-04-01 02:21:02 +00:00
|
|
|
mux.Handle("/v1/sys/revoke/", handleSysRevoke(core))
|
2015-04-01 02:23:32 +00:00
|
|
|
mux.Handle("/v1/sys/revoke-prefix/", handleSysRevokePrefix(core))
|
2015-04-01 03:24:51 +00:00
|
|
|
mux.Handle("/v1/sys/auth/", handleSysAuth(core))
|
2015-04-02 01:36:13 +00:00
|
|
|
mux.Handle("/v1/sys/audit", handleSysListAudit(core))
|
|
|
|
mux.Handle("/v1/sys/audit/", handleSysAudit(core))
|
2015-03-16 02:34:47 +00:00
|
|
|
mux.Handle("/v1/", handleLogical(core))
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
return dec.Decode(out)
|
|
|
|
}
|
|
|
|
|
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 20:18:09 +00:00
|
|
|
func request(core *vault.Core, w http.ResponseWriter, reqURL *url.URL, r *logical.Request) (*logical.Response, bool) {
|
2015-04-08 18:19:03 +00:00
|
|
|
resp, err := core.HandleRequest(r)
|
2015-04-19 20:18:09 +00:00
|
|
|
if err == vault.ErrStandby {
|
|
|
|
respondStandby(core, w, reqURL)
|
|
|
|
return resp, false
|
|
|
|
}
|
2015-04-08 18:19:03 +00:00
|
|
|
if respondCommon(w, resp) {
|
|
|
|
return resp, false
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
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-03-31 04:06:15 +00:00
|
|
|
// Attach the cookie value as the token if we have it
|
2015-03-29 23:14:54 +00:00
|
|
|
cookie, err := r.Cookie(AuthCookieName)
|
|
|
|
if err == nil {
|
|
|
|
req.ClientToken = cookie.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2015-03-12 06:05:16 +00:00
|
|
|
func respondError(w http.ResponseWriter, status int, err error) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:29:53 +00:00
|
|
|
func respondCommon(w http.ResponseWriter, resp *logical.Response) bool {
|
|
|
|
if resp == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.IsError() {
|
|
|
|
err := fmt.Errorf("%s", resp.Data["error"].(string))
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
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"`
|
|
|
|
}
|