2015-03-16 02:34:47 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2015-04-07 21:36:17 +00:00
|
|
|
"io"
|
2015-05-20 05:23:41 +00:00
|
|
|
"net"
|
2015-03-16 02:34:47 +00:00
|
|
|
"net/http"
|
2016-01-19 23:09:26 +00:00
|
|
|
"strconv"
|
2015-03-16 02:34:47 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-05-02 02:39:45 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-03-16 02:34:47 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2016-03-18 14:37:49 +00:00
|
|
|
type PrepareRequestFunc func(req *logical.Request) error
|
2016-03-18 02:29:53 +00:00
|
|
|
|
2016-05-20 17:03:54 +00:00
|
|
|
func buildLogicalRequest(w http.ResponseWriter, r *http.Request) (*logical.Request, int, error) {
|
|
|
|
// Determine the path...
|
|
|
|
if !strings.HasPrefix(r.URL.Path, "/v1/") {
|
|
|
|
return nil, http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
path := r.URL.Path[len("/v1/"):]
|
|
|
|
if path == "" {
|
|
|
|
return nil, http.StatusNotFound, nil
|
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
|
2016-05-20 17:03:54 +00:00
|
|
|
// Determine the operation
|
|
|
|
var op logical.Operation
|
|
|
|
switch r.Method {
|
|
|
|
case "DELETE":
|
|
|
|
op = logical.DeleteOperation
|
|
|
|
case "GET":
|
|
|
|
op = logical.ReadOperation
|
|
|
|
// Need to call ParseForm to get query params loaded
|
|
|
|
queryVals := r.URL.Query()
|
|
|
|
listStr := queryVals.Get("list")
|
|
|
|
if listStr != "" {
|
|
|
|
list, err := strconv.ParseBool(listStr)
|
2015-04-07 21:36:17 +00:00
|
|
|
if err != nil {
|
2016-05-20 17:03:54 +00:00
|
|
|
return nil, http.StatusBadRequest, nil
|
|
|
|
}
|
|
|
|
if list {
|
|
|
|
op = logical.ListOperation
|
2015-03-16 02:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-20 17:03:54 +00:00
|
|
|
case "POST", "PUT":
|
|
|
|
op = logical.UpdateOperation
|
|
|
|
case "LIST":
|
|
|
|
op = logical.ListOperation
|
|
|
|
default:
|
|
|
|
return nil, http.StatusMethodNotAllowed, nil
|
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
|
2016-05-20 17:03:54 +00:00
|
|
|
// Parse the request if we can
|
|
|
|
var data map[string]interface{}
|
|
|
|
if op == logical.UpdateOperation {
|
|
|
|
err := parseRequest(r, &data)
|
|
|
|
if err == io.EOF {
|
|
|
|
data = nil
|
|
|
|
err = nil
|
|
|
|
}
|
2016-05-02 02:39:45 +00:00
|
|
|
if err != nil {
|
2016-05-20 17:03:54 +00:00
|
|
|
return nil, http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
req := requestAuth(r, &logical.Request{
|
|
|
|
Operation: op,
|
|
|
|
Path: path,
|
|
|
|
Data: data,
|
|
|
|
Connection: getConnection(r),
|
|
|
|
})
|
|
|
|
req, err = requestWrapTTL(r, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, http.StatusBadRequest, errwrap.Wrapf("error parsing X-Vault-Wrap-TTL header: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLogical(core *vault.Core, dataOnly bool, prepareRequestCallback PrepareRequestFunc) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req, statusCode, err := buildLogicalRequest(w, r)
|
|
|
|
if err != nil || statusCode != 0 {
|
|
|
|
respondError(w, statusCode, err)
|
2016-05-02 02:39:45 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-18 02:29:53 +00:00
|
|
|
|
2016-05-19 17:33:51 +00:00
|
|
|
// Certain endpoints may require changes to the request object. They
|
|
|
|
// will have a callback registered to do the needed operations, so
|
|
|
|
// invoke it before proceeding.
|
2016-03-18 02:29:53 +00:00
|
|
|
if prepareRequestCallback != nil {
|
|
|
|
if err := prepareRequestCallback(req); err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the internal request. We attach the connection info
|
|
|
|
// as well in case this is an authentication request that requires
|
|
|
|
// it. Vault core handles stripping this if we need to.
|
|
|
|
resp, ok := request(core, w, r, req)
|
2015-04-08 18:19:03 +00:00
|
|
|
if !ok {
|
2015-03-16 02:42:24 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-02 23:37:47 +00:00
|
|
|
switch {
|
2016-05-20 17:03:54 +00:00
|
|
|
case req.Operation == logical.ReadOperation:
|
2016-05-02 23:37:47 +00:00
|
|
|
if resp == nil {
|
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basically: if we have empty "keys" or no keys at all, 404. This
|
|
|
|
// provides consistency with GET.
|
2016-05-20 17:03:54 +00:00
|
|
|
case req.Operation == logical.ListOperation:
|
2016-05-02 23:37:47 +00:00
|
|
|
if resp == nil || len(resp.Data) == 0 {
|
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keysInt, ok := resp.Data["keys"]
|
2016-05-03 00:00:46 +00:00
|
|
|
if !ok || keysInt == nil {
|
2016-05-02 23:37:47 +00:00
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keys, ok := keysInt.([]string)
|
|
|
|
if !ok {
|
2016-05-03 00:00:46 +00:00
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
2016-05-02 23:37:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(keys) == 0 {
|
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
2015-04-01 04:24:20 +00:00
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
|
2015-04-14 00:21:31 +00:00
|
|
|
// Build the proper response
|
2016-05-20 17:03:54 +00:00
|
|
|
respondLogical(w, r, req.Path, dataOnly, resp)
|
2015-04-14 00:21:31 +00:00
|
|
|
})
|
|
|
|
}
|
2015-03-31 04:06:15 +00:00
|
|
|
|
2015-08-20 20:20:35 +00:00
|
|
|
func respondLogical(w http.ResponseWriter, r *http.Request, path string, dataOnly bool, resp *logical.Response) {
|
2015-04-14 00:21:31 +00:00
|
|
|
var httpResp interface{}
|
|
|
|
if resp != nil {
|
|
|
|
if resp.Redirect != "" {
|
2015-08-20 20:20:35 +00:00
|
|
|
// If we have a redirect, redirect! We use a 307 code
|
2015-04-14 00:21:31 +00:00
|
|
|
// because we don't actually know if its permanent.
|
2015-08-20 20:20:35 +00:00
|
|
|
http.Redirect(w, r, resp.Redirect, 307)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if dataOnly {
|
|
|
|
respondOk(w, resp.Data)
|
2015-04-14 00:21:31 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
|
2015-05-27 21:10:00 +00:00
|
|
|
// Check if this is a raw response
|
|
|
|
if _, ok := resp.Data[logical.HTTPContentType]; ok {
|
|
|
|
respondRaw(w, r, path, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:17:51 +00:00
|
|
|
if resp.WrapInfo != nil && resp.WrapInfo.Token != "" {
|
2016-05-02 04:08:07 +00:00
|
|
|
httpResp = logical.HTTPResponse{
|
|
|
|
WrapInfo: &logical.HTTPWrapInfo{
|
2016-06-13 23:58:17 +00:00
|
|
|
Token: resp.WrapInfo.Token,
|
|
|
|
TTL: int(resp.WrapInfo.TTL.Seconds()),
|
|
|
|
CreationTime: resp.WrapInfo.CreationTime,
|
|
|
|
WrappedAccessor: resp.WrapInfo.WrappedAccessor,
|
2016-05-02 04:08:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
httpResp = logical.SanitizeResponse(resp)
|
|
|
|
}
|
2015-04-14 00:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Respond
|
|
|
|
respondOk(w, httpResp)
|
2016-05-20 15:49:48 +00:00
|
|
|
return
|
2015-03-16 02:34:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 21:10:00 +00:00
|
|
|
// respondRaw is used when the response is using HTTPContentType and HTTPRawBody
|
|
|
|
// to change the default response handling. This is only used for specific things like
|
|
|
|
// returning the CRL information on the PKI backends.
|
|
|
|
func respondRaw(w http.ResponseWriter, r *http.Request, path string, resp *logical.Response) {
|
|
|
|
// Ensure this is never a secret or auth response
|
|
|
|
if resp.Secret != nil || resp.Auth != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the status code
|
|
|
|
statusRaw, ok := resp.Data[logical.HTTPStatusCode]
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
status, ok := statusRaw.(int)
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the header
|
|
|
|
contentTypeRaw, ok := resp.Data[logical.HTTPContentType]
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contentType, ok := contentTypeRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the body
|
|
|
|
bodyRaw, ok := resp.Data[logical.HTTPRawBody]
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body, ok := bodyRaw.([]byte)
|
|
|
|
if !ok {
|
|
|
|
respondError(w, http.StatusInternalServerError, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the response
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write(body)
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:27:28 +00:00
|
|
|
// getConnection is used to format the connection information for
|
|
|
|
// attaching to a logical request
|
2015-06-19 00:17:18 +00:00
|
|
|
func getConnection(r *http.Request) (connection *logical.Connection) {
|
|
|
|
var remoteAddr string
|
|
|
|
|
|
|
|
remoteAddr, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
remoteAddr = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
connection = &logical.Connection{
|
|
|
|
RemoteAddr: remoteAddr,
|
|
|
|
ConnState: r.TLS,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|