Kiel Uzi MySQL Sen Pasvorto (kaj Sekurecaj Riskoj)

Kiel Uzi MySQL Sen Pasvorto (kaj Sekurecaj Riskoj)

Ili diras, ke la plej bona pasvorto estas tiu, kiun vi ne devas memori. En la kazo de MySQL tio eblas danke al la kromaĵo aŭth_socket kaj ĝia versio por MariaDB - unix_socket.

Ambaŭ ĉi tiuj kromprogramoj tute ne estas novaj; pri ili oni multe diris en ĉi tiu sama blogo, ekzemple en la artikolo pri kiel ŝanĝi pasvortojn en MySQL 5.7 uzante aldonaĵon auth_socket. Tamen, rigardante kio estas nova en MariaDB 10.4, mi malkovris, ke unix_socket nun estas instalita defaŭlte kaj estas unu el la aŭtentikigmetodoj ("unu el", ĉar en MariaDB 10.4 pli ol unu kromaĵo estas disponebla por unu uzanto por aŭtentigo, kiu estas klarigita en la dokumento "Aŭtentikigo" de MariaDB 10.04).

Kiel mi diris, ĉi tio ne estas novaĵo, kaj kiam oni instalas MySQL per la pakaĵoj .deb subtenataj de la Debiana teamo, oni kreas radikan uzanton por socket-aŭtentikigo. Ĉi tio validas kaj por MySQL kaj MariaDB.

root@app:~# apt-cache show mysql-server-5.7 | grep -i maintainers
Original-Maintainer: Debian MySQL Maintainers <[email protected]>
Original-Maintainer: Debian MySQL Maintainers <<a href="mailto:[email protected]">[email protected]</a>>

Kun Debianaj pakoj por MySQL, la radika uzanto estas aŭtentikigita jene:

root@app:~# whoami
root=
root@app:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> select user, host, plugin, authentication_string from mysql.user where user = 'root';
+------+-----------+-------------+-----------------------+
| user | host      | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket |                       |
+------+-----------+-------------+-----------------------+
1 row in set (0.01 sec)

La sama estas la kazo kun la pako .deb por MariaDB:

10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04

MariaDB [(none)]> show grants;
+------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                      |
+------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION                                  |
+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

La .deb-pakaĵoj de la oficiala Percona deponejo ankaŭ agordas radikan uzantan aŭtentikigon sub aŭth-socket kaj por Percona Server. Ni donu ekzemplon kun Percona Servilo por MySQL 8.0.16-7 kaj Ubuntu 16.04:

root@app:~# whoami
root
root@app:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.16-7 Percona Server (GPL), Release '7', Revision '613e312'

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> select user, host, plugin, authentication_string from mysql.user where user ='root';
+------+-----------+-------------+-----------------------+
| user | host      | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket |                       |
+------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)

Kio do estas la magio? La kromaĵo kontrolas, ke la Linukso-uzanto kongruas kun la MySQL-uzanto uzante la SO_PEERCRED socket-opcion por kolekti informojn pri la uzanto prizorganta la klientprogramon. Tiel, la kromaĵo povas esti uzata nur en sistemoj kiuj subtenas la opcion SO_PEERCRED, kiel Linukso. La SO_PEERCRED ingo opcio permesas vin ekscii la uid de la procezo asociita kun la ingo. Kaj tiam li jam ricevas la uzantnomon asociitan kun ĉi tiu uid.

Jen ekzemplo kun la uzanto "vagrant":

vagrant@mysql1:~$ whoami
vagrant
vagrant@mysql1:~$ mysql
ERROR 1698 (28000): Access denied for user 'vagrant'@'localhost'

Ĉar ne ekzistas "vagabunda" uzanto en MySQL, ni estas rifuzita aliro. Ni kreu tian uzanton kaj provu denove:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket;
Query OK, 0 rows affected (0.00 sec)

vagrant@mysql1:~$ mysql
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 45
Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> show grants;
+---------------------------------------------------------------------------------+
| Grants for vagrant@localhost                                                    |
+---------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket |
+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Okazis!

Nu, kio pri ne-Debiana distribuo kie ĉi tio ne estas provizita defaŭlte? Ni provu Percona Server por MySQL 8 instalita sur CentOS 7:

mysql> show variables like '%version%comment';
+-----------------+---------------------------------------------------+
| Variable_name   | Value                                   |
+-----------------+---------------------------------------------------+
| version_comment | Percona Server (GPL), Release 7, Revision 613e312 |
+-----------------+---------------------------------------------------+
1 row in set (0.01 sec)

mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded

Bummer. Kio mankis? Kromaĵo ne ŝarĝita:

mysql> pager grep socket
PAGER set to 'grep socket'
mysql> show plugins;
47 rows in set (0.00 sec)

Ni aldonu kromprogramon al la procezo:

mysql> nopager
PAGER set to stdout
mysql> INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
Query OK, 0 rows affected (0.00 sec)

mysql> pager grep socket; show plugins;
PAGER set to 'grep socket'
| auth_socket                     | ACTIVE | AUTHENTICATION | auth_socket.so | GPL     |
48 rows in set (0.00 sec)

Nun ni havas ĉion, kion ni bezonas. Ni provu denove:

mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'percona'@'localhost';
Query OK, 0 rows affected (0.01 sec)

Vi nun povas ensaluti uzante la uzantnomon "percona".

[percona@ip-192-168-1-111 ~]$ whoami
percona
[percona@ip-192-168-1-111 ~]$ mysql -upercona
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 19
Server version: 8.0.16-7 Percona Server (GPL), Release 7, Revision 613e312

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> select user, host, plugin, authentication_string from mysql.user where user ='percona';
+---------+-----------+-------------+-----------------------+
| user    | host   | plugin   | authentication_string |
+---------+-----------+-------------+-----------------------+
| percona | localhost | auth_socket |                       |
+---------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)

Kaj ĝi denove funkciis!

Demando: ĉu eblos ensaluti al la sistemo sub la sama percona ensaluto, sed kiel malsama uzanto?

[percona@ip-192-168-1-111 ~]$ logout
[root@ip-192-168-1-111 ~]# mysql -upercona
ERROR 1698 (28000): Access denied for user 'percona'@'localhost'

Ne, ĝi ne funkcios.

konkludo

MySQL estas sufiĉe fleksebla en pluraj aspektoj, unu el kiuj estas la aŭtentikiga metodo. Kiel vi povas vidi de ĉi tiu afiŝo, aliro povas esti akirita sen pasvortoj, surbaze de OS-uzantoj. Ĉi tio povas esti utila en certaj scenaroj, kaj unu el ili estas dum migrado de RDS/Aurora al regula MySQL uzante IAM-datumbaza aŭtentikigopor ankoraŭ akiri aliron, sed sen pasvortoj.

fonto: www.habr.com

Aldoni komenton