2015-04-02 01:36:13 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysListAudit(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-20 05:40:50 +00:00
|
|
|
resp, ok := request(core, w, r, requestAuth(r, &logical.Request{
|
2015-04-02 01:36:13 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "sys/audit",
|
|
|
|
}))
|
2015-04-20 05:40:50 +00:00
|
|
|
if !ok {
|
2015-04-02 01:36:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, resp.Data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysAudit(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
fallthrough
|
|
|
|
case "PUT":
|
|
|
|
handleSysEnableAudit(core, w, r)
|
|
|
|
case "DELETE":
|
|
|
|
handleSysDisableAudit(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysDisableAudit(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Determine the path...
|
|
|
|
prefix := "/v1/sys/audit/"
|
|
|
|
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-20 05:40:50 +00:00
|
|
|
_, ok := request(core, w, r, requestAuth(r, &logical.Request{
|
2015-04-02 01:36:13 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "sys/audit/" + path,
|
|
|
|
}))
|
2015-04-20 05:40:50 +00:00
|
|
|
if !ok {
|
2015-04-02 01:36:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysEnableAudit(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Determine the path...
|
|
|
|
prefix := "/v1/sys/audit/"
|
|
|
|
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 enableAuditRequest
|
|
|
|
if err := parseRequest(r, &req); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-20 05:40:50 +00:00
|
|
|
_, ok := request(core, w, r, requestAuth(r, &logical.Request{
|
2015-04-02 01:36:13 +00:00
|
|
|
Operation: logical.WriteOperation,
|
|
|
|
Path: "sys/audit/" + path,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": req.Type,
|
|
|
|
"description": req.Description,
|
|
|
|
"options": req.Options,
|
|
|
|
},
|
|
|
|
}))
|
2015-04-20 05:40:50 +00:00
|
|
|
if !ok {
|
2015-04-02 01:36:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type enableAuditRequest struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Options map[string]string `json:"options"`
|
|
|
|
}
|