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
Sanitize database details during serialization import (#71311)
Ryan Laurie88a87db2b8d2
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex f0fedddd84a..6afacd0a397 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -176,6 +176,7 @@clojure.core.async/to-chan!clojure.core.async/to-chan!!metabase.channel.core/send!+ metabase.warehouses.models.database/assert-not-h2!metabase.driver.sql-jdbc.execute/execute-prepared-statement!metabase.notification.models/create-notification!metabase.pulse.send/send-pulse!diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.cljindex 009e6970582..9d9514cdb47 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj@@ -15,6 +15,7 @@[metabase.test :as mt][metabase.test.generate :as test-gen][metabase.util.yaml :as yaml]+ [metabase.warehouses.models.database :as models.database][reifyhealth.specmonstah.core :as rs][toucan2.core :as t2])(:import@@ -26,7 +27,8 @@;; `reindex!` below is ok in a parallel test since it's not actually executing anything#_{:clj-kondo/ignore [:metabase/validate-deftest]}(use-fixtures :each (fn [thunk]- (mt/with-dynamic-fn-redefs [search/reindex! (constantly nil)]+ (mt/with-dynamic-fn-redefs [search/reindex! (constantly nil)+ models.database/assert-not-h2! (constantly nil)](thunk))))(defn- dir->contents-set [p ^File dir]diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.cljindex 14ce058d05f..50701d228f1 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj@@ -17,12 +17,14 @@[metabase.util :as u][metabase.util.json :as json][metabase.util.log.capture :as log.capture]+ [metabase.warehouses.models.database :as models.database][toucan2.core :as t2]));; `reindex!` below is ok in a parallel test since it's not actually executing anything#_{:clj-kondo/ignore [:metabase/validate-deftest]}(use-fixtures :each (fn [thunk]- (mt/with-dynamic-fn-redefs [search/reindex! (constantly nil)]+ (mt/with-dynamic-fn-redefs [search/reindex! (constantly nil)+ models.database/assert-not-h2! (constantly nil)](thunk))))(defn- no-labels [path]diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.cljindex 56e912ccb31..eaa0615f3f0 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/round_trip_test.clj@@ -25,7 +25,7 @@[clojure.java.io :as io][clojure.set :as set][clojure.string :as str]- [clojure.test :refer [deftest is testing]]+ [clojure.test :refer [deftest is testing use-fixtures]][clojure.walk :as walk][metabase-enterprise.serialization.v2.extract :as extract][metabase-enterprise.serialization.v2.ingest :as ingest]@@ -37,12 +37,18 @@[metabase.search.test-util :as search.tu][metabase.test :as mt][metabase.util.log :as log]- [metabase.util.yaml :as yaml])+ [metabase.util.yaml :as yaml]+ [metabase.warehouses.models.database :as models.database])(:import(java.io File)(java.nio.file Files Path StandardCopyOption)(java.nio.file.attribute FileAttribute)))+#_{:clj-kondo/ignore [:metabase/validate-deftest]}+(use-fixtures :each (fn [thunk]+ (mt/with-dynamic-fn-redefs [models.database/assert-not-h2! (constantly nil)]+ (thunk))))+(set! *warn-on-reflection* true)(def ^File source-dir (io/as-file (io/resource "serialization_baseline")))diff --git a/src/metabase/driver.clj b/src/metabase/driver.cljindex c36354d062c..71f618f891d 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -232,18 +232,28 @@(defmethod llm-sql-dialect-resource :default [_] nil)+(def disallowed-db-detail-keys+ "Low-level JDBC keys that should not appear in database connection details."+ #{"classname" "subprotocol" "connection-uri" "subname" "init"})++(defn sanitize-db-details+ "Remove [[disallowed-db-detail-keys]] from a database details map."+ [details]+ (when (map? details)+ (into {} (remove (fn [[k _]] (disallowed-db-detail-keys (u/lower-case-en (name k))))) details)))+(defn dispatch-on-initialized-driver-safe-keys"Dispatch on initialized driver, except checks for `classname`,`subprotocol`, `connection-uri` in the details map in order toprevent a mismatch in spec type vs driver."[driver details-map]- (let [invalid-keys #{"classname" "subprotocol" "connection-uri"}- ks (->> details-map keys- (map name)- (map u/lower-case-en) set)]- (when (seq (set/intersection ks invalid-keys))- (throw (ex-info "Cannot specify subname, protocol, or connection-uri in details map"- {:invalid-keys (set/intersection ks invalid-keys)})))+ (let [ks (->> details-map keys+ (map name)+ (map u/lower-case-en) set)]+ (let [invalid (set/intersection ks disallowed-db-detail-keys)]+ (when (seq invalid)+ (throw (ex-info (str "Cannot specify " (str/join ", " (sort invalid)) " in details map")+ {:invalid-keys invalid}))))(dispatch-on-initialized-driver driver)))(defmulti can-connect?diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 163a071a532..83d1316a229 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -549,7 +549,7 @@(defmethod sql-jdbc.conn/connection-details->spec :h2[_ details]{:pre [(map? details)]}- (driver-api/spec :h2 (cond-> details+ (driver-api/spec :h2 (cond-> (driver/sanitize-db-details details)(string? (:db details)) (update :db connection-string-set-safe-options))))(defmethod sql-jdbc.sync/active-tables :h2diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.cljindex b4a89f63721..d2295efb456 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -692,6 +692,21 @@;; ["databases" "db_name" "db_name"] directory for the database with same-named file inside.["databases" name name])+(defn assert-not-h2!+ "Validate db is not h2 for serialization import"+ [ingested]+ (when (= :h2 (keyword (:engine ingested)))+ (throw (ex-info "h2 is not supported for serialization import"+ {:engine (:engine ingested) :name (:name ingested)}))))++(defmethod serdes/load-one! "Database"+ [ingested maybe-local]+ (assert-not-h2! ingested)+ (serdes/default-load-one! (cond-> ingested+ (:details ingested) (update :details driver/sanitize-db-details)+ (:write_data_details ingested) (update :write_data_details driver/sanitize-db-details))+ maybe-local))+(def ^{:arglists '([table-id])} table-id->database-id"Retrieve the `Database` ID for the given table-id."(mdb/memoize-for-application-dbdiff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex 7c2fb606fbc..0bb540db63f 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -90,17 +90,17 @@(catch Exception e e)))](testing "connection-uri"(let [result (f {:connection-uri conn})]- (is (= "Cannot specify subname, protocol, or connection-uri in details map"+ (is (= "Cannot specify connection-uri in details map"(ex-message result)))(is (= {:invalid-keys #{"connection-uri"}} (ex-data result)))))(testing "subprotocol"(let [result (f {:db conn, :subprotocol "h2"})]- (is (= "Cannot specify subname, protocol, or connection-uri in details map"+ (is (= "Cannot specify subprotocol in details map"(ex-message result)))(is (= {:invalid-keys #{"subprotocol"}} (ex-data result)))))- (testing "subprotocol"+ (testing "classname"(let [result (f {:db conn, :classname "org.h2.Driver"})]- (is (= "Cannot specify subname, protocol, or connection-uri in details map"+ (is (= "Cannot specify classname in details map"(ex-message result)))(is (= {:invalid-keys #{"classname"}} (ex-data result))))))))