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

fix: grant role before creating database #5828

Merged
merged 5 commits into from
Jan 26, 2023

Conversation

ltalirz
Copy link
Member

@ltalirz ltalirz commented Dec 8, 2022

In some postgresql installations, the following fails:

template1=> create user test;
CREATE ROLE
template1=> create database test owner test;
ERROR:  must be member of role "test"

This is fixed by

template1=> grant test to azureuser;
GRANT ROLE
template1=> create database test owner test;
CREATE DATABASE

Furthermore, granting privileges to the owner is superfluous according to the postgresql docs, so we drop it.

The investigation of this issue also surfaces a bug in the error message for "manual database creation", and that the --su-db-name was not passed on from the commandline. This is fixed in passing.

In some postgresql installations, the following fails:

	template1=> create user test;
	CREATE ROLE
	template1=> create database test owner test;
	ERROR:  must be member of role "test"

This is fixed by

	template1=> grant test to azureuser;
	GRANT ROLE
	template1=> create database test owner test;
	CREATE DATABASE

Furthermore, granting privileges to the owner is superfluous according
to the postgresql docs, so we drop it.

The investigation of this issue also surfaces a bug in the error message
for "manual database creation", and that the `--su-db-name` was not
passed on from the commandline.
@ltalirz ltalirz linked an issue Dec 8, 2022 that may be closed by this pull request
@ltalirz
Copy link
Member Author

ltalirz commented Dec 8, 2022

The error is

+ verdi quicksetup --non-interactive --profile default --email aiida@localhost --first-name Giuseppe --last-name Verdi --institution Khedivial --db-backend core.psql_dos
Error: Oops! quicksetup was unable to create the AiiDA database for you.
See `verdi quicksetup -h` for how to specify non-standard parameters for the postgresql connection.
Alternatively, create the AiiDA database yourself: 
Run the following commands as a UNIX user with access to PostgreSQL (Ubuntu: $ sudo su postgres):

	$ psql template1
	==> CREATE USER "aiida_qs_aiida_477d3dfc78a2042156110cb00ae18f" WITH PASSWORD '<password>' 
	==> GRANT "aiida_qs_aiida_477d3dfc78a2042156110cb00ae3618f" TO "postgres"
	==> CREATE DATABASE "default_aiida_477d3dfc78a2042156110cb00ae3618f" OWNER "aiida_qs_aiida_477d3dfc78a2042156110cb00ae3618f" ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE=template0

and then use `verdi setup` instead

Traceback (most recent call last):
  File "/opt/conda/bin/verdi", line 8, in <module>
    sys.exit(verdi())
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1[40](https://github.com/aiidateam/aiida-core/actions/runs/3644014214/jobs/6152813665#step:7:41)4, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 7[60](https://github.com/aiidateam/aiida-core/actions/runs/3644014214/jobs/6152813665#step:7:61), in invoke
    return __callback(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/aiida/cmdline/commands/cmd_setup.py", line 185, in quicksetup
    raise exception
  File "/opt/conda/lib/python3.8/site-packages/aiida/cmdline/commands/cmd_setup.py", line 174, in quicksetup
    db_username, db_name = postgres.create_dbuser_db_safe(dbname=db_name, dbuser=db_username, dbpass=db_password)
  File "/opt/conda/lib/python3.8/site-packages/aiida/manage/external/postgres.py", line 194, in create_dbuser_db_safe
    self.create_dbuser(dbuser=dbuser, dbpass=dbpass)
  File "/opt/conda/lib/python3.8/site-packages/aiida/manage/external/postgres.py", line 112, in create_dbuser
    self.execute(_GRANT_ROLE_COMMAND.format(dbuser, self.dsn['user']))
  File "/opt/conda/lib/python3.8/site-packages/pgsu/__init__.py", line 120, in execute
    return _execute_psyco(command, dsn)
  File "/opt/conda/lib/python3.8/site-packages/pgsu/__init__.py", line 2[61](https://github.com/aiidateam/aiida-core/actions/runs/3644014214/jobs/6152813665#step:7:62), in _execute_psyco
    cursor.execute(command)
psycopg2.errors.UndefinedObject: role "None" does not exist

I.e. somehow self.dsn['user'] is None, while postgres is correctly passed along from the cli (since the "manual instructions" are correct).

When using peer authentication, the `dsn['user']` in PGSU is `None`.
@ltalirz
Copy link
Member Author

ltalirz commented Dec 8, 2022

I see - inside the container, going via the default postgres user doesn't work, and AiiDA ends up using peer authentication (which PGSU represents internally as user None).

We can use the current_user shorthand in postgresql when granting the role.

@ltalirz ltalirz marked this pull request as ready for review December 8, 2022 10:52
@ltalirz ltalirz requested a review from sphuber December 8, 2022 10:52
Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

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

Thanks @ltalirz some minor comments

@@ -177,8 +178,8 @@ def quicksetup(
'Oops! quicksetup was unable to create the AiiDA database for you.',
'See `verdi quicksetup -h` for how to specify non-standard parameters for the postgresql connection.\n'
'Alternatively, create the AiiDA database yourself: ',
manual_setup_instructions(dbuser=su_db_username,
dbname=su_db_name), '', 'and then use `verdi setup` instead', ''
manual_setup_instructions(db_username=db_username,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not concerning this line, but line 184: should we just reraise the exception? Isn't it better to simply use echo.echo_critical? Usually showing a scary traceback by default to a user from a CLI command is not a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also just noted that the instructions above (to use verdi quicksetup -h for help) were wrong for a long time and only (coincidentally) got fixed just two weeks ago in 6469e23 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Sometimes the traceback can also help to see where the problem is... let me know if you want me to change it

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a reusable option that we have --traceback that will optionally show the traceback which I think can be useful. But we can leave it for now, it is not critical

@@ -108,6 +108,8 @@ def create_dbuser(self, dbuser, dbpass, privileges=''):
self.connection_mode == PostgresConnectionMode.PSYCOPG
"""
self.execute(_CREATE_USER_COMMAND.format(dbuser, dbpass, privileges))
# this is needed on some postgresql installations
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add more info on what this is doing? It seems quite weird to GRANT a user to current_user. What does that mean? Grant all rights that dbuser has also to current_user?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the comment. Let me know whether it is understandable now

Copy link
Contributor

Choose a reason for hiding this comment

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

Cheers.

@sphuber sphuber self-requested a review January 26, 2023 12:10
Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

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

Thanks @ltalirz

@sphuber sphuber merged commit 76b6ba5 into aiidateam:main Jan 26, 2023
@sphuber sphuber deleted the fix/5827/create-with-owner branch January 26, 2023 12:11
@sphuber
Copy link
Contributor

sphuber commented Jan 26, 2023

@ltalirz did this address #5827 ? Think so right?

Never mind, seems it got automatically closed somehow. Didn't think it would since I didn't see the issue linked explicitly in the OP. How did Github infer that it should be closed this time? From the branch name? Surely not...

@ltalirz
Copy link
Member Author

ltalirz commented Jan 26, 2023

Yes, that is fixed now (meaning you can now use AiiDA with the hosted Azure Database for Postgresql out of the box).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

quicksetup: creating database with owner fails
2 participants