Thursday, April 12, 2012

Security decisions: Separate Operations & Queries

Security decisions: Separate Operations & Queries:
The question came up several times in the mailing list with regards to how the RavenDB Authorization Bundle operates, and I think it serves a broader discussion.
Let us imagine a system where we have contracts, which may be in several states:

  • Mine – Contracts that an employee signed.

  • Done – Standard users can view, Lawyers assigned to the company can sign.

  • Draft – Lawyers can view / edit, Partners can approve.

  • Proposed – Lawyers can create / edit, but only the lawyer that created it can view it, Partners can accept.


So far, fairly simple, right? Except the pure hell that you are going to get into when you are trying to show the users all of the contracts that they can see, sorted by edit date and in the NDA category.
Why am I being so negative here? Well, let us look at what we are going to have to do in the most trivial of cases:
image
In this sort of system, we are going to have to show the user all of the contracts that they are allowed to see, and show them some indication what operations they can do on each.
The problem is that generating this sort of view is expensive. Especially when you have large amount of data to work through. More interesting, from a UX perspective, it also doesn’t really work that well. Most users would want a better separation of the things that they can do, probably something like this:
image
This allows us to do a first level filtering on the data itself, rather than try to apply security rules to it.
In the first case, we need to get all the contracts that we are allowed to see. The security rules above are really simple, mind. But trying to translate them into an efficient query is going to be pretty hard. Both in terms of the code requires and the cost to actually perform the query on the server. There are other things that are involved as well, such as paging and sorting in such an environment.  I have created several such systems in the past, Rhino Security is probably the most well known of them, and it gets really hard to optimize things and make sure that everything works when you start getting more complex security rules (especially when you have a user editable security system, which is a common request).
The second case is cheaper because we can limit the choices that we see in the query itself. We may still need to apply security concerns, but those goes through the query directly, rather than a security sub system. This kind of change usually force people to be more explicit in what they want, and it result in a system that tends to be simpler. The security rules aren’t just something arbitrary that can be defined, they are actually visible on the screen (My Contracts, Drafts, etc). Changing them isn’t something that is done on an administrator’s whim.
Yes, this is a way to manage the client and their expectations, but that is important. But what about the complex security that they want?
That might still be there, certainly, but that would be active mostly for operations (stuff that happen on a single entity), not on things that happen over all entities. It is drastically easier to make a single entity security decisions work efficiently than make it work over the whole set inside the database.

No comments:

Post a Comment

Thank's!