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
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
// ChainedAuthorizer can combine multiple Authorizers into one.
|
|
|
|
// Each Authorizer in the chain is asked (in order) for an
|
|
|
|
// enforcement decision. The first non-Default decision that
|
|
|
|
// is rendered by an Authorizer in the chain will be used
|
|
|
|
// as the overall decision of the ChainedAuthorizer
|
|
|
|
type ChainedAuthorizer struct {
|
|
|
|
chain []Authorizer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChainedAuthorizer creates a ChainedAuthorizer with the provided
|
|
|
|
// chain of Authorizers. The slice provided should be in the order of
|
|
|
|
// most precedent Authorizer at the beginning and least precedent
|
|
|
|
// Authorizer at the end.
|
|
|
|
func NewChainedAuthorizer(chain []Authorizer) *ChainedAuthorizer {
|
|
|
|
return &ChainedAuthorizer{
|
|
|
|
chain: chain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:06:16 +00:00
|
|
|
func (c *ChainedAuthorizer) AuthorizerChain() []Authorizer {
|
|
|
|
return c.chain
|
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
func (c *ChainedAuthorizer) executeChain(enforce func(authz Authorizer) EnforcementDecision) EnforcementDecision {
|
|
|
|
for _, authz := range c.chain {
|
|
|
|
decision := enforce(authz)
|
|
|
|
if decision != Default {
|
|
|
|
return decision
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLRead checks for permission to list all the ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) ACLRead(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ACLRead(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLWrite checks for permission to manipulate ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) ACLWrite(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ACLWrite(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentRead checks for permission to read from agent endpoints for a
|
|
|
|
// given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) AgentRead(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.AgentRead(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentWrite checks for permission to make changes via agent endpoints
|
|
|
|
// for a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) AgentWrite(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.AgentWrite(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventRead determines if a specific event can be queried.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) EventRead(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.EventRead(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventWrite determines if a specific event may be fired.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) EventWrite(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.EventWrite(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:22:51 +00:00
|
|
|
// IdentityRead checks for permission to read a given workload identity.
|
|
|
|
func (c *ChainedAuthorizer) IdentityRead(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IdentityRead(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityReadAll checks for permission to read all workload identities.
|
|
|
|
func (c *ChainedAuthorizer) IdentityReadAll(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IdentityReadAll(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityWrite checks for permission to create or update a given
|
|
|
|
// workload identity.
|
|
|
|
func (c *ChainedAuthorizer) IdentityWrite(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IdentityWrite(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityWriteAny checks for write permission on any workload identity.
|
|
|
|
func (c *ChainedAuthorizer) IdentityWriteAny(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IdentityWriteAny(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *ChainedAuthorizer) IntentionDefaultAllow(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IntentionDefaultAllow(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionRead determines if a specific intention can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) IntentionRead(prefix string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IntentionRead(prefix, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionWrite determines if a specific intention can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) IntentionWrite(prefix string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.IntentionWrite(prefix, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyList checks for permission to list keys under a prefix
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) KeyList(keyPrefix string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyList(keyPrefix, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyRead checks for permission to read a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) KeyRead(key string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyRead(key, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyWrite checks for permission to write a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) KeyWrite(key string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyWrite(key, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
func (c *ChainedAuthorizer) KeyWritePrefix(keyPrefix string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyWritePrefix(keyPrefix, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringRead determines if the encryption keyring used in
|
|
|
|
// the gossip layer can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) KeyringRead(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyringRead(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringWrite determines if the keyring can be manipulated
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) KeyringWrite(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.KeyringWrite(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// MeshRead determines if the read-only Consul mesh functions
|
|
|
|
// can be used.
|
|
|
|
func (c *ChainedAuthorizer) MeshRead(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.MeshRead(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeshWrite determines if the state-changing Consul mesh
|
|
|
|
// functions can be used.
|
|
|
|
func (c *ChainedAuthorizer) MeshWrite(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.MeshWrite(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:42:23 +00:00
|
|
|
// PeeringRead determines if the read-only Consul peering functions
|
|
|
|
// can be used.
|
|
|
|
func (c *ChainedAuthorizer) PeeringRead(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.PeeringRead(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeeringWrite determines if the state-changing Consul peering
|
|
|
|
// functions can be used.
|
|
|
|
func (c *ChainedAuthorizer) PeeringWrite(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.PeeringWrite(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *ChainedAuthorizer) NodeRead(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.NodeRead(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
func (c *ChainedAuthorizer) NodeReadAll(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.NodeReadAll(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *ChainedAuthorizer) NodeWrite(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.NodeWrite(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorRead determines if the read-only Consul operator functions
|
|
|
|
// can be used.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) OperatorRead(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.OperatorRead(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorWrite determines if the state-changing Consul operator
|
|
|
|
// functions can be used.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) OperatorWrite(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.OperatorWrite(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
func (c *ChainedAuthorizer) PreparedQueryRead(query string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.PreparedQueryRead(query, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreparedQueryWrite determines if a specific prepared query can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) PreparedQueryWrite(query string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.PreparedQueryWrite(query, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceRead checks for permission to read a given service
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) ServiceRead(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ServiceRead(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
func (c *ChainedAuthorizer) ServiceReadAll(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ServiceReadAll(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *ChainedAuthorizer) ServiceWrite(name string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ServiceWrite(name, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-24 11:25:05 +00:00
|
|
|
// ServiceWriteAny checks for write permission on any service
|
|
|
|
func (c *ChainedAuthorizer) ServiceWriteAny(entCtx *AuthorizerContext) EnforcementDecision {
|
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.ServiceWriteAny(entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (c *ChainedAuthorizer) SessionRead(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.SessionRead(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionWrite checks for permission to create sessions for a given
|
|
|
|
// node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) SessionWrite(node string, entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.SessionWrite(node, entCtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot checks for permission to take and restore snapshots.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (c *ChainedAuthorizer) Snapshot(entCtx *AuthorizerContext) EnforcementDecision {
|
2019-10-15 20:58:50 +00:00
|
|
|
return c.executeChain(func(authz Authorizer) EnforcementDecision {
|
|
|
|
return authz.Snapshot(entCtx)
|
|
|
|
})
|
|
|
|
}
|
2022-03-11 02:48:27 +00:00
|
|
|
|
|
|
|
func (c *ChainedAuthorizer) ToAllowAuthorizer() AllowAuthorizer {
|
|
|
|
return AllowAuthorizer{Authorizer: c}
|
|
|
|
}
|