ITPub博客

首页 > 数据库 > 数据库开发技术 > 高可用: 体验使用Odyssey连接池(二)

高可用: 体验使用Odyssey连接池(二)

原创 数据库开发技术 作者:数据库杂记 时间:2023-05-13 06:15:00 0 删除 编辑

高可用: 体验使用Odyssey连接池(二)

1、前言

这里接着上一篇继续。TODO:  介绍Odyssey连接池支持远程DB host, 支持scram-sha-256验证方式;同时支持读、读-写模式;基于Session和基于Transaction的区别。

假设我们的Odyssey(奥德赛)连接池已经安装完毕。结构如同前一篇:

1/usr/odyssey
2├── bin
3│   └── odyssey
4├── conf
5│   └── odyssey-dev.conf

2、实例分析

2.1. 远程DB Host连接

1、使用md5认证来进行连接

我们有一台主机:sean-rh1, IP:10.47.43.138 (当然这是局域网里头的内部IP)。为便于说明问题,我们先使用odyssey直接支持的md5远程验证方式。

针对PG server (14.7或以上) 这一端,我们对默认的scram-sha-256进行适当修改,改成默认为md5认证。

1vi pg_hba.conf
2
3local   all             all                                     trust
4host    all             all             127.0.0.1/32            trust
5#新加下边这一行
6host    all             all             10.0.0.1/8              md5

然后修改mydb用户的密码为md5的形式:

 1[00:51:00-postgres@sean-rh1:/var/lib/pgsql/14/data]$ psql
2psql (14.7)
3Type "help" for help.
4
5postgres=# alter system set password_encryption = 'md5';
6ALTER SYSTEM
7postgres=# alter user mydb with password 'test123';
8ALTER ROLE
9postgres=# select passwd from pg_shadow where usename='mydb';
10                                                                passwd
11---------------------------------------------------------------------------------------------------------------------------------------
12 SCRAM-SHA-256$4096:PoyJ70yFHGRawlzk9P+BMg==$uP4zBq4Tm7qgS2fCaXrHEuhQ4TezpyJ5ymmXhznmSzA=:fOBfsPglHhLe5Q6Df53GRLBo9AHhZYgh3r6BBml1jhg=
13(1 row)
14
15postgres=# select pg_reload_conf();
16 pg_reload_conf
17----------------
18 t
19(1 row)
20
21postgres=# alter user mydb with password 'test123';
22ALTER ROLE
23postgres=# select passwd from pg_shadow where usename='mydb';
24               passwd
25-------------------------------------
26 md525e68c2fa604c27b505dd92da46ede22
27(1 row)

这样修改完以后,odyssey的配置文件大致如下:

 1storage "postgres_server" {
2        type "remote"
3        host "10.47.43.138"   # 直接IP访问远程数据库
4        port 5555
5        target_session_attrs "read-write"  # 使用的read-write
6}
7
8database "mydb" {
9        user "mydb" {
10                authentication "md5"  # md5认证
11                password "md525e68c2fa604c27b505dd92da46ede22"  # md5密码
12                storage "postgres_server"
13                pool "session"
14                server_lifetime 3600
15
16                log_debug yes
17                pool_discard yes
18
19                quantiles "0.99,0.95,0.5"
20                client_max 1007
21        }
22}

启动odyssey:

1odyssey conf/odyssey-dev.conf
2
325453 2023-05-12T00:45:47Z info [none none] (rules)
425453 2023-05-12T00:45:47Z info [none none] (server) listening on /var/run/postgresql/.s.PGSQL.6432

我们直接以6432端口连接一下试试:

 1[00:54:18-postgres@sean-rh1:/var/lib/pgsql/14/data]$ psql -p 6432 -U mydb -h 10.47.43.138
2Password for user mydb:
3psql (14.7)
4Type "help" for help.
5
6mydb=> \d
7             List of relations
8 Schema |     Name      | Type  |  Owner
9--------+---------------+-------+----------
10 public | vacuum_test   | table | postgres
11 public | vacuum_test_1 | table | postgres
12(2 rows)

能很顺利的连接上数据库。这一步其实在以前的老版本里头,可能默认的就是md5,基本上还比较顺利。可是如果换作是scam-sha-256的方式,就会遇到问题。

2、使用scram-sha-256认证

