Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMYWZ-0000wx-SR for pgsql-general@arkaria.postgresql.org; Fri, 03 May 2019 13:46:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hMYWW-0003BJ-5P for pgsql-general@arkaria.postgresql.org; Fri, 03 May 2019 13:46:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMYWV-0003BC-OM for pgsql-general@lists.postgresql.org; Fri, 03 May 2019 13:46:47 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMYWS-0002qh-Uq for pgsql-general@lists.postgresql.org; Fri, 03 May 2019 13:46:46 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 27EB72312E; Fri, 3 May 2019 09:46:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 03 May 2019 09:46:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=j CpDdqTjE6r30UeTlurfkzYW+CdceQgvxKzzpRvnfkU=; b=ZMHM4UPySRuh+INB/ mAT1+PuLD4oYZkRozcMUudDoRyAMQwNk21QnRwVKMuuNnQ8tjOaSmHGlI5sgcHU0 EKpmXloBAC7zy0y/BJr67VLxoTTznwEApQz1dfCaw6dSV4YufZsfKAAwP+fOudNy LcYVMZpeXqDfHu1WdI7S2aE5Xd6e6mub3pP/5zTEAdjnGVBpUQwOpiqp92EBfDBi PKx6iyDN4PJpYaqCLa7shMQLlNI5OcMg2RCjHSJbnTSElguZenysohQiAMtSTw6r edgJRSQyqNy+xyrF/dNKjBh6EqLcPz9GYLHENxhKxueCbZII0VHidjr4B8RJ6hdz uzvPA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=jCpDdqTjE6r30UeTlurfkzYW+CdceQgvxKzzpRvnf kU=; b=hL1YOOKVmr6f8ldphajk/7P7j62gQwjyVwfH8K3ilqH4fryF0bjqkW8yv 2dGTjN8MAOlCDnWVWpxUGwgPfod1Zdm1flulflJs2/vFg1vkUzoHX8kwGRJXnlUj L4l1+n0qDNUGBmBX/nrUi2V8E5Y4v8FQQpQJz5mUK682a3QW71OsRPSbbL7MS2d1 Pm4Wk2NQ9fPrTftQKKkQsA/KqPWsnIaFcB6jM6zQBgmS1L7JrmENitDvoDGZu43/ 7+qLaB6/iB2uiPTKAaxk55peb2gwYHQGTJ+zp4PvCKNcGIZKPGeQqSvKiTzySGI2 ku/eNUUJ6YGFXKIGiQEVBy7H823pA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrjedugdejvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddv rdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id A0F7EE4544; Fri, 3 May 2019 09:46:42 -0400 (EDT) Subject: Re: Update row attribute that is part of row level security policy using_expression To: Saupe Stefan , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: Date: Fri, 3 May 2019 06:46:41 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/3/19 4:47 AM, Saupe Stefan wrote: > I'd like to use RLS to 'hide' or 'deactivate' data at some point that > some rows are not visible to the application user anymore. > > Let's say user a owns the data and can see all his data. > > The application user 'b' can only select,update,delete... 'active' data, > but is also able to 'deactivate' currently 'active' rows. > > Below is how I tried to accomplish this. > > But I'm not able to 'deactivate' rows in the table as application user b. > > How can i accomplish this? If possible without having to change the > application sql’s that run against the table(s)? > > create user a with password 'a'; > > create user b with password 'b'; > > \c postgres a; > > create table t1(id int,active boolean); > > insert into t1 values(1,true); > > insert into t1 values(2,false); > > create policy mypolicy on t1 for all to b using (active); > > alter table t1 enable row level security; > > grant all on t1 to b; > > select * from t1; > > id | active > > ----+-------- > >   1 | t > >   2 | f > > (2 rows) > > --> OK > > --Now connect as the application user b > > \c postgres b; > > select * from t1; > > id | active > > ----+-------- > >   1 | t > > (1 row) > > --> OK > > --now I want to 'deactivate' the active row > > update t1 set active=false where id=1; > > ERROR:  new row violates row-level security policy for table "t1" > > --> I want to be able to do this. > > My question is: > > How can user b read just ‘active’ data AND be able to ‘deactivate’ some > active rows? The primary issue here is you are using a security policy to try to enforce something that is not security related, the visibility of data. If a user was locked out of data for security reasons, but had the ability to unlock that data on their own it would not be much of a security policy. I see two choices: 1) Don't use RLS for this. Just allow the user to toggle active as needed. Not sure where the user is viewing the data, but active/inactive could be part of the code that allows data through. 2) If you want to use RLS then create a SECURITY DEFINER function that runs as the user that does have non-RLS restricted access to the table. Have user b use that to change the active status. > > According to the docs > (https://www.postgresql.org/docs/current/sql-createpolicy.html) the > reason why the update fails is: > > The policy USING expression is applied to Existing & new rows on UPDATES > if read access is required to the existing or new row > -- Adrian Klaver adrian.klaver@aklaver.com