heathen

heathen

Attribute-based Access Control (ABAC) and a DDD application

Hello!

I’m trying to figure out how to implement a complicated authorization scheme and allow to change the rules at run-time (without altering code base). Currently I evaluating this for a DDD (CQRS\ES) application I’am planning but it is more general question I think.

So let’s imagine we have the next set of authorization rules in the specs:

Pic with a table

It is possible to hard-code this rules but obviously I think it’s a dirty practice to put authorization code into the core domain for the one hand, and it will be non-trivial to support and extend this rules, especially when new domains, entities and\or attributes come to the scene.

So I think about ABAC with some kind of XACML implementation. In this case it would be possible to make policies like:

Policy name: List awaiting documents orders
Effect: Allow
Union: All
Rule 1: CommandOrQuery == ListOrders
Rule 2: Subject.Role == “Branch operator”
Rule 3: Object.Creator.Branch == Subject.Branch
Rule 4: Object.Status == “Documents required”

In case of commands Object-related rules become conditions, I believe.

Then it will be necessary to send the whole object and subject (user in our case) to the Application level authorization service like:

defmodule MyApp.Orders.Queries.ListOrders do
  alias MyApp.Orders.Projections.{User, Order}

  def execute(params, %User{} = user) do
    case MyApp.SecurityDomain.can?(__MODULE__, Order, user) do
      :not_authorized -> {:error, :not_authorized}
      conditions -> params
                    |> Map.merge(conditions)
                    |> buildQuery()
                    |> runQuery()
    end
  end

end

defmodule MyApp.SecurityDomain do
  def can?(MyApp.Orders.Queries.ListOrders, object, subject) do
    ...
  end
end

Sure, there will be a lot of difficulties beginning from query builder, especially with rules\conditions like in my example (Order aggregation and projection must have a Creator structure (even sub-aggregate probably) with Branch field) and so on, Order domain must follow User events and update appropriate local user aggregates etc etc.

But this will be let the security officers to create very flexible rules right at the run-time without developers intervention and downtime.

Of course I may over-complicate things and the same can be done much more easily. If so please let me know where can I read best practice and realization (especially in Elixir). Haven’t seen any examples of the more or less complete authorization system, all applications\articles I found have very simple auth part.

What do you think?

Most Liked

StefanHoutzager

StefanHoutzager

A couple of messages around this subject appeared in different threads on this forum. The XACML standard https://en.wikipedia.org/wiki/XACML was named. As I built a (start of) a rules engine in the past, as part of a BPMS (start of also, haha), I wonder if it would be interesting to use the broader DMN standard for this, for which open source modelers, interpreters (execution engines I mean) and validators are available. At least from Camunda. Here some thoughts / questions about the position of XACML, modelers and execution engines (search for keyword XACML), note: the book is almost 10 yrs old: https://books.google.nl/books?id=8kJSr9JEaGsC&pg=PT256&lpg=PT256&dq="rules+engine"+xacml&source=bl&ots=yajaur0O6z&sig=5F6uKamom-m2Ae36sjUNMhrI91M&hl=en&sa=X&ved=2ahUKEwjR1_zfttjfAhXS66QKHfpcBjkQ6AEwCnoECAIQAQ#v=onepage&q="rules%20engine"%20xacml&f=false

karmajunkie

karmajunkie

I think you’re on the right track. A couple of suggestions:

  • If you’re using Commanded as your CQRS framework, which is the most commonly used in Elixir, you can use a middleware around command dispatch for authorization, which would be a good place to put your check.
  • There are a few libraries for authorization already on the scene; it might be worth looking at them.
  • If you’re going to roll your own (which is perfectly acceptable in my mind) you might look at using a protocol and providing implementations in different contexts that have different business rules around when a command can be dispatched.
MilosMosovsky

MilosMosovsky

Hey @heathen, it’s funny as I went to same scenario as you :slight_smile: (actually not that funny as it is common use case in real world app). I was struggling with hard coded rules as you but recently I created the library

I think it can cover many cases (if not all) in your described chart. Could you give a look to it? Thanks! I would love to see problems and cover any possible scenario needed and your complex table looks as good start :slight_smile:

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement