open-vault/vault/capabilities.go

51 lines
1006 B
Go
Raw Normal View History

2016-03-03 02:32:52 +00:00
package vault
import (
"fmt"
"github.com/hashicorp/errwrap"
)
2016-03-03 02:32:52 +00:00
// Capabilities is used to fetch the capabilities of the given token on the given path
func (c *Core) Capabilities(token, path string) ([]string, error) {
2016-03-03 02:32:52 +00:00
if path == "" {
return nil, errwrap.Wrapf("{{err}}", fmt.Errorf("missing path"))
2016-03-03 02:32:52 +00:00
}
if token == "" {
return nil, errwrap.Wrapf("{{err}}", fmt.Errorf("missing token"))
2016-03-03 02:32:52 +00:00
}
te, err := c.tokenStore.Lookup(token)
if err != nil {
return nil, err
}
if te == nil {
return nil, errwrap.Wrapf("{{err}}", fmt.Errorf("invalid token"))
2016-03-03 02:32:52 +00:00
}
if te.Policies == nil {
return []string{DenyCapability}, nil
2016-03-03 02:32:52 +00:00
}
var policies []*Policy
2016-03-03 02:32:52 +00:00
for _, tePolicy := range te.Policies {
policy, err := c.policyStore.GetPolicy(tePolicy)
if err != nil {
return nil, err
}
policies = append(policies, policy)
}
if len(policies) == 0 {
return []string{DenyCapability}, nil
}
acl, err := NewACL(policies)
if err != nil {
return nil, err
}
return acl.Capabilities(path), nil
2016-03-03 02:32:52 +00:00
}