open-vault/vault/acl_test.go

206 lines
4.9 KiB
Go
Raw Normal View History

2015-03-18 01:31:20 +00:00
package vault
import (
"testing"
"github.com/hashicorp/vault/logical"
)
func TestACL_Root(t *testing.T) {
// Create the root policy ACL
policy := []*Policy{&Policy{Name: "root"}}
acl, err := NewACL(policy)
if err != nil {
t.Fatalf("err: %v", err)
}
allowed, rootPrivs := acl.AllowOperation(logical.UpdateOperation, "sys/mount/foo")
if !rootPrivs {
2015-03-18 01:31:20 +00:00
t.Fatalf("expected root")
}
if !allowed {
2015-03-18 01:31:20 +00:00
t.Fatalf("expected permission")
}
}
func TestACL_Single(t *testing.T) {
policy, err := Parse(aclPolicy)
if err != nil {
t.Fatalf("err: %v", err)
}
acl, err := NewACL([]*Policy{policy})
if err != nil {
t.Fatalf("err: %v", err)
}
// Type of operation is not important here as we only care about checking
// sudo/root
_, rootPrivs := acl.AllowOperation(logical.ReadOperation, "sys/mount/foo")
if rootPrivs {
2015-03-18 01:31:20 +00:00
t.Fatalf("unexpected root")
}
type tcase struct {
op logical.Operation
path string
allowed bool
rootPrivs bool
2015-03-18 01:31:20 +00:00
}
tcases := []tcase{
{logical.ReadOperation, "root", false, false},
{logical.HelpOperation, "root", true, false},
2015-03-18 01:31:20 +00:00
{logical.ReadOperation, "dev/foo", true, true},
{logical.UpdateOperation, "dev/foo", true, true},
2015-03-18 01:31:20 +00:00
{logical.DeleteOperation, "stage/foo", true, false},
{logical.ListOperation, "stage/aws/foo", true, true},
{logical.UpdateOperation, "stage/aws/foo", true, true},
{logical.UpdateOperation, "stage/aws/policy/foo", true, true},
2015-03-18 01:31:20 +00:00
{logical.DeleteOperation, "prod/foo", false, false},
{logical.UpdateOperation, "prod/foo", false, false},
{logical.ReadOperation, "prod/foo", true, false},
{logical.ListOperation, "prod/foo", true, false},
{logical.ReadOperation, "prod/aws/foo", false, false},
{logical.ReadOperation, "foo/bar", true, true},
{logical.ListOperation, "foo/bar", false, true},
{logical.UpdateOperation, "foo/bar", false, true},
{logical.CreateOperation, "foo/bar", true, true},
2015-03-18 01:31:20 +00:00
}
for _, tc := range tcases {
allowed, rootPrivs := acl.AllowOperation(tc.op, tc.path)
if allowed != tc.allowed {
t.Fatalf("bad: case %#v: %v, %v", tc, allowed, rootPrivs)
}
if rootPrivs != tc.rootPrivs {
t.Fatalf("bad: case %#v: %v, %v", tc, allowed, rootPrivs)
2015-03-18 01:31:20 +00:00
}
}
}
func TestACL_Layered(t *testing.T) {
policy1, err := Parse(aclPolicy)
if err != nil {
t.Fatalf("err: %v", err)
}
policy2, err := Parse(aclPolicy2)
if err != nil {
t.Fatalf("err: %v", err)
}
acl, err := NewACL([]*Policy{policy1, policy2})
if err != nil {
t.Fatalf("err: %v", err)
}
2015-03-18 19:17:03 +00:00
testLayeredACL(t, acl)
}
2015-03-18 01:31:20 +00:00
2015-03-18 19:17:03 +00:00
func testLayeredACL(t *testing.T, acl *ACL) {
// Type of operation is not important here as we only care about checking
// sudo/root
_, rootPrivs := acl.AllowOperation(logical.ReadOperation, "sys/mount/foo")
if rootPrivs {
2015-03-18 01:31:20 +00:00
t.Fatalf("unexpected root")
}
type tcase struct {
op logical.Operation
path string
allowed bool
rootPrivs bool
2015-03-18 01:31:20 +00:00
}
tcases := []tcase{
{logical.ReadOperation, "root", false, false},
{logical.HelpOperation, "root", true, false},
2015-03-18 01:31:20 +00:00
{logical.ReadOperation, "dev/foo", true, true},
{logical.UpdateOperation, "dev/foo", true, true},
{logical.ReadOperation, "dev/hide/foo", false, false},
{logical.UpdateOperation, "dev/hide/foo", false, false},
2015-03-18 01:31:20 +00:00
{logical.DeleteOperation, "stage/foo", true, false},
{logical.ListOperation, "stage/aws/foo", true, true},
{logical.UpdateOperation, "stage/aws/foo", true, true},
{logical.UpdateOperation, "stage/aws/policy/foo", false, false},
2015-03-18 01:31:20 +00:00
{logical.DeleteOperation, "prod/foo", true, false},
{logical.UpdateOperation, "prod/foo", true, false},
{logical.ReadOperation, "prod/foo", true, false},
{logical.ListOperation, "prod/foo", true, false},
{logical.ReadOperation, "prod/aws/foo", false, false},
2015-07-05 23:34:34 +00:00
{logical.ReadOperation, "sys/status", false, false},
{logical.UpdateOperation, "sys/seal", true, true},
{logical.ReadOperation, "foo/bar", false, false},
{logical.ListOperation, "foo/bar", false, false},
{logical.UpdateOperation, "foo/bar", false, false},
{logical.CreateOperation, "foo/bar", false, false},
2015-03-18 01:31:20 +00:00
}
for _, tc := range tcases {
allowed, rootPrivs := acl.AllowOperation(tc.op, tc.path)
if allowed != tc.allowed {
t.Fatalf("bad: case %#v: %v, %v", tc, allowed, rootPrivs)
}
if rootPrivs != tc.rootPrivs {
t.Fatalf("bad: case %#v: %v, %v", tc, allowed, rootPrivs)
2015-03-18 01:31:20 +00:00
}
}
}
var aclPolicy = `
name = "dev"
path "dev/*" {
2015-03-18 01:31:20 +00:00
policy = "sudo"
}
path "stage/*" {
2015-03-18 01:31:20 +00:00
policy = "write"
}
path "stage/aws/*" {
2015-03-18 01:31:20 +00:00
policy = "read"
capabilities = ["update", "sudo"]
2015-03-18 01:31:20 +00:00
}
path "stage/aws/policy/*" {
2015-03-18 01:31:20 +00:00
policy = "sudo"
}
path "prod/*" {
2015-03-18 01:31:20 +00:00
policy = "read"
}
path "prod/aws/*" {
2015-03-18 01:31:20 +00:00
policy = "deny"
}
2015-07-05 23:34:34 +00:00
path "sys/*" {
policy = "deny"
}
path "foo/bar" {
capabilities = ["read", "create", "sudo"]
}
2015-03-18 01:31:20 +00:00
`
var aclPolicy2 = `
name = "ops"
path "dev/hide/*" {
2015-03-18 01:31:20 +00:00
policy = "deny"
}
path "stage/aws/policy/*" {
2015-03-18 01:31:20 +00:00
policy = "deny"
# This should have no effect
capabilities = ["read", "update", "sudo"]
2015-03-18 01:31:20 +00:00
}
path "prod/*" {
2015-03-18 01:31:20 +00:00
policy = "write"
}
2015-07-05 23:34:34 +00:00
path "sys/seal" {
policy = "sudo"
}
path "foo/bar" {
capabilities = ["deny"]
2015-07-05 23:34:34 +00:00
}
2015-03-18 01:31:20 +00:00
`