先让数据库还原成默认的scram-sha-256方式:

 1vi pg_hba.conf
2host    all             all             10.0.0.1/8              scram-sha-256
3
4# 修改password_encryption method
5postgres=# alter system set password_encryption='scram-sha-256';
6ALTER SYSTEM
7postgres=# select pg_reload_conf();
8 pg_reload_conf
9----------------
10 t
11(1 row)
12
13postgres=# alter user mydb with password 'test123';
14ALTER ROLE
15postgres=# select passwd from pg_shadow where usename='mydb';
16                                                                passwd
17---------------------------------------------------------------------------------------------------------------------------------------
18 SCRAM-SHA-256$4096:bes6Rqk2olL9X7ZdMnljTg==$H141KGu9itj4fZYmPppIlxV3efBUl41KaR+Bh6+3lx4=:XScdldfe3lWLAeD8RovfvAJECdF1G9p2T/xNygCOPM4=
19(1 row)

这时我们如果把前边的odyssey配置文件改一下认证部分:

1database "mydb" {
2        user "mydb" {
3                authentication "scram-sha-256"
4                password "SCRAM-SHA-256$4096:bes6Rqk2olL9X7ZdMnljTg==$H141KGu9itj4fZYmPppIlxV3efBUl41KaR+Bh6+3lx4=:XScdldfe3lWLAeD8RovfvAJECdF1G9p2T/xNygCOPM4="
5                storage "postgres_server"
6                ......

启动完odyssey, 并验证一下连接,发现连接端会报错,内容如下:

1[01:19:22-postgres@sean-rh1:/var/lib/pgsql/14/data]$ psql -p 6432 -U mydb -h 10.47.43.138
2Password for user mydb:
3psql: error: connection to server at "10.47.43.138", port 6432 failed: FATAL:  odyssey: ccfb8797be672: failed to connect to remote server s2b0fdc7f70f4

检查下odyssey服务进程的log,会发现如下的错误:

 127479 2023-05-12T01:21:01Z info [ccfb8797be672 none] (auth) ip '10.47.43.138' user 'mydb.mydb': host based authentication allowed
227479 2023-05-12T01:21:01Z debug [ccfb8797be672 s2b0fdc7f70f4] (setup) client ccfb8797b attached to s2b0fdc7f
327479 2023-05-12T01:21:01Z info [ccfb8797be672 s2b0fdc7f70f4] (setup) new server connection 10.47.43.138:5555 (connect time: 1085 usec, resolve time: 0 usec)
427479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (startup) received packet type: Authentication
527479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (auth) requested SASL authentication
627479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (startup) received packet type: Authentication
727479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (auth) continue SASL authentication
827479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (startup) received packet type: ErrorResponse
927479 2023-05-12T01:21:01Z error [ccfb8797be672 s2b0fdc7f70f4] (startup) FATAL 28P01 password authentication failed for user "mydb"
1027479 2023-05-12T01:21:01Z debug [none s2b0fdc7f70f4] (setup) failed to find primary within 10.47.43.138

出现:FATAL 28P01 password authentication failed for user "mydb"。

如果仔细查看官方文档,会发现,默认情况下,它是不支持scram-sha-256的认证方式的。如何解决这个问题?

我们看看文档中对"password"这一项的说明:

password string

Set route authentication password. Depending on selected method, password can be in plain text, md5 hash or SCRAM secret.

To generate SCRAM secret you can use this tool.

需要用到这个工具:https://github.com/DenisMedeirosBBD/PostgresSCRAM256PasswordGenerator

既然这样,我们照着这个库的安装走一遍。

 1git clone https://github.com/DenisMedeirosBBD/PostgresSCRAM256PasswordGenerator
2cd PostgresSCRAM256PasswordGenerator
3autoreconf -iv
4
5export LIBPQ_CFLAGS="-I/usr/pgsql-14/include/"
6export LIBPQ_LIBS="pq"
7export CPPFLAGS="-I/usr/pgsql-14/include/"
8export LDFLAGS="-L/usr/pgsql-14/lib/"
9
10./configure --prefix=/usr/odyssey
11make && make install

这样就算安装完毕了。会有一个可执行文件在/usr/odyssey/bin目录中,名为:pg-password-generator。我们再生成密码:

1[01:29:46-postgres@sean-rh1:/usr/odyssey/bin]$ ./pg-password-generator mydb test123
2Encrypted password:
3----------------------------------------
4SCRAM-SHA-256$4096:bM6x77iv98T9lEMEJ9oBDA==$wHtaJVkEe8w8sIO8cOM04gZdIxvpqWpGOu+Ei3f2PB0=:50JuKLRqjsSSapvRv6diom+hsoghTEN+c+M47CDxcIk=
5----------------------------------------
6[01:29:55-postgres@sean-rh1:/usr/odyssey/bin]$

如此看来,它用的加密与PG自身所带的不一样????带着疑惑,我们重新更新一下前边的配置文件里头的内容,把password值替换成刚生成的密码。再重启odyssey,看看效果:

令人有些失望的发现,还是连接出问题:

130398 2023-05-12T01:43:42Z debug [none sd8fb49391928] (startup) received packet type: Authentication
230398 2023-05-12T01:43:42Z debug [none sd8fb49391928] (auth) continue SASL authentication
330398 2023-05-12T01:43:42Z debug [none sd8fb49391928] (startup) received packet type: ErrorResponse
430398 2023-05-12T01:43:42Z error [c95e546466ada sd8fb49391928] (startup) FATAL 28P01 password authentication failed for user "mydb"
530398 2023-05-12T01:43:42Z debug [none sd8fb49391928] (setup) failed to find primary within 10.47.43.138
6
7PG server报的错是:
82023-05-12 01:43:42.758 UTC [30410] FATAL:  password authentication failed for user "mydb"
92023-05-12 01:43:42.758 UTC [30410] DETAIL:  Connection matched pg_hba.conf line 3: "host       all             all             10.0.0.1/8              scram-sha-256"

看来它们的文档写的是真的有问题。我再一查issues列表,有很多提出关于scram-sha-256 支持的问题。

基于此,我们可以折中一下,pg使用password_encryption='scram-sha-256',但是pg_hba里头仍然用md5。看看效果如何:

PG:

1postgres=# show password_encryption ;
2 password_encryption
3---------------------
4 scram-sha-256
5(1 row)
6
7vi /var/lib/pgsql/14/data/pg_hba.conf
8host    all             all             10.0.0.1/8              md5
 1postgres=# show password_encryption ;
2 password_encryption
3---------------------
4 scram-sha-256
5(1 row)
6postgres=# select pg_reload_conf();
7 pg_reload_conf
8----------------
9 t
10(1 row)
11
12postgres=# alter user mydb with password 'test123';
13ALTER ROLE
14postgres=# select passwd from pg_shadow where usename='mydb';
15                                                                passwd
16---------------------------------------------------------------------------------------------------------------------------------------
17 SCRAM-SHA-256$4096:ONDf4rDPAUcuQq3I/Mj06Q==$YkgSKmctm8ymRUzHIRMlk/PNaLQmkmHvj7LaMz2YY74=:4KLmCECg4zvbIwaSXAQ0OrHGTAtsdf8yB6M1O6JFQmU=
18(1 row)

odyssey配置文件:

1database "mydb" {
2        user "mydb" {
3                authentication "clear_text"
4                password "test123"
5                storage "postgres_server"

启动odyssey以后,还是可以访问的。可以总结为以下两种情况:

1)、PG server中的password_encryption = 'scram-sha-256',  如果pg_hba中auth='md5' 或者 'scram-sha-256',  那么odyssey中的authentication只能是 "clear_text",使用明文,这个时候需要好好的保护该文件

2)、PG server中的password_encryption = 'md5',  pg_hba中也是auth='md5',那么,odyssey中的authentication可以是'clear_text', 也可以是'md5’。

不管怎么说,安全性角度来讲,目前还是略显不足。希望odyssey能尽快处理这块问题。

只要连接问题解决了,同时支持读、读-写模式;基于Session和基于Transaction的区别。

读模式:

target_session_attrs "read-only"

写模式:

target_session_attrs "read-write"

而session和transaction,复用的级别不一样。transaction会更快的释放连接并复用,而session是会保持更长时间的连接。与pgbouncer中的使用差不多。

4、参考

[1] https://github.com/yandex/odyssey

[2] https://github.com/yandex/odyssey/blob/master/documentation/internals.md




原文作者:数据库杂记;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-04-22

  • 博文量
    276
  • 访问量
    160349