open-vault/vault/policy.go

71 lines
1.4 KiB
Go
Raw Normal View History

2015-03-17 22:53:29 +00:00
package vault
import (
"fmt"
"strings"
2015-03-17 22:53:29 +00:00
"github.com/hashicorp/hcl"
)
const (
PathPolicyDeny = "deny"
PathPolicyRead = "read"
PathPolicyWrite = "write"
PathPolicySudo = "sudo"
)
2015-03-18 01:31:20 +00:00
var (
pathPolicyLevel = map[string]int{
PathPolicyDeny: 0,
PathPolicyRead: 1,
PathPolicyWrite: 2,
PathPolicySudo: 3,
}
)
2015-03-17 22:53:29 +00:00
// Policy is used to represent the policy specified by
// an ACL configuration.
type Policy struct {
Name string `hcl:"name"`
Paths []*PathPolicy `hcl:"path,expand"`
2015-03-18 19:03:33 +00:00
Raw string
2015-03-17 22:53:29 +00:00
}
// PathPolicy represents a policy for a path in the namespace
type PathPolicy struct {
Prefix string `hcl:",key"`
Policy string
Glob bool
2015-03-17 22:53:29 +00:00
}
// Parse is used to parse the specified ACL rules into an
// intermediary set of policies, before being compiled into
// the ACL
func Parse(rules string) (*Policy, error) {
// Decode the rules
2015-03-18 19:03:33 +00:00
p := &Policy{Raw: rules}
2015-03-17 22:53:29 +00:00
if err := hcl.Decode(p, rules); err != nil {
return nil, fmt.Errorf("Failed to parse ACL rules: %v", err)
}
// Validate the path policy
for _, pp := range p.Paths {
// Strip the glob character if found
if strings.HasSuffix(pp.Prefix, "*") {
pp.Prefix = strings.TrimSuffix(pp.Prefix, "*")
pp.Glob = true
}
// Check the policy is valid
2015-03-17 22:53:29 +00:00
switch pp.Policy {
case PathPolicyDeny:
case PathPolicyRead:
case PathPolicyWrite:
case PathPolicySudo:
default:
return nil, fmt.Errorf("Invalid path policy: %#v", pp)
}
}
return p, nil
}