open-consul/acl/policy.go

85 lines
1.7 KiB
Go
Raw Normal View History

2014-08-06 22:08:17 +00:00
package acl
import (
"fmt"
2014-12-01 03:18:16 +00:00
2014-08-06 22:08:17 +00:00
"github.com/hashicorp/hcl"
)
const (
2014-12-01 03:18:16 +00:00
KeyPolicyDeny = "deny"
KeyPolicyRead = "read"
KeyPolicyWrite = "write"
ServicePolicyDeny = "deny"
ServicePolicyRead = "read"
ServicePolicyWrite = "write"
2014-08-06 22:08:17 +00:00
)
// Policy is used to represent the policy specified by
// an ACL configuration.
type Policy struct {
2014-12-01 03:18:16 +00:00
ID string `hcl:"-"`
Keys []*KeyPolicy `hcl:"key,expand"`
Services []*ServicePolicy `hcl:"service,expand"`
2014-08-06 22:08:17 +00:00
}
// KeyPolicy represents a policy for a key
type KeyPolicy struct {
Prefix string `hcl:",key"`
2014-08-08 22:57:28 +00:00
Policy string
}
func (k *KeyPolicy) GoString() string {
return fmt.Sprintf("%#v", *k)
2014-08-06 22:08:17 +00:00
}
2014-12-01 03:18:16 +00:00
// ServicePolicy represents a policy for a service
type ServicePolicy struct {
Name string `hcl:",key"`
Policy string
}
func (k *ServicePolicy) GoString() string {
return fmt.Sprintf("%#v", *k)
}
2014-08-06 22:08:17 +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
p := &Policy{}
2014-08-08 21:36:09 +00:00
if rules == "" {
// Hot path for empty rules
return p, nil
}
2014-08-06 22:08:17 +00:00
if err := hcl.Decode(p, rules); err != nil {
return nil, fmt.Errorf("Failed to parse ACL rules: %v", err)
}
// Validate the key policy
for _, kp := range p.Keys {
switch kp.Policy {
case KeyPolicyDeny:
case KeyPolicyRead:
case KeyPolicyWrite:
default:
return nil, fmt.Errorf("Invalid key policy: %#v", kp)
}
}
2014-12-01 03:18:16 +00:00
// Validate the service policy
for _, sp := range p.Services {
switch sp.Policy {
case ServicePolicyDeny:
case ServicePolicyRead:
case ServicePolicyWrite:
default:
return nil, fmt.Errorf("Invalid service policy: %#v", sp)
}
}
2014-08-06 22:08:17 +00:00
return p, nil
}