CLI

Using the CLIv0.6.0

Check policy files, evaluate requests, and read explanations in English or French.
maelys-datalog · CLI 0.6.0EXAMPLE SESSION
$ maelys-datalog check --domain domain.json policy.dl Policy valid. $ maelys-datalog solve --domain domain.json --facts facts.dl policy.dl allow/2 (1 fact): allow("Leela", "billing:read"). can_deliver/1 (1 fact): can_deliver("Leela"). $ maelys-datalog explain --domain domain.json --facts facts.dl --why false policy.dl can_deliver '"Mallory"' The fact is not derived: can_deliver("Mallory"). Rule 1 cannot derive allow("Mallory", "billing:read"). With R = "billing:read", U = "Mallory". No matching fact for right("Mallory", "billing:read"). Rule 2 cannot derive can_deliver("Mallory"). With U = "Mallory". No matching fact for allow("Mallory", "billing:read").

Recorded CLI session. The complete inputs, commands and output appear below.

maelys-datalog checks policy files, evaluates facts and explains a decision in the terminal. Start with three small files, then ask why a request is allowed or refused.

Install

PLATFORMChoose your platform

macOS

METHODChoose your installation method
brew tap maelys-dev/tap
brew trust maelys-dev/tap
brew install maelys-dev/tap/maelys-datalog

For an archive or source installation, add $HOME/.local/bin to your shell’s PATH if it is not already there.

Create three files

A predicate is a named relation, such as request. Its arity is the number of arguments: request/2 has two. The domain declares these relations and which ones you may query. policy_fact marks policy facts, edb marks runtime inputs, and idb marks facts derived by rules. query: true allows a relation to be queried.

Save this as domain.json:

JSON
{
  "format": "maelys-datalog-domain-v1",
  "name": "rbac_cli",
  "predicates": [
    {"name": "right", "arity": 2, "role": "policy_fact"},
    {"name": "request", "arity": 2, "role": "edb"},
    {"name": "allow", "arity": 2, "role": "idb", "query": true},
    {"name": "can_deliver", "arity": 1, "role": "idb", "query": true}
  ],
  "atoms": ["Leela", "billing:read"]
}

Save the policy as policy.dl. right lists policy rights; the two rules derive an allowed operation and a deliverable request. U stands for a user and R for a right.

DATALOG
right("Leela", "billing:read").
allow(U, R) :- right(U, R).
can_deliver(U) :- allow(U, "billing:read"), request(U, "billing:read").

Save the runtime facts as facts.dl. Leela requests an operation named in the policy; Mallory requests one that is not granted.

DATALOG
% one permitted request and one refused request
request("Leela", "billing:read").
/* The operation is deliberately absent from the policy's rights. */
request("Mallory", "billing:write").

Run every command below from the directory containing those three files.

Check the policy and discover its queries

maelys-datalog checkCOMMAND & OUTPUT
maelys-datalog check --domain domain.json policy.dl
Policy valid.

queries lists the relations this policy allows you to ask about:

maelys-datalog queriesCOMMAND & OUTPUT
maelys-datalog queries --domain domain.json policy.dl
policy "cli" (domain rbac_cli):
  allow/2
  can_deliver/1

inspect describes the compiled policy without evaluating facts. Its input and derived capacities are zero because no facts are loaded:

maelys-datalog inspectCOMMAND & OUTPUT
maelys-datalog inspect --domain domain.json policy.dl
policy "cli" (domain rbac_cli):
  4 predicates, 1 policy facts, 2 rules
  program limits: input=1024, derived=1024, per predicate=64
  inspection capacity: input=0, derived=0, symbols=512, text bytes=32768
  allow/2
  can_deliver/1

Evaluate the requests

maelys-datalog solveCOMMAND & OUTPUT
maelys-datalog solve --domain domain.json --facts facts.dl policy.dl
allow/2 (1 fact):
  allow("Leela", "billing:read").
can_deliver/1 (1 fact):
  can_deliver("Leela").

A printed Datalog fact ends with a period. The result includes can_deliver("Leela").; it does not include Mallory. To print only the allow/2 relation:

maelys-datalog solveCOMMAND & OUTPUT
maelys-datalog solve --domain domain.json --facts facts.dl --query allow/2 policy.dl
allow/2 (1 fact):
  allow("Leela", "billing:read").

Explain an allowed request

--why true asks for a derivation. Keep the double quotes around a symbol inside one shell argument, as in '"Leela"':

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why true policy.dl can_deliver '"Leela"'
The fact is present: can_deliver("Leela").

Rule 1 derives allow("Leela", "billing:read").
  - right("Leela", "billing:read"). (policy fact)

Rule 2 derives can_deliver("Leela").
  - allow("Leela", "billing:read"). (derived fact)
  - request("Leela", "billing:read"). (input fact)

Explain a missing request

--why false asks which obstacles prevent a fact from being derived:

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why false policy.dl can_deliver '"Mallory"'
The fact is not derived: can_deliver("Mallory").

Rule 1 cannot derive allow("Mallory", "billing:read").
  With R = "billing:read", U = "Mallory".
  No matching fact for right("Mallory", "billing:read").

Rule 2 cannot derive can_deliver("Mallory").
  With U = "Mallory".
  No matching fact for allow("Mallory", "billing:read").

The explanation names source variables such as U and R, writes premises as Datalog facts, and follows the rules used to derive the answer.

Here the missing right fact prevents the rule from granting Mallory access. A missing result is an answer about these files and this batch of runtime facts.

For the same explanation in French:

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why false policy.dl can_deliver '"Mallory"' --lang fr
Le fait n’est pas déduit : can_deliver("Mallory").

La règle 1 ne peut pas déduire allow("Mallory", "billing:read").
  Avec R = "billing:read", U = "Mallory".
  Aucun fait ne correspond à right("Mallory", "billing:read").

La règle 2 ne peut pas déduire can_deliver("Mallory").
  Avec U = "Mallory".
  Aucun fait ne correspond à allow("Mallory", "billing:read").

Choose the question

Asking for a derivation of a missing fact reports that it was not derived:

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why true policy.dl can_deliver '"Mallory"'
The fact is not derived: can_deliver("Mallory").
No derivation is available. Use --why false to inspect the obstacles.

Asking why an already derived fact is absent reports that the absence question does not apply:

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why false policy.dl can_deliver '"Leela"'
The fact is present, so an absence explanation does not apply:
can_deliver("Leela").

Use the result from a script

--format json keeps the complete machine-readable result, including the original SDK document in document and typed explanation fields in structure. Text output is for reading. --raw explicitly prints the original SDK document when needed.

To select one field as a JSON record, use --format jsonl with --field:

maelys-datalog explainCOMMAND & OUTPUT
maelys-datalog explain --domain domain.json --facts facts.dl --why false policy.dl can_deliver '"Mallory"' --format jsonl --field present
false

A command exits with status 0 on success, 1 for an execution error, and 2 for a completed validation report containing violations. A fact that is absent is not itself an execution error.

Next