open-vault/http/sys_policy.go

151 lines
3.2 KiB
Go
Raw Normal View History

2015-04-02 00:43:58 +00:00
package http
import (
"net/http"
2015-04-02 00:52:55 +00:00
"strings"
2015-04-02 00:43:58 +00:00
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/vault"
)
func handleSysListPolicies(core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
respondError(w, http.StatusMethodNotAllowed, nil)
return
}
2015-04-19 21:36:50 +00:00
resp, ok := request(core, w, r, requestAuth(r, &logical.Request{
Operation: logical.ReadOperation,
Path: "sys/policy",
Connection: getConnection(r),
2015-04-02 00:43:58 +00:00
}))
2015-04-08 18:19:03 +00:00
if !ok {
2015-04-02 00:43:58 +00:00
return
}
var policies []string
policiesRaw, ok := resp.Data["keys"]
if ok {
policies = policiesRaw.([]string)
}
respondOk(w, &listPolicyResponse{Policies: policies})
})
}
2015-04-02 00:52:55 +00:00
func handleSysPolicy(core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handleSysReadPolicy(core, w, r)
2015-04-02 05:58:37 +00:00
case "PUT":
fallthrough
2015-04-02 00:52:55 +00:00
case "POST":
handleSysWritePolicy(core, w, r)
case "DELETE":
handleSysDeletePolicy(core, w, r)
default:
respondError(w, http.StatusMethodNotAllowed, nil)
return
}
})
}
func handleSysDeletePolicy(core *vault.Core, w http.ResponseWriter, r *http.Request) {
// Determine the path...
prefix := "/v1/sys/policy/"
if !strings.HasPrefix(r.URL.Path, prefix) {
respondError(w, http.StatusNotFound, nil)
return
}
path := r.URL.Path[len(prefix):]
if path == "" {
respondError(w, http.StatusNotFound, nil)
return
}
2015-04-19 21:36:50 +00:00
_, ok := request(core, w, r, requestAuth(r, &logical.Request{
Operation: logical.DeleteOperation,
Path: "sys/policy/" + path,
Connection: getConnection(r),
2015-04-02 00:52:55 +00:00
}))
2015-04-08 18:19:03 +00:00
if !ok {
2015-04-02 00:52:55 +00:00
return
}
respondOk(w, nil)
}
func handleSysReadPolicy(core *vault.Core, w http.ResponseWriter, r *http.Request) {
// Determine the path...
prefix := "/v1/sys/policy/"
if !strings.HasPrefix(r.URL.Path, prefix) {
respondError(w, http.StatusNotFound, nil)
return
}
path := r.URL.Path[len(prefix):]
if path == "" {
respondError(w, http.StatusNotFound, nil)
return
}
2015-04-19 21:36:50 +00:00
resp, ok := request(core, w, r, requestAuth(r, &logical.Request{
Operation: logical.ReadOperation,
Path: "sys/policy/" + path,
Connection: getConnection(r),
2015-04-02 00:52:55 +00:00
}))
2015-04-08 18:19:03 +00:00
if !ok {
2015-04-02 00:52:55 +00:00
return
}
2015-04-19 23:36:11 +00:00
if resp == nil {
respondError(w, http.StatusNotFound, nil)
return
}
2015-04-02 00:52:55 +00:00
respondOk(w, resp.Data)
}
func handleSysWritePolicy(core *vault.Core, w http.ResponseWriter, r *http.Request) {
// Determine the path...
prefix := "/v1/sys/policy/"
if !strings.HasPrefix(r.URL.Path, prefix) {
respondError(w, http.StatusNotFound, nil)
return
}
path := r.URL.Path[len(prefix):]
if path == "" {
respondError(w, http.StatusNotFound, nil)
return
}
// Parse the request if we can
var req writePolicyRequest
if err := parseRequest(r, &req); err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
2015-04-19 21:36:50 +00:00
_, ok := request(core, w, r, requestAuth(r, &logical.Request{
2016-01-07 15:30:47 +00:00
Operation: logical.UpdateOperation,
Path: "sys/policy/" + path,
Connection: getConnection(r),
2015-04-02 00:52:55 +00:00
Data: map[string]interface{}{
"rules": req.Rules,
},
}))
2015-04-08 18:19:03 +00:00
if !ok {
2015-04-02 00:52:55 +00:00
return
}
respondOk(w, nil)
}
2015-04-02 00:43:58 +00:00
type listPolicyResponse struct {
Policies []string `json:"policies"`
}
2015-04-02 00:52:55 +00:00
type writePolicyRequest struct {
Rules string `json:"rules"`
}