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 1p5uGA-0000Qr-5f for pgsql-general@arkaria.postgresql.org; Thu, 15 Dec 2022 19:51:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p5uG8-0006G7-W4 for pgsql-general@arkaria.postgresql.org; Thu, 15 Dec 2022 19:51:12 +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 1p5uG8-0006C3-Fg for pgsql-general@lists.postgresql.org; Thu, 15 Dec 2022 19:51:12 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p5uG5-00048m-Ti for pgsql-general@lists.postgresql.org; Thu, 15 Dec 2022 19:51:11 +0000 Received: by mail-pl1-x629.google.com with SMTP id l10so72640plb.8 for ; Thu, 15 Dec 2022 11:51:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=0VTnBVgpGTWedONCCdvTtryNmyRvK5TfPdtoSpBRo+4=; b=enjx104jJNCa8BVjEg4ngbAoYI9nQfwfxNggwYVFOzTK0eJ9PxOWXm9ez2vPYpbVZz SO8GUtSXpuMtqngCpRvKalcGMZmtlM8LjJQESOhQ0K50xQFjppvDuEh9fgNit7S3/Ntz O3Few+CgwvkPz9mOmb9eXYzgZtLPxK3LJ3yZY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0VTnBVgpGTWedONCCdvTtryNmyRvK5TfPdtoSpBRo+4=; b=vyGujLQ26ErS63AoSxcrFrBklCLIIno1tAz03/Xpvq+sig/n48OoB2/PhlABGu2gPy 1ZAhD8h2rQ0twN5R44cXpnJrNhORUHtkGSajmXzcIjCVcHdCHBYusDFkRZAgVeeIM+ZJ 0Cu2SK5JLT4R+58SajxuIYVlnGHi6XwrqEypPuNm9CGuIhEQyCjN5HBDSRBa3ISCy7lh NlQ/0PxfktikisUUSX1naC6gE6fkFInLlbhDrCf3z4towmV5dMuNOaNVqAMNqisR/kGW 72mR6uq3yYAHzhfG2FDpvr6H+3Sd2nDI5YjDquhhCDvDfSh/VjbJQziivtQfIa6nJOgH xCGw== X-Gm-Message-State: ANoB5pnwjiQ5TEM8W/Ppb6wgo4P9asq12kkN5Tg7i0PYuRICVSlFJGqk qzJIichaIrsSklADbBZIHRrQnA== X-Google-Smtp-Source: AA0mqf6ZM3C2upSwd03VfFkzgYwhPhpTrxR4RKROhSEVhYhI9RLKjjNvS2yhvy1YnTpTUsx6Gd90Yg== X-Received: by 2002:a05:6a21:1088:b0:a7:8b3e:1207 with SMTP id nl8-20020a056a21108800b000a78b3e1207mr30971892pzb.13.1671133868480; Thu, 15 Dec 2022 11:51:08 -0800 (PST) Received: from smtpclient.apple ([2601:646:101:7fda:4434:a9b2:8cb1:c3d2]) by smtp.gmail.com with ESMTPSA id e11-20020a17090a728b00b0020d48bc6661sm3509248pjg.31.2022.12.15.11.51.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Dec 2022 11:51:08 -0800 (PST) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_DFD3A095-D491-4015-9EB1-AD2392F13009" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Test if a database has any privilege granted to public Date: Thu, 15 Dec 2022 11:51:07 -0800 In-Reply-To: <3300816.1671084655@sss.pgh.pa.us> Cc: Ron , pgsql-general list To: Tom Lane PostgreSQL References: <0BFC452B-0AC8-46B3-9E65-B685544ED110@yugabyte.com> <3300816.1671084655@sss.pgh.pa.us> 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=_DFD3A095-D491-4015-9EB1-AD2392F13009 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > tgl@sss.pgh.pa.us wrote: >=20 >> ronljohnsonjr@gmail.com writes: >>=20 >> Off-topic, but you don't need all those text casts. >=20 > Indeed. Something like this ought to do it: >=20 > select datname from pg_database where 0::oid =3D any(select = (aclexplode(datacl)).grantee); >=20 > datname =20 > ------------ > template1 > template0 > regression Thanks, both, for the lightning-fast replies. Yes, I see it now. (I got = myself confused about the requirements for using parentheses.) I should = have slept on it before sending to the list. There's still a little snag though. I created a brand-new cluster (with = bootstrap superuser called "postgres"), started a session as "postgres", = and did this: create database d1; revoke all on database d1 from postgres; revoke all on database d1 from public; create database d2; revoke all on database d2 from postgres; create database d3; select datname::text as name, case when datacl is null then '' else datacl::text end as datacl, (0::oid =3D any(select (aclexplode(datacl)).grantee))::text as = "public has a priv" from pg_database where datname in ('d1', 'd2', 'd3') order by 1; It produced this result: name | datacl | public has a priv=20 ------+----------------+------------------- d1 | {} | false d2 | {=3DTc/postgres} | true d3 | | false This seems to imply that this wording from "5.7. Privileges" = (https://www.postgresql.org/docs/current/ddl-priv.html) is a little = sketchy: =C2=AB For other types of objects, the default privileges granted to PUBLIC are = as follows: CONNECT and TEMPORARY (create temporary tables) privileges = for databases=E2=80=A6 =C2=BB The effect of a NULL "datacl" is as if CONNECT and TEMPORARY have been = granted to public. But even so, these privileges are not shown to have = been actually granted. In my test, I simply revoked "all" on "d2" from postgres. And this = produced a not null "datacl" that did then show the documented default = regime. The following test: create role r with login password 'p'; \c d1 r \c d2 r \c d3 r Showed that "public has a priv" (as I coded it) doesn't tell the whole = story because "\c d3 r" (as well as "\c d2 r") succeeds. Of course, "\c = d1 r" fails. I do see that, in a strict "legal sense", the doc that I quoted is not = (quite) wrong. But to implement the test that I want robustly, I need to = extend the logic thus: select datname::text from pg_database where 0::oid =3D any(select (aclexplode(datacl)).grantee) or datacl is null; That's easy if you know that you need to write this. But the need to do = so seems to depend on pretty arcane knowledge that, as far as I can see, = isn't documented. Anyway, my immediate requirement is solved. Thanks again! --Apple-Mail=_DFD3A095-D491-4015-9EB1-AD2392F13009 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
tgl@sss.pgh.pa.us wrote:

