From 5be200338b349cbc6351bb7e83570b36604c6561 Mon Sep 17 00:00:00 2001 From: Connectplay ltd Date: Tue, 18 Dec 2018 18:01:10 +1300 Subject: [PATCH 1/6] Updated to gorethink v5 --- go.mod | 15 +++++++++ go.sum | 74 ++++++++++++++++++++++++++++++++++++++++++ rethinkadapter.go | 2 +- rethinkadapter_test.go | 2 +- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..93488e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module rethinkdb-adapter + +require ( + github.com/Knetic/govaluate v3.0.0+incompatible // indirect + github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect + github.com/casbin/casbin v1.7.0 + github.com/onsi/ginkgo v1.7.0 // indirect + github.com/onsi/gomega v1.4.3 // indirect + github.com/pkg/errors v0.8.0 // indirect + golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect + gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect + gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect + gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7e3f415 --- /dev/null +++ b/go.sum @@ -0,0 +1,74 @@ +github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg= +github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= +github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= +github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/casbin/casbin v1.7.0 h1:PuzlE8w0JBg/DhIqnkF1Dewf3z+qmUZMVN07PonvVUQ= +github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE= +github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY= +github.com/cenkalti/backoff v2.0.0+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= +github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.0.6 h1:hcP1GmhGigz/O7h1WVUM5KklBp1JoNS9FggWKdj/j3s= +github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac h1:7d7lG9fHOLdL6jZPtnV4LpI41SbohIJ1Atq7U991dMg= +golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b h1:cmOZLU2i7CLArKNViO+ZCQ47wqYFyKEIpbGWp+b6Uoc= +golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo= +gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fatih/pool.v2 v2.0.0 h1:xIFeWtxifuQJGk/IEPKsTduEKcKvPmhoiVDGpC40nKg= +gopkg.in/fatih/pool.v2 v2.0.0/go.mod h1:8xVGeu1/2jr2wm5V9SPuMht2H5AEmf5aFMGSQixtjTY= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 h1:OAj3g0cR6Dx/R07QgQe8wkA9RNjB2u4i700xBkIT4e0= +gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= +gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1 h1:cBTUuUFTllRYtInK6FtBRa+tOBlZqpeDTnZF54xjGbg= +gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1/go.mod h1:x+1XKi70FH0kHCpvPQ78hGBCCxoNdE7sP+kEFdKgN6A= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/rethinkadapter.go b/rethinkadapter.go index 8e53d5c..74322e1 100644 --- a/rethinkadapter.go +++ b/rethinkadapter.go @@ -6,7 +6,7 @@ import ( "github.com/casbin/casbin/model" "github.com/casbin/casbin/persist" - r "gopkg.in/gorethink/gorethink.v3" + r "gopkg.in/rethinkdb/rethinkdb-go.v5" ) // adapter represents the RethinkDB adapter for policy storage. diff --git a/rethinkadapter_test.go b/rethinkadapter_test.go index 15dc4f7..1c020bf 100644 --- a/rethinkadapter_test.go +++ b/rethinkadapter_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/casbin/casbin" - r "gopkg.in/gorethink/gorethink.v4" + r "gopkg.in/rethinkdb/rethinkdb-go.v5" ) func getConnect() r.QueryExecutor { From d09d036d12a22ed57c46593f8424d98d8f98964b Mon Sep 17 00:00:00 2001 From: Connectplay ltd Date: Wed, 19 Dec 2018 10:06:04 +1300 Subject: [PATCH 2/6] Updated module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 93488e7..449b539 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module rethinkdb-adapter +module github.com/Connectplayltd/rethinkdb-adapter require ( github.com/Knetic/govaluate v3.0.0+incompatible // indirect From 39ace2d74cb04930b9dc807280a08f6c433c176c Mon Sep 17 00:00:00 2001 From: Connectplay ltd Date: Wed, 19 Dec 2018 10:43:08 +1300 Subject: [PATCH 3/6] Added support for a fifth policy entry --- rethinkadapter.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rethinkadapter.go b/rethinkadapter.go index 74322e1..ea5bce4 100644 --- a/rethinkadapter.go +++ b/rethinkadapter.go @@ -21,6 +21,7 @@ type policy struct { V2 string `gorethink:"v2"` V3 string `gorethink:"v3"` V4 string `gorethink:"v4"` + V5 string `gorethink:"v5"` } func finalizer(a *adapter) { @@ -101,6 +102,10 @@ func loadPolicyLine(line policy, model model.Model) { tokens = append(tokens, line.V4) } + if line.V5 != "" { + tokens = append(tokens, line.V5) + } + model[sec][key].Policy = append(model[sec][key].Policy, tokens) } @@ -137,6 +142,8 @@ func (a *adapter) writeTableLine(ptype string, rule []string) policy { items.V3 = rule[i] case 3: items.V4 = rule[i] + case 4: + items.V5 = rule[i] } } return items @@ -206,6 +213,10 @@ func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) { selector.V4 = fieldValues[3-fieldIndex] } + if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) { + selector.V5 = fieldValues[4-fieldIndex] + } + _, err := r.DB("casbin").Table("policy").Filter(selector).Delete().Run(a.session) if err != nil { From 616ca879719519a1a41e5c71520c27b28d6c5b92 Mon Sep 17 00:00:00 2001 From: Connectplay ltd Date: Wed, 19 Dec 2018 13:21:56 +1300 Subject: [PATCH 4/6] Added configurable database and table names --- rethinkadapter.go | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/rethinkadapter.go b/rethinkadapter.go index ea5bce4..6077647 100644 --- a/rethinkadapter.go +++ b/rethinkadapter.go @@ -11,7 +11,9 @@ import ( // adapter represents the RethinkDB adapter for policy storage. type adapter struct { - session r.QueryExecutor + session r.QueryExecutor + database string + table string } type policy struct { @@ -30,19 +32,39 @@ func finalizer(a *adapter) { // NewAdapter is the constructor for adapter. func NewAdapter(Sessionvar r.QueryExecutor) persist.Adapter { - a := &adapter{session: Sessionvar} + a := &adapter{session: Sessionvar, database: a.database, table: a.table} a.open() // Call the destructor when the object is released. runtime.SetFinalizer(a, finalizer) return a } +// GetDatabaseName returns the name of the database that the adapter will use +func (a *adapter) GetDatabaseName() string { + return a.database +} + +// GetTableName returns the name of the table that the adapter will use +func (a *adapter) GetTableName() string { + return a.database +} + +// SetDatabaseName sets the database that the adapter will use +func (a *adapter) SetDatabaseName(s string) { + a.database = s +} + +// SetTableName sets the tablet that the adapter will use +func (a *adapter) SetTableName(s string) { + a.table = s +} + func (a *adapter) close() { a.session = nil } func (a *adapter) createDatabase() error { - _, err := r.DBList().Contains("casbin").Do(r.DBCreate("casbin").Exec(a.session)).Run(a.session) + _, err := r.DBList().Contains(a.database).Do(r.DBCreate(a.database).Exec(a.session)).Run(a.session) if err != nil { return err } @@ -50,7 +72,7 @@ func (a *adapter) createDatabase() error { } func (a *adapter) createTable() error { - _, err := r.DB("casbin").TableList().Contains("policy").Do(r.DB("casbin").TableCreate("policy").Exec(a.session)).Run(a.session) + _, err := r.DB(a.database).TableList().Contains(a.table).Do(r.DB(a.database).TableCreate(a.table).Exec(a.session)).Run(a.session) if err != nil { return err } @@ -69,7 +91,7 @@ func (a *adapter) open() { //Erase the table data func (a *adapter) dropTable() error { - _, err := r.DB("casbin").Table("policy").Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Delete().Run(a.session) if err != nil { panic(err) } @@ -113,7 +135,7 @@ func loadPolicyLine(line policy, model model.Model) { func (a *adapter) LoadPolicy(model model.Model) error { a.open() - rows, errn := r.DB("casbin").Table("policy").Run(a.session) + rows, errn := r.DB(a.database).Table(a.table).Run(a.session) if errn != nil { fmt.Printf("E: %v\n", errn) return errn @@ -168,7 +190,7 @@ func (a *adapter) SavePolicy(model model.Model) error { lines = append(lines, line) } } - _, err := r.DB("casbin").Table("policy").Insert(lines).Run(a.session) + _, err := r.DB(a.database).Table(a.table).Insert(lines).Run(a.session) if err != nil { fmt.Printf("Error: %v\n", err) return err @@ -179,7 +201,7 @@ func (a *adapter) SavePolicy(model model.Model) error { //AddPolicy for adding a new policy to rethinkdb func (a *adapter) AddPolicy(sec string, PTYPE string, policys []string) error { line := a.writeTableLine(PTYPE, policys) - _, err := r.DB("casbin").Table("policy").Insert(line).Run(a.session) + _, err := r.DB(a.database).Table(a.table).Insert(line).Run(a.session) if err != nil { return err } @@ -189,7 +211,7 @@ func (a *adapter) AddPolicy(sec string, PTYPE string, policys []string) error { //RemovePolicy for removing a policy rule from rethinkdb func (a *adapter) RemovePolicy(sec string, PTYPE string, policys []string) error { line := a.writeTableLine(PTYPE, policys) - _, err := r.DB("casbin").Table("policy").Filter(line).Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Filter(line).Delete().Run(a.session) if err != nil { return err } @@ -217,8 +239,7 @@ func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, selector.V5 = fieldValues[4-fieldIndex] } - - _, err := r.DB("casbin").Table("policy").Filter(selector).Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Filter(selector).Delete().Run(a.session) if err != nil { panic(err) } From 84bb01c4d7a140467c30247357585a7e597be8e0 Mon Sep 17 00:00:00 2001 From: Connectplay ltd Date: Wed, 19 Dec 2018 13:30:20 +1300 Subject: [PATCH 5/6] Updated module name to merge back --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 449b539..8253892 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Connectplayltd/rethinkdb-adapter +module github.com/adityapandey9/rethinkdb-adapter require ( github.com/Knetic/govaluate v3.0.0+incompatible // indirect From 79572da167cf1bc6346d3dc8e264dcaafaa00e3c Mon Sep 17 00:00:00 2001 From: Connectplayltd <41409303+Connectplayltd@users.noreply.github.com> Date: Wed, 19 Dec 2018 13:43:54 +1300 Subject: [PATCH 6/6] Update rethinkadapter.go --- rethinkadapter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rethinkadapter.go b/rethinkadapter.go index 6077647..723df08 100644 --- a/rethinkadapter.go +++ b/rethinkadapter.go @@ -32,7 +32,7 @@ func finalizer(a *adapter) { // NewAdapter is the constructor for adapter. func NewAdapter(Sessionvar r.QueryExecutor) persist.Adapter { - a := &adapter{session: Sessionvar, database: a.database, table: a.table} + a := &adapter{session: Sessionvar, database:"casbin", table: "policy"} a.open() // Call the destructor when the object is released. runtime.SetFinalizer(a, finalizer)