privileges.td 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. $ set-sql-timeout duration=1s
  10. $ mysql-connect name=mysql url=mysql://root@mysql password=${arg.mysql-root-password}
  11. $ mysql-execute name=mysql
  12. DROP DATABASE IF EXISTS public;
  13. CREATE DATABASE public;
  14. USE public;
  15. CREATE USER priv IDENTIFIED BY 'priv';
  16. DROP SCHEMA IF EXISTS other;
  17. CREATE SCHEMA other;
  18. CREATE TABLE other.s (a int);
  19. # do not grant any privileges to priv
  20. CREATE TABLE public.t (a int);
  21. # do not grant any privileges to priv
  22. #
  23. # no CONNECT error
  24. #
  25. # no connect privilege exists in MySQL, a user can always connect to MySQL (but not necessarily to the schema)
  26. > CREATE SECRET mysqlpass AS 'priv'
  27. > CREATE CONNECTION mysql_conn TO MYSQL (
  28. HOST mysql,
  29. USER priv,
  30. PASSWORD SECRET mysqlpass
  31. )
  32. #
  33. # USAGE error
  34. #
  35. $ mysql-execute name=mysql
  36. GRANT ALL ON public.* TO priv;
  37. # still no privileges on schema other
  38. ! CREATE SOURCE mz_source
  39. FROM MYSQL CONNECTION mysql_conn
  40. FOR SCHEMAS (public, other);
  41. contains:no tables found in referenced schemas: "other"
  42. #
  43. # SELECT errors
  44. #
  45. $ mysql-execute name=mysql
  46. # Note that it is not possible in MySQL to grant permissions on a schema and revoke them on a single table.
  47. # Grant permissions only on the schema 'other' but not on table 's' in it.
  48. GRANT ALL ON other TO priv;
  49. ! CREATE SOURCE mz_source
  50. FROM MYSQL CONNECTION mysql_conn
  51. FOR SCHEMAS (public, other);
  52. contains:no tables found in referenced schemas: "other"
  53. $ mysql-execute name=mysql
  54. CREATE TABLE other.u (a int);
  55. CREATE TABLE other.access_not_granted (a int);
  56. CREATE TABLE other.`select` (a INT);
  57. CREATE TABLE other.`"select"` (a INT);
  58. # non-complete privileges on at least one table in schema 'other' are now present
  59. GRANT SELECT ON other.u TO priv;
  60. GRANT INDEX ON other.s TO priv;
  61. # A new error since the tables are now visible but we are missing some privileges
  62. ! CREATE SOURCE mz_source
  63. FROM MYSQL CONNECTION mysql_conn
  64. FOR SCHEMAS (other);
  65. contains:User lacks required MySQL privileges
  66. detail:Missing MySQL privileges: 'LOCK TABLES' on 'other.s', 'SELECT' on 'other.s', 'LOCK TABLES' on 'other.u', 'REPLICATION SLAVE' on '*.*'
  67. # fix table privileges, use wildcard for LOCK TABLES
  68. $ mysql-execute name=mysql
  69. GRANT SELECT ON other.s TO priv;
  70. GRANT LOCK TABLES ON other.* TO priv;
  71. ! CREATE SOURCE mz_source
  72. FROM MYSQL CONNECTION mysql_conn
  73. FOR SCHEMAS (other);
  74. contains:User lacks required MySQL privileges
  75. detail:Missing MySQL privileges: 'REPLICATION SLAVE' on '*.*'
  76. $ mysql-execute name=mysql
  77. GRANT REPLICATION SLAVE ON *.* TO priv;
  78. > CREATE SOURCE mz_source
  79. FROM MYSQL CONNECTION mysql_conn
  80. FOR SCHEMAS (other);
  81. # confirm the source works
  82. $ mysql-execute name=mysql
  83. USE other;
  84. INSERT INTO u VALUES (2), (3), (4);
  85. > SELECT * FROM u;
  86. 2
  87. 3
  88. 4
  89. # Verify privileges provided via default roles
  90. $ mysql-execute name=mysql
  91. CREATE ROLE 'r1';
  92. GRANT 'r1' TO 'priv';
  93. SET DEFAULT ROLE 'r1' TO 'priv';
  94. USE other;
  95. CREATE TABLE other.z (a int);
  96. INSERT INTO other.z VALUES (1);
  97. GRANT INDEX ON other.z TO 'priv';
  98. ! CREATE SOURCE mz_source_other
  99. FROM MYSQL CONNECTION mysql_conn
  100. FOR TABLES (other.z);
  101. contains:User lacks required MySQL privileges
  102. detail:Missing MySQL privileges: 'SELECT' on 'other.z'
  103. # grant the privilege to the role not the user directly
  104. $ mysql-execute name=mysql
  105. GRANT SELECT ON other.z TO 'r1';
  106. > CREATE SOURCE mz_source_other
  107. FROM MYSQL CONNECTION mysql_conn
  108. FOR TABLES (other.z);
  109. > SELECT * FROM z;
  110. 1
  111. # cleanup the 'other' database to avoid issues in other testdrive files
  112. > DROP SOURCE mz_source CASCADE;
  113. > DROP SOURCE mz_source_other CASCADE;
  114. $ mysql-execute name=mysql
  115. DROP DATABASE other;