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 1nqpeS-0005KQ-OK for pgsql-general@arkaria.postgresql.org; Tue, 17 May 2022 05:21:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nqpeR-0004vi-FF for pgsql-general@arkaria.postgresql.org; Tue, 17 May 2022 05:21:43 +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 1nqpeQ-0004vY-Rh for pgsql-general@lists.postgresql.org; Tue, 17 May 2022 05:21:43 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nqpeN-0006wB-T2 for pgsql-general@lists.postgresql.org; Tue, 17 May 2022 05:21:41 +0000 Received: by mail-pf1-x433.google.com with SMTP id p8so15912255pfh.8 for ; Mon, 16 May 2022 22:21:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=dKNs+9pLL50A9lhm60H9mEwp2DP4vuOcNacK5SE/wVE=; b=dWvvQ2Q0K0GGaoQ2jF4y5tDz1xdzAA4s9UCiOD5sq7u80FmMsMmVAZus5ab3gNftCi I5cLvUxj/yyxnk6KhCZVwZubRzspI3gyp4pUVlGTDaoJsyMKKUsrecOfR7elh5SXZYZ1 tEarNpY5Mj+/ON/7Cqkkg0nD34GDTQC2ysHs8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=dKNs+9pLL50A9lhm60H9mEwp2DP4vuOcNacK5SE/wVE=; b=OKmmhZXGB3QdajURpx11QsixjPG3cUgC0WgAvWX8IF/MM8QJIYlshAFXOPBzG5AgT2 jGGIm2Bp3Jo15vXP9BvUpG4KFMzpydoVoZ0SRJP8NLlC2DPEDga32HBzCCGl1zpF05zN UsMKghn18ohM6nXkyDkTrX3jV3jd83wrMDKHQkokXt6PrwDBWMThV9vDYd8OkGW3QHKq xcAY1MyYyfSe50tPbuXSG7ZC2DCqf2KYvg/WqTU1FgZzbNRakOLlqOAQs3uLC7ozvR5Y HW6sCprlEA4Z4qoPX4PZ5dXZXZOPtI3TzqF6WOKmx/xxX/eK74dmKSU1dj8MqLBm2ok1 DP9A== X-Gm-Message-State: AOAM532R7lzNFC536MEzdQ9iDNV+Gpe1oLkU15XaUqSMv/Zlpmg0AxNj mGPk15JiyDFa4yiLMtFFsGdRpg== X-Google-Smtp-Source: ABdhPJyWmTlo8SIko7OmCymkDbMktbIEXONIRjx3W06P4l4HjSTTh4/ty1uiS269DxSaHhgdo9Iy9A== X-Received: by 2002:a05:6a00:198f:b0:50e:7e6:6d5c with SMTP id d15-20020a056a00198f00b0050e07e66d5cmr20588371pfl.20.1652764898453; Mon, 16 May 2022 22:21:38 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:c1a5:6d20:fbe4:b33b]) by smtp.gmail.com with ESMTPSA id n18-20020a170903111200b0015e8d4eb25csm8021543plh.166.2022.05.16.22.21.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 May 2022 22:21:37 -0700 (PDT) From: Bryn Llewellyn Message-Id: <766C91F2-6FF8-4D4B-94BB-39B3BAD2AFE8@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_8DF98991-7EBD-4CA6-A404-07D71676BA97" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Restricting user to see schema structure Date: Mon, 16 May 2022 22:21:36 -0700 In-Reply-To: Cc: Neeraj M R , Tom Lane PostgreSQL , pgsql-general list To: Adrian Klaver , "David G. Johnston" References: <048ab2c5-ddf6-d039-3c2a-1828d8c9137b@aklaver.com> <569304.1652411286@sss.pgh.pa.us> <228E8A44-A504-4E0D-99BD-71B41812FFE2@yugabyte.com> <557fd2b6-dd2e-66ad-4920-25d173eaf10e@aklaver.com> <84B060E2-4393-4D49-AB4C-BAFCA94620F1@yugabyte.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_8DF98991-7EBD-4CA6-A404-07D71676BA97 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > adrian.klaver@aklaver.com wrote: >=20 >> bryn@yugabyte.com wrote: >>=20 >>> adrian.klaver@aklaver.com wrote: >>>=20 >>>> bryn@yugabyte.com wrote: >>>>=20 >>>>> neerajmr12219@gmail.com wrote: >>>>>=20 >>>>> ... >>>>=20 >>>> What exactly do you mean by "have created a new user and granted = connection access to database"? >>>=20 >>> Besides the REVOKE CONNECT, it is also possible to prevent = connections to a given database by a particular user by using settings = in pg_hba.conf. >>=20 >>=20 >> Adrian, I have the "pg_hba.conf" unmodified that came with the "brew" = PG installation of PG Version 14.2 on my MacOS Big Sur laptop. It has = just six non-comment lines... [But thing don't work as I want.] >=20 > Because as mentioned previously you did not "revoke connect on = database postgres from public". Right, I see the importance of this now. I now realize that when a = database is created, CONNECT on it is automatically granted to PUBLIC. = But there's no mention of this (or what to read to learn that this is = the case) in the "pg_hba.conf" chapter. Nor does the section on the = CREATE DATABASE statement mention this. How is the neophyte supposed to = know about this behavior? Another thing that confused me was the significance of the lines for the = database "replication" in the "pg_hba.conf" that came with my = installation. Add to this the mutually exclusive keywords "REPLICATION" = and "NO REPLICATION" in the CREATE ROLE statement. ("These clauses = determine whether a role is a replication role.") So this seems to be a = distinct use of the word from how it's used in "pg_hba.conf" as the name = of a database (that might well not exist). Strangely, the CREATE ROLE = doc says that you don't need either of "REPLICATION" or "NO REPLICATION" = but it doesn't say what the default is. David Johnston wrote this in a separate thread: > I don't quite know how to address your random experimentation with = pg_hba.conf. None of the things you showed are surprising though - were = you expecting different? My reports of my random experimentation were the email equivalent of the = "think aloud" approach to usability testing. That paradigm has the = creators of a system observe a new user trying to get things done (using = any appropriate doc that's available). Sometimes, the user appears to be = trying things randomly. Then the creators ask "why did you do = that"=E2=80=94and they learn what faulty mental model the user has = formed. And then they try to find out how the new user came to acquire = that model. Often, the problem is that the doc (or the UI, when it's = meant to me self-evident) suffers from what Steven Pinker calls the = "curse of knowledge" in his book "The sense of style". Anyway, with my experimentation and with the clues that you two (Adrian = and David) have given me, I arrived that the following practice. It = seems to give me what I want=E2=80=94i.e. a regime where ordinary new = users that I create can operate without me needing to change the = "pg_hba.conf" file and where they can connect to the one-and-only = database that I intend and then perform exactly and only the tasks that = I intend=E2=80=94in other words a regime that honors the principle of = least privilege. (We've discussed the caveat that I can't prevent them = from reading all of the metadata across all databases earlier.) * I use this bare bones "pg_hba.conf" file. local all all = trust host all all 127.0.0.1/32 = trust host all all ::1/128 = trust * I say "\c postgres postgres" and use a script to strip the cluster = done to its bare minimum=E2=80=94in my case: the users "Bllewell" and = postgres; and the databases postgres, template0, and template1. I say "revoke connect on database postgres from public" and "drop schema = if exists public". (And I drop any other schemas that might have been = created in the database postgres). * When I create a database, I immediately drop its public schema and = revoke connect on it from public. * When I create a user, I say NOREPLICATION and grant it CONNECT on just = the one database (it's always one) that I intend. However, when I create = a superuser, I cannot prevent it from connecting to *any* database = (present or future). --Apple-Mail=_8DF98991-7EBD-4CA6-A404-07D71676BA97 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
adrian.klaver@aklaver.com wrote:

