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 1osYlk-0004Lk-47 for pgsql-general@arkaria.postgresql.org; Wed, 09 Nov 2022 00:16:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1osYlj-00027o-2c for pgsql-general@arkaria.postgresql.org; Wed, 09 Nov 2022 00:16:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osYli-00027e-JO for pgsql-general@lists.postgresql.org; Wed, 09 Nov 2022 00:16:38 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1osYlg-0006Cv-9V for pgsql-general@lists.postgresql.org; Wed, 09 Nov 2022 00:16:37 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d10so15220180pfh.6 for ; Tue, 08 Nov 2022 16:16:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=LzPLQYtnJ9RMvVby87fPQ+lW3Pjky3V8sVqTF/HRZQU=; b=Ok1V4sitwKIWiAe3t6shFibt8lEYEmXzjV1lkgmCAe5/u3hViyMZOFi/rUT6sjNzD2 D1BeL+4ZuAyxWxC8xED7z+iWyhRDNilcecIEzEgjEvuonkrV0GpMaGBqjVRQjrw7HGIh HneYGXOWrL4myKull0Pt8dyW4vGNHc+rRyXRw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=LzPLQYtnJ9RMvVby87fPQ+lW3Pjky3V8sVqTF/HRZQU=; b=b3hdkzgGZY7VdZZ2Z1W5Eoi4Z9os5fu0rtjD3AVRdBX+FeyqjW2N8Spz5qh89Ogw6n tF1QFIo9TWtw/wh0q7TfOhOCUTc8cBZijBaRulw/L8K1vPOGq8f0/vQ7Ekap/ZKPspIY yITCqx6MOY835DbAi3zp4ZVm6EARiQVjFSS1DI59E6SiFYuZ3SRNvGVAordUllO5jseN 4hMNSBVwE2G0pIuzMCgPIXxVK+9361Ci5djlTUQ+o0xgnDFnocjDnqII47/hLXO4soBV LZlGhjB5yGcZGCHpT5KyzQuUaofz8dc25euxZRpNUJUHgW9HUi9Ua0/IGGtcOEzRkfHg cwew== X-Gm-Message-State: ACrzQf3ORgl/ayDU8ZfDMfaGyDkIEZdX3AtIOgpVjoogi5aiKWZgBOLM gSFzSyJdhbVjNhu2QcFSJBZ/D+Im6e1v1Q== X-Google-Smtp-Source: AMsMyM6mNnuCNqg+4I48nvp9dRULI8TjPk+MavBsC7Af+P5MJTlT9ziaHtMfF28p3K5pumgxc3nygQ== X-Received: by 2002:a63:ea04:0:b0:43a:b17f:cd12 with SMTP id c4-20020a63ea04000000b0043ab17fcd12mr49363914pgi.109.1667952993451; Tue, 08 Nov 2022 16:16:33 -0800 (PST) Received: from smtpclient.apple ([2601:646:101:7fda:740e:2ee5:4a08:ce8f]) by smtp.gmail.com with ESMTPSA id x35-20020a634863000000b0046b1dabf9a8sm6377261pgk.70.2022.11.08.16.16.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Nov 2022 16:16:32 -0800 (PST) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_254F9656-0265-4196-A2CD-CBF4BCA1F8E2" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: "set role" semantics Message-Id: Date: Tue, 8 Nov 2022 16:16:31 -0800 To: pgsql-general list 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=_254F9656-0265-4196-A2CD-CBF4BCA1F8E2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I created a little test to demonstrate to myself how =E2=80=9Cset = role=E2=80=9D works. I ran it in a freshly-created PG 11.17 cluster on = Ubuntu, installed and configured like I=E2=80=99ve recently discussed on = this list. I copied my "pg-init.sh" script at the end. I then did this test, after starting like this (as the system admin O/S = user for my VM): ---------------------- source pg-init.sh sudo -u postgres psql This is the SQL script: create role clstr$mgr with create role clstr$mgr with nosuperuser createrole createdb noreplication nobypassrls connection limit 0 login password null; set role clstr$mgr; create role d1$mgr nosuperuser nocreaterole nocreatedb noreplication nobypassrls connection limit 0 login password null; create role d2$mgr nosuperuser nocreaterole nocreatedb noreplication nobypassrls connection limit 0 login password null; create database d1; revoke all on database d1 from public; create database d2; revoke all on database d2 from public; \c d1 postgres set role clstr$mgr; grant create on database d1 to d1$mgr; create schema s; grant usage on schema s to d1$mgr; grant create on schema s to d1$mgr; set role d1$mgr; select current_database()||' > '||session_user||' > '||current_user; create table s.t(k int); insert into s.t(k) values(17); select * from s.t; set role d2$mgr; select current_database()||' > '||session_user||' > '||current_user; -- permission denied... select * from s.t; ---------------------- Notice that I didn't grant "connect" on either of the databases, "d1" or = "d2", to any of the roles, "clstr$mgr, "d1$mgr", or "d2$mgr". I couldn't find the doc that tells me what to expect. Where is it? I was = a bit surprised that I could end up with the "current_database()" as = "d1" or "d2" and the "current_user" as "clstr$mgr" when this role = doesn't have "connect" on either of the databases. But I guessed that = permission to do this was implied by the "createdb" attribute (as a = special case of the general unstoppability of a superuser). However, I = was very surprised that I could end up with the "current_database()" as = "d1" or "d2" and the "current_user" as "d2$mgr" or because it is so far = minimally privileged (and in particular doesn't have "connect" on "d1" = or "d2"). I'd been hoping that "set role d2$mgr" would fail when "d2$mgr" doesn't = have "connect" on the target database, "d1". My plan, then, had been to = set up "d1$mgr" as the manager for "d1" by granting it "connect on "d1" = but not on "d2". Then I'd've used a similar scheme for "d2$mgr". Is there anything that can be done to limit the scope of the ability to = end up in a database like I'd thought would be possible? (A little test = showed me that "set role" doesn't fire an event trigger.) I do see that, as far as I've taken this test, "d2$mgr" is entirely = impotent when the "current_database()" is "d1". Is this, maybe, just as = good as it gets. I suppose I can live with what seems to me to be very = odd as long as no harm can come of it. ---------------------- # pg-init.sh sudo pg_dropcluster --stop 11 main sudo rm -Rf /etc/ybmt-generated/pg-logs/* sudo pg_createcluster 11 main \ -e UTF8 --locale=3DC --lc-collate=3DC --lc-ctype=3Den_US.UTF-8 \ -d /var/lib/postgresql/11/main \ > /dev/null sudo cp /etc/ybmt-code/pg-and-yb-config-files/*.conf \ /etc/postgresql/11/main sudo chown postgres /etc/postgresql/11/main/*.conf sudo chgrp postgres /etc/postgresql/11/main/*.conf sudo chmod 644 /etc/postgresql/11/main/*.conf sudo chmod 640 /etc/postgresql/11/main/pg_hba.conf sudo chmod 640 /etc/postgresql/11/main/pg_ident.conf sudo pg_ctlcluster start 11/main sudo -u postgres psql -c " alter role postgres with superuser connection = limit -1 login password 'x'; alter database postgres with = allow_connections =3D true connection_limit =3D -1; " sudo -u postgres psql -c " select name, setting from pg_settings where = category =3D 'File Locations'; " =E3=80=82 --Apple-Mail=_254F9656-0265-4196-A2CD-CBF4BCA1F8E2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
I created a little test to = demonstrate to myself how =E2=80=9Cset role=E2=80=9D works. I ran it in = a freshly-created PG 11.17 cluster on Ubuntu, installed and configured = like I=E2=80=99ve recently discussed on this list. I copied my = "pg-init.sh" script at the end.

I then did this test, = after starting like this (as the system admin O/S user for my = VM):

----------------------

source pg-init.sh
sudo -u = postgres psql

This is the SQL = script:

create role clstr$mgr with
create role clstr$mgr with
  nosuperuser
  createrole  createdb
  noreplication
  nobypassrls
  connection = limit 0
  login password null;

set role clstr$mgr;

create role = d1$mgr
  nosuperuser
  nocreaterole
  nocreatedb
  noreplication
  nobypassrls
  connection = limit 0
  login password null;

create role d2$mgr
  nosuperuser
  nocreaterole
  nocreatedb
  noreplication
  nobypassrls
  connection = limit 0
  login password null;

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

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

\c d1 postgres

set role = clstr$mgr;
grant create on database d1 to d1$mgr;
create schema s;
grant usage  on = schema s to d1$mgr;
grant create on schema s to d1$mgr;

set role d1$mgr;
select = current_database()||' > '||session_user||' > '||current_user;
create table s.t(k int);
insert into s.t(k) = values(17);
select * from s.t;

set role d2$mgr;
select current_database()||' = > '||session_user||' > '||current_user;

-- permission denied...
select * from = s.t;

----------------------

Notice that I didn't grant = "connect" on either of the databases, "d1" or "d2", to any of the roles, = "clstr$mgr, "d1$mgr", or = "d2$mgr".

I couldn't find the doc that tells me what to expect. Where = is it? I was a bit surprised that I could end up with the = "current_database()" as "d1" or "d2" and the "current_user" as = "clstr$mgr" when this role doesn't have "connect" on either of the = databases. But I guessed that permission to do this was implied by the = "createdb" attribute (as a special case of the general unstoppability of = a superuser). However, I was very surprised that I could end up = with the "current_database()" as "d1" or "d2" and the = "current_user" as "d2$mgr" or because it is so far minimally = privileged (and in particular doesn't have "connect" on "d1" or = "d2").

I'd been hoping that "set role d2$mgr" would fail when = "d2$mgr" doesn't have "connect" on the target database, "d1". My plan, = then, had been to set up "d1$mgr" as the manager for "d1"  by = granting it "connect on "d1" but not on "d2". Then I'd've used a similar = scheme for "d2$mgr".

Is there anything that can = be done to limit the scope of the ability to end up in a database like = I'd thought would be possible? (A little test showed me that "set role" = doesn't fire an event trigger.)

I do see that, as far as = I've taken this test, "d2$mgr" is entirely impotent when the = "current_database()" is "d1". Is this, maybe, just as = good as it gets. I suppose I can live with what seems to me to be very = odd as long as no harm can come of it.

----------------------
# = pg-init.sh

sudo= pg_dropcluster --stop 11 main
sudo rm -Rf = /etc/ybmt-generated/pg-logs/*
sudo pg_createcluster 11 main \
  -e UTF8 --locale=3DC --lc-collate=3DC = --lc-ctype=3Den_US.UTF-8 \
  -d = /var/lib/postgresql/11/main \
  > = /dev/null

sudo= cp /etc/ybmt-code/pg-and-yb-config-files/*.conf \
                =     /etc/postgresql/11/main
sudo chown postgres = /etc/postgresql/11/main/*.conf
sudo chgrp postgres = /etc/postgresql/11/main/*.conf
sudo chmod 644  =     /etc/postgresql/11/main/*.conf
sudo= chmod 640      = /etc/postgresql/11/main/pg_hba.conf
sudo chmod 640  =     /etc/postgresql/11/main/pg_ident.conf

sudo pg_ctlcluster = start 11/main

sudo= -u postgres psql -c " alter role postgres with superuser connection = limit -1 login password 'x'; alter database postgres with = allow_connections =3D true connection_limit =3D -1; "

sudo -u postgres psql = -c " select name, setting from pg_settings where category =3D 'File = Locations'; "
=E3=80=82

= --Apple-Mail=_254F9656-0265-4196-A2CD-CBF4BCA1F8E2--