Restrict documents access
Document Access Control describes who is allowed to do what on the documents of a collection. At a high level, these are the steps to set it up:
- Create a policy – Describe the set of rules that will apply to private documents.
- Register a policy – Upload the policy into DefraDB.
- Create a permissioned collection – Attach the policy to a collection.
- Grant permissions to other actors – Create relations between an identity and a document.
When access control is configured, each actor should authenticate with their private key. An actor that creates a document in a permissioned collection owns the document. Authenticated requests can see public documents and the private documents they have permission to; unauthenticated requests can access only public documents.
Create policies
A policy is the set of rules to enforce on the documents it will be applied to.
Example – policy.yml
name: A basic policy
description: Thou shall read but not pass
resources:
- name: books
relations:
- name: reader
- name: updater
- name: deleter
permissions:
- name: read
expr: reader
- name: update
expr: updater + deleter
- name: delete
expr: deleter
name– Policy name.description(optional) – Policy description.resources– List of permissions definitions.name– Permission object name (refer to this when creating a permissioned collection).relations– List of relations that actors may be given to act on documents (similar to user roles).name– Relation name.
permissions– List specifying which relation will get which permission.name– Permission name (read,update,delete).expr(optional) – Expression involvingrelationsnames. Actors who fulfill the expression get the permission. Supported operators are union+and subtraction-.
Constraints for a policy to be valid
- The
permissionslist must include all ofread,update,delete, even ifexpris not specified. You can include more permissions: DefraDB will ignore them, but other applications may use them.
note
- The document creator gets all permissions, even if you omit a permission expression. For example, this policy grants read permission to
readeractors and write permissions only to the document creator:- name: readexpr: reader- name: update- name: delete - Relations allowed to update or delete automatically get permission to read. Subtract those relations if you don't want that:
name: readexpr: reader - updater - deleter