ronljohnsonjr@gmail.com writes:

Off-topic, but you don't need all those text = casts.

Indeed.  Something like this ought to do it:

select datname from pg_database where 0::oid =3D= any(select (aclexplode(datacl)).grantee);

 datname   
------------
template1
template0
regression

Thanks, = both, for the lightning-fast replies. Yes, I see it now. (I got myself = confused about the requirements for using parentheses.) I should have = slept on it before sending to the list.

There's still a little snag though. I = created a brand-new cluster (with bootstrap superuser called = "postgres"), started a session as "postgres", and did this:

create database d1;
revoke= all on database d1 from postgres;
revoke all on database = d1 from public;

create database d2;
revoke all on database d2 from postgres;

create database d3;

select
  datname::text           =                     =                 as name,
  case
    when datacl is null = then '<NULL>'
    else     =                 datacl::text
  end               =                     =                     =   as datacl,
  (0::oid =3D any(select = (aclexplode(datacl)).grantee))::text   as "public has a priv"
from pg_database
where datname in ('d1', 'd2', = 'd3')
order by 1;

It produced this result:

 name |   =   datacl     | public has a priv 
------+----------------+-------------------
 d1   | {}       =       | false
 d2   | {=3DTc/postgres} | true
 d3   | <NULL>     =     | false

This seems to imply that this wording = from "5.7. Privileges" (https://www.postgresql.org/docs/current/ddl-priv.html) is = a little sketchy:

=C2=AB
For other types of objects, the = default privileges granted to PUBLIC are as = follows: CONNECT and TEMPORARY (create temporary = tables) privileges for databases=E2=80=A6
=C2=BB

The effect of  a NULL "datacl" is as if = CONNECT and TEMPORARY have been granted to public. But even = so, these privileges are not shown to have been actually = granted.

In my = test, I simply revoked "all" on "d2" from postgres. And this produced a = not null "datacl" that did then show the documented default = regime.

The = following test:

create role r = with login password 'p';
\c d1 r
\c d2 r
\c d3 r

Showed that "public has a priv" (as I = coded it) doesn't tell the whole story because "\c d3 r" (as well as "\c = d2 r") succeeds. Of course, "\c d1 r" fails.

I do see that, in a strict "legal = sense", the doc that I quoted is not (quite) wrong. But to implement the = test that I want robustly, I need to extend the logic thus:

select datname::text
from = pg_database
where 0::oid =3D any(select = (aclexplode(datacl)).grantee)
or datacl is null;

That's = easy if you know that you need to write this. But the need to do so = seems to depend on pretty arcane knowledge that, as far as I can see, = isn't documented.

Anyway, my immediate requirement is solved. Thanks = again!

= --Apple-Mail=_DFD3A095-D491-4015-9EB1-AD2392F13009--