open-vault/api/sys_audit.go

157 lines
4.1 KiB
Go
Raw Normal View History

2015-04-02 01:38:25 +00:00
package api
import (
"context"
"errors"
2015-04-02 01:38:25 +00:00
"fmt"
"net/http"
2016-08-08 20:00:31 +00:00
"github.com/mitchellh/mapstructure"
2015-04-02 01:38:25 +00:00
)
func (c *Sys) AuditHash(path string, input string) (string, error) {
return c.AuditHashWithContext(context.Background(), path, input)
}
func (c *Sys) AuditHashWithContext(ctx context.Context, path string, input string) (string, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
body := map[string]interface{}{
"input": input,
}
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit-hash/%s", path))
if err := r.SetJSONBody(body); err != nil {
return "", err
}
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return "", err
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
2016-08-08 20:00:31 +00:00
if err != nil {
return "", err
}
if secret == nil || secret.Data == nil {
return "", errors.New("data from server response is empty")
}
2016-08-08 20:00:31 +00:00
hash, ok := secret.Data["hash"]
if !ok {
return "", errors.New("hash not found in response data")
}
hashStr, ok := hash.(string)
if !ok {
return "", errors.New("could not parse hash in response data")
}
return hashStr, nil
}
2015-04-02 01:38:25 +00:00
func (c *Sys) ListAudit() (map[string]*Audit, error) {
return c.ListAuditWithContext(context.Background())
}
func (c *Sys) ListAuditWithContext(ctx context.Context) (map[string]*Audit, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodGet, "/v1/sys/audit")
resp, err := c.c.rawRequestWithContext(ctx, r)
2015-04-02 01:38:25 +00:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
2016-08-08 20:00:31 +00:00
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}
2016-08-08 20:00:31 +00:00
mounts := map[string]*Audit{}
err = mapstructure.Decode(secret.Data, &mounts)
if err != nil {
return nil, err
2016-08-08 20:00:31 +00:00
}
return mounts, nil
2015-04-02 01:38:25 +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 {
return c.EnableAuditWithOptions(path, &EnableAuditOptions{
Type: auditType,
Description: desc,
Options: opts,
})
}
func (c *Sys) EnableAuditWithOptions(path string, options *EnableAuditOptions) error {
return c.EnableAuditWithOptionsWithContext(context.Background(), path, options)
}
func (c *Sys) EnableAuditWithOptionsWithContext(ctx context.Context, path string, options *EnableAuditOptions) error {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit/%s", path))
if err := r.SetJSONBody(options); err != nil {
2015-04-02 01:38:25 +00:00
return err
}
resp, err := c.c.rawRequestWithContext(ctx, r)
2015-04-02 01:38:25 +00:00
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (c *Sys) DisableAudit(path string) error {
return c.DisableAuditWithContext(context.Background(), path)
}
func (c *Sys) DisableAuditWithContext(ctx context.Context, path string) error {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodDelete, fmt.Sprintf("/v1/sys/audit/%s", path))
resp, err := c.c.rawRequestWithContext(ctx, r)
2015-04-02 01:38:25 +00:00
if err == nil {
defer resp.Body.Close()
}
return err
}
// Structures for the requests/response are all down here. They aren't
// 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.
type EnableAuditOptions struct {
Type string `json:"type" mapstructure:"type"`
Description string `json:"description" mapstructure:"description"`
Options map[string]string `json:"options" mapstructure:"options"`
Local bool `json:"local" mapstructure:"local"`
}
2015-04-02 01:38:25 +00:00
type Audit struct {
Type string `json:"type" mapstructure:"type"`
Description string `json:"description" mapstructure:"description"`
Options map[string]string `json:"options" mapstructure:"options"`
Local bool `json:"local" mapstructure:"local"`
Path string `json:"path" mapstructure:"path"`
2015-04-02 01:38:25 +00:00
}