博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EnterpriseDB & PostgreSQL RLS & Oracle VPD
阅读量:6982 次
发布时间:2019-06-27

本文共 5850 字,大约阅读时间需要 19 分钟。

PostgreSQL 9.5的RLS用法请参照

EnterpriseDB的RLS用法略有差别,因为EDB主要为Oracle兼容性做了很多适配,用法和Oracle的VPD用法相似,调用DBMS_RLS.add_policy来实现RLS。
详细的用法参照:
例子:

postgres=> create table test(id int, info text, rol name);

创建一个策略,只允许用户操作(select,update,delete,insert) rol=当前用户名的记录。

首先要创建一个函数,函数的参数类型和返回值类型必须使用这种格式。
代表行安全策略要在哪个schema.object对象上应用,以及应用时添加的条件。
函数返回值就是添加的条件。
例如'rol='||current_user这个会作为附加条件,判断记录是否匹配这个条件。

postgres=> create or replace function f(p_schema text, p_obj text) returns text as $$  declare                                           begin                           return 'rol=$_$'||current_user||'$_$';  end;                                $$ language plpgsql;  CREATE FUNCTION

创建策略(需超级用户):

postgres=> select                        dbms_rls.add_policy (object_schema => 'public'::text      ,object_name => 'test'::text      ,policy_name => 'policy1'::text             ,function_schema => 'public'::text  ,STATEMENT_TYPES => 'SELECT, INSERT, UPDATE, DELETE'::text  ,POLICY_FUNCTION => 'f'::text, update_check=>'true');    rds_add_policy   ----------------     (1 row)

验证策略是否生效

postgres=> select current_user;   current_user   --------------   digoal  (1 row)

当前用户为digoal,只能插入rol='digoal'的记录

postgres=> insert into test values (1,'test','digoal');  INSERT 16426 1  postgres=> insert into test values (1,'test','A');  ERROR:  policy with check option violation  DETAIL:  Policy predicate was evaluated to FALSE with the updated values

更换一个名为test的用户测试:

postgres=> insert into test values (1,'test','a');  ERROR:  policy with check option violation  DETAIL:  Policy predicate was evaluated to FALSE with the updated values  postgres=> insert into test values (1,'test','test');  INSERT 16428 1  postgres=> select current_user;   current_user   --------------   test  (1 row)  postgres=> update test set id=1;  UPDATE 1  postgres=> update test set id=1 returning *;   id | info | rol    ----+------+------    1 | test | test  (1 row)  UPDATE 1

这样做到了数据的隔离。

查看已有的策略:

postgres=# select * from dba_policies ;   object_owner | schema_name | object_name | policy_group |    policy_name     | pf_owner | package | function | sel | ins | upd | del | idx | chk_option | enable | static_policy | policy_type | long_predicate   --------------+-------------+-------------+--------------+--------------------+----------+---------+----------+-----+-----+-----+-----+-----+------------+--------+---------------+-------------+----------------   digoal       | public      | test        |              | policy1            | public   |         | f        | YES | YES | YES | YES | NO  | YES        | YES    | NO            | UNKNOWN     | YES

禁用,生效策略

postgres=# select dbms_rls.enable_policy('public','test','policy1',false);  -- 禁用  postgres=# select dbms_rls.enable_policy('public','test','policy1',true);  -- 生效

删除策略

postgres=# select dbms_rls.drop_policy('public','test','policy1');  -[ RECORD 1 ]-  drop_policy |

注意EDB的策略只能针对所有用户,不能像PG那样只对某些用户创建策略。

如果要让EDB的策略有针对性,可以在函数中加上角色判断,对不同的角色使用不同的返回值来控制策略的多样性。
例如

switch current_user    when 'digoal' then return '.....';    when '..' then return '...';  等。

策略还有一个需要注意的地方,静态和动态。通过add_policy的参数控制,默认是动态。

静态策略指会话中第一次触发策略时,编译成静态的,以后直接使用内存中缓存的策略。
动态策略指每次都重新调用。例如使用current_user这种变量作为return值中的一部分,就需要使用动态策略。

权限问题:

dbms_rls包需要超级用户才能执行,如果需要给普通用户执行权限,可以通过封装,或者将函数的执行权限给普通用户。
例如:

