2

postgresql_anonymizer使用

 1 year ago
source link: https://blog.51cto.com/u_13646489/5987049
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

postgresql_anonymizer使用

精选 原创

瀚高实验室 2023-01-04 10:16:14 博主文章分类:PostgreSQL通用知识 ©著作权

文章标签 数据 敏感数据 数据库 文章分类 MySQL 数据库 阅读数183

 瀚高数据库

系统平台:Linux x86-64 Red Hat Enterprise Linux 7

版本:12

postgresql_anonymizer是对数据库中的个人识别信息或商业敏感数据进行屏蔽或替换的扩展。

postgresql_anonymizer是对数据库中的个人识别信息或商业敏感数据进行屏蔽或替换的扩展。该扩展使用标准sql语句定义规则,内置多种屏蔽规则函数。依据定义的规则有3中使用方式

  • Anonymous Dumps : 将屏蔽数据导出到SQL文件中
  • Static Masking : 根据规则移除替换敏感数据(此方法慎用,避免数据被替换而造成丢失。)
  • Dynamic Masking : 依据规则屏蔽隐藏敏感数据

支持多种安装方式,包括rpm,pgxn,docker等。建议使用源码安装方式。

下载,编译

git clone https://gitlab.com/dalibo/postgresql_anonymizer.git
make extension PG_CONFIG=/opt/pg1211/bin/pg_config
sudo make install PG_CONFIG=/opt/pg1211/bin/pg_config

postgresql_anonymizer使用_数据_02

配置加载扩展

ALTER DATABASE postgres SET session_preload_libraries = 'anon';

pg_ctl restart --重启生效

postgresql_anonymizer使用_敏感数据_04

CREATE EXTENSION anon CASCADE;create extension pgcrypto ;

postgresql_anonymizer使用_数据库_06

初始化扩展

SELECT anon.init();

postgresql_anonymizer使用_敏感数据_08

用于声明屏蔽规则的函数必须位于指定的模式内,默认是pg_catalog和anon

ALTER DATABASE postgres SET anon.restrict_to_trusted_schemas = on;

postgresql_anonymizer使用_数据库_10

声明屏蔽规则,数据屏蔽规则仅通过使用security labels来声明

声明屏蔽规则(MASKED WITH FUNCTION需要大写)

security label for anon on column test_mask.name is 'MASKED WITH FUNCTION anon.fake_last_name()';

删除屏蔽规则

security label for anon on column test_mask.name is null;

删除所有规则

SELECT anon.remove_masks_for_all_columns();

postgresql_anonymizer使用_数据库_12

4.屏蔽策略介绍

共8种。参考官方文档描述 ​ ​Masking Functions​

  • Destruction
  • Adding Noise #是数据进行一定幅度的变化。对于数值和日期,Adding Noise通常很有趣
  • Randomization
  • Faking #使用随机但看似合理的数据替换敏感数据。对于姓名和其他“直接标识符”,Faking通常很有用
  • Advanced Faking
  • Pseudonymization
  • Generic Hashing
  • Partial scrambling #对部分数据进行遮挡。非常适合用于电子邮件地址和电话号码
  • Generalization

Faking

为了使用faking函数,必须先加载init()扩展。

SELECT anon.init();

返回通用的名字

security label for anon on column test_mask.name is 'MASKED WITH FUNCTION anon.fake_first_name()';

postgresql_anonymizer使用_数据_14

Adding Noise

返回的值是原始值随机+/-20%

security label for anon on column test_mask.salary is 'MASKED WITH FUNCTION anon.noise(original_value,0.2)';

返回的值是原始值随机+/-7天

security label for anon on column test_mask.hiredate is 'MASKED WITH FUNCTION anon.dnoise(original_value,7 days)';

postgresql_anonymizer使用_数据库_16

Partial scrambling

返回值显示后四位,其他以xxxx代替

security label for anon on column test_mask.telephone is 'MASKED WITH FUNCTION anon.partial(telephone,2,$$*****$$$$,4)';

postgresql_anonymizer使用_敏感数据_18

5. Static Masking静态屏蔽使用

永久删除敏感数据

应用屏蔽规则,对整个数据库

SELECT anon.anonymize_database();

应用屏蔽规则,对指定表

SELECT anon.anonymize_table('public.test_mask');

应用屏蔽规则,对指定列

SELECT anon.anonymize_column('customer','zipcode');

postgresql_anonymizer使用_敏感数据_20

注意,数据会被替换,适用于测试数据脱敏。

6. Dynamic Masking动态屏蔽使用

对“屏蔽”用户隐藏敏感数据

开启动态屏蔽

SELECT anon.start_dynamic_masking();

声明屏蔽用户

SECURITY LABEL FOR anon ON ROLE test IS 'MASKED';

解除用户屏蔽

SECURITY LABEL FOR anon ON ROLE bob IS NULL;

解除所有用户屏蔽

SELECT anon.remove_masks_for_all_roles();

postgresql_anonymizer使用_数据_22

动态屏蔽使用限制

  • drop表需要使用CASCADE
  • psql命令\dt 无法显示被屏蔽表信息
  • 只能屏蔽一个schema模式下的数据,默认是public,可修改为其他shema,但只能屏蔽一个模式
  • 会使查询性能非常低,特别是join表时
  • 使用图形化工具是,访问屏蔽表信息会报错,ERROR: permission denied for table foo

7. Anonymous Dumps导出屏蔽数据

由于屏蔽设置,不能使用pg_dump导出数据。需要使用pg_dump_anon.sh

pg_dump_anon.sh -h localhost -U postgres -d postgres -t test_dy_mask > /tmp/test_dy_mask_anon.sql

postgresql_anonymizer使用_数据_24

8. 相关字典试图

  • pg_seclabels
  • pg_seclabel

9. 使用示例

创建表

create table test_dy_mask (id int,name varchar(22),salary int,hiredate timestamp,telephone text);

insert into test_dy_mask values (1,'max',20000,'2022-06-21 14:00:00','15512345678');

postgres=# select * from test_dy_mask ;

id | name | salary | hiredate | telephone

----+------+--------+---------------------+-------------

1 | aaa | 20000 | 2022-06-21 14:00:00 | 15512345678

(1 row)

声明屏蔽规则

SELECT anon.init(); --使用faking

security label for anon on column test_dy_mask.name is 'MASKED WITH FUNCTION anon.fake_first_name()'; --faking

security label for anon on column test_dy_mask.salary is 'MASKED WITH FUNCTION anon.noise(salary,0.2)'; --add noise

security label for anon on column test_dy_mask.telephone is 'MASKED WITH FUNCTION anon.partial(telephone,2,$$*****$$,4)'; --Partial scrambling

使用动态屏蔽

SELECT anon.start_dynamic_masking(); --开启动态屏蔽

create user test with password 'test'; --创建一个新用户

SECURITY LABEL FOR anon ON ROLE test IS 'MASKED'; --声明屏蔽用户

grant select on test_dy_mask to test; --授权

select * from test_dy_mask; --使用屏蔽用户查询数据

查看结果

postgres=> select * from test_dy_mask ;

id | name | salary | hiredate | telephone

----+------+--------+---------------------+-------------

1 | Koby | 16618 | 2022-06-21 14:00:00 | 15*****5678

(1 row)

postgresql_anonymizer使用_数据_26

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK