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

feat: reduce user interaction in quicksetup #5768

Merged

Conversation

ltalirz
Copy link
Member

@ltalirz ltalirz commented Nov 16, 2022

When verdi quicksetup detected that a database user or database already exists, it would prompt the user whether to use those and, if not, to pick a new name.

Novice users likely do not know what exactly this choice entails; furthermore, choosing to reuse the database user could actually fail, namely when the correct password for this user was not known in the config.json.

Furthermore, if the user decides not to reuse the db user/db, there is no point in letting them choose the new name. It is much easier to autogenerate it (if the user really wants to choose the name, they can pass it in via the --db-username/--db-name options to verdi quicksetup).

Here, we remove this user interaction from verdi quicksetup and simply generate new names for the dbuser and the dbname as needed.

When `verdi quicksetup` detected that a database user or database already
exists, it would prompt the user whether to use those and, if not, to pick a
new name.

Novice users likely do not know what exactly this choice entails;
furthermore, choosing to reuse the database user may actually not be
possible if the correct password for this user is not known in the
`config.json`.

Furthermore, if the user decides not to reuse the db user/db, there is
no point in letting them choose the new name. It is much easier to
autogenerate it (if the user really wants to choose the name, they can
pass it in via the `--db-username`/`--db-name` options to `verdi
quicksetup`).

Here, we remove any user interaction from `verdi quicksetup` and simply
generate new names for the dbuser and the dbname as needed.
@ltalirz ltalirz marked this pull request as ready for review November 16, 2022 14:11
@ltalirz ltalirz requested a review from sphuber November 16, 2022 14:11
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 . I agree with the change in behavior, just some questions about the implementation

aiida/manage/external/postgres.py Show resolved Hide resolved
# a second try should reuse the database user but create a new database
user2, db2 = postgres.create_dbuser_db_safe(self.dbname, self.dbuser, self.dbpass)
assert user2 == self.dbuser
assert db2 == f'{self.dbname}_1'
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add an additional case here where you pass an invalid password to see it creates a new user automatically? Or do we hit the same problem potentially where certain postgres servers trust local connections? Is this the case for our CI setup? If not, I would still add the test (as well as the one you commented out) just so we have more coverage. If it then fails when people run the tests locally and they have their postgres configured that way, that is ok I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm... I've thought about it but is it really worth adding a test here that may fail depending on the details of the test setup?

The only thing this test would add is to check that psycopg2 throws the same exception when the password is wrong as when the user name is wrong (wrong user name is already tested).
I've checked that manually, and it does.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I mean though is that lines 221-224 of create_dbuser_db_safe are never hit in this test. In the first test, the user doesn't exist, and so the first conditional is hit and it is created. In the second test, the user exists and it can authenticate, so it goes straight to the database creation, skipping the block with self.find_new_user. So is that actually getting covered now by the tests? And my point here is that it is better to have that test covered in our CI and have it potentially fail if some developer runs the test locally and has a weird Postgres configuration. Anyway, aren't you testing this against a complete mock cluster provided by PGTest? So this should never fail, should it?

Copy link
Member Author

Choose a reason for hiding this comment

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

PGTest currently hardcodes the trust authentication method, i.e. when using the temporary postgres cluster the test will always fail
https://github.com/jamesnunn/pgtest/blob/417f0d89e66a9395db694a96122e0d5ac0097d59/pgtest/pgtest.py#L506-L507

I will open an issue for this to remind ourselves, also related to #3762

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so that means adding the test I suggested will fail on GHA? If so, then I guess we can leave it for now, but it would be good to fix in the future so we can increase coverage.

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, see

POSTGRES_HOST_AUTH_METHOD: trust

Copy link
Member Author

Choose a reason for hiding this comment

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

also opened jamesnunn/pgtest#24

Copy link
Member Author

Choose a reason for hiding this comment

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

We can have a look here at what happens today if we require passwords for postgres
#5776

aiida/manage/external/postgres.py Outdated Show resolved Hide resolved
@sphuber
Copy link
Contributor

sphuber commented Jan 30, 2023

@ltalirz what is the status on this PR? Are there still open questions or should it simply be updated to contain your other recently merged PRs that worked on the quicksetup?

@ltalirz
Copy link
Member Author

ltalirz commented Jan 30, 2023

I think this can be updated and merged.

If you like, you can open an issue to remind ourselves that one may want to add a test that checks:
a new user is automatically created when one provides an existing username with an incorrect password.

This is currently problematic to add to the test suite, since for local tests we use pgtest which will accept any password. I will continue with #3762 to switch our CI setup to require a password for postgres, which should be closer to the real-world use cases.
If we wanted to, we could then add the test, but run it only on CI.

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 e03a3e1 into aiidateam:main Jan 31, 2023
@sphuber sphuber deleted the fix/5766/reduce-user-interaction-quicksetup branch January 31, 2023 12:53
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.

Reduce user interaction in verdi quicksetup for pre-existing user/database
2 participants