2014-08-08 21:36:09 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
|
2017-09-14 19:31:01 +00:00
|
|
|
"github.com/hashicorp/consul/sentinel"
|
2014-08-08 21:36:09 +00:00
|
|
|
"github.com/hashicorp/golang-lru"
|
|
|
|
)
|
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
// FaultFunc is a function used to fault in the parent,
|
2016-08-04 00:01:32 +00:00
|
|
|
// rules for an ACL given its ID
|
2014-08-12 17:35:27 +00:00
|
|
|
type FaultFunc func(id string) (string, string, error)
|
2014-08-08 21:36:09 +00:00
|
|
|
|
2014-08-08 22:25:11 +00:00
|
|
|
// aclEntry allows us to store the ACL with it's policy ID
|
|
|
|
type aclEntry struct {
|
2014-08-15 02:32:05 +00:00
|
|
|
ACL ACL
|
|
|
|
Parent string
|
|
|
|
RuleID string
|
2014-08-08 22:25:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 21:36:09 +00:00
|
|
|
// Cache is used to implement policy and ACL caching
|
|
|
|
type Cache struct {
|
|
|
|
faultfn FaultFunc
|
2016-08-09 18:00:22 +00:00
|
|
|
aclCache *lru.TwoQueueCache // Cache id -> acl
|
|
|
|
policyCache *lru.TwoQueueCache // Cache policy -> acl
|
|
|
|
ruleCache *lru.TwoQueueCache // Cache rules -> policy
|
2017-09-14 19:31:01 +00:00
|
|
|
sentinel sentinel.Evaluator
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:25:06 +00:00
|
|
|
// NewCache constructs a new policy and ACL cache of a given size
|
2017-09-14 19:31:01 +00:00
|
|
|
func NewCache(size int, faultfn FaultFunc, sentinel sentinel.Evaluator) (*Cache, error) {
|
2014-08-08 21:36:09 +00:00
|
|
|
if size <= 0 {
|
|
|
|
return nil, fmt.Errorf("Must provide positive cache size")
|
|
|
|
}
|
2016-08-09 18:00:22 +00:00
|
|
|
|
|
|
|
rc, err := lru.New2Q(size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := lru.New2Q(size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ac, err := lru.New2Q(size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:36:09 +00:00
|
|
|
c := &Cache{
|
|
|
|
faultfn: faultfn,
|
2014-08-12 17:35:27 +00:00
|
|
|
aclCache: ac,
|
2014-08-08 21:36:09 +00:00
|
|
|
policyCache: pc,
|
2014-08-09 00:37:13 +00:00
|
|
|
ruleCache: rc,
|
2017-09-14 19:31:01 +00:00
|
|
|
sentinel: sentinel,
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicy is used to get a potentially cached policy set.
|
|
|
|
// If not cached, it will be parsed, and then cached.
|
|
|
|
func (c *Cache) GetPolicy(rules string) (*Policy, error) {
|
2016-08-04 00:01:32 +00:00
|
|
|
return c.getPolicy(RuleID(rules), rules)
|
2014-08-08 22:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getPolicy is an internal method to get a cached policy,
|
|
|
|
// but it assumes a pre-computed ID
|
|
|
|
func (c *Cache) getPolicy(id, rules string) (*Policy, error) {
|
2014-08-09 00:37:13 +00:00
|
|
|
raw, ok := c.ruleCache.Get(id)
|
2014-08-08 21:36:09 +00:00
|
|
|
if ok {
|
|
|
|
return raw.(*Policy), nil
|
|
|
|
}
|
2017-09-14 19:31:01 +00:00
|
|
|
policy, err := Parse(rules, c.sentinel)
|
2014-08-08 21:36:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-08 23:51:19 +00:00
|
|
|
policy.ID = id
|
2014-08-09 00:37:13 +00:00
|
|
|
c.ruleCache.Add(id, policy)
|
2014-08-08 21:36:09 +00:00
|
|
|
return policy, nil
|
2014-08-08 22:25:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:01:32 +00:00
|
|
|
// RuleID is used to generate an ID for a rule
|
|
|
|
func RuleID(rules string) string {
|
2014-08-08 22:25:11 +00:00
|
|
|
return fmt.Sprintf("%x", md5.Sum([]byte(rules)))
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:32:05 +00:00
|
|
|
// policyID returns the cache ID for a policy
|
|
|
|
func (c *Cache) policyID(parent, ruleID string) string {
|
|
|
|
return parent + ":" + ruleID
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:25:11 +00:00
|
|
|
// GetACLPolicy is used to get the potentially cached ACL
|
|
|
|
// policy. If not cached, it will be generated and then cached.
|
2014-08-12 17:45:28 +00:00
|
|
|
func (c *Cache) GetACLPolicy(id string) (string, *Policy, error) {
|
2014-08-08 22:25:11 +00:00
|
|
|
// Check for a cached acl
|
|
|
|
if raw, ok := c.aclCache.Get(id); ok {
|
|
|
|
cached := raw.(aclEntry)
|
2014-08-15 02:32:05 +00:00
|
|
|
if raw, ok := c.ruleCache.Get(cached.RuleID); ok {
|
2014-08-12 17:45:28 +00:00
|
|
|
return cached.Parent, raw.(*Policy), nil
|
2014-08-08 22:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fault in the rules
|
2014-08-12 17:45:28 +00:00
|
|
|
parent, rules, err := c.faultfn(id)
|
2014-08-08 22:25:11 +00:00
|
|
|
if err != nil {
|
2014-08-12 17:45:28 +00:00
|
|
|
return "", nil, err
|
2014-08-08 22:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get cached
|
2014-08-12 17:45:28 +00:00
|
|
|
policy, err := c.GetPolicy(rules)
|
|
|
|
return parent, policy, err
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetACL is used to get a potentially cached ACL policy.
|
|
|
|
// If not cached, it will be generated and then cached.
|
|
|
|
func (c *Cache) GetACL(id string) (ACL, error) {
|
|
|
|
// Look for the ACL directly
|
|
|
|
raw, ok := c.aclCache.Get(id)
|
|
|
|
if ok {
|
2014-08-08 22:25:11 +00:00
|
|
|
return raw.(aclEntry).ACL, nil
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the rules
|
2014-08-12 17:35:27 +00:00
|
|
|
parentID, rules, err := c.faultfn(id)
|
2014-08-08 21:36:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-04 00:01:32 +00:00
|
|
|
ruleID := RuleID(rules)
|
2014-08-08 21:36:09 +00:00
|
|
|
|
2014-08-09 00:37:13 +00:00
|
|
|
// Check for a compiled ACL
|
2014-08-15 02:32:05 +00:00
|
|
|
policyID := c.policyID(parentID, ruleID)
|
2014-08-09 00:37:13 +00:00
|
|
|
var compiled ACL
|
2014-08-15 02:32:05 +00:00
|
|
|
if raw, ok := c.policyCache.Get(policyID); ok {
|
2014-08-09 00:37:13 +00:00
|
|
|
compiled = raw.(ACL)
|
|
|
|
} else {
|
|
|
|
// Get the policy
|
|
|
|
policy, err := c.getPolicy(ruleID, rules)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-08 21:36:09 +00:00
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
// Get the parent ACL
|
|
|
|
parent := RootACL(parentID)
|
|
|
|
if parent == nil {
|
|
|
|
parent, err = c.GetACL(parentID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:37:13 +00:00
|
|
|
// Compile the ACL
|
2017-09-14 19:31:01 +00:00
|
|
|
acl, err := New(parent, policy, c.sentinel)
|
2014-08-09 00:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the compiled ACL
|
2014-08-15 02:32:05 +00:00
|
|
|
c.policyCache.Add(policyID, acl)
|
2014-08-09 00:37:13 +00:00
|
|
|
compiled = acl
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache and return the ACL
|
2014-08-12 17:45:28 +00:00
|
|
|
c.aclCache.Add(id, aclEntry{compiled, parentID, ruleID})
|
2014-08-09 00:37:13 +00:00
|
|
|
return compiled, nil
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClearACL is used to clear the ACL cache if any
|
|
|
|
func (c *Cache) ClearACL(id string) {
|
|
|
|
c.aclCache.Remove(id)
|
|
|
|
}
|
2014-08-09 00:44:23 +00:00
|
|
|
|
|
|
|
// Purge is used to clear all the ACL caches. The
|
|
|
|
// rule and policy caches are not purged, since they
|
|
|
|
// are content-hashed anyways.
|
|
|
|
func (c *Cache) Purge() {
|
|
|
|
c.aclCache.Purge()
|
|
|
|
}
|