2014-08-08 21:36:09 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCache_GetPolicy(t *testing.T) {
|
2014-08-12 17:35:27 +00:00
|
|
|
c, err := NewCache(1, nil)
|
2014-08-08 21:36:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := c.GetPolicy("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get the same policy
|
|
|
|
p1, err := c.GetPolicy("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if p != p1 {
|
|
|
|
t.Fatalf("should be cached")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache a new policy
|
|
|
|
_, err = c.GetPolicy(testSimplePolicy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test invalidation of p
|
|
|
|
p3, err := c.GetPolicy("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if p == p3 {
|
|
|
|
t.Fatalf("should be not cached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_GetACL(t *testing.T) {
|
|
|
|
policies := map[string]string{
|
|
|
|
"foo": testSimplePolicy,
|
2014-08-09 00:37:13 +00:00
|
|
|
"bar": testSimplePolicy2,
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
2014-08-12 17:35:27 +00:00
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
return "deny", policies[id], nil
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
c, err := NewCache(1, faultfn)
|
2014-08-08 21:36:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl.KeyRead("bar/test") {
|
|
|
|
t.Fatalf("should deny")
|
|
|
|
}
|
|
|
|
if !acl.KeyRead("foo/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
|
|
|
|
acl2, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl != acl2 {
|
|
|
|
t.Fatalf("should be cached")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate cache
|
|
|
|
_, err = c.GetACL("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl3, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl == acl3 {
|
|
|
|
t.Fatalf("should not be cached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCache_ClearACL(t *testing.T) {
|
|
|
|
policies := map[string]string{
|
|
|
|
"foo": testSimplePolicy,
|
|
|
|
"bar": testSimplePolicy,
|
|
|
|
}
|
2014-08-12 17:35:27 +00:00
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
return "deny", policies[id], nil
|
2014-08-08 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
c, err := NewCache(1, faultfn)
|
2014-08-08 21:36:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the cache
|
|
|
|
c.ClearACL("foo")
|
|
|
|
|
2014-08-09 00:37:13 +00:00
|
|
|
// Clear the policy cache
|
2014-08-15 02:32:05 +00:00
|
|
|
c.policyCache.Purge()
|
2014-08-09 00:37:13 +00:00
|
|
|
|
2014-08-08 21:36:09 +00:00
|
|
|
acl2, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl == acl2 {
|
|
|
|
t.Fatalf("should not be cached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:44:23 +00:00
|
|
|
func TestCache_Purge(t *testing.T) {
|
|
|
|
policies := map[string]string{
|
|
|
|
"foo": testSimplePolicy,
|
|
|
|
"bar": testSimplePolicy,
|
|
|
|
}
|
2014-08-12 17:35:27 +00:00
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
return "deny", policies[id], nil
|
2014-08-09 00:44:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
c, err := NewCache(1, faultfn)
|
2014-08-09 00:44:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the cache
|
|
|
|
c.Purge()
|
|
|
|
c.policyCache.Purge()
|
|
|
|
|
|
|
|
acl2, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl == acl2 {
|
|
|
|
t.Fatalf("should not be cached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:25:11 +00:00
|
|
|
func TestCache_GetACLPolicy(t *testing.T) {
|
|
|
|
policies := map[string]string{
|
|
|
|
"foo": testSimplePolicy,
|
|
|
|
"bar": testSimplePolicy,
|
|
|
|
}
|
2014-08-12 17:35:27 +00:00
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
return "deny", policies[id], nil
|
2014-08-08 22:25:11 +00:00
|
|
|
}
|
2014-08-12 17:35:27 +00:00
|
|
|
c, err := NewCache(1, faultfn)
|
2014-08-08 22:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := c.GetPolicy(testSimplePolicy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:45:28 +00:00
|
|
|
parent, p2, err := c.GetACLPolicy("foo")
|
2014-08-08 22:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-08-12 17:45:28 +00:00
|
|
|
if parent != "deny" {
|
|
|
|
t.Fatalf("bad: %v", parent)
|
|
|
|
}
|
2014-08-08 22:25:11 +00:00
|
|
|
|
|
|
|
if p2 != p {
|
|
|
|
t.Fatalf("expected cached policy")
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:45:28 +00:00
|
|
|
parent, p3, err := c.GetACLPolicy("bar")
|
2014-08-08 22:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-08-12 17:45:28 +00:00
|
|
|
if parent != "deny" {
|
|
|
|
t.Fatalf("bad: %v", parent)
|
|
|
|
}
|
2014-08-08 22:25:11 +00:00
|
|
|
|
|
|
|
if p3 != p {
|
|
|
|
t.Fatalf("expected cached policy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:35:27 +00:00
|
|
|
func TestCache_GetACL_Parent(t *testing.T) {
|
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
switch id {
|
|
|
|
case "foo":
|
|
|
|
// Foo inherits from bar
|
|
|
|
return "bar", testSimplePolicy, nil
|
|
|
|
case "bar":
|
|
|
|
return "deny", testSimplePolicy2, nil
|
|
|
|
}
|
|
|
|
t.Fatalf("bad case")
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewCache(1, faultfn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !acl.KeyRead("bar/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
if !acl.KeyRead("foo/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:32:05 +00:00
|
|
|
func TestCache_GetACL_ParentCache(t *testing.T) {
|
|
|
|
// Same rules, different parent
|
|
|
|
faultfn := func(id string) (string, string, error) {
|
|
|
|
switch id {
|
|
|
|
case "foo":
|
|
|
|
return "allow", testSimplePolicy, nil
|
|
|
|
case "bar":
|
|
|
|
return "deny", testSimplePolicy, nil
|
|
|
|
}
|
|
|
|
t.Fatalf("bad case")
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewCache(16, faultfn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
acl, err := c.GetACL("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !acl.KeyRead("bar/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
if !acl.KeyRead("foo/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
|
|
|
|
acl2, err := c.GetACL("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl == acl2 {
|
|
|
|
t.Fatalf("should not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if acl2.KeyRead("bar/test") {
|
|
|
|
t.Fatalf("should not allow")
|
|
|
|
}
|
|
|
|
if !acl2.KeyRead("foo/test") {
|
|
|
|
t.Fatalf("should allow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:36:09 +00:00
|
|
|
var testSimplePolicy = `
|
|
|
|
key "foo/" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`
|
2014-08-09 00:37:13 +00:00
|
|
|
|
|
|
|
var testSimplePolicy2 = `
|
|
|
|
key "bar/" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`
|