Command-line Tools
Casbin CLIs offer command-line interfaces that expose Casbin's full API directly in your shell environment. This guide explains how to use Casbin's CLI implementations across multiple languages, including Rust, Java, Go, Python, .NET, and Node.js.
Installation
Go (casbin-go-cli)
-
Clone the repository:
git clone https://github.com/casbin/casbin-go-cli.git -
Build the CLI:
cd casbin-go-cli
go build -o casbin
Rust (casbin-rust-cli)
Install from crates.io
cargo install casbin-rust-cli
Build from source
-
Clone the repository:
git clone https://github.com/casbin-rs/casbin-rust-cli.git -
Build with release optimizations:
cd casbin-rust-cli
cargo build --release
Java (casbin-java-cli)
-
Clone the repository:
git clone https://github.com/jcasbin/casbin-java-cli.git -
Build using Maven (the JAR file will be created in the
targetdirectory):cd casbin-java-cli
mvn clean install
Python (casbin-python-cli)
-
Clone the repository:
git clone https://github.com/casbin/casbin-python-cli.git -
Install required dependencies:
cd casbin-python-cli
pip install -r requirements.txt -
Run the CLI:
python -m casbin_cli.client [command] [options] [args]
.NET (casbin-dotnet-cli)
-
Clone the repository:
git clone https://github.com/casbin-net/casbin-dotnet-cli.git -
Build the project:
cd casbin-dotnet-cli
dotnet build
Usage
Options
| Option | Description | Required | Notes |
|---|---|---|---|
-m, --model | Model file path or model definition text | Yes | Wrap in quotes and use | as line separator |
-p, --policy | Policy file path or policy definition text | Yes | Wrap in quotes and use | as line separator |
-e, --enforce | Evaluate access permissions | No | Wrap arguments in quotes |
-ex, --enforceEx | Evaluate permissions and display the matched policy | No | Wrap arguments in quotes |
-AF, --addFuntion | Register a custom function (casbin-java-cli only) | No | Wrap in quotes and use | as line separator |
-ap, --addPolicy | Append a policy rule to the policy file (casbin-java-cli only) | No | Wrap arguments in quotes |
-rp, --removePolicy | Delete a policy rule from the policy file (casbin-java-cli only) | No | Wrap arguments in quotes |
Getting Started
-
Verify if Alice can read data1:
./casbin enforce -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data1" "read"{"allow":true,"explain":null}./casbin enforce -m "[request_definition]\nr = sub, obj, act\n[policy_definition]\np = sub, obj, act\n[role_definition]\ng = _, _\n[policy_effect]\ne = some(where (p.eft == allow))\n[matchers]\nm = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act" -p "p, alice, data1, read\np, bob, data2, write\np, data2_admin, data2, read\np, data2_admin, data2, write\ng, alice, data2_admin" "alice" "data1" "read"{"allow":true,"explain":null} -
Verify if Alice can write to data2 and show which policy grants the permission:
./casbin enforceEx -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"{"allow":true,"explain":["data2_admin","data2","write"]} -
Add a new policy rule to the policy file (casbin-java-cli only):
./casbin addPolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"{"allow":true,"explain":null} -
Remove a policy rule from the policy file (casbin-java-cli only):
./casbin removePolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"{"allow":true,"explain":null}