The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
introduce validate-db-details multimethod (#74343)
Nicola Mometto3610fda57891
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.mdindex 6e8b576b409..670a9f0e484 100644--- a/docs/developers-guide/driver-changelog.md+++ b/docs/developers-guide/driver-changelog.md@@ -89,6 +89,9 @@ title: Driver interface changelog## Metabase 0.57.0+- Added `metabase.driver/validate-db-details!` multimethod for rejecting connection details that are unsafe to+ persist (independent of whether the database is currently reachable). The default implementation is a no-op.+- `driver/field-reference-mlv2` is now deprecated, and is no longer used. Please remove your implementations.- The key `metabase.driver-api.core/qp.add.nfc-path` is now more consistently populated; other `qp.add.*` keys nodiff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.cljindex 457f5e2b6f4..4a61f1c0374 100644--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj@@ -3,6 +3,7 @@[metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.api.routes.common :refer [+auth]]+ [metabase.driver :as driver][metabase.driver.util :as driver.u][metabase.events.core :as events][metabase.settings.core :as setting]@@ -22,9 +23,9 @@(api.macros/defendpoint :post "/destination-database""Create new Destination Databases.- Note that unlike the normal `POST /api/database` endpoint, does NOT check the details before adding the Database.-- This is OK, it's not an invariant that all database details are always valid, but it's something to note."+ Note that unlike the normal `POST /api/database` endpoint, this endpoint does not test that the database is actually+ reachable before adding it — destination details are not required to be valid at creation time, and an+ unreachable destination is fine."[_route-params{:keys [check_connection_details]} :- [:map[:check_connection_details {:optional true} ms/MaybeBooleanValue]]@@ -39,14 +40,17 @@(api/check-400 (not (t2/exists? :model/Database :router_database_id router_database_id :name [:in (map :name destinations)]))"A destination database with that name already exists.")(let [{:keys [engine auto_run_queries is_on_demand] :as router-db} (t2/select-one :model/Database :id router_database_id)]- (if-let [invalid-destinations (and check_connection_details- (->> destinations- (keep (fn [{details :details n :name}]- (let [details-or-error (warehouses/test-connection-details engine details)- valid? (not= (:valid details-or-error) false)]- (when-not valid?- [n (dissoc details-or-error :valid)]))))- seq))]+ (if-let [invalid-destinations (->> destinations+ (keep (fn [{details :details n :name}]+ (try+ (driver/validate-db-details! engine details)+ (when check_connection_details+ (let [details-or-error (warehouses/test-connection-details engine details)]+ (when (false? (:valid details-or-error))+ [n (dissoc details-or-error :valid)])))+ (catch Throwable e+ [n {:message (ex-message e)}]))))+ seq)]{:status 400:body (into {} invalid-destinations)}(u/prog1 (t2/insert-returning-instances!diff --git a/src/metabase/driver.clj b/src/metabase/driver.cljindex da8ebb373a0..a2d92496786 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -266,6 +266,14 @@dispatch-on-initialized-driver-safe-keys:hierarchy #'hierarchy)+(defmulti validate-db-details!+ "Throw if `details` are unsafe to persist for `driver`, independent of whether the database is currently reachable."+ {:added "0.57.0" :arglists '([driver details])}+ dispatch-on-initialized-driver-safe-keys+ :hierarchy #'hierarchy)++(defmethod validate-db-details! :default [_driver _details] nil)+(defmulti dbms-version"Return a map containing information that describes the version of the DBMS. This typically includes a`:version` containing the (semantic) version of the DBMS as a string and potentially a `:flavor`diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 6d3c3c677c3..548f7371bec 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -134,8 +134,8 @@bad-markers))](pred s)))-(defmethod driver/can-connect? :h2- [driver {:keys [db] :as details}]+(defmethod driver/validate-db-details! :h2+ [_driver {:keys [db]}](when-not driver.settings/*allow-testing-h2-connections*(throw (ex-info (tru "H2 is not supported as a data warehouse") {:status-code 400})))(when (string? db)@@ -151,7 +151,11 @@;; keys are uppercased by h2 when parsed:;; https://github.com/h2database/h2database/blob/master/h2/src/main/org/h2/engine/ConnectionInfo.java#L298(when (contains? properties "INIT")- (throw (ex-info "INIT not allowed" {:keys ["INIT"]})))))+ (throw (ex-info "INIT not allowed" {:keys ["INIT"]}))))))++(defmethod driver/can-connect? :h2+ [driver details]+ (driver/validate-db-details! driver details)(sql-jdbc.conn/can-connect? driver details))(defmethod driver/db-start-of-week :h2diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex 31df543e24c..c21f7d38f7b 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -214,13 +214,16 @@(def ^:private disallowed-additional-opts #"(?:allowLoadLocalInfile|allowLoadLocalInfileInPath|allowUrlInLocalInfile|autoDeserialize|serverRSAPublicKeyFile)")+(defmethod driver/validate-db-details! :mysql+ [_driver details]+ (when-let [match (some->> (:additional-options details) (re-find disallowed-additional-opts))]+ (throw (ex-info "Potentially dangerous keys in additional options" {:disallowed-key match}))))+(defmethod driver/can-connect? :mysql[driver details];; delegate to parent method to check whether we can connect; if so, check if it's an unsupported version and issue;; a warning if it is- (let [match (some->> (:additional-options details) (re-find disallowed-additional-opts))]- (when match- (throw (ex-info "Potentially dangerous keys in additional options" {:disallowed-key match}))))+ (driver/validate-db-details! driver details)(when ((get-method driver/can-connect? :sql-jdbc) driver details)(warn-on-unsupported-versions driver details)true))