2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-02 04:38:25 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-07-25 01:49:55 +03:00
|
|
|
"context"
|
2018-08-14 22:29:22 +03:00
|
|
|
"errors"
|
2015-04-02 04:38:25 +03:00
|
|
|
"fmt"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2016-08-08 23:00:31 +03:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-04-02 04:38:25 +03:00
|
|
|
)
|
|
|
|
|
2015-11-19 04:26:03 +03:00
|
|
|
func (c *Sys) AuditHash(path string, input string) (string, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
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()
|
|
|
|
|
2015-11-19 04:26:03 +03:00
|
|
|
body := map[string]interface{}{
|
|
|
|
"input": input,
|
|
|
|
}
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit-hash/%s", path))
|
2015-11-19 04:26:03 +03:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-11-19 04:26:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-08-08 23:00:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return "", errors.New("data from server response is empty")
|
|
|
|
}
|
2016-08-08 23:00:31 +03:00
|
|
|
|
2018-08-14 22:29:22 +03: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-11-19 04:26:03 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 04:38:25 +03:00
|
|
|
func (c *Sys) ListAudit() (map[string]*Audit, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.ListAuditWithContext(context.Background())
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) ListAuditWithContext(ctx context.Context) (map[string]*Audit, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-25 01:49:55 +03:00
|
|
|
defer cancelFunc()
|
2022-03-24 00:47:43 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/sys/audit")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-02 04:38:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-08-08 23:00:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
2016-08-08 23:00:31 +03:00
|
|
|
|
2016-08-14 21:52:45 +03:00
|
|
|
mounts := map[string]*Audit{}
|
2018-08-14 22:29:22 +03:00
|
|
|
err = mapstructure.Decode(secret.Data, &mounts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-08 23:00:31 +03:00
|
|
|
}
|
|
|
|
|
2016-08-14 21:52:45 +03:00
|
|
|
return mounts, nil
|
2015-04-02 04:38:25 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 19:37:27 +03:00
|
|
|
// DEPRECATED: Use EnableAuditWithOptions instead
|
2015-04-02 04:38:25 +03:00
|
|
|
func (c *Sys) EnableAudit(
|
2022-09-08 03:31:20 +03:00
|
|
|
path string, auditType string, desc string, opts map[string]string,
|
|
|
|
) error {
|
2017-02-16 19:37:27 +03:00
|
|
|
return c.EnableAuditWithOptions(path, &EnableAuditOptions{
|
|
|
|
Type: auditType,
|
|
|
|
Description: desc,
|
|
|
|
Options: opts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) EnableAuditWithOptions(path string, options *EnableAuditOptions) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
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()
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, fmt.Sprintf("/v1/sys/audit/%s", path))
|
2018-04-10 05:23:56 +03:00
|
|
|
if err := r.SetJSONBody(options); err != nil {
|
2015-04-02 04:38:25 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-02 04:38:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) DisableAudit(path string) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.DisableAuditWithContext(context.Background(), path)
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) DisableAuditWithContext(ctx context.Context, path string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-25 01:49:55 +03:00
|
|
|
defer cancelFunc()
|
2022-03-24 00:47:43 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodDelete, fmt.Sprintf("/v1/sys/audit/%s", path))
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2015-04-02 04:38:25 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-21 23:07:50 +03:00
|
|
|
// Structures for the requests/response are all down here. They aren't
|
2016-08-14 21:52:45 +03:00
|
|
|
// individually documented because the map almost directly to the raw HTTP API
|
2015-04-02 04:38:25 +03:00
|
|
|
// documentation. Please refer to that documentation for more details.
|
|
|
|
|
2017-02-16 19:37:27 +03:00
|
|
|
type EnableAuditOptions struct {
|
2018-08-14 22:29:22 +03:00
|
|
|
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"`
|
2017-02-16 19:37:27 +03:00
|
|
|
}
|
|
|
|
|
2015-04-02 04:38:25 +03:00
|
|
|
type Audit struct {
|
2018-08-14 22:29:22 +03:00
|
|
|
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 04:38:25 +03:00
|
|
|
}
|