rediswatcher

package module
v2.0.0-...-07a1bd7 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 12 Imported by: 1

README

Redis Watcher

Go report Coverage Status Go Reference Release

Redis Watcher is a Redis watcher for Casbin.

Installation

go get github.com/casbin/redis-watcher/v2

Simple Example

package main

import (
	"log"

	"github.com/casbin/casbin/v2"
	rediswatcher "github.com/casbin/redis-watcher/v2"
	"github.com/go-redis/redis/v8"
)

func updateCallback(msg string) {
	log.Println(msg)
}

func main() {
	// Initialize the watcher.
	// Use the Redis host as parameter.
	w, _ := rediswatcher.NewWatcher("localhost:6379", rediswatcher.WatcherOptions{
		Options: redis.Options{
			Network:  "tcp",
			Password: "",
		},
		Channel:    "/casbin",
		// Only exists in test, generally be true
		IgnoreSelf: false,
	})

	// Or initialize the watcher in redis cluster.
	// w, _ := rediswatcher.NewWatcherWithCluster("localhost:6379,localhost:6379,localhost:6379", rediswatcher.WatcherOptions{
	// 	ClusterOptions: redis.ClusterOptions{
	// 		Password: "",
	// 	},
	// 	Channel: "/casbin",
	// 	IgnoreSelf: false,
	// })

	// Initialize the enforcer.
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

	// Set the watcher for the enforcer.
	_ = e.SetWatcher(w)

	// Set callback to local example
	_ = w.SetUpdateCallback(updateCallback)
	
	// Or use the default callback
	// _ = w.SetUpdateCallback(rediswatcher.DefaultUpdateCallback(e))

	// Update the policy to test the effect.
	// You should see "[casbin rules updated]" in the log.
	_ = e.SavePolicy()
	// Only exists in test
	fmt.Scanln()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultUpdateCallback

func DefaultUpdateCallback(e casbin.IEnforcer) func(string)

func NewPublishWatcher

func NewPublishWatcher(addr string, option WatcherOptions) (persist.Watcher, error)

NewPublishWatcher return a Watcher only publish but not subscribe

func NewWatcher

func NewWatcher(addr string, option WatcherOptions) (persist.Watcher, error)

NewWatcher creates a new Watcher to be used with a Casbin enforcer addr is a redis target string in the format "host:port" setters allows for inline WatcherOptions

Example:
		w, err := rediswatcher.NewWatcher("127.0.0.1:6379",WatcherOptions{}, nil)

func NewWatcherWithCluster

func NewWatcherWithCluster(addrs string, option WatcherOptions) (persist.Watcher, error)

NewWatcherWithCluster creates a new Watcher to be used with a Casbin enforcer addrs is a redis-cluster target string in the format "host1:port1,host2:port2,host3:port3"

Example:
		w, err := rediswatcher.NewWatcherWithCluster("127.0.0.1:6379,127.0.0.1:6379,127.0.0.1:6379",WatcherOptions{})

Types

type MSG

type MSG struct {
	Method      UpdateType
	ID          string
	Sec         string
	Ptype       string
	OldRule     []string
	OldRules    [][]string
	NewRule     []string
	NewRules    [][]string
	FieldIndex  int
	FieldValues []string
}

func (*MSG) MarshalBinary

func (m *MSG) MarshalBinary() ([]byte, error)

func (*MSG) UnmarshalBinary

func (m *MSG) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the struct into a User

type UpdateType

type UpdateType string
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"
)

type Watcher

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

func (*Watcher) Close

func (w *Watcher) Close()

func (*Watcher) GetWatcherOptions

func (w *Watcher) GetWatcherOptions() WatcherOptions

func (*Watcher) SetUpdateCallback

func (w *Watcher) SetUpdateCallback(callback func(string)) error

SetUpdateCallback sets the update callback function invoked by the watcher when the policy is updated. Defaults to Enforcer.LoadPolicy()

func (*Watcher) Update

func (w *Watcher) Update() error

Update publishes a message to all other casbin instances telling them to invoke their update callback

func (*Watcher) UpdateForAddPolicies

func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]string) error

UpdateForAddPolicies calls the update callback of other instances to synchronize their policies in batch. It is called after Enforcer.AddPolicies()

func (*Watcher) UpdateForAddPolicy

func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error

UpdateForAddPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicy()

func (*Watcher) UpdateForRemoveFilteredPolicy

func (w *Watcher) UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) error

UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

func (*Watcher) UpdateForRemovePolicies

func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) error

UpdateForRemovePolicies calls the update callback of other instances to synchronize their policies in batch. It is called after Enforcer.RemovePolicies()

func (*Watcher) UpdateForRemovePolicy

func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) error

UpdateForRemovePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicy()

func (*Watcher) UpdateForSavePolicy

func (w *Watcher) UpdateForSavePolicy(model model.Model) error

UpdateForSavePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

func (*Watcher) UpdateForUpdatePolicies

func (w *Watcher) UpdateForUpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error

UpdateForUpdatePolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.UpdatePolicies()

func (*Watcher) UpdateForUpdatePolicy

func (w *Watcher) UpdateForUpdatePolicy(sec string, ptype string, oldRule, newRule []string) error

UpdateForUpdatePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.UpdatePolicy()

type WatcherOptions

type WatcherOptions struct {
	Options                rds.Options
	ClusterOptions         rds.ClusterOptions
	SubClient              *rds.Client
	PubClient              *rds.Client
	Channel                string
	IgnoreSelf             bool
	LocalID                string
	OptionalUpdateCallback func(string)
}

Jump to

Keyboard shortcuts

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