Skip to content

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

Additional Snowflake and Databricks config validation (#83126)

Nathan Voxlande745acbead1b
diff --git a/modules/drivers/databricks/src/metabase/driver/databricks.clj b/modules/drivers/databricks/src/metabase/driver/databricks.clj
index 4f8815d993d..8b79804e623 100644
--- a/modules/drivers/databricks/src/metabase/driver/databricks.clj
+++ b/modules/drivers/databricks/src/metabase/driver/databricks.clj
@@ -44,7 +44,13 @@
(defmethod driver/host-carrying-parameters :databricks
[_driver]
- ["ProxyHost" "OAuth2ConnAuthAuthorizationEndPoint" "OAuth2ConnAuthTokenEndpoint"])
+ ["ProxyHost" "CFProxyHost" "OAuth2ConnAuthAuthorizeEndpoint" "OAuth2ConnAuthTokenEndpoint"
+ "OAuth2AuthorizationEndPoint" "OAuth2TokenEndpoint" "OAuthDiscoveryURL" "OIDCDiscoveryEndpoint"])
+
+(defmethod driver/non-host-parameters :databricks
+ [_driver]
+ ["ProxyPort" "CFProxyPort" "UseProxy" "UseCFProxy" "UseSystemProxy" "ProxyIgnoreList" "OAuth2RedirectUrlPort"
+ "EnableOIDCDiscovery" "OAuthDiscoveryMode"])
(doseq [[feature supported?] {:basic-aggregations true
:binning true
diff --git a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj b/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
index bd3a3e53bed..37f48f4a62b 100644
--- a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
+++ b/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
@@ -12,6 +12,7 @@
[metabase.driver.databricks :as databricks]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync]
+ [metabase.driver.util :as driver.u]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -551,3 +552,22 @@
(lib/native-query (mt/metadata-provider))
(qp/process-query)
(mt/rows)))))))
+
+(deftest url-valued-connection-parameters-honor-network-policy-test
+ ;; the client fetches tokens and discovery documents from these, or routes through them, so a value naming an
+ ;; internal host has to be refused like the workspace host itself would be
+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "external-only"]
+ (let [details {:host "adb-1.azuredatabricks.net" :http-path "/sql/1.0/warehouses/x" :catalog "c" :token "t"}]
+ (doseq [opts ["OAuthDiscoveryURL=http://localhost/.well-known/openid-configuration"
+ "OIDCDiscoveryEndpoint=http://localhost/.well-known/openid-configuration"
+ "OAuth2AuthorizationEndPoint=http://localhost/authorize"
+ "OAuth2TokenEndpoint=http://localhost/token"
+ "OAuth2ConnAuthAuthorizeEndpoint=http://localhost/authorize"
+ "OAuth2ConnAuthTokenEndpoint=http://localhost/token"
+ "UseCFProxy=1;CFProxyHost=localhost;CFProxyPort=3128"
+ "UseProxy=1;ProxyHost=localhost;ProxyPort=3128"]]
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-hosts! :databricks (assoc details :additional-options opts))
+ nil
+ (catch clojure.lang.ExceptionInfo e (ex-data e))))
+ (str "should be refused: " opts))))))
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 2163200956d..f7e830509d7 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -33,6 +33,7 @@
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.http :as u.http]
[metabase.util.i18n :refer [deferred-tru tru]]
[metabase.util.json :as json]
[metabase.util.log :as log]
@@ -50,9 +51,44 @@
(driver/register! :snowflake, :parent #{:sql-jdbc ::sql-jdbc.legacy/use-legacy-classes-for-read-and-set})
+;; `authenticator` is left out: it names a host only for native Okta (`https://<okta-account>.okta.com`), and otherwise
+;; a flow such as `SNOWFLAKE_JWT`, which is not a name to hand the resolver. See the `connection-parameter-hosts` method
+;; below.
(defmethod driver/host-carrying-parameters :snowflake
[_driver]
- ["proxyHost" "host"])
+ ["proxyHost" "host" "serverURL" "oauthTokenRequestUrl" "oauthAuthorizationUrl"])
+
+(defmethod driver/non-host-parameters :snowflake
+ [_driver]
+ ["oauthRedirectUri" "nonProxyHosts" "proxyPort" "proxyUser" "proxyPassword" "proxyProtocol" "useProxy"
+ "disableSocksProxy" "allowUnderscoresInHost" "disableSamlURLCheck"])
+
+(defn- authenticator-values
+ "Every `authenticator` the client could be handed by `spec`: each one in the connection string, as written and as the
+ client percent-decodes it, and each property whose name matches regardless of case -- the client looks property
+ names up case-insensitively, and details the driver does not recognize are passed through as properties. All are
+ returned rather than guessing which one the client lets win. The connection string is `:connection-uri` when the
+ driver has built one (private-key auth does), otherwise `:subname`; both are read."
+ [spec]
+ (into (into []
+ (mapcat #(sql-jdbc.common/connection-string-parameter-values % "authenticator"))
+ [(:connection-uri spec) (:subname spec)])
+ (keep (fn [[k v]]
+ (when (= "authenticator" (u/lower-case-en (str/trim (name k))))
+ v)))
+ spec))
+
+(defmethod driver/connection-parameter-hosts :snowflake
+ [driver details]
+ (let [spec (try
+ (sql-jdbc.conn/connection-details->spec driver details)
+ ;; details that do not make a spec cannot open a connection either; the `:sql-jdbc` method below
+ ;; answers the same way
+ (catch Throwable _ nil))]
+ (into ((get-method driver/connection-parameter-hosts :sql-jdbc) driver details)
+ (comp (filter #(and (string? %) (re-find #"(?i)^\s*https?://" %)))
+ (keep u.http/->hostname))
+ (authenticator-values spec))))
(defmethod driver/connection-hosts :snowflake
[_driver {:keys [account host use-hostname]}]
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index afa995a0dbc..713824988c5 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -25,6 +25,7 @@
[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.sql.parameters.substitution :as sql.params.substitution]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.driver.util :as driver.u]
[metabase.events.core :as events]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -2214,3 +2215,57 @@
(t/zone-offset "+02:00")) "2026-07-08 01:02:03.123456789 +0200"
"not temporal" "not temporal"
42 42)))
+
+(deftest url-valued-connection-parameters-honor-network-policy-test
+ ;; the client connects to these instead of (or as well as) the account host, so a value naming an internal host has
+ ;; to be refused like the account host itself would be
+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "external-only"]
+ (let [check! #(driver.u/validate-connection-hosts! :snowflake {:account "acct" :db "db" :user "u" :password "p"
+ :additional-options %})
+ refusal #(try (check! %) nil (catch clojure.lang.ExceptionInfo e (ex-data e)))]
+ (testing "an internal host is refused whichever parameter names it"
+ (doseq [opts ["authenticator=OAUTH_CLIENT_CREDENTIALS&oauthTokenRequestUrl=http://localhost:18078/steal"
+ "oauthAuthorizationUrl=http://localhost/authorize"
+ "serverURL=https://localhost/"
+ "authenticator=https://localhost/"
+ "AUTHENTICATOR=http://localhost:8080/okta"]]
+ (is (=? {:status-code 400} (refusal opts))
+ (str "should be refused: " opts))))
+ (testing "the client percent-decodes names and values, so an encoded one is refused like the plain one"
+ (doseq [opts ["serverURL=https%3A%2F%2Flocalhost%2F"
+ "useProxy=true&proxy%48ost=localhost"
+ "useProxy=true&proxyHost=%6Cocalhost"
+ "authenticator=https%3A%2F%2Flocalhost%2F"
+ "authenticat%6Fr=https://localhost/"]]
+ (is (=? {:status-code 400} (refusal opts))
+ (str "should be refused: " opts))))
+ (testing "every `authenticator` is checked when one is given more than once"
+ (doseq [opts ["authenticator=SNOWFLAKE_JWT&authenticator=https://localhost/"
+ "authenticator=https://localhost/&authenticator=SNOWFLAKE_JWT"]]
+ (is (=? {:status-code 400} (refusal opts))
+ (str "should be refused: " opts))))
+ (testing "an `authenticator` naming a flow rather than a URL is not treated as a host"
+ (doseq [opts [nil
+ "authenticator=SNOWFLAKE_JWT"
+ "authenticator=OAUTH_CLIENT_CREDENTIALS"
+ "authenticator=externalbrowser"
+ "authenticator=SNOWFLAKE%5FJWT"]]
+ (is (nil? (check! opts))
+ (str "should be allowed: " opts))))
+ (testing "an `authenticator` detail key reaches the client as a connection property too, in any case"
+ (doseq [k [:authenticator :AUTHENTICATOR :Authenticator]]
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-hosts! :snowflake {:account "acct" :db "db" :user "u" :password "p"
+ k "https://localhost/"
+ :additional-options "authenticator=SNOWFLAKE_JWT"})
+ nil
+ (catch clojure.lang.ExceptionInfo e (ex-data e))))
+ (str "should be refused: " k)))))))
+
+(deftest authenticator-values-read-connection-uri-test
+ ;; private-key auth moves the connection string from `:subname` into `:connection-uri`
+ (testing "an `authenticator` in `:connection-uri` is found as well as one in `:subname`"
+ (doseq [k [:subname :connection-uri]]
+ (is (= ["https://localhost/"]
+ (#'driver.snowflake/authenticator-values {k "//acct.snowflakecomputing.com/?authenticator=https://localhost/"}))
+ (str "should be found in " k)))))
diff --git a/src/metabase/driver/sql_jdbc/common.clj b/src/metabase/driver/sql_jdbc/common.clj
index 145aa19d3ca..39dbaa5b870 100644
--- a/src/metabase/driver/sql_jdbc/common.clj
+++ b/src/metabase/driver/sql_jdbc/common.clj
@@ -4,7 +4,12 @@
[clojure.string :as str]
[metabase.util :as u]
[metabase.util.http :as u.http]
- [metabase.util.performance :refer [not-empty]]))
+ [metabase.util.performance :refer [not-empty]])
+ (:import
+ (java.net URLDecoder)
+ (java.nio.charset StandardCharsets)))
+
+(set! *warn-on-reflection* true)
(def ^:private valid-separator-styles #{:url :comma :semicolon})
@@ -129,25 +134,54 @@
{:connection-string connection-string})))
hosts))))
+(defn- percent-decoded
+ "`s` percent-decoded, or nil when it is not valid percent-encoding."
+ [^String s]
+ (try
+ (URLDecoder/decode s StandardCharsets/UTF_8)
+ (catch IllegalArgumentException _ nil)))
+
(defn- connection-string-parameters
"The `name=value` pairs in a JDBC `connection-string`, whichever separator style the driver that built it uses. Only
what follows the first separator is read: the authority (`//host:port/db`) is [[connection-string-hosts]]' business,
- not ours."
+ not ours.
+
+ Each pair is given as written and, when that differs, percent-decoded as well. Some clients (Snowflake's) decode
+ names and values after splitting the string, so `proxy%48ost=%6Cocalhost` reaches them as `proxyHost=localhost`;
+ others read the string as written. Answering with both reads it the way either kind of client would."
[connection-string]
(when-let [params (second (str/split (str connection-string) #"[?;,]" 2))]
(for [pair (str/split params #"[&;,]")
:let [[k v] (str/split pair #"=" 2)]
- :when (and k v)]
+ :when (and k v)
+ [k v] (distinct (cons [k v] (when-let [decoded-k (percent-decoded k)]
+ (when-let [decoded-v (percent-decoded v)]
+ [[decoded-k decoded-v]]))))]
[(str/trim k) (str/trim v)])))
+(defn connection-string-parameter-values
+ "Every value `connection-string` gives the parameter named `parameter-name` (matched case-insensitively), as written
+ and percent-decoded, in order. For a driver that reads a parameter itself rather than declaring it
+ in [[metabase.driver/host-carrying-parameters]] -- one whose value names a host only sometimes."
+ [connection-string parameter-name]
+ (let [parameter-name (u/lower-case-en parameter-name)]
+ (into []
+ (comp (filter (fn [[k _v]] (= parameter-name (u/lower-case-en k))))
+ (map second))
+ (connection-string-parameters connection-string))))
+
(defn- parameter-host [declared? backstop? [k v]]
(when-let [host (u.http/->hostname v)]
- (when (or (declared? (name k))
- ;; A parameter of the connection string that was not declared is read only when its value is already an
- ;; IP address. That covers a declaration that has fallen behind the client it describes without ever
- ;; resolving a value that may not be a host at all -- which would both hand the value to the resolver
- ;; and, for a name that happens to resolve inside the cluster, refuse a database over its username.
- (and backstop? (u.http/ip-literal? host)))
+ (when (and (or (declared? (name k))
+ ;; A parameter of the connection string that was not declared is read only when its value is
+ ;; already an IP address. That covers a declaration that has fallen behind the client it describes
+ ;; without ever resolving a value that may not be a host at all -- which would both hand the value to
+ ;; the resolver and, for a name that happens to resolve inside the cluster, refuse a database over
+ ;; its username.
+ (and backstop? (u.http/ip-literal? host)))
+ ;; still percent-encoded, so not a name any client resolves: one that decodes it is checked against
+ ;; the decoded pair, and one that does not fails to resolve it
+ (or (not (str/includes? host "%")) (u.http/ip-literal? host)))
host)))
(defn connection-parameter-hosts
diff --git a/test/metabase/driver/sql_jdbc/common_test.clj b/test/metabase/driver/sql_jdbc/common_test.clj
index c5ca592ac71..22e84480379 100644
--- a/test/metabase/driver/sql_jdbc/common_test.clj
+++ b/test/metabase/driver/sql_jdbc/common_test.clj
@@ -66,7 +66,27 @@
(is (= ["backup.corp"]
(sql-jdbc.common/connection-parameter-hosts "jdbc:vertica://real.example.com:5433/db"
{:backupservernode "backup.corp" :loginTimeout 10}
- ["backupservernode"])))))
+ ["backupservernode"]))))
+ (testing "a percent-encoded name or value is read as the client that decodes it would read it"
+ (are [conn-str declared expected]
+ (= expected (sql-jdbc.common/connection-parameter-hosts conn-str nil declared))
+ "jdbc:snowflake://acct.snowflakecomputing.com/?proxy%48ost=internal.corp" ["proxyHost"] ["internal.corp"]
+ "jdbc:snowflake://acct.snowflakecomputing.com/?proxyHost=%69nternal.corp" ["proxyHost"] ["internal.corp"]
+ "jdbc:snowflake://acct.snowflakecomputing.com/?serverURL=https%3A%2F%2F10.0.0.1%2F" nil ["10.0.0.1"]))
+ (testing "a value that is not valid percent-encoding is still read as written"
+ (is (= ["internal.corp"]
+ (sql-jdbc.common/connection-parameter-hosts "jdbc:x://h:1/db?host=internal.corp&password=100%zz"
+ nil ["host"])))))
+
+(deftest ^:parallel connection-string-parameter-values-test
+ (testing "every value given the parameter, case-insensitively, as written and percent-decoded"
+ (is (= ["SNOWFLAKE_JWT" "https://a.corp/" "https%3A%2F%2Fb.corp%2F" "https://b.corp/"]
+ (sql-jdbc.common/connection-string-parameter-values
+ "jdbc:snowflake://acct.snowflakecomputing.com/?authenticator=SNOWFLAKE_JWT&AUTHENTICATOR=https://a.corp/&user=u&authenticator=https%3A%2F%2Fb.corp%2F"
+ "authenticator"))))
+ (testing "nothing for a parameter that is absent, or a string with no parameters"
+ (is (= [] (sql-jdbc.common/connection-string-parameter-values "jdbc:x://h:1/db?user=u" "authenticator")))
+ (is (= [] (sql-jdbc.common/connection-string-parameter-values "jdbc:x://h:1/db" "authenticator")))))
(deftest ^:parallel connection-string-hosts-test
(testing "the authority is read whichever shape the driver built it in"