open-vault/vault/policy_store_test.go

202 lines
4.4 KiB
Go
Raw Normal View History

2015-03-18 19:17:03 +00:00
package vault
import (
"reflect"
"testing"
"github.com/hashicorp/vault/logical"
2015-03-18 19:17:03 +00:00
)
func mockPolicyStore(t *testing.T) *PolicyStore {
_, barrier, _ := mockBarrier(t)
view := NewBarrierView(barrier, "foo/")
p := NewPolicyStore(view, logical.TestSystemView())
return p
}
func mockPolicyStoreNoCache(t *testing.T) *PolicyStore {
sysView := logical.TestSystemView()
sysView.CachingDisabledVal = true
_, barrier, _ := mockBarrier(t)
view := NewBarrierView(barrier, "foo/")
p := NewPolicyStore(view, sysView)
2015-03-18 19:17:03 +00:00
return p
}
2015-03-24 18:27:21 +00:00
func TestPolicyStore_Root(t *testing.T) {
ps := mockPolicyStore(t)
// Get should return a special policy
2017-10-23 18:59:37 +00:00
p, err := ps.GetPolicy("root", PolicyTypeToken)
2015-03-24 18:27:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if p == nil {
t.Fatalf("bad: %v", p)
}
if p.Name != "root" {
t.Fatalf("bad: %v", p)
}
// Set should fail
err = ps.SetPolicy(p)
if err.Error() != "cannot update root policy" {
t.Fatalf("err: %v", err)
}
// Delete should fail
2017-10-23 18:59:37 +00:00
err = ps.DeletePolicy("root", PolicyTypeACL)
2015-03-24 18:27:21 +00:00
if err.Error() != "cannot delete root policy" {
t.Fatalf("err: %v", err)
}
}
2015-03-18 19:17:03 +00:00
func TestPolicyStore_CRUD(t *testing.T) {
ps := mockPolicyStore(t)
testPolicyStore_CRUD(t, ps)
ps = mockPolicyStoreNoCache(t)
testPolicyStore_CRUD(t, ps)
}
2015-03-18 19:17:03 +00:00
func testPolicyStore_CRUD(t *testing.T, ps *PolicyStore) {
2015-03-18 19:17:03 +00:00
// Get should return nothing
2017-10-23 18:59:37 +00:00
p, err := ps.GetPolicy("Dev", PolicyTypeToken)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if p != nil {
t.Fatalf("bad: %v", p)
}
// Delete should be no-op
2017-10-23 18:59:37 +00:00
err = ps.DeletePolicy("deV", PolicyTypeACL)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
// List should be blank
2017-10-23 18:59:37 +00:00
out, err := ps.ListPolicies(PolicyTypeACL)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 0 {
t.Fatalf("bad: %v", out)
}
// Set should work
2017-10-23 18:59:37 +00:00
policy, _ := ParseACLPolicy(aclPolicy)
2015-03-18 19:17:03 +00:00
err = ps.SetPolicy(policy)
if err != nil {
t.Fatalf("err: %v", err)
}
// Get should work
2017-10-23 18:59:37 +00:00
p, err = ps.GetPolicy("dEv", PolicyTypeToken)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(p, policy) {
t.Fatalf("bad: %v", p)
}
// List should be one element
2017-10-23 18:59:37 +00:00
out, err = ps.ListPolicies(PolicyTypeACL)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 1 || out[0] != "dev" {
t.Fatalf("bad: %v", out)
}
// Delete should be clear the entry
2017-10-23 18:59:37 +00:00
err = ps.DeletePolicy("Dev", PolicyTypeACL)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
// Get should fail
2017-10-23 18:59:37 +00:00
p, err = ps.GetPolicy("deV", PolicyTypeToken)
2015-03-18 19:17:03 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if p != nil {
t.Fatalf("bad: %v", p)
}
}
// Test predefined policy handling
func TestPolicyStore_Predefined(t *testing.T) {
core, _, _ := TestCoreUnsealed(t)
// Ensure both default policies are created
err := core.setupPolicyStore()
if err != nil {
t.Fatalf("err: %v", err)
}
// List should be two elements
2017-10-23 18:59:37 +00:00
out, err := core.policyStore.ListPolicies(PolicyTypeACL)
if err != nil {
t.Fatalf("err: %v", err)
}
2016-07-25 19:59:02 +00:00
// This shouldn't contain response-wrapping since it's non-assignable
if len(out) != 1 || out[0] != "default" {
t.Fatalf("bad: %v", out)
}
2017-10-23 18:59:37 +00:00
pCubby, err := core.policyStore.GetPolicy("response-wrapping", PolicyTypeToken)
if err != nil {
t.Fatalf("err: %v", err)
}
2017-10-23 18:59:37 +00:00
if pCubby == nil {
t.Fatal("nil cubby policy")
}
2016-09-29 04:01:28 +00:00
if pCubby.Raw != responseWrappingPolicy {
t.Fatalf("bad: expected\n%s\ngot\n%s\n", responseWrappingPolicy, pCubby.Raw)
}
2017-10-23 18:59:37 +00:00
pRoot, err := core.policyStore.GetPolicy("root", PolicyTypeToken)
if err != nil {
t.Fatalf("err: %v", err)
}
2017-10-23 18:59:37 +00:00
if pRoot == nil {
t.Fatal("nil root policy")
}
err = core.policyStore.SetPolicy(pCubby)
if err == nil {
t.Fatalf("expected err setting %s", pCubby.Name)
}
err = core.policyStore.SetPolicy(pRoot)
if err == nil {
t.Fatalf("expected err setting %s", pRoot.Name)
}
2017-10-23 18:59:37 +00:00
err = core.policyStore.DeletePolicy(pCubby.Name, PolicyTypeACL)
if err == nil {
t.Fatalf("expected err deleting %s", pCubby.Name)
}
2017-10-23 18:59:37 +00:00
err = core.policyStore.DeletePolicy(pRoot.Name, PolicyTypeACL)
if err == nil {
t.Fatalf("expected err deleting %s", pRoot.Name)
}
}
2015-03-18 19:17:03 +00:00
func TestPolicyStore_ACL(t *testing.T) {
ps := mockPolicyStore(t)
2017-10-23 18:59:37 +00:00
policy, _ := ParseACLPolicy(aclPolicy)
2015-03-18 19:17:03 +00:00
err := ps.SetPolicy(policy)
if err != nil {
t.Fatalf("err: %v", err)
}
2017-10-23 18:59:37 +00:00
policy, _ = ParseACLPolicy(aclPolicy2)
2015-03-18 19:17:03 +00:00
err = ps.SetPolicy(policy)
if err != nil {
t.Fatalf("err: %v", err)
}
acl, err := ps.ACL("dev", "ops")
if err != nil {
t.Fatalf("err: %v", err)
}
testLayeredACL(t, acl)
}