2015-03-16 04:18:25 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-03-16 17:36:29 +00:00
|
|
|
"strings"
|
2015-03-16 04:18:25 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2015-03-16 17:51:13 +00:00
|
|
|
func handleSysMounts(core *vault.Core) http.Handler {
|
2015-03-16 17:36:29 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-03-16 17:41:08 +00:00
|
|
|
switch r.Method {
|
2015-03-16 17:51:13 +00:00
|
|
|
case "GET":
|
2015-04-22 05:55:40 +00:00
|
|
|
handleSysListMounts(core).ServeHTTP(w, r)
|
2015-03-16 17:41:08 +00:00
|
|
|
case "POST":
|
2015-03-16 17:51:13 +00:00
|
|
|
fallthrough
|
2015-03-16 17:41:08 +00:00
|
|
|
case "DELETE":
|
2015-03-16 17:51:13 +00:00
|
|
|
handleSysMountUnmount(core, w, r)
|
2015-03-16 17:41:08 +00:00
|
|
|
default:
|
2015-03-16 17:36:29 +00:00
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
2015-03-16 17:51:13 +00:00
|
|
|
})
|
|
|
|
}
|
2015-03-16 17:36:29 +00:00
|
|
|
|
2015-04-07 17:54:58 +00:00
|
|
|
func handleSysRemount(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-04-07 17:55:18 +00:00
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
case "PUT":
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:54:58 +00:00
|
|
|
// Parse the request if we can
|
|
|
|
var req RemountRequest
|
|
|
|
if err := parseRequest(r, &req); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
|
|
|
Operation: logical.WriteOperation,
|
|
|
|
Path: "sys/remount",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"from": req.From,
|
|
|
|
"to": req.To,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
func handleSysListMounts(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-03-16 17:36:29 +00:00
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
resp, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "sys/mounts",
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-16 17:51:13 +00:00
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
respondOk(w, resp.Data)
|
|
|
|
})
|
2015-03-16 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysMountUnmount(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
case "DELETE":
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the path...
|
|
|
|
prefix := "/v1/sys/mounts/"
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
handleSysMount(core, w, r, path)
|
|
|
|
case "DELETE":
|
|
|
|
handleSysUnmount(core, w, r, path)
|
|
|
|
default:
|
|
|
|
panic("should never happen")
|
|
|
|
}
|
2015-03-16 17:41:08 +00:00
|
|
|
}
|
2015-03-16 17:36:29 +00:00
|
|
|
|
2015-03-16 17:41:08 +00:00
|
|
|
func handleSysMount(
|
|
|
|
core *vault.Core,
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
path string) {
|
|
|
|
// Parse the request if we can
|
|
|
|
var req MountRequest
|
|
|
|
if err := parseRequest(r, &req); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:14:54 +00:00
|
|
|
_, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
2015-03-16 17:41:08 +00:00
|
|
|
Operation: logical.WriteOperation,
|
2015-03-16 17:52:35 +00:00
|
|
|
Path: "sys/mounts/" + path,
|
2015-03-16 17:41:08 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": req.Type,
|
|
|
|
"description": req.Description,
|
|
|
|
},
|
2015-03-29 23:14:54 +00:00
|
|
|
}))
|
2015-03-16 17:41:08 +00:00
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-16 17:36:29 +00:00
|
|
|
|
2015-03-16 17:41:08 +00:00
|
|
|
respondOk(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysUnmount(
|
|
|
|
core *vault.Core,
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
path string) {
|
2015-03-29 23:14:54 +00:00
|
|
|
_, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
2015-03-16 17:41:08 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
2015-03-16 17:52:35 +00:00
|
|
|
Path: "sys/mounts/" + path,
|
2015-03-29 23:14:54 +00:00
|
|
|
}))
|
2015-03-16 17:41:08 +00:00
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
2015-03-16 17:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountRequest struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
2015-04-07 17:54:58 +00:00
|
|
|
|
|
|
|
type RemountRequest struct {
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
}
|