2015-03-17 22:53:29 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2016-01-07 20:10:05 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
expect := []*PathCapabilities{
|
|
|
|
&PathCapabilities{"", "deny",
|
|
|
|
[]string{
|
|
|
|
"deny",
|
2016-01-12 22:08:10 +00:00
|
|
|
}, DenyCapabilityInt, true},
|
2016-01-07 20:10:05 +00:00
|
|
|
&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},
|
2016-01-07 20:10:05 +00:00
|
|
|
&PathCapabilities{"prod/version", "read",
|
|
|
|
[]string{
|
|
|
|
"read",
|
|
|
|
"list",
|
2016-01-12 22:08:10 +00:00
|
|
|
}, ReadCapabilityInt | ListCapabilityInt, false},
|
2016-01-07 20:10:05 +00:00
|
|
|
&PathCapabilities{"foo/bar", "read",
|
|
|
|
[]string{
|
|
|
|
"read",
|
|
|
|
"list",
|
2016-01-12 22:08:10 +00:00
|
|
|
}, ReadCapabilityInt | ListCapabilityInt, false},
|
2016-01-07 20:10:05 +00:00
|
|
|
&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) {
|
2016-01-07 20:10:05 +00:00
|
|
|
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
|
2015-07-05 21:58:38 +00:00
|
|
|
path "*" {
|
2015-03-17 22:53:29 +00:00
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Allow full access to staging
|
2015-07-05 21:58:38 +00:00
|
|
|
path "stage/*" {
|
2015-03-17 22:53:29 +00:00
|
|
|
policy = "sudo"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Limited read privilege to production
|
2015-07-05 21:58:38 +00:00
|
|
|
path "prod/version" {
|
2015-03-17 22:53:29 +00:00
|
|
|
policy = "read"
|
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
`
|