mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 02:02:43 +00:00
Command: token capabilities using accessor (#24479)
* Command: token capabilities using accessor * release note * Apply suggestions from code review Co-authored-by: Marc Boudreau <marc.boudreau@hashicorp.com> --------- Co-authored-by: Marc Boudreau <marc.boudreau@hashicorp.com>
This commit is contained in:
committed by
GitHub
parent
dc5c3e8d97
commit
e4ffe8979c
@@ -78,3 +78,56 @@ func (c *Sys) CapabilitiesWithContext(ctx context.Context, token, path string) (
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *Sys) CapabilitiesAccessor(accessor, path string) ([]string, error) {
|
||||
return c.CapabilitiesAccessorWithContext(context.Background(), accessor, path)
|
||||
}
|
||||
|
||||
func (c *Sys) CapabilitiesAccessorWithContext(ctx context.Context, accessor, path string) ([]string, error) {
|
||||
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
||||
defer cancelFunc()
|
||||
|
||||
body := map[string]string{
|
||||
"accessor": accessor,
|
||||
"path": path,
|
||||
}
|
||||
|
||||
reqPath := "/v1/sys/capabilities-accessor"
|
||||
|
||||
r := c.c.NewRequest(http.MethodPost, reqPath)
|
||||
if err := r.SetJSONBody(body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.c.rawRequestWithContext(ctx, r)
|
||||
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
|
||||
}
|
||||
|
||||
if len(res) == 0 {
|
||||
_, ok := secret.Data["capabilities"]
|
||||
if ok {
|
||||
err = mapstructure.Decode(secret.Data["capabilities"], &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user