open-vault/vault/capabilities.go

59 lines
1.4 KiB
Go
Raw Normal View History

2016-03-03 02:32:52 +00:00
package vault
import (
"context"
"sort"
2016-03-18 16:40:17 +00:00
"github.com/hashicorp/vault/logical"
)
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(ctx context.Context, token, path string) ([]string, error) {
2016-03-03 02:32:52 +00:00
if path == "" {
return nil, &logical.StatusBadRequest{Err: "missing path"}
2016-03-03 02:32:52 +00:00
}
if token == "" {
return nil, &logical.StatusBadRequest{Err: "missing token"}
2016-03-03 02:32:52 +00:00
}
te, err := c.tokenStore.Lookup(ctx, token)
2016-03-03 02:32:52 +00:00
if err != nil {
return nil, err
}
if te == nil {
return nil, &logical.StatusBadRequest{Err: "invalid token"}
2016-03-03 02:32:52 +00:00
}
// Start with token entry policies
policies := te.Policies
// Fetch entity and entity group policies
entity, derivedPolicies, err := c.fetchEntityAndDerivedPolicies(te.EntityID)
if err != nil {
return nil, err
}
if entity != nil && entity.Disabled {
c.logger.Warn("permission denied as the entity on the token is disabled")
return nil, logical.ErrPermissionDenied
}
if te.EntityID != "" && entity == nil {
c.logger.Warn("permission denied as the entity on the token is invalid")
return nil, logical.ErrPermissionDenied
}
policies = append(policies, derivedPolicies...)
if len(policies) == 0 {
return []string{DenyCapability}, nil
}
acl, err := c.policyStore.ACL(ctx, entity, policies...)
if err != nil {
return nil, err
}
2016-03-18 16:40:17 +00:00
capabilities := acl.Capabilities(path)
sort.Strings(capabilities)
return capabilities, nil
2016-03-03 02:32:52 +00:00
}