PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/modules/test_rls_hooks/sql/test_rls_hooks.sql

https://bitbucket.org/adunstan/pgdevel
SQL | 176 lines | 83 code | 53 blank | 40 comment | 0 complexity | 79ac2d09427647d396b3b3d9cbdc0b00 MD5 | raw file
Possible License(s): AGPL-3.0
  1. LOAD 'test_rls_hooks';
  2. CREATE TABLE rls_test_permissive (
  3. username name,
  4. supervisor name,
  5. data integer
  6. );
  7. -- initial test data
  8. INSERT INTO rls_test_permissive VALUES ('regress_r1','regress_s1',4);
  9. INSERT INTO rls_test_permissive VALUES ('regress_r2','regress_s2',5);
  10. INSERT INTO rls_test_permissive VALUES ('regress_r3','regress_s3',6);
  11. CREATE TABLE rls_test_restrictive (
  12. username name,
  13. supervisor name,
  14. data integer
  15. );
  16. -- At least one permissive policy must exist, otherwise
  17. -- the default deny policy will be applied. For
  18. -- testing the only-restrictive-policies from the hook,
  19. -- create a simple 'allow all' policy.
  20. CREATE POLICY p1 ON rls_test_restrictive USING (true);
  21. -- initial test data
  22. INSERT INTO rls_test_restrictive VALUES ('regress_r1','regress_s1',1);
  23. INSERT INTO rls_test_restrictive VALUES ('regress_r2','regress_s2',2);
  24. INSERT INTO rls_test_restrictive VALUES ('regress_r3','regress_s3',3);
  25. CREATE TABLE rls_test_both (
  26. username name,
  27. supervisor name,
  28. data integer
  29. );
  30. -- initial test data
  31. INSERT INTO rls_test_both VALUES ('regress_r1','regress_s1',7);
  32. INSERT INTO rls_test_both VALUES ('regress_r2','regress_s2',8);
  33. INSERT INTO rls_test_both VALUES ('regress_r3','regress_s3',9);
  34. ALTER TABLE rls_test_permissive ENABLE ROW LEVEL SECURITY;
  35. ALTER TABLE rls_test_restrictive ENABLE ROW LEVEL SECURITY;
  36. ALTER TABLE rls_test_both ENABLE ROW LEVEL SECURITY;
  37. CREATE ROLE regress_r1;
  38. CREATE ROLE regress_s1;
  39. GRANT SELECT,INSERT ON rls_test_permissive TO regress_r1;
  40. GRANT SELECT,INSERT ON rls_test_restrictive TO regress_r1;
  41. GRANT SELECT,INSERT ON rls_test_both TO regress_r1;
  42. GRANT SELECT,INSERT ON rls_test_permissive TO regress_s1;
  43. GRANT SELECT,INSERT ON rls_test_restrictive TO regress_s1;
  44. GRANT SELECT,INSERT ON rls_test_both TO regress_s1;
  45. SET ROLE regress_r1;
  46. -- With only the hook's policies, permissive
  47. -- hook's policy is current_user = username
  48. EXPLAIN (costs off) SELECT * FROM rls_test_permissive;
  49. SELECT * FROM rls_test_permissive;
  50. -- success
  51. INSERT INTO rls_test_permissive VALUES ('regress_r1','regress_s1',10);
  52. -- failure
  53. INSERT INTO rls_test_permissive VALUES ('regress_r4','regress_s4',10);
  54. SET ROLE regress_s1;
  55. -- With only the hook's policies, restrictive
  56. -- hook's policy is current_user = supervisor
  57. EXPLAIN (costs off) SELECT * FROM rls_test_restrictive;
  58. SELECT * FROM rls_test_restrictive;
  59. -- success
  60. INSERT INTO rls_test_restrictive VALUES ('regress_r1','regress_s1',10);
  61. -- failure
  62. INSERT INTO rls_test_restrictive VALUES ('regress_r4','regress_s4',10);
  63. SET ROLE regress_s1;
  64. -- With only the hook's policies, both
  65. -- permissive hook's policy is current_user = username
  66. -- restrictive hook's policy is current_user = superuser
  67. -- combined with AND, results in nothing being allowed
  68. EXPLAIN (costs off) SELECT * FROM rls_test_both;
  69. SELECT * FROM rls_test_both;
  70. -- failure
  71. INSERT INTO rls_test_both VALUES ('regress_r1','regress_s1',10);
  72. -- failure
  73. INSERT INTO rls_test_both VALUES ('regress_r4','regress_s1',10);
  74. -- failure
  75. INSERT INTO rls_test_both VALUES ('regress_r4','regress_s4',10);
  76. RESET ROLE;
  77. -- Create "internal" policies, to check that the policies from
  78. -- the hooks are combined correctly.
  79. CREATE POLICY p1 ON rls_test_permissive USING (data % 2 = 0);
  80. -- Remove the original allow-all policy
  81. DROP POLICY p1 ON rls_test_restrictive;
  82. CREATE POLICY p1 ON rls_test_restrictive USING (data % 2 = 0);
  83. CREATE POLICY p1 ON rls_test_both USING (data % 2 = 0);
  84. SET ROLE regress_r1;
  85. -- With both internal and hook policies, permissive
  86. EXPLAIN (costs off) SELECT * FROM rls_test_permissive;
  87. SELECT * FROM rls_test_permissive;
  88. -- success
  89. INSERT INTO rls_test_permissive VALUES ('regress_r1','regress_s1',7);
  90. -- success
  91. INSERT INTO rls_test_permissive VALUES ('regress_r3','regress_s3',10);
  92. -- failure
  93. INSERT INTO rls_test_permissive VALUES ('regress_r4','regress_s4',7);
  94. SET ROLE regress_s1;
  95. -- With both internal and hook policies, restrictive
  96. EXPLAIN (costs off) SELECT * FROM rls_test_restrictive;
  97. SELECT * FROM rls_test_restrictive;
  98. -- success
  99. INSERT INTO rls_test_restrictive VALUES ('regress_r1','regress_s1',8);
  100. -- failure
  101. INSERT INTO rls_test_restrictive VALUES ('regress_r3','regress_s3',10);
  102. -- failure
  103. INSERT INTO rls_test_restrictive VALUES ('regress_r1','regress_s1',7);
  104. -- failure
  105. INSERT INTO rls_test_restrictive VALUES ('regress_r4','regress_s4',7);
  106. -- With both internal and hook policies, both permissive
  107. -- and restrictive hook policies
  108. EXPLAIN (costs off) SELECT * FROM rls_test_both;
  109. SELECT * FROM rls_test_both;
  110. -- success
  111. INSERT INTO rls_test_both VALUES ('regress_r1','regress_s1',8);
  112. -- failure
  113. INSERT INTO rls_test_both VALUES ('regress_r3','regress_s3',10);
  114. -- failure
  115. INSERT INTO rls_test_both VALUES ('regress_r1','regress_s1',7);
  116. -- failure
  117. INSERT INTO rls_test_both VALUES ('regress_r4','regress_s4',7);
  118. RESET ROLE;
  119. DROP TABLE rls_test_restrictive;
  120. DROP TABLE rls_test_permissive;
  121. DROP TABLE rls_test_both;
  122. DROP ROLE regress_r1;
  123. DROP ROLE regress_s1;