2015-04-02 01:38:25 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-08 20:00:31 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-02 01:38:25 +00:00
|
|
|
)
|
|
|
|
|
2015-11-19 01:26:03 +00:00
|
|
|
func (c *Sys) AuditHash(path string, input string) (string, error) {
|
|
|
|
body := map[string]interface{}{
|
|
|
|
"input": input,
|
|
|
|
}
|
|
|
|
|
|
|
|
r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/sys/audit-hash/%s", path))
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
type d struct {
|
2016-08-14 18:52:45 +00:00
|
|
|
Hash string `json:"hash"`
|
2015-11-19 01:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result d
|
2016-08-14 18:52:45 +00:00
|
|
|
err = resp.DecodeJSON(&result)
|
2016-08-08 20:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:26:03 +00:00
|
|
|
return result.Hash, err
|
|
|
|
}
|
|
|
|
|
2015-04-02 01:38:25 +00:00
|
|
|
func (c *Sys) ListAudit() (map[string]*Audit, error) {
|
|
|
|
r := c.c.NewRequest("GET", "/v1/sys/audit")
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-08-14 18:52:45 +00:00
|
|
|
var result map[string]interface{}
|
|
|
|
err = resp.DecodeJSON(&result)
|
2016-08-08 20:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-14 18:52:45 +00:00
|
|
|
mounts := map[string]*Audit{}
|
|
|
|
for k, v := range result {
|
|
|
|
switch v.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2016-08-08 20:00:31 +00:00
|
|
|
var res Audit
|
|
|
|
err = mapstructure.Decode(v, &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-14 18:52:45 +00:00
|
|
|
// Not a mount, some other api.Secret data
|
|
|
|
if res.Type == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mounts[k] = &res
|
2016-08-08 20:00:31 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 18:52:45 +00:00
|
|
|
return mounts, nil
|
2015-04-02 01:38:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 16:37:27 +00:00
|
|
|
// DEPRECATED: Use EnableAuditWithOptions instead
|
2015-04-02 01:38:25 +00:00
|
|
|
func (c *Sys) EnableAudit(
|
|
|
|
path string, auditType string, desc string, opts map[string]string) error {
|
2017-02-16 16:37:27 +00:00
|
|
|
return c.EnableAuditWithOptions(path, &EnableAuditOptions{
|
|
|
|
Type: auditType,
|
|
|
|
Description: desc,
|
|
|
|
Options: opts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) EnableAuditWithOptions(path string, options *EnableAuditOptions) error {
|
2015-04-02 01:38:25 +00:00
|
|
|
r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/sys/audit/%s", path))
|
2018-04-10 02:23:56 +00:00
|
|
|
if err := r.SetJSONBody(options); err != nil {
|
2015-04-02 01:38:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) DisableAudit(path string) error {
|
|
|
|
r := c.c.NewRequest("DELETE", fmt.Sprintf("/v1/sys/audit/%s", path))
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Structures for the requests/resposne are all down here. They aren't
|
2016-08-14 18:52:45 +00:00
|
|
|
// individually documented because the map almost directly to the raw HTTP API
|
2015-04-02 01:38:25 +00:00
|
|
|
// documentation. Please refer to that documentation for more details.
|
|
|
|
|
2017-02-16 16:37:27 +00:00
|
|
|
type EnableAuditOptions struct {
|
2018-04-10 02:23:56 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Options map[string]string `json:"options"`
|
|
|
|
Local bool `json:"local"`
|
2017-02-16 16:37:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 01:38:25 +00:00
|
|
|
type Audit struct {
|
2016-03-14 21:15:07 +00:00
|
|
|
Path string
|
2015-04-02 01:38:25 +00:00
|
|
|
Type string
|
|
|
|
Description string
|
|
|
|
Options map[string]string
|
2017-02-16 16:37:27 +00:00
|
|
|
Local bool
|
2015-04-02 01:38:25 +00:00
|
|
|
}
|