Re: LYDB: Feasible to use PG roles instead of application-level security?

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Guyren Howe <guyren(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: LYDB: Feasible to use PG roles instead of application-level security?
Date: 2016-12-30 11:23:27
Message-ID: 5155877D-016E-47E9-A6FA-97204D17165C@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On 30 Dec 2016, at 4:19, Guyren Howe <guyren(at)gmail(dot)com> wrote:
>
> Further to my attempts to enlighten application developers about what they might better do in the database:
>
> https://medium.com/@gisborne/love-your-database-lydb-23c69f480a1d#.4jngp2rcb
>
> it occurs to me to wonder whether it is practical to use PG’s own roles and security model in lieu of using an application-level one.
>
> It seems that the role system in PG is sufficient for most general purposes. One could presumably also have a table with role names and associated metainformation (email address etc) as needed.
>
> If I have a system with many thousands of users, is it practical to manage these users’ authentication and authorization using *just* Postgres?

Postgres roles are global to the cluster, so you would end up with multiple thousands of roles if you have multiple databases in your cluster with different users on each. Which roles each user is allowed to have becomes quite the nightmare for the administrators, I suspect.

For a web-application facing the internet, I'd say no, don't do that. You're dealing with far too many users to be maintainable.

For an intranet database in a not-too-large company with a fixed set of users, it could be a good solution, especially if those roles can be linked to the company's LDAP server (assuming that's possible, I don't know). Multiple intranet applications on that same database can use the same users and roles.

Someone needs to do the administration though; with volumes (of users) like that and the database knowledge level of the average system administrator, a GUI seems preferable. IMHO, pgadmin provides too many features to be practical for someone like that, you would probably prefer something that only does user administration. I don't know of anything that does that though (not a GUI user myself)...

> It occurs to me that some client frameworks might have issues with their connection pools if those connections keep switching users, assuming they even can, but let’s set that aside for now. Or perhaps every connection could immediately do a SET USER before executing its connection?
>
> This seems an attractive proposition from a security standpoint: if I use row-level security pervasively, I can have a security system that’s nestled nice and close to the data and presumably tricky to work around from a hacker given direct access only to the client application.

With a few changes, that could work very well.

First, create roles for the different types of users that you expect. In a company, that could be by division, distinguishing division-heads, interns, etc.

Secondly, have a table with the users and their attributes like you describe. Include an attribute for their database role there. Only administrator users should have access to that table.

Finally, create a stored procedure that looks up a user name in that table and sets the accompanying role. If a user is not found, set the role to some default 'unprivileged' user.
Make that procedure a SECURITY DEFINER with according permissions. That role stays active the entire session, so unless you close the connection, create a new one or change the user's role, this procedure doesn't need calling again.

> Is this practical? Has anyone here done it? What might the caveats be?

It's a fairly common practice, the ML archives should contain plenty of examples.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Charles Clavadetscher 2016-12-30 11:28:42 Re: Special index for "like"-based query
Previous Message Job 2016-12-30 10:42:24 R: Special index for "like"-based query