From a82bb28462e3a948cc335b28551d652407323262 Mon Sep 17 00:00:00 2001 From: andikemgitopsbot Date: Fri, 20 Sep 2024 09:17:57 -0500 Subject: [PATCH 1/3] add unit test for GetDomains function --- examples/rbac_with_domains_model.conf | 14 ++++++++++++++ examples/rbac_with_domains_policy.csv | 6 ++++++ server/rbac_api_test.go | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 examples/rbac_with_domains_model.conf create mode 100644 examples/rbac_with_domains_policy.csv diff --git a/examples/rbac_with_domains_model.conf b/examples/rbac_with_domains_model.conf new file mode 100644 index 0000000..57c3721 --- /dev/null +++ b/examples/rbac_with_domains_model.conf @@ -0,0 +1,14 @@ +[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 \ No newline at end of file diff --git a/examples/rbac_with_domains_policy.csv b/examples/rbac_with_domains_policy.csv new file mode 100644 index 0000000..8558d17 --- /dev/null +++ b/examples/rbac_with_domains_policy.csv @@ -0,0 +1,6 @@ +p, admin, domain1, data1, read +p, admin, domain1, data1, write +p, admin, domain2, data2, read +p, admin, domain2, data2, write +g, alice, admin, domain1 +g, bob, admin, domain2 \ No newline at end of file diff --git a/server/rbac_api_test.go b/server/rbac_api_test.go index bf99b98..39058d2 100644 --- a/server/rbac_api_test.go +++ b/server/rbac_api_test.go @@ -239,3 +239,22 @@ func TestPermissionAPI(t *testing.T) { testEnforceWithoutUsers(t, e, "bob", "read", false) testEnforceWithoutUsers(t, e, "bob", "write", false) } + +func testGetDomains(t *testing.T, e *testEngine, name string, res []string) { + t.Helper() + reply, err := e.s.GetDomains(e.ctx, &pb.UserRoleRequest{EnforcerHandler: e.h, User: name}) + assert.NoError(t, err) + + t.Log("Domains for ", name, ": ", reply.Array) + + if !util.SetEquals(res, reply.Array) { + t.Error("Domains for ", name, ": ", reply.Array, ", supposed to be ", res) + } +} + +func TestRoleDomainAPI(t *testing.T) { + e := newTestEngine(t, "file", "../examples/rbac_with_domains_policy.csv", "../examples/rbac_with_domains_model.conf") + + testGetDomains(t, e, "alice", []string{"domain1"}) + testGetDomains(t, e, "bob", []string{"domain2"}) +} From 19ec61d2900a367a0f6c7ca3cea65d0169dbdf0a Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 20 Sep 2024 09:53:10 -0500 Subject: [PATCH 2/3] fix: remove ioutils.ReadFile function after deprecation Use os package version of the function, as ioutils implementation was deprecated. --- server/enforcer.go | 4 ++-- server/model_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/enforcer.go b/server/enforcer.go index 882f3b9..896660f 100644 --- a/server/enforcer.go +++ b/server/enforcer.go @@ -17,7 +17,7 @@ package server import ( "context" "errors" - "io/ioutil" + "os" "strings" "sync" @@ -98,7 +98,7 @@ func (s *Server) NewEnforcer(ctx context.Context, in *pb.NewEnforcerRequest) (*p if in.ModelText == "" { cfg := LoadConfiguration(getLocalConfigPath()) - data, err := ioutil.ReadFile(cfg.Enforcer) + data, err := os.ReadFile(cfg.Enforcer) if err != nil { return &pb.NewEnforcerReply{Handler: 0}, err } diff --git a/server/model_test.go b/server/model_test.go index 7d3b381..3c757ed 100644 --- a/server/model_test.go +++ b/server/model_test.go @@ -16,7 +16,7 @@ package server import ( "context" - "io/ioutil" + "os" "testing" pb "github.com/casbin/casbin-server/proto" @@ -54,7 +54,7 @@ func TestRBACModel(t *testing.T) { t.Error(err) } - modelText, err := ioutil.ReadFile("../examples/rbac_model.conf") + modelText, err := os.ReadFile("../examples/rbac_model.conf") if err != nil { t.Error(err) } @@ -85,7 +85,7 @@ func TestABACModel(t *testing.T) { s := NewServer() ctx := context.Background() - modelText, err := ioutil.ReadFile("../examples/abac_model.conf") + modelText, err := os.ReadFile("../examples/abac_model.conf") if err != nil { t.Error(err) } From 8fc3343c1cde2779571e8e5b38de0ba3c0d7e5fb Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 20 Sep 2024 09:53:49 -0500 Subject: [PATCH 3/3] fix: adds context.TODO to avoid passing nil context adds context.TODO to avoid passing nil context --- server/model_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/model_test.go b/server/model_test.go index 3c757ed..7ef21c3 100644 --- a/server/model_test.go +++ b/server/model_test.go @@ -117,7 +117,7 @@ func TestABACModel(t *testing.T) { func testModel(t *testing.T, s *Server, enforcerHandler int32, sub string, obj string, act string, res bool) { t.Helper() - reply, err := s.Enforce(nil, &pb.EnforceRequest{EnforcerHandler: enforcerHandler, Params: []string{sub, obj, act}}) + reply, err := s.Enforce(context.TODO(), &pb.EnforceRequest{EnforcerHandler: enforcerHandler, Params: []string{sub, obj, act}}) assert.NoError(t, err) if reply.Res != res {