--创建dev_mask
demo=# drop user dev_mask;
DROP ROLE
demo=#
demo=# create user dev_mask password 'Huawei@123';
CREATE ROLE
--创建一个表tb_for_masking
demo=# drop table tb_for_masking;
DROP TABLE
demo=#
demo=# create table tb_for_masking(col1 text,col2 text,col3 text);
CREATE TABLE
--插入测试数据
demo=# insert into tb_for_masking values('4880-9898-4545-2525','4880-9898-4545-2525','4880-9898-4545-2525');
INSERT 01
--读取测试数据
demo=#select*from tb_for_masking;
col1 | col2 | col3
---------------------+---------------------+---------------------
4880-9898-4545-2525|4880-9898-4545-2525|4880-9898-4545-2525
(1 row)
--创建资源标签标记敏感列col1
demo=# CREATE RESOURCE LABEL mask_lb1 ADD COLUMN(tb_for_masking.col1);
ERROR: mask_lb1 label already defined
--删除资源标签标记敏感列col1
demo=# DROP RESOURCE LABEL mask_lb1;
DROP RESOURCE LABEL
--创建资源标签标记敏感列col1
demo=# CREATE RESOURCE LABEL mask_lb1 ADD COLUMN(tb_for_masking.col1);
CREATE RESOURCE LABEL
--对访问敏感列col1的操作创建脱敏策略
demo=# CREATE MASKING POLICY msk_creditcard regexpmasking('[\d+]','x',5,9) ON LABEL(mask_lb1);
CREATE MASKING POLICY
--读取测试数据
demo=#select*from tb_for_masking;
col1 | col2 | col3
---------------------+---------------------+---------------------
4880-xxxx-xxxx-2525|4880-9898-4545-2525|4880-9898-4545-2525
(1 row)
demo=#
修改策略并测试
demo=# ALTER MASKING POLICY msk_creditcard MODIFY (FILTER ON ROLES(dev_mask));
ALTER MASKING POLICY
openGauss=# grant usage on schema public to dev_mask;
GRANT
demo=# grant select on table tb_for_masking to dev_mask;
GRANT
openGauss=# \c demo dev_mask
Passwordfor user dev_mask:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "demo"as user "dev_mask".
demo=>select*from tb_for_masking;
col1 | col2 | col3
---------------------+---------------------+---------------------
4880-xxxx-xxxx-2525|4880-9898-4545-2525|4880-9898-4545-2525
(1 row)
demo=>
手机号脱敏
CREATE MASKING POLICY msk_creditcardmobile regexpmasking('[\d+]', '*', 3, 5 ) ON LABEL(mask_lb2);
DROP MASKING POLICY IF EXISTS msk_creditcardmobile;
select * from tb_for_masking;
demo=# select * from tb_for_masking;
mobile | col2 | col3
-------------+---------------------+---------------------
139*****264 | 4880-9898-4545-2525 | 4880-9898-4545-2525
(1 row)
demo=#
文章转载自SmallDB,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




