open-vault/vault/policy_test.go

93 lines
1.8 KiB
Go
Raw Normal View History

2015-03-17 22:53:29 +00:00
package vault
import (
"fmt"
2015-03-17 22:53:29 +00:00
"reflect"
"testing"
)
func TestPolicy_Parse(t *testing.T) {
p, err := Parse(rawPolicy)
if err != nil {
t.Fatalf("err: %v", err)
}
if p.Name != "dev" {
t.Fatalf("bad: %#v", p)
}
expect := []*PathCapabilities{
&PathCapabilities{"", "deny",
[]string{
"deny",
2016-01-12 22:08:10 +00:00
}, DenyCapabilityInt, true},
&PathCapabilities{"stage/", "sudo",
[]string{
"create",
"read",
"update",
"delete",
"list",
"sudo",
2016-01-12 22:08:10 +00:00
}, CreateCapabilityInt | ReadCapabilityInt | UpdateCapabilityInt |
DeleteCapabilityInt | ListCapabilityInt | SudoCapabilityInt, true},
&PathCapabilities{"prod/version", "read",
[]string{
"read",
"list",
2016-01-12 22:08:10 +00:00
}, ReadCapabilityInt | ListCapabilityInt, false},
&PathCapabilities{"foo/bar", "read",
[]string{
"read",
"list",
2016-01-12 22:08:10 +00:00
}, ReadCapabilityInt | ListCapabilityInt, false},
&PathCapabilities{"foo/bar", "",
[]string{
"create",
"sudo",
2016-01-12 22:08:10 +00:00
}, CreateCapabilityInt | SudoCapabilityInt, false},
2015-03-17 22:53:29 +00:00
}
if !reflect.DeepEqual(p.Paths, expect) {
ret := fmt.Sprintf("bad:\nexpected:\n")
for _, v := range expect {
ret = fmt.Sprintf("%s\n%#v", ret, *v)
}
ret = fmt.Sprintf("%s\n\ngot:\n", ret)
for _, v := range p.Paths {
ret = fmt.Sprintf("%s\n%#v", ret, *v)
}
t.Fatalf("%s\n", ret)
2015-03-17 22:53:29 +00:00
}
}
var rawPolicy = `
# Developer policy
name = "dev"
# Deny all paths by default
path "*" {
2015-03-17 22:53:29 +00:00
policy = "deny"
}
# Allow full access to staging
path "stage/*" {
2015-03-17 22:53:29 +00:00
policy = "sudo"
}
# Limited read privilege to production
path "prod/version" {
2015-03-17 22:53:29 +00:00
policy = "read"
}
# Read access to foobar
path "foo/bar" {
policy = "read"
}
# Add capabilities for creation and sudo to foobar
# This will be separate; they are combined when compiled into an ACL
path "foo/bar" {
capabilities = ["create", "sudo"]
}
2015-03-17 22:53:29 +00:00
`