PostgreSQL

一、插件session_exec安装配置篇

下载插件并编译安装。
https://github.com/okbob/session_exec

  1. $ unzip session_exec-master.zip
  2. $ cd session_exec-master/
  3. $ make pg_config=/opt/pgsql/bin/pg_config
  4. $ make pg_config=/opt/pgsql/bin/pg_config install

配置postgresql.conf。

  1. session_preload_libraries='session_exec'
  2. session_exec.login_name='login'

:::tips 注意:上面第一个变量是设置session_preload_libraries而不是通常设置的shared_preload_libraries。 ::: 第二个变量是需要自定义实现的登录函数。
重启数据库服务。

  1. $ sudo systemctl restart postgresql-12

二、自定义登录函数

创建t_login表用于存储提取自数据库日志中登录失败的信息。

  1. create table t_login
  2. (
  3. login_time timestamp(3) with time zone --插入时间,
  4. user_name text --数据库登录用户,
  5. flag int4 --标志位,0代表过期数据,1代表正常状态数据
  6. );

使用file_fdw外部表记录数据库日志信息。
file_fdw如果未配置过,参见下面步骤。

  1. $ cd /opt/postgresql-12.5/contrib/file_fdw
  2. $ make && make install
  1. create extension file_fdw;
  2. CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

建立外部表postgres_log,关联数据库日志中登录失败的信息。

  1. CREATE FOREIGN TABLE postgres_log(
  2. log_time timestamp(3) with time zone,
  3. user_name text,
  4. database_name text,
  5. process_id integer,
  6. connection_from text,
  7. session_id text,
  8. session_line_num bigint,
  9. command_tag text,
  10. session_start_time timestamp with time zone,
  11. virtual_transaction_id text,
  12. transaction_id bigint,
  13. error_severity text,
  14. sql_state_code text,
  15. message text,
  16. detail text,
  17. hint text,
  18. internal_query text,
  19. internal_query_pos integer,
  20. context text,
  21. query text,
  22. query_pos integer,
  23. location text,
  24. application_name text
  25. ) SERVER pglog
  26. 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)。

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

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

创建登录函数login。

  1. create or replace function login() returns void as $$
  2. declare
  3. res text;
  4. c1 timestamp(3) with time zone;
  5. begin
  6. --获取当前日志中最新时间
  7. select login_time
  8. from public.t_login
  9. where flag = 0
  10. order by login_time
  11. desc limit 1
  12. into c1;
  13. --将最新的数据插入t_login
  14. insert into public.t_login
  15. select log_time,user_name
  16. from public.postgres_log
  17. where command_tag='authentication'
  18. and error_severity= 'FATAL'
  19. and log_time > c1;
  20. update public.t_login set flag = 1 where login_time > c1;
  21. --检查登录失败次数是否大于3,若大于3则锁定用户
  22. for res in select user_name from public.t_login where flag = 1 group by user_name having count(*) >=3
  23. loop
  24. --锁定用户
  25. EXECUTE format('alter user %I nologin',res);
  26. --断开当前被锁定用户会话
  27. EXECUTE 'select pg_catalog.pg_terminate_backend(pid) from pg_catalog.pg_stat_activity where usename=$1' using res;
  28. raise notice 'Account % is locked!',res;
  29. end loop;
  30. end;
  31. $$ language plpgsql strict security definer set search_path to 'public';

三、测试使用

创建测试用户。

  1. create user test1 encrypted password 'XXX';

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

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

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

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

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

  1. insert into t_login select log_time,user_name,0
  2. from postgres_log
  3. where command_tag='authentication'
  4. and error_severity= 'FATAL';

参考上面登录失败测试,接着再测试2次。
然后使用postgres用户登录数据库,观察t_login表数据。

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

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

  1. [postgres@node11 ~]$ psql
  2. NOTICE: Account test1 is locked!
  3. psql (12.5)
  4. Type "help" for help.
  5. postgres=# select * from t_login;
  6. login_time | user_name | flag
  7. -------------------------+-----------+------
  8. 2021-02-08 06:45:38.017 | test1 | 0
  9. 2021-02-08 06:45:58.809 | test1 | 1
  10. 2021-02-08 06:45:58.809 | test1 | 1
  11. 2021-02-08 06:46:08.116 | test1 | 1
  12. 2021-02-08 06:46:11.986 | test1 | 1
  13. (5 rows)

解锁用户。

  1. alter user test1 login;

同时清空登录失败的标记位。

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

总结

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