墨墨导读:PostgreSQL使用session_exec插件实现用户密码验证失败几次后自动锁定,本文介绍一种处理方案。
一、插件session_exec安装配置篇
下载插件并编译安装。
https://github.com/okbob/session_exec
$ unzip session_exec-master.zip
$ cd session_exec-master/
$ make pg_config=/opt/pgsql/bin/pg_config
$ make pg_config=/opt/pgsql/bin/pg_config install
配置postgresql.conf。
session_preload_libraries='session_exec'
session_exec.login_name='login'
注意:上面第一个变量是设置session_preload_libraries而不是通常设置的shared_preload_libraries。
第二个变量是需要自定义实现的登录函数。
重启数据库服务。
$ sudo systemctl restart postgresql-12
二、自定义登录函数篇
创建t_login表用于存储提取自数据库日志中登录失败的信息。
create table t_login
(
login_time timestamp(3) with time zone --插入时间,
user_name text --数据库登录用户,
flag int4 --标志位,0代表过期数据,1代表正常状态数据
);
使用file_fdw外部表记录数据库日志信息。
file_fdw如果未配置过,参见下面步骤。
$ cd /opt/postgresql-12.5/contrib/file_fdw
$ make && make install
create extension file_fdw;
CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
建立外部表postgres_log,关联数据库日志中登录失败的信息。
CREATE FOREIGN TABLE postgres_log(
log_time timestamp(3) with time zone,
user_name text,
database_name text,
process_id integer,
connection_from text,
session_id text,
session_line_num bigint,
command_tag text,
session_start_time timestamp with time zone,
virtual_transaction_id text,
transaction_id bigint,
error_