2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 20:12:41 +00:00
|
|
|
|
2014-08-06 22:08:17 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-15 20:58:50 +00:00
|
|
|
"strings"
|
2018-10-19 16:04:07 +00:00
|
|
|
)
|
|
|
|
|
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"`
|
2022-07-22 20:42:23 +00:00
|
|
|
Peering string `hcl:"peering"`
|
2024-09-05 14:50:46 +00:00
|
|
|
|
|
|
|
// Deprecated: exists just to track the former field for decoding
|
|
|
|
Identities []*IdentityRule `hcl:"identity,expand"`
|
|
|
|
// Deprecated: exists just to track the former field for decoding
|
|
|
|
IdentityPrefixes []*IdentityRule `hcl:"identity_prefix,expand"`
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-12 21:22:51 +00:00
|
|
|
// IdentityRule represents a policy for a workload identity
|
2024-09-05 14:50:46 +00:00
|
|
|
//
|
|
|
|
// Deprecated: exists just to track the former field for decoding
|
2023-09-12 21:22:51 +00:00
|
|
|
type IdentityRule struct {
|
|
|
|
Name string `hcl:",key"`
|
|
|
|
Policy string
|
|
|
|
|
|
|
|
// Intentions is the policy for intentions where this workload identity
|
|
|
|
// is the destination. This may be empty, in which case the Policy determines
|
|
|
|
// the intentions policy.
|
|
|
|
Intentions string
|
|
|
|
|
|
|
|
EnterpriseRule `hcl:",squash"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-09-05 14:50:46 +00:00
|
|
|
// Identity rules are deprecated, zero them out.
|
|
|
|
pr.Identities = nil
|
|
|
|
pr.IdentityPrefixes = nil
|
2023-09-12 21:22:51 +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)
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:42:23 +00:00
|
|
|
// Validate the peering policy - this one is allowed to be empty
|
|
|
|
if pr.Peering != "" && !isPolicyValid(pr.Peering, false) {
|
|
|
|
return fmt.Errorf("Invalid peering policy: %#v", pr.Peering)
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-06 15:35:52 +00:00
|
|
|
func parse(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
|
|
|
|
|
|
|
// NewPolicyFromSource is used to parse the specified ACL rules into an
|
|
|
|
// intermediary set of policies, before being compiled into
|
|
|
|
// the ACL
|
2023-02-06 15:35:52 +00:00
|
|
|
func NewPolicyFromSource(rules string, conf *Config, meta *EnterprisePolicyMeta) (*Policy, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
if rules == "" {
|
|
|
|
// Hot path for empty source
|
2021-10-15 17:31:04 +00:00
|
|
|
return &Policy{}, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var policy *Policy
|
|
|
|
var err error
|
2023-02-06 15:35:52 +00:00
|
|
|
policy, err = parse(rules, conf, meta)
|
2018-10-19 16:04:07 +00:00
|
|
|
return policy, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|