diff --git a/swh/core/db/db_utils.py b/swh/core/db/db_utils.py --- a/swh/core/db/db_utils.py +++ b/swh/core/db/db_utils.py @@ -689,3 +689,12 @@ "Asked for flavor %s, but module does not support database flavors", flavor, ) + + # Grant read-access to guest user on all tables of the schema + with connect_to_conninfo(db_or_conninfo) as db: + if not db: + return None + + with db.cursor() as c: + query = "grant select on all tables in schema public to guest" + c.execute(query) diff --git a/swh/core/db/tests/conftest.py b/swh/core/db/tests/conftest.py --- a/swh/core/db/tests/conftest.py +++ b/swh/core/db/tests/conftest.py @@ -7,7 +7,9 @@ from click.testing import CliRunner from hypothesis import HealthCheck +import psycopg2 import pytest +from pytest_postgresql import factories from swh.core.db.db_utils import import_swhmodule @@ -21,6 +23,17 @@ ) +def create_role_guest(**kwargs): + with psycopg2.connect(**kwargs) as conn: + with conn.cursor() as cur: + cur.execute("CREATE ROLE guest LOGIN PASSWORD 'guest'") + + +postgresql_proc = factories.postgresql_proc( + load=[create_role_guest], +) + + @pytest.fixture def cli_runner(): return CliRunner()