2015-04-02 00:59:41 +00:00
|
|
|
package api
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2018-08-14 19:29:22 +00:00
|
|
|
"errors"
|
2018-07-24 22:49:55 +00:00
|
|
|
"fmt"
|
2018-08-14 19:29:22 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-07-24 22:49:55 +00:00
|
|
|
)
|
2015-04-02 00:59:41 +00:00
|
|
|
|
|
|
|
func (c *Sys) ListPolicies() ([]string, error) {
|
2018-10-29 17:11:19 +00:00
|
|
|
r := c.c.NewRequest("LIST", "/v1/sys/policies/acl")
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2015-04-02 00:59:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-08-08 20:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 19:29:22 +00:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
2016-08-08 20:00:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
var result []string
|
2018-10-29 17:11:19 +00:00
|
|
|
err = mapstructure.Decode(secret.Data["keys"], &result)
|
2018-08-14 19:29:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-08 20:00:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
return result, err
|
2015-04-02 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GetPolicy(name string) (string, error) {
|
2018-08-14 19:29:22 +00:00
|
|
|
r := c.c.NewRequest("GET", fmt.Sprintf("/v1/sys/policies/acl/%s", name))
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2015-07-13 09:20:00 +00:00
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
2015-04-02 00:59:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-08-08 20:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-08-14 19:29:22 +00:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return "", errors.New("data from server response is empty")
|
2017-10-23 20:52:56 +00:00
|
|
|
}
|
2018-08-14 19:29:22 +00:00
|
|
|
|
|
|
|
if policyRaw, ok := secret.Data["policy"]; ok {
|
2017-10-23 20:52:56 +00:00
|
|
|
return policyRaw.(string), nil
|
2016-08-08 20:00:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 20:52:56 +00:00
|
|
|
return "", fmt.Errorf("no policy found in response")
|
2015-04-02 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) PutPolicy(name, rules string) error {
|
|
|
|
body := map[string]string{
|
2018-10-29 17:11:19 +00:00
|
|
|
"policy": rules,
|
2015-04-02 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 17:11:19 +00:00
|
|
|
r := c.c.NewRequest("PUT", fmt.Sprintf("/v1/sys/policies/acl/%s", name))
|
2015-04-02 00:59:41 +00:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2015-04-02 00:59:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) DeletePolicy(name string) error {
|
2018-10-29 17:11:19 +00:00
|
|
|
r := c.c.NewRequest("DELETE", fmt.Sprintf("/v1/sys/policies/acl/%s", name))
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2015-04-02 00:59:41 +00:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type getPoliciesResp struct {
|
|
|
|
Rules string `json:"rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type listPoliciesResp struct {
|
|
|
|
Policies []string `json:"policies"`
|
|
|
|
}
|