Skip to content

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

Snowflake passphrase protected private keys (#79542)

Riley Thompson33ac781b2590
diff --git a/docs/databases/connections/snowflake.md b/docs/databases/connections/snowflake.md
index fbcd6278714..1d5316c8e97 100644
--- a/docs/databases/connections/snowflake.md
+++ b/docs/databases/connections/snowflake.md
@@ -61,6 +61,10 @@ The password for the username that you use to connect to the database.
Not required. You have the option of using a **Local file path** or an **Uploaded file path**. If you're on Metabase Cloud, you'll need to select **Uploaded file path** and upload your certificate.
+### Private key passphrase
+
+Only required if your private key is encrypted.
+
### Warehouse
Snowflake warehouse. If the user lacks a default warehouse, you'll need to enter the warehouse to connect to.
diff --git a/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml b/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
index 0c0b5edd0a2..51f4c19d0f8 100644
--- a/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
+++ b/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
@@ -35,6 +35,13 @@ driver:
visible-if:
use-password: false
required: true
+ - name: private-key-passphrase
+ display-name: Private key passphrase
+ helper-text: Only needed if your private key is encrypted.
+ type: password
+ placeholder: "********"
+ visible-if:
+ use-password: false
- merge:
- password
- visible-if:
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 045b5b89099..5ebfaddecc0 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -1,6 +1,6 @@
(ns metabase.driver.snowflake
"Snowflake Driver."
- (:refer-clojure :exclude [select-keys not-empty mapv])
+ (:refer-clojure :exclude [some select-keys not-empty mapv])
(:require
[buddy.core.codecs :as codecs]
[clojure.java.jdbc :as jdbc]
@@ -37,7 +37,7 @@
[metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
- [metabase.util.performance :refer [mapv not-empty select-keys]]
+ [metabase.util.performance :refer [mapv not-empty select-keys some]]
[ring.util.codec :as codec])
(:import
(java.io File)
@@ -99,14 +99,20 @@
(defmethod driver/humanize-connection-error-message :snowflake
[_ messages]
- (let [message (first messages)]
- (log/spy :error (type message))
- (condp re-matches message
- #"(?s).*Object does not exist.*$"
+ (let [message (first messages)
+ any-message? (fn [re] (boolean (some (partial re-find re) (filter string? messages))))]
+ (cond
+ (any-message? #"(?s)privateKeyPwd.*is null")
+ (tru "This private key is encrypted. Enter the passphrase to connect.")
+
+ (any-message? #"(?s)Error finalising cipher|unable to read encrypted data")
+ (tru "The passphrase for this private key is incorrect.")
+
+ (and (string? message) (re-matches #"(?s).*Object does not exist.*$" message))
:database-name-incorrect
- ; default - the Snowflake errors have a \n in them
- message)))
+ ;; default - the Snowflake errors have a \n in them
+ :else message)))
(defmethod driver/db-start-of-week :snowflake
[_]
@@ -124,7 +130,7 @@
[]
(inc (driver.common/start-of-week->int)))
-(defn- handle-conn-uri [details user account private-key-file]
+(defn- handle-conn-uri [details user account private-key-file private-key-passphrase]
(let [existing-conn-uri (or (:connection-uri details)
(when-let [sub (:subname details)]
(format "jdbc:snowflake:%s" sub))
@@ -133,7 +139,10 @@
(cond-> {:user (codec/url-encode user)
:private_key_file (codec/url-encode (.getCanonicalPath ^File private-key-file))}
(:db details)
- (assoc :db (codec/url-encode (:db details)))))
+ (assoc :db (codec/url-encode (:db details)))
+
+ (not (str/blank? private-key-passphrase))
+ (assoc :private_key_pwd (codec/form-encode private-key-passphrase))))
new-conn-uri (sql-jdbc.common/conn-str-with-additional-opts existing-conn-uri :url opts-str)]
(-> details
(assoc :connection-uri new-conn-uri)
@@ -145,15 +154,20 @@
Setting the Snowflake driver property privatekey would be easier, but that doesn't work
because clojure.java.jdbc (properly) converts the property values into strings while the
Snowflake driver expects a java.security.PrivateKey instance."
- [{:keys [user password account]
+ [{:keys [user password account private-key-passphrase]
:as details}]
(if password
details
(if-let [private-key-file (driver-api/secret-value-as-file! :snowflake details "private-key")]
(-> details
(driver-api/clean-secret-properties-from-details :snowflake)
- (handle-conn-uri user account private-key-file)
- (assoc :private_key_file private-key-file))
+ (handle-conn-uri user account private-key-file private-key-passphrase)
+ (assoc :private_key_file private-key-file)
+ ;; We need to put the `:private_key_pwd` property in both the `:connection-uri` and the connection spec.
+ ;; It uses the raw value here in the connection spec, but needs to use `codec/form-encode` for the `:connection-uri`.
+ (cond-> (not (str/blank? private-key-passphrase))
+ (assoc :private_key_pwd private-key-passphrase))
+ (dissoc :private-key-passphrase))
(driver-api/clean-secret-properties-from-details details :snowflake))))
(defn- quote-name
@@ -207,7 +221,8 @@
(-> details
;; Setting private-key-value to nil will delete the secret
(assoc :use-password true :private-key-value nil)
- (dissoc :private-key-id :private-key-path :private-key-options)
+ (dissoc :private-key-id :private-key-path :private-key-options
+ :private-key-passphrase)
;; Add meta for testing
(with-meta {:auth :password})))
private-key-path-details (when private-key-path
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index 9c4bea84408..cdb24ee6ac5 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -52,7 +52,13 @@
[metabase.warehouse-schema.models.field-user-settings :as field-user-settings]
[metabase.warehouses.models.database :as database]
[ring.util.codec :as codec]
- [toucan2.core :as t2]))
+ [toucan2.core :as t2])
+ (:import
+ (java.io StringWriter)
+ (java.security KeyFactory)
+ (java.security.spec PKCS8EncodedKeySpec)
+ (org.bouncycastle.openssl PKCS8Generator)
+ (org.bouncycastle.openssl.jcajce JcaPEMWriter JcaPKCS8Generator JceOpenSSLPKCS8EncryptorBuilder)))
(set! *warn-on-reflection* true)
@@ -199,6 +205,39 @@
(is (= "Metabase_Metabase"
(:application (sql-jdbc.conn/connection-details->spec :snowflake details)))))))
+(defn- pem->private-key
+ [pem]
+ (let [encoded (-> pem
+ (str/replace #"-----(?:BEGIN|END) (?:\p{Alnum}+ )?PRIVATE KEY-----" "")
+ (str/replace #"\s" "")
+ u/decode-base64-to-bytes
+ (PKCS8EncodedKeySpec.))]
+ (.generatePrivate (KeyFactory/getInstance "RSA") encoded)))
+
+(defn- encrypt-pkcs8-pem
+ [private-key ^String passphrase]
+ (let [encryptor (-> (JceOpenSSLPKCS8EncryptorBuilder. PKCS8Generator/AES_256_CBC)
+ (.setPassword (.toCharArray passphrase))
+ (.build))
+ sw (StringWriter.)]
+ (with-open [pw (JcaPEMWriter. sw)]
+ (.writeObject pw (JcaPKCS8Generator. private-key encryptor)))
+ (str sw)))
+
+(deftest ^:parallel private-key-passphrase-test
+ (mt/test-driver :snowflake
+ (let [raw-pem (tx/db-test-env-var-or-throw :snowflake :private-key)
+ private-key (pem->private-key raw-pem)]
+ (are [passphrase] (driver/can-connect? :snowflake (-> (:details (mt/db))
+ (dissoc :private-key-id) ; make sure the stored secret doesn't shadow our new value
+ (assoc :private-key-value (mt/priv-key->base64-uri (encrypt-pkcs8-pem private-key passphrase))
+ :private-key-options "uploaded"
+ :private-key-passphrase passphrase)))
+ "passphrase"
+ "space and numb3r5"
+ "special,./;'[]-=`~!@#$%^&*()_+|}{:?><}chars"
+ "üñïçodé"))))
+
(deftest ddl-statements-test
(testing "make sure we didn't break the code that is used to generate DDL statements when we add new test datasets"
(mt/with-dynamic-fn-redefs [test.data.snowflake/qualified-db-name (constantly "v4_test-data")]