【原创】简易数据库访问授权控制脚本

简易数据库访问授权控制脚本



2019-06-05 09:45:06

  一、思路

  本人比较懒,没有搭建例如Jumpserver之类的开源堡垒机,但领导又想控制数据库的访问权限,于是利用ssh的密钥文件白名单授权登录的方法控制用户在一定时间内可使用跳板机登录数据库加上计划任务来清理过期用户的授权信息以阻止用户登录跳板机,用户登录后有条件的可以下载sql审计文件来查看用户的执行操作。

  二、流程图

 

  三、代码

#!/bin/bash
#source /etc/profile
:<<EOF
在计划任务里添加下面范例,"-1"为授权文件失效时间,"name"为授权人姓名目录名
*/5 * * * * /home/jump/sql.sh -1 name > /dev/null 2>&1
EOF
#失效天数
old=$1
#授权人
name=$2
#当前时间
date_time=`date +"%Y-%m-%d %H:%M:%S"`
#公钥文件目录
key_pub_dir=/home/jump/.ssh/${name}
#白名单文件
ssh_key_file=/home/jump/.ssh/authorized_keys

if [ ! -n "$old" ] || [ ! -n "$name" ];then
        echo "请输入过期时间和授权人目录名"
        exit 0
else
        end_file=`find ${key_pub_dir} -name "*.pub" -mtime ${old}`
if [ -f "${end_file}" ];then
        #echo "find ${key_pub_dir} -name "*.pub" -mtime ${old}" >> delete.log 
for i in `ls ${end_file}`;do
        echo "======================task start======================" >> delete.log
        echo "" >> delete.log
        echo "${date_time} 发现过期授权文件" >> delete.log
        ls -ctl ${i} >> delete.log
        echo "" >> delete.log
        echo "${date_time} 正在删除过期授权文件信息" >> delete.log
        sed -i "s#`cat ${i}`##g" ${ssh_key_file} >> delete.log
        key_wc=`cat ${ssh_key_file} | grep cat ${i} | wc -l`
if [ ${key_wc} -le 1 ];then
        echo "${date_time} 删除授权信息成功" >> delete.log
else
        echo "${date_time} 删除授权信息失败,请手动删除${ssh_key_file}文件内的授权信息" >> delete.log
fi
#        rm -rf ${i} >> delete.log
        echo "" >> delete.log
        echo "${date_time} 删除授权信息后的${ssh_key_file}内容:" >> delete.log
        cat ${ssh_key_file} >> delete.log
        echo "======================task end========================" >> delete.log
done
else
        echo "======================task start======================" >> delete.log
        echo "${date_time} 无过期授权文件" >> delete.log
        echo "======================task end========================" >> delete.log
fi
fi

   以上是实现的所有代码,配合计划任务每5分钟执行一次扫描,只要过期就删除,这样用户没有了跳板机的权限也就不能登录数据库了。如有不足之处请多指教。

猜你喜欢

转载自www.cnblogs.com/quguoliang2018/p/10977955.html
今日推荐