Vai al contenuto principale

Command-line Tools

Casbin CLIs expose the Casbin API from the command line so you can load models and policies, run enforce checks, and manage policy from your shell. Implementations are available for Go, Java, Node.js, Python, .NET, and Rust.

Installation

Go (casbin-go-cli)

  1. Clone the repository:

    git clone https://github.com/casbin/casbin-go-cli.git
  2. 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

  1. Clone the repository:

    git clone https://github.com/casbin-rs/casbin-rust-cli.git
  2. Build with release optimizations:

    cd casbin-rust-cli
    cargo build --release

Java (casbin-java-cli)

  1. Clone the repository:

    git clone https://github.com/jcasbin/casbin-java-cli.git
  2. Build using Maven (the JAR file will be created in the target directory):

    cd casbin-java-cli
    mvn clean install

Python (casbin-python-cli)

  1. Clone the repository:

    git clone https://github.com/casbin/casbin-python-cli.git
  2. Install required dependencies:

    cd casbin-python-cli
    pip install -r requirements.txt
  3. Run the CLI:

    python -m casbin_cli.client [command] [options] [args]

.NET (casbin-dotnet-cli)

  1. Clone the repository:

    git clone https://github.com/casbin-net/casbin-dotnet-cli.git
  2. Build the project:

    cd casbin-dotnet-cli
    dotnet build

Usage

Options

OptionDescriptionRequiredNotes
-m, --modelModel file path or model definition textYesWrap in quotes and use | as line separator
-p, --policyPolicy file path or policy definition textYesWrap in quotes and use | as line separator
-e, --enforceEvaluate access permissionsNoWrap arguments in quotes
-ex, --enforceExEvaluate permissions and display the matched policyNoWrap arguments in quotes
-AF, --addFuntionRegister a custom function (casbin-java-cli only)NoWrap in quotes and use | as line separator
-ap, --addPolicyAppend a policy rule to the policy file (casbin-java-cli only)NoWrap arguments in quotes
-rp, --removePolicyDelete a policy rule from the policy file (casbin-java-cli only)NoWrap 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}