2015-03-17 22:53:29 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-07-05 21:58:38 +00:00
|
|
|
"strings"
|
2015-03-17 22:53:29 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-01-12 22:08:10 +00:00
|
|
|
DenyCapability = "deny"
|
2016-01-07 20:10:05 +00:00
|
|
|
CreateCapability = "create"
|
|
|
|
ReadCapability = "read"
|
|
|
|
UpdateCapability = "update"
|
|
|
|
DeleteCapability = "delete"
|
|
|
|
ListCapability = "list"
|
|
|
|
SudoCapability = "sudo"
|
2016-03-04 18:21:07 +00:00
|
|
|
RootCapability = "root"
|
2016-01-07 20:10:05 +00:00
|
|
|
|
|
|
|
// 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 {
|
2016-01-07 20:10:05 +00:00
|
|
|
Name string `hcl:"name"`
|
|
|
|
Paths []*PathCapabilities `hcl:"path,expand"`
|
2015-03-18 19:03:33 +00:00
|
|
|
Raw string
|
2015-03-17 22:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
// Capability represents a policy for a path in the namespace
|
|
|
|
type PathCapabilities struct {
|
2016-01-12 22:08:10 +00:00
|
|
|
Prefix string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
Capabilities []string
|
|
|
|
CapabilitiesBitmap uint32 `hcl:"-"`
|
|
|
|
Glob bool
|
2015-07-05 23:30:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
2016-01-07 20:10:05 +00:00
|
|
|
for _, pc := range p.Paths {
|
2016-03-03 16:32:48 +00:00
|
|
|
// 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:]
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:58:38 +00:00
|
|
|
// Strip the glob character if found
|
2016-01-07 20:10:05 +00:00
|
|
|
if strings.HasSuffix(pc.Prefix, "*") {
|
|
|
|
pc.Prefix = strings.TrimSuffix(pc.Prefix, "*")
|
|
|
|
pc.Glob = true
|
2015-07-05 21:58:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
// Map old-style policies into capabilities
|
|
|
|
switch pc.Policy {
|
|
|
|
case OldDenyPathPolicy:
|
2016-01-12 22:08:10 +00:00
|
|
|
pc.Capabilities = []string{DenyCapability}
|
2016-01-07 20:10:05 +00:00
|
|
|
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}...)
|
2015-03-17 22:53:29 +00:00
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
|
|
|
|
// Initialize the map
|
2016-01-12 22:08:10 +00:00
|
|
|
pc.CapabilitiesBitmap = 0
|
2016-01-07 20:10:05 +00:00
|
|
|
for _, cap := range pc.Capabilities {
|
|
|
|
switch cap {
|
|
|
|
// If it's deny, don't include any other capability
|
|
|
|
case DenyCapability:
|
|
|
|
pc.Capabilities = []string{DenyCapability}
|
2016-01-12 22:08:10 +00:00
|
|
|
pc.CapabilitiesBitmap = DenyCapabilityInt
|
2016-01-07 20:10:05 +00:00
|
|
|
goto PathFinished
|
|
|
|
case CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability:
|
2016-01-12 22:08:10 +00:00
|
|
|
pc.CapabilitiesBitmap |= cap2Int[cap]
|
2016-01-07 20:10:05 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid capability: %#v", pc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PathFinished:
|
2015-03-17 22:53:29 +00:00
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|