package guardian

  1. Overview
  2. Docs
Role-based access control for OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

0.0.4.tar.gz
md5=45ecb4358a2405a3b4cc263f25ad995c
sha512=d070bd79c14123f720bd0c14291ee520bda3bfac5d6ceb58ceb85185c9be4cebbc5c99894ce29b62bf9e2ed4291ae094506d4d176c12945e99ed9d0283f55db8

Description

Defined actors can access targets according to specified rules (allowed actions: CRUD).

Published: 24 Nov 2022

README

Guardian

Generic framework for roles and permissions to be used in our projects

Limitations and Notes

  • Supported Database: Implementation with MariaDb (Sqlite only for testing)

  • Context (ctx): Allows to have multiple database pools (See next section)

Setup with MariaDB backend (MultiPools)

  let open Guardian_backend.Pools in
  let module MariaConfig = struct
    include DefaultConfig

    let database =
      MultiPools
        [ "pool-one", "mariadb://root@database:3306/dev"
        ; "pool-two", "mariadb://root@database:3306/test"
        ]
    ;;
  end
  in
  let module MariaDb = Guardian_backend.MariaDb.Make (Role) (Make (MariaConfig))
  let%lwt () = Lwt_list.iter (fun pool -> MariaDb.migrate ~ctx:["pool", pool] ()) ["pool-one"; "pool-two"]
  (** NOTE: To integrate migrations into your applications migration state see
      e.g. function 'MariaDB.find_migrations *)

Usage

The test directory shows an example implementation of how guardian can be used.

  • role.ml: Definition of actors and targets

  • role.mli: Signature of the defined actors and targets

  • guard.ml: Create the guardian service

  • article.ml: Definition of the article target

  • hacker.ml: Definition of the hacker actor

  • user.ml: Definition of the user actor and target

  • main.ml: implementation of all test cases

Example usage:

  module Guard = Guardian.Make (Role.Actor) (Role.Target)

  let thomas = "Thomas", Guard.Uuid.Actor.create ()
  let mike = "Mike", Guard.Uuid.Actor.create ()

  let thomas_article = Article.make "Foo" "Bar" thomas
  let mike_article = Article.make "Hello" "World" mike

  let example_rule = `Actor (snd mike), `Update, `Target thomas_article.uuid

  let initialize_authorizables_and_rules ?ctx =
    (* Note: As a user can be an actor and a target, both need to be initialized *)
    let* (_: [> `User ] MariaDb.authorizable) = User.to_authorizable ?ctx thomas in
    let* (_: [> `User ] MariaDb.authorizable) = User.to_authorizable ?ctx mike in
    let* (_: [> `User ] MariaDb.authorizable_target) = UserTarget.to_authorizable ?ctx thomas in
    let* (_: [> `User ] MariaDb.authorizable_target) = UserTarget.to_authorizable ?ctx mike in
    let* (_: [> `Article ] MariaDb.authorizable_target) = Article.to_authorizable ?ctx thomas_article in
    let* (_: [> `Article ] MariaDb.authorizable_target) = Article.to_authorizable ?ctx mike_article in
    let* () = MariaDb.Actor.save_rule ?ctx example_role in
    Lwt.return_unit

  (* let mike Update the title of thomas article -> returns a (Article.t, string) Lwt_result.t  *)
  let update_title = Article.update_title ?ctx mike thomas_article "Updated Title"

Development

A guide how to setup the project with devcontainers can be found here.

Commands

Most used commands can be found in the following list. For the full list of commands, checkout the Makefile.

  • make build - to build the project

  • make build-watch - to build and watch the project

  • make test - to run all tests. This requires a running MariaDB instance (sqlite will be created).

Release to production

In order to deploy to production:

  1. edit dune-project and update version (version 0.0.0)

  2. build the project dune build or edit pool.opam and update version version: "0.0.0"

  3. commit

  4. tag commit and push

  5. create github and opam release

Dependencies (19)

  1. yojson >= "2.0.2"
  2. uuidm >= "0.9.8"
  3. uri >= "4.2.0"
  4. sqlite3 >= "5.1.0"
  5. ppx_inline_test
  6. ppx_deriving_yojson >= "3.6.1"
  7. ppx_deriving >= "5.2.1"
  8. ocamlformat >= "0.24.1"
  9. ocaml >= "4.12.0"
  10. mariadb >= "1.1.4"
  11. lwt_ppx >= "2.1.0"
  12. lwt >= "5.6.1"
  13. logs >= "0.7.0"
  14. dune >= "2.9"
  15. containers >= "3.6"
  16. conformist >= "0.8.1"
  17. caqti-lwt >= "1.8.0" & < "2.0.0~"
  18. caqti-driver-mariadb >= "1.8.0" & < "2.0.0~"
  19. base64 >= "3.5.0"

Dev Dependencies (2)

  1. odoc with-doc
  2. alcotest-lwt with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.