pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Miles Elam <miles.elam@productops.com>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Inserts restricted to a trigger
Date: Fri, 21 Jun 2019 16:02:51 -0700
Message-ID: <d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com> (raw)
In-Reply-To: <CAALojA-OK6wK13kLJw3LM5a0oy92uXyj=jftC7RrO+AAqn+L-A@mail.gmail.com>
References: <CAALojA-Mn+V+j9gdS2-jyoCzAGrjwGQh2hJq0H1it5M4=UaHBw@mail.gmail.com>
	<6fe78cce-7e74-926b-7d3d-2e09da698cc8@aklaver.com>
	<20190618004728.ph5aeg4wlucll7y5@raf.org>
	<CAALojA82fH0Nbb+qKRPJpx_mcdZ1Dh3xQrBk3hTQBbQ9kbcWtA@mail.gmail.com>
	<CAKkG4_mu=--NFCJO1R7n5DfXPX7Y+RcxFPha7d7e9ppsebncXg@mail.gmail.com>
	<CAALojA_ODo7GP_zq6y5s2WBwjbCwMT9AMR0omSQngZur9Bzkxg@mail.gmail.com>
	<b5b505ba-e9e6-98ee-a2b7-57d5d51bae09@aklaver.com>
	<CAALojA-gg3yRt61hcUKzU67ns0o+z-Fg=46gTRkQ6E_Vnqf+yg@mail.gmail.com>
	<48ddb8b0-2628-9b9e-84c4-955f5c15ddd1@aklaver.com>
	<CAALojA-OK6wK13kLJw3LM5a0oy92uXyj=jftC7RrO+AAqn+L-A@mail.gmail.com>

On 6/20/19 3:30 PM, Miles Elam wrote:
> Thanks for the reply, Adrian.
> 
> 1. The audit tables (plural) are recording the historical data for a 
> table, ie., updates and deletes. All the same data as found in a given 
> table along with the role that performed the operation, the transaction 
> id, and the time range where this data was in active use.
> 
> 2. Only thorough a web UI via an API service.
> 
> 3. Should be limited to web app, but the data scientists may need direct 
> access in the near future.
> 
> PGAudit does not cover our use case. We are making a temporal table 
> system since PostgreSQL does not support one natively. For example: 
> "What would this query have returned yesterday at 4:27pm PT?" Access is 
> as expected for inserts but updates and deletes are logged to history 
> tables. We cannot use 3rd party extensions because we are on AWS managed 
> databases. We are following the model detailed here 
> (https://wiki.postgresql.org/wiki/SQL2011Temporal) with some modifications.
> 
> Given the model listed in the link, it's not clear how we can prevent 
> user tampering with history inserts. (History updates and deletes are 
> already REVOKE restricted.) Since we are going through an API server via 
> REST and/or GraphQL, the possibility is very unlikely, but we would 
> prefer a defense in depth approach in case an oversight somehow allowed 
> arbitrary query access to the database with the web user. For the most 
> part, we're fairly well locked down, but I just can't quite see how to 
> restrict aforementioned query access from inserting to the history in an 
> ad-hoc manner rather than the trigger-based predetermined insert pattern.

Some draft ideas:

1) A session table that among other things records the Web Token/user 
combination. Then in the trigger(s) that INSERT into the history table 
check that the user has a valid current token.

2) In the inner function use PG_CONTEXT:

https://www.postgresql.org/docs/11/plpgsql-control-structures.html#PLPGSQL-CALL-STACK

to determine whether the inner SECURITY DEFINER function is being called 
directly or through the outer trigger function.

> 
> 
> 
> On Thu, Jun 20, 2019 at 8:01 AM Adrian Klaver <adrian.klaver@aklaver.com 
> <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 6/19/19 3:07 PM, Miles Elam wrote:
>      > Hi Adrian, thanks for responding.
>      >
>      > How would I restrict access to the SECURITY DEFINER function? If
>     it can
>      > be called by the trigger, it can be called by the user as well I
>     would
>      > think. Same issue as access to the table itself only now with a
>      > superuser intermediary, right?
>      >
> 
>     Should have also mentioned, if you are not adverse to a third party
>     solution there is PGAudit:
> 
>     https://www.pgaudit.org/
> 
> 
>     -- 
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (12+ messages)

Message-ID: <d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com>
Permalink:  ../d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com/
Also on:    postgresql.org/message-id/d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, miles.elam@productops.com
  Subject: Re: Inserts restricted to a trigger
  In-Reply-To: <d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com>
  References: <CAALojA-Mn+V+j9gdS2-jyoCzAGrjwGQh2hJq0H1it5M4=UaHBw@mail.gmail.com> <6fe78cce-7e74-926b-7d3d-2e09da698cc8@aklaver.com> <20190618004728.ph5aeg4wlucll7y5@raf.org> <CAALojA82fH0Nbb+qKRPJpx_mcdZ1Dh3xQrBk3hTQBbQ9kbcWtA@mail.gmail.com> <CAKkG4_mu=--NFCJO1R7n5DfXPX7Y+RcxFPha7d7e9ppsebncXg@mail.gmail.com> <CAALojA_ODo7GP_zq6y5s2WBwjbCwMT9AMR0omSQngZur9Bzkxg@mail.gmail.com> <b5b505ba-e9e6-98ee-a2b7-57d5d51bae09@aklaver.com> <CAALojA-gg3yRt61hcUKzU67ns0o+z-Fg=46gTRkQ6E_Vnqf+yg@mail.gmail.com> <48ddb8b0-2628-9b9e-84c4-955f5c15ddd1@aklaver.com> <CAALojA-OK6wK13kLJw3LM5a0oy92uXyj=jftC7RrO+AAqn+L-A@mail.gmail.com> <d6310f2c-1b98-af72-dba6-52e77d329f46@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox