1. 程式人生 > 其它 >KingbaseES 資料脫敏功能

KingbaseES 資料脫敏功能

資料脫敏,指對某些敏感資訊通過脫敏規則進行資料的變形,實現敏感隱私資料的可靠保護。
KingbaseES主要提供動態資料脫敏功能。
動態資料脫敏(Dynamic Data Masking)是與生產環境緊密關聯的,訪問敏感資料時實時地進行脫敏,主要用於直接訪問生產資料的場景,在遮蔽敏感資訊的同時也保證了源資料的一致性和有效性。

功能開啟

  1. 修改配置檔案kingbase.conf 檔案中shared_preload_libraries引數,增加資料脫敏外掛 sys_anon
  2. 重啟資料庫。
  3. 以sso使用者登入資料庫開啟脫敏開關。
    ALTER SYSTEM SET anon.enable = on; CALL sys_reload_conf();

資料脫敏策略配置
1.新增脫敏策略

anon.add_policy(policy_name text,
objname text,
username text,
func_desc text,
para_list text
)

引數說明:
policy_name 脫敏策略名,不可為空,策略名唯一。
objname 待脫敏的物件,不可為空,格式為:模式. 表. 列(不寫模式時為表. 列,模式預設設定為public)。
username 待脫敏的物件使用者名稱,可為空,為空對所有使用者脫敏。
func_desc 脫敏使用的函式(只需要填寫函式名),可為空,為空設定為預設脫敏。支援使用的脫敏函式詳見表脫敏函式說明。
para_list 脫敏函式的引數列表(目前僅適用於部分脫敏),若函式無引數,則該項無效;若函式為部分脫敏時需要填寫,引數之間用‘,’分隔,例如’2,2’,引數列表個數為2。

2.修改脫敏策略

anon.alter_policy(policy_name text,
username text,
func_desc text,
para_list text
)

3.刪除脫敏策略

anon.remove_policy(policy_name text)

4.資料脫敏配置查詢
資料脫敏策略設定可通過系統檢視anon.all_policy查詢

5.脫敏函式

示例
1.建立測試使用者
create user u1 with password '123456';
2.建立測試表,插入資料

create table t1(id int,t_def boolean,t_str varchar(20),t_date timestamptz,t_int int,t_mail text, t_part text);
insert into t1 values (1,true,'kingbase',now(),5678,'[email protected]','datamasking');

3.授權,檢查脫敏開關是否已開啟

test=# grant select on t1 to u1;
GRANT
test=#  show anon.enable;   
 anon.enable 
-------------
 on
(1 行記錄)

test=> select * from t1;
 id | t_def |  t_str   |            t_date             | t_int |      t_mail      |   t_part    
----+-------+----------+-------------------------------+-------+------------------+-------------
  1 | t     | kingbase | 2022-05-17 17:36:03.872724+08 |  5678 | [email protected] | datamasking
(1 行記錄)

4.設定脫敏規則

\c - sso
select anon.add_policy('pol1','public.t1.t_def','u1','default','');
select anon.add_policy('pol2','public.t1.t_str','u1','random_string','');
select anon.add_policy('pol3','public.t1.t_date','u1','random_date','');
select anon.add_policy('pol4','public.t1.t_int','u1','random_int','');
select anon.add_policy('pol5','public.t1.t_mail','u1','email_mask','');
select anon.add_policy('pol6','public.t1.t_part','u1','partial','2,3');

5.切換至u1使用者查詢t1表

test=> \c - u1
您現在已經連線到資料庫 "test",使用者 "u1".
test=> select * from t1;                                                        
 id | t_def |  t_str   |            t_date             |   t_int   |      t_mail      |   t_part    
----+-------+----------+-------------------------------+-----------+------------------+-------------
  1 | f     | vj7VJxl9 | 1976-04-25 04:42:54.452940+08 | 850842047 | ***@********.com | da******ing
(1 行記錄)

6.注意事項

  1. 資料脫敏策略僅允許安全員sso 進行配置及檢視,其他使用者均無許可權。
  2. 一個脫敏物件(列)上只能配置一種脫敏策略。若是同一個脫敏物件上存在多個策略,則所有策略都不會生效。若要對多個使用者生效脫敏測試,新增策略時引數username請置為空。