2019-10-15 20:58:50 +00:00
|
|
|
package acl
|
|
|
|
|
2019-12-06 14:25:26 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
type EnforcementDecision int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Deny returned from an Authorizer enforcement method indicates
|
|
|
|
// that a corresponding rule was found and that access should be denied
|
|
|
|
Deny EnforcementDecision = iota
|
|
|
|
// Allow returned from an Authorizer enforcement method indicates
|
|
|
|
// that a corresponding rule was found and that access should be allowed
|
|
|
|
Allow
|
|
|
|
// Default returned from an Authorizer enforcement method indicates
|
|
|
|
// that a corresponding rule was not found and that whether access
|
|
|
|
// should be granted or denied should be deferred to the default
|
|
|
|
// access level
|
|
|
|
Default
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d EnforcementDecision) String() string {
|
|
|
|
switch d {
|
|
|
|
case Allow:
|
|
|
|
return "Allow"
|
|
|
|
case Deny:
|
|
|
|
return "Deny"
|
|
|
|
case Default:
|
|
|
|
return "Default"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:25:26 +00:00
|
|
|
type Resource string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ResourceACL Resource = "acl"
|
|
|
|
ResourceAgent Resource = "agent"
|
|
|
|
ResourceEvent Resource = "event"
|
|
|
|
ResourceIntention Resource = "intention"
|
|
|
|
ResourceKey Resource = "key"
|
|
|
|
ResourceKeyring Resource = "keyring"
|
|
|
|
ResourceNode Resource = "node"
|
|
|
|
ResourceOperator Resource = "operator"
|
2021-08-20 22:11:01 +00:00
|
|
|
ResourceMesh Resource = "mesh"
|
2019-12-06 14:25:26 +00:00
|
|
|
ResourceQuery Resource = "query"
|
|
|
|
ResourceService Resource = "service"
|
|
|
|
ResourceSession Resource = "session"
|
|
|
|
)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// Authorizer is the interface for policy enforcement.
|
|
|
|
type Authorizer interface {
|
|
|
|
// ACLRead checks for permission to list all the ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
ACLRead(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// ACLWrite checks for permission to manipulate ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
ACLWrite(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// AgentRead checks for permission to read from agent endpoints for a
|
|
|
|
// given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
AgentRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// AgentWrite checks for permission to make changes via agent endpoints
|
|
|
|
// for a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
AgentWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// EventRead determines if a specific event can be queried.
|
2019-12-18 18:43:24 +00:00
|
|
|
EventRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// EventWrite determines if a specific event may be fired.
|
2019-12-18 18:43:24 +00:00
|
|
|
EventWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// IntentionDefaultAllow determines the default authorized behavior
|
|
|
|
// when no intentions match a Connect request.
|
2019-12-18 18:43:24 +00:00
|
|
|
IntentionDefaultAllow(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// IntentionRead determines if a specific intention can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
IntentionRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// IntentionWrite determines if a specific intention can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
IntentionWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyList checks for permission to list keys under a prefix
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyList(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyRead checks for permission to read a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyWrite checks for permission to write a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyWritePrefix checks for permission to write to an
|
|
|
|
// entire key prefix. This means there must be no sub-policies
|
|
|
|
// that deny a write.
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyWritePrefix(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyringRead determines if the encryption keyring used in
|
|
|
|
// the gossip layer can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyringRead(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// KeyringWrite determines if the keyring can be manipulated
|
2019-12-18 18:43:24 +00:00
|
|
|
KeyringWrite(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// MeshRead determines if the read-only Consul mesh functions
|
|
|
|
// can be used.
|
|
|
|
MeshRead(*AuthorizerContext) EnforcementDecision
|
|
|
|
|
|
|
|
// MeshWrite determines if the state-changing Consul mesh
|
|
|
|
// functions can be used.
|
|
|
|
MeshWrite(*AuthorizerContext) EnforcementDecision
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// NodeRead checks for permission to read (discover) a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
NodeRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
// NodeReadAll checks for permission to read (discover) all nodes.
|
|
|
|
NodeReadAll(*AuthorizerContext) EnforcementDecision
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// NodeWrite checks for permission to create or update (register) a
|
|
|
|
// given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
NodeWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// OperatorRead determines if the read-only Consul operator functions
|
|
|
|
// can be used.
|
2019-12-18 18:43:24 +00:00
|
|
|
OperatorRead(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// OperatorWrite determines if the state-changing Consul operator
|
|
|
|
// functions can be used.
|
2019-12-18 18:43:24 +00:00
|
|
|
OperatorWrite(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// PreparedQueryRead determines if a specific prepared query can be read
|
|
|
|
// to show its contents (this is not used for execution).
|
2019-12-18 18:43:24 +00:00
|
|
|
PreparedQueryRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// PreparedQueryWrite determines if a specific prepared query can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
PreparedQueryWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// ServiceRead checks for permission to read a given service
|
2019-12-18 18:43:24 +00:00
|
|
|
ServiceRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
// ServiceReadAll checks for permission to read all services
|
|
|
|
ServiceReadAll(*AuthorizerContext) EnforcementDecision
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// ServiceWrite checks for permission to create or update a given
|
|
|
|
// service
|
2019-12-18 18:43:24 +00:00
|
|
|
ServiceWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// SessionRead checks for permission to read sessions for a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
SessionRead(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// SessionWrite checks for permission to create sessions for a given
|
|
|
|
// node.
|
2019-12-18 18:43:24 +00:00
|
|
|
SessionWrite(string, *AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// Snapshot checks for permission to take and restore snapshots.
|
2019-12-18 18:43:24 +00:00
|
|
|
Snapshot(*AuthorizerContext) EnforcementDecision
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
// Embedded Interface for Consul Enterprise specific ACL enforcement
|
2019-12-18 18:44:32 +00:00
|
|
|
enterpriseAuthorizer
|
2019-10-15 20:58:50 +00:00
|
|
|
}
|
2019-12-06 14:25:26 +00:00
|
|
|
|
2019-12-18 18:43:24 +00:00
|
|
|
func Enforce(authz Authorizer, rsc Resource, segment string, access string, ctx *AuthorizerContext) (EnforcementDecision, error) {
|
2019-12-06 14:25:26 +00:00
|
|
|
lowerAccess := strings.ToLower(access)
|
|
|
|
|
|
|
|
switch rsc {
|
|
|
|
case ResourceACL:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.ACLRead(ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.ACLWrite(ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceAgent:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.AgentRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.AgentWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceEvent:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.EventRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.EventWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceIntention:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.IntentionRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.IntentionWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceKey:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.KeyRead(segment, ctx), nil
|
|
|
|
case "list":
|
|
|
|
return authz.KeyList(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.KeyWrite(segment, ctx), nil
|
|
|
|
case "write-prefix":
|
|
|
|
return authz.KeyWritePrefix(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceKeyring:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.KeyringRead(ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.KeyringWrite(ctx), nil
|
|
|
|
}
|
2021-08-20 22:11:01 +00:00
|
|
|
case ResourceMesh:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.MeshRead(ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.MeshWrite(ctx), nil
|
|
|
|
}
|
2019-12-06 14:25:26 +00:00
|
|
|
case ResourceNode:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.NodeRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.NodeWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceOperator:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.OperatorRead(ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.OperatorWrite(ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceQuery:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.PreparedQueryRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.PreparedQueryWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceService:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.ServiceRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.ServiceWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
case ResourceSession:
|
|
|
|
switch lowerAccess {
|
|
|
|
case "read":
|
|
|
|
return authz.SessionRead(segment, ctx), nil
|
|
|
|
case "write":
|
|
|
|
return authz.SessionWrite(segment, ctx), nil
|
|
|
|
}
|
|
|
|
default:
|
2019-12-18 18:44:32 +00:00
|
|
|
if processed, decision, err := enforceEnterprise(authz, rsc, segment, lowerAccess, ctx); processed {
|
2019-12-06 14:25:26 +00:00
|
|
|
return decision, err
|
|
|
|
}
|
|
|
|
return Deny, fmt.Errorf("Invalid ACL resource requested: %q", rsc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Deny, fmt.Errorf("Invalid access level for %s resource: %s", rsc, access)
|
|
|
|
}
|
2020-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
// NewAuthorizerFromRules is a convenience function to invoke NewPolicyFromSource followed by NewPolicyAuthorizer with
|
|
|
|
// the parse policy.
|
|
|
|
func NewAuthorizerFromRules(id string, revision uint64, rules string, syntax SyntaxVersion, conf *Config, meta *EnterprisePolicyMeta) (Authorizer, error) {
|
|
|
|
policy, err := NewPolicyFromSource(id, revision, rules, syntax, conf, meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewPolicyAuthorizer([]*Policy{policy}, conf)
|
|
|
|
}
|