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

sql_mode can be set in session, therefore we should look for ANSI_QUOTES in session variable instead of global variable #677

Conversation

SoledaD208
Copy link
Contributor

…TES in session variable instead of global variable

SUMMARY
ISSUE TYPE
  • Bugfix Pull Request
  • Docs Pull Request
  • Feature Pull Request
  • New Module Pull Request
COMPONENT NAME
ADDITIONAL INFORMATION

@laurent-indermuehle
Copy link
Collaborator

Hi @SoledaD208 and thank you for taking the time to open a PR.

Before we can merge, it misses:

  • A description that explains why the change is made.
  • A change log fragment with the same explanation.
  • Integration tests.

About tests : I'm not sure what will happen when you change the scope of the sql_mode from global to session for the mysql_role and mysql_user modules. A rapid grep showed that the line you modified is the only time we use sql_mode in the entire collection.

Also, while writing tests, you could demonstrate how you intent to set a session with set sql_mode='ANSI_QUOTES'". As I don't think community.mysql allow for this. But tests are a great way to experiment that.

If I'm right, we could modify your PR to add such a feature. Feel free to ask me for help executing tests. I'm also available on matrix : #mysql:ansible.com

@SoledaD208
Copy link
Contributor Author

SoledaD208 commented Sep 15, 2024 via email

@Andersson007
Copy link
Collaborator

folks any thoughts on ^?

@laurent-indermuehle
Copy link
Collaborator

Hi @SoledaD208,
Could you provide examples of when sql_mode='ANSI_QUOTES' is useful please? Then I can help you with the integration tests. You already talked about table names in the priv fields of mysql_user. But are there other places where this would be useful? Like in a query, for instance?

For your question about the role/revoke/read of privileges, we should keep discussions separate. Maybe in a new issue. But before doing so, please use the mysql_info module with the users and users_info filters. Maybe they can do what you want. Also, keep in mind that MySQL and MariaDB treat roles differently. So please provide the engine and version you're using when you file a bug. This helps to better understand the issue.

@SoledaD208
Copy link
Contributor Author

Could you provide examples of when sql_mode='ANSI_QUOTES' is useful please?

if you ask about the bad effects to community.mysql module. Incorrect quotation in store procedure and function (not tables) will make the module always see the privs on function/SP are the new ones. As consequence, it will try to revoke the existing privs and re-grant them. Not only mysql_user, mysql_role also import the function privileges_unpack: , thus it will also affect by ANSI_QUOTES.

