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 1heSYi-0007pq-Si for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2019 23:03:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1heSYh-00053m-OJ for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2019 23:03:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1heSYg-00053e-4g for pgsql-general@lists.postgresql.org; Fri, 21 Jun 2019 23:03:03 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1heSYb-0001aG-O4 for pgsql-general@lists.postgresql.org; Fri, 21 Jun 2019 23:03:01 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 65C183C8; Fri, 21 Jun 2019 19:02:54 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 21 Jun 2019 19:02:54 -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=l +Qiks+sf+16lEGqr3i05t4tkOAYZxqQfTVmsmhntVk=; b=cuXGvmlfYX72z0ho/ h3Twwsp1KPZNq9ysm8LRqAJ+yCuGzeUUbECtqkkG6pA5lINMf0H1Uuk/92x4evJl Oj1g/e7rvIiUPzm6aHo1+0hiC7K4jJjfyon/Uw1DZOsEuu8m6N8Fb/4kq34GFTb3 o3ae/SVkspIEsOnidluf4Y5+yXOBcjumuqKr3VKilUGCsEATEtmFx9pQ9/BPh4s+ jZLj0sGMvCZzB4p4ixm/FZkdTP4BTqNYLTvKbZnC0IXSX9Hn7nlgv/gXeaEPtdu4 zHLYvaTdb2+TOqpt37pQcVt1BY0Ac7npf3k5eqxe35JeztnMyMA/cuPsk1yQEiym m7KTg== 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=l+Qiks+sf+16lEGqr3i05t4tkOAYZxqQfTVmsmhnt Vk=; b=AK5OIxqGiwwVACp8roj9c9re0s4wZhA+SolBcoKTRcFmZWEJZGsYLxWNG IiuGqbp73QOB5+uj02d5ECSDUvWE3B9H6VGQJso8N6isDi1MEmY45BNX7Z8AB0Ic gQRWZCs+gsC3tPhfrpXJGwCEeSExpJNrMivfQReEMZWJXzKXXJ2aXxnCSZUMiIJN U32Nu9koPKBavIXEkVjvqYT50cgMf7ho6q6sh1wBvWMUCbE2ShE8xNkaV8z7wDrn 0e9PwgX3Z/kFoMGNxn/BVKjumWxjmeia15SU5VZM6v0o60SeQ1wAIs2ETJ7NkP1i toWEV4Zy6NYKHfbIotf2YPB1DWnoA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrtdejgdduiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpphhgrghuughithdrohhr ghenucfkphepjeefrddvfeelrdduudejrddvfeeknecurfgrrhgrmhepmhgrihhlfhhroh hmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgv rhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 886E5380083; Fri, 21 Jun 2019 19:02:52 -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> <48ddb8b0-2628-9b9e-84c4-955f5c15ddd1@aklaver.com> From: Adrian Klaver Message-ID: Date: Fri, 21 Jun 2019 16:02:51 -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: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/20/19 3:30 PM, Miles Elam wrote: > Thanks for the reply, Adrian. > > 1. The audit tables (plural) are recording the historical data for a > table, ie., updates and deletes. All the same data as found in a given > table along with the role that performed the operation, the transaction > id, and the time range where this data was in active use. > > 2. Only thorough a web UI via an API service. > > 3. Should be limited to web app, but the data scientists may need direct > access in the near future. > > PGAudit does not cover our use case. We are making a temporal table > system since PostgreSQL does not support one natively. For example: > "What would this query have returned yesterday at 4:27pm PT?" Access is > as expected for inserts but updates and deletes are logged to history > tables. We cannot use 3rd party extensions because we are on AWS managed > databases. We are following the model detailed here > (https://wiki.postgresql.org/wiki/SQL2011Temporal) with some modifications. > > Given the model listed in the link, it's not clear how we can prevent > user tampering with history inserts. (History updates and deletes are > already REVOKE restricted.) Since we are going through an API server via > REST and/or GraphQL, the possibility is very unlikely, but we would > prefer a defense in depth approach in case an oversight somehow allowed > arbitrary query access to the database with the web user. For the most > part, we're fairly well locked down, but I just can't quite see how to > restrict aforementioned query access from inserting to the history in an > ad-hoc manner rather than the trigger-based predetermined insert pattern. Some draft ideas: 1) A session table that among other things records the Web Token/user combination. Then in the trigger(s) that INSERT into the history table check that the user has a valid current token. 2) In the inner function use PG_CONTEXT: https://www.postgresql.org/docs/11/plpgsql-control-structures.html#PLPGSQL-CALL-STACK to determine whether the inner SECURITY DEFINER function is being called directly or through the outer trigger function. > > > > On Thu, Jun 20, 2019 at 8:01 AM Adrian Klaver > wrote: > > On 6/19/19 3:07 PM, Miles Elam wrote: > > Hi Adrian, thanks for responding. > > > > How would I restrict access to the SECURITY DEFINER function? If > it can > > be called by the trigger, it can be called by the user as well I > would > > think. Same issue as access to the table itself only now with a > > superuser intermediary, right? > > > > Should have also mentioned, if you are not adverse to a third party > solution there is PGAudit: > > https://www.pgaudit.org/ > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com