PostgreSQL用户登录失败自动锁定的处理方案

 更新时间:2021年03月21日 08:20:58   作者:寰宇001  
这篇文章主要介绍了PostgreSQL用户登录失败自动锁定的解决办法,本文给大家分享解决方案,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

墨墨导读: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_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
) SERVER pglog 
OPTIONS ( program 'find /opt/pg_log_5432 -type f -name "*.csv" -mtime -1 -exec cat {} \;', format 'csv' );

注意:
1./opt/pg_log_5432需要修改为实际环境日志目录。
2. 不同PG版本csv日志格式可能有所差异,参考PG官网文档runtime-config-logging章节(http://postgres.cn/docs/12/runtime-config-logging.html)。

此时连接数据库因未创建登录函数会出现下面的警告信息。

$ psql -Upostgres
WARNING: function "login()" does not exist
psql (12.5)
Type "help" for help.

创建登录函数login。

create or replace function login() returns void as $$
declare
res text;
c1 timestamp(3) with time zone;
begin

--获取当前日志中最新时间
select login_time 
from public.t_login 
where flag = 0 
order by login_time 
desc limit 1 
into c1; 

 --将最新的数据插入t_login表
insert into public.t_login 
select log_time,user_name 
from public.postgres_log 
where command_tag='authentication' 
and error_severity= 'FATAL' 
and log_time > c1;

update public.t_login set flag = 1 where login_time > c1; 

--检查登录失败次数是否大于3,若大于3则锁定用户
for res in select user_name from public.t_login where flag = 1 group by user_name having count(*) >=3 
loop
--锁定用户
EXECUTE format('alter user %I nologin',res); 
--断开当前被锁定用户会话
EXECUTE 'select pg_catalog.pg_terminate_backend(pid) from pg_catalog.pg_stat_activity where usename=$1' using res; 
raise notice 'Account % is locked!',res;
end loop;
end;
$$ language plpgsql strict security definer set search_path to 'public';

测试使用篇

创建测试用户。

create user test1 encrypted password 'XXX';

模拟test1用户登录失败,输入错误密码。

$ psql -h192.168.137.11 -Utest1 postgres
Password for user test1: 
psql: error: FATAL: password authentication failed for user "test1"

通过外部表查看登录失败的日志。

select * from postgres_log where command_tag='authentication' and error_severity= 'FATAL';

可以看到1条数据,手工插入一条登录失败的信息到t_login表。

insert into t_login select log_time,user_name,0
 from postgres_log 
 where command_tag='authentication' 
 and error_severity= 'FATAL';

参考上面登录失败测试,接着再测试2次。

然后使用postgres用户登录数据库,观察t_login表数据。

postgres=# select * from t_login;
  login_time  | user_name | flag 
-------------------------+-----------+------
 2021-02-08 06:24:47.101 | test1  | 0
 2021-02-08 06:25:16.581 | test1  | 1
 2021-02-08 06:25:18.429 | test1  | 1
(3 rows)

再测试两次失败登录,然后使用postgres用户登录数据库,看到提示该用户被锁定。

[postgres@node11 ~]$ psql
NOTICE: Account test1 is locked!
psql (12.5)
Type "help" for help.

postgres=# select * from t_login;
  login_time  | user_name | flag 
-------------------------+-----------+------
 2021-02-08 06:45:38.017 | test1  | 0
 2021-02-08 06:45:58.809 | test1  | 1
 2021-02-08 06:45:58.809 | test1  | 1
 2021-02-08 06:46:08.116 | test1  | 1
 2021-02-08 06:46:11.986 | test1  | 1
(5 rows)

解锁用户。

update t_login set flag = 0 where user_name='test1' and flag=1;

总结

  • session_exec通过用户登录成功后调用login函数去实现锁定登录失败次数过多的用户。
  • 此种方式有点繁琐且会造成数据库连接变慢。
  • 不支持自动解锁,需要管理用户手工处理。

参考链接:

https://www.jb51.net/article/208018.htm

到此这篇关于PostgreSQL用户登录失败自动锁定的解决办法的文章就介绍到这了,更多相关PostgreSQL登录失败自动锁定内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • PostgreSQL 存储过程的进阶讲解(含游标、错误处理、自定义函数、事务)

    PostgreSQL 存储过程的进阶讲解(含游标、错误处理、自定义函数、事务)

    PL/pgSQL 游标允许我们封装一个查询,然后每次处理结果集中的一条记录,这篇文章主要介绍了PostgreSQL 存储过程的进阶介绍(含游标、错误处理、自定义函数、事务),需要的朋友可以参考下
    2023-03-03
  • pg中replace和translate的用法说明(数据少的中文排序)

    pg中replace和translate的用法说明(数据少的中文排序)

    这篇文章主要介绍了pg中replace和translate的用法说明(数据少的中文排序),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • 在Linux系统上安装PostgreSQL数据库

    在Linux系统上安装PostgreSQL数据库

    这篇文章介绍了在Linux系统上安装PostgreSQL数据库的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • PostgreSQL数据库管理系统快速入门

    PostgreSQL数据库管理系统快速入门

    这篇文章主要介绍了PostgreSQL数据库快速入门,PostgreSQL是一个功能强大的开源对象关系型数据库系统,他使用和扩展了SQL语言,并结合了许多安全存储和扩展最复杂数据工作负载的功能,需要的朋友可以参考下
    2023-07-07
  • PostgreSQL常用优化技巧示例介绍

    PostgreSQL常用优化技巧示例介绍

    PostgreSQL的SQL优化技巧其实和大多数使用CBO优化器的数据库类似,因此一些常用的SQL优化改写技巧在PostgreSQL也是能够使用的。当然也会有一些不同的地方,今天我们来看看一些在PostgreSQL常用的SQL优化改写技巧
    2022-09-09
  • 使用PostgreSQL数据库进行中文全文搜索的实现方法

    使用PostgreSQL数据库进行中文全文搜索的实现方法

    目前在PostgreSQL中常见的两个中文分词插件是zhparser和pg_jieba,这里我们使用zhparser,插件的编译和安装请查看官方文档 ,安装还是比较复杂的,建议找个现成docker镜像,本文给大家介绍了在PostgreSQL数据库使用中文全文搜索,需要的朋友可以参考下
    2023-09-09
  • Mac系统重置PostgreSQL密码的方法示例代码

    Mac系统重置PostgreSQL密码的方法示例代码

    PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行。这篇文章主要介绍了Mac系统重置PostgreSQL密码的方法示例代码,需要的朋友可以参考下
    2020-02-02
  • PostgreSQL 如何修改文本类型字段的存储方式

    PostgreSQL 如何修改文本类型字段的存储方式

    这篇文章主要介绍了PostgreSQL 如何修改文本类型字段的存储方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • PostgreSQL 查看服务器版本的三种方法

    PostgreSQL 查看服务器版本的三种方法

    这篇文章主要介绍了PostgreSQL 查看服务器版本的几种方法,本文给大家分享三种方法,结合实例代码给大家讲解的非常详细,需要的朋友可以参考下
    2023-03-03
  • PostgreSQL时间相差天数代码实例

    PostgreSQL时间相差天数代码实例

    PostgreSQL是一款简介而又性能强大的数据库应用程序,其在日期时间数据方面所支持的功能也都非常给力,这篇文章主要给大家介绍了关于PostgreSQL时间相差天数的相关资料,需要的朋友可以参考下
    2023-11-11

最新评论