RBAC for an HTTP API
The API of a reports service lets a user call an endpoint if a role of the user permits the method and the path. A user can always read the profile of that user.
Bundle default, revision 2026-12-03, decision data.httpapi.authz.decision
Examples
Input: alice-edits-a-report
{ "method": "PUT", "path": "/reports/q3", "user": "alice" }
Files
Show tree shaking for the decision
1# The tests of the policy. `opa test` runs them, and `just test-rego` with rego4k. `DemoBundlesTest` runs them as JUnit
2# tests. The debugger offers the input of each test as an example.
3package httpapi.authz_test
4
5import data.httpapi.authz
6
7test_viewer_reads_a_report if {
8 authz.allow with input as {"method": "GET", "path": "/reports/q3", "user": "alice"}
9}
10
11test_viewer_cannot_edit_a_report if {
12 not authz.allow with input as {"method": "PUT", "path": "/reports/q3", "user": "alice"}
13}
14
15test_user_reads_own_profile if {
16 authz.allow with input as {"method": "GET", "path": "/users/bob", "user": "bob"}
17}
18
19test_unknown_user_has_no_roles if {
20 authz.decision == {"allow": false, "roles": []} with input as {"method": "GET", "path": "/reports/q3", "user": "mallory"}
21}
22
23# Each case is a test of its own, with the note as its name.
24test_permissions[tc.note] if {
25 some tc in [
26 {"note": "an editor edits a report", "user": "bob", "method": "PUT", "path": "/reports/q3", "allow": true},
27 {"note": "an editor cannot delete a report", "user": "bob", "method": "DELETE", "path": "/reports/q3", "allow": false},
28 {"note": "an admin deletes a user", "user": "carol", "method": "DELETE", "path": "/users/bob", "allow": true},
29 {"note": "a viewer cannot read another profile", "user": "alice", "method": "GET", "path": "/users/bob", "allow": false},
30 ]
31 authz.allow == tc.allow with input as {"method": tc.method, "path": tc.path, "user": tc.user}
32}
33