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"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2015-08-20 20:20:35 +00:00
|
|
|
func handleLogical(core *vault.Core, dataOnly bool) http.Handler {
|
2015-03-16 02:34:47 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Determine the path...
|
|
|
|
if !strings.HasPrefix(r.URL.Path, "/v1/") {
|
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path := r.URL.Path[len("/v1/"):]
|
|
|
|
if path == "" {
|
|
|
|
respondError(w, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the operation
|
|
|
|
var op logical.Operation
|
|
|
|
switch r.Method {
|
2015-04-07 18:04:06 +00:00
|
|
|
case "DELETE":
|
|
|
|
op = logical.DeleteOperation
|
2015-03-16 02:34:47 +00:00
|
|
|
case "GET":
|
2016-01-19 23:09:26 +00:00
|
|
|
op = logical.ReadOperation
|
2016-01-14 19:18:27 +00:00
|
|
|
// Need to call ParseForm to get query params loaded
|
2016-01-15 16:37:14 +00:00
|
|
|
queryVals := r.URL.Query()
|
2016-01-19 23:09:26 +00:00
|
|
|
listStr := queryVals.Get("list")
|
|
|
|
if listStr != "" {
|
|
|
|
list, err := strconv.ParseBool(listStr)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, nil)
|
|
|
|
}
|
|
|
|
if list {
|
|
|
|
op = logical.ListOperation
|
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
}
|
|
|
|
case "POST", "PUT":
|
2016-01-07 15:30:47 +00:00
|
|
|
op = logical.UpdateOperation
|
2015-09-14 15:06:30 +00:00
|
|
|
case "LIST":
|
|
|
|
op = logical.ListOperation
|
2015-03-16 02:34:47 +00:00
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the request if we can
|
|
|
|
var req map[string]interface{}
|
2016-01-07 15:30:47 +00:00
|
|
|
if op == logical.UpdateOperation {
|
2015-04-07 21:36:17 +00:00
|
|
|
err := parseRequest(r, &req)
|
|
|
|
if err == io.EOF {
|
|
|
|
req = nil
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-03-16 02:34:47 +00:00
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 04:06:15 +00:00
|
|
|
// 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.
|
2015-04-19 21:36:50 +00:00
|
|
|
resp, ok := request(core, w, r, requestAuth(r, &logical.Request{
|
2015-06-29 22:27:28 +00:00
|
|
|
Operation: op,
|
|
|
|
Path: path,
|
|
|
|
Data: req,
|
2015-06-19 00:17:18 +00:00
|
|
|
Connection: getConnection(r),
|
2015-03-29 23:14:54 +00:00
|
|
|
}))
|
2015-04-08 18:19:03 +00:00
|
|
|
if !ok {
|
2015-03-16 02:42:24 +00:00
|
|
|
return
|
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
if (op == logical.ReadOperation || op == logical.ListOperation) && resp == nil {
|
2015-04-01 04:29:53 +00:00
|
|
|
respondError(w, http.StatusNotFound, nil)
|
2015-04-01 04:24:20 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
|
2015-04-14 00:21:31 +00:00
|
|
|
// Build the proper response
|
2015-08-20 20:20:35 +00:00
|
|
|
respondLogical(w, r, 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
|
|
|
|
}
|
|
|
|
|
2015-10-07 19:30:54 +00:00
|
|
|
logicalResp := &LogicalResponse{
|
|
|
|
Data: resp.Data,
|
2015-10-07 19:41:25 +00:00
|
|
|
Warnings: resp.Warnings(),
|
2015-10-07 19:30:54 +00:00
|
|
|
}
|
2015-04-14 00:21:31 +00:00
|
|
|
if resp.Secret != nil {
|
|
|
|
logicalResp.LeaseID = resp.Secret.LeaseID
|
|
|
|
logicalResp.Renewable = resp.Secret.Renewable
|
2015-08-21 00:47:17 +00:00
|
|
|
logicalResp.LeaseDuration = int(resp.Secret.TTL.Seconds())
|
2015-04-14 00:21:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 00:36:19 +00:00
|
|
|
// If we have authentication information, then
|
|
|
|
// set up the result structure.
|
2015-04-14 00:21:31 +00:00
|
|
|
if resp.Auth != nil {
|
|
|
|
logicalResp.Auth = &Auth{
|
|
|
|
ClientToken: resp.Auth.ClientToken,
|
|
|
|
Policies: resp.Auth.Policies,
|
|
|
|
Metadata: resp.Auth.Metadata,
|
2015-08-21 00:47:17 +00:00
|
|
|
LeaseDuration: int(resp.Auth.TTL.Seconds()),
|
2015-04-14 00:21:31 +00:00
|
|
|
Renewable: resp.Auth.Renewable,
|
|
|
|
}
|
2015-03-16 02:34:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 00:21:31 +00:00
|
|
|
httpResp = logicalResp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond
|
|
|
|
respondOk(w, httpResp)
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-16 02:34:47 +00:00
|
|
|
type LogicalResponse struct {
|
2015-04-08 20:35:32 +00:00
|
|
|
LeaseID string `json:"lease_id"`
|
2015-03-16 20:29:51 +00:00
|
|
|
Renewable bool `json:"renewable"`
|
|
|
|
LeaseDuration int `json:"lease_duration"`
|
|
|
|
Data map[string]interface{} `json:"data"`
|
2015-10-07 19:30:54 +00:00
|
|
|
Warnings []string `json:"warnings"`
|
2015-04-04 22:40:41 +00:00
|
|
|
Auth *Auth `json:"auth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Auth struct {
|
2015-04-24 16:00:00 +00:00
|
|
|
ClientToken string `json:"client_token"`
|
2015-04-04 22:40:41 +00:00
|
|
|
Policies []string `json:"policies"`
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
LeaseDuration int `json:"lease_duration"`
|
|
|
|
Renewable bool `json:"renewable"`
|
2015-03-16 02:34:47 +00:00
|
|
|
}
|