guard

package
v0.69.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Conditional policy writes

Use GetPolicySnapshot followed by UpdatePolicyIfUnchanged or DeletePolicyIfUnchanged for read/change/write operations. The snapshot retains the exact stored JSON, including its original formatting. Policy IDs cannot change.

A missing policy returns a nil snapshot. A conditional write returns false when the row changed or disappeared. Updates that change no stored values can also return false. Check errors before treating false as a conflict. Only retry after reading a new snapshot and recomputing the intended change; do not retry a stale replacement unchanged.

Snapshot reads bypass the request cache. Conditional writes clear that cache, including on conflicts, so authorization checks on the next attempt read current policies. The caller remains responsible for authorization.

The existing unconditional write methods remain available for compatibility. Editors must use conditional writes too if stale edits must not overwrite newer changes. Custom implementations of Guard or Manager must implement the new snapshot and conditional-write methods.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuditLogger added in v0.8.4

type AuditLogger interface {
	ladon.AuditLogger
}

func NewAuditLogger added in v0.8.4

func NewAuditLogger(config cfg.Config, logger log.Logger) (AuditLogger, error)

type CachedManager added in v0.37.3

type CachedManager struct {
	// contains filtered or unexported fields
}

func (CachedManager) Create added in v0.37.3

func (m CachedManager) Create(ctx context.Context, pol ladon.Policy) error

func (CachedManager) Delete added in v0.37.3

func (m CachedManager) Delete(ctx context.Context, id string) error

func (CachedManager) DeleteIfUnchanged added in v0.69.2

func (m CachedManager) DeleteIfUnchanged(ctx context.Context, before PolicySnapshot) (bool, error)

DeleteIfUnchanged invalidates cached authorization even when the write conflicts.

func (CachedManager) FindPoliciesForResource added in v0.37.3

func (m CachedManager) FindPoliciesForResource(ctx context.Context, resource string) (ladon.Policies, error)

func (CachedManager) FindPoliciesForSubject added in v0.37.3

func (m CachedManager) FindPoliciesForSubject(ctx context.Context, subject string) (ladon.Policies, error)

func (CachedManager) FindRequestCandidates added in v0.37.3

func (m CachedManager) FindRequestCandidates(ctx context.Context, r *ladon.Request) (ladon.Policies, error)

func (CachedManager) Get added in v0.37.3

func (m CachedManager) Get(ctx context.Context, id string) (ladon.Policy, error)

func (CachedManager) GetAll added in v0.37.3

func (m CachedManager) GetAll(ctx context.Context, limit, offset int64) (ladon.Policies, error)

func (CachedManager) GetSnapshot added in v0.69.2

func (m CachedManager) GetSnapshot(ctx context.Context, id string) (*PolicySnapshot, error)

GetSnapshot bypasses request caching so conflict retries read the current policy.

func (CachedManager) Update added in v0.37.3

func (m CachedManager) Update(ctx context.Context, pol ladon.Policy) error

func (CachedManager) UpdateIfUnchanged added in v0.69.2

func (m CachedManager) UpdateIfUnchanged(ctx context.Context, before PolicySnapshot, after ladon.Policy) (bool, error)

UpdateIfUnchanged invalidates cached authorization even when the write conflicts.

type Guard

type Guard interface {
	CreatePolicy(ctx context.Context, pol ladon.Policy) error
	DeletePolicy(ctx context.Context, pol ladon.Policy) error
	DeletePolicyIfUnchanged(ctx context.Context, before PolicySnapshot) (bool, error)
	GetPolicy(ctx context.Context, id string) (ladon.Policy, error)
	GetPolicySnapshot(ctx context.Context, id string) (*PolicySnapshot, error)
	GetPolicies(ctx context.Context) (ladon.Policies, error)
	GetPoliciesBySubject(ctx context.Context, subject string) (ladon.Policies, error)
	IsAllowed(ctx context.Context, request *ladon.Request) error
	UpdatePolicy(ctx context.Context, pol ladon.Policy) error
	UpdatePolicyIfUnchanged(ctx context.Context, before PolicySnapshot, after ladon.Policy) (bool, error)
}

type LadonGuard

type LadonGuard struct {
	// contains filtered or unexported fields
}

func NewGuard

func NewGuard(ctx context.Context, config cfg.Config, logger log.Logger) (*LadonGuard, error)

func NewGuardWithInterfaces

func NewGuardWithInterfaces(manager Manager, logger AuditLogger) *LadonGuard

func (LadonGuard) CreatePolicy

func (g LadonGuard) CreatePolicy(ctx context.Context, pol ladon.Policy) error

func (LadonGuard) DeletePolicy

