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 nullable deleted column in API Keys table #15956

Merged

Conversation

davelopez
Copy link
Contributor

By default, the deleted column is nullable even if it has a default value of False.

There is a chance, some external scripts that create API keys for users directly in the database might not set the deleted value explicitly causing possible incongruences with multiple API keys for the user not being explicitly deleted=False.

This restricts those cases by forcing a non-null value.

We could also try to handle possible null values in the API logic, I can change the approach if this is more desirable than altering the database schema at this point.

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. [add testing steps and prerequisites here if you didn't write automated tests covering all your changes]

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@davelopez davelopez added kind/bug area/database Galaxy's database or data access layer labels Apr 19, 2023
@github-actions github-actions bot added this to the 23.0 milestone Apr 19, 2023
@mvdbeek
Copy link
Member

mvdbeek commented Apr 19, 2023

Doesn't this potentially make old (and thus supposedly inactive) API keys valid ? Can we just fix those external scripts and call it a day?

@mvdbeek
Copy link
Member

mvdbeek commented Apr 19, 2023

See also #14489 for context

@nsoranzo
Copy link
Member

nsoranzo commented Apr 19, 2023

Would you also need to add , nullable=False to https://github.com/galaxyproject/galaxy/blob/release_23.0/lib/galaxy/model/__init__.py#L9700 ?

Edit: I initially wrote , nullable=True by mistake here!

@davelopez
Copy link
Contributor Author

Doesn't this potentially make old (and thus supposedly inactive) API keys valid ?

If I remember correctly with #14489, only the latest API that is not deleted will be the valid one, so I guess the only problem will be with a new API key that is deleted=NULL

Can we just fix those external scripts and call it a day?

I'm completely fine with that, ping @hexylena in case she has some opinions.

Would you also need to add , nullable=True to https://github.com/galaxyproject/galaxy/blob/release_23.0/lib/galaxy/model/__init__.py#L9700 ?

Apparently, this is the default when you don't explicitly set the nullable property, so it won't change anything I guess.

@davelopez davelopez marked this pull request as draft April 19, 2023 10:34
@mvdbeek
Copy link
Member

mvdbeek commented Apr 19, 2023

Apparently, this is the default when you don't explicitly set the nullable property, so it won't change anything I guess.

I guess @nsoranzo meant that it needs to be changed to nullable=False, no ?

@nsoranzo
Copy link
Member

I guess @nsoranzo meant that it needs to be changed to nullable=False, no ?

Indeed, sorry!

@mvdbeek
Copy link
Member

mvdbeek commented Apr 19, 2023

only the latest API that is not deleted will be the valid one, so I guess the only problem will be with a new API key that is deleted=NULL

right ... so if an admin set deleted=False on the latest API key your old key (which is now the latest non-deleted one) becomes active ... it just seems like there is too much room for error here. It may not work that way, but the fact that we can't talk about this without tripping up makes me think we should migrate deleted=null to deleted=true in the migration to be on the safe side.

Or not do anything and fix the script.

@mvdbeek
Copy link
Member

mvdbeek commented Apr 19, 2023

what happens when you declare a column as non-nullable in the migration but you have null values in the database ? if that only blocks inserting new null values then that would also be a valid option.

@davelopez
Copy link
Contributor Author

what happens when you declare a column as non-nullable in the migration but you have null values in the database ?

The migration will fail with:

sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) column "deleted" of relation "api_keys" contains null values

Agree, I will change the delete=NULL existing rows to delete=True as suggested, feels much safer, in the worst case the user will need to recreate the API key because the one that may be used was discarded in the migration.

@bgruening
Copy link
Member

This probably impacts only EU. If it's all easier, then let's fix this on dev and don't introduce a migration in a stable branch.

@davelopez davelopez force-pushed the 23.0_fix_api_keys_deleted_column_nullable branch from 53f7db6 to 0a4d214 Compare April 19, 2023 11:49
@davelopez davelopez marked this pull request as ready for review April 19, 2023 11:51
@hexylena
Copy link
Member

Can we just fix those external scripts and call it a day?

I'm completely fine with that, ping @hexylena in case she has some opinions.

I mean, yea, we can. We are doing that separately. That said, this column allowed nulls which semantically do not make sense, and we should fix that underlying issue as well at some point, how that happens (23.0 vs dev) all is fine for me. But now that it 'fails closed', this looks better to me.

Copy link
Member

@mvdbeek mvdbeek left a comment

Choose a reason for hiding this comment

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

Looks good to me, @jdavcs can you also take a look ?

