.. _security_utils:

==============
security_utils
==============


插件security_utils简介
-----------------------

security_utils是KingbaseES的一个扩展插件，主要用于支持一些安全方面的功能，目前支持的功能有日志密码脱敏，弱口令扫描。

- 插件名为 security_utils

- 插件版本 V1.0


插件security_utils加载方式
---------------------------

在使用 security_utils 之前，我们需要将他添加到 kingbase.conf 文件的 shared_preload_libraries 中，并重启 KingbaseES 数据库。

示例：

.. code:: 

    shared_preload_libraries = 'security_utils' 


插件security_utils的参数配置
----------------------------

``security_utils.password_masking_enable``
  启动日志密码脱敏功能，取值范围为 on 或者 off，缺省值为 off。

.. code:: 

    show security_utils.password_masking_enable;
     security_utils.password_masking_enable
    ----------------------------------------
     off
    (1 row)

    \c - system
    alter system set security_utils.password_masking_enable = on;
    select sys_reload_conf();
    show security_utils.password_masking_enable;
     security_utils.password_masking_enable
    ----------------------------------------
     on
    (1 row)
    

``security_utils.weak_pwd_check_enable`` 
   弱口令检查功能，取值范围为 on 或者 off，默认为关闭状态。


插件security_utils使用方法
----------------------------

口令脱敏
^^^^^^^^^^^^^^^^^^^^

KingbaseES加载security_utils插件，并修改了参数后，当开启DDL日志时，日志中涉及密码的SQL语句可以进行脱敏处理。

**示例**

.. code::

    ---开启密码脱敏
    \c - system
    create extension kdb_schedule;
    create extension security_utils;
    alter system set security_utils.password_masking_enable=on;
    select sys_reload_conf();

    ---设置日志级别
    alter system set log_statement='ddl';
    select sys_reload_conf();

    ---执行user的包含密码的ddl语句
    drop user testpwdtmu1;
    create user testpwdtmu1 with password '12345678ab';
    alter user testpwdtmu1 with password '12345678abc';
    drop user testpwdtmu1;

    ---查看sys_log下的日志文件，上述语句password脱敏，以*显示
    ERROR:  role "testpwdtmu1" does not exist
    STATEMENT:  drop user testpwdtmu1;
    LOG:  statement: create user testpwdtmu1 with password '**********';
    LOG:  statement: alter user testpwdtmu1 with password '***********';
    LOG:  statement: drop user testpwdtmu1;

    ---清空环境
    alter system set security_utils.password_masking_enable=off;
    select sys_reload_conf();
    drop extension security_utils;


弱口令扫描
^^^^^^^^^^^^^^^^^^^^

**弱口令字典配置语法**

.. code::

   --设置弱口令字典
   set weak password [if not exists] 'wpwd1'[, 'wpwd2'...];
   --删除弱口令字典
   remove weak password [if exists] 'wpwd1'[, 'wpwd2'...];

**说明**

-  可以同时创建或删除多个弱口令，使用方法见上配置语法。

-  弱口令字符串类型为text，不限制长度，且配置语法不限制弱口令个数。

-  当存在if not exists或if exists，将忽略已存在或不存在的弱口令，避免报错，否则会报错提示。

-  不允许输入的弱口令为null，例如 set weak password null。

-  输入的弱口令为空字符串时，例如 set weak password ''，处理方法与第三点相同。

-  弱口令扫描支持三种口令加密方式，包括 sha256、md5、sm3 。

.. tip::

   1.当弱口令扫描与口令复杂度功能同时开启时，且检查的口令既属于弱口令，又不符合口令复杂度要求，报错提示的内容与两个功能的加载顺序相关，报错提示会显示写在 shared_preload_libraries 中靠前的插件的提示内容。

   2.当数据库中已创建了弱口令扫描的扩展 security_utils ，请使用数据库管理员进行备份恢复以避免报错导致备份恢复失败。


**示例**

开启弱口令扫描功能：

.. code::

  shared_preload_libraries = 'security_utils'

  \c - system
  create extension security_utils;

  show security_utils.weak_pwd_check_enable;
  security_utils.weak_pwd_check_enable
  ------------------------------------
  off
  (1 行记录)

  alter system set security_utils.weak_pwd_check_enable = on;

  select sys_reload_conf();
  sys_reload_conf
  -----------------
  t
  (1 行记录)

  show security_utils.weak_pwd_check_enable;
  security_utils.weak_pwd_check_enable
  ------------------------------------
  on
  (1 行记录)



