Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aEKhG-00046d-Tj for pgsql-sql@arkaria.postgresql.org; Wed, 30 Dec 2015 17:38:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aEKhG-0001vD-G3 for pgsql-sql@arkaria.postgresql.org; Wed, 30 Dec 2015 17:38:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aEKhE-0001mJ-Qt for pgsql-sql@postgresql.org; Wed, 30 Dec 2015 17:38:00 +0000 Received: from mail-wm0-x231.google.com ([2a00:1450:400c:c09::231]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aEKhC-0006WO-35 for pgsql-sql@postgresql.org; Wed, 30 Dec 2015 17:37:59 +0000 Received: by mail-wm0-x231.google.com with SMTP id b14so57114891wmb.1 for ; Wed, 30 Dec 2015 09:37:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=UbZ93gV4r3aI08kVcQvmr+cMdKrYd7cCaHLpJIjRD/w=; b=iarUeM98wR+Ksb0iIBXaAgN5g0SYOpV5wGfNzLZVqz/j+xyNUcfI1+/X63B23O9yWJ q+nnreYaePlABuRsvc7UKGb7sObKVbc5gF7zS2z//2vYdBQF2aYlKgwrkmnbvzcD9dCn VXzNFWQA+5cJzUFj0wr0gqIT97zch1RZmJ/NeTGUWfEEDXm0vrBpLEDmDz7u0A2vmDQe MSO33tTAwXlDw0TBgKo2LJXAxjBgPhGq1Pw1lIOrl12w+lH8mxZD48k5FoDZm22hIXC+ X+TQibX0XlsTJAd67if10kJvQ4CPJ03mX7ie+AIhvXOCVNvdXa98aN175oBAyw3P+OAT NKZA== X-Received: by 10.28.131.70 with SMTP id f67mr17194732wmd.66.1451497076046; Wed, 30 Dec 2015 09:37:56 -0800 (PST) Received: from timbomac.home (host86-147-73-225.range86-147.btcentralplus.com. [86.147.73.225]) by smtp.googlemail.com with ESMTPSA id 67sm12096419wmp.20.2015.12.30.09.37.55 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 30 Dec 2015 09:37:55 -0800 (PST) Subject: Re: question on row level security To: Joe Conway , pgsql-sql@postgresql.org References: <56840D1A.8030203@gmail.com> <56841541.6080409@joeconway.com> From: Tim Dudgeon Message-ID: <56841672.4090201@gmail.com> Date: Wed, 30 Dec 2015 17:37:54 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 In-Reply-To: <56841541.6080409@joeconway.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 30/12/2015 17:32, Joe Conway wrote: > On 12/30/2015 08:58 AM, Tim Dudgeon wrote: >> e.g. conceptually: >> >> set app_user 'john'; >> select * from foo; >> >> where the select * is restricted by a RLS check that includes 'john' as >> the app_user. >> Of course custom SQL could be generated for this, but it would be safer >> if it could be handled using RLS. >> >> Any ways to do this? > Something like this: > > 8<-------------------------- > CREATE USER application; > > CREATE TABLE t1 (id int primary key, f1 text, app_user text); > INSERT INTO t1 VALUES(1,'a','bob'); > INSERT INTO t1 VALUES(2,'b','alice'); > ALTER TABLE t1 ENABLE ROW LEVEL SECURITY; > CREATE POLICY P ON t1 USING (app_user = > current_setting('app_name.app_user')); > GRANT SELECT ON t1 TO application; > > SET SESSION AUTHORIZATION application; > > regression=> SET app_name.app_user = 'bob'; > SET > regression=> SELECT * FROM t1; > id | f1 | app_user > ----+----+---------- > 1 | a | bob > (1 row) > > regression=> SET app_name.app_user = 'alice'; > SET > regression=> SELECT * FROM t1; > id | f1 | app_user > ----+----+---------- > 2 | b | alice > (1 row) > > regression=> SET app_name.app_user = 'none'; > SET > regression=> SELECT * FROM t1; > id | f1 | app_user > ----+----+---------- > (0 rows) > > 8<-------------------------- > > HTH, > > Joe > Looks like that's what I need. I'll give it a try. Thanks Tim -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql