open-vault/http/sys_capabilities.go

90 lines
1.9 KiB
Go
Raw Normal View History

2016-03-02 18:42:32 +00:00
package http
import (
"net/http"
2016-03-03 16:08:27 +00:00
"strings"
2016-03-02 18:42:32 +00:00
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/vault"
)
func handleSysCapabilitiesAccessor(core *vault.Core) http.Handler {
2016-03-02 18:42:32 +00:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2016-03-03 02:34:06 +00:00
switch r.Method {
case "PUT":
case "POST":
default:
2016-03-02 18:42:32 +00:00
respondError(w, http.StatusMethodNotAllowed, nil)
return
}
// Parse the request if we can
var data capabilitiesAccessorRequest
if err := parseRequest(r, &data); err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
capabilities, err := core.CapabilitiesAccessor(data.Accessor, data.Path)
if err != nil {
respondErrorStatus(w, err)
return
}
respondOk(w, &capabilitiesResponse{
Capabilities: capabilities,
})
})
}
func handleSysCapabilities(core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
case "POST":
default:
respondError(w, http.StatusMethodNotAllowed, nil)
return
}
2016-03-02 18:42:32 +00:00
// Parse the request if we can
2016-03-03 02:34:06 +00:00
var data capabilitiesRequest
if err := parseRequest(r, &data); err != nil {
2016-03-02 18:42:32 +00:00
respondError(w, http.StatusBadRequest, err)
return
}
2016-03-03 16:08:27 +00:00
if strings.HasPrefix(r.URL.Path, "/v1/sys/capabilities-self") {
// Get the auth for the request so we can access the token directly
req := requestAuth(r, &logical.Request{})
2016-03-03 02:34:06 +00:00
data.Token = req.ClientToken
}
2016-03-02 18:42:32 +00:00
capabilities, err := core.Capabilities(data.Token, data.Path)
2016-03-03 02:34:06 +00:00
if err != nil {
respondErrorStatus(w, err)
return
2016-03-02 18:42:32 +00:00
}
2016-03-03 16:08:27 +00:00
respondOk(w, &capabilitiesResponse{
Capabilities: capabilities,
})
2016-03-02 18:42:32 +00:00
})
2016-03-03 02:34:06 +00:00
2016-03-02 18:42:32 +00:00
}
type capabilitiesResponse struct {
2016-03-03 02:34:06 +00:00
Capabilities []string `json:"capabilities"`
2016-03-02 18:42:32 +00:00
}
type capabilitiesRequest struct {
Token string `json:"token"`
Path string `json:"path"`
}
type capabilitiesAccessorRequest struct {
Accessor string `json:"accessor"`
Path string `json:"path"`
}