Skip to content

Commit 223d43a

Browse files
committed
feat: update example schemas shown in playground
1 parent a376dc7 commit 223d43a

File tree

16 files changed

+338
-440
lines changed

16 files changed

+338
-440
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Developers create a schema that models their permissions requirements and use a
1717
Examples in this repository include:
1818

1919
- How to set up SpiceDB with tracing: see [tracing](./tracing)
20-
- How to invoke SpiceDB as a library: see [library](./library)
20+
- How to invoke SpiceDB as a library: see [library](./spicedb-as-library)
2121
- How to run SpiceDB in a Kubernetes cluster: see [kubernetes](./kubernetes)
2222
- CI/CD Workflows
2323

schemas/basic-rbac/README.md

Lines changed: 0 additions & 99 deletions
This file was deleted.

schemas/basic-rebac/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Simple Relationship Based Access Control (ReBAC)
2+
3+
Access is granted to users based on the relation(s) that they have to a given document.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
---
22
schema: |-
33
/**
4-
* user represents a user that can be granted role(s)
4+
* an entity that can be granted permissions
55
*/
66
definition user {}
77
88
/**
9-
* document represents a document protected by Authzed.
9+
* a resource that we are trying to protect
1010
*/
1111
definition document {
1212
/**
13-
* writer indicates that the user is a writer on the document.
13+
* users can be made writers of specific documents
1414
*/
1515
relation writer: user
1616
1717
/**
18-
* reader indicates that the user is a reader on the document.
18+
* users can be made readers of specific documents
1919
*/
2020
relation reader: user
2121
2222
/**
23-
* edit indicates that the user has permission to edit the document.
23+
* if a user has the writer relationship to a specific document, they automatically get permission to edit it
2424
*/
2525
permission edit = writer
2626
2727
/**
28-
* view indicates that the user has permission to view the document, if they
29-
* are a `reader` *or* have `edit` permission.
28+
* if a user has the reader relation to a document OR the permission to edit a document (or both), they automatically get permission to view it
3029
*/
3130
permission view = reader + edit
3231
}

schemas/caveats/README.md

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
# Caveats for conditional access
1+
# Simple Attribute Based Access Control (ABAC)
22

3-
Models the use of caveats, which allows for conditional access based on information provided at _runtime_ to permission checks.
3+
Access can be granted to users based on information provided at runtime to permission checks.
44

5-
---
6-
7-
## Schema
8-
9-
```
10-
definition user {}
11-
12-
/**
13-
* only allowed on tuesdays. `day_of_week` can be provided either at the time
14-
* the relationship is written, or in the CheckPermission API call.
15-
*/
16-
caveat only_on_tuesday(day_of_week string) {
17-
day_of_week == 'tuesday'
18-
}
19-
20-
definition document {
21-
/**
22-
* reader indicates that the user is a reader on the document, either
23-
* directly or only on tuesday.
24-
*/
25-
relation reader: user | user with only_on_tuesday
26-
27-
permission view = reader
28-
}
29-
```

schemas/caveats/schema-and-data.yaml

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
11
---
22
schema: |-
3+
/**
4+
* an entity that can be granted permissions
5+
*/
36
definition user {}
47
58
/**
6-
* only allowed on tuesdays. `day_of_week` can be provided either at the time
7-
* the relationship is written, or in the CheckPermission API call.
9+
* a resource that we are trying to protect
810
*/
9-
caveat only_on_tuesday(day_of_week string) {
10-
day_of_week == 'tuesday'
11-
}
12-
13-
caveat ip_allowlist(user_ip ipaddress, cidr string) {
14-
user_ip.in_cidr(cidr)
15-
}
16-
1711
definition document {
1812
/**
19-
* reader indicates that the user is a reader on the document, either directly,
20-
* only on tuesday, or from allowed IPs.
13+
* users can be made readers of specific documents,
14+
* either directly, or only if they have a valid IP, or only if they aren't rate limited.
2115
*/
22-
relation reader: user | user with only_on_tuesday | user with ip_allowlist
16+
relation reader: user | user with has_valid_ip | user with not_rate_limited
2317
18+
/**
19+
* if a user has the reder relationship to a specific document, they automatically get permission to view it
20+
*/
2421
permission view = reader
2522
}
23+
24+
/**
25+
* only allowed if the IP address is allowed.
26+
* we can provide cidr at the time we write the relation, and
27+
* we can provide user_ip at the time the CheckPermission is made.
28+
*/
29+
caveat has_valid_ip(user_ip ipaddress, cidr string) {
30+
user_ip.in_cidr(cidr)
31+
}
32+
33+
/**
34+
* only allowed if rate limits haven't been exceeded.
35+
* we can provide allowed_max at the time we write the relation, and
36+
* we can provide current at the time the CheckPermission is made.
37+
*/
38+
caveat not_rate_limited(allowed_max int, current int) {
39+
current < allowed_max
40+
}
2641
relationships: |-
2742
document:firstdoc#reader@user:fred
28-
document:firstdoc#reader@user:tom[only_on_tuesday]
29-
document:firstdoc#reader@user:alice[ip_allowlist:{"cidr":"1.2.3.0/24"}]
43+
document:firstdoc#reader@user:tom[not_rate_limited:{"allowed_max":100}]
44+
document:firstdoc#reader@user:alice[has_valid_ip:{"cidr":"1.2.3.0/24"}]
3045
assertions:
3146
assertTrue:
32-
- 'document:firstdoc#view@user:tom with {"day_of_week": "tuesday"}'
47+
- 'document:firstdoc#view@user:tom with {"current": 1}'
3348
- "document:firstdoc#view@user:fred"
3449
- 'document:firstdoc#view@user:alice with {"user_ip": "1.2.3.4"}'
3550
assertCaveated:
3651
- "document:firstdoc#view@user:tom"
3752
- "document:firstdoc#view@user:alice"
3853
assertFalse:
39-
- 'document:firstdoc#view@user:tom with {"day_of_week": "wednesday"}'
54+
- 'document:firstdoc#view@user:tom with {"current": 500}'
4055
- 'document:firstdoc#view@user:alice with {"user_ip": "8.8.8.8"}'
4156
validation:
4257
document:firstdoc#view:
Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,3 @@
1-
# Google Docs-style Sharing
1+
# Group membership and parent-of/child-of relations
22

