etcdwatcher

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Etcd Watcher Go Coverage Status Godoc

Etcd Watcher is the Etcd watcher for Casbin. With this library, Casbin can synchronize the policy with the database in multiple enforcer instances.

Installation

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

Simple Example

package main

import (
    "log"

    casbin "github.com/casbin/casbin/v2"
    etcdwatcher "github.com/casbin/etcd-watcher/v2"
)

func updateCallback(rev string) {
    log.Println("New revision detected:", rev)
}

func main() {
    // Initialize the watcher.
    // Use the endpoint of etcd cluster as parameter.
    w, _ := etcdwatcher.NewWatcher([]string{"http://127.0.0.1:2379"}, "keyname")
    
    // Initialize the enforcer.
    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
    
    // Set the watcher for the enforcer.
    e.SetWatcher(w)
    
    // By default, the watcher's callback is automatically set to the
    // enforcer's LoadPolicy() in the SetWatcher() call.
    // We can change it by explicitly setting a callback.
    w.SetUpdateCallback(updateCallback)
    
    // Update the policy to test the effect.
    // You should see "[New revision detected: X]" in the log.
    e.SavePolicy()
}

Simple Example(Configuration Mode)

package main

import (
    "log"

    casbin "github.com/casbin/casbin/v2"
    etcdwatcher "github.com/casbin/etcd-watcher/v2"
)

func updateCallback(rev string) {
    log.Println("New revision detected:", rev)
}

func main() {
    // Initialize the watcher.
    // Use the configuration file as the parameter
	w, _ := etcdwatcher.NewWatcherWithConfig(etcdwatcher.WatcherConfig{
		Hosts: []string{"http://127.0.0.1:2379"},
		Key:   "/casbin",
		User:  "root",
		Pass:  "123",
	})
    
    // Initialize the enforcer.
    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
    
    // Set the watcher for the enforcer.
    e.SetWatcher(w)
    
    // By default, the watcher's callback is automatically set to the
    // enforcer's LoadPolicy() in the SetWatcher() call.
    // We can change it by explicitly setting a callback.
    w.SetUpdateCallback(updateCallback)
    
    // Update the policy to test the effect.
    // You should see "[New revision detected: X]" in the log.
    e.SavePolicy()
}

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 NewWatcher

func NewWatcher(endpoints []string, keyName string, password ...string) (persist.Watcher, error)

NewWatcher is the constructor for Watcher. endpoints is the endpoints for etcd clusters.

func NewWatcherWithConfig added in v2.3.0

func NewWatcherWithConfig(config WatcherConfig) (persist.Watcher, error)

NewWatcherWithConfig is a configurable Watcher constructor

Types

type Watcher

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

func (*Watcher) Close

func (w *Watcher) Close()

Close closes the Watcher.

func (*Watcher) SetUpdateCallback

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

SetUpdateCallback sets the callback function that the watcher will call when the policy in DB has been changed by other instances. A classic callback is Enforcer.LoadPolicy().

func (*Watcher) Update

func (w *Watcher) Update() error

Update calls the update callback of other instances to synchronize their policy. It is usually called after changing the policy in DB, like Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc.

type WatcherConfig added in v2.3.0

type WatcherConfig struct {
	Hosts                []string
	Key                  string `json:",default=casbin_watcher"`
	User                 string
	Pass                 string
	DialKeepAliveTimeout time.Duration `json:",default=10"`
	DialTimeout          time.Duration `json:",default=30"`
}

Jump to

Keyboard shortcuts

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