Access Policies
AccessPolicy is an Aidbox custom resource representing a set of checks for the request. AccessPolicy resources are used to check request objects against a set of rules. If there are no AccessPolicy resources defined in Aidbox, all requests will be denied.
If AccessPolicies exist, Aidbox iterates through them, evaluating each AccessPolicy against the request object. As soon as one of them passes the validation (the evaluation result is true), the request is considered authorized, and Aidbox stops further policy evaluation. If all policies fail to validate it (all of them evaluated to false), then Aidbox denies the request and responds with 403 Forbidden.
The structure of the AccessPolicy resource and the request object are described below.
AccessPolicy resource structure
AccessPolicy resource has the following structure:
It supports various evaluation engines: Allow, JSON Schema, SQL, Matcho, and Complex, as well as Allow-RPC and Matcho-RPC. They specify how checks are implemented β with an SQL statement, with a JSON Schema, or as a list of allowed endpoints.
Request object structure
Aidbox evaluates AccessPolicy against a request object that represents an incoming HTTP request. It has the following structure:
AccessPolicy links
AccessPolicy instance can be linked to User, Client or Operation resources with the AccessPolicy.link field. If AccessPolicy has no links, itβs considered a global policy. To authorize a request, Aidbox uses global policies in addition to all AccessPolicy rules linked to User, Client, and Operation associated with the current request.
See tutorials:
Evaluation engines
Aidbox provides several ways to specify rules for AccessPolicy resources β so-called evaluation engines. They come with their syntax and offer varying degrees of flexibility and convenience for writing those rules.
There are five evaluation engines:
Allow
Simplest engine that allows everything for certain User
, Client
or makes Operation
public
- Testing and development environments - Superuser/admin access - Public endpoints
Matcho
Mathes request context with a set of rules
- Most common access control scenarios (90% of cases)
JSON Schema
Uses JSON Schema validation for request objects
- For those familiar with JSON Schema
SQL
Uses SQL queries for access control logic
- Complex data-dependent rules - When you need to join multiple tables - Performance-critical scenarios
Complex
Combines multiple engines and conditions
- Multi-step validation workflows - When you need to combine different types of checks - Advanced access control scenarios
It is recommended to pick the Matcho engine. In 90% of cases, it is enough. Sometimes, the complex access policy can only be written by SQL or Complex engines.
Allow engine
The Allow engine is the simplest. It allows all requests.
The allow engine always evaluates true regardless of the request object. Use it if you want to provide unrestricted access to everything.
Example:
Matcho engine
Example
In this example, the request is allowed only if:
Uri is related to Encounter. E.g.
/fhir/Encounter
or/Encounter
,HTTP request is either
GET
orPOST
, notPUT
,practitioner
parameter provided as a query string in case of theGET
request or body params in case of thePOST
request must be equal topractitioner_id
of the user,
User data, which is fetched from the "user" table, must contain the inpatient
department and practitioner.
SQL engine
The sql
engine executes an SQL statement and uses its return value as an evaluation result. Thus, the statement should return a single row with just one column:
SQL statement can refer to request fields with a double curly braces interpolation. The string inside the braces will be used as a path to value in the request object.
The SQL engine is sometimes the only way to perform complex checks when the AccesPolicy needs to check data stored in the database but not in the request context.
Example
Suppose that we are checking a request that comes with a User
resource referencing a Practitioner
through User.data.practitioner_id
element. The following policy allows requests only to /fhir/Patient/<patient_id>
URLs and only to those patients who have a Patient.generalPractitioner
element referencing the same practitioner as a User
of our current request. In other words, User
as a Practitioner
is only allowed to see his own patients β those who reference him as their generalPractitioner
.
Interpolation Rules
You can parameterize your SQL queries with request object using {{path}}
syntax. For example, to get a user role provided {user: {data: {role: "admin"}}}
you write {{user.data.role}}
. Parameter expressions are escaped by default to protect from SQL injection.
For dynamic queries β to parameterize table name, for example β you have to use {{!path}}
syntax. The expression SELECT true from {{!params.resource/type}} limit 1
when a request contains {params: {"resource/type": "Patient"}}
will be transformed into SELECT true from "patient".
By default, identifier names are double-quoted and lower-cased.
More examples
In this example, User
as a Practitioner
is only allowed to see the conditions of his patients β those who reference him as their generalPractitioner
.
JSON Schema
Example
The following policy requires request.params.resource/type
to be present and have a value of "Organization"
:
Complex
The complex
engine allows you to combine several rules with and
and or
operators. You can use any engine rule to define a rule and even complex
engine itself but it is forbidden to have both and
and or
keys on the same level. Rules are defined as an array of objects that must include an engine with a set of corresponding keys.
How AND & OR work
Aidbox applies inner policies one after the other top-bottom.
AND rule
Aidbox applies policies till one of two events happens:
One policy rejects the access. No further policies are applied. The access is rejected
There are no more policies to evaluate. It means the access is granted
OR rule
Aidbox applies policies till at least one grants access. If it has happened no further policies are applied.
Example 1
Policy in the example above represents the following logical expression:
check-1 AND (check-2 OR check-3)
Example 2
Let's split the SQL policy example from above into two separate rules and combine them under and
rule:
See also
Last updated
Was this helpful?