Skip to content

Commit 1211500

Browse files
craig[bot]Dedej-Bergin
andcommitted
Merge #143428
143428: sql/delegate: fix SHOW POLICIES to properly display roles r=Dedej-Bergin a=Dedej-Bergin The SHOW POLICIES command was not correctly displaying roles for policies. When users were created and added to a policy, they would show up as NULL in the output. This was caused by using pg_catalog.pg_user in the join, which only contains users, not roles. The fix changes the query to join with pg_catalog.pg_roles instead, which contains information for both users and roles. This ensures all roles and users are properly displayed in the SHOW POLICIES output. Added a test to verify that roles, users, and mixed policies all display correctly. Fixes: #143358 Epic: CRDB-11724 Release note: none Co-authored-by: Bergin Dedej <[email protected]>
2 parents f66151f + 2b29d0c commit 1211500

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

pkg/sql/delegate/show_policies.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ func (d *delegator) delegateShowPolicies(stmt *tree.ShowPolicies) (tree.Statemen
2525
array_agg(
2626
CASE
2727
WHEN role_id.uid = 0 THEN 'public'
28-
ELSE u.usename
28+
ELSE r.rolname
2929
END
30+
ORDER BY r.rolname
3031
) AS roles,
3132
COALESCE(p.polqual::text, '') AS using_expr,
3233
COALESCE(p.polwithcheck::text, '') AS with_check_expr
3334
FROM pg_policy p
3435
LEFT JOIN LATERAL unnest(p.polroles) AS role_id(uid) ON true
35-
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = role_id.uid
36+
LEFT JOIN pg_catalog.pg_roles r ON r.oid = role_id.uid
3637
WHERE p.polrelid = %[6]d
3738
GROUP BY p.polname, p.polcmd, p.polpermissive, p.polqual, p.polwithcheck`
3839

pkg/sql/logictest/testdata/logic_test/row_level_security

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ query TTTTTT colnames,rowsort
188188
SHOW POLICIES FOR multi_pol_tab1
189189
----
190190
name cmd type roles using_expr with_check_expr
191-
policy8 ALL permissive {papa_roach,public} · ·
192191
policy1 ALL permissive {public} · ·
193192
policy2 ALL restrictive {public} · ·
194193
policy3 ALL permissive {public} · ·
195194
policy4 INSERT permissive {public} · ·
196195
policy5 UPDATE permissive {public} · ·
197196
policy6 DELETE permissive {public} · ·
198197
policy7 SELECT permissive {public} · ·
198+
policy8 ALL permissive {public,papa_roach} · ·
199199

200200
statement ok
201201
CREATE TABLE multi_pol_tab2 (c1 INT NOT NULL PRIMARY KEY)
@@ -1367,15 +1367,14 @@ alter_policy_table CREATE TABLE public.alter_policy_table (
13671367
CREATE POLICY p_ins ON public.alter_policy_table AS PERMISSIVE FOR INSERT TO public WITH CHECK (nextval('public.seq1'::REGCLASS) < 10000:::INT8);
13681368
CREATE POLICY p_sel ON public.alter_policy_table AS PERMISSIVE FOR SELECT TO aux1, alter_policy_role, aux2 USING (c1 != 1:::INT8)
13691369

1370-
# TODO(143358): Include roles in the SHOW POLICIES output.
1371-
query TTTTT colnames
1372-
SELECT name,cmd,type,using_expr,with_check_expr
1370+
query TTTTTT colnames
1371+
SELECT name,cmd,type,roles,using_expr,with_check_expr
13731372
FROM [SHOW POLICIES FOR alter_policy_table]
13741373
ORDER BY name DESC;
13751374
----
1376-
name cmd type using_expr with_check_expr
1377-
p_sel SELECT permissive c1 != 1:::INT8 ·
1378-
p_ins INSERT permissive · nextval('public.seq1'::REGCLASS) < 10000:::INT8
1375+
name cmd type roles using_expr with_check_expr
1376+
p_sel SELECT permissive {alter_policy_role,aux1,aux2} c1 != 1:::INT8 ·
1377+
p_ins INSERT permissive {public} · nextval('public.seq1'::REGCLASS) < 10000:::INT8
13791378

13801379
statement ok
13811380
SET ROLE root;
@@ -2573,7 +2572,7 @@ CREATE POLICY p1 ON rls_disabled USING (true);
25732572
statement ok
25742573
ALTER TABLE rls_disabled DISABLE ROW LEVEL SECURITY;
25752574

2576-
query TTTTTT colnames,rowsort
2575+
query TTTTTT colnames
25772576
SHOW POLICIES FOR rls_disabled;
25782577
----
25792578
name cmd type roles using_expr with_check_expr
@@ -2586,10 +2585,9 @@ CREATE TABLE no_policies (id INT PRIMARY KEY);
25862585
statement ok
25872586
ALTER TABLE no_policies ENABLE ROW LEVEL SECURITY;
25882587

2589-
query TTTTTT colnames,rowsort
2588+
query TTTTTT
25902589
SHOW POLICIES FOR no_policies;
25912590
----
2592-
name cmd type roles using_expr with_check_expr
25932591

25942592
# This is another test for multiple policies. But the focus here is how multiple
25952593
# policies are applied when they apply for other commands. For example, having
@@ -2873,4 +2871,66 @@ DROP TABLE cnt;
28732871
statement ok
28742872
DROP USER r1_user;
28752873

2874+
subtest show_policies_roles_and_users
2875+
2876+
statement ok
2877+
CREATE ROLE test_role1;
2878+
2879+
statement ok
2880+
CREATE ROLE test_role2;
2881+
2882+
statement ok
2883+
CREATE USER test_user1;
2884+
2885+
statement ok
2886+
CREATE USER test_user2;
2887+
2888+
statement ok
2889+
CREATE TABLE policy_roles_test (id INT PRIMARY KEY, val TEXT);
2890+
2891+
statement ok
2892+
CREATE POLICY mixed_policy ON policy_roles_test TO test_role1, test_user1, test_role2, test_user2;
2893+
2894+
query TTTTTT colnames
2895+
SHOW POLICIES FOR policy_roles_test
2896+
----
2897+
name cmd type roles using_expr with_check_expr
2898+
mixed_policy ALL permissive {test_role1,test_role2,test_user1,test_user2} · ·
2899+
2900+
statement ok
2901+
CREATE POLICY users_only_policy ON policy_roles_test TO test_user1, test_user2;
2902+
2903+
query TTTTTT colnames,rowsort
2904+
SHOW POLICIES FOR policy_roles_test
2905+
----
2906+
name cmd type roles using_expr with_check_expr
2907+
mixed_policy ALL permissive {test_role1,test_role2,test_user1,test_user2} · ·
2908+
users_only_policy ALL permissive {test_user1,test_user2} · ·
2909+
2910+
statement ok
2911+
CREATE POLICY roles_only_policy ON policy_roles_test TO test_role1, test_role2;
2912+
2913+
query TTTTTT colnames,rowsort
2914+
SHOW POLICIES FOR policy_roles_test
2915+
----
2916+
name cmd type roles using_expr with_check_expr
2917+
mixed_policy ALL permissive {test_role1,test_role2,test_user1,test_user2} · ·
2918+
roles_only_policy ALL permissive {test_role1,test_role2} · ·
2919+
users_only_policy ALL permissive {test_user1,test_user2} · ·
2920+
2921+
statement ok
2922+
DROP TABLE policy_roles_test;
2923+
2924+
statement ok
2925+
DROP USER test_user1;
2926+
2927+
statement ok
2928+
DROP USER test_user2;
2929+
2930+
statement ok
2931+
DROP ROLE test_role1;
2932+
2933+
statement ok
2934+
DROP ROLE test_role2;
2935+
28762936
subtest end

0 commit comments

Comments
 (0)