Copied from internal/domain/identity, internal/domain/org, internal/domain/access, internal/infrastructure/access and internal/infrastructure/postgres.
119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
const versionTimeout = 5 * time.Second
|
|
|
|
// Live is an engine rebuilt from stored policies whenever their revision
|
|
// moves. A check that cannot read the store refuses so an edit never drops
|
|
// out behind a stale cache.
|
|
type Live struct {
|
|
version func(context.Context) (int64, error)
|
|
load func(context.Context) ([]Policy, error)
|
|
log *slog.Logger
|
|
|
|
engine atomic.Pointer[Engine]
|
|
mu sync.Mutex
|
|
stamp atomic.Int64
|
|
}
|
|
|
|
// NewPending returns a chain whose engine refuses every request until the
|
|
// stored policies load.
|
|
func NewPending(version func(context.Context) (int64, error), load func(context.Context) ([]Policy, error), log *slog.Logger) *Live {
|
|
l := &Live{
|
|
version: version,
|
|
load: func(ctx context.Context) ([]Policy, error) {
|
|
return load(WithoutPrincipal(ctx))
|
|
},
|
|
log: log,
|
|
}
|
|
l.engine.Store(pendingEngine)
|
|
l.stamp.Store(-1)
|
|
return l
|
|
}
|
|
|
|
// Ready reports whether the stored policies have loaded.
|
|
func (l *Live) Ready() bool { return l.engine.Load().Ready() }
|
|
|
|
// Reload rebuilds the engine from the store immediately.
|
|
func (l *Live) Reload(ctx context.Context) error {
|
|
ctx, cancel := context.WithTimeout(ctx, versionTimeout)
|
|
defer cancel()
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
version, err := l.version(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return l.apply(ctx, version)
|
|
}
|
|
|
|
func (l *Live) current(ctx context.Context) *Engine {
|
|
ctx, cancel := context.WithTimeout(ctx, versionTimeout)
|
|
defer cancel()
|
|
|
|
// Read the version before the policies so a stamped engine can never hold
|
|
// content older than its stamp.
|
|
version, err := l.version(ctx)
|
|
if err != nil {
|
|
l.log.WarnContext(ctx, "access policy version unavailable; refusing until it is readable", "err", err)
|
|
return pendingEngine
|
|
}
|
|
if version == l.stamp.Load() {
|
|
return l.engine.Load()
|
|
}
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if version == l.stamp.Load() {
|
|
return l.engine.Load()
|
|
}
|
|
if err := l.apply(ctx, version); err != nil {
|
|
l.log.WarnContext(ctx, "access policies not reloaded; refusing until they load", "err", err)
|
|
return pendingEngine
|
|
}
|
|
return l.engine.Load()
|
|
}
|
|
|
|
func (l *Live) apply(ctx context.Context, version int64) error {
|
|
stored, err := l.load(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
engine := NewEngine(stored)
|
|
for _, problem := range engine.Problems() {
|
|
l.log.ErrorContext(ctx, "stored access policy is invalid and denies its level",
|
|
"policy", problem.Policy.ID, "level", problem.Policy.Level, "err", problem.Err)
|
|
}
|
|
l.engine.Store(engine)
|
|
l.stamp.Store(version)
|
|
return nil
|
|
}
|
|
|
|
// Authorize evaluates the live policy chain for one request.
|
|
func (l *Live) Authorize(ctx context.Context, p Principal, a Action, r Resource, c AccessContext) Decision {
|
|
return l.current(ctx).Authorize(ctx, p, a, r, c)
|
|
}
|
|
|
|
// AuthorizeDelivery decides whether addr may deliver an event to a source.
|
|
func (l *Live) AuthorizeDelivery(ctx context.Context, orgID OrgID, sourcePath, addr string) Decision {
|
|
return l.current(ctx).AuthorizeDelivery(ctx, orgID, sourcePath, addr)
|
|
}
|
|
|
|
// Problems reports the current engine's invalid policies.
|
|
func (l *Live) Problems() []Problem { return l.engine.Load().Problems() }
|
|
|
|
// StartLive loads the stored policies and keeps them current as they change.
|
|
func StartLive(ctx context.Context, list func(context.Context) ([]Policy, error), version func(context.Context) (int64, error), log *slog.Logger) *Live {
|
|
live := NewPending(version, list, log)
|
|
if err := live.Reload(ctx); err != nil {
|
|
log.WarnContext(ctx, "access policies not loaded; requests are refused until they load", "err", err)
|
|
}
|
|
return live
|
|
}
|