Postgresql限制用户登录错误次数

在oracle中我们可以通过设置FAILED_LOGIN_ATTEMPTS来限制用户密码登录错误的次数,但是在postgresql中是不支持这个功能的。尽管PostgreSQL支持event trigger,可是event局限于DDL,对于登录登出事件是没办法使用event trigger的。

不过像登录新建会话触发某个事件这个需求可以通过hook实现,不过该方法比较复杂,需要修改内核代码,在客户端认证中添加逻辑,判断输入密码次数统计。这里推荐一种比较简单的方法实现类似的功能。

这里我们要使用到session_exec这个插件,使用该插件会在登录时执行一个指定的function。

下载地址:
https://github.com/okbob/session_exec

下载解压之后需要进行以下配置:

  1. set session_preload_libraries to session_exec
  2. set session_exec.login_name to name of your login function

该插件有以下特点:

  1. 如果函数不存在则会进行警告;
  2. 函数执行失败则不允许连接。

利用该插件我们可以写一个简单的函数来实现限制用户登录错误次数的功能。

例子:
1、建立外部表记录数据库日志信息。

CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

CREATE FOREIGN TABLE pglog (  
  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_severity text,  
  sql_state_code text,  
  message text,  
  detail text,  
  hint text,  
  internal_query text,  
  internal_query_pos integer,  
  context text,  
  query text,  
  query_pos integer,  
  location text,  
  application_name text,
  backend_type text  
) SERVER pglog  
OPTIONS ( program 'find $PGDATA/log -type f -name "*.csv" -mtime -1 -exec cat {} \;', format 'csv' );  

2、创建表t_login提取数据库日志中的登录信息。

create table t_login
(
login_time timestamp(3) with time zone --插入时间,
user_name text,
flag int --标志位,0代表过期数据
);

插入登录信息:

bill=# insert into t_login  select log_time,user_name from pglog where command_tag='authentication' and error_severity= 'FATAL'
bill-# ;
INSERT 0 4

3、创建登录执行的function

create or replace function lock_user() returns void as $$
declare
res text;
c1 timestamp(3) with time zone;
begin
select login_time from t_login where flag = 0 order by login_time desc limit 1 into c1; --获取当前日志中最新时间
insert into t_login  select log_time,user_name from pglog where command_tag='authentication' and error_severity= 'FATAL' and log_time > c1; --将最新的数据插入t_login表
update t_login set flag = 1 where login_time > c1; 
for res in select user_name from t_login where flag = 1 group by user_name having count(*) >=3 --检查登录失败次数是否大于3,若大于3则锁定用户
loop
EXECUTE format('alter user %I nologin',res); --锁定用户
EXECUTE 'select pg_terminate_backend(pid) from pg_stat_activity where usename=$1' using res; --断开当前被锁定用户会话
raise notice 'Account % is locked!',res;
end loop;
end;
$$ language plpgsql strict;

4、编辑postgresql.conf文件,配置登录函数

session_preload_libraries='session_exec'
session_exec.login_name='lock_user'

5、测试
模拟test1用户登录错误超过3次:

bill=# select * from t_login;
         login_time         | user_name | flag 
----------------------------+-----------+------
 2020-08-26 07:26:45.42+08  | test1     |    1
 2020-08-26 07:26:50.179+08 | test1     |    1
 2020-08-26 07:26:52.487+08 | test1     |    1
 2020-08-26 07:26:54.537+08 | test1     |    1
(4 rows)

当我们在使用test1用户登录时则无法连接

pg13@cnndr4pptliot-> psql bill test1
Password for user test1: 
NOTICE:  c1 = <NULL>
psql: error: could not connect to server: FATAL:  terminating connection due to administrator command
CONTEXT:  SQL statement "select pg_terminate_backend(pid) from pg_stat_activity where usename=$1"
PL/pgSQL function lock_user() line 13 at EXECUTE

再次登录可以看到提示该用户被锁定:

pg13@cnndr4pptliot-> psql bill test1
Password for user test1: 
psql: error: could not connect to server: FATAL:  role "test1" is not permitted to log in

6、解锁用户
此时想要解锁该用户则需要执行:

bill=# alter user test1 login;
ALTER ROLE

然后需要注意还要将t_login中过期的数据修改。

bill=# update t_login set flag = 0;
UPDATE 4

参考链接:
https://github.com/okbob/session_exec

猜你喜欢

转载自blog.csdn.net/weixin_39540651/article/details/108227188