633c231d67
Prior to this change, prepared queries had the following behavior for ACLs, which will need to change to support templates: 1. A management token, or a token with read access to the service being queried needed to be provided in order to create a prepared query. 2. The token used to create the prepared query was stored with the query in the state store and used to execute the query. 3. A management token, or the token used to create the query needed to be supplied to perform and CRUD operations on an existing prepared query. This was pretty subtle and complicated behavior, and won't work for templates since the service name is computed at execution time. To solve this, we introduce a new "prepared-query" ACL type, where the prefix applies to the query name for static prepared query types and to the prefix for template prepared query types. With this change, the new behavior is: 1. A management token, or a token with "prepared-query" write access to the query name or (soon) the given template prefix is required to do any CRUD operations on a prepared query, or to list prepared queries (the list is filtered by this ACL). 2. You will no longer need a management token to list prepared queries, but you will only be able to see prepared queries that you have access to (you get an empty list instead of permission denied). 3. When listing or getting a query, because it was easy to capture management tokens given the past behavior, this will always blank out the "Token" field (replacing the contents as <hidden>) for all tokens unless a management token is supplied. Going forward, we should discourage people from binding tokens for execution unless strictly necessary. 4. No token will be captured by default when a prepared query is created. If the user wishes to supply an execution token then can pass it in via the "Token" field in the prepared query definition. Otherwise, this field will default to empty. 5. At execution time, we will use the captured token if it exists with the prepared query definition, otherwise we will use the token that's passed in with the request, just like we do for other RPCs (or you can use the agent's configured token for DNS). 6. Prepared queries with no name (accessible only by ID) will not require ACLs to create or modify (execution time will depend on the service ACL configuration). Our argument here is that these are designed to be ephemeral and the IDs are as good as an ACL. Management tokens will be able to list all of these. These changes enable templates, but also enable delegation of authority to manage the prepared query namespace.
130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
package acl
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/hcl"
|
|
)
|
|
|
|
const (
|
|
PolicyDeny = "deny"
|
|
PolicyRead = "read"
|
|
PolicyWrite = "write"
|
|
)
|
|
|
|
// Policy is used to represent the policy specified by
|
|
// an ACL configuration.
|
|
type Policy struct {
|
|
ID string `hcl:"-"`
|
|
Keys []*KeyPolicy `hcl:"key,expand"`
|
|
Services []*ServicePolicy `hcl:"service,expand"`
|
|
Events []*EventPolicy `hcl:"event,expand"`
|
|
PreparedQueries []*PreparedQueryPolicy `hcl:"prepared_query,expand"`
|
|
Keyring string `hcl:"keyring"`
|
|
}
|
|
|
|
// KeyPolicy represents a policy for a key
|
|
type KeyPolicy struct {
|
|
Prefix string `hcl:",key"`
|
|
Policy string
|
|
}
|
|
|
|
func (k *KeyPolicy) GoString() string {
|
|
return fmt.Sprintf("%#v", *k)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// EventPolicy represents a user event policy.
|
|
type EventPolicy struct {
|
|
Event string `hcl:",key"`
|
|
Policy string
|
|
}
|
|
|
|
func (e *EventPolicy) GoString() string {
|
|
return fmt.Sprintf("%#v", *e)
|
|
}
|
|
|
|
// PreparedQueryPolicy represents a prepared query policy.
|
|
type PreparedQueryPolicy struct {
|
|
Prefix string `hcl:",key"`
|
|
Policy string
|
|
}
|
|
|
|
func (e *PreparedQueryPolicy) GoString() string {
|
|
return fmt.Sprintf("%#v", *e)
|
|
}
|
|
|
|
// isPolicyValid makes sure the given string matches one of the valid policies.
|
|
func isPolicyValid(policy string) bool {
|
|
switch policy {
|
|
case PolicyDeny:
|
|
return true
|
|
case PolicyRead:
|
|
return true
|
|
case PolicyWrite:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 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{}
|
|
if rules == "" {
|
|
// Hot path for empty rules
|
|
return p, nil
|
|
}
|
|
|
|
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 {
|
|
if !isPolicyValid(kp.Policy) {
|
|
return nil, fmt.Errorf("Invalid key policy: %#v", kp)
|
|
}
|
|
}
|
|
|
|
// Validate the service policy
|
|
for _, sp := range p.Services {
|
|
if !isPolicyValid(sp.Policy) {
|
|
return nil, fmt.Errorf("Invalid service policy: %#v", sp)
|
|
}
|
|
}
|
|
|
|
// Validate the user event policies
|
|
for _, ep := range p.Events {
|
|
if !isPolicyValid(ep.Policy) {
|
|
return nil, fmt.Errorf("Invalid event policy: %#v", ep)
|
|
}
|
|
}
|
|
|
|
// Validate the prepared query policies
|
|
for _, pq := range p.PreparedQueries {
|
|
if !isPolicyValid(pq.Policy) {
|
|
return nil, fmt.Errorf("Invalid prepared_query policy: %#v", pq)
|
|
}
|
|
}
|
|
|
|
// Validate the keyring policy - this one is allowed to be empty
|
|
if p.Keyring != "" && !isPolicyValid(p.Keyring) {
|
|
return nil, fmt.Errorf("Invalid keyring policy: %#v", p.Keyring)
|
|
}
|
|
|
|
return p, nil
|
|
}
|