privileges.td 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #
  39. # SELECT errors
  40. #
  41. $ mysql-execute name=mysql
  42. # Note that it is not possible in MySQL to grant permissions on a schema and revoke them on a single table.
  43. # Grant permissions only on the schema 'other' but not on table 's' in it.
  44. GRANT ALL ON other TO priv;
  45. $ mysql-execute name=mysql
  46. CREATE TABLE other.u (a int);
  47. CREATE TABLE other.access_not_granted (a int);
  48. CREATE TABLE other.`select` (a INT);
  49. CREATE TABLE other.`"select"` (a INT);
  50. # non-complete privileges on at least one table in schema 'other' are now present
  51. GRANT SELECT ON other.u TO priv;
  52. GRANT INDEX ON other.s TO priv;
  53. # A new error since the tables are now visible but we are missing some privileges
  54. > CREATE SOURCE mz_source FROM MYSQL CONNECTION mysql_conn;
  55. ! CREATE TABLE s FROM SOURCE mz_source (REFERENCE other.s);
  56. contains:User lacks required MySQL privileges
  57. detail:Missing MySQL privileges: 'LOCK TABLES' on 'other.s', 'SELECT' on 'other.s', 'REPLICATION SLAVE' on '*.*'
  58. ! CREATE TABLE u FROM SOURCE mz_source (REFERENCE other.u);
  59. contains:User lacks required MySQL privileges
  60. detail:Missing MySQL privileges: 'LOCK TABLES' on 'other.u', 'REPLICATION SLAVE' on '*.*
  61. # fix table privileges, use wildcard for LOCK TABLES
  62. $ mysql-execute name=mysql
  63. GRANT SELECT ON other.s TO priv;
  64. GRANT LOCK TABLES ON other.* TO priv;
  65. ! CREATE TABLE u FROM SOURCE mz_source (REFERENCE other.s);
  66. contains:User lacks required MySQL privileges
  67. detail:Missing MySQL privileges: 'REPLICATION SLAVE' on '*.*'
  68. $ mysql-execute name=mysql
  69. GRANT REPLICATION SLAVE ON *.* TO priv;
  70. > CREATE TABLE s FROM SOURCE mz_source (REFERENCE other.s);
  71. > CREATE TABLE u FROM SOURCE mz_source (REFERENCE other.u);
  72. # confirm the source works
  73. $ mysql-execute name=mysql
  74. USE other;
  75. INSERT INTO u VALUES (2), (3), (4);
  76. > SELECT * FROM u;
  77. 2
  78. 3
  79. 4
  80. # Verify privileges provided via default roles
  81. $ mysql-execute name=mysql
  82. CREATE ROLE 'r1';
  83. GRANT 'r1' TO 'priv';
  84. SET DEFAULT ROLE 'r1' TO 'priv';
  85. USE other;
  86. CREATE TABLE other.z (a int);
  87. INSERT INTO other.z VALUES (1);
  88. GRANT INDEX ON other.z TO 'priv';
  89. > CREATE SOURCE mz_source_other
  90. FROM MYSQL CONNECTION mysql_conn;
  91. ! CREATE TABLE other.z FROM SOURCE mz_source_other (REFERENCE other.z);
  92. contains:User lacks required MySQL privileges
  93. detail:Missing MySQL privileges: 'SELECT' on 'other.z'
  94. # grant the privilege to the role not the user directly
  95. $ mysql-execute name=mysql
  96. GRANT SELECT ON other.z TO 'r1';
  97. > CREATE TABLE z FROM SOURCE mz_source_other (REFERENCE other.z);
  98. > SELECT * FROM z;
  99. 1
  100. # cleanup the 'other' database to avoid issues in other testdrive files
  101. > DROP SOURCE mz_source CASCADE;
  102. > DROP SOURCE mz_source_other CASCADE;
  103. $ mysql-execute name=mysql
  104. DROP DATABASE other;