2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-03-02 18:42:32 +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"
|
2022-03-24 17:58:03 +00:00
|
|
|
"net/http"
|
2018-08-14 19:29:22 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-07-24 22:49:55 +00:00
|
|
|
)
|
2016-03-02 18:42:32 +00:00
|
|
|
|
2016-08-08 20:00:31 +00:00
|
|
|
func (c *Sys) CapabilitiesSelf(path string) ([]string, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CapabilitiesSelfWithContext(context.Background(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) CapabilitiesSelfWithContext(ctx context.Context, path string) ([]string, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
return c.CapabilitiesWithContext(ctx, c.c.Token(), path)
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 18:21:07 +00:00
|
|
|
func (c *Sys) Capabilities(token, path string) ([]string, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CapabilitiesWithContext(context.Background(), token, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) CapabilitiesWithContext(ctx context.Context, token, path string) ([]string, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2016-03-02 18:42:32 +00:00
|
|
|
body := map[string]string{
|
|
|
|
"token": token,
|
|
|
|
"path": path,
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:00:31 +00:00
|
|
|
reqPath := "/v1/sys/capabilities"
|
|
|
|
if token == c.c.Token() {
|
|
|
|
reqPath = fmt.Sprintf("%s-self", reqPath)
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, reqPath)
|
2016-03-02 18:42:32 +00:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-03-02 18:42:32 +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-03-17 19:14:17 +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")
|
2018-05-11 15:58:12 +00:00
|
|
|
}
|
2018-08-14 19:29:22 +00:00
|
|
|
|
|
|
|
var res []string
|
|
|
|
err = mapstructure.Decode(secret.Data[path], &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-17 19:14:17 +00:00
|
|
|
}
|
2018-08-14 19:29:22 +00:00
|
|
|
|
2018-09-19 17:06:37 +00:00
|
|
|
if len(res) == 0 {
|
|
|
|
_, ok := secret.Data["capabilities"]
|
|
|
|
if ok {
|
|
|
|
err = mapstructure.Decode(secret.Data["capabilities"], &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
return res, nil
|
2016-03-02 18:42:32 +00:00
|
|
|
}
|