Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-11962: Update privileges for all database users on schema changes #3243

Merged
merged 2 commits into from
May 8, 2024

Conversation

mwiencek
Copy link
Member

@mwiencek mwiencek commented Apr 21, 2024

Problem

MBS-11962

When new tables or schemas are added, production database users like musicbrainz_ro may not be granted access to them.

Solution

Adds a new script, admin/UpdateDatabasePrivileges.pl, which is invoked from upgrade.sh and updates privileges for the following users if they exist:

  • musicbrainz_ro
  • caa_redirect
  • sir

The script works by revoking all write privileges from musicbrainz_ro, and then granting USAGE on all schemas, and SELECT privileges on all tables in those schemas.

Next, for the last two schemas, it revokes all privileges from them (including SELECT and USAGE), and then simply grants them membership in the musicbrainz_ro role.

Testing

I added the three roles in question to my local database and ran admin/UpdateDatabasePrivileges.pl. I observed that each of the three users could SELECT from any table in our schemas, but not modify them.

Copy link
Member

@reosarevok reosarevok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea seems great. Can we add a test for the script, please? :)

admin/UpdateDatabasePrivileges.pl Outdated Show resolved Hide resolved
admin/UpdateDatabasePrivileges.pl Outdated Show resolved Hide resolved
admin/InitDb.pl Show resolved Hide resolved
@reosarevok
Copy link
Member

Tests seem to be failing

@mwiencek
Copy link
Member Author

Tests seem to be failing

Seems to be because the WITH INHERIT clause I'm using was added in PostgreSQL 16.

@mwiencek mwiencek added this to the Schema Change 2024 Q2 milestone May 3, 2024
@mwiencek mwiencek changed the base branch from master to schema-change-2024-q2 May 3, 2024 15:20
Copy link
Member

@reosarevok reosarevok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMBDNT

Copy link
Contributor

@yvanzo yvanzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the roles musicbrainz_ro, caa_redirect and sir using admin/psql then ran the new script and obtained the following error message:

`LINE 3: WITH INHERIT TRUE;`
Failed query:
        'GRANT "musicbrainz_ro"
   TO "caa_redirect"
 WITH INHERIT TRUE;
'
        ()
42601 DBD::Pg::st execute failed: ERROR:  syntax error at or near "INHERIT"
LINE 3:  WITH INHERIT TRUE;
              ^ [for Statement "GRANT "musicbrainz_ro"
   TO "caa_redirect"
 WITH INHERIT TRUE;
"]
 at /musicbrainz-server/admin/../lib/Sql.pm line 123.
        Sql::catch {...} (MusicBrainz::Server::Exceptions::DatabaseError=HASH(0x5acb49453658)) called at /musicbrainz-server/perl_modules/lib/perl5/Try/Tiny.pm line 123
        Try::Tiny::try(CODE(0x5acb49453dd8), Try::Tiny::Catch=REF(0x5acb49353978)) called at /musicbrainz-server/admin/../lib/Sql.pm line 124
        Sql::do(Sql=HASH(0x5acb48ebf288), "GRANT \"musicbrainz_ro\"\x{a}   TO \"caa_redirect\"\x{a} WITH INHERIT TRUE;\x{a}") called at ./admin/UpdateDatabasePrivileges.pl line 119

Edit: I just realized from your above comment that PG 16 is required here. I can’t test it at the moment, but my other comments probably still stand.

admin/UpdateDatabasePrivileges.pl Show resolved Hide resolved
t/script/UpdateDatabasePrivileges.t Outdated Show resolved Hide resolved
admin/UpdateDatabasePrivileges.pl Outdated Show resolved Hide resolved
@mwiencek
Copy link
Member Author

mwiencek commented May 7, 2024

@yvanzo I think I've addressed all of your comments (and it should also work on PG<16 now).

Copy link
Contributor

@yvanzo yvanzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

💯 agreed that WITH INHERIT is better set once for all for roles. The other new syntax in PG 16 is probably for more casual use case.


I made a couple of (also minor) ready-to-pick suggestions for improving the output.


Last, when running this script (without --nogrant) two times in a row, two additional lines are logged:

NOTICE:  role "caa_redirect" is already a member of role "musicbrainz_ro"
NOTICE:  role "sir" is already a member of role "musicbrainz_ro"

This doesn’t seem to be an issue, mentioning it just in case.

admin/UpdateDatabasePrivileges.pl Outdated Show resolved Hide resolved
admin/UpdateDatabasePrivileges.pl Outdated Show resolved Hide resolved
Adds a new script, admin/UpdateDatabasePrivileges.pl, which is invoked from
upgrade.sh and updates privileges for the following users if they exist:

 * `musicbrainz_ro`
 * `caa_redirect`
 * `sir`

The script works by revoking all write privileges from `musicbrainz_ro`, and
then granting `USAGE` on all schemas, and `SELECT` privileges on all tables in
those schemas.

Next, for the last two schemas, it revokes all privileges from them (including
`SELECT` and `USAGE`), and then simply grants them membership in the
`musicbrainz_ro` role.
@mwiencek
Copy link
Member Author

mwiencek commented May 8, 2024

Thanks!

Last, when running this script (without --nogrant) two times in a row, two additional lines are logged:

Yeah, those are logged by PG if you try to grant a privilege that already exists. It's harmless though and may be useful information (if it wasn't expected for them to exist).

@mwiencek mwiencek merged commit 78e2251 into metabrainz:schema-change-2024-q2 May 8, 2024
2 checks passed
@mwiencek mwiencek deleted the mbs-11962 branch May 8, 2024 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants