PostgreSQL使用session_exec和file_fdw实现失败次数锁定用户策略

news/2024/7/9 20:46:35 标签: postgresql, 数据库

使用session_exec 、file_fdw以及自定义函数实现该功能。

缺陷:实测发现锁用户后,进去解锁特定用户。只能允许一次登陆,应该再次登陆的时候,触发函数,把之前的日志里的错误登陆的信息也计算到登录次数里了。而且foreign table不能在数据库里清理。需要删除对应的pg_log,才能使foreign table信息清理掉,来重制该用户的密码错误记录。

https://github.com/okbob/session_exec

unzip session_exec-master.zip
cd session_exec-master/
make pg_config=$PGHOME/bin/pg_config
make pg_config=$PGHOME/bin/pg_config install

更改配置文件

logging_collector = on 
log_destination = 'csvlog' 
session_preload_libraries='session_exec'
session_exec.login_name='login'

     更改完之后需要重启数据库

pg_ctl restart

安装file_fdw扩展

cd contrib/file_fdw/
# 安装命令
make && make install

创建外部表postgres_log

create extension file_fdw;

CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;

CREATE FOREIGN TABLE public.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 /home/pg13/data/log/ -type f -name "*.csv" -mtime -1 -exec cat {} \;', format 'csv' );

grant SELECT on postgres_log to PUBLIC;

如下设置的是5 5次登录失败就锁定用户

create or replace function public.login() returns void as $$
declare
res record;
failed_login_times int = 5;
failed_login int = 0;
begin
--获取数据库中所有可连接数据库的用户
for res in select rolname from pg_catalog.pg_roles where rolcanlogin= 't' and rolname !='postgres'
loop
  raise notice 'user: %!',res.rolname;
  --获取当前用户最近连续登录失败次数
  select count(*)
  from (select log_time,user_name,error_severity,message,detail from public.postgres_log where command_tag = 'authentication' and user_name = res.rolname and (detail is null or detail not like 'Role % does not exist.%') order by log_time desc limit failed_login_times) A
  WHERE A.error_severity='FATAL'
  into  failed_login ;
  raise notice 'failed_login_times: %! failed_login: %!',failed_login_times,failed_login;
  --用户最近密码输入错误次数达到5次或以上
  if failed_login >= failed_login_times then
    --锁定用户
    EXECUTE format('alter user %I nologin',res.rolname);
    raise notice 'Account % is locked!',res.rolname;
  end if;
end loop;
end;
$$ language plpgsql strict security definer set search_path to 'public';

用户是否被锁/是否允许登陆,可以查看pg_roles系统视图里的rolcanlogin字段。

验证密码失败:

使用如下语句,可以查询登陆失败的记录。

select log_time,user_name,error_severity,message,detail from public.postgres_log where command_tag = 'authentication'  order by log_time desc limit 10 offset 0;

解锁用户命令如下:

alter user test_user login;

解锁同时需要删除pg_log下csv文件里的对应有改用户登陆失败的日志记录,重制密码登录错误的记录,否则可能会重复计算之前的错误登陆记录。


http://www.niftyadmin.cn/n/5382924.html

相关文章

Ubuntu/WSL下生产密钥脚本

说明: 有时候需要为开发人员配发密钥,为方便写了个小脚本,在linux下运行,要求 python10, putty-tools。 使用时,在staffList定义用户列表,运行后程序自动产生对应目录及密钥。 安装: apt inst…

rust gui fltk

FLTK 图形用户界面库的 Rust 绑定。 fltk crate 是一个跨平台的轻量级 GUI 库,可以静态链接以生成小型、独立且快速的 GUI 应用程序。 doc https://www.rust-lang.org/zh-CN/learn/get-started https://docs.rs/fltk/latest/fltk/ install $ curl --proto http…

AnyText: 多语言视觉文本生成与编辑

AnyText: 多语言视觉文本生成与编辑 论文介绍 Anytext: Multilingual Visual Text Generation and Editing 关注微信公众号: DeepGoAI 项目地址:https://github.com/tyxsspa/AnyText(已经3.3k) 论文地址:https://arxiv.org/ab…

基于Java的车辆租赁管理平台/租车系统

功能介绍 平台采用B/S结构,后端采用主流的Springboot框架进行开发,前端采用主流的Vue.js进行开发。 整个平台包括前台和后台两个部分。 前台功能包括:首页、车辆详情、车辆预订、用户中心模块。后台功能包括:车辆管理、分类管理…

leetcode刷题记录:二叉树02(思路篇)

参考labuladong的算法小抄:https://labuladong.online/algo/data-structure/binary-tree-part1/ 复习二叉树纲领篇,二叉树解题的思维模式分两类: 1、是否可以通过遍历一遍二叉树得到答案?如果可以,用一个 traverse 函…

STM32 USART详细解读(理论知识)

文章目录 前言一、同步传输和异步传输二、UART协议三、UART硬件结构1.波特率,数据位,校验位,停止位设置2.数据发送流程3.数据接收流程4.中断控制 总结 前言 本篇文章来给大家讲解一下STM32中的USART,USART是STM32中非常重要的一个…

基于SpringBoot+Dubbo构建的电商平台-微服务架构、商城、电商、微服务、高并发、kafka、Elasticsearc+源代码+文档说明

文章目录 项目用到的技术前端使用的技术后端使用的技术项目模块说明项目搭建方式项目开发进度源码下载地址 项目基于springboot2.1.6.RELEASEDubbo2.7.3 来构建微服务。 业务模块划分,尽量贴合互联网公司的架构体系。所以,除了业务本身的复杂度不是很高之…

unity学习(28)——登录功能

有之前注册的知识,登录就很容易处理了。 登陆成功返回id: 登录失败返回null: 测试同一账号不能重复登陆!登录成功后最好可以跳到新的场景中 结果是好的,去服务器看一下对应部分的代码,可见,登…