2015-04-01 03:24:51 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysAuth(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
2015-04-22 05:55:51 +00:00
|
|
|
case "GET":
|
|
|
|
handleSysListAuth(core).ServeHTTP(w, r)
|
2015-05-13 00:57:02 +00:00
|
|
|
case "POST", "PUT", "DELETE":
|
2015-04-01 03:24:51 +00:00
|
|
|
handleSysEnableDisableAuth(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
func handleSysListAuth(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-01 03:24:51 +00:00
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
resp, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
2015-06-19 00:17:18 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "sys/auth",
|
|
|
|
Connection: getConnection(r),
|
2015-04-22 05:55:40 +00:00
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-01 03:24:51 +00:00
|
|
|
|
2015-04-22 05:55:40 +00:00
|
|
|
respondOk(w, resp.Data)
|
|
|
|
})
|
2015-04-01 03:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysEnableDisableAuth(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Determine the path...
|
|
|
|
prefix := "/v1/sys/auth/"
|
|
|
|
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 {
|
2015-05-13 00:57:02 +00:00
|
|
|
case "PUT", "POST":
|
2015-04-01 03:24:51 +00:00
|
|
|
handleSysEnableAuth(core, w, r, path)
|
|
|
|
case "DELETE":
|
|
|
|
handleSysDisableAuth(core, w, r, path)
|
|
|
|
default:
|
|
|
|
panic("should never happen")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysEnableAuth(
|
|
|
|
core *vault.Core,
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
path string) {
|
|
|
|
// Parse the request if we can
|
|
|
|
var req EnableAuthRequest
|
|
|
|
if err := parseRequest(r, &req); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
2015-06-19 00:17:18 +00:00
|
|
|
Operation: logical.WriteOperation,
|
|
|
|
Path: "sys/auth/" + path,
|
|
|
|
Connection: getConnection(r),
|
2015-04-01 03:24:51 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": req.Type,
|
|
|
|
"description": req.Description,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysDisableAuth(
|
|
|
|
core *vault.Core,
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
path string) {
|
|
|
|
_, err := core.HandleRequest(requestAuth(r, &logical.Request{
|
2015-06-19 00:17:18 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "sys/auth/" + path,
|
|
|
|
Connection: getConnection(r),
|
2015-04-01 03:24:51 +00:00
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type EnableAuthRequest struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|