2015-03-18 19:17:03 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-18 19:17:03 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-07-06 01:14:15 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/vault/helper/logging"
|
2015-07-06 01:14:15 +00:00
|
|
|
"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/")
|
2018-04-03 00:46:59 +00:00
|
|
|
p := NewPolicyStore(context.Background(), nil, view, logical.TestSystemView(), logging.NewVaultLogger(log.Trace))
|
2016-04-21 13:52:42 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockPolicyStoreNoCache(t *testing.T) *PolicyStore {
|
|
|
|
sysView := logical.TestSystemView()
|
2016-04-21 20:32:06 +00:00
|
|
|
sysView.CachingDisabledVal = true
|
2016-04-21 13:52:42 +00:00
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "foo/")
|
2018-04-03 00:46:59 +00:00
|
|
|
p := NewPolicyStore(context.Background(), nil, view, sysView, logging.NewVaultLogger(log.Trace))
|
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
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err := ps.GetPolicy(context.Background(), "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
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.SetPolicy(context.Background(), p)
|
2018-04-06 00:43:29 +00:00
|
|
|
if err.Error() != `cannot update "root" policy` {
|
2015-03-24 18:27:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.DeletePolicy(context.Background(), "root", PolicyTypeACL)
|
2018-04-06 00:43:29 +00:00
|
|
|
if err.Error() != `cannot delete "root" policy` {
|
2015-03-24 18:27:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 19:17:03 +00:00
|
|
|
func TestPolicyStore_CRUD(t *testing.T) {
|
|
|
|
ps := mockPolicyStore(t)
|
2016-04-21 13:52:42 +00:00
|
|
|
testPolicyStore_CRUD(t, ps)
|
|
|
|
|
|
|
|
ps = mockPolicyStoreNoCache(t)
|
|
|
|
testPolicyStore_CRUD(t, ps)
|
|
|
|
}
|
2015-03-18 19:17:03 +00:00
|
|
|
|
2016-04-21 13:52:42 +00:00
|
|
|
func testPolicyStore_CRUD(t *testing.T, ps *PolicyStore) {
|
2015-03-18 19:17:03 +00:00
|
|
|
// Get should return nothing
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err := ps.GetPolicy(context.Background(), "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
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.DeletePolicy(context.Background(), "deV", PolicyTypeACL)
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List should be blank
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := ps.ListPolicies(context.Background(), 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)
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.SetPolicy(context.Background(), policy)
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should work
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err = ps.GetPolicy(context.Background(), "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
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = ps.ListPolicies(context.Background(), 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
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.DeletePolicy(context.Background(), "Dev", PolicyTypeACL)
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get should fail
|
2018-01-19 06:44:44 +00:00
|
|
|
p, err = ps.GetPolicy(context.Background(), "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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 04:08:07 +00:00
|
|
|
// Test predefined policy handling
|
|
|
|
func TestPolicyStore_Predefined(t *testing.T) {
|
|
|
|
core, _, _ := TestCoreUnsealed(t)
|
|
|
|
// Ensure both default policies are created
|
2018-01-19 06:44:44 +00:00
|
|
|
err := core.setupPolicyStore(context.Background())
|
2016-05-02 04:08:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// List should be two elements
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := core.policyStore.ListPolicies(context.Background(), PolicyTypeACL)
|
2016-05-02 04:08:07 +00:00
|
|
|
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" {
|
2016-05-02 04:08:07 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
pCubby, err := core.policyStore.GetPolicy(context.Background(), "response-wrapping", PolicyTypeToken)
|
2016-05-02 04:08:07 +00:00
|
|
|
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)
|
2016-05-16 20:11:33 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
pRoot, err := core.policyStore.GetPolicy(context.Background(), "root", PolicyTypeToken)
|
2016-05-16 20:11:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
if pRoot == nil {
|
|
|
|
t.Fatal("nil root policy")
|
|
|
|
}
|
2016-05-16 20:11:33 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
err = core.policyStore.SetPolicy(context.Background(), pCubby)
|
2016-05-16 20:11:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected err setting %s", pCubby.Name)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = core.policyStore.SetPolicy(context.Background(), pRoot)
|
2016-05-16 20:11:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected err setting %s", pRoot.Name)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = core.policyStore.DeletePolicy(context.Background(), pCubby.Name, PolicyTypeACL)
|
2016-05-16 20:11:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected err deleting %s", pCubby.Name)
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
err = core.policyStore.DeletePolicy(context.Background(), pRoot.Name, PolicyTypeACL)
|
2016-05-16 20:11:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected err deleting %s", pRoot.Name)
|
2016-05-02 04:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := ps.SetPolicy(context.Background(), policy)
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-10-23 18:59:37 +00:00
|
|
|
policy, _ = ParseACLPolicy(aclPolicy2)
|
2018-01-19 06:44:44 +00:00
|
|
|
err = ps.SetPolicy(context.Background(), policy)
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:42:56 +00:00
|
|
|
acl, err := ps.ACL(context.Background(), nil, "dev", "ops")
|
2015-03-18 19:17:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
testLayeredACL(t, acl)
|
|
|
|
}
|