1
0
vault-redux/api/sys_capabilities.go

55 lines
1.0 KiB
Go
Raw Normal View History

2016-03-02 21:42:32 +03:00
package api
import (
"context"
"errors"
"fmt"
"github.com/mitchellh/mapstructure"
)
2016-03-02 21:42:32 +03:00
2016-08-08 23:00:31 +03:00
func (c *Sys) CapabilitiesSelf(path string) ([]string, error) {
return c.Capabilities(c.c.Token(), path)
2016-03-02 21:42:32 +03:00
}
func (c *Sys) Capabilities(token, path string) ([]string, error) {
2016-03-02 21:42:32 +03:00
body := map[string]string{
"token": token,
"path": path,
}
2016-08-08 23:00:31 +03:00
reqPath := "/v1/sys/capabilities"
if token == c.c.Token() {
reqPath = fmt.Sprintf("%s-self", reqPath)
}
r := c.c.NewRequest("POST", reqPath)
2016-03-02 21:42:32 +03:00
if err := r.SetJSONBody(body); err != nil {
return nil, err
}
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
resp, err := c.c.RawRequestWithContext(ctx, r)
2016-03-02 21:42:32 +03:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}
var res []string
err = mapstructure.Decode(secret.Data[path], &res)
if err != nil {
return nil, err
}
return res, nil
2016-03-02 21:42:32 +03:00
}