mlsen / casbin-redis-adapter Goto Github PK
View Code? Open in Web Editor NEWRedis Adapter for Casbin
License: Apache License 2.0
Redis Adapter for Casbin
License: Apache License 2.0
Its obvious that New constructor doesn`t implement other than redis.Client connection. Its not possible to make adapter for Redis cluster
We found in testing that an input of unusual string type would cause the Enforce function to fail completely, as shown in the code block below. When calling the enforcer.Enforce
function, an error of invalid policy size will be thrown, resulting in a failure to judge the permission.
//exception error: false invalid policy size: expected 3, got 1, pvals: [role::35046358490550272]
import (
"fmt"
"github.com/casbin/casbin/v2"
redisAdapter "github.com/mlsen/casbin-redis-adapter/v2"
"testing"
)
func TestRedisAdapter(t *testing.T) {
adapter, err := redisAdapter.NewFromURL("redis://:123@localhost:6379/0")
if err != nil{
panic(err)
}
enforcer, err := casbin.NewEnforcer("model.conf", adapter)
if err != nil{
panic(err)
}
enforcer.EnableAutoSave(true)
// Load policy from redis
enforcer.LoadPolicy()
// normal case
_, _ = enforcer.AddNamedPolicy("p", []string{"sub1", "obj1", "on"})
fmt.Println(enforcer.Enforce("sub1", "obj1","on"))
// exception case with newline character
sub2 := `role::35046358490550272
expr 855534449 + 870448142`
//it can be successfully inserted into redis list
_, _ = enforcer.AddNamedPolicy("p", []string{sub2, "obj2", "on"})
fmt.Println(enforcer.Enforce("sub1", "obj1", "on"))
//exception error: false invalid policy size: expected 3, got 1, pvals: [role::35046358490550272]
}
model.conf
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
g2 = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act
Here is the code:
package main
import (
"log"
"github.com/casbin/casbin/v2"
casModel "github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
redisadapter "github.com/mlsen/casbin-redis-adapter/v2"
"github.com/pkg/errors"
)
func main() {
_, err := initCasbin()
if err != nil {
log.Fatal(err)
}
}
var (
model string = `
[request_definition]
r = sub, dom, obj, act
[policy_definition]
p = sub, dom, obj, act
[role_definition]
g = _, _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act
`
)
func initCasbin() (casbin.IEnforcer, error) {
mdl, err := casModel.NewModelFromString(model)
if err != nil {
return nil, errors.Wrap(err, "failed to load casbin model")
}
redisAdapter, err := initRedisAdapter()
if err != nil {
return nil, errors.Wrap(err, "failed to initiate redis adapeter")
}
enforcer, err := casbin.NewEnforcer(mdl, redisAdapter)
if err != nil {
return nil, errors.Wrap(err, "failed to create enforcer")
}
return enforcer, nil
}
func initRedisAdapter() (persist.Adapter, error) {
redis, err := redisadapter.NewFromURL("redis://@localhost:6379/0")
if err != nil {
return nil, errors.Wrap(err, "unable connect to redis")
}
return redis, nil
}
My go.mod
module github.com/novrian/casbin-research
go 1.20
require (
github.com/casbin/casbin/v2 v2.71.1
github.com/mlsen/casbin-redis-adapter/v2 v2.0.1
github.com/pkg/errors v0.9.1
)
require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v8 v8.4.8 // indirect
go.opentelemetry.io/otel v0.15.0 // indirect
)
I've encounter error when I run the code
# github.com/mlsen/casbin-redis-adapter/v2
vendor/github.com/mlsen/casbin-redis-adapter/v2/adapter.go:53:34: cannot use persist.LoadPolicyLine (value of type func(line string, m model.Model) error) as func(string, model.Model) value in argument to a.loadPolicy
My Go Version:
go version go1.20 darwin/arm64
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.