Skip to content

Commit e5e5b6c

Browse files
committed
initial version
1 parent 3b3c5d9 commit e5e5b6c

29 files changed

+1839
-2
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
go: ['1.24', '1.25']
13+
timeout-minutes: 30
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
- name: Set up Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: ${{ matrix.go }}
21+
- name: Start NATS server and test
22+
run: make start-nats-server test
23+
- name: Start NATS server and benchmark
24+
run: make start-nats-server benchmark
25+
- name: Stop NATS server
26+
if: always()
27+
run: make stop-nats-server

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ go.work.sum
2828
.env
2929

3030
# Editor/IDE
31-
# .idea/
32-
# .vscode/
31+
.vscode/
32+
/.idea/
33+
*.iws
34+
*.iml
35+
*.ipr

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
SHELL := /bin/bash
2+
.SHELLFLAGS += -o pipefail -O extglob
3+
.DEFAULT_GOAL := help
4+
5+
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
6+
7+
.PHONY: help
8+
help: ## Display this help
9+
@printf "\nUsage:\n make \033[36m<target>\033[0m\n"
10+
@awk 'BEGIN {FS = ":.*##";} \
11+
/^[a-zA-Z_0-9-]+:.*?##/ { \
12+
printf " \033[36m%-35s\033[0m %s\n", $$1, $$2 } \
13+
/^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) \
14+
} ' \
15+
$(MAKEFILE_LIST)
16+
17+
18+
##@ Test targets
19+
20+
.PHONY: test
21+
test: ## run tests
22+
go test -v -race -count=1 ./...
23+
24+
.PHONY: benchmark
25+
benchmark: ## run benchmarks
26+
go test -bench=. -benchmem ./...
27+
28+
##@ Run targets
29+
30+
.PHONY: start-nats-server
31+
start-nats-server: ## start NATS server
32+
docker compose -f tests/nats/docker-compose.yaml up -d --force-recreate --wait
33+
34+
.PHONY: stop-nats-server
35+
stop-nats-server: ## stop NATS server
36+
docker compose -f tests/nats/docker-compose.yaml down --volumes
37+
38+
##@ Auxiliary targets
39+
40+
.PHONY: list-rules
41+
list-rules: ## list all casbin rules in the NATS KV store
42+
@for key in $$(nats kv ls casbin_rules | grep -v '^No ' | awk '{print $$1}'); do \
43+
echo "== $$key =="; \
44+
nats kv get casbin_rules $$key; \
45+
done
46+
47+
.PHONY: list-keys
48+
list-keys: ## list all keys in the NATS KV store
49+
nats kv ls casbin_rules
50+
51+
.PHONY: del-bucket
52+
del-bucket: ## delete the casbin_rules bucket
53+
nats kv del casbin_rules

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Casbin JetStream
2+
====
3+
4+
[![Build](https://github.com/grepplabs/casbin-jetstream/actions/workflows/ci.yml/badge.svg)](https://github.com/grepplabs/casbin-jetstream/actions/workflows/ci.yml)
5+
6+
Casbin JetStream is the [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from JetStream or save policy to it.
7+
8+
## Installation
9+
10+
go get github.com/grepplabs/casbin-jetstream
11+
12+
## Usage Examples
13+
14+
### Basic Usage
15+
16+
```go
17+
package main
18+
19+
import (
20+
"github.com/casbin/casbin/v2"
21+
jsadapter "github.com/grepplabs/casbin-jetstream"
22+
)
23+
24+
func main() {
25+
// Initialize a casbin jetstream adapter and use it in a Casbin enforcer:
26+
a, _ := jsadapter.NewAdapter(&jsadapter.Config{
27+
URL: "nats://localhost:4222",
28+
})
29+
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
30+
31+
// Load the policy from KV Store.
32+
e.LoadPolicy()
33+
34+
// Check the permission.
35+
e.Enforce("alice", "data1", "read")
36+
37+
// Modify the policy.
38+
// e.AddPolicy(...)
39+
// e.RemovePolicy(...)
40+
41+
// Save the policy back to KV Store.
42+
e.SavePolicy()
43+
}
44+
```
45+
46+
### With mTLS
47+
48+
```go
49+
50+
a, _ := jsadapter.NewAdapter(&jsadapter.Config{
51+
URL: "nats://localhost:4223",
52+
Bucket: "casbin_rules",
53+
TLSConfig: jsadapter.TLSConfig{
54+
Enable: true,
55+
Refresh: 15 * time.Second,
56+
File: jsadapter.TLSClientFiles{
57+
Cert: "/etc/nats/certs/nats-client.pem",
58+
Key: "/etc/nats/certs/nats-client-key.pem",
59+
RootCAs: "/etc/nats/certs/ca.pem",
60+
},
61+
},
62+
})
63+
```

0 commit comments

Comments
 (0)