Skip to content

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

[ClickHouse] Handle SSH tunneling when checking for `:connection-impersonation` support (#62743)

Braden Shepherdsone20f7e23820f
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index e7ddea4b980..bdd980842c5 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -277,7 +277,8 @@
[_driver _feature db]
(if db
(try (clickhouse-version/is-at-least? 24 4 db)
- (catch Throwable _e
+ (catch Throwable e
+ (log/warn e "Error checking connection impersonation")
false))
false))
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_version.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_version.clj
index c703369b1ad..17d043efaad 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_version.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_version.clj
@@ -19,12 +19,13 @@
(str "WITH s AS (SELECT version() AS ver, splitByChar('.', ver) AS verSplit) "
"SELECT s.ver, toInt32(verSplit[1]), toInt32(verSplit[2]) FROM s"))
-(def ^:private ^{:arglists '([db-details])} get-clickhouse-version
+(def ^:private ^{:arglists '([database])} get-clickhouse-version
(memoize/ttl
- (fn [db-details]
+ ^{:clojure.core.memoize/args-fn (fn [[database]] (:details database))}
+ (fn [database]
(sql-jdbc.execute/do-with-connection-with-options
:clickhouse
- (sql-jdbc.conn/connection-details->spec :clickhouse db-details)
+ (sql-jdbc.conn/db->pooled-connection-spec database)
nil
(fn [^java.sql.Connection conn]
(with-open [stmt (.createStatement conn)
@@ -37,7 +38,7 @@
(defmethod driver/dbms-version :clickhouse
[_driver db]
- (get-clickhouse-version (:details db)))
+ (get-clickhouse-version db))
(defn is-at-least?
"Is ClickHouse version at least `major.minor` (e.g., 24.4)?"
diff --git a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_impersonation_test.clj b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_impersonation_test.clj
index 83739b692a8..8be421365b4 100644
--- a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_impersonation_test.clj
+++ b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_impersonation_test.clj
@@ -1,17 +1,21 @@
(ns ^:mb/driver-tests metabase.driver.clickhouse-impersonation-test
"SET ROLE (connection impersonation feature) tests with single node or on-premise cluster setups."
(:require
+ [clojure.core.memoize :as memoize]
[clojure.test :refer :all]
[metabase-enterprise.impersonation.util-test :as impersonation.tu]
[metabase.driver :as driver]
+ [metabase.driver.clickhouse-version :as clickhouse.version]
[metabase.driver.sql :as driver.sql]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
+ [metabase.driver.test-util :as driver.tu]
[metabase.query-processor.store :as qp.store]
[metabase.sync.core :as sync.core]
[metabase.test :as mt]
[metabase.test.data.clickhouse :as ctd]
[metabase.util :as u]
+ [toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp])
(:import [java.sql SQLException]))
@@ -175,3 +179,41 @@
(check-impersonation! "row_a,row_c" [["a"] ["c"]])
(check-impersonation! "row_b,row_c" [["b"] ["c"]])
(check-impersonation! "row_a,row_b,row_c" [["a"] ["b"] ["c"]]))))))))
+
+(defn- with-ssh-tunnel*! [tunnel-details f]
+ (let [base-details (t2/select-one-fn :details 'Database :id (mt/id))]
+ ;; Set up SSH tunnel
+ (t2/update! 'Database (mt/id) {:details (merge base-details tunnel-details)})
+ ;; Discard any existing connection pool to make sure the new one uses it.
+ (sql-jdbc.conn/invalidate-pool-for-db! (mt/id))
+ ;; Run the test body
+ (f)
+ ;; Clean up
+ (t2/update! 'Database (mt/id) {:details base-details})
+ (sql-jdbc.conn/invalidate-pool-for-db! (mt/id))))
+
+(defmacro ^:private with-ssh-tunnel! [tunnel-details & body]
+ `(with-ssh-tunnel*! ~tunnel-details (^:once fn* [] ~@body)))
+
+(deftest impersonation-support-check-over-ssh-tunnel-test
+ (testing "driver-supports? :connection-impersonation should work through an SSH tunnel (#62377)"
+ ;; Use an in-memory SSH server that forwards to the correct port through localhost.
+ ;; Since the real database is still reachable locally, it's possible for the driver to ignore the SSH tunnel and
+ ;; still connect to the DB. So this test runs twice, once with all correct and once with an incorrect SSH
+ ;; password. The latter check should fail (returning false for `driver-supports?`) if the SSH tunnel is being
+ ;; respected.
+ (mt/test-driver :clickhouse
+ (let [username "username", password "password"]
+ (with-open [ssh-server (driver.tu/basic-auth-ssh-server username password)]
+ (doseq [[correct-password? ssh-password] [[true password] [false "wrong-password"]]]
+ (memoize/memo-clear! @#'clickhouse.version/get-clickhouse-version)
+ (let [ssh-port (.getPort ssh-server)]
+ (with-ssh-tunnel! {:tunnel-enabled true
+ :tunnel-host "localhost"
+ :tunnel-auth-option "password"
+ :tunnel-port ssh-port
+ :tunnel-user username
+ :tunnel-pass ssh-password}
+ (testing (str "using " ssh-password)
+ (is (= correct-password?
+ (driver/database-supports? :clickhouse :connection-impersonation (mt/id)))))))))))))
diff --git a/test/metabase/actions/actions_test.clj b/test/metabase/actions/actions_test.clj
index af36093865c..47fa4981864 100644
--- a/test/metabase/actions/actions_test.clj
+++ b/test/metabase/actions/actions_test.clj
@@ -10,6 +10,7 @@
[metabase.api.common :refer [*current-user-permissions-set*]]
[metabase.driver :as driver]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
+ [metabase.driver.test-util :as driver.tu]
[metabase.query-processor :as qp]
[metabase.sync.core :as sync]
[metabase.test :as mt]
@@ -17,10 +18,7 @@
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [toucan2.core :as t2])
- (:import
- (org.apache.sshd.server SshServer)
- (org.apache.sshd.server.forward AcceptAllForwardingFilter)))
+ [toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -504,26 +502,6 @@
[3 "Artisan"]]
(first-three-categories)))))))))
-(defn basic-auth-ssh-server ^java.io.Closeable [username password]
- (try
- (let [password-auth (reify org.apache.sshd.server.auth.password.PasswordAuthenticator
- (authenticate [_ auth-username auth-password _session]
- (and
- (= auth-username username)
- (= auth-password password))))
- keypair-provider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider.)
- sshd (doto (SshServer/setUpDefaultServer)
- (.setPort 0)
- (.setKeyPairProvider keypair-provider)
- (.setPasswordAuthenticator password-auth)
- (.setForwardingFilter AcceptAllForwardingFilter/INSTANCE)
- .start)]
- sshd)
- (catch Throwable e
- (throw (ex-info (format "Error starting SSH mock server with password")
- {:username username :password password}
- e)))))
-
(deftest actions-on-ssh-tunneled-db
;; testing actions against dbs with ssh tunnels. Use an in-memory ssh server that just forwards to the correct port
;; through localhost. Since it is local, it's possible for the application to ignore the ssh tunnel and just talk to
@@ -532,10 +510,10 @@
;; through the ssh tunnel
(mt/test-drivers (disj (mt/normal-drivers-with-feature :actions) :h2)
(let [username "username", password "password"]
- (with-open [ssh-server (basic-auth-ssh-server username password)]
+ (with-open [ssh-server (driver.tu/basic-auth-ssh-server username password)]
(doseq [[correct-password? ssh-password] [[true password] [false "wrong-password"]]]
(with-actions-test-data-and-actions-permissively-enabled!
- (let [ssh-port (.getPort ^SshServer ssh-server)
+ (let [ssh-port (.getPort ssh-server)
table-id (mt/id :categories)]
(let [details (t2/select-one-fn :details 'Database :id (mt/id))]
(t2/update! 'Database (mt/id)
diff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.clj
index fa4c7b9e3a4..38d9666c162 100644
--- a/test/metabase/driver/sql_jdbc/connection_test.clj
+++ b/test/metabase/driver/sql_jdbc/connection_test.clj
@@ -144,11 +144,21 @@
hash-change-fn (fn [db-id]
(is (= (u/the-id db) db-id))
(swap! hash-change-called-times inc)
- nil)]
+ nil)
+ ;; HACK: The ClickHouse driver also calls `db->pooled-connection-spec` to answer
+ ;; `driver-supports? :connection-impersonation`. That perturbs the call count, so add a special case
+ ;; to [[driver.u/supports?]].
+ original-supports? driver.u/supports?
+ supports?-fn (fn [driver feature database]
+ (if (and #_{:clj-kondo/ignore [:metabase/disallow-hardcoded-driver-names-in-tests]}
+ (= driver :clickhouse)
+ (= feature :connection-impersonation))
+ true
+ (original-supports? driver feature database)))]
(try
(sql-jdbc.conn/invalidate-pool-for-db! db)
- ;; a little bit hacky to redefine the log fn, but it's the most direct way to test
- (with-redefs [sql-jdbc.conn/log-jdbc-spec-hash-change-msg! hash-change-fn]
+ (with-redefs [sql-jdbc.conn/log-jdbc-spec-hash-change-msg! hash-change-fn
+ driver.u/supports? supports?-fn]
(let [pool-spec-1 (sql-jdbc.conn/db->pooled-connection-spec db)
db-hash-1 (get @@#'sql-jdbc.conn/database-id->jdbc-spec-hash (u/the-id db))]
(testing "hash value calculated correctly for new pooled conn"
diff --git a/test/metabase/driver/test_util.clj b/test/metabase/driver/test_util.clj
index 6aba69de58c..4dd8666987b 100644
--- a/test/metabase/driver/test_util.clj
+++ b/test/metabase/driver/test_util.clj
@@ -2,7 +2,12 @@
(:require
[mb.hawk.parallel]
[metabase.driver.events.report-timezone-updated]
- [metabase.test.initialize :as initialize]))
+ [metabase.test.initialize :as initialize])
+ (:import
+ (org.apache.sshd.server SshServer)
+ (org.apache.sshd.server.forward AcceptAllForwardingFilter)))
+
+(set! *warn-on-reflection* true)
(defn -notify-all-databases-updated! []
(mb.hawk.parallel/assert-test-is-not-parallel `-notify-all-databases-updated!)
@@ -19,3 +24,26 @@
~@body
(finally
(-notify-all-databases-updated!)))))
+
+(defn basic-auth-ssh-server
+ ;^java.io.Closeable
+ ^SshServer
+ [username password]
+ (try
+ (let [password-auth (reify org.apache.sshd.server.auth.password.PasswordAuthenticator
+ (authenticate [_ auth-username auth-password _session]
+ (and
+ (= auth-username username)
+ (= auth-password password))))
+ keypair-provider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider.)
+ sshd (doto (SshServer/setUpDefaultServer)
+ (.setPort 0)
+ (.setKeyPairProvider keypair-provider)
+ (.setPasswordAuthenticator password-auth)
+ (.setForwardingFilter AcceptAllForwardingFilter/INSTANCE)
+ .start)]
+ sshd)
+ (catch Throwable e
+ (throw (ex-info (format "Error starting SSH mock server with password")
+ {:username username :password password}
+ e)))))