什么是行级权限?

基于行的安全策略,限制数据库用户的查看表数据权限。
采用RLS后,不同的用户访问一个表可以看到不同的数据。
属于权限中的粗粒度权限

松鼠举个栗子

创建测试用户

  1. est=# create user u1 password '123';
  2. CREATE ROLE
  3. test=#

创建测试表,插入数据

  1. test=# create table t1(id int);
  2. CREATE TABLE
  3. test=#
  4. test=# insert into t1 values (10),(11),(12),(13),(14);
  5. INSERT 0 5

为用户增加权限

  1. test=# grant all ON t
  2. t1 TABLE TABLESPACE TYPE
  3. test=# grant all ON TABLE t1 TO u1;
  4. GRANT

启用行级权限

  1. test=# alter table t1 enable
  2. ALWAYS NOVALIDATE REPLICA RULE UNIQUE (
  3. CONSTRAINT PRIMARY KEY ROW LEVEL SECURITY TRIGGER VALIDATE
  4. test=# alter table t1 enable ROW LEVEL SECURITY ;
  5. ALTER TABLE
  6. test=#
  7. test=# create policy p1 ON t1 using ( id != 10 and id != 13 ) to u1;
  8. 错误: 语法错误 "to" 或附近的
  9. 1行...reate policy p1 ON t1 using ( id != 10 and id != 13 ) to u1;
  10. ^
  11. test=# create policy p1 ON t1 using ( id != 10 and id != 13 ) ;
  12. CREATE POLICY
  13. test=#
  14. test=# alter policy p1 to
  15. test=# alter policy p1 ON t1
  16. RENAME TO TO USING ( WITH CHECK (
  17. test=# alter policy p1 ON t1 TO u1 ;
  18. ALTER POLICY
  19. test=#

测试1

  1. test=# \c - u1;
  2. 您现在已经连接到数据库 "test",用户 "u1".
  3. test=>
  4. test=>
  5. test=> select * from t1;
  6. id
  7. ----
  8. 11
  9. 12
  10. 14
  11. (3 行记录)

测试2

  1. test=> \c - system
  2. 您现在已经连接到数据库 "test",用户 "system".
  3. test=#
  4. test=# alter policy p1 ON t1 USING ( id = 10 or id = 13);
  5. ALTER POLICY
  6. test=#
  7. test=# \c - u1
  8. 您现在已经连接到数据库 "test",用户 "u1".
  9. test=>
  10. test=> select * from t1;
  11. id
  12. ----
  13. 10
  14. 13
  15. (2 行记录)

测试3

  1. test=> \c - system
  2. 您现在已经连接到数据库 "test",用户 "system".
  3. test=#
  4. test=# alter policy p1 ON t1 USING ( id = 10 and id = 13);
  5. ALTER POLICY
  6. test=#
  7. test=# \c - u1;
  8. 您现在已经连接到数据库 "test",用户 "u1".
  9. test=>
  10. test=> select * from t1;
  11. id
  12. ----
  13. (0 行记录)

测试4

  1. test=> \c - system
  2. 您现在已经连接到数据库 "test",用户 "system".
  3. test=#
  4. test=# alter policy p1 ON t1 USING ( id = 10 and id != 13);
  5. ALTER POLICY
  6. test=#
  7. test=# \c - u1;
  8. 您现在已经连接到数据库 "test",用户 "u1".
  9. test=>
  10. test=> select * from t1;
  11. id
  12. ----
  13. 10
  14. (1 行记录)
  15. test=> select * from t1 where id = 10 and id != 13;
  16. id
  17. ----
  18. 10
  19. (1 行记录)
  20. test=> select * from t1 where id = 10 and id = 13;
  21. id
  22. ----
  23. (0 行记录)
  24. test=>
  25. test=> select * from t1 where id != 10 and id !=13;
  26. id
  27. ----
  28. (0 行记录)
  29. test=>
  30. test=> select * from t1 where id != 10 and id != 13;
  31. id
  32. ----
  33. (0 行记录)
  34. test=> select * from t1 where id != 10 or id != 13;
  35. id
  36. ----
  37. 10
  38. (1 行记录)
  39. test=> select * from t1;
  40. id
  41. ----
  42. 10
  43. (1 行记录)

思考

行级权限与”where”

扩展:列级权限

细粒度权限

形式一:列级细粒度权限
比如,授权user1只能看到t1表的name列的数据。
形式二:行级细粒度权限
比如,授权user1能看到t1表的所有列,但只能看到符合条件的少量数据行。
以列级安全控制为例,演示KES的列级细粒度权限

实验:让某用户只能看到表的部分字段

创建新的测试数据

  1. test=# create table t1(ename name,sal int,comm text);
  2. CREATE TABLE
  3. test=#
  4. test=# insert into t1 values ('king',10000,'abc');
  5. INSERT 0 1
  6. test=# insert into t1 values ('base',20000,'def');
  7. INSERT 0 1
  8. test=#

创建测试用户

  1. test=#
  2. test=# create user user1 password '123';
  3. CREATE ROLE
  4. test=#

执行细粒度授权

  1. test=# grant select (ename,sal) on t1 to user1 ;
  2. GRANT
  3. test=#
  4. test=# \dp+ t1;
  5. 存取权限
  6. 架构模式 | 名称 | 类型 | 存取权限 | 列特权 | 策略
  7. ----------+------+--------+----------+------------------+------
  8. public | t1 | 数据表 | | ename: +|
  9. | | | | user1=r/system+|
  10. | | | | sal: +|
  11. | | | | user1=r/system |
  12. (1 行记录)
  13. test=#
  14. test=# select * from t1;
  15. ename | sal | comm
  16. -------+-------+------
  17. king | 10000 | abc
  18. base | 20000 | def
  19. (2 行记录)

结果

  1. test=> select * from t1;
  2. 错误: 对表 t1 权限不够
  3. test=>
  4. test=>
  5. test=> select ename,sal from t1;
  6. ename | sal
  7. -------+-------
  8. king | 10000
  9. base | 20000
  10. (2 行记录)
  11. test=>

实验:让某用户能查询所有字段,但只能修改部分字段

创建新的测试用户并授权

  1. test=# create user admin password 'kingbase';
  2. CREATE ROLE
  3. test=#
  4. test=# grant select,update(sal) on t
  5. test=# grant select,update(sal) on t1 to admin;
  6. GRANT
  7. test=#

打印表上的权限清单

  1. test=# \dp t1;
  2. 存取权限
  3. 架构模式 | 名称 | 类型 | 存取权限 | 列特权 | 策略
  4. ----------+------+--------+-----------------------+------------------+------
  5. public | t1 | 数据表 | system=arwdDxt/system+| sal: +|
  6. | | | admin=r/system | admin=w/system |
  7. (1 行记录)

实验结果

  1. test=# \c - admin
  2. 您现在已经连接到数据库 "test",用户 "admin".
  3. test=>
  4. test=> select * from t1;
  5. ename | sal | comm
  6. -------+-------+------
  7. king | 10000 | abc
  8. base | 20000 | def
  9. (2 行记录)
  10. test=>
  11. test=> update t1 set sal=sal*1.1;
  12. UPDATE 2
  13. test=>
  14. test=> select * from t1;
  15. ename | sal | comm
  16. -------+-------+------
  17. king | 11000 | abc
  18. base | 22000 | def
  19. (2 行记录)
  20. test=> update t1 set comm = 'c' where comm = 'b';
  21. 错误: 对表 t1 权限不够
  22. test=>

参考学习

https://cdn.modb.pro/db/105031
https://mp.weixin.qq.com/s/iXMT67y7C1a34tcZfET6jA