open-vault/vault/policy.go

224 lines
5.7 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
2016-03-10 18:36:54 +00:00
"github.com/hashicorp/go-multierror"
2015-03-17 22:53:29 +00:00
"github.com/hashicorp/hcl"
2016-03-10 18:36:54 +00:00
"github.com/hashicorp/hcl/hcl/ast"
2015-03-17 22:53:29 +00:00
)
const (
2016-01-12 22:08:10 +00:00
DenyCapability = "deny"
CreateCapability = "create"
ReadCapability = "read"
UpdateCapability = "update"
DeleteCapability = "delete"
ListCapability = "list"
SudoCapability = "sudo"
RootCapability = "root"
// Backwards compatibility
OldDenyPathPolicy = "deny"
OldReadPathPolicy = "read"
OldWritePathPolicy = "write"
OldSudoPathPolicy = "sudo"
2015-03-17 22:53:29 +00:00
)
2016-01-12 22:08:10 +00:00
const (
DenyCapabilityInt uint32 = 1 << iota
CreateCapabilityInt
ReadCapabilityInt
UpdateCapabilityInt
DeleteCapabilityInt
ListCapabilityInt
SudoCapabilityInt
)
var (
cap2Int = map[string]uint32{
DenyCapability: DenyCapabilityInt,
CreateCapability: CreateCapabilityInt,
ReadCapability: ReadCapabilityInt,
UpdateCapability: UpdateCapabilityInt,
DeleteCapability: DeleteCapabilityInt,
ListCapability: ListCapabilityInt,
SudoCapability: SudoCapabilityInt,
}
)
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"`
2016-03-10 18:36:54 +00:00
Paths []*PathCapabilities `hcl:"-"`
2015-03-18 19:03:33 +00:00
Raw string
2015-03-17 22:53:29 +00:00
}
/*
*/
2016-03-10 18:36:54 +00:00
// PathCapabilities represents a policy for a path in the namespace.
type PathCapabilities struct {
Prefix string
Policy string
Capabilities []string
Permissions *Permissions
Glob bool
}
type Permissions struct {
CapabilitiesBitmap uint32
AllowedParameters map[string][]string
DisallowedParameters map[string][]string
}
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) {
2016-03-10 18:36:54 +00:00
// Parse the rules
root, err := hcl.Parse(rules)
if err != nil {
return nil, fmt.Errorf("Failed to parse policy: %s", err)
}
// Top-level item should be the object list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("Failed to parse policy: does not contain a root object")
}
// Check for invalid top-level keys
valid := []string{
"name",
"path",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, fmt.Errorf("Failed to parse policy: %s", err)
}
// Create the initial policy and store the raw text of the rules
2016-03-10 20:55:47 +00:00
var p Policy
p.Raw = rules
2016-03-10 18:36:54 +00:00
if err := hcl.DecodeObject(&p, list); err != nil {
return nil, fmt.Errorf("Failed to parse policy: %s", err)
}
if o := list.Filter("path"); len(o.Items) > 0 {
2016-03-10 20:55:47 +00:00
if err := parsePaths(&p, o); err != nil {
2016-03-10 18:36:54 +00:00
return nil, fmt.Errorf("Failed to parse policy: %s", err)
}
2015-03-17 22:53:29 +00:00
}
2016-03-10 20:55:47 +00:00
return &p, nil
2016-03-10 18:36:54 +00:00
}
func parsePaths(result *Policy, list *ast.ObjectList) error {
// specifically how can we access the key value pairs for
// permissions
2016-03-10 18:36:54 +00:00
paths := make([]*PathCapabilities, 0, len(list.Items))
for _, item := range list.Items {
key := "path"
if len(item.Keys) > 0 {
key = item.Keys[0].Token.Value().(string) // "secret/foo"
2016-03-10 18:36:54 +00:00
}
valid := []string{
"policy",
"capabilities",
"permissions", // added here to validate
2016-03-10 18:36:54 +00:00
}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf("path %q:", key))
}
var pc PathCapabilities
// allocate memory so that DecodeObject can initialize the Permissions struct
pc.Permissions = new(Permissions)
2016-03-10 18:36:54 +00:00
pc.Prefix = key
if err := hcl.DecodeObject(&pc, item.Val); err != nil {
return multierror.Prefix(err, fmt.Sprintf("path %q:", key))
}
// Strip a leading '/' as paths in Vault start after the / in the API path
if len(pc.Prefix) > 0 && pc.Prefix[0] == '/' {
pc.Prefix = pc.Prefix[1:]
}
// Strip the glob character if found
if strings.HasSuffix(pc.Prefix, "*") {
pc.Prefix = strings.TrimSuffix(pc.Prefix, "*")
pc.Glob = true
}
// Map old-style policies into capabilities
2016-03-10 18:36:54 +00:00
if len(pc.Policy) > 0 {
switch pc.Policy {
case OldDenyPathPolicy:
pc.Capabilities = []string{DenyCapability}
case OldReadPathPolicy:
pc.Capabilities = append(pc.Capabilities, []string{ReadCapability, ListCapability}...)
case OldWritePathPolicy:
pc.Capabilities = append(pc.Capabilities, []string{CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability}...)
case OldSudoPathPolicy:
pc.Capabilities = append(pc.Capabilities, []string{CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability}...)
default:
return fmt.Errorf("path %q: invalid policy '%s'", key, pc.Policy)
}
2015-03-17 22:53:29 +00:00
}
// Initialize the map
pc.Permissions.CapabilitiesBitmap = 0
for _, cap := range pc.Capabilities {
switch cap {
// If it's deny, don't include any other capability
case DenyCapability:
pc.Capabilities = []string{DenyCapability}
pc.Permissions.CapabilitiesBitmap = DenyCapabilityInt
goto PathFinished
case CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability:
pc.Permissions.CapabilitiesBitmap |= cap2Int[cap]
default:
2016-03-10 18:36:54 +00:00
return fmt.Errorf("path %q: invalid capability '%s'", key, cap)
}
}
PathFinished:
2016-03-10 18:36:54 +00:00
paths = append(paths, &pc)
2015-03-17 22:53:29 +00:00
}
2016-03-10 18:36:54 +00:00
result.Paths = paths
return nil
}
func checkHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key '%s' on line %d", key, item.Assign.Line))
}
}
return result
2015-03-17 22:53:29 +00:00
}