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: use socket authentication as default for root #561

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Al-thi
Copy link

@Al-thi Al-thi commented Jan 30, 2025

This is an attempt to allow the root account to use socket authentication instead of password authentication.

Socket authentication is the new default for both MariaDB and MySQL on modern versions.

The current role configuration causes authentication problems for the root account, cf. issues:

I've had authentication problems too, with the following message on every mysq_db and mysql_user tasks:

2024-09-30 19:45:59,235 (item={'name': '[REDACTED]', 'password': '[REDACTED]', 'priv': '[MY_DB_NAME].*:ALL/mysql.*:SELECT,SHOW VIEW/*.*:BINLOG ADMIN'}) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "[REDACTED]", "password": "[REDACTED]", "priv": "[MY_DB_NAME].*:ALL/mysql.*:SELECT,SHOW VIEW/*.*:BINLOG ADMIN"}, "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"}

As this socket authentication is only effective on recent MySQL (≥ 8.4) / MariaDB (≥ 10.4) versions, this PR also refactors the version parsing by:

  • extracting the interesting part of the mysql --version standard output
  • using ansible version filter instead of substring detection (with {{ 'something' in my_string }} ansible variable)

Finally, as there was some linting warnings, I also included syntax fixes in the 3 last commits.

Please tell me what you think of this :)
Thank you for your time and your amazing work.
Regards.

@Al-thi Al-thi force-pushed the fix/authentication-root-unix-socket branch 12 times, most recently from 14a6db0 to 2dcfb73 Compare January 31, 2025 08:50
@Al-thi Al-thi changed the title Fix/authentication root unix socket feat: use socket authentication as default for root Jan 31, 2025
@Al-thi Al-thi force-pushed the fix/authentication-root-unix-socket branch 4 times, most recently from e6c1a37 to 103019b Compare January 31, 2025 09:40
@Al-thi Al-thi marked this pull request as ready for review January 31, 2025 09:41
@brunick
Copy link

brunick commented Jan 31, 2025

Hi @Al-thi , i've had this problem with my newest install as well, but i also got in some trouble trying you branch.

  - name: Run mysql --version
      ansible.builtin.command: 'mysql --version'
      register: mysql_cli_version
      changed_when: false
      check_mode: false

this does not apply to newest mariadb version, as those throw deprecation warning in their response of using mysql instead of mariadb:

mysql: Deprecated program name. It will be removed in a future release, use '/usr/bin/mariadb' instead
mysql from 11.4.4-MariaDB, client 15.2 for Linux (x86_64) using  EditLine wrapper

could you update the script, to check for mysql vs mariadb and use mariadb if installed?
cheers
Edit: this is on Rocky Linux 9.5

@Al-thi
Copy link
Author

Al-thi commented Jan 31, 2025

@brunick Could you run the following command please:

ls -l $(which mysql)

I have seen many different configurations with some mysql binaries being symlinks to mariadb, other with plain mysql binaries which are in fact mariadb .... It's a mess really 😓

EDIT: could you please try the latest version ? It should run mariadb --version instead of mysql --version for rockylinux.

`mysql --version` returns a complete line that has to be parsed in order to
extract the version value, e.g.:
```
mysql  Ver 15.1 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
```

This commit makes the ansible variable `mysql_cli_version` to only store the
interesting part of the returned line, e.g.:
```
10.11.6-Mariadb
```

This will make version conditionals in tasks and templates easier everywhere
else.

Signed-off-by: Alexis Thietard <[email protected]>
Use ansible `version()` filter instead of substring detection.

cf. the official documentation:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/version_test.html

This will avoid potential versions detection false positives, with for example:
```
'8.0.' in mysql_cli_version
```
That could return `true` if the version was `11.8.0-MariaDB`.

NB:
This removes `query_cache_*` variables on MariaDB which do not work the same
way as on MySQL, cf.
https://mariadb.com/kb/en/server-system-variables/#query_cache_type
```
Warning: Starting from MariaDB 10.1.7, query_cache_type is automatically set to
ON if the server is started with the query_cache_size set to a non-zero (and
non-default) value. This will happen even if query_cache_type is explicitly set
to OFF in the configuration.
```
It is misleading to have `query_cache_type: 0` by default and still enabling
the `query_cache` feature, so this commit restricts `query_cache` explicit
configuration to MySQL.
This effectively resets the query cache to its default upstream value on
MariaDB:
* query cache enabled
* query cache size: 1M
* query cache limit: 1M

Signed-off-by: Alexis Thietard <[email protected]>
…l versions

Since MariaDB 10.4 / MySQL 8.4, the root mysql user is configured to use
`unix_socket` instead of password authentication. No password is privisioned
at MariaDB installation time for the root user.

Using socket authentication is recommended and considered a good security
practice.
cf. https://mariadb.com/kb/en/authentication-from-mariadb-10-4/
cf. https://dev.mysql.com/doc/refman/8.4/en/native-pluggable-authentication.html

Fix geerlingguy#550
Fix geerlingguy#522
Fix geerlingguy#431
Fix geerlingguy#421

Signed-off-by: Alexis Thietard <[email protected]>
Only use `true` / `false` for booleans.
cf. https://ansible.readthedocs.io/projects/lint/rules/yaml/#yaml

Signed-off-by: Alexis Thietard <[email protected]>
@Al-thi Al-thi force-pushed the fix/authentication-root-unix-socket branch from 103019b to 0b5b6c2 Compare January 31, 2025 14:57
@brunick
Copy link

brunick commented Feb 3, 2025

@brunick Could you run the following command please:

ls -l $(which mysql)

I have seen many different configurations with some mysql binaries being symlinks to mariadb, other with plain mysql binaries which are in fact mariadb .... It's a mess really 😓

EDIT: could you please try the latest version ? It should run mariadb --version instead of mysql --version for rockylinux.

Hey @Al-thi,

[root@mysql-server]# ls -l $(which mysql)
lrwxrwxrwx 1 root root 7 30. Okt 18:40 /usr/bin/mysql -> mariadb

@Al-thi
Copy link
Author

Al-thi commented Feb 3, 2025

@brunick did you try the latest version ?

@brunick
Copy link

brunick commented Feb 4, 2025

@brunick did you try the latest version ?

not yet, i had to create a workaround to get my server up'n'running

i actually "only" had to add a root user with permissions to connect from 127.0.0.1 with a password.

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.

2 participants