Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdGVA-0000Gn-LE for pgsql-general@arkaria.postgresql.org; Mon, 25 May 2020 17:03:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdGV8-0003Ce-D1 for pgsql-general@arkaria.postgresql.org; Mon, 25 May 2020 17:02:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdGV8-00037Y-0v for pgsql-general@lists.postgresql.org; Mon, 25 May 2020 17:02:58 +0000 Received: from gandalf.net-wizard.org ([2a01:4f8:172:2a5c::5]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdGV4-0001Yp-9j for pgsql-general@postgresql.org; Mon, 25 May 2020 17:02:56 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id A650082083; Mon, 25 May 2020 19:02:49 +0200 (CEST) X-Virus-Scanned: Gentoo amavisd-new at aragorn Received: from aragorn.net-wizard.org ([127.0.0.1]) by localhost (aragorn.net-wizard.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id j8FQleMReYeC; Mon, 25 May 2020 19:02:49 +0200 (CEST) Received: from webmail.net-wizard.org (localhost [IPv6:::1]) by gandalf.net-wizard.org (Postfix) with ESMTP id 749E680F47; Mon, 25 May 2020 19:02:49 +0200 (CEST) MIME-Version: 1.0 Date: Mon, 25 May 2020 19:02:49 +0200 From: Charles Clavadetscher To: Laurenz Albe Cc: Ted Toth , pgsql-general Subject: Re: FDW and RLS In-Reply-To: References: User-Agent: Roundcube Webmail/1.4.3 Message-ID: <62c5b2b1bc707e036b278ca41829aefa@swisspug.org> X-Sender: clavadetscher@swisspug.org Organization: Swiss PostgreSQL Users Group Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello On 2020-05-25 15:50, Laurenz Albe wrote: > On Fri, 2020-05-22 at 08:02 -0500, Ted Toth wrote: >> Will RLS be applied to data being retrieved via a FDW? > > ALTER FOREIGN TABLE rp_2019 ENABLE ROW LEVEL SECURITY; > ERROR: "rp_2019" is not a table > > Doesn't look good. > > Yours, > Laurenz Albe Actually it does work if you set the policy on the source table and access it using the user defined in the user mappings on the foreign table on the remote server. Server 1: charles@kofdb.archivedb.5432=# \d public.test_fdw_rls Table "public.test_fdw_rls" Column | Type | Collation | Nullable | Default ----------+---------+-----------+----------+--------- id | integer | | | content | text | | | username | text | | | Policies: POLICY "kofadmin_select" FOR SELECT TO kofadmin USING ((username = ("current_user"())::text)) kofadmin@kofdb.archivedb.5432=> \dp public.test_fdw_rls Access privileges Schema | Name | Type | Access privileges | Column privileges | Policies --------+--------------+-------+-------------------------+-------------------+---------------------------------------------- public | test_fdw_rls | table | charles=arwdDxt/charles+| | kofadmin_select (r): + | | | kofadmin=arwd/charles | | (u): (username = ("current_user"())::text)+ | | | | | to: kofadmin charles@kofdb.archivedb.5432=# SELECT CURRENT_USER; SELECT * FROM public.test_fdw_rls; charles@kofdb.archivedb.5432=# SELECT CURRENT_USER; SELECT * FROM public.test_fdw_rls; current_user -------------- charles (1 row) id | content | username ----+----------------------------------+---------- 1 | Text for charles | charles 1 | Access from fdw via user fdwsync | fdwsync (2 rows) charles@kofdb.archivedb.5432=# set role fdwsync ; SET charles@kofdb.archivedb.5432=> SELECT CURRENT_USER; SELECT * FROM public.test_fdw_rls; current_user -------------- fdwsync (1 row) id | content | username ----+----------------------------------+---------- 1 | Access from fdw via user fdwsync | fdwsync (1 row) On the server accessing the table via FDW: kofadmin@kofdb.t-archivedb.5432=> \deu+ List of user mappings Server | User name | FDW options ------------+-----------+--------------------------------------------- kofdb_prod | kofadmin | (password 'mysecret', "user" 'fdwsync') kofadmin@kofdb.t-archivedb.5432=> SELECT CURRENT_USER; SELECT * FROM public.test_fdw_rls ; current_user -------------- kofadmin (1 row) id | content | username ----+----------------------------------+---------- 1 | Access from fdw via user fdwsync | fdwsync (1 row) Regards Charles