Skip to content

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

writable check for mysql (#62338)

Ngoc Khuatc4ab9abd8b1e
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
index e7d4eb15f4d..7a52131d18d 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
@@ -695,7 +695,8 @@
(fn [driver db options f]
(do-with-resolved-connection driver db options
(fn [conn]
- (driver/set-role! driver/*driver* conn role-a)
+ (when-not (:connection db)
+ (driver/set-role! driver/*driver* conn role-a))
(f conn))))]
(is (= default-table-set (tables-set)))))))))))))
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index eab1e80404b..1ba62d5d083 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -755,7 +755,10 @@
:database-routing
;; Does this driver support replication?
- :database-replication})
+ :database-replication
+
+ ;; whether this driver supports checking table writeable permissions
+ :metadata/table-writable-check})
(defmulti database-supports?
"Does this driver and specific instance of a database support a certain `feature`?
@@ -798,8 +801,9 @@
:upload-with-auto-pk true
:saved-question-sandboxing true
:test/dynamic-dataset-loading true
- :test/uuids-in-create-table-statements true
- :metadata/table-existence-check false}]
+ :test/uuids-in-create-table-statements true
+ :metadata/table-existence-check false
+ :metadata/table-writable-check false}]
(defmethod database-supports? [::driver feature] [_driver _feature _db] supported?))
;;; By default a driver supports `:native-parameter-card-reference` if it supports `:native-parameters` AND
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index 8bdd5be1794..37ea7e7e891 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -112,12 +112,34 @@
[driver conn]
(->> conn (sql-jdbc.sync/dbms-version driver) :flavor (= "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)
false
#_(and (= driver :mysql) (not (mariadb? db))))
+(defmethod driver/database-supports? [:mysql :metadata/table-writable-check]
+ [driver _feat db]
+ (and (= driver :mysql)
+ (not (mariadb? db))
+ (not (try
+ (partial-revokes-enabled? driver db)
+ (catch Exception e
+ (log/warn e "Failed to check table writable")
+ false)))))
+
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | metabase.driver impls |
;;; +----------------------------------------------------------------------------------------------------------------+
diff --git a/src/metabase/driver/sql_jdbc/sync/describe_database.clj b/src/metabase/driver/sql_jdbc/sync/describe_database.clj
index 36a7f14b9a5..6f4f379ff2c 100644
--- a/src/metabase/driver/sql_jdbc/sync/describe_database.clj
+++ b/src/metabase/driver/sql_jdbc/sync/describe_database.clj
@@ -193,6 +193,7 @@
(if (driver/database-supports? driver :table-privileges nil)
(let [privilege-map (build-privilege-map driver conn)]
(fn [{schema :schema table :name ttype :type} privilege]
+ (assert (#{:select :write} privilege))
;; driver/current-user-table-privileges does not return privileges for external table on redshift, and foreign
;; table on postgres, so we need to use the select method on them
;;
@@ -200,13 +201,18 @@
(if (#{[:postgres "FOREIGN TABLE"]}
[driver ttype])
(case privilege
- :select (sql-jdbc.sync.interface/have-select-privilege? driver conn schema table)
- :write nil) ; Foreign tables typically don't support write operations
+ :select (sql-jdbc.sync.interface/have-select-privilege? driver conn schema table)
+ :write nil) ; Foreign tables typically don't support write operations
(contains? (get-in privilege-map [schema table] #{}) privilege))))
- (fn [{schema :schema table :name} privilege]
- (case privilege
- :select (sql-jdbc.sync.interface/have-select-privilege? driver conn schema table)
- :write nil))))
+ (let [can-check-writable? (driver/database-supports? driver :metadata/table-writable-check {:connection conn})
+ check-writable-privilege-map (when can-check-writable?
+ (build-privilege-map driver conn))]
+ (fn [{schema :schema table :name} privilege]
+ (assert (#{:select :write} privilege))
+ (case privilege
+ :select (sql-jdbc.sync.interface/have-select-privilege? driver conn schema table)
+ :write (when can-check-writable?
+ (contains? (get-in check-writable-privilege-map [schema table] #{}) privilege)))))))
(defn fast-active-tables
"Default, fast implementation of `active-tables` best suited for DBs with lots of system tables (like Oracle). Fetch
diff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.clj
index 0d4a9bdc0a5..9316a14fb3f 100644
--- a/src/metabase/driver/util.clj
+++ b/src/metabase/driver/util.clj
@@ -232,9 +232,13 @@
(let [f (if *memoize-supports?* memoized-supports?* supports?*)]
(f driver feature database)))
+(def ^:private skip-internal-features
+ #{;; used intenrally during the sync process, does not really need to be hydrated
+ :metadata/table-writable-check})
+
(defn- features* [driver database]
(set (for [feature driver/features
- :when (supports? driver feature database)]
+ :when (and (not (skip-internal-features feature)) (supports? driver feature database))]
feature)))
(def ^:private memoized-features*
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 338c188e2ce..40df5037b9b 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -856,46 +856,96 @@
driver/*driver* conn schema table-name))))))))))))
;; table privileges for mysql currently has bug unfortunately
-#_(deftest sync-writable-test
- (mt/test-driver :mysql
- (when-not (mysql/mariadb? (mt/db))
- (testing "`sync-tables-and-database!` should set is_writable correctly based on table privileges"
- (tx/drop-if-exists-and-create-db! driver/*driver* "sync_writable_test")
- (let [details (tx/dbdef->connection-details :mysql :db {:database-name "sync_writable_test"})
- spec (sql-jdbc.conn/connection-details->spec :mysql details)]
- (try
- (doseq [stmt ["CREATE TABLE `readonly_table` (id INTEGER);"
- "CREATE TABLE `readwrite_table` (id INTEGER);"
- "CREATE TABLE `fullaccess_table` (id INTEGER);"
- "CREATE USER 'sync_writable_test_user' IDENTIFIED BY 'password';"]]
- (jdbc/execute! spec stmt))
+(deftest sync-writable-test
+ (mt/test-driver :mysql
+ (when-not (mysql/mariadb? (mt/db))
+ (testing "`sync-tables-and-database!` should set is_writable correctly based on table privileges"
+ (tx/drop-if-exists-and-create-db! driver/*driver* "sync_writable_test")
+ (let [details (tx/dbdef->connection-details :mysql :db {:database-name "sync_writable_test"})
+ spec (sql-jdbc.conn/connection-details->spec :mysql details)]
+ (try
+ (doseq [stmt ["CREATE TABLE `readonly_table` (id INTEGER);"
+ "CREATE TABLE `readwrite_table` (id INTEGER);"
+ "CREATE TABLE `fullaccess_table` (id INTEGER);"
+ "CREATE USER 'sync_writable_test_user' IDENTIFIED BY 'password';"]]
+ (jdbc/execute! spec stmt))
- (doseq [stmt ["GRANT SELECT ON sync_writable_test.`readonly_table` TO 'sync_writable_test_user'"
- "GRANT SELECT, INSERT ON sync_writable_test.`readwrite_table` TO 'sync_writable_test_user'"
- "GRANT SELECT, INSERT, UPDATE, DELETE ON sync_writable_test.`fullaccess_table` TO 'sync_writable_test_user'"]]
- (jdbc/execute! spec stmt))
+ (doseq [stmt ["GRANT SELECT ON sync_writable_test.`readonly_table` TO 'sync_writable_test_user'"
+ "GRANT SELECT, INSERT ON sync_writable_test.`readwrite_table` TO 'sync_writable_test_user'"
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON sync_writable_test.`fullaccess_table` TO 'sync_writable_test_user'"]]
+ (jdbc/execute! spec stmt))
+
+ (let [user-connection-details (assoc details
+ :user "sync_writable_test_user"
+ :password "password"
+ :ssl true
+ :additional-options "trustServerCertificate=true")]
+ (mt/with-temp [:model/Database database {:engine "mysql", :details user-connection-details}]
+ (sync/sync-database! database)
+ (testing "only fullaccess table has writeable permissions"
+ (is (= {"readonly_table" false
+ "readwrite_table" false
+ "fullaccess_table" true}
+ (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))))
+ (testing "After granting full access to all tables and re-syncing"
+ (doseq [table-name ["readonly_table" "readwrite_table"]]
+ (jdbc/execute! spec (format "GRANT INSERT, UPDATE, DELETE ON sync_writable_test.`%s` TO 'sync_writable_test_user'" table-name)))
+
+ (sync/sync-database! database)
+ (is (= {"readonly_table" true
+ "readwrite_table" true
+ "fullaccess_table" true}
+ (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))))))
+ (finally
+ (jdbc/execute! spec "DROP USER IF EXISTS 'sync_writable_test_user';"))))))))
+
+(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"
+ (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);"
+ "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'"]]
+ (jdbc/execute! spec stmt))
- (let [user-connection-details (assoc details
- :user "sync_writable_test_user"
- :password "password"
- :ssl true
- :additional-options "trustServerCertificate=true")]
- (mt/with-temp [:model/Database database {:engine "mysql", :details user-connection-details}]
+ (let [user-connection-details (assoc details
+ :user "partial_revokes_test_user"
+ :password "password"
+ :ssl true
+ :additional-options "trustServerCertificate=true")]
+ (mt/with-temp [:model/Database database {:engine "mysql", :details user-connection-details}]
+ (testing "With partial_revokes OFF (default), metadata/table-writable-check is supported"
+ (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")
+
+ (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
(sync/sync-database! database)
- (testing "only fullaccess table has writeable permissions"
- (is (= {"readonly_table" false
- "readwrite_table" false
- "fullaccess_table" true}
- (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))))
- (testing "After granting full access to all tables and re-syncing"
- (doseq [table-name ["readonly_table" "readwrite_table"]]
- (jdbc/execute! spec (format "GRANT INSERT, UPDATE, DELETE ON sync_writable_test.`%s` TO 'sync_writable_test_user'" table-name)))
-
- (sync/sync-database! database)
- (is (= {"readonly_table" false
- "readwrite_table" false
- "fullaccess_table" true}
- (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))))))
-
- (finally
- (jdbc/execute! spec "DROP USER IF EXISTS 'sync_writable_test_user';"))))))))
+ (is (= {"test_table" nil}
+ (t2/select-fn->fn :name :is_writable :model/Table :db_id (:id database)))
+ "is_writable should still be nil after partial revoke"))))
+
+ (finally
+ ;; Clean up: Reset partial_revokes to OFF before exiting
+ (jdbc/execute! spec "SET GLOBAL partial_revokes = OFF;")
+ (jdbc/execute! spec "DROP USER IF EXISTS 'partial_revokes_test_user';"))))))))