pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Karl Czajkowski <karlcz@isi.edu>
To: pgsql-general@postgresql.org
Subject: Re: RLS policy dump/restore failure due to elided type-casts
Date: Wed, 20 Apr 2016 17:35:56 -0700
Message-ID: <5718206C.5070101@aklaver.com> (raw)
In-Reply-To: <20160421001832.GB7976@moraine.isi.edu>
References: <20160421001832.GB7976@moraine.isi.edu>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

On 04/20/2016 05:18 PM, Karl Czajkowski wrote:
> Our row level policies require very explicit casts in order to be
> accepted by the DB, but those casts are discarded in the dumped policy
> statements. Thus, an attempt to reload the dump file fails.  I'm not
> sure if the underlying problem is that the cast shouldn't be required
> in the first place, or if the normalization applied to the policy
> expression is just incorrect.
>
> Below is a trivialized example that shows the problem while removing
> complexity found in our real policies.  We are doing this to implement
> differentiated policies in a web application, where the web server
> stores authenticated web client context into session parameters and
> our policies check against those attributes rather than DB roles.
>
> To work around this, we have to maintain our policies in a separate
> SQL file, manually edit the dumps, and reapply our working policies.
> This is obviously undesirable in the long run, where dumps taken as
> periodic backups are not directly usable to reconstruct the DB...
>
>
> Karl
>
>
> Create a test database with these contents:
>
>    CREATE FUNCTION current_attributes() RETURNS text[]
>    STABLE AS $$
>    BEGIN
>      RETURN (
>        SELECT array_agg(value)
>        FROM json_array_elements_text(current_setting('ourapp.attributes')::json)
>      );
>    EXCEPTION WHEN OTHERS THEN
>      RETURN NULL::text[];
>    END;
>    $$ LANGUAGE plpgsql;
>
>    CREATE TABLE stuff (
>      value text PRIMARY KEY
>    );
>
>    CREATE POLICY delete_stuff ON stuff
>    FOR DELETE USING ('example attribute value' = ANY ( ((SELECT current_attributes()))::text[] ));


Trying to figure out how this works in the case above. Looks to me like 
you are comparing a text value to a text[].

Also why the the cast to text[], when the return value from the function 
is text[]?

>
>
> The output of pg_dump (and similarly the '\d' command in psql) drops
> the cast:
>
>    CREATE POLICY delete_stuff ON stuff
>    FOR DELETE TO PUBLIC USING (('example attribute value'::text = ANY (( SELECT current_attributes() AS current_attributes))));
>
>
> And this causes an error when executing the dump file on a new database:
>
>    ERROR:  operator does not exist: text = text[]
>    HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
>
>
>
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (9+ messages)  latest in thread

Message-ID: <5718206C.5070101@aklaver.com>
Permalink:  ../5718206C.5070101@aklaver.com/
Also on:    postgresql.org/message-id/5718206C.5070101@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, karlcz@isi.edu
  Subject: Re: RLS policy dump/restore failure due to elided type-casts
  In-Reply-To: <5718206C.5070101@aklaver.com>
  References: <20160421001832.GB7976@moraine.isi.edu> <5718206C.5070101@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