bryn@yugabyte.com = wrote:

adrian.klaver@aklaver.com wrote:

bryn@yugabyte.com <mailto:bryn@yugabyte.com> wrote:


What exactly do you mean by "have created a new user and = granted connection access to database"?

Besides the REVOKE CONNECT, = it is also possible to prevent connections to a given database by a = particular user by using settings in pg_hba.conf.

Adrian,= I have the "pg_hba.conf" unmodified that came with the "brew" PG = installation of PG Version 14.2 on my MacOS Big Sur laptop. It has = just six non-comment lines... [But thing don't work as I want.]

Because as mentioned previously you did not "revoke connect = on database postgres from public".

Right, I see the = importance of this now. I now realize that when a database is created, = CONNECT on it is automatically granted to PUBLIC. But there's no mention = of this (or what to read to learn that this is the case) in = the "pg_hba.conf" chapter. Nor does the section on the CREATE = DATABASE statement mention this. How is the neophyte supposed to know = about this behavior?

Another thing that confused me was the significance of the = lines for the database "replication" in the "pg_hba.conf" that came = with my installation. Add to this the mutually exclusive keywords = "REPLICATION" and "NO REPLICATION" in the CREATE ROLE statement. = ("These clauses determine whether a role is a replication role.") So = this seems to be a distinct use of the word from how it's used in = "pg_hba.conf" as the name of a database (that might well not exist). = Strangely, the CREATE ROLE doc says that you don't need either = of "REPLICATION" or "NO REPLICATION" but it doesn't say what the = default is.

David Johnston wrote this in a separate thread:

I don't quite know how to = address your random experimentation with pg_hba.conf.  None of the = things you showed are surprising though - were you expecting = different?

My reports of my random experimentation were the email = equivalent of the "think aloud" approach to usability testing. That = paradigm has the creators of a system observe a new user trying to get = things done (using any appropriate doc that's available). Sometimes, the = user appears to be trying things randomly. Then the creators ask "why = did you do that"=E2=80=94and they learn what faulty mental model the = user has formed. And then they try to find out how the new user came to = acquire that model. Often, the problem is that the doc (or the UI, when = it's meant to me self-evident) suffers from what Steven Pinker calls the = "curse of knowledge" in his book "The sense of style".

Anyway, with my = experimentation and with the clues that you two (Adrian and David) have = given me, I arrived that the following practice. It seems to give me = what I want=E2=80=94i.e. a regime where ordinary new users that I create = can operate without me needing to change the "pg_hba.conf" file and = where they can connect to the one-and-only database that I intend and = then perform exactly and only the tasks that I intend=E2=80=94in other = words a regime that honors the principle of least privilege. (We've = discussed the caveat that I can't prevent them from reading all of the = metadata across all databases earlier.)

* I use this bare = bones "pg_hba.conf" file.

     local   all   =           all       =                     =           trust
  =    host    all         =     all           =   127.0.0.1/32          =   trust
     host  =   all           =   all           =   ::1/128             =     trust

* I say "\c postgres postgres" and use = a script to strip the cluster done to its bare minimum=E2=80=94in my = case: the users "Bllewell" and postgres; and the databases postgres, = template0, and template1.

I say "revoke connect on database = postgres from public" and "drop schema if exists public". (And I drop = any other schemas that might have been created in the database = postgres).

* = When I create a database, I immediately drop its public schema and = revoke connect on it from public.

* When I create a user, I say = NOREPLICATION and grant it CONNECT on just the one database (it's always = one) that I intend. However, when I create a superuser, I cannot prevent = it from connecting to *any* database (present or future).

= --Apple-Mail=_8DF98991-7EBD-4CA6-A404-07D71676BA97--