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 1hdPGx-0005Iz-Ts for pgsql-general@arkaria.postgresql.org; Wed, 19 Jun 2019 01:20:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hdPGv-00034P-JC for pgsql-general@arkaria.postgresql.org; Wed, 19 Jun 2019 01:20:21 +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 1hdPGv-0002x7-3g for pgsql-general@lists.postgresql.org; Wed, 19 Jun 2019 01:20:21 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdPGn-0002sG-VY for pgsql-general@lists.postgresql.org; Wed, 19 Jun 2019 01:20:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 41BF922404; Tue, 18 Jun 2019 21:20:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 18 Jun 2019 21:20:13 -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=fm3; bh=U ++eRvHyLcDwhMZha5DahqjSFqHU0pKTTz8X8zoZWZ4=; b=PlJPMOLMpiUMaRAyj KUFLTSxNydH66f3z9QC5zGzFlqnvXQ8CnYwseazb6JOlq6ibh278CKFF1/+UGA7q tD5uZ4i7kMoTKLGzcOc7J0KZDfA/YnMm4C8mZ1NGv1pLyoB/FHjGmJDW2+6TGHU8 ypAdWw9VyWM0Bxv2Uei6deu8juREgLkZ/RgSry8KIfkPl2MyEt8TMf6m7fM4TrEv 7xy3PUS3FmU/OzMMd8kRfarBGLkEgXi/NU64n2ydPM2FPznkRUBqoNh6xcW55NC+ kMyOEDePLfm1C6WtF2Aqjpw5jIHPNQlws8yPfrrsVBdpOReuWp8Reha7+D6gyLGl EDkCw== 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=fm3; bh=U++eRvHyLcDwhMZha5DahqjSFqHU0pKTTz8X8zoZW Z4=; b=wKRr1DU/O4yv6MbzPyqYqoF4bnJRP5yzCYlevUaajErNm2DvAN5jtSJPx +cEjuQxi9lpBZ0LXPnY/xbOODAaLAuF63q2w/6f6NrpveQsqxjFAngXEeg+w/BSO hwE8unikdVXQMa9d0BiSeK66Vs05JhJxd3Svx65TuEay3zfyFMpSkFN3FPfHS3wQ FULOnua2nLsEaPPkYXXrRyPSjKfpjjmjhJoPaJishcCcRjSqlsrUd1u4Ine6NYEM ge2+g0MVrE71Wq6B7Mvf7VTMoY88gslWRyJH4lao6LI5A+OEyqhnx+OMWQMYt4oZ 8JZBb4YCY8JIC79Z+exSpLAssv1HQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrtddugdegfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 4FD6380061; Tue, 18 Jun 2019 21:20:12 -0400 (EDT) Subject: Re: Inserts restricted to a trigger To: Miles Elam , pgsql-general References: <6fe78cce-7e74-926b-7d3d-2e09da698cc8@aklaver.com> <20190618004728.ph5aeg4wlucll7y5@raf.org> From: Adrian Klaver Message-ID: Date: Tue, 18 Jun 2019 18:20:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 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 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 > 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 > > 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 > 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 > > -- Adrian Klaver adrian.klaver@aklaver.com