2014-08-06 22:08:17 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2018-10-19 16:04:07 +00:00
|
|
|
"bytes"
|
2014-08-06 22:08:17 +00:00
|
|
|
"fmt"
|
2019-04-15 18:34:36 +00:00
|
|
|
"strconv"
|
2019-10-15 20:58:50 +00:00
|
|
|
"strings"
|
2014-12-01 03:18:16 +00:00
|
|
|
|
2014-08-06 22:08:17 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
hclprinter "github.com/hashicorp/hcl/hcl/printer"
|
2019-04-15 18:34:36 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/token"
|
2018-10-19 16:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SyntaxVersion int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SyntaxCurrent SyntaxVersion = iota
|
|
|
|
SyntaxLegacy
|
2014-08-06 22:08:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
PolicyDeny = "deny"
|
|
|
|
PolicyRead = "read"
|
2017-10-02 22:10:21 +00:00
|
|
|
PolicyList = "list"
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyWrite = "write"
|
2014-08-06 22:08:17 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
type AccessLevel int
|
2014-08-06 22:08:17 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
const (
|
|
|
|
AccessUnknown AccessLevel = iota
|
|
|
|
AccessDeny
|
|
|
|
AccessRead
|
|
|
|
AccessList
|
|
|
|
AccessWrite
|
|
|
|
)
|
|
|
|
|
|
|
|
func (l AccessLevel) String() string {
|
|
|
|
switch l {
|
|
|
|
case AccessDeny:
|
|
|
|
return PolicyDeny
|
|
|
|
case AccessRead:
|
|
|
|
return PolicyRead
|
|
|
|
case AccessList:
|
|
|
|
return PolicyList
|
|
|
|
case AccessWrite:
|
|
|
|
return PolicyWrite
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
func AccessLevelFromString(level string) (AccessLevel, error) {
|
|
|
|
switch strings.ToLower(level) {
|
|
|
|
case PolicyDeny:
|
|
|
|
return AccessDeny, nil
|
|
|
|
case PolicyRead:
|
|
|
|
return AccessRead, nil
|
|
|
|
case PolicyList:
|
|
|
|
return AccessList, nil
|
|
|
|
case PolicyWrite:
|
|
|
|
return AccessWrite, nil
|
|
|
|
default:
|
|
|
|
return AccessUnknown, fmt.Errorf("%q is not a valid access level", level)
|
|
|
|
}
|
2016-12-13 07:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
type PolicyRules struct {
|
|
|
|
ACL string `hcl:"acl,expand"`
|
|
|
|
Agents []*AgentRule `hcl:"agent,expand"`
|
|
|
|
AgentPrefixes []*AgentRule `hcl:"agent_prefix,expand"`
|
|
|
|
Keys []*KeyRule `hcl:"key,expand"`
|
|
|
|
KeyPrefixes []*KeyRule `hcl:"key_prefix,expand"`
|
|
|
|
Nodes []*NodeRule `hcl:"node,expand"`
|
|
|
|
NodePrefixes []*NodeRule `hcl:"node_prefix,expand"`
|
|
|
|
Services []*ServiceRule `hcl:"service,expand"`
|
|
|
|
ServicePrefixes []*ServiceRule `hcl:"service_prefix,expand"`
|
|
|
|
Sessions []*SessionRule `hcl:"session,expand"`
|
|
|
|
SessionPrefixes []*SessionRule `hcl:"session_prefix,expand"`
|
|
|
|
Events []*EventRule `hcl:"event,expand"`
|
|
|
|
EventPrefixes []*EventRule `hcl:"event_prefix,expand"`
|
|
|
|
PreparedQueries []*PreparedQueryRule `hcl:"query,expand"`
|
|
|
|
PreparedQueryPrefixes []*PreparedQueryRule `hcl:"query_prefix,expand"`
|
|
|
|
Keyring string `hcl:"keyring"`
|
|
|
|
Operator string `hcl:"operator"`
|
2021-08-20 22:11:01 +00:00
|
|
|
Mesh string `hcl:"mesh"`
|
2016-12-13 07:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// Policy is used to represent the policy specified by an ACL configuration.
|
|
|
|
type Policy struct {
|
|
|
|
ID string `hcl:"id"`
|
|
|
|
Revision uint64 `hcl:"revision"`
|
|
|
|
PolicyRules `hcl:",squash"`
|
|
|
|
EnterprisePolicyRules `hcl:",squash"`
|
2014-08-08 22:57:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// AgentRule represents a rule for working with agent endpoints on nodes
|
|
|
|
// with specific name prefixes.
|
|
|
|
type AgentRule struct {
|
|
|
|
Node string `hcl:",key"`
|
|
|
|
Policy string
|
2014-08-06 22:08:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// KeyRule represents a rule for a key
|
|
|
|
type KeyRule struct {
|
|
|
|
Prefix string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
|
|
|
|
EnterpriseRule `hcl:",squash"`
|
2016-12-07 04:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// NodeRule represents a rule for a node
|
|
|
|
type NodeRule struct {
|
|
|
|
Name string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
|
|
|
|
EnterpriseRule `hcl:",squash"`
|
2016-12-07 04:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// ServiceRule represents a policy for a service
|
|
|
|
type ServiceRule struct {
|
|
|
|
Name string `hcl:",key"`
|
|
|
|
Policy string
|
2018-03-04 00:14:33 +00:00
|
|
|
|
|
|
|
// Intentions is the policy for intentions where this service is the
|
|
|
|
// destination. This may be empty, in which case the Policy determines
|
|
|
|
// the intentions policy.
|
|
|
|
Intentions string
|
2014-12-01 03:18:16 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
EnterpriseRule `hcl:",squash"`
|
2014-12-01 03:18:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// SessionRule represents a rule for making sessions tied to specific node
|
2016-12-13 04:20:28 +00:00
|
|
|
// name prefixes.
|
2019-10-15 20:58:50 +00:00
|
|
|
type SessionRule struct {
|
2016-12-13 04:20:28 +00:00
|
|
|
Node string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// EventRule represents a user event rule.
|
|
|
|
type EventRule struct {
|
2015-06-18 01:56:29 +00:00
|
|
|
Event string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// PreparedQueryRule represents a prepared query rule.
|
|
|
|
type PreparedQueryRule struct {
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
Prefix string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPolicyValid makes sure the given string matches one of the valid policies.
|
2019-10-15 20:58:50 +00:00
|
|
|
func isPolicyValid(policy string, allowList bool) bool {
|
|
|
|
access, err := AccessLevelFromString(policy)
|
|
|
|
if err != nil {
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if access == AccessList && !allowList {
|
|
|
|
return false
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
return true
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func (pr *PolicyRules) Validate(conf *Config) error {
|
2019-10-15 20:58:50 +00:00
|
|
|
// Validate the acl policy - this one is allowed to be empty
|
|
|
|
if pr.ACL != "" && !isPolicyValid(pr.ACL, false) {
|
|
|
|
return fmt.Errorf("Invalid acl policy: %#v", pr.ACL)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 07:05:11 +00:00
|
|
|
// Validate the agent policy
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, ap := range pr.Agents {
|
|
|
|
if !isPolicyValid(ap.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid agent policy: %#v", ap)
|
2016-12-13 07:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, ap := range pr.AgentPrefixes {
|
|
|
|
if !isPolicyValid(ap.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid agent_prefix policy: %#v", ap)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 07:05:11 +00:00
|
|
|
|
2014-08-06 22:08:17 +00:00
|
|
|
// Validate the key policy
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, kp := range pr.Keys {
|
|
|
|
if !isPolicyValid(kp.Policy, true) {
|
|
|
|
return fmt.Errorf("Invalid key policy: %#v", kp)
|
2014-08-06 22:08:17 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := kp.EnterpriseRule.Validate(kp.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid key enterprise policy: %#v, got error: %v", kp, err)
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2014-08-06 22:08:17 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, kp := range pr.KeyPrefixes {
|
|
|
|
if !isPolicyValid(kp.Policy, true) {
|
|
|
|
return fmt.Errorf("Invalid key_prefix policy: %#v", kp)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := kp.EnterpriseRule.Validate(kp.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid key_prefix enterprise policy: %#v, got error: %v", kp, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 03:18:16 +00:00
|
|
|
|
2016-12-07 04:05:15 +00:00
|
|
|
// Validate the node policies
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, np := range pr.Nodes {
|
|
|
|
if !isPolicyValid(np.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid node policy: %#v", np)
|
2016-12-07 04:05:15 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := np.EnterpriseRule.Validate(np.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid node enterprise policy: %#v, got error: %v", np, err)
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2016-12-07 04:05:15 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, np := range pr.NodePrefixes {
|
|
|
|
if !isPolicyValid(np.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid node_prefix policy: %#v", np)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := np.EnterpriseRule.Validate(np.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid node_prefix enterprise policy: %#v, got error: %v", np, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-07 04:05:15 +00:00
|
|
|
|
|
|
|
// Validate the service policies
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, sp := range pr.Services {
|
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid service policy: %#v", sp)
|
2014-12-01 03:18:16 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if sp.Intentions != "" && !isPolicyValid(sp.Intentions, false) {
|
|
|
|
return fmt.Errorf("Invalid service intentions policy: %#v", sp)
|
2018-03-04 00:14:33 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := sp.EnterpriseRule.Validate(sp.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid service enterprise policy: %#v, got error: %v", sp, err)
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2014-12-01 03:18:16 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, sp := range pr.ServicePrefixes {
|
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid service_prefix policy: %#v", sp)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if sp.Intentions != "" && !isPolicyValid(sp.Intentions, false) {
|
|
|
|
return fmt.Errorf("Invalid service_prefix intentions policy: %#v", sp)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := sp.EnterpriseRule.Validate(sp.Policy, conf); err != nil {
|
|
|
|
return fmt.Errorf("Invalid service_prefix enterprise policy: %#v, got error: %v", sp, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 03:18:16 +00:00
|
|
|
|
2016-12-13 04:20:28 +00:00
|
|
|
// Validate the session policies
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, sp := range pr.Sessions {
|
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid session policy: %#v", sp)
|
2016-12-13 04:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, sp := range pr.SessionPrefixes {
|
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid session_prefix policy: %#v", sp)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 04:20:28 +00:00
|
|
|
|
2015-06-18 01:56:29 +00:00
|
|
|
// Validate the user event policies
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, ep := range pr.Events {
|
|
|
|
if !isPolicyValid(ep.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid event policy: %#v", ep)
|
2015-06-18 01:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, ep := range pr.EventPrefixes {
|
|
|
|
if !isPolicyValid(ep.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid event_prefix policy: %#v", ep)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-18 01:56:29 +00:00
|
|
|
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
// Validate the prepared query policies
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, pq := range pr.PreparedQueries {
|
|
|
|
if !isPolicyValid(pq.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid query policy: %#v", pq)
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
for _, pq := range pr.PreparedQueryPrefixes {
|
|
|
|
if !isPolicyValid(pq.Policy, false) {
|
|
|
|
return fmt.Errorf("Invalid query_prefix policy: %#v", pq)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
Creates new "prepared-query" ACL type and new token capture behavior.
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.
2016-02-23 08:12:58 +00:00
|
|
|
|
|
|
|
// Validate the keyring policy - this one is allowed to be empty
|
2019-10-15 20:58:50 +00:00
|
|
|
if pr.Keyring != "" && !isPolicyValid(pr.Keyring, false) {
|
|
|
|
return fmt.Errorf("Invalid keyring policy: %#v", pr.Keyring)
|
2015-07-07 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 02:09:57 +00:00
|
|
|
// Validate the operator policy - this one is allowed to be empty
|
2019-10-15 20:58:50 +00:00
|
|
|
if pr.Operator != "" && !isPolicyValid(pr.Operator, false) {
|
|
|
|
return fmt.Errorf("Invalid operator policy: %#v", pr.Operator)
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// Validate the mesh policy - this one is allowed to be empty
|
|
|
|
if pr.Mesh != "" && !isPolicyValid(pr.Mesh, false) {
|
|
|
|
return fmt.Errorf("Invalid mesh policy: %#v", pr.Mesh)
|
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func parseCurrent(rules string, conf *Config, meta *EnterprisePolicyMeta) (*Policy, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
p, err := decodeRules(rules, conf, meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-15 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.PolicyRules.Validate(conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.EnterprisePolicyRules.Validate(conf); err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return nil, err
|
2016-08-30 02:09:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:08:17 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func parseLegacy(rules string, conf *Config) (*Policy, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
p := &Policy{}
|
|
|
|
|
|
|
|
type LegacyPolicy struct {
|
2019-10-15 20:58:50 +00:00
|
|
|
Agents []*AgentRule `hcl:"agent,expand"`
|
|
|
|
Keys []*KeyRule `hcl:"key,expand"`
|
|
|
|
Nodes []*NodeRule `hcl:"node,expand"`
|
|
|
|
Services []*ServiceRule `hcl:"service,expand"`
|
|
|
|
Sessions []*SessionRule `hcl:"session,expand"`
|
|
|
|
Events []*EventRule `hcl:"event,expand"`
|
|
|
|
PreparedQueries []*PreparedQueryRule `hcl:"query,expand"`
|
|
|
|
Keyring string `hcl:"keyring"`
|
|
|
|
Operator string `hcl:"operator"`
|
2021-08-20 22:11:01 +00:00
|
|
|
// NOTE: mesh resources not supported here
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lp := &LegacyPolicy{}
|
|
|
|
|
|
|
|
if err := hcl.Decode(lp, rules); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse ACL rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the agent policy
|
|
|
|
for _, ap := range lp.Agents {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(ap.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid agent policy: %#v", ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.AgentPrefixes = append(p.AgentPrefixes, ap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the key policy
|
|
|
|
for _, kp := range lp.Keys {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(kp.Policy, true) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid key policy: %#v", kp)
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
if err := kp.EnterpriseRule.Validate(kp.Policy, conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid key enterprise policy: %#v, got error: %v", kp, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.KeyPrefixes = append(p.KeyPrefixes, kp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the node policies
|
|
|
|
for _, np := range lp.Nodes {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(np.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid node policy: %#v", np)
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := np.EnterpriseRule.Validate(np.Policy, conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid node enterprise policy: %#v, got error: %v", np, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.NodePrefixes = append(p.NodePrefixes, np)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the service policies
|
|
|
|
for _, sp := range lp.Services {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid service policy: %#v", sp)
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if sp.Intentions != "" && !isPolicyValid(sp.Intentions, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid service intentions policy: %#v", sp)
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if err := sp.EnterpriseRule.Validate(sp.Policy, conf); err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid service enterprise policy: %#v, got error: %v", sp, err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.ServicePrefixes = append(p.ServicePrefixes, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the session policies
|
|
|
|
for _, sp := range lp.Sessions {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(sp.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid session policy: %#v", sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.SessionPrefixes = append(p.SessionPrefixes, sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the user event policies
|
|
|
|
for _, ep := range lp.Events {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(ep.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid event policy: %#v", ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.EventPrefixes = append(p.EventPrefixes, ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the prepared query policies
|
|
|
|
for _, pq := range lp.PreparedQueries {
|
2019-10-15 20:58:50 +00:00
|
|
|
if !isPolicyValid(pq.Policy, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid query policy: %#v", pq)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.PreparedQueryPrefixes = append(p.PreparedQueryPrefixes, pq)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the keyring policy - this one is allowed to be empty
|
2019-10-15 20:58:50 +00:00
|
|
|
if lp.Keyring != "" && !isPolicyValid(lp.Keyring, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid keyring policy: %#v", lp.Keyring)
|
|
|
|
} else {
|
|
|
|
p.Keyring = lp.Keyring
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the operator policy - this one is allowed to be empty
|
2019-10-15 20:58:50 +00:00
|
|
|
if lp.Operator != "" && !isPolicyValid(lp.Operator, false) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Invalid operator policy: %#v", lp.Operator)
|
|
|
|
} else {
|
|
|
|
p.Operator = lp.Operator
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPolicyFromSource is used to parse the specified ACL rules into an
|
|
|
|
// intermediary set of policies, before being compiled into
|
|
|
|
// the ACL
|
2019-12-18 18:44:32 +00:00
|
|
|
func NewPolicyFromSource(id string, revision uint64, rules string, syntax SyntaxVersion, conf *Config, meta *EnterprisePolicyMeta) (*Policy, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
if rules == "" {
|
|
|
|
// Hot path for empty source
|
|
|
|
return &Policy{ID: id, Revision: revision}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var policy *Policy
|
|
|
|
var err error
|
|
|
|
switch syntax {
|
|
|
|
case SyntaxLegacy:
|
2019-10-15 20:58:50 +00:00
|
|
|
policy, err = parseLegacy(rules, conf)
|
2018-10-19 16:04:07 +00:00
|
|
|
case SyntaxCurrent:
|
2019-10-24 18:38:09 +00:00
|
|
|
policy, err = parseCurrent(rules, conf, meta)
|
2018-10-19 16:04:07 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid rules version: %d", syntax)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
policy.ID = id
|
|
|
|
policy.Revision = revision
|
|
|
|
}
|
|
|
|
return policy, err
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// TODO(ACL-Legacy): remove this
|
2018-10-19 16:04:07 +00:00
|
|
|
func (policy *Policy) ConvertToLegacy() *Policy {
|
|
|
|
converted := &Policy{
|
|
|
|
ID: policy.ID,
|
|
|
|
Revision: policy.Revision,
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: PolicyRules{
|
|
|
|
ACL: policy.ACL,
|
|
|
|
Keyring: policy.Keyring,
|
|
|
|
Operator: policy.Operator,
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
converted.Agents = append(converted.Agents, policy.Agents...)
|
|
|
|
converted.Agents = append(converted.Agents, policy.AgentPrefixes...)
|
|
|
|
converted.Keys = append(converted.Keys, policy.Keys...)
|
|
|
|
converted.Keys = append(converted.Keys, policy.KeyPrefixes...)
|
|
|
|
converted.Nodes = append(converted.Nodes, policy.Nodes...)
|
|
|
|
converted.Nodes = append(converted.Nodes, policy.NodePrefixes...)
|
|
|
|
converted.Services = append(converted.Services, policy.Services...)
|
|
|
|
converted.Services = append(converted.Services, policy.ServicePrefixes...)
|
|
|
|
converted.Sessions = append(converted.Sessions, policy.Sessions...)
|
|
|
|
converted.Sessions = append(converted.Sessions, policy.SessionPrefixes...)
|
|
|
|
converted.Events = append(converted.Events, policy.Events...)
|
|
|
|
converted.Events = append(converted.Events, policy.EventPrefixes...)
|
|
|
|
converted.PreparedQueries = append(converted.PreparedQueries, policy.PreparedQueries...)
|
|
|
|
converted.PreparedQueries = append(converted.PreparedQueries, policy.PreparedQueryPrefixes...)
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// TODO(ACL-Legacy): remove this
|
2018-10-19 16:04:07 +00:00
|
|
|
func (policy *Policy) ConvertFromLegacy() *Policy {
|
|
|
|
return &Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
ID: policy.ID,
|
|
|
|
Revision: policy.Revision,
|
|
|
|
PolicyRules: PolicyRules{
|
|
|
|
AgentPrefixes: policy.Agents,
|
|
|
|
KeyPrefixes: policy.Keys,
|
|
|
|
NodePrefixes: policy.Nodes,
|
|
|
|
ServicePrefixes: policy.Services,
|
|
|
|
SessionPrefixes: policy.Sessions,
|
|
|
|
EventPrefixes: policy.Events,
|
|
|
|
PreparedQueryPrefixes: policy.PreparedQueries,
|
|
|
|
Keyring: policy.Keyring,
|
|
|
|
Operator: policy.Operator,
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// takesPrecedenceOver returns true when permission a
|
|
|
|
// should take precedence over permission b
|
|
|
|
func takesPrecedenceOver(a, b string) bool {
|
|
|
|
if a == PolicyDeny {
|
|
|
|
return true
|
|
|
|
} else if b == PolicyDeny {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == PolicyWrite {
|
|
|
|
return true
|
|
|
|
} else if b == PolicyWrite {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == PolicyList {
|
|
|
|
return true
|
|
|
|
} else if b == PolicyList {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == PolicyRead {
|
|
|
|
return true
|
|
|
|
} else if b == PolicyRead {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func TranslateLegacyRules(policyBytes []byte) ([]byte, error) {
|
|
|
|
parsed, err := hcl.ParseBytes(policyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rewritten := ast.Walk(parsed, func(node ast.Node) (ast.Node, bool) {
|
|
|
|
switch n := node.(type) {
|
2019-04-15 18:34:36 +00:00
|
|
|
case *ast.ObjectItem:
|
|
|
|
if len(n.Keys) < 1 {
|
|
|
|
return node, true
|
|
|
|
}
|
|
|
|
|
|
|
|
txt := n.Keys[0].Token.Text
|
|
|
|
if n.Keys[0].Token.Type == token.STRING {
|
|
|
|
txt, err = strconv.Unquote(txt)
|
|
|
|
if err != nil {
|
|
|
|
return node, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch txt {
|
|
|
|
case "policy":
|
|
|
|
n.Keys[0].Token.Text = "policy"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "agent":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "agent_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "key":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "key_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "node":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "node_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "query":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "query_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "service":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "service_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "session":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "session_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
case "event":
|
2019-04-15 18:34:36 +00:00
|
|
|
n.Keys[0].Token.Text = "event_prefix"
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, true
|
|
|
|
})
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if err := hclprinter.Fprint(buffer, rewritten); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to output new rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|