roles.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from textwrap import dedent
  10. from materialize.checks.actions import Testdrive
  11. from materialize.checks.checks import TESTDRIVE_NOP, Check
  12. class CreateRole(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(TESTDRIVE_NOP)
  15. def manipulate(self) -> list[Testdrive]:
  16. return [
  17. Testdrive(dedent(s))
  18. for s in [
  19. """
  20. > CREATE ROLE create_role1;
  21. > GRANT create_role1 TO materialize;
  22. """,
  23. """
  24. > CREATE ROLE create_role2;
  25. > GRANT create_role2 TO materialize;
  26. """,
  27. ]
  28. ]
  29. def validate(self) -> Testdrive:
  30. return Testdrive(
  31. dedent(
  32. """
  33. > SELECT name FROM mz_roles WHERE name LIKE 'create_role%';
  34. create_role1
  35. create_role2
  36. # TODO(def-) Grantor information is currently not stable during
  37. # upgrades due to https://github.com/MaterializeInc/materialize/pull/18780
  38. # Reenable on next release
  39. > SELECT role.name, member.name from mz_role_members JOIN mz_roles role ON mz_role_members.role_id = role.id JOIN mz_roles member ON mz_role_members.member = member.id JOIN mz_roles grantor ON mz_role_members.grantor = grantor.id WHERE role.name LIKE 'create_role%';
  40. create_role1 materialize
  41. create_role2 materialize
  42. """
  43. )
  44. )
  45. class DropRole(CreateRole):
  46. def initialize(self) -> Testdrive:
  47. return Testdrive(
  48. dedent(
  49. """
  50. > CREATE ROLE drop_role1;
  51. > GRANT drop_role1 TO materialize;
  52. """
  53. )
  54. )
  55. def manipulate(self) -> list[Testdrive]:
  56. return [
  57. Testdrive(dedent(s))
  58. for s in [
  59. """
  60. > REVOKE drop_role1 FROM materialize;
  61. > DROP ROLE drop_role1;
  62. > CREATE ROLE drop_role2;
  63. > GRANT drop_role2 TO materialize;
  64. """,
  65. """
  66. > REVOKE drop_role2 FROM materialize;
  67. > DROP ROLE drop_role2;
  68. """,
  69. ]
  70. ]
  71. def validate(self) -> Testdrive:
  72. return Testdrive(
  73. dedent(
  74. """
  75. > SELECT COUNT(*) FROM mz_roles WHERE name LIKE 'drop_role%';
  76. 0
  77. > SELECT COUNT(*) FROM mz_role_members JOIN mz_roles ON mz_role_members.role_id = mz_roles.id WHERE name LIKE 'drop_role%';
  78. 0
  79. """
  80. )
  81. )
  82. class BuiltinRoles(CreateRole):
  83. def manipulate(self) -> list[Testdrive]:
  84. return [Testdrive(TESTDRIVE_NOP), Testdrive(TESTDRIVE_NOP)]
  85. def validate(self) -> Testdrive:
  86. return Testdrive(
  87. dedent(
  88. """
  89. $ skip-if
  90. SELECT mz_version_num() < 8300
  91. > SELECT name FROM mz_roles WHERE name IN ('mz_monitor', 'mz_monitor_redacted') ORDER BY name
  92. mz_monitor
  93. mz_monitor_redacted
  94. """
  95. )
  96. )