Ինչպես օգտագործել MySQL առանց գաղտնաբառի (և անվտանգության ռիսկերի)

Ինչպես օգտագործել MySQL առանց գաղտնաբառի (և անվտանգության ռիսկերի)

Ասում են, որ ամենալավ գաղտնաբառն այն է, որ պետք չէ հիշել։ MySQL-ի դեպքում դա հնարավոր է plugin-ի շնորհիվ auth_socket և դրա տարբերակը MariaDB-ի համար - unix_socket.

Այս երկու պլագիններն էլ ամենևին էլ նոր չեն, դրանց մասին շատ բան է ասվել այս նույն բլոգում, օրինակ՝ հոդվածում. ինչպես փոխել գաղտնաբառերը MySQL 5.7-ում՝ օգտագործելով auth_socket plugin-ը. Այնուամենայնիվ, MariaDB 10.4-ում նորությունները ուսումնասիրելիս ես հայտնաբերեցի, որ unix_socket-ն այժմ տեղադրված է լռելյայն և հանդիսանում է նույնականացման մեթոդներից մեկը («մեկը», քանի որ MariaDB 10.4-ում մեկից ավելի փլագին հասանելի է մեկ օգտագործողի համար նույնականացման համար, որը բացատրվում է փաստաթղթում «Նույնականացում» MariaDB 10.04-ից).

Ինչպես ասացի, սա նորություն չէ, և MySQL-ը տեղադրելիս՝ օգտագործելով Debian թիմի կողմից աջակցվող .deb փաթեթները, ստեղծվում է արմատային օգտատեր՝ socket authentication-ի համար։ Սա ճիշտ է ինչպես MySQL-ի, այնպես էլ 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>>

MySQL-ի համար Debian փաթեթներով արմատական ​​օգտագործողը վավերացվում է հետևյալ կերպ.

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)

Նույնը վերաբերում է MariaDB-ի .deb փաթեթին.

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)

Պերկոնայի պաշտոնական պահոցից .deb փաթեթները նաև կարգավորում են արմատային օգտվողի նույնականացումը auth-socket-ի և Percona սերվերի համար: Եկեք օրինակ բերենք հետ Percona սերվեր MySQL 8.0.16-7-ի համար և 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)

Այսպիսով, ո՞րն է կախարդանքը: Փլագինը ստուգում է, որ Linux օգտագործողը համընկնում է MySQL օգտագործողի հետ՝ օգտագործելով SO_PEERCRED socket տարբերակը՝ հաճախորդի ծրագիրը վարող օգտատիրոջ մասին տեղեկություններ հավաքելու համար: Այսպիսով, plugin-ը կարող է օգտագործվել միայն համակարգերում, որոնք աջակցում են SO_PEERCRED տարբերակը, օրինակ՝ Linux-ը: SO_PEERCRED socket տարբերակը թույլ է տալիս պարզել վարդակի հետ կապված գործընթացի uid-ը: Եվ հետո նա արդեն ստանում է այս uid-ի հետ կապված օգտվողի անունը։

Ահա մի օրինակ «բոմժ» օգտագործողի հետ.

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

Քանի որ MySQL-ում «բոմժ» օգտվող չկա, մեզ արգելված է մուտքը: Եկեք ստեղծենք նման օգտվող և նորից փորձենք.

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)

Տեղի է ունեցել!

Դե, ինչ վերաբերում է ոչ Debian բաշխմանը, որտեղ սա լռելյայն տրամադրված չէ: Եկեք փորձենք Percona սերվերը MySQL 8-ի համար, որը տեղադրված է 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

Սխալ. Ի՞նչն էր պակասում։ Փլագինը բեռնված չէ.

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

Եկեք պրոցեսին ավելացնենք հավելում.

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)

Այժմ մենք ունենք այն ամենը, ինչ անհրաժեշտ է: Եկեք նորից փորձենք.

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)

Այժմ կարող եք մուտք գործել՝ օգտագործելով «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)

Եվ դա նորից աշխատեց:

Հարց. հնարավո՞ր է մուտք գործել համակարգ նույն percona մուտքի ներքո, բայց որպես այլ օգտվող:

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

Ոչ, չի ստացվի:

Արտադրողականություն

MySQL-ը բավականին ճկուն է մի քանի առումներով, որոնցից մեկը վավերացման մեթոդն է: Ինչպես տեսնում եք այս գրառումից, մուտքը կարելի է ձեռք բերել առանց գաղտնաբառերի՝ հիմնվելով ՕՀ օգտագործողների վրա: Սա կարող է օգտակար լինել որոշակի սցենարներում, և դրանցից մեկն այն է, երբ միգրացիան RDS/Aurora-ից սովորական MySQL՝ օգտագործելով IAM տվյալների բազայի նույնականացումդեռ մուտք գործելու համար, բայց առանց գաղտնաբառերի:

Source: www.habr.com

Добавить комментарий