Documentation
¶
Index ¶
- Constants
- func InitCasbinWatcherMux(pubClient, subClient *redis.Client)
- func SetEnforcerLookup(fn func(tenantID int) *casbin.SyncedEnforcer)
- func SetupRedisWatcherForEnforcer(e *casbin.SyncedEnforcer, tenantID int)
- func SetupWithProvider(provider PolicyProvider, tenantID int) (*casbin.SyncedEnforcer, error)
- func ShutdownCasbinWatcherMux()
- type CasbinWatcherMux
- type Logger
- func (l *Logger) EnableLog(enable bool)
- func (l *Logger) IsEnabled() bool
- func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)
- func (l *Logger) LogModel(model [][]string)
- func (l *Logger) LogPolicy(policy map[string][][]string)
- func (l *Logger) LogRole(roles []string)
- type MSG
- type PolicyProvider
- type PolicyRule
- type ProviderAdapter
- func (a *ProviderAdapter) AddPolicy(sec string, ptype string, rule []string) error
- func (a *ProviderAdapter) LoadPolicy(m model.Model) error
- func (a *ProviderAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
- func (a *ProviderAdapter) RemovePolicy(sec string, ptype string, rule []string) error
- func (a *ProviderAdapter) SavePolicy(m model.Model) error
- type UpdateType
Constants ¶
const ModelText = `` /* 251-byte string literal not displayed */
Initialize the model from a string. ModelText is the canonical RBAC model for Casbin enforcers. Exported so security-management's local SetupForTenant reuses the same model instead of copying (avoids model drift between local-DB and remote-provider paths).
Variables ¶
This section is empty.
Functions ¶
func InitCasbinWatcherMux ¶ added in v1.1.51
InitCasbinWatcherMux initialises the global CasbinWatcherMux singleton and starts the background listener goroutine. It is safe to call multiple times from concurrent goroutines (subsequent calls after the first are no-ops).
Parameters:
- pubClient: Client #1 from config.GetRedisClient() for PUBLISH
- subClient: Client #3 from config.EnsureSubscriberClient() for PSUBSCRIBE
func SetEnforcerLookup ¶ added in v1.1.51
func SetEnforcerLookup(fn func(tenantID int) *casbin.SyncedEnforcer)
SetEnforcerLookup sets the function used to look up tenant enforcers. Called by runtime during Application initialization.
func SetupRedisWatcherForEnforcer ¶ added in v1.1.53
func SetupRedisWatcherForEnforcer(e *casbin.SyncedEnforcer, tenantID int)
SetupRedisWatcherForEnforcer sets up the CasbinWatcherMux watcher for an enforcer. Called by both SetupForTenant and SetupWithProvider to eliminate code duplication.
Behavior:
- Lazily initialises the mux from the configured Redis clients on first use (idempotent), so callers don't need to wire InitCasbinWatcherMux manually
- If Redis is not configured, degrades gracefully (no cross-instance sync)
- If Client #1 is configured but the subscriber Client #3 is missing, logs a warning so operators can tell that policy sync is silently disabled
func SetupWithProvider ¶ added in v1.1.35
func SetupWithProvider(provider PolicyProvider, tenantID int) (*casbin.SyncedEnforcer, error)
SetupWithProvider creates a Casbin enforcer using a PolicyProvider (for microservices) Unlike SetupForTenant, this does not require a local database connection
⚠️ For non-security-management microservices only (remote fetch mode) security-management should continue using SetupForTenant
Parameters:
- provider: Policy provider (implemented by microservice, typically a gRPC adapter)
- tenantID: Tenant identifier
Returns:
- *casbin.SyncedEnforcer: Enforcer with loaded policies
- error: Initialization error
func ShutdownCasbinWatcherMux ¶ added in v1.1.51
func ShutdownCasbinWatcherMux()
ShutdownCasbinWatcherMux stops the mux listener and releases resources. It is safe to call when the mux is not initialised (no-op).
Types ¶
type CasbinWatcherMux ¶ added in v1.1.51
type CasbinWatcherMux struct {
// contains filtered or unexported fields
}
CasbinWatcherMux is a singleton that holds a single PSUBSCRIBE connection (Client #3) and dispatches incoming messages to the correct tenant enforcer.
func (*CasbinWatcherMux) NewWatcher ¶ added in v1.1.51
func (m *CasbinWatcherMux) NewWatcher(tenantID int) persist.Watcher
NewWatcher creates a per-tenant watcher handle that implements persist.Watcher + persist.WatcherEx + persist.UpdatableWatcher. The handle uses Client #1 for PUBLISH; lifecycle is managed by the mux.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the implementation for a Logger using zap logger.
func (*Logger) LogEnforce ¶
func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)
LogEnforce log info related to enforce.
type MSG ¶ added in v1.1.51
type MSG struct {
Method UpdateType `json:"Method"`
ID string `json:"ID"`
Sec string `json:"Sec"`
Ptype string `json:"Ptype"`
OldRule []string `json:"OldRule"`
OldRules [][]string `json:"OldRules"`
NewRule []string `json:"NewRule"`
NewRules [][]string `json:"NewRules"`
FieldIndex int `json:"FieldIndex"`
FieldValues []string `json:"FieldValues"`
}
MSG is the wire payload. Its exported-field JSON encoding is part of the fixed wire format — do not change field names (see the rolling-upgrade note above).
type PolicyProvider ¶ added in v1.1.35
type PolicyProvider interface {
// GetPolicies retrieves all policy rules for the specified tenant
//
// Parameters:
// - ctx: Context (for timeout control, cancellation, etc.)
// - tenantID: Tenant identifier
//
// Returns:
// - []PolicyRule: List of policy rules (including both p and g types)
// - error: Error information
GetPolicies(ctx context.Context, tenantID int) ([]PolicyRule, error)
}
PolicyProvider is the strategy interface for providing policy data Microservices implement this interface to supply policies from any source (gRPC, HTTP, DB, etc.)
type PolicyRule ¶ added in v1.1.35
type PolicyRule struct {
PType string // Policy type: "p" (policy) or "g" (role inheritance)
V0 string // Usually sub (role name)
V1 string // Usually obj (resource path)
V2 string // Usually act (HTTP method)
V3 string // Optional extension field
V4 string // Optional extension field
V5 string // Optional extension field
}
PolicyRule represents a single Casbin policy rule.
Validation Rules:
- PType must be a valid Casbin policy type: "p" (policy), "g" (role inheritance), etc.
- V0-V5 fields must be filled contiguously from left to right without gaps
- Empty strings at the end of the sequence are allowed (e.g., V3="", V4="", V5="")
- Empty strings in the middle are NOT allowed (e.g., V0="admin", V1="", V2="GET" is invalid)
This format matches Casbin's standard storage schema (gorm-adapter).
type ProviderAdapter ¶ added in v1.1.35
type ProviderAdapter struct {
// contains filtered or unexported fields
}
ProviderAdapter implements Casbin's persist.Adapter interface using a PolicyProvider This is a read-only adapter - write operations return errors
func NewProviderAdapter ¶ added in v1.1.35
func NewProviderAdapter(provider PolicyProvider, tenantID int) *ProviderAdapter
NewProviderAdapter creates a new ProviderAdapter
func (*ProviderAdapter) AddPolicy ¶ added in v1.1.35
func (a *ProviderAdapter) AddPolicy(sec string, ptype string, rule []string) error
AddPolicy is not supported (read-only adapter)
func (*ProviderAdapter) LoadPolicy ¶ added in v1.1.35
func (a *ProviderAdapter) LoadPolicy(m model.Model) error
LoadPolicy loads all policies from the Provider into the model This is called by Casbin SyncedEnforcer.LoadPolicy()
func (*ProviderAdapter) RemoveFilteredPolicy ¶ added in v1.1.35
func (a *ProviderAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
RemoveFilteredPolicy is not supported (read-only adapter)
func (*ProviderAdapter) RemovePolicy ¶ added in v1.1.35
func (a *ProviderAdapter) RemovePolicy(sec string, ptype string, rule []string) error
RemovePolicy is not supported (read-only adapter)
func (*ProviderAdapter) SavePolicy ¶ added in v1.1.35
func (a *ProviderAdapter) SavePolicy(m model.Model) error
SavePolicy is not supported (read-only adapter)
type UpdateType ¶ added in v1.1.51
type UpdateType string
UpdateType enumerates the policy-change message kinds carried on the wire.
const ( Update UpdateType = "Update" UpdateForAddPolicy UpdateType = "UpdateForAddPolicy" UpdateForRemovePolicy UpdateType = "UpdateForRemovePolicy" UpdateForRemoveFilteredPolicy UpdateType = "UpdateForRemoveFilteredPolicy" UpdateForSavePolicy UpdateType = "UpdateForSavePolicy" UpdateForAddPolicies UpdateType = "UpdateForAddPolicies" UpdateForRemovePolicies UpdateType = "UpdateForRemovePolicies" UpdateForUpdatePolicy UpdateType = "UpdateForUpdatePolicy" UpdateForUpdatePolicies UpdateType = "UpdateForUpdatePolicies" )