- add trusted proxy settings, migration, API, and UI - derive client addresses from X-Forwarded-For behind trusted hops - enforce delivery policy for listener batches and record denials
264 lines
8.7 KiB
Go
264 lines
8.7 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
cedar "github.com/cedar-policy/cedar-go"
|
|
"github.com/cedar-policy/cedar-go/types"
|
|
expast "github.com/cedar-policy/cedar-go/x/exp/ast"
|
|
schema "github.com/cedar-policy/cedar-go/x/exp/schema"
|
|
validate "github.com/cedar-policy/cedar-go/x/exp/schema/validate"
|
|
)
|
|
|
|
const schemaText = `namespace Archie {
|
|
entity Identity in [Org] = {"org": String, "role": String};
|
|
entity Org;
|
|
entity Workspace in [Org] = {"org": String, "environment": String};
|
|
entity Object in [Org, Workspace] = {"org": String, "workspace": Workspace, "kind": String, "owner": String, "state": String};
|
|
action read appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action create appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action update appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action delete appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action approve appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action run appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action read_logs appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action read_secret appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action manage_members appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action manage_identities appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action deliver appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action manage_policies appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action read_audit appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
action read_usage appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} };
|
|
}`
|
|
|
|
// Problem is one stored policy the engine could not validate.
|
|
type Problem struct {
|
|
Policy Policy
|
|
Err error
|
|
}
|
|
|
|
type levelSet struct {
|
|
at Level
|
|
ids []string
|
|
set *cedar.PolicySet
|
|
invalid error
|
|
}
|
|
|
|
func (l *levelSet) active() bool { return l != nil && (len(l.ids) > 0 || l.invalid != nil) }
|
|
|
|
// Engine evaluates the instance -> org -> workspace -> object Cedar policy chain.
|
|
type Engine struct {
|
|
instance *levelSet
|
|
orgs map[OrgID]*levelSet
|
|
spaces map[spaceKey]*levelSet
|
|
objects map[objectKey]*levelSet
|
|
problems []Problem
|
|
pending bool
|
|
}
|
|
|
|
var pendingEngine = &Engine{pending: true}
|
|
|
|
type (
|
|
spaceKey struct {
|
|
org OrgID
|
|
ws WorkspaceID
|
|
}
|
|
objectKey struct {
|
|
org OrgID
|
|
kind ResourceKind
|
|
id string
|
|
}
|
|
)
|
|
|
|
var (
|
|
_ Authorizer = (*Engine)(nil)
|
|
_ Validator = (*Engine)(nil)
|
|
)
|
|
|
|
// NewEngine builds an engine from the stored policies. An invalid policy makes
|
|
// its level deny everything and is reported in Problems.
|
|
func NewEngine(policies []Policy) *Engine {
|
|
e := &Engine{
|
|
orgs: map[OrgID]*levelSet{},
|
|
spaces: map[spaceKey]*levelSet{},
|
|
objects: map[objectKey]*levelSet{},
|
|
}
|
|
for _, p := range policies {
|
|
lvl := e.level(p)
|
|
policy, err := compile(p)
|
|
if err != nil {
|
|
lvl.ids = append(lvl.ids, p.ID)
|
|
lvl.invalid = err
|
|
e.problems = append(e.problems, Problem{Policy: p, Err: err})
|
|
continue
|
|
}
|
|
lvl.ids = append(lvl.ids, p.ID)
|
|
lvl.set.Add(types.PolicyID(p.ID), policy)
|
|
}
|
|
return e
|
|
}
|
|
|
|
// Ready reports whether the engine was built from stored policies.
|
|
func (e *Engine) Ready() bool { return !e.pending }
|
|
|
|
// Problems reports every stored policy the engine could not compile.
|
|
func (e *Engine) Problems() []Problem { return e.problems }
|
|
|
|
func (e *Engine) level(p Policy) *levelSet {
|
|
switch p.Level {
|
|
case LevelInstance:
|
|
if e.instance == nil {
|
|
e.instance = &levelSet{at: p.Level, set: cedar.NewPolicySet()}
|
|
}
|
|
return e.instance
|
|
case LevelOrg:
|
|
id := p.OrgID
|
|
if lvl, ok := e.orgs[id]; ok {
|
|
return lvl
|
|
}
|
|
lvl := &levelSet{at: p.Level, set: cedar.NewPolicySet()}
|
|
e.orgs[id] = lvl
|
|
return lvl
|
|
case LevelWorkspace:
|
|
id := spaceKey{p.OrgID, p.WorkspaceID}
|
|
if lvl, ok := e.spaces[id]; ok {
|
|
return lvl
|
|
}
|
|
lvl := &levelSet{at: p.Level, set: cedar.NewPolicySet()}
|
|
e.spaces[id] = lvl
|
|
return lvl
|
|
case LevelObject:
|
|
id := objectKey{p.OrgID, p.ObjectKind, p.ObjectID}
|
|
if lvl, ok := e.objects[id]; ok {
|
|
return lvl
|
|
}
|
|
lvl := &levelSet{at: p.Level, set: cedar.NewPolicySet()}
|
|
e.objects[id] = lvl
|
|
return lvl
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Validate checks one policy against the Cedar schema.
|
|
func (e *Engine) Validate(p Policy) error {
|
|
_, err := compile(p)
|
|
return err
|
|
}
|
|
|
|
func compile(p Policy) (*cedar.Policy, error) {
|
|
if err := p.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
pls, err := cedar.NewPolicyListFromBytes(p.ID, []byte(p.Text))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrPolicyInvalidText, err)
|
|
}
|
|
if len(pls) != 1 {
|
|
return nil, fmt.Errorf("%w: %s carries %d policies, want exactly one", ErrPolicyInvalidText, p.ID, len(pls))
|
|
}
|
|
v, err := schemaValidator()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := v.Policy(p.ID, (*expast.Policy)(pls[0].AST())); err != nil {
|
|
return nil, fmt.Errorf("%w: %w", ErrPolicyInvalidText, err)
|
|
}
|
|
return pls[0], nil
|
|
}
|
|
|
|
// Authorize evaluates the policy chain for one request.
|
|
func (e *Engine) Authorize(_ context.Context, p Principal, a Action, r Resource, c AccessContext) Decision {
|
|
if e.pending {
|
|
return Unavailable()
|
|
}
|
|
resOrg := r.Org
|
|
if resOrg == "" {
|
|
resOrg = p.Org
|
|
}
|
|
if p.Org == "" || resOrg != p.Org {
|
|
return DeniedAt(LevelInstance, []string{CrossOrgForbidID})
|
|
}
|
|
// Only org-level policies grant access within an org. evaluate skips empty
|
|
// levels, so without this check an org with no org-level policies would be
|
|
// allowed.
|
|
if !e.orgs[resOrg].active() {
|
|
return DeniedAt(LevelOrg, nil)
|
|
}
|
|
return evaluate(e.chain(p, r), buildEntities(p, r), buildRequest(p, a, r, c))
|
|
}
|
|
|
|
// AuthorizeDelivery decides whether addr may deliver an event to a source.
|
|
func (e *Engine) AuthorizeDelivery(_ context.Context, orgID OrgID, sourcePath, addr string) Decision {
|
|
if e.pending {
|
|
return Unavailable()
|
|
}
|
|
sender := Principal{IdentityID: SenderID, Org: orgID}
|
|
resource := Resource{Kind: KindSource, ID: sourcePath, Org: orgID}
|
|
levels := []*levelSet{e.instance}
|
|
if l, ok := e.objects[objectKey{orgID, KindSource, sourcePath}]; ok {
|
|
levels = append(levels, l)
|
|
}
|
|
return evaluate(levels, buildEntities(sender, resource), buildRequest(sender, ActionDeliver, resource, AccessContext{Addr: addr}))
|
|
}
|
|
|
|
func evaluate(levels []*levelSet, entities types.EntityMap, request types.Request) Decision {
|
|
for _, lvl := range levels {
|
|
if !lvl.active() {
|
|
continue
|
|
}
|
|
if lvl.invalid != nil {
|
|
decision := DeniedAt(lvl.at, lvl.ids)
|
|
decision.Err = lvl.invalid
|
|
return decision
|
|
}
|
|
decision, diag := cedar.Authorize(lvl.set, entities, request)
|
|
if decision == cedar.Allow {
|
|
continue
|
|
}
|
|
ids := make([]string, 0, len(diag.Reasons))
|
|
for _, reason := range diag.Reasons {
|
|
ids = append(ids, string(reason.PolicyID))
|
|
}
|
|
if len(ids) == 0 {
|
|
ids = lvl.ids
|
|
}
|
|
return DeniedAt(lvl.at, ids)
|
|
}
|
|
return Allowed()
|
|
}
|
|
|
|
func (e *Engine) chain(p Principal, r Resource) []*levelSet {
|
|
var out []*levelSet
|
|
if e.instance == nil || e.instance.invalid == nil || r.Kind != KindPolicy || !p.InstanceAdmin() {
|
|
out = append(out, e.instance)
|
|
}
|
|
if l, ok := e.orgs[r.Org]; ok {
|
|
out = append(out, l)
|
|
}
|
|
if r.Workspace != "" {
|
|
if l, ok := e.spaces[spaceKey{r.Org, r.Workspace}]; ok {
|
|
out = append(out, l)
|
|
}
|
|
}
|
|
if r.Org != "" && r.ID != "" {
|
|
if l, ok := e.objects[objectKey{r.Org, r.Kind, r.ID}]; ok {
|
|
out = append(out, l)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
var schemaValidator = sync.OnceValues(func() (*validate.Validator, error) {
|
|
var sch schema.Schema
|
|
if err := sch.UnmarshalCedar([]byte(schemaText)); err != nil {
|
|
return nil, err
|
|
}
|
|
resolved, err := sch.Resolve()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return validate.New(resolved), nil
|
|
})
|