create or replace function your_add_policy(object_schema text DEFAULT NULL::text, object_name text, policy_name text, function_schema text DEFAULT NULL::text, policy_function text, statement_types text DEFAULT 'insert,update,delete,select'::text, update_check boolean DEFAULT false, enable boolean DEFAULT true, static_policy boolean DEFAULT false, policy_type integer DEFAULT NULL::integer, long_predicate boolean DEFAULT false, sec_relevant_cols text DEFAULT NULL::text, sec_relevant_cols_opt integer DEFAULT NULL::integer) returns void as $$           declare  begin  perform dbms_rls.add_policy(object_schema , object_name , policy_name , function_schema , policy_function , statement_types , update_check , enable , static_policy , policy_type , long_predicate , sec_relevant_cols , sec_relevant_cols_opt);  end;  $$ language plpgsql;    grant execute on function your_add_policy( text ,  text,  text,  text ,  text,  text ,  boolean ,  boolean ,  boolean ,  integer ,  boolean ,  text ,  integer ) to public;

给普通用户操作add_policy的风险:

对超级用户创建的表,普通用户也能通过add_policy来控制安全策略,这个控制结果有点越权的感觉。
例如用户创建一个函数如下:

create or replace function f2(name,name) returns text as $$  declare  begin  return 'false';  end;  $$ language plpgsql;

然后把这个函数作为策略函数,在一个超级用户创建的表上创建策略,超级用户对这个表的内容就会变成完全不可见状态。

是非常危险的。不过还好对系统表不起作用,否则问题更严重。

安全加固方法:

在封装函数中过滤需要过滤的表,对这些表不允许创建policy:

create or replace function your_add_policy(object_schema text DEFAULT NULL::text, object_name text, policy_name text, function_schema text DEFAULT NULL::text, policy_function text, statement_types text DEFAULT 'insert,update,delete,select'::text, update_check boolean DEFAULT false, enable boolean DEFAULT true, static_policy boolean DEFAULT false, policy_type integer DEFAULT NULL::integer, long_predicate boolean DEFAULT false, sec_relevant_cols text DEFAULT NULL::text, sec_relevant_cols_opt integer DEFAULT NULL::integer) returns void as $$         declarefilter_name text[];beginfilter_name = array['public.tbl1'];  -- 过滤这个表perform 1 where object_schema||'.'||object_name = any(filter_name);if not found thenperform dbms_rls.add_policy(object_schema , object_name , policy_name , function_schema , policy_function , statement_types , update_check , enable , static_policy , policy_type , long_predicate , sec_relevant_cols , sec_relevant_cols_opt);end if;end;$$ language plpgsql security definer;grant execute on function your_add_policy( text ,  text,  text,  text ,  text,  text ,  boolean ,  boolean ,  boolean ,  integer ,  boolean ,  text ,  integer ) to public;

用户再次使用your_add_policy添加策略时,对public.tbl1不起作用。

[参考]

转载地址:http://zoxpl.baihongyu.com/

你可能感兴趣的文章
UNREFERENCED_PARAMETER的作用
查看>>
PHP计算表达式-栈
查看>>
sysbench OLTP基准测试
查看>>
IBATIS中关于iterate"$"与"#"的应用
查看>>
centos -bash: vim: command not found
查看>>
为什么要将对象序列化
查看>>
新增网址/网页 截图api[增加安全防护本接口已停用]源码可下载
查看>>
SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
查看>>
刷leetcode第705题- 设计哈希集合
查看>>
dubbo协议参考
查看>>
SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
查看>>
读《白帽子讲Web安全》之安全意识篇(一)
查看>>
Session问题
查看>>
运用 autoconf 和 automake 自动生成 Makefile 实例讲解
查看>>
OpenSSL 之 RSA 相关命令学习笔记
查看>>
GLSL三种修饰符区别与用途(uniform,attribute和varying)
查看>>
python django django-debug-toolbar 加载缓慢,不能使用。
查看>>
操作系之进程调度及算法详解
查看>>
PHPexcel实列
查看>>
Butterknife 的简单使用 和 配合 Butterknife的插件 Zelezny
查看>>