@mvdbeek mvdbeek requested a review from jdavcs April 19, 2023 13:48
@davelopez davelopez marked this pull request as draft April 19, 2023 14:18
@jdavcs
Copy link
Member

jdavcs commented Apr 19, 2023

Both alter statements (upgrade and downgrade) should be wrapped in a batch operations context manager:

with op.batch_alter_table(table_name) as batch_op:
    batch_op.alter_column(column_name, existing_type=sa.Boolean(), nullable=True, server_default=None)

Note that we do not need to pass table name to alter_column, and we call it on batch_op, not op.

The reason for that is that SQLite has very limited support for ALTER statements. The batch mode uses the "move and copy" approach.

The rest looks fine.

@jdavcs
Copy link
Member

jdavcs commented Apr 19, 2023

Also, not a dealbreaker, but would it be possible to merge this after #15811? Otherwise, it would have to be edited after that PR is merged. With 15811, this revision script would look like this:

with transaction():
   op.execute(...)
   alter_column(...)

In addition, this revision would be tested (that PR adds a test to the CI so that all revisions go through a downgrade>upgrade test sequence on both postgresql and sqlite).

@nsoranzo
Copy link
Member

Also, not a dealbreaker, but would it be possible to merge this after #15811? Otherwise, it would have to be edited after that PR is merged. With 15811, this revision script would look like this:

This seems another reason to target dev instead of 23.0 ?

@davelopez
Copy link
Contributor Author

Yes, I will retarget dev and update the PR with John's suggestions as soon as #15811 is merged. I will keep it in draft for now and rename the title :)

@davelopez davelopez changed the title [23.0] Fix nullable deleted column in API Keys table Fix nullable deleted column in API Keys table Apr 20, 2023
@davelopez davelopez modified the milestones: 23.0, 23.1 Apr 20, 2023
@davelopez davelopez changed the base branch from release_23.0 to dev April 26, 2023 15:39
@davelopez davelopez force-pushed the 23.0_fix_api_keys_deleted_column_nullable branch from 0a4d214 to 56598e4 Compare April 26, 2023 15:40
@davelopez
Copy link
Contributor Author

Rebased and retargeted to dev.
@jdavcs a final review to see if I included your requested changes properly would be much appreciated 🍻

@jdavcs
Copy link
Member

jdavcs commented Apr 26, 2023

Rebased and retargeted to dev. @jdavcs a final review to see if I included your requested changes properly would be much appreciated beers

@davelopez my suggestion is to do the same as we do with all other DDL statements: have a helper like this. That way they are all routed through the base DDLOperation class that handles all checks, makes --repair mode possible, etc. I'll add one today.

@jdavcs jdavcs mentioned this pull request Apr 27, 2023
4 tasks
@jdavcs
Copy link
Member

jdavcs commented Apr 27, 2023

@davelopez I've added the utility in #16009. With that, instead of with op.batch... you can just call alter_column( passing the table name + the same arguments you passed to the batch op function.

This makes sure the deleted column in the `api_keys` table has a valid boolean value from now on and sets any existing null value as deleted (True).
@davelopez davelopez force-pushed the 23.0_fix_api_keys_deleted_column_nullable branch from 56598e4 to 5066f23 Compare April 27, 2023 09:15
@davelopez davelopez marked this pull request as ready for review April 27, 2023 09:16
@jdavcs
Copy link
Member

jdavcs commented Apr 27, 2023

Test failures: server_default cannot be a bool. None or str. https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column.params.server_default

This makes sure the default value is set when inserting new rows with or without using Sqlalchemy.

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>
@davelopez davelopez force-pushed the 23.0_fix_api_keys_deleted_column_nullable branch from 0028fb8 to 65984d7 Compare April 27, 2023 14:31
@jdavcs jdavcs merged commit 1ef0d66 into galaxyproject:dev Apr 27, 2023
38 checks passed
@jdavcs
Copy link
Member

jdavcs commented Apr 27, 2023

Thank you, @davelopez and everyone for such a thorough review!

@mvdbeek mvdbeek added highlight/power-user Included at bottom of user-facing release notes (please use either this or highlight, but not both) highlight/admin Included in admin/dev release notes labels Apr 27, 2023
@mvdbeek
Copy link
Member

mvdbeek commented Apr 27, 2023

this definitely needs to go into the release announcement, since it'll invalidate old api keys

@davelopez davelopez deleted the 23.0_fix_api_keys_deleted_column_nullable branch April 28, 2023 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/database Galaxy's database or data access layer highlight/admin Included in admin/dev release notes highlight/power-user Included at bottom of user-facing release notes (please use either this or highlight, but not both) kind/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants