pgbouncer-认证过程

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: pgbouncer

密码文件:
认证方法,可设置为any, trust, plain, crypt, md5

连接信息未指定用户名称和密码。

auth_type = trust

pgbouncer.ini
[databases]
;;; 未设置用户密码,需要使用auth_file密码连接数据。
appdb=host=db01 port=5666 dbname=appdb
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
auth_type = trust
;;;pgbouncer密码文件,和数据库用户对应即可。
auth_file = /data/pg
listen_addr = *
listen_port = 5766

密码文件

cat /data/pg
"appuser" "1qaz@WSX"

输入不存在的用户(密码文件不存在,数据不存在。)

密码文件(auth_file)用户不存在;(错误信息:用户不存在)

psql -h db01 -p 5766 appdb -U abc
psql: ERROR: no such user: abc

pgbouncer.log日志:

2021-11-17 21:02:44.049 CST [77537] LOG C-0x15d58c0: (nodb)/(nouser)@172.25.113.97:42630 closing because: no such user: abc (age=0s)
2021-11-17 21:02:44.049 CST [77537] WARNING C-0x15d58c0: (nodb)/(nouser)@172.25.113.97:42630 pooler error: no such user: abc
2021-11-17 21:02:44.049 CST [77537] LOG C-0x15d58c0: (nodb)/(nouser)@172.25.113.97:42630 login failed: db=appdb user=abc

数据库不存在用户,密码文件存在的用户;(错误信息:密码错误)

[pg10@db01 tmp]$ psql -h db01 -p 5766 appdb -U admin
psql: ERROR: password authentication failed for user "admin"
[pg10@db01 tmp]$ psql -h db01 -p 5766 appdb -U admin
psql: ERROR: pgbouncer cannot connect to server

pgbouncer.log日志:

2021-11-17 21:43:47.916 CST [79506] LOG C-0xf6ba10: appdb/admin@172.25.113.97:43832 login attempt: db=appdb user=admin tls=no
2021-11-17 21:43:47.916 CST [79506] LOG S-0xf72850: appdb/admin@172.25.113.97:5666 new connection to server (from 172.25.113.97:49812)
2021-11-17 21:43:47.924 CST [79506] WARNING server login failed: FATAL password authentication failed for user "admin"
2021-11-17 21:43:47.924 CST [79506] LOG C-0xf6ba10: appdb/admin@172.25.113.97:43832 closing because: password authentication failed for user "admin" (age=0s)
2021-11-17 21:43:47.924 CST [79506] WARNING C-0xf6ba10: appdb/admin@172.25.113.97:43832 pooler error: password authentication failed for user "admin"
2021-11-17 21:43:47.924 CST [79506] LOG S-0xf72850: appdb/admin@172.25.113.97:5666 closing because: login failed (age=0s)
2021-11-17 21:43:50.250 CST [79506] LOG C-0xf6ba10: appdb/admin@172.25.113.97:43836 login attempt: db=appdb user=admin tls=no
2021-11-17 21:43:50.250 CST [79506] LOG C-0xf6ba10: appdb/admin@172.25.113.97:43836 closing because: pgbouncer cannot connect to server (age=0s)
2021-11-17 21:43:50.250 CST [79506] WARNING C-0xf6ba10: appdb/admin@172.25.113.97:43836 pooler error: pgbouncer cannot connect to server

源数据库日志:

2021-11-17 21:43:47.924 CST,"admin","appdb",79511,"172.25.113.97:49812",61950713.13697,1,"authentication",2021-11-17 21:43:47 CST,5/344,0,FATAL,28P01,"password authentication failed for user ""admin""","Role ""admin"" does not exist.
Connection matched pg_hba.conf line 95: ""host all all 0.0.0.0/0 scram-sha-256""",,,,,,,,""

密码文件,数据库,已经存在在的用户

不用输入密码即可登录数据库。(trust认证,用户名称正确即可登录)

[pg10@db01 pgbouncer]$ psql -h db01 -p 5766 appdb -U appuser <<客户端不用输入密码。
psql (10.14)
Type "help" for help.
appdb=> ###成功登录###

pgbouncer.log日志:

appdb/appuser@172.25.113.97:42608 login attempt: db=appdb user=appuser tls=no

auth_type = md5

5432数据库建立测试用户和数据库:
create user test01 with password '123456';
create database test01 owner test01;
grant all privileges on database test01 to test01;

pgbouncer.ini
[databases]
;;; 未设置用户密码,需要使用auth_file密码连接数据;
test01=host=db01 port=5432 dbname=test01
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
auth_type = md5
auth_file = /data/pg
listen_addr = *
listen_port = 5766

密码文件存储数据库中对应的用户名称和密码;
/data/pg
"test01" "123456"

客户端登录输入认证密码:

[pg10@db01 tmp]$ psql -h db01 -p5766 -d test01 -U test01
Password for user test01:
psql (10.14)
Type "help" for help.
test01=> \q

连接信息指定用户名称和密码

auth_type = trust

pgbouncer.ini
[databases]
;;; 设置了用户和秘密,需要使用auth_file密码连接数据。
appdb=host=db01 port=5666 user=appuser password=1qaz@WSX dbname=appdb
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
;;;trust不认证密码,只要用户名对即可。
auth_type = trust
auth_file = /data/pg
listen_addr = *
listen_port = 5766

只需要认证auth_file 用户名即可,不用输入密码。

[pg10@db01 tmp]$ psql -h db01 -p 5766 appdb -U admin
psql (10.14)
Type "help" for help.
appdb=> \q

auth_type = md5

pgbouncer.ini
[databases]
;;; 设置了用户和秘密,需要使用auth_file密码连接数据。
appdb=host=db01 port=5666 user=appuser password=1qaz@WSX dbname=appdb
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
;;;md5用户名和密码必须全部正确。
auth_type = md5
auth_file = /data/pg
listen_addr = *
listen_port = 5766

需要验证密码文件(auth_file)第二列的密码;即使用户在数据库中不存在,可以认证成功。

[pg10@db01 tmp]$ psql -h db01 -p 5766 appdb -U admin1
Password for user admin1:

密码文件:

cat /data/pg
"appuser" "1qaz@WSX"
"postgres" "123456"
"admin" "1qaz@WSX"
"admin1" "123456"

数据库使用scram-sha-256密码认证:
userslist.txt通过指定文件AUTH_FILE只包含用于连接到PostgreSQL的用户和口令的信息;该文件中的密码可以是纯文本密码,也可以是使用MD5或SCRAM加密的密码,具体取决于要使用的身份验证方法。

pg_hba.conf:更改scram-sha-256.

更改数据库认证密码类型:

SELECT name,setting,source,enumvals FROM pg_settings WHERE name = 'password_encryption';
ALTER SYSTEM SET password_encryption TO 'scram-sha-256';
SELECT pg_reload_conf();

SELECT name,setting,source,enumvals FROM pg_settings WHERE name = 'password_encryption';

ALTER SYSTEM SET password_encryption TO 'md5';

SELECT pg_reload_conf();

SELECT name,setting,source,enumvals FROM pg_settings WHERE name = 'password_encryption';

更改类型之后要重新设置用户密码。

alter user postgres with superuser password '1qaz@WSX';

pgbouncer 密码文件(auth_file)为明文或者md5则连接数据库失败:

密码文件:
[pg10@db01 data]$ cat /data/pg
"test01" "123456"
[pg10@db01 data]$

连接失败,类型不支持:

[pg10@db01 data]$ psql -h db01 -p5766 -d test01 -U test01
Password for user test01:
psql: ERROR: server login failed: wrong password type

报错信息为密码类型错误.

[pg10@db01 data]$ psql -h db01 -p5766 -d test01 -U test01
psql: ERROR: pgbouncer cannot connect to server
pgbouncer.log日志信息:
2021-11-18 22:47:11.018 CST [127387] LOG C-0x9e0a10: test01/test01@172.25.113.97:55974 login attempt: db=test01 user=test01 tls=no
2021-11-18 22:47:11.018 CST [127387] LOG C-0x9e0a10: test01/test01@172.25.113.97:55974 closing because: client unexpected eof (age=0s)
2021-11-18 22:47:13.453 CST [127387] LOG C-0x9e0a10: test01/test01@172.25.113.97:55978 login attempt: db=test01 user=test01 tls=no
2021-11-18 22:47:13.453 CST [127387] LOG S-0x9e7870: test01/test01@172.25.113.97:5432 new connection to server (from 172.25.113.97:46598)
2021-11-18 22:47:13.454 CST [127387] ERROR S-0x9e7870: test01/test01@172.25.113.97:5432 cannot do SCRAM authentication: wrong password type
2021-11-18 22:47:13.454 CST [127387] LOG C-0x9e0a10: test01/test01@172.25.113.97:55978 closing because: server login failed: wrong password type (age=0s)
2021-11-18 22:47:13.454 CST [127387] WARNING C-0x9e0a10: test01/test01@172.25.113.97:55978 pooler error: server login failed: wrong password type
2021-11-18 22:47:13.454 CST [127387] LOG S-0x9e7870: test01/test01@172.25.113.97:5432 closing because: failed to answer authreq (age=0s)

需要修改用户密码文件密码信息(auth_file)为SCRAM-SHA-256类型新密码:

数据库更改用户密码类型,和重新设置用户密码;

alter user test01 password '123456';
select usename, passwd from pg_shadow order by 1;

postgres=# SELECT name,setting,source,enumvals FROM pg_settings WHERE name = 'password_encryption';

    name         |    setting    |       source       |      enumvals       
password_encryption scram-sha-256 configuration file {md5,scram-sha-256}

(1 row)

postgres=# select usename, passwd from pg_shadow order by 1;

usename passwd
pg10
test01 md5a1e41a7c7c50af10ea8fad2b76087168

(2 rows)

postgres=# alter user test01 password '123456'; --重新设置密码,SCRAM-SHA-256才生效。
ALTER ROLE
postgres=# select usename, passwd from pg_shadow order by 1;

usename passwd
pg10
test01 SCRAM-SHA-256$4096:bthWsuBz7AFCz2laBzPxbA==$YfZnMNIbx6q0HXCEVW7CRmNZh2VfEjy8L+WJiImG648=:0dxvESqwB/tT/G5goPrRdvfrX39j77p4gPx36jKtj94=

(2 rows)

修改pgbouncer密码文件(auth_file):

可见pgbouncer默认密码类型为md5:(如使用SCRAM-SHA-256$,则不能使用明文密码)

[pg10@db01 pgbouncer]$ cat pgbouncer.ini
[databases]
;;; 未设置用户密码,需要使用auth_file密码连接数据。
test01=host=db01 port=5432 dbname=test01
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
auth_type = md5
auth_file = /data/pg
listen_addr = *
listen_port = 5766

修改密码文件密码列为SCRAM-SHA-256类型密码:

[pg10@db01 pgbouncer]$ cat /data/pg
"test01" "SCRAM-SHA-256$4096:bthWsuBz7AFCz2laBzPxbA==$YfZnMNIbx6q0HXCEVW7CRmNZh2VfEjy8L+WJiImG648=:0dxvESqwB/tT/G5goPrRdvfrX39j77p4gPx36jKtj94="
[pg10@db01 pgbouncer]$

连接测试:---连接成功!
[pg10@db01 data]$ psql -h db01 -p5766 -d test01 -U test01
Password for user test01:
psql (10.14)
Type "help" for help.
test01=>

auth_type = any

当配置any,数据连接需要指定用户和密码(user=appuser password=1qaz@WS)。

[databases]
appdb=host=db01 port=5666 user=appuser password=1qaz@WSX dbname=appdb
[pgbouncer]
logfile = /home/pg10/pgbouncer/pgbouncer.log
pidfile = /home/pg10/pgbouncer/pgbouncer.pid
auth_type = any
listen_addr = *
listen_port = 5766

---只需要输入正确的数据名称,不需要认证用户名称和密码,也不需要密码文件(auth_file)
[root@db01 ~]# /home/pg10/soft/bin/psql -h db01 -p 5766 -d appdb
psql (10.14)
Type "help" for help.
appdb=>
---不指定用户名称,即可登录。
[pg10@db01 ~]$ psql -h db01 -p 5766 -d appdb
psql (10.14)
Type "help" for help.
appdb=>

database pgbouncer::
$ psql -p 6543 pgbouncer
Only users listed in configuration parameters admin_users or stats_users
are allowed to login to the console. (Except when auth_mode=any, then
any user is allowed in as a stats_user.)

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
2月前
|
关系型数据库 网络安全 数据安全/隐私保护
你会开启Postgresql 的SSL单向认证 配置?
你会开启Postgresql 的SSL单向认证 配置?
你会开启Postgresql 的SSL单向认证 配置?
|
2月前
|
关系型数据库 网络安全 分布式数据库
如何为PolarDB数据库设置SSL加密以提高链路安全性
如何为PolarDB数据库设置SSL加密以提高链路安全性 为了保障网络安全,提高链路安全性,您可以为PolarDB数据库启用SSL(Secure Sockets Layer)加密,并安装SSL CA证书到相关的应用服务。SSL在传输层对网络连接进行加密,能提升通信数据的安全性和完整性,但可能会增加网络连接响应时间。
92 2
|
12月前
|
NoSQL MongoDB 数据库
mongodb使用使用 SCRAM 验证客户端设置访问控制
SCRAM 验证客户端设置访问控制
84 0
|
关系型数据库 MySQL 数据库连接
【数据库报错系列】不建议不使用服务器身份验证建立SSL连接,必须默认建立SSL连接。
【数据库报错系列】不建议不使用服务器身份验证建立SSL连接,必须默认建立SSL连接。
|
网络协议 关系型数据库 Unix
OushuDB-客户端认证-配置方法
OushuDB-客户端认证-配置方法
75 0
|
网络协议 关系型数据库 Unix
oushudb-客户端认证-示例
oushudb-客户端认证-示例
53 0
|
数据安全/隐私保护
etcd集群开启用户身份认证
etcd集群开启用户身份认证,发现单机etcd和集群的授权不会同步,单机启动的是单机实例,集群启动的是集群实例
1150 1
|
NoSQL 安全 Shell
MongoDB 安全策略:验证和授权
对于新安装的 MongoDB ,它是没有开启身份验证的。需要进入 admin 数据库,创建一个 root 角色的超级管理员用户。之后再针对具体应用的数据库,使用管理员用户为其创建不同权限的用户,从而做到针对数据库安全的基本保障。
2141 0
|
监控 关系型数据库 Unix
PostgreSQL ident和peer基于操作系统用户的认证
PostgreSQL支持的认证方法非常多,除了自身的密码认证以外,还支持很多其他认证服务。 trust md5 password GSSAPI SSPI Ident Peer LDAP RADIUS PAM Certificate BSD 详见https://
9407 0
|
存储 监控 关系型数据库
Postgresql数据库安全性配置-密码
Postgresql数据库安全性配置-密码 数据库密码管理是数据库安全的重要环节之一。密码管理及配置策略主要包括: 密码加密存储 密码有效期 密码复杂度 密码验证失败延迟 密码验证失败次数限制,失败后锁定, 以及解锁时间 设置密码时防止密码被记录到数据库日志中下面会依次讲解在PostgreSQL中如何实现密码相关的安全性配置。
5402 0