【磐维数据库】行级访问控制
2025-03-31 15:07 狂澜与玉昆0950 阅读(92) 评论(0) 收藏 举报中国移动磐维数据是基于openGauss定制开发的中国移动自用版OLTP数据库。自去2023年年12月发布以来,受到广泛关注,目前已成功上线百余套。 在产品落地的过程中,我们积累了大量的迁移、适配,以及问题分析诊断的经验。 北京海量数据技术股份有限公司,作为移动磐维集中式数据库外协厂商,对集中式磐维数据库的运维、管理、开发等均有深入了解。在江西移动现场运维整理汇总经验。
数据库版本信息:
testdb=# select pw_version() ; pw_version ----------------------------------------------------------------------------- (PanWeiDB_V2.0-S3.0.1_B01) compiled at 2024-09-29 21:50:47 commit d086caf + product name:PanWeiDB + version:V2.0-S3.0.1_B01 + commit:d086caf + openGauss version:5.0.0 + host:x86_64-pc-linux-gnu (1 row)
实现效果:
分别创建3个用户,使用同一条sql语句访问同一个表,查询到的结果不一样。
开始
1、创建三个用户
create user u1 password 'Tpcc@123' ; create user u2 password 'Tpcc@123' ; create user u3 password 'Tpcc@123' ;
2、创建表并插入示例数据
create table t2 (id int ,role varchar(30)) ; insert into t2 values(1,'u1'); insert into t2 values(2,'u2'); insert into t2 values(3,'u3');
这里将实现u1 用户只能查询到u1的条目,u2只能查询到u2的条目
3、授权表的查询权限
GRANT SELECT ON t2 TO u1,u2,u3; --如果目标表不是在public schema下,需要额外授权用户对表所在schema 的usage权限 grant usage on schema schema_edu to u1,u2,u3 ;
4、开启访问控制策略
ALTER TABLE t2 ENABLE ROW LEVEL SECURITY; CREATE ROW LEVEL SECURITY POLICY t2_rls ON t2 USING(role = CURRENT_USER); testdb=# \d t2 Table "public.t2" Column | Type | Modifiers | Attalias --------+-------------+-----------+---------- id | integer | | id role | varchar(30) | | role Row Level Security Policies: POLICY "t2_rls" FOR ALL TO {public} USING (((role)::name = "current_user"()))
5、切换不同的用户查询结果
testdb=# \c - u1 Password for user u1: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb" as user "u1". testdb=> select * from t2 ; id | role ----+------ 1 | u1 (1 row) testdb=> \c - u2 Password for user u2: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb" as user "u2". testdb=> select * from t2 ; id | role ----+------ 2 | u2 (1 row) testdb=> \c - u3 Password for user u3: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb" as user "u3". testdb=> select * from t2 ; id | role ----+------ 3 | u3 (1 row)
我报路长嗟日暮,学诗谩有惊人句。
九万里风鹏正举。风休住,蓬舟吹取三山去!
浙公网安备 33010602011771号