您好,登錄后才能下訂單哦!
PostgreSQL提供了Row Level Security(RLS)來控制哪些行可以訪問或修改。
一、簡介
其標準的語法是:
[pg12@localhost ~]$ psql -d testdb
Timing is on.
Expanded display is used automatically.
psql (12.0)
Type "help" for help.
[local]:5432 pg12@testdb=# \help create policy
Command: CREATE POLICY
Description: define a new row level security policy for a table
Syntax:
CREATE POLICY name ON table_name
[ AS { PERMISSIVE | RESTRICTIVE } ]
[ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ]
[ TO { role_name | PUBLIC | CURRENT_USER | SESSION_USER } [, ...] ]
[ USING ( using_expression ) ]
[ WITH CHECK ( check_expression ) ]
URL: https://www.postgresql.org/docs/12/sql-createpolicy.html
[local]:5432 pg12@testdb=#
其中,USING表示滿足using_expression表達式為T的行才能看到或操作,否則這些行將被隱藏(hidden);
WITH CHECK表示在執(zhí)行INSERT/UPDATE操作時,如新數(shù)據(jù)不能通過check_expression表達式的校驗則被視為非法數(shù)據(jù)。
二、注意事項
1.RLS必須通過ALTER TABLE … ENABLE ROW LEVEL SECURITY顯式啟用;
2.如啟用RLS,那么至少存在一個policy
創(chuàng)建數(shù)據(jù)表,插入數(shù)據(jù),啟用RLS
[local]:5432 pg12@testdb=# drop table if exists t_rls1;
NOTICE: table "t_rls1" does not exist, skipping
DROP TABLE
Time: 37.363 ms
[local]:5432 pg12@testdb=# create table t_rls1(id int,c1 int);
CREATE TABLE
Time: 137.931 ms
[local]:5432 pg12@testdb=#
[local]:5432 pg12@testdb=# insert into t_rls1 values(1,1);
INSERT 0 1
Time: 1.820 ms
[local]:5432 pg12@testdb=# insert into t_rls1 values(2,2);
INSERT 0 1
Time: 0.582 ms
[local]:5432 pg12@testdb=#
[local]:5432 pg12@testdb=# ALTER TABLE t_rls1 ENABLE ROW LEVEL SECURITY;
ALTER TABLE
Time: 1.714 ms
[local]:5432 pg12@testdb=# select * from t_rls1;
id | c1
----+----
1 | 1
2 | 2
(2 rows)
創(chuàng)建新的user,查詢數(shù)據(jù)表,因為不存在policy,因此沒有數(shù)據(jù)返回(但為什么pg12這個用戶可以?后續(xù)有解釋)
Time: 23.349 ms
[local]:5432 pg12@testdb=# create user testuser with passwod 'test';
ERROR: unrecognized role option "passwod"
LINE 1: create user testuser with passwod 'test';
^
Time: 1.165 ms
[local]:5432 pg12@testdb=# create user testuser with password 'test';
CREATE ROLE
Time: 15.721 ms
[local]:5432 pg12@testdb=# grant all on t_rls1 to testuser;
GRANT
Time: 8.125 ms
[local]:5432 pg12@testdb=#
3.同一個命令,同一種類型,多個policys之間是OR的關(guān)系,即某行滿足其中一個policy就可以返回(如select命令);同一個命令不同的命令類型,則多個policys之間的關(guān)系是AND,必須所有policys都滿足才能返回(如update中存在where子句時)。
4.只有表的owner可以創(chuàng)建policys
5.超級用戶或者具有BYPASSRLS權(quán)限的用戶會跳過RLS檢查。這解釋先前為什么policy沒有,但pg12查詢有數(shù)據(jù)返回但testuser用戶沒有。
[local]:5432 pg12@testdb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
pg12 | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
testuser |
6.數(shù)據(jù)表的owner用戶默認會跳過RLS檢查
[local]:5432 testuser@testdb=> create table t_rls2(id int,c1 int);
CREATE TABLE
Time: 10.256 ms
[local]:5432 testuser@testdb=>
[local]:5432 testuser@testdb=> insert into t_rls2 values(1,1);
INSERT 0 1
Time: 3.422 ms
[local]:5432 testuser@testdb=> insert into t_rls2 values(2,2);
INSERT 0 1
Time: 2.580 ms
[local]:5432 testuser@testdb=>
[local]:5432 testuser@testdb=> ALTER TABLE t_rls2 ENABLE ROW LEVEL SECURITY;
ALTER TABLE
Time: 2.124 ms
[local]:5432 testuser@testdb=> select * from t_rls2;
id | c1
----+----
1 | 1
2 | 2
(2 rows)
Time: 1.127 ms
[local]:5432 testuser@testdb=> \d t_rls2
Table "public.t_rls2"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
c1 | integer | | |
Policies (row security enabled): (none)
testuser1&testuser2均為普通用戶,但testuser1查詢數(shù)據(jù)有返回,但testuser2沒有
[pg12@localhost ~]$ psql -d testdb -U testuser2
Timing is on.
Expanded display is used automatically.
psql (12.0)
Type "help" for help.
[local]:5432 testuser2@testdb=> select * from t_rls2;
id | c1
----+----
(0 rows)
Time: 3.808 ms
[local]:5432 testuser2@testdb=>
可通過ALTER TABLE … FORCE ROW LEVEL SECURITY命令強制啟用
[local]:5432 testuser@testdb=> ALTER TABLE t_rls2 FORCE ROW LEVEL SECURITY;
ALTER TABLE
Time: 1.967 ms
[local]:5432 testuser@testdb=> select * from t_rls2;
id | c1
----+----
(0 rows)
Time: 2.369 ms
具體的應(yīng)用案例可詳見參考資料中的鏈接。
三、參考資料
PG Conf of US 2019 - Row Level Security
Using “Row Level Security” to make large companies more secure
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。