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: Thu, 20 Jun 2019 07:38:04 -0700
Message-ID: <fbb36b17-2905-127b-eb23-4885bfb73498@aklaver.com> (raw)
In-Reply-To: <CAALojA-gg3yRt61hcUKzU67ns0o+z-Fg=46gTRkQ6E_Vnqf+yg@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>
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?
We may need to back this up:
1) What is the audit table recording?
2) How much access do your users have to the database itself, outside
the Web app?
3) What access do users have in the Web app?
>
>
> On Tue, Jun 18, 2019 at 6:20 PM Adrian Klaver <adrian.klaver@aklaver.com
> <mailto:adrian.klaver@aklaver.com>> wrote:
>
> On 6/18/19 10:14 AM, Miles Elam wrote:
> > Thanks for the suggestion. Unfortunately we only have a single login
> > role (it's a web app) and then we SET ROLE according to the
> contents of
> > a JSON Web Token. So we end up with SESSION_USER as the logged in
> user
> > and the active role as CURRENT_USER.
>
> Have not tried it but nested function?:
>
> 1) Outer function runs as normal user and grabs the CURRENT_USER. This
> is passed into 2)
>
> 2) Audit function that runs with SECURITY DEFINER.
>
> Other option is to record the CURRENT_USER in the table the trigger is
> on and just pass that to the audit function.
>
> >
> > It may be that we're just stuck with a gap and need to just try
> and keep
> > track of our mutation points, such as limit what is accessible
> through
> > REST or GraphQL, and there is no way to fundamentally lock this
> down in
> > Postgres. I was checking the mailing list to see if I'd missed
> anything.
> >
> >
> > On Tue, Jun 18, 2019 at 9:47 AM Torsten Förtsch
> <tfoertsch123@gmail.com <mailto:tfoertsch123@gmail.com>
> > <mailto:tfoertsch123@gmail.com <mailto:tfoertsch123@gmail.com>>>
> wrote:
> >
> > Have you tried session_user?
> >
> > create function xx() returns table (cur text, sess text)
> > security definer language sql as $$
> > select current_user::text, session_user::text;
> > $$;
> >
> > Then log in as different user and:
> >
> > => select (xx()).*;
> > cur | sess
> > ----------+-------
> > postgres | write
> >
> >
> > On Tue, Jun 18, 2019 at 6:30 PM Miles Elam
> > <miles.elam@productops.com <mailto:miles.elam@productops.com>
> <mailto:miles.elam@productops.com
> <mailto:miles.elam@productops.com>>> wrote:
> >
> > That seems straightforward. Unfortunately I also want to know
> > the user/role that performed the operation. If I use SECURITY
> > DEFINER, I get the superuser account back from
> CURRENT_USER, not
> > the actual user.
> >
> > Sorry, should have included that in the original email.
> How do I
> > restrict access while still retaining info about the current
> > user/role?
> >
> >
> > On Mon, Jun 17, 2019 at 5:47 PM <raf@raf.org
> <mailto:raf@raf.org>
> > <mailto:raf@raf.org <mailto:raf@raf.org>>> wrote:
> >
> > Adrian Klaver wrote:
> >
> > > On 6/17/19 4:54 PM, Miles Elam wrote:
> > > > Is there are way to restrict direct access to a
> table
> > for inserts but
> > > > allow a trigger on another table to perform an
> insert
> > for that user?
> > > >
> > > > I'm trying to implement an audit table without
> allowing
> > user tampering
> > > > with the audit information.
> > >
> > > Would the below not work?:
> > > CREATE the table as superuser or other privileged user
> > > Have trigger function run as above user(use
> SECURITY DEFINER)
> >
> > and make sure not to give any other users
> insert/update/delete
> > permissions on the audit table.
> >
> > > > Thanks in advance,
> > > >
> > > > Miles Elam
> > >
> > > --
> > > Adrian Klaver
> > > adrian.klaver@aklaver.com
> <mailto:adrian.klaver@aklaver.com> <mailto:adrian.klaver@aklaver.com
> <mailto:adrian.klaver@aklaver.com>>
> >
> >
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: <fbb36b17-2905-127b-eb23-4885bfb73498@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> <fbb36b17-2905-127b-eb23-4885bfb73498@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