Documentation
¶
Index ¶
- Variables
- func WithMatcherOptions(matcher MatcherOptions) func(*BunAdapter)
- type BunAdapter
- func (adapter *BunAdapter) AddPolicy(sec string, ptype string, rule []string) error
- func (adapter *BunAdapter) CreateJournal(ctx context.Context, schemaName string, readerRoles ...string) error
- func (adapter *BunAdapter) EnableJournalCleanup(ctx context.Context, schemaName string, readerRoles ...string) error
- func (adapter *BunAdapter) EnableJournalNotifications(ctx context.Context, schemaName string) error
- func (adapter *BunAdapter) LoadPolicy(model model.Model) error
- func (adapter *BunAdapter) NewJournalReader(ctx context.Context, schemaName string, m model.Model, ...) (*JournalReader, error)
- func (adapter *BunAdapter) PruneJournal(ctx context.Context, schemaName string) (int, error)
- func (adapter *BunAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
- func (adapter *BunAdapter) RemovePolicy(sec string, ptype string, rule []string) error
- func (adapter *BunAdapter) SavePolicy(model model.Model) error
- type CasbinPolicy
- type JournalReader
- func (reader *JournalReader) Enforce(values ...interface{}) (bool, error)
- func (reader *JournalReader) EnforceCached(values ...interface{}) (bool, error)
- func (reader *JournalReader) EnforceContext(ctx context.Context, values ...interface{}) (bool, error)
- func (reader *JournalReader) Reload(ctx context.Context) error
- func (reader *JournalReader) Run(ctx context.Context) error
- func (reader *JournalReader) Status() JournalStatus
- func (reader *JournalReader) Sync(ctx context.Context) error
- type JournalReaderOptions
- type JournalStatus
- type MatcherOptions
Constants ¶
This section is empty.
Variables ¶
var ErrJournalNotReady = errors.New("policy journal is not synchronized")
ErrJournalNotReady indicates that authorization data cannot currently be trusted.
Functions ¶
func WithMatcherOptions ¶
func WithMatcherOptions(matcher MatcherOptions) func(*BunAdapter)
WithMatcherOptions overrides default matching options. If some of keys are empty strings than default values will be applied
Types ¶
type BunAdapter ¶
BunAdapter is just wrapper around *bun.DB
func NewBunAdapter ¶
func NewBunAdapter(bunConnection *bun.DB, opts ...func(*BunAdapter)) *BunAdapter
NewBunAdapter returns new *BunAdapter. Connections to database must be provided. Other arguments are optional
func (*BunAdapter) AddPolicy ¶
func (adapter *BunAdapter) AddPolicy(sec string, ptype string, rule []string) error
AddPolicy adds a policy rule to the storage. Needed for AutoSave, see the ref. https://casbin.org/docs/adapters/#autosave
func (*BunAdapter) CreateJournal ¶ added in v0.0.2
func (adapter *BunAdapter) CreateJournal(ctx context.Context, schemaName string, readerRoles ...string) error
CreateJournal installs a transactional change journal in a new, dedicated schema. It requires the policy table owner or a migration role with equivalent privileges. Optional reader roles receive schema usage and SELECT on the journal tables. Journal creation does not start synchronization with an enforcer.
func (*BunAdapter) EnableJournalCleanup ¶ added in v0.0.2
func (adapter *BunAdapter) EnableJournalCleanup(ctx context.Context, schemaName string, readerRoles ...string) error
EnableJournalCleanup installs bounded cleanup for an existing journal as its owner. Reader roles can invoke cleanup but cannot choose its retention period or delete arbitrary rows.
func (*BunAdapter) EnableJournalNotifications ¶ added in v0.0.2
func (adapter *BunAdapter) EnableJournalNotifications(ctx context.Context, schemaName string) error
EnableJournalNotifications installs commit notifications for an existing journal as its owner.
func (*BunAdapter) LoadPolicy ¶
func (adapter *BunAdapter) LoadPolicy(model model.Model) error
LoadPolicy loads all policy rules from the storage
func (*BunAdapter) NewJournalReader ¶ added in v0.0.2
func (adapter *BunAdapter) NewJournalReader(ctx context.Context, schemaName string, m model.Model, options JournalReaderOptions) (*JournalReader, error)
NewJournalReader loads policies and the journal position from one database snapshot.
func (*BunAdapter) PruneJournal ¶ added in v0.0.2
PruneJournal removes up to 1000 complete transactions older than seven days.
func (*BunAdapter) RemoveFilteredPolicy ¶
func (adapter *BunAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
RemoveFilteredPolicy removes policy rules that match the filter from the storage. Needed for AutoSave, see the ref. https://casbin.org/docs/adapters/#autosave
func (*BunAdapter) RemovePolicy ¶
func (adapter *BunAdapter) RemovePolicy(sec string, ptype string, rule []string) error
RemovePolicy removes a policy rule from the storage. Needed for AutoSave, see the ref. https://casbin.org/docs/adapters/#autosave
func (*BunAdapter) SavePolicy ¶
func (adapter *BunAdapter) SavePolicy(model model.Model) error
SavePolicy saves all policy rules to the storage
type CasbinPolicy ¶
type CasbinPolicy struct {
bun.BaseModel `bun:"casbin_policies,alias:t"`
ID int `bun:"id,pk,autoincrement" json:"id"`
PType string `bun:"ptype,type:varchar(2),notnull,default:'p'" json:"ptype"`
V0 *string `bun:"v0,type:varchar(256)" json:"v0"`
V1 *string `bun:"v1,type:varchar(256)" json:"v1"`
V2 *string `bun:"v2,type:varchar(256)" json:"v2"`
V3 *string `bun:"v3,type:varchar(256)" json:"v3"`
V4 *string `bun:"v4,type:varchar(256)" json:"v4"`
V5 *string `bun:"v5,type:varchar(256)" json:"v5"`
}
CasbinPolicy is database storage format following the below https://casbin.org/docs/policy-storage#database-storage-format Template on which Golang struct has been prepared: CREATE TABLE casbin_policies (
id int4 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE) NOT NULL, ptype varchar(2) DEFAULT 'p'::character varying NOT NULL, v0 varchar(256) null, v1 varchar(256) null, v2 varchar(256) null, v3 varchar(256) null, v4 varchar(256) null, v5 varchar(256) null, CONSTRAINT casbin_policies_pk PRIMARY KEY (id), CONSTRAINT casbin_policies_unique UNIQUE NULLS NOT DISTINCT (ptype, v0, v1, v2, v3, v4, v5) -- Aware! This will work only for Postgres 15+. For the ref. see: https://stackoverflow.com/a/8289253/6026885
);
func NewCasbinPolicyFrom ¶
func NewCasbinPolicyFrom(ptype string, rule []string) (CasbinPolicy, error)
NewCasbinPolicyFrom preserves empty values and leaves unused columns NULL.
type JournalReader ¶ added in v0.0.2
type JournalReader struct {
// contains filtered or unexported fields
}
JournalReader owns a read-only authorization view; policies are changed in PostgreSQL.
func (*JournalReader) Enforce ¶ added in v0.0.2
func (reader *JournalReader) Enforce(values ...interface{}) (bool, error)
Enforce checks against policies at least as recent as the journal position read for this call. The adapter must connect to the primary PostgreSQL database.
func (*JournalReader) EnforceCached ¶ added in v0.0.2
func (reader *JournalReader) EnforceCached(values ...interface{}) (bool, error)
EnforceCached checks only the local view and may miss recently committed changes. Synchronization errors, known unfinished catch-up and MaxStaleness block the check.
func (*JournalReader) EnforceContext ¶ added in v0.0.2
func (reader *JournalReader) EnforceContext(ctx context.Context, values ...interface{}) (bool, error)
EnforceContext synchronizes to a fixed journal position before evaluating the request. Context cancellation or a synchronization error returns false and an error.
func (*JournalReader) Reload ¶ added in v0.0.2
func (reader *JournalReader) Reload(ctx context.Context) error
Reload replaces local policies and the cursor with a fresh, consistent database snapshot.
func (*JournalReader) Run ¶ added in v0.0.2
func (reader *JournalReader) Run(ctx context.Context) error
Run maintains retention and enables notifications or polling according to its options. Strict checks do not require Run and continue working after it stops.
func (*JournalReader) Status ¶ added in v0.0.2
func (reader *JournalReader) Status() JournalStatus
Status returns the current cursor, synchronization time and errors.
func (*JournalReader) Sync ¶ added in v0.0.2
func (reader *JournalReader) Sync(ctx context.Context) error
Sync applies complete journal transactions or reloads a full snapshot for large deltas or missing history. A failed attempt leaves the cursor unchanged and suspends authorization until recovery.
type JournalReaderOptions ¶ added in v0.0.2
type JournalReaderOptions struct {
// Listen enables notification-driven synchronization in Run through a dedicated pgdriver connection.
Listen bool
// PollInterval enables background synchronization when positive. Zero disables polling.
PollInterval time.Duration
// Timeout bounds database synchronization, including waiting for another updater.
Timeout time.Duration
// MaxStaleness limits the age of policies used by EnforceCached.
MaxStaleness time.Duration
// BatchLimit limits journal transactions per step. Large deltas or missing history reload a full snapshot.
BatchLimit int
// Configure runs on each new enforcer before its role links are built.
Configure func(*casbin.Enforcer) error
}
JournalReaderOptions controls synchronization and explicitly cached checks.
Source Files
¶
- adapter.go
- adapter_options.go
- datastore.go
- journal.go
- journal_cleanup.go
- journal_notifications.go
- journal_permissions.go
- journal_reader.go
- journal_reader_enforce.go
- journal_reader_listen.go
- journal_reader_model.go
- journal_reader_mutation.go
- journal_reader_run.go
- journal_reader_store.go
- journal_reader_view.go
- journal_sql.go
- validation.go