About the general purpose of ANSI_QUOTES. Afaik, ANSI_QUOTES is the standard SQL (which Mysql, by default, doesn't follow). So without enabling that mode, if you dump some data from Postresql or other RDBMS to a sql file, then run it in Mysql, the sql dump files will fail.

sql_mode='ANSI_QUOTE' or any session's variable can be easily set by making use of community.mysql's session_vars argument. The ansible module allows users to freely set or unset ANSI_QUOTES per session. Due to Bug #115953 , that feature is really needed for mysql_user and mysql_role to work properly, but for now it's not working correctly. The reason is it's extracting ANSI_QUOTES from global sql_mode instead of session

@SoledaD208
Copy link
Contributor Author

please provide the engine and version you're using

I'm using Mysql 8. Mysql 5.7 doesn't have partial revokes or RBAC, so there's no issue with the old version.
It looks like MariaDB does not have such feature, so we may not have issue with MariaDB

For your question about the role/revoke/read of privileges, we should keep discussions separate

Agreed with you, I will open another issue. mysql_user, mysql_role and also mysql_info will break if partial revoke is used.

@SoledaD208 SoledaD208 force-pushed the check_ansi_quotes_in_session_variable branch from 27e3b08 to 61a4ff4 Compare October 9, 2024 14:11
@laurent-indermuehle
Copy link
Collaborator

Very good tests @SoledaD208.
I'm wondering if the failed tests are because of your change. It is related to roles so it could be.
I'll need to study how the "params + persist" works. I never used it before.
I'm busy on something else, but remember you can asks for help if the tests/github action or else bother you.

@SoledaD208 SoledaD208 force-pushed the check_ansi_quotes_in_session_variable branch 2 times, most recently from d44c899 to 22e3531 Compare October 10, 2024 16:17
@SoledaD208 SoledaD208 force-pushed the check_ansi_quotes_in_session_variable branch from 22e3531 to 6d4fc3c Compare October 10, 2024 16:18
Copy link

codecov bot commented Oct 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.96%. Comparing base (28bf709) to head (24bf639).
Report is 4 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #677       +/-   ##
===========================================
+ Coverage   25.74%   76.96%   +51.21%     
===========================================
  Files          32       20       -12     
  Lines        2808     2657      -151     
  Branches      704      679       -25     
===========================================
+ Hits          723     2045     +1322     
+ Misses       2044      413     -1631     
- Partials       41      199      +158     
Flag Coverage Δ
integration 76.32% <100.00%> (?)
sanity ?
units ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@SoledaD208
Copy link
Contributor Author

SoledaD208 commented Oct 10, 2024

thanks @laurent-indermuehle indeed my "params + persist" messed up the tests.
Reason is when persist is used, the sql_mode's value will be written down to the container's .my.conf therefore it affects the other tests. I added Ansible tasks to reconfigure sql_mode back to the original value after the test succeeds. Let's see if all the tests get passed...

@SoledaD208
Copy link
Contributor Author

SoledaD208 commented Oct 10, 2024

The tests went well for Mysql, but failed for Mariadb. Apparently, it's because in Mariadb, users can't persist sql_mode to config file like what they can do in Mysql 8. I had changed the testing playbook a bit: mode: "{% if db_engine == 'mariadb' %}global{% else %}persist{% endif %}" , then thanks god, all good now.

@laurent-indermuehle
Copy link
Collaborator

@SoledaD208 great job! Are you ready for a review or you want to add more tests?

Also, please add a changelogs / framents file to describe what you're changing and why.

Copy link
Collaborator

@Andersson007 Andersson007 left a comment

Choose a reason for hiding this comment

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

@SoledaD208 thanks for the persistent work on the improvement!

Yep, as @laurent-indermuehle spotted we need a fragment here https://docs.ansible.com/ansible/latest/community/development_process.html#creating-a-changelog-fragment

mysql_query:
<<: *mysql_params
query: 'select @@GLOBAL.sql_mode AS sql_mode'
register: sql_mode_orig
Copy link
Collaborator

@Andersson007 Andersson007 Oct 15, 2024

Choose a reason for hiding this comment

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

Suggested change
register: sql_mode_orig
register: sql_mode_orig

would you like to assert: this one too? I see you assert the second one called result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, let me check and add the assert. by the way, due to some reasons, I will need to close this PR and open another via other account.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks, let me check and add the assert. by the way, due to some reasons, I will need to close this PR and open another via other account.

SGTM

@SoledaD208
Copy link
Contributor Author

hi @laurent-indermuehle @Andersson007 , I just modified my PR to add fragments files and improve the test file. The PR should be ready now, could you please have a look?

@Andersson007
Copy link
Collaborator

@SoledaD208 let's wait for @laurent-indermuehle a bit, if no response i'll merge it within a few days, thanks

@SoledaD208
Copy link
Contributor Author

SoledaD208 commented Oct 23, 2024 via email

@laurent-indermuehle
Copy link
Collaborator

I'm on vacation. I'll back next week. No computer here so don't wait for me. Or do if it can wait ;)

Copy link
Collaborator

@laurent-indermuehle laurent-indermuehle left a comment

Choose a reason for hiding this comment

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

Here is some linting propositions, mainly usage of FQCN and failed_when instead of assert when only one test is performed (this makes for better CI output).

But didn't you said you have to create a new PR @SoledaD208 ?

tests/integration/targets/test_mysql_user/tasks/main.yml Outdated Show resolved Hide resolved
@SoledaD208
Copy link
Contributor Author

hi @laurent-indermuehle , please continue with this PR. I don't need to create new one any more. I just modified my PR and commited all the suggestions, please review again.
Thanks

@Andersson007
Copy link
Collaborator

@laurent-indermuehle could you please take a look?

@laurent-indermuehle
Copy link
Collaborator

Sure! Sorry for the delay.
This looks good to me, thanks for the modifications @SoledaD208. I'm merging now. This PR will be available in the next release.

@Andersson007
Copy link
Collaborator

@SoledaD208 thanks for the contribution!
@laurent-indermuehle thanks for reviewing and merging!

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.

3 participants