2014-08-06 22:08:17 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2019-10-15 20:58:50 +00:00
|
|
|
"errors"
|
2020-06-05 14:43:23 +00:00
|
|
|
"fmt"
|
2019-10-15 20:58:50 +00:00
|
|
|
"strings"
|
2014-08-06 22:08:17 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// These error constants define the standard ACL error types. The values
|
|
|
|
// must not be changed since the error values are sent via RPC calls
|
|
|
|
// from older clients and may not have the correct type.
|
|
|
|
const (
|
|
|
|
errNotFound = "ACL not found"
|
|
|
|
errRootDenied = "Cannot resolve root ACL"
|
|
|
|
errDisabled = "ACL support disabled"
|
|
|
|
errPermissionDenied = "Permission denied"
|
|
|
|
errInvalidParent = "Invalid Parent"
|
2014-08-06 22:08:17 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
var (
|
|
|
|
// ErrNotFound indicates there is no matching ACL.
|
|
|
|
ErrNotFound = errors.New(errNotFound)
|
2016-12-07 04:05:15 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// ErrRootDenied is returned when attempting to resolve a root ACL.
|
|
|
|
ErrRootDenied = errors.New(errRootDenied)
|
2016-12-07 04:05:15 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// ErrDisabled is returned when ACL changes are not permitted since
|
|
|
|
// they are disabled.
|
|
|
|
ErrDisabled = errors.New(errDisabled)
|
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
|
|
|
// ErrPermissionDenied is returned when an ACL based rejection
|
|
|
|
// happens.
|
|
|
|
ErrPermissionDenied = PermissionDeniedError{}
|
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
|
|
|
// ErrInvalidParent is returned when a remotely resolve ACL
|
|
|
|
// token claims to have a non-root parent
|
|
|
|
ErrInvalidParent = errors.New(errInvalidParent)
|
|
|
|
)
|
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
|
|
|
// IsErrNotFound checks if the given error message is comparable to
|
|
|
|
// ErrNotFound.
|
|
|
|
func IsErrNotFound(err error) bool {
|
|
|
|
return err != nil && strings.Contains(err.Error(), errNotFound)
|
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
|
|
|
// IsErrRootDenied checks if the given error message is comparable to
|
|
|
|
// ErrRootDenied.
|
|
|
|
func IsErrRootDenied(err error) bool {
|
|
|
|
return err != nil && strings.Contains(err.Error(), errRootDenied)
|
2016-08-30 02:09:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// IsErrDisabled checks if the given error message is comparable to
|
|
|
|
// ErrDisabled.
|
|
|
|
func IsErrDisabled(err error) bool {
|
|
|
|
return err != nil && strings.Contains(err.Error(), errDisabled)
|
2016-10-26 02:20:24 +00:00
|
|
|
}
|
2016-12-13 04:20:28 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// IsErrPermissionDenied checks if the given error message is comparable
|
|
|
|
// to ErrPermissionDenied.
|
|
|
|
func IsErrPermissionDenied(err error) bool {
|
|
|
|
return err != nil && strings.Contains(err.Error(), errPermissionDenied)
|
2016-12-13 04:20:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 20:53:23 +00:00
|
|
|
// Arguably this should be some sort of union type.
|
|
|
|
// The usage of Cause and the rest of the fields is entirely disjoint.
|
2019-10-15 20:58:50 +00:00
|
|
|
type PermissionDeniedError struct {
|
|
|
|
Cause string
|
2022-02-11 20:53:23 +00:00
|
|
|
|
|
|
|
// Accessor contains information on the accessor used e.g. "token <GUID>"
|
|
|
|
Accessor string
|
|
|
|
// Resource (e.g. Service)
|
|
|
|
Resource Resource
|
2023-01-18 22:14:34 +00:00
|
|
|
// Access level (e.g. Read)
|
2022-02-11 20:53:23 +00:00
|
|
|
AccessLevel AccessLevel
|
|
|
|
// e.g. "sidecar-proxy-1"
|
|
|
|
ResourceID ResourceDescriptor
|
2016-12-13 04:20:28 +00:00
|
|
|
}
|
2017-09-14 19:31:01 +00:00
|
|
|
|
2022-02-11 20:53:23 +00:00
|
|
|
// Initially we may not have attribution information; that will become more complete as we work this change through
|
|
|
|
// There are generally three classes of errors
|
|
|
|
// 1) Named entities without a context
|
|
|
|
// 2) Unnamed entities with a context
|
|
|
|
// 3) Completely context free checks (global permissions)
|
|
|
|
// 4) Errors that only have a cause (for example bad token)
|
2019-10-15 20:58:50 +00:00
|
|
|
func (e PermissionDeniedError) Error() string {
|
2022-02-11 20:53:23 +00:00
|
|
|
var message strings.Builder
|
|
|
|
message.WriteString(errPermissionDenied)
|
|
|
|
|
|
|
|
// Type 4)
|
2019-10-15 20:58:50 +00:00
|
|
|
if e.Cause != "" {
|
2022-02-11 20:53:23 +00:00
|
|
|
fmt.Fprintf(&message, ": %s", e.Cause)
|
|
|
|
return message.String()
|
|
|
|
}
|
|
|
|
// Should only be empty when default struct is used.
|
|
|
|
if e.Resource == "" {
|
|
|
|
return message.String()
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
if e.Accessor == AnonymousTokenID {
|
2023-01-09 18:28:53 +00:00
|
|
|
message.WriteString(": anonymous token")
|
2022-02-11 20:53:23 +00:00
|
|
|
} else {
|
2022-03-25 19:34:59 +00:00
|
|
|
fmt.Fprintf(&message, ": token with AccessorID '%s'", e.Accessor)
|
2022-02-11 20:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&message, " lacks permission '%s:%s'", e.Resource, e.AccessLevel.String())
|
|
|
|
|
|
|
|
if e.ResourceID.Name != "" {
|
2022-03-11 02:48:27 +00:00
|
|
|
fmt.Fprintf(&message, " on %s", e.ResourceID.ToString())
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2023-01-09 18:28:53 +00:00
|
|
|
|
|
|
|
if e.Accessor == AnonymousTokenID {
|
|
|
|
message.WriteString(". The anonymous token is used implicitly when a request does not specify a token.")
|
|
|
|
}
|
2022-02-11 20:53:23 +00:00
|
|
|
return message.String()
|
2017-09-14 19:31:01 +00:00
|
|
|
}
|
2020-06-05 14:43:23 +00:00
|
|
|
|
|
|
|
func PermissionDenied(msg string, args ...interface{}) PermissionDeniedError {
|
|
|
|
cause := fmt.Sprintf(msg, args...)
|
|
|
|
return PermissionDeniedError{Cause: cause}
|
|
|
|
}
|
2022-02-11 20:53:23 +00:00
|
|
|
|
|
|
|
// TODO Extract information from Authorizer
|
2022-03-18 17:32:25 +00:00
|
|
|
func PermissionDeniedByACL(authz Authorizer, context *AuthorizerContext, resource Resource, accessLevel AccessLevel, resourceID string) PermissionDeniedError {
|
2022-02-11 20:53:23 +00:00
|
|
|
desc := NewResourceDescriptor(resourceID, context)
|
2022-03-18 17:32:25 +00:00
|
|
|
return PermissionDeniedError{Accessor: extractAccessorID(authz), Resource: resource, AccessLevel: accessLevel, ResourceID: desc}
|
2022-02-11 20:53:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 17:32:25 +00:00
|
|
|
func PermissionDeniedByACLUnnamed(authz Authorizer, context *AuthorizerContext, resource Resource, accessLevel AccessLevel) PermissionDeniedError {
|
2022-02-11 20:53:23 +00:00
|
|
|
desc := NewResourceDescriptor("", context)
|
2022-03-18 17:32:25 +00:00
|
|
|
return PermissionDeniedError{Accessor: extractAccessorID(authz), Resource: resource, AccessLevel: accessLevel, ResourceID: desc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractAccessorID(authz interface{}) string {
|
|
|
|
var accessor string
|
|
|
|
switch v := authz.(type) {
|
|
|
|
case AllowAuthorizer:
|
|
|
|
accessor = v.AccessorID
|
|
|
|
case string:
|
|
|
|
accessor = v
|
|
|
|
}
|
|
|
|
return accessor
|
2022-02-11 20:53:23 +00:00
|
|
|
}
|
2023-02-08 23:49:44 +00:00
|
|
|
|
|
|
|
func ACLResourceNotExistError(resourceType string, entMeta EnterpriseMeta) error {
|
|
|
|
if ns := entMeta.NamespaceOrEmpty(); ns != "" {
|
|
|
|
return fmt.Errorf("Requested %s not found in namespace %s: %w", resourceType, ns, ErrNotFound)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Requested %s does not exist: %w", resourceType, ErrNotFound)
|
|
|
|
}
|