|
| 1 | +import { Callout } from "nextra/components"; |
| 2 | + |
| 3 | +# Querying Data |
| 4 | + |
| 5 | +This page walks through the main ways to query data in SpiceDB. The options are listed roughly in the order of how often you should be calling them, and roughly in order of their expected performance. Choose the one that makes sense for your use case, but consider whether your use case actually requires the call you're looking at. |
| 6 | + |
| 7 | +<Callout type="info"> |
| 8 | + In most of the APIs below, if you want to be able to read your write, you can |
| 9 | + pass a `consistency` parameter to the queries. Use either `fully_consistent` |
| 10 | + or `at_least_as_fresh(revision)` depending on how strict you need to be. See |
| 11 | + [Consistency](consistency) for more details. |
| 12 | +</Callout> |
| 13 | + |
| 14 | +<Callout type="important"> |
| 15 | + When invoking any of our APIs, you can send a header `X-Request-ID=somevalue` |
| 16 | + and it will be echoed back in the response, which makes correlating logs or |
| 17 | + tracing requests easy. |
| 18 | +</Callout> |
| 19 | + |
| 20 | +## Check Permission |
| 21 | + |
| 22 | +Send: |
| 23 | + |
| 24 | +- Subject Type |
| 25 | +- Subject ID |
| 26 | +- Permission (or relation) |
| 27 | +- Object Type |
| 28 | +- Object ID |
| 29 | + |
| 30 | +Receive: |
| 31 | + |
| 32 | +- Yes/no (or a provisional response if missing caveat data) |
| 33 | + |
| 34 | +[`CheckPermission`](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.CheckPermission) is the go-to for most access checks. It’s designed for high-traffic workloads. |
| 35 | + |
| 36 | +You can debug a check locally with `zed permission check resource:someresource somepermission user:someuser --explain` to understand how the decision was made. |
| 37 | + |
| 38 | +When your schema uses [caveats](caveats) and you don't provide all the required context in the request parameters, the API will tell you that in the response that the result is "conditional" instead of simply denying or allowing, and it's up to you to inspect that result. |
| 39 | + |
| 40 | +The `subject` of the query can be a single user (e.g. `user:someuser`) or a set of users (e.g. `group:engineering#member`). |
| 41 | + |
| 42 | +### CheckBulkPermissions |
| 43 | + |
| 44 | +Send Many: |
| 45 | + |
| 46 | +- Subject Type |
| 47 | +- Subject ID |
| 48 | +- Permission (or relation) |
| 49 | +- Object Type |
| 50 | +- Object ID |
| 51 | + |
| 52 | +Receive Many: |
| 53 | + |
| 54 | +- Yes/no (or a provisional response if missing caveat data) |
| 55 | + |
| 56 | +If your app needs to do multiple checks (for example, for various subjects), use the [`CheckBulkPermissions`](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.CheckBulkPermissions) API. It's great for UI workloads where you need to check multiple permissions at once: think tables, lists, and dashboards. |
| 57 | + |
| 58 | +It's also the recommended way to ask "what permissions does a given subject have on a resource?" |
| 59 | +Make a check for each permission in your schema. This will require you to update calling code when you add permissions, |
| 60 | +but the consuming code will need to be changed to include the logic associated with the new permission in any case. |
| 61 | + |
| 62 | +It's always preferable to perform one call to `CheckBulkPermissions` with N checks than N calls to `CheckPermission`, unless you don't want to wait for the N checks to finish. |
| 63 | + |
| 64 | +## LookupResources |
| 65 | + |
| 66 | +Send Many: |
| 67 | + |
| 68 | +- Subject Type |
| 69 | +- Subject ID |
| 70 | +- Permission (or relation) |
| 71 | +- Object Type |
| 72 | + |
| 73 | +Receive many: |
| 74 | + |
| 75 | +- Object ID |
| 76 | + |
| 77 | +[`LookupResources`](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.LookupResources) is a good choice when you need to find all resources of a given type that a specific subject can access. It supports cursoring and works well for moderate result sizes. |
| 78 | + |
| 79 | +It's a good way to do [prefiltering of results](/spicedb/modeling/protecting-a-list-endpoint#filtering-with-lookupresources) in |
| 80 | +a List endpoint, but it's a heavy request that can cause performance problems when more than 10k results are involved. |
| 81 | +In that case we recommend postfiltering with [CheckBulk](#checkbulkpermissions), and if that still doesn't work, |
| 82 | +we recommend evaluating [Materialize](/authzed/concepts/authzed-materialize). |
| 83 | + |
| 84 | +## LookupSubjects |
| 85 | + |
| 86 | +Send Many: |
| 87 | + |
| 88 | +- Subject Type |
| 89 | +- Permission (or relation) |
| 90 | +- Object Type |
| 91 | +- Object ID |
| 92 | + |
| 93 | +Receive many: |
| 94 | + |
| 95 | +- Subject ID |
| 96 | + |
| 97 | +[`LookupSubjects`](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.LookupSubjects) returns |
| 98 | +all subjects that have access to a specific resource. It does not support cursoring. |
| 99 | + |
| 100 | +It's commonly used to drive UIs and APIs that list users with given permission or set of permissions, such as a table of Admins |
| 101 | +on a particular Organization. |
| 102 | + |
| 103 | +Note that LookupSubjects will do a full path walk between an object and a subject, and will consider all valid paths between object and subject. |
| 104 | +If you are looking to find all the subjects that are on a specific relation, use `ReadRelationships` instead of `LookupSubjects`. |
| 105 | + |
| 106 | +If your schema includes exclusions and wildcards, the response can include a list of subjects that have been explicitly excluded. |
| 107 | + |
| 108 | +For example, if the schema is |
| 109 | + |
| 110 | +```zed |
| 111 | +definition user {} |
| 112 | +
|
| 113 | +definition document { |
| 114 | + relation blocked: user |
| 115 | + relation view: user:* |
| 116 | + permission viewer = view - blocked |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +and the relationships are |
| 121 | + |
| 122 | +```relationship filename="Relationships" |
| 123 | +document:finance#view@user:* |
| 124 | +document:finance#blocked@user:anne |
| 125 | +document:finance#blocked@user:bob |
| 126 | +``` |
| 127 | + |
| 128 | +Then `LookupSubjects` for `document:finance` will return a response of the form `{user:* - [user:anne,user:bob]}`. |
| 129 | + |
| 130 | +## ReadRelationships |
| 131 | + |
| 132 | +[`ReadRelationships`](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.ReadRelationships) is intended a an escape hatch, |
| 133 | +and should only be considered if no other API matches your use case. |
| 134 | + |
| 135 | +It's considerably more flexible in that you can send any combination of: |
| 136 | + |
| 137 | +- Subject Type |
| 138 | +- Subject ID |
| 139 | +- Permission (or relation) |
| 140 | +- Object Type |
| 141 | +- Object ID |
| 142 | + |
| 143 | +And receive all relationships that match that filter, but it comes with some important caveats: |
| 144 | + |
| 145 | +- It's generally less optimized than the `Check` and `Lookup` APIs because of that flexibility |
| 146 | +- It does no caching. The `Check` and `Lookup` APIs gain much of their performance by caching the results of subproblem computation; `ReadRelationships` does no caching and will go directly to the database every time. |
| 147 | +- The indexes in the backing [datastores](/spicedb/concepts/datastores) are optimized around the `Check` and `Lookup` APIs, so it's possible to |
| 148 | + craft a `ReadRelationships` request that misses indexes and requires a full table scan. This is because covering all combinations of filters |
| 149 | + with an index would impact write performance too much. |
| 150 | + |
| 151 | +Some use cases where `ReadRelationships` may be warranted: |
| 152 | + |
| 153 | +- Deleting all relationships in a subtree, where a simple [RelationshipFilter](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.RelationshipFilter) in a [DeleteRelationships](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.DeleteRelationshipsRequest) would be insufficient |
| 154 | +- Admin UIs that show lists of roles or relationships between objects |
| 155 | + |
| 156 | +## Watch |
| 157 | + |
| 158 | +The Watch API is not meant to answer permission questions but to serve other use-cases, like auditing. See [the Watch API documentation](/spicedb/concepts/watch) for more details. |
| 159 | + |
| 160 | +## ExpandPermissionTree |
| 161 | + |
| 162 | +This [ExpandPermissionTree](https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.PermissionsService.ExpandPermissionTree) API |
| 163 | +comes from the [Zanzibar Paper](https://authzed.com/zanzibar#2.4.5-expand). It allows you to examine the subtree of relationships |
| 164 | +around a particular point in the graph, and is useful in scenarios like your UI needing to show which users _and groups_ have access to something. |
| 165 | + |
| 166 | +In practice we don't see many uses of this API, especially because it only resolves one "hop" in the graph at a time and must be called |
| 167 | +repeatedly to get a full picture of a subtree. |
0 commit comments