Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Allow editing MySQL tables when partial_revokes is enabled (#74573)

Ngoc Khuat6d926642ddb3
diff --git a/.clj-kondo/src/hooks/metabase/util/i18n.clj b/.clj-kondo/src/hooks/metabase/util/i18n.clj
index 802bbee8e74..ff58857d887 100644
--- a/.clj-kondo/src/hooks/metabase/util/i18n.clj
+++ b/.clj-kondo/src/hooks/metabase/util/i18n.clj
@@ -1,7 +1,6 @@
(ns hooks.metabase.util.i18n
(:require
[clj-kondo.hooks-api :as api]
- [clojure.string :as str]
[hooks.common]))
(defn- valid-apostrophes?
diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
index 767d0f00808..ca549ea82b8 100644
--- a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
@@ -11,10 +11,13 @@
[metabase-enterprise.action-v2.test-util :as action-v2.tu]
[metabase.actions.test-util :as actions.tu]
[metabase.driver :as driver]
+ [metabase.driver.mysql :as mysql]
[metabase.driver.sql-jdbc :as sql-jdbc]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.query-processor.test :as qp]
+ [metabase.sync.core :as sync]
[metabase.test :as mt]
+ [metabase.test.data.interface :as tx]
[metabase.test.data.sql :as sql.tx]
[metabase.warehouse-schema.models.field-values :as field-values]
[toucan2.core :as t2])
@@ -675,3 +678,103 @@
(is (= {table-id [nil
{:price "Must be an integer"}
{:name "This field is required"}]} (:errors result))))))))))
+
+(deftest partial-revokes-editing-end-to-end-test
+ (testing "a writable MySQL table can be edited end-to-end even when partial_revokes is enabled (metabase#73276)"
+ (mt/with-premium-features #{actions-feature-flag}
+ (mt/test-driver :mysql
+ (when-not (mysql/mariadb? (mt/db))
+ (tx/drop-if-exists-and-create-db! driver/*driver* "partial_revokes_edit_test")
+ (let [details (tx/dbdef->connection-details :mysql :db {:database-name "partial_revokes_edit_test"})
+ spec (sql-jdbc.conn/connection-details->spec :mysql details)]
+ (try
+ ;; Two fully-granted tables on a server with partial_revokes ON. Previously, partial_revokes being ON
+ ;; disabled the writable check for the *entire* database, so every table -- even genuinely-editable
+ ;; ones -- showed up as uneditable. We also revoke INSERT on one table to show that a revoke elsewhere
+ ;; in the database no longer drags the others down: writable_table stays editable end-to-end.
+ (doseq [stmt ["CREATE TABLE `writable_table` (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"
+ "CREATE TABLE `revoked_table` (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"
+ "CREATE USER 'partial_revokes_edit_user' IDENTIFIED BY 'password';"
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON partial_revokes_edit_test.writable_table TO 'partial_revokes_edit_user'"
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON partial_revokes_edit_test.revoked_table TO 'partial_revokes_edit_user'"]]
+ (jdbc/execute! spec stmt))
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = ON;")
+ (jdbc/execute! spec "REVOKE INSERT ON partial_revokes_edit_test.revoked_table FROM 'partial_revokes_edit_user';")
+ (let [user-details (assoc details
+ :user "partial_revokes_edit_user"
+ :password "password"
+ :ssl true
+ :additional-options "trustServerCertificate=true")]
+ (mt/with-temp [:model/Database database {:engine "mysql"
+ :details user-details
+ :dbms_version {:flavor "MySQL"}
+ :settings {:database-enable-table-editing true
+ :database-enable-actions true}}]
+ (sync/sync-database! database)
+ (let [writable-id (t2/select-one-fn :id :model/Table :db_id (:id database) :name "writable_table")]
+ (testing "the writable table syncs as editable despite the REVOKE on the other table"
+ (is (true? (t2/select-one-fn :is_writable :model/Table :id writable-id))))
+ (testing "rows can actually be inserted through the editing API"
+ (is (=? [{:id 1, :name "Pichu"}]
+ (map :row (:outputs (action-v2.tu/create-rows! writable-id [{:name "Pichu"}])))))
+ (is (= [[1 "Pichu"]]
+ (mt/rows
+ (qp/process-query {:database (:id database)
+ :type :query
+ :query {:source-table writable-id}}))))))))
+ (finally
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = OFF;")
+ (jdbc/execute! spec "DROP USER IF EXISTS 'partial_revokes_edit_user';")))))))))
+
+(deftest partial-revokes-optimistic-edit-fails-at-runtime-test
+ (testing "a table that only *looks* editable under a schema-level partial revoke fails at runtime when edited (metabase#73276)"
+ (mt/with-premium-features #{actions-feature-flag}
+ (mt/test-driver :mysql
+ (when-not (mysql/mariadb? (mt/db))
+ (tx/drop-if-exists-and-create-db! driver/*driver* "partial_revokes_runtime_test")
+ (let [details (tx/dbdef->connection-details :mysql :db {:database-name "partial_revokes_runtime_test"})
+ spec (sql-jdbc.conn/connection-details->spec :mysql details)]
+ (try
+ ;; Grant DML globally, then partially revoke INSERT at the *schema* level -- the real partial_revokes
+ ;; mechanism. SHOW GRANTS then emits a `REVOKE INSERT ON db.* FROM user` line, which we deliberately
+ ;; ignore, so the table optimistically syncs as writable even though the user genuinely cannot insert.
+ ;; This is the trade-off the fix makes: rather than marking everything uneditable, we let it look
+ ;; editable and fail at runtime -- which is exactly what this test pins down.
+ (doseq [stmt ["CREATE TABLE `optimistic_table` (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"
+ "CREATE USER 'partial_revokes_runtime_user' IDENTIFIED BY 'password';"
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'partial_revokes_runtime_user'"]]
+ (jdbc/execute! spec stmt))
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = ON;")
+ (jdbc/execute! spec "REVOKE INSERT ON partial_revokes_runtime_test.* FROM 'partial_revokes_runtime_user';")
+ (let [user-details (assoc details
+ :user "partial_revokes_runtime_user"
+ :password "password"
+ :ssl true
+ :additional-options "trustServerCertificate=true")]
+ (mt/with-temp [:model/Database database {:engine "mysql"
+ :details user-details
+ :dbms_version {:flavor "MySQL"}
+ :settings {:database-enable-table-editing true
+ :database-enable-actions true}}]
+ (sync/sync-database! database)
+ (let [table-id (t2/select-one-fn :id :model/Table :db_id (:id database) :name "optimistic_table")]
+ (testing "the table optimistically syncs as writable (the schema-level REVOKE line is ignored)"
+ (is (true? (t2/select-one-fn :is_writable :model/Table :id table-id))))
+ (testing "but editing it through the API fails at runtime -- no row is actually inserted"
+ ;; We don't assert a specific status code; the point is simply that the edit does not succeed.
+ (let [resp (mt/user-http-request :crowberto :post action-v2.tu/execute-bulk-url
+ {:action :data-grid.row/create
+ :scope {:table-id table-id}
+ :inputs [{:name "Pichu"}]})]
+ (is (empty? (filter :row (:outputs resp)))
+ "the create must not report a successfully written row")
+ (is (empty? (mt/rows (qp/process-query {:database (:id database)
+ :type :query
+ :query {:source-table table-id}})))
+ "no row may be inserted into a table the user cannot actually write to"))))))
+ (finally
+ ;; Drop the user *before* disabling partial_revokes: MySQL refuses to turn partial_revokes off while
+ ;; any partial-revoke restriction still exists (error 3896), and dropping the user removes its
+ ;; restriction. Doing it in the other order leaves the shared server poisoned for later tests.
+ (jdbc/execute! spec "DROP USER IF EXISTS 'partial_revokes_runtime_user';")
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = OFF;")))))))))
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index d61ee96bb9b..4dc700cf782 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -167,18 +167,6 @@
[conn :- (lib.schema.common/instance-of-class Connection)]
(= (connection-flavor conn) "MariaDB"))
-(defn- partial-revokes-enabled?
- [driver db]
- (sql-jdbc.execute/do-with-connection-with-options
- driver
- db
- nil
- (fn [^java.sql.Connection conn]
- (let [stmt (.prepareStatement conn "SHOW VARIABLES LIKE 'partial_revokes';")
- rset (.executeQuery stmt)]
- (when (.next rset)
- (= "ON" (.getString rset 2)))))))
-
(defmethod driver/database-supports? [:mysql :table-privileges]
[_driver _feat _db]
;; Disabled completely due to errors when dealing with partial revokes (metabase#38499)
@@ -188,12 +176,7 @@
(defmethod driver/database-supports? [:mysql :metadata/table-writable-check]
[driver _feat db]
(and (= driver :mysql)
- (mysql? db)
- (not (try
- (partial-revokes-enabled? driver db)
- (catch Exception e
- (log/warn e "Failed to check table writable")
- false)))))
+ (mysql? db)))
(defmethod driver/database-supports? [:mysql :regex/lookaheads-and-lookbehinds]
[driver _feat db]
@@ -1082,6 +1065,11 @@
(condp re-find grant
#"^GRANT PROXY ON "
nil
+ ;; Under `partial_revokes`, `SHOW GRANTS` emits `REVOKE ... ON ... FROM ...` lines. We ignore them and rely on the
+ ;; GRANT lines alone, which yields an optimistic view of privileges (a table revoked from may still look writable).
+ ;; In that case the edit fails at runtime rather than every table being treated as uneditable.
+ #"^REVOKE "
+ nil
#"^GRANT (.+) ON FUNCTION "
nil
#"^GRANT (.+) ON PROCEDURE "
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 0b7eb9201d0..3ec0140cedb 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -832,7 +832,10 @@
(is (= {:type :roles
:roles #{"`example_role`@`%`" "`example_role_2`@`%`"}}
(#'mysql/parse-grant "GRANT `example_role`@`%`,`example_role_2`@`%` TO 'metabase'@'localhost'")))
- (is (nil? (#'mysql/parse-grant "GRANT PROXY ON 'metabase'@'localhost' TO 'metabase'@'localhost' WITH GRANT OPTION")))))
+ (is (nil? (#'mysql/parse-grant "GRANT PROXY ON 'metabase'@'localhost' TO 'metabase'@'localhost' WITH GRANT OPTION")))
+ (testing "REVOKE grants (emitted under partial_revokes) are ignored rather than throwing"
+ (is (nil? (#'mysql/parse-grant "REVOKE INSERT ON `test-data`.`foo` FROM 'metabase'@'localhost'")))
+ (is (nil? (#'mysql/parse-grant "REVOKE INSERT, DELETE ON `test-data`.* FROM 'metabase'@'localhost'"))))))
(deftest ^:parallel table-name->privileges-test
(testing "table-names->privileges should work correctly"
@@ -987,15 +990,17 @@
(deftest partial-revokes-writable-test
(mt/test-driver :mysql
(when-not (mysql/mariadb? (mt/db))
- (testing "`database-supports :metadata/table-writable-check` returns true normally but false with partial revokes"
+ (testing "a genuinely-writable table stays editable under partial_revokes, even when another table is revoked"
(tx/drop-if-exists-and-create-db! driver/*driver* "partial_revokes_test")
(let [details (tx/dbdef->connection-details :mysql :db {:database-name "partial_revokes_test"})
spec (sql-jdbc.conn/connection-details->spec :mysql details)]
(try
- ;; Create test tables and user
- (doseq [stmt ["CREATE TABLE `test_table` (id INTEGER);"
+ ;; Two fully-writable tables and a user with full DML on both.
+ (doseq [stmt ["CREATE TABLE `writable_table` (id INTEGER);"
+ "CREATE TABLE `revoked_table` (id INTEGER);"
"CREATE USER 'partial_revokes_test_user' IDENTIFIED BY 'password';"
- "GRANT SELECT, INSERT, UPDATE, DELETE ON partial_revokes_test.test_table TO 'partial_revokes_test_user'"]]
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON partial_revokes_test.writable_table TO 'partial_revokes_test_user'"
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON partial_revokes_test.revoked_table TO 'partial_revokes_test_user'"]]
(jdbc/execute! spec stmt))
(let [user-connection-details (assoc details
:user "partial_revokes_test_user"
@@ -1004,27 +1009,27 @@
:additional-options "trustServerCertificate=true")]
(mt/with-temp [:model/Database database {:engine "mysql", :details user-connection-details
:dbms_version {:flavor "MySQL"}}]
- (testing "With partial_revokes OFF (default), metadata/table-writable-check is supported"
+ (testing "With partial_revokes OFF (default), both tables sync as writable"
(jdbc/execute! spec "SET GLOBAL partial_revokes = OFF;")
(is (true? (driver/database-supports? driver/*driver* :metadata/table-writable-check database))
- "Should support metadata/table-writable-check when partial_revokes is OFF"))
- (testing "With partial_revokes ON, metadata/table-writable-check is not supported"
- (jdbc/execute! spec "SET GLOBAL partial_revokes = ON;")
- (is (false? (driver/database-supports? driver/*driver* :metadata/table-writable-check database))
- "Should not support metadata/table-writable-check when partial_revokes is ON")
+ "Should support metadata/table-writable-check when partial_revokes is OFF")
(sync/sync-database! database)
- (is (= {"test_table" nil}
- (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))
- "is_writable should sync to nil when partial_revokes is ON"))
- (testing "Revoke some permissions with partial_revokes ON"
- (jdbc/execute! spec "REVOKE INSERT ON partial_revokes_test.test_table FROM 'partial_revokes_test_user';")
- (is (false? (driver/database-supports? driver/*driver* :metadata/table-writable-check database))
- "Should still not support metadata/table-writable-check after partial revoke")
- ;; Sync database again and verify is_writable is still nil
+ (is (= {"writable_table" true, "revoked_table" true}
+ (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))))
+ (testing "With partial_revokes ON and INSERT revoked on one table, the check stays enabled"
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = ON;")
+ (jdbc/execute! spec "REVOKE INSERT ON partial_revokes_test.revoked_table FROM 'partial_revokes_test_user';")
+ (is (true? (driver/database-supports? driver/*driver* :metadata/table-writable-check database))
+ "Should still support metadata/table-writable-check when partial_revokes is ON")
(sync/sync-database! database)
- (is (= {"test_table" nil}
+ ;; The bug (metabase#73276): turning partial_revokes ON disabled the writable check for the whole
+ ;; database, so *every* table became uneditable. The fix removes that blanket gate, so writability is
+ ;; computed per-table from the GRANT lines again: the untouched table stays writable, and the table
+ ;; whose INSERT was revoked correctly reports not-writable instead of dragging everything down with it.
+ ;; (Optimistic handling of schema-level partial-revoke REVOKE lines is covered by `parse-grant-test`.)
+ (is (= {"writable_table" true, "revoked_table" false}
(t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))
- "is_writable should still be nil after partial revoke"))))
+ "writable_table stays writable; revoked_table loses writability after its INSERT is revoked"))))
(finally
;; Clean up: Reset partial_revokes to OFF before exiting
(jdbc/execute! spec "SET GLOBAL partial_revokes = OFF;")