func (g LadonGuard) DeletePolicy(ctx context.Context, pol ladon.Policy) error

func (LadonGuard) DeletePolicyIfUnchanged added in v0.69.2

func (g LadonGuard) DeletePolicyIfUnchanged(ctx context.Context, before PolicySnapshot) (bool, error)

DeletePolicyIfUnchanged deletes a policy only when its stored value still matches before.

func (LadonGuard) GetPolicies

func (g LadonGuard) GetPolicies(ctx context.Context) (ladon.Policies, error)

func (LadonGuard) GetPoliciesBySubject

func (g LadonGuard) GetPoliciesBySubject(ctx context.Context, subject string) (ladon.Policies, error)

func (LadonGuard) GetPolicy

func (g LadonGuard) GetPolicy(ctx context.Context, id string) (ladon.Policy, error)

func (LadonGuard) GetPolicySnapshot added in v0.69.2

func (g LadonGuard) GetPolicySnapshot(ctx context.Context, id string) (*PolicySnapshot, error)

GetPolicySnapshot reads an uncached policy and its original stored value. Missing policies return nil.

func (LadonGuard) IsAllowed

func (g LadonGuard) IsAllowed(ctx context.Context, request *ladon.Request) error

func (LadonGuard) UpdatePolicy

func (g LadonGuard) UpdatePolicy(ctx context.Context, pol ladon.Policy) error

func (LadonGuard) UpdatePolicyIfUnchanged added in v0.69.2

func (g LadonGuard) UpdatePolicyIfUnchanged(ctx context.Context, before PolicySnapshot, after ladon.Policy) (bool, error)

UpdatePolicyIfUnchanged updates a policy only when its stored value still matches before.

type Manager

type Manager interface {
	ladon.Manager
	GetSnapshot(ctx context.Context, id string) (*PolicySnapshot, error)
	UpdateIfUnchanged(ctx context.Context, before PolicySnapshot, after ladon.Policy) (bool, error)
	DeleteIfUnchanged(ctx context.Context, before PolicySnapshot) (bool, error)
}

func NewCachedManagerWithInterfaces added in v0.37.3

func NewCachedManagerWithInterfaces(manager Manager) Manager

type PolicySnapshot added in v0.69.2

type PolicySnapshot struct {
	Policy *ladon.DefaultPolicy
	// contains filtered or unexported fields
}

PolicySnapshot retains the exact stored value for conditional writes. Obtain snapshots through GetPolicySnapshot or GetSnapshot, not by constructing them.

type SqlManager

type SqlManager struct {
	// contains filtered or unexported fields
}

func NewSqlManager

func NewSqlManager(ctx context.Context, config cfg.Config, logger log.Logger) (*SqlManager, error)

func NewSqlManagerWithInterfaces

func NewSqlManagerWithInterfaces(logger log.Logger, dbClient db.Client) *SqlManager

func (SqlManager) Create

func (m SqlManager) Create(ctx context.Context, pol ladon.Policy) error

func (SqlManager) Delete

func (m SqlManager) Delete(ctx context.Context, id string) error

func (SqlManager) DeleteIfUnchanged added in v0.69.2

func (m SqlManager) DeleteIfUnchanged(ctx context.Context, before PolicySnapshot) (bool, error)

DeleteIfUnchanged returns false on a changed or missing policy.

func (SqlManager) FindPoliciesForResource

func (m SqlManager) FindPoliciesForResource(ctx context.Context, resource string) (ladon.Policies, error)

func (SqlManager) FindPoliciesForSubject

func (m SqlManager) FindPoliciesForSubject(ctx context.Context, subject string) (ladon.Policies, error)

func (SqlManager) FindRequestCandidates

func (m SqlManager) FindRequestCandidates(ctx context.Context, r *ladon.Request) (ladon.Policies, error)

func (SqlManager) Get

func (m SqlManager) Get(ctx context.Context, id string) (ladon.Policy, error)

func (SqlManager) GetAll

func (m SqlManager) GetAll(ctx context.Context, limit, offset int64) (ladon.Policies, error)

func (SqlManager) GetSnapshot added in v0.69.2

func (m SqlManager) GetSnapshot(ctx context.Context, id string) (*PolicySnapshot, error)

GetSnapshot reads the original stored payload without reconstructing it from a policy. Missing policies return nil without an error.

func (SqlManager) Update

func (m SqlManager) Update(ctx context.Context, pol ladon.Policy) error

func (SqlManager) UpdateIfUnchanged added in v0.69.2

func (m SqlManager) UpdateIfUnchanged(ctx context.Context, before PolicySnapshot, after ladon.Policy) (bool, error)

UpdateIfUnchanged returns false on a changed or missing policy, or an update with no changed values.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL