From 9c5bdf3963b1d011a957a4e3b17cb0c1e6a201db Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz <57230398+gonzalochief@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:32:43 -0500 Subject: [PATCH] feat: replace ioutils.ReadFile functions by os.ReadFile due to deprecation (#114) * add unit test for GetDomains function * fix: remove ioutils.ReadFile function after deprecation Use os package version of the function, as ioutils implementation was deprecated. * fix: adds context.TODO to avoid passing nil context adds context.TODO to avoid passing nil context --- examples/rbac_with_domains_model.conf | 14 ++++++++++++++ examples/rbac_with_domains_policy.csv | 6 ++++++ server/enforcer.go | 4 ++-- server/model_test.go | 8 ++++---- server/rbac_api_test.go | 19 +++++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) 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/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..7ef21c3 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) } @@ -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 { 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"}) +}