在功能开启后，数据库管理员system登录数据库后，可以进行弱口令字典的配置：

.. code::

   \c - system
   set weak password '123qwe';--成功
   set weak password '123qwe','1234asdf';--失败，重复弱口令报错
   错误:  weak passwd dic "123qwe" already exists
   set weak password if not exists '123qwe','1234asdf';--成功，忽略已存在的弱口令

   --查看弱口令配置
   select * from security_utils.weak_pwd_dic;
    weak_passwd |         created_time
   -------------+-------------------------------
    123qwe      | 2022-08-01 15:11:21.102412+08
    1234asdf    | 2022-08-01 15:12:52.640116+08
   (2 rows)

创建新用户u1，使用弱口令作为口令：

.. code::

   \c - system
   create user u1 with password '123qwe';--失败，口令不符合弱口令检查
   错误:  password is too weak!
   create user u1 with password '123qwer';--成功
   alter user u1 with password '123qwe';
   错误:  password is too weak!--失败，口令不符合弱口令检查

数据库管理员进行弱口令扫描：

.. code::

   \c - system
   --将u1用户的口令设置为弱口令字典
   set weak password '123qwer';

   --进行弱口令扫描
   select * from security_utils.weak_passwd_scan_result;
    username | weak_passwd
   ----------+-------------
    u1       | 123qwer
   (1 row)

数据库管理员删除弱口令配置：

.. code::

   \c - system
   --查看现有弱口令配置
   select * from security_utils.weak_pwd_dic;
    weak_passwd |         created_time
   -------------+-------------------------------
    123qwe      | 2022-08-01 15:11:21.102412+08
    1234asdf    | 2022-08-01 15:12:52.640116+08
    123qwer     | 2022-08-01 15:22:12.404342+08
   (3 rows)

   --删除弱口令配置
   remove weak password '123qwe';--成功
   remove weak password '123qwe','1234asdf','123qwer';--失败，不存在的弱口令报错
   错误:  weak passwd dic "123qwe" does not exists
   remove weak password if exists '123qwe','1234asdf','123qwer';--成功，忽略不存在的弱口令

   --查看弱口令字典
   select * from security_utils.weak_pwd_dic;
    weak_passwd | created_time
   -------------+--------------
   (0 rows)

相关函数
----------------------------

check_guc_in_cluster()
^^^^^^^^

**功能**

在集群环境中检查主备机之间安全相关guc参数是否相等。

**返回值**

返回bool型。

返回‘t’：表示guc一致，或者没有备机服务器；

返回‘f’：表示主备机guc不一致、不是在esrep数据库中、不是主机服务器，或者无repmgr.nodes表。


set_guc_in_cluster()
^^^^^^^^

**功能**

在集群环境中将主机安全相关guc参数，设置到各个备机服务器中。

**返回值**

返回bool型。

返回‘t’：表示guc设置成功；

返回‘f’：表示不是在esrep数据库中、不是主机服务器，或者无repmgr.nodes表。

**注意**

1）如果无法检查主备机之间的guc参数，则会在日志中记录command failed:xxxx。不影响程序查看比较后面的guc参数。

2）建议主备机配置文件shared_preload_libraries相同。

.. _定时任务:

定时任务
~~~~~~~~~~~~~

集群在部署阶段设置sync_security_guc参数：

如果设置为yes，则会在部署集群的时候创建kdb_schedule、security_utils这两个插件，并配置相关guc参数。集群环境通过kdb_schedule定时调用security_utils.set_guc_in_cluster函数，可实现集群数据库服务器自动同步主备机安全guc参数。

如果设置为no，则不执行任何操作。默认值为no。


插件security_utils卸载方法
----------------------------

通过 drop extension 来卸载插件：

.. code:: 

  drop extension security_utils;


如果设置了 shared_preload_libraries，则需要删除 'security_utils'：

.. code:: 

  shared_preload_libraries = ''


插件security_utils升级方法
----------------------------

security_utils扩展插件随着KingbaseES安装包一并升级。但请注意，V008R006C006B0021版本之前的版本，当使用sys_upgrade升级后无法正常使用弱口令功能时，请删除扩展并重新创建。
