Git Product home page Git Product logo

casbin-mesh's Introduction

Casbin

Go Report Card Build Coverage Status Godoc Release Discord Sourcegraph

News: still worry about how to write the correct Casbin policy? Casbin online editor is coming to help! Try it at: https://casbin.org/editor/

casbin Logo

Casbin is a powerful and efficient open-source access control library for Golang projects. It provides support for enforcing authorization based on various access control models.

Sponsored by

Build auth with fraud prevention, faster.
Try Stytch for API-first authentication, user & org management, multi-tenant SSO, MFA, device fingerprinting, and more.

All the languages supported by Casbin:

golang java nodejs php
Casbin jCasbin node-Casbin PHP-Casbin
production-ready production-ready production-ready production-ready
python dotnet c++ rust
PyCasbin Casbin.NET Casbin-CPP Casbin-RS
production-ready production-ready production-ready production-ready

Table of contents

Supported models

  1. ACL (Access Control List)
  2. ACL with superuser
  3. ACL without users: especially useful for systems that don't have authentication or user log-ins.
  4. ACL without resources: some scenarios may target for a type of resources instead of an individual resource by using permissions like write-article, read-log. It doesn't control the access to a specific article or log.
  5. RBAC (Role-Based Access Control)
  6. RBAC with resource roles: both users and resources can have roles (or groups) at the same time.
  7. RBAC with domains/tenants: users can have different role sets for different domains/tenants.
  8. ABAC (Attribute-Based Access Control): syntax sugar like resource.Owner can be used to get the attribute for a resource.
  9. RESTful: supports paths like /res/*, /res/:id and HTTP methods like GET, POST, PUT, DELETE.
  10. Deny-override: both allow and deny authorizations are supported, deny overrides the allow.
  11. Priority: the policy rules can be prioritized like firewall rules.

How it works?

In Casbin, an access control model is abstracted into a CONF file based on the PERM metamodel (Policy, Effect, Request, Matchers). So switching or upgrading the authorization mechanism for a project is just as simple as modifying a configuration. You can customize your own access control model by combining the available models. For example, you can get RBAC roles and ABAC attributes together inside one model and share one set of policy rules.

The most basic and simplest model in Casbin is ACL. ACL's model CONF is:

# Request definition
[request_definition]
r = sub, obj, act

# Policy definition
[policy_definition]
p = sub, obj, act

# Policy effect
[policy_effect]
e = some(where (p.eft == allow))

# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

An example policy for ACL model is like:

p, alice, data1, read
p, bob, data2, write

It means:

  • alice can read data1
  • bob can write data2

We also support multi-line mode by appending '\' in the end:

# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj \
  && r.act == p.act

Further more, if you are using ABAC, you can try operator in like following in Casbin golang edition (jCasbin and Node-Casbin are not supported yet):

# Matchers
[matchers]
m = r.obj == p.obj && r.act == p.act || r.obj in ('data2', 'data3')

But you SHOULD make sure that the length of the array is MORE than 1, otherwise there will cause it to panic.

For more operators, you may take a look at govaluate

Features

What Casbin does:

  1. enforce the policy in the classic {subject, object, action} form or a customized form as you defined, both allow and deny authorizations are supported.
  2. handle the storage of the access control model and its policy.
  3. manage the role-user mappings and role-role mappings (aka role hierarchy in RBAC).
  4. support built-in superuser like root or administrator. A superuser can do anything without explicit permissions.
  5. multiple built-in operators to support the rule matching. For example, keyMatch can map a resource key /foo/bar to the pattern /foo*.

What Casbin does NOT do:

  1. authentication (aka verify username and password when a user logs in)
  2. manage the list of users or roles. I believe it's more convenient for the project itself to manage these entities. Users usually have their passwords, and Casbin is not designed as a password container. However, Casbin stores the user-role mapping for the RBAC scenario.

Installation

go get github.com/casbin/casbin/v2

Documentation

https://casbin.org/docs/overview

Online editor

You can also use the online editor (https://casbin.org/editor/) to write your Casbin model and policy in your web browser. It provides functionality such as syntax highlighting and code completion, just like an IDE for a programming language.

Tutorials

https://casbin.org/docs/tutorials

Get started

  1. New a Casbin enforcer with a model file and a policy file:

    e, _ := casbin.NewEnforcer("path/to/model.conf", "path/to/policy.csv")

Note: you can also initialize an enforcer with policy in DB instead of file, see Policy-persistence section for details.

  1. Add an enforcement hook into your code right before the access happens:

    sub := "alice" // the user that wants to access a resource.
    obj := "data1" // the resource that is going to be accessed.
    act := "read" // the operation that the user performs on the resource.
    
    if res, _ := e.Enforce(sub, obj, act); res {
        // permit alice to read data1
    } else {
        // deny the request, show an error
    }
  2. Besides the static policy file, Casbin also provides API for permission management at run-time. For example, You can get all the roles assigned to a user as below:

    roles, _ := e.GetImplicitRolesForUser(sub)

See Policy management APIs for more usage.

Policy management

Casbin provides two sets of APIs to manage permissions:

  • Management API: the primitive API that provides full support for Casbin policy management.
  • RBAC API: a more friendly API for RBAC. This API is a subset of Management API. The RBAC users could use this API to simplify the code.

We also provide a web-based UI for model management and policy management:

model editor

policy editor

Policy persistence

https://casbin.org/docs/adapters

Policy consistence between multiple nodes

https://casbin.org/docs/watchers

Role manager

https://casbin.org/docs/role-managers

Benchmarks

https://casbin.org/docs/benchmark

Examples

Model Model file Policy file
ACL basic_model.conf basic_policy.csv
ACL with superuser basic_model_with_root.conf basic_policy.csv
ACL without users basic_model_without_users.conf basic_policy_without_users.csv
ACL without resources basic_model_without_resources.conf basic_policy_without_resources.csv
RBAC rbac_model.conf rbac_policy.csv
RBAC with resource roles rbac_model_with_resource_roles.conf rbac_policy_with_resource_roles.csv
RBAC with domains/tenants rbac_model_with_domains.conf rbac_policy_with_domains.csv
ABAC abac_model.conf N/A
RESTful keymatch_model.conf keymatch_policy.csv
Deny-override rbac_model_with_deny.conf rbac_policy_with_deny.csv
Priority priority_model.conf priority_policy.csv

Middlewares

Authz middlewares for web frameworks: https://casbin.org/docs/middlewares

Our adopters

https://casbin.org/docs/adopters

How to Contribute

Please read the contributing guide.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! ๐Ÿ™ [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Star History

Star History Chart

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcomed.

casbin-mesh's People

Contributors

abdelrahmanelawady avatar harshalmittal4 avatar nodece avatar prathamjaiswal001 avatar wenyxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

casbin-mesh's Issues

Helm Charts

Nowadays, Helm is popular with users who use Kubernetes.

Helm helps you manage Kubernetes applications โ€” Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.

We plan to provide users with Helm Charts to supply an easy way to allow users to manage Casbin-Mesh in Kubernetes.
Read the getting started on the helm website that may help you have a good start.

Docs: Cluster Management

We need to write the documents for cluster management, and I highly recommend you to have a looking at the following codes.

  • The HTTP endpoints of cluster management can be found at here, which may help write documents.
  • Join / Remove implementations
    Currently, we still don't support the management cluster via gRPC. It will be implemented later.

Links in Readme file not opening

Hello, I'm trying to get familiar with cabin-mesh and noticed that 3 links in Readme are incorrect,

  1. models - To setup an Casbin model for a specific namespace, executes following request on /set/model endpoint. See all supported models.
  2. Policies - Let's add policies for the test namespace. See more of Policies
  3. docs - All documents were located in docs directory.

I suppose models link must point to https://casbin.org/docs/supported-models, is this correct?
Could you also please tell where do Policies and docs are supposed to redirect, I would like to send a PR

Docs: APIs Overview

We need to write the documents of APIs. Several source codes may help:

  • For HTTP endpoints can be found at here.
  • For gRPC endpoints and proto file were located at gRPC, Proto, respectively.

not able to run/launch project in vscodium

This is the error in debug console I get when trying to launch the project in vs-code IDE.

DAP server listening at: 127.0.0.1:52655
Type 'dlv help' for list of commands.
fatal: no data directory set
Process 55290 has exited with status 1
Detaching

How can I set the data directory to run the project on my computer. Please help

auto forward issues

When we send a request to the follower, the follower should forward the incoming message to the leader. However, in some case, we will get 502 bad gateway due following reason:

node0_1  | [Casbin-mesh] 2021/09/28 05:24:05 http: proxy error: http: no Host in request URL

Go Client

We need to implement an MVP(Minimum Viable Product) version for now. Check the CLI on how communication with nodes may be helpful.

Add configuration file support

Summary

Currently, the casbin-mesh doesn't support the configuration file, we should support this which is important, I create a draft for this:

name:

data-dir:

log:
  level: info
  
bind-address:
  - 192.168.1.10:4001
  - 10.168.1.10:4002

advertise-address: 192.168.1.10:8080

endpoint:
  tls:
    enable: false
    ca:
    key:
    cert:
  authorization:
    enable: false
  authentication:
    enable: false
    type: basic
    params:
      username: root
      password: root

peer:
  tls:
    enable: false
    ca:
    key:
    cert:
  snap-threshol: 10
  snap-interval: 10
  leader-lease-timeout: 0
  heart-beat-timeout: 0
  election-timeout: 0
  apply-timeout: 0
  open-timeout: 0
  wait-leader: false
  shutdown-on-remove: false
  compression-size: 10
  compression-batch: 20

pprof:
  enable: false
  cpu-profile:
  mem-profile:

Bench: the various raft

We are currently using the HashiCorp/raft, but I still consider that we should make a benchmark for the various raft. We may need to implement several sample Key-value stores upper on the different raft implementations to compare their performance.

Roadmap v0.1

Several works still need to be done to achieve a releasable version. First of all, is documentation. We need to provide documentation to potential users to let them understand the features of Casbin-Mesh and how to use this project in practice. Furthermore, we need to show users examples of deployment, which allow them to evaluate Casbin-Mesh efficiently.

Features

Issues

Documentation

Tests

Bench

Clients

  • #46
  • Java Client
  • Javascript Client

GUI

Luopan, The GUI for Casbin-Mesh

Others

In the future, we will restructure the storage layer of Casbin to achieve higher performance.

Engine

Neo, A Casbin-compatible engine

Separate TLS config

Motivaction

Currently, we just have one TLS config, which is used for Raft and HTTP/GRPC servers, it is unsafe. When a client uses this TLS config, it can access the Raft and HTTP/GRPC servers, we cannot allow the client to access the Raft server, it is dangerous, so we need to split two TLS configs for HTTP/GPRC servers and Raft servers.

Improvement Way

Support setting the TLS for HTTP/GPRC and Raft servers respectively.

  • HTTP/GRPC server TLS config, it is used to client-to-server communication, so like:
--endpoint-ca-file <path>
--endpoint-cert-file <path>
--endpoint-key-file <path>
  • Raft server TLS config, it is used to server-to-server communication, so like:
--peer-ca-file <path>
--peer-cert-file <path>
--peer-key-file <path>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.