3-
Models a Google Docs-style sharing permission system where users can be granted direct access to a resource, or access via organizations and nested groups.
4-
5-
---
6-
7-
## Schema
8-
9-
```
10-
definition user {}
11-
12-
definition resource {
13-
relation manager: user | usergroup#member | usergroup#manager
14-
relation viewer: user | usergroup#member | usergroup#manager
15-
16-
permission manage = manager
17-
permission view = viewer + manager
18-
}
19-
20-
definition usergroup {
21-
relation manager: user | usergroup#member | usergroup#manager
22-
relation direct_member: user | usergroup#member | usergroup#manager
23-
24-
permission member = direct_member + manager
25-
}
26-
27-
definition organization {
28-
relation group: usergroup
29-
relation administrator: user | usergroup#member | usergroup#manager
30-
relation direct_member: user
31-
32-
relation resource: resource
33-
34-
permission admin = administrator
35-
permission member = direct_member + administrator + group->member
36-
}
37-
```
38-
39-
### user
40-
41-
`user` is an example of a "user" type, which is used to represent users. The definition itself is empty, as it is only used for referencing purposes.
42-
43-
```zed
44-
definition user {}
45-
```
46-
47-
### resource
48-
49-
`resource` is the definition used to represent the resource being shared
50-
51-
```zed
52-
definition resource {
53-
relation manager: user | usergroup#member | usergroup#manager
54-
relation viewer: user | usergroup#member | usergroup#manager
55-
56-
permission manage = manager
57-
permission view = viewer + manager
58-
}
59-
```
60-
61-
Within the definition, there are defined two relations: `viewer` and `manager`, which are used to represent roles for users _or members/managers of groups_ for the resource, as well as the `view` and `manage` permissions for viewing and managing the resource, respectively.
62-
63-
### usergroup
64-
65-
`usergroup` is the definition used to represent groups, which can contain either users or other groups. Groups support a distinction between member and manager.
66-
67-
```zed
68-
definition usergroup {
69-
relation manager: user | usergroup#member | usergroup#manager
70-
relation direct_member: user | usergroup#member | usergroup#manager
71-
72-
permission member = direct_member + manager
73-
}
74-
```
75-
76-
### organization
77-
78-
`organization` is the definition used to represent the overall organization.
79-
80-
```zed
81-
definition organization {
82-
relation group: usergroup
83-
relation administrator: user | usergroup#member | usergroup#manager
84-
relation direct_member: user
85-
86-
relation resource: resource
87-
88-
permission admin = administrator
89-
permission member = direct_member + administrator + group->member
90-
}
91-
```
92-
93-
Organizations contain four relations (`group`, `resource`, `member`, `administrator`) which are used to reference the groups, resources, direct members and administrator users for the organization.
94-
95-
#### member permission
96-
97-
The `member` permission under organization computes the transitive closure of _all_ member users/groups of an organization by combining data from three sources:
98-
99-
1. `direct_member`: users directly added to the organization as a member
100-
2. `administrator` is used to add any users found as an `administrator` of the organization
101-
3. `group->member` is used to walk from the organization to any of its groups, and then from the `group` to any of its members. This ensure that if a user is available under `member` under any group in the organization, they are treated as a member of the organization as well.
3+
Access can be granted via direct access, or by being member of a group (that can be a child of another group) that has access to the resource.

0 commit comments

Comments
 (0)