2015-03-18 01:31:20 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/armon/go-radix"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
2015-07-05 23:31:30 +00:00
|
|
|
var (
|
|
|
|
// permittedPolicyLevel is used to map each logical operation
|
2016-01-07 20:10:05 +00:00
|
|
|
// into the set of capabilities that allow the operation.
|
2015-07-05 23:31:30 +00:00
|
|
|
permittedPolicyLevels = map[logical.Operation][]string{
|
2016-01-07 20:10:05 +00:00
|
|
|
logical.CreateOperation: []string{CreateCapability},
|
|
|
|
logical.ReadOperation: []string{ReadCapability},
|
|
|
|
logical.UpdateOperation: []string{UpdateCapability},
|
|
|
|
logical.DeleteOperation: []string{DeleteCapability},
|
|
|
|
logical.ListOperation: []string{ListCapability},
|
|
|
|
logical.HelpOperation: []string{CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability, DenyCapability},
|
|
|
|
logical.RevokeOperation: []string{UpdateCapability},
|
|
|
|
logical.RenewOperation: []string{UpdateCapability},
|
|
|
|
logical.RollbackOperation: []string{UpdateCapability},
|
2015-07-05 23:31:30 +00:00
|
|
|
}
|
|
|
|
)
|
2015-03-18 01:31:20 +00:00
|
|
|
|
|
|
|
// ACL is used to wrap a set of policies to provide
|
|
|
|
// an efficient interface for access control.
|
|
|
|
type ACL struct {
|
2015-07-05 23:31:30 +00:00
|
|
|
// exactRules contains the path policies that are exact
|
|
|
|
exactRules *radix.Tree
|
|
|
|
|
|
|
|
// globRules contains the path policies that glob
|
|
|
|
globRules *radix.Tree
|
2015-03-18 01:31:20 +00:00
|
|
|
|
|
|
|
// root is enabled if the "root" named policy is present.
|
|
|
|
root bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New is used to construct a policy based ACL from a set of policies.
|
|
|
|
func NewACL(policies []*Policy) (*ACL, error) {
|
|
|
|
// Initialize
|
|
|
|
a := &ACL{
|
2015-07-05 23:31:30 +00:00
|
|
|
exactRules: radix.New(),
|
|
|
|
globRules: radix.New(),
|
|
|
|
root: false,
|
2015-03-18 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inject each policy
|
|
|
|
for _, policy := range policies {
|
2015-03-24 22:49:17 +00:00
|
|
|
// Ignore a nil policy object
|
|
|
|
if policy == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-18 01:31:20 +00:00
|
|
|
// Check if this is root
|
|
|
|
if policy.Name == "root" {
|
|
|
|
a.root = true
|
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
for _, pc := range policy.Paths {
|
2015-07-05 23:31:30 +00:00
|
|
|
// Check which tree to use
|
|
|
|
tree := a.exactRules
|
2016-01-07 20:10:05 +00:00
|
|
|
if pc.Glob {
|
2015-07-05 23:31:30 +00:00
|
|
|
tree = a.globRules
|
|
|
|
}
|
2015-03-18 01:31:20 +00:00
|
|
|
|
|
|
|
// Check for an existing policy
|
2016-01-07 20:10:05 +00:00
|
|
|
raw, ok := tree.Get(pc.Prefix)
|
2015-03-18 01:31:20 +00:00
|
|
|
if !ok {
|
2016-01-07 20:10:05 +00:00
|
|
|
tree.Insert(pc.Prefix, pc)
|
2015-03-18 01:31:20 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
existing := raw.(*PathCapabilities)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case existing.CapabilitiesMap[DenyCapability]:
|
|
|
|
// If we are explicitly denied in the existing capability set,
|
|
|
|
// don't save anything else
|
|
|
|
|
|
|
|
case pc.CapabilitiesMap[DenyCapability]:
|
|
|
|
// If this new policy explicitly denies, only save the deny value
|
|
|
|
tree.Insert(pc.Prefix, pc)
|
2015-03-18 01:31:20 +00:00
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
default:
|
|
|
|
// Insert the capabilities in this new policy into the existing
|
|
|
|
// value; since it's a pointer we can just modify the
|
|
|
|
// underlying data
|
|
|
|
|
|
|
|
for k, _ := range pc.CapabilitiesMap {
|
|
|
|
existing.CapabilitiesMap[k] = true
|
|
|
|
}
|
2015-03-18 01:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllowOperation is used to check if the given operation is permitted
|
2016-01-07 20:10:05 +00:00
|
|
|
func (a *ACL) AllowOperation(op logical.Operation, path string) (opAllowed bool, sudoPriv bool) {
|
2015-03-18 01:31:20 +00:00
|
|
|
// Fast-path root
|
|
|
|
if a.root {
|
2016-01-07 20:10:05 +00:00
|
|
|
return true, true
|
2015-03-18 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
// Help is always allowed
|
|
|
|
if op == logical.HelpOperation {
|
|
|
|
return true, false
|
2015-07-05 23:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find an exact matching rule, look for glob if no match
|
2016-01-07 20:10:05 +00:00
|
|
|
var policy *PathCapabilities
|
2015-07-05 23:31:30 +00:00
|
|
|
raw, ok := a.exactRules.Get(path)
|
2015-03-18 01:31:20 +00:00
|
|
|
if ok {
|
2016-01-07 20:10:05 +00:00
|
|
|
policy = raw.(*PathCapabilities)
|
2015-07-05 23:31:30 +00:00
|
|
|
goto CHECK
|
2015-03-18 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:31:30 +00:00
|
|
|
// Find a glob rule, default deny if no match
|
|
|
|
_, raw, ok = a.globRules.LongestPrefix(path)
|
|
|
|
if !ok {
|
2016-01-07 20:10:05 +00:00
|
|
|
return false, false
|
2015-07-05 23:31:30 +00:00
|
|
|
} else {
|
2016-01-07 20:10:05 +00:00
|
|
|
policy = raw.(*PathCapabilities)
|
2015-07-05 23:31:30 +00:00
|
|
|
}
|
2015-03-18 01:31:20 +00:00
|
|
|
|
2015-07-05 23:31:30 +00:00
|
|
|
CHECK:
|
2015-03-18 01:31:20 +00:00
|
|
|
// Check if the minimum permissions are met
|
2016-01-07 20:10:05 +00:00
|
|
|
// If "deny" has been explicitly set, only deny will be in the map, so we
|
|
|
|
// only need to check for the existence of other values
|
|
|
|
return policy.CapabilitiesMap[op.String()], policy.CapabilitiesMap[SudoCapability]
|
2015-03-18 01:31:20 +00:00
|
|
|
}
|