Skip to content

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

Add external OAuth for Snowflake (#85708)

metamben8a07926013ab
diff --git a/.github/workflows/drivers.yml b/.github/workflows/drivers.yml
index 49949ec82fb..64cf3ef0478 100644
--- a/.github/workflows/drivers.yml
+++ b/.github/workflows/drivers.yml
@@ -1017,6 +1017,22 @@ jobs:
MB_SNOWFLAKE_TEST_WIF_USER: ${{ github.event_name == 'pull_request' && 'METABASE WIF' || (github.ref == 'refs/heads/master' && 'METABASE WIF MAIN' || 'skip') }}
MB_SNOWFLAKE_TEST_WIF_DB: SNOWFLAKE_SAMPLE_DATA
MB_SNOWFLAKE_TEST_WIF_ROLE: PUBLIC
+
+ # OAuth client-credentials live test. The driver fetches a JWT from the IdP (e.g. AWS
+ # Cognito) and submits it to a Snowflake EXTERNAL_OAUTH integration. USER opts out via
+ # 'skip' on contexts without the OAuth secrets available (forks, release branches).
+ MB_SNOWFLAKE_TEST_OAUTH_CC_ACCOUNT: ${{ secrets.MB_SNOWFLAKE_TEST_ACCOUNT }}
+ MB_SNOWFLAKE_TEST_OAUTH_CC_WAREHOUSE: ${{ secrets.MB_SNOWFLAKE_TEST_WAREHOUSE }}
+ MB_SNOWFLAKE_TEST_OAUTH_CC_USER: ${{ (github.event_name == 'pull_request' || github.ref == 'refs/heads/master') && 'METABASE OAUTH CC' || 'skip' }}
+ MB_SNOWFLAKE_TEST_OAUTH_CC_DB: SNOWFLAKE_SAMPLE_DATA
+ MB_SNOWFLAKE_TEST_OAUTH_CC_ROLE: PUBLIC
+ MB_SNOWFLAKE_TEST_OAUTH_CC_CLIENT_ID: ${{ secrets.MB_SNOWFLAKE_TEST_OAUTH_CC_CLIENT_ID }}
+ MB_SNOWFLAKE_TEST_OAUTH_CC_CLIENT_SECRET: ${{ secrets.MB_SNOWFLAKE_TEST_OAUTH_CC_CLIENT_SECRET }}
+ MB_SNOWFLAKE_TEST_OAUTH_CC_TOKEN_REQUEST_URL: ${{ secrets.MB_SNOWFLAKE_TEST_OAUTH_CC_TOKEN_REQUEST_URL }}
+ # Cognito resource server scope. Driver sends this to the IdP's token endpoint; without it the
+ # JDBC driver falls back to deriving a `session:role:<role>` scope from `:role`, which Cognito
+ # rejects (not an allowed scope on the app client).
+ MB_SNOWFLAKE_TEST_OAUTH_CC_SCOPE: default-m2m-resource-server-c8qxtq/read
name: Snowflake Driver Tests ${{ matrix.runner-index }}
steps:
- uses: actions/checkout@v7
diff --git a/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml b/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
index eb7168167e5..b3c72505d60 100644
--- a/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
+++ b/modules/drivers/snowflake/resources/metabase/snowflake/metabase-plugin.yaml
@@ -29,9 +29,10 @@ driver:
default: key-pair
required: true
options:
- - {name: "RSA key pair", value: "key-pair"}
- - {name: "Workload Identity Federation", value: "wif"}
- - {name: "Username & password", value: "password"}
+ - {name: "RSA key pair", value: "key-pair"}
+ - {name: "Workload Identity Federation", value: "wif"}
+ - {name: "OAuth 2.0 (client credentials)", value: "oauth-client-credentials"}
+ - {name: "Username & password", value: "password"}
- name: wif-provider
display-name: Workload identity provider
type: select
@@ -57,6 +58,33 @@ driver:
placeholder: "/var/run/secrets/tokens/oidc-token"
visible-if:
wif-provider: OIDC
+ - name: oauth-client-id
+ display-name: OAuth client ID
+ helper-text: The client ID issued by your identity provider for Snowflake access.
+ visible-if:
+ auth-mode: oauth-client-credentials
+ required: true
+ - merge:
+ - password
+ - name: oauth-client-secret
+ display-name: OAuth client secret
+ helper-text: The client secret issued by your identity provider.
+ visible-if:
+ auth-mode: oauth-client-credentials
+ required: true
+ - name: oauth-token-request-url
+ display-name: OAuth token request URL
+ helper-text: Your identity provider's token endpoint.
+ placeholder: https://your-idp.example.com/oauth2/v1/token
+ visible-if:
+ auth-mode: oauth-client-credentials
+ required: true
+ - name: oauth-scope
+ display-name: OAuth scope (optional)
+ helper-text: Space-separated scopes. Leave blank to use the configured role.
+ placeholder: session:role:ANALYST
+ visible-if:
+ auth-mode: oauth-client-credentials
- name: private-key
display-name: RSA private key (PKCS#8/.p8)
type: secret
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 10edd066d9b..2cffdc5b6d7 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -204,49 +204,31 @@
[]
(inc (driver.common/start-of-week->int)))
-(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))
- (format "jdbc:snowflake://%s.snowflakecomputing.com" account))
- opts-str (sql-jdbc.common/additional-opts->string :url
- (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)))
-
- (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)
- ;; The Snowflake driver uses the :account property, but we need to drop the region from it first (#30376 comment)
- (m/assoc-some :account (some-> account (str/split #"\.") first)))))
-
-(defn- resolve-private-key
- "Convert the private-key secret properties into a private_key_file property in `details`.
- 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 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 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))))
+(def ^:private auth-mode->credential-keys
+ "Credential-carrying detail keys owned by each auth mode."
+ {"password" #{:password}
+ "key-pair" #{:private-key :private-key-value :private-key-path :private-key-id
+ :private-key-options :private-key-passphrase
+ :private-key-source :private-key-creator-id :private-key-created-at}
+ "wif" #{:wif-provider :wif-token :wif-token-file-path}
+ "oauth-client-credentials" #{:oauth-client-id :oauth-client-secret :oauth-token-request-url
+ :oauth-scope}})
+
+(def ^:private metabase-auth-detail-keys
+ "Every Metabase-side detail key that carries auth information for any supported mode."
+ (into #{:auth-mode :use-password} cat (vals auth-mode->credential-keys)))
+
+(defn- strip-metabase-auth-keys [details]
+ (apply dissoc details metabase-auth-detail-keys))
+
+(defn- strip-other-mode-credentials
+ "Return `details` with every credential key not owned by `keep-mode` dissoc'd."
+ [details keep-mode]
+ (transduce cat dissoc details (vals (dissoc auth-mode->credential-keys keep-mode))))
(defn- jwt-expiry-ms
- "Returns the JWT's `exp` claim as ms-since-epoch, or nil if the token can't be parsed or has no
- `exp`. Decode-only — no signature verification; the token has already been trusted at this point."
+ "Parse the `exp` claim from the payload of `jwt`, in ms since epoch. Returns nil when the token
+ has no `exp` or isn't a parseable JWT."
[^String jwt]
(try
(let [[_ payload _] (str/split jwt #"\.")
@@ -262,9 +244,11 @@
;; walks the same ambient paths. Whitelisting here fails closed on both.
#{"OIDC"})
-(defn- resolve-wif-credentials
- "Return `details` with Snowflake JDBC WIF properties replacing the Metabase-side auth keys."
- [{:keys [wif-provider wif-token wif-token-file-path] :as details}]
+(defn- wif-jdbc-properties
+ "Return the Snowflake JDBC WIF connection properties for the given WIF details. For OIDC, a
+ `:wif-token-file-path` is slurped and wins over an inline `:wif-token`. When the resulting
+ token carries an `exp` claim, the returned map includes `:password-expiry-timestamp`."
+ [{:keys [wif-provider wif-token wif-token-file-path]}]
(let [provider (some-> wif-provider u/upper-case-en)
_ (when (and (driver-api/is-hosted?)
(not (contains? hosted-allowed-wif-providers provider)))
@@ -275,8 +259,6 @@
"on Metabase Cloud. Use OIDC with an explicit token.")
provider))
{:wif-provider provider})))
- ;; Pass OIDC tokens inline; the Snowflake JDBC driver only reads `token_file_path` under
- ;; the special `jdbc:snowflake:auto` URL, which we don't use.
token (when (= "OIDC" provider)
(cond
(not (str/blank? wif-token-file-path))
@@ -285,19 +267,48 @@
(not (str/blank? wif-token))
wif-token))
- ;; `:password-expiry-timestamp` lets the connection pool self-invalidate on expiry and
- ;; rebuild against a rotated file or a fresh in-process mint.
- expiry-ms (some-> token jwt-expiry-ms)
- wif-spec (cond-> {:authenticator "WORKLOAD_IDENTITY"
- :workloadIdentityProvider provider}
- token (assoc :token token)
- expiry-ms (assoc :password-expiry-timestamp expiry-ms))]
- (-> details
- (merge wif-spec)
- (dissoc :wif-provider :wif-token :wif-token-file-path
- :password :private-key :private-key-value :private-key-path
- :private-key-id :private-key-options :private-key-passphrase
- :private-key-source :private-key-creator-id :private-key-created-at))))
+ expiry-ms (some-> token jwt-expiry-ms)]
+ (cond-> {:authenticator "WORKLOAD_IDENTITY"
+ :workloadIdentityProvider provider}
+ token (assoc :token token)
+ expiry-ms (assoc :password-expiry-timestamp expiry-ms))))
+
+(defn- oauth-client-credentials-jdbc-properties
+ "Return the Snowflake JDBC OAuth client-credentials connection properties for the given OAuth
+ details. A blank `:oauth-scope` is omitted from the result."
+ [{:keys [oauth-client-id oauth-client-secret oauth-token-request-url oauth-scope]}]
+ (cond-> {:authenticator "oauth_client_credentials"
+ :oauthClientId oauth-client-id
+ :oauthClientSecret oauth-client-secret
+ :oauthTokenRequestUrl oauth-token-request-url}
+ (not (str/blank? oauth-scope)) (assoc :oauthScope oauth-scope)))
+
+(defn- password-jdbc-properties [{:keys [password]}]
+ {:password password})
+
+(defn- key-pair-jdbc-properties
+ "Return the Snowflake JDBC key-pair connection properties for the given details. Materializes
+ the private-key secret to a file and emits `:private_key_file`, a `:connection-uri` carrying
+ user, key file, db and (form-encoded) passphrase, a trimmed `:account`, and `:private_key_pwd`
+ when the key is encrypted. Returns nil when no private-key secret is configured."
+ [{:keys [user account private-key-passphrase db subname] :as details}]
+ (when-let [private-key-file (driver-api/secret-value-as-file! :snowflake details "private-key")]
+ (let [existing-conn-uri (or (:connection-uri details)
+ (when subname (format "jdbc:snowflake:%s" subname))
+ (format "jdbc:snowflake://%s.snowflakecomputing.com" account))
+ opts-str (sql-jdbc.common/additional-opts->string
+ :url
+ (cond-> {:user (codec/url-encode user)
+ :private_key_file (codec/url-encode (.getCanonicalPath ^File private-key-file))}
+ db (assoc :db (codec/url-encode db))
+ (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)]
+ ;; :private_key_pwd lives in both the connection spec (raw) and the URI (form-encoded above).
+ (cond-> {:private_key_file private-key-file
+ :connection-uri new-conn-uri}
+ account (assoc :account (first (str/split account #"\.")))
+ (not (str/blank? private-key-passphrase)) (assoc :private_key_pwd private-key-passphrase)))))
(defn- quote-name
[raw-name]
@@ -346,48 +357,61 @@
(defmethod driver/db-details-to-test-and-migrate :snowflake
[_ {:keys [password private-key-id private-key-value private-key-path
use-password private-key-options
- auth-mode wif-token wif-token-file-path]
+ auth-mode wif-token wif-token-file-path
+ oauth-client-id oauth-client-secret oauth-token-request-url]
:as details}]
;; Private-key fields are counted independently because Metabase's history has left DBs with more
;; than one populated at once (legacy `-value` plus a persisted `-id`, etc.) — the migrate flow
- ;; tries each. WIF is new, has no such legacy state, and has a deterministic within-mode
- ;; precedence (file-path wins in resolve-wif-credentials), so it counts as one signal.
- (let [wif-signal (or wif-token wif-token-file-path)]
- (when-not (= 1 (count (remove nil? [password private-key-id private-key-value private-key-path wif-signal])))
+ ;; tries each. WIF and oauth-client-credentials are newer, have no such legacy state, and have a
+ ;; deterministic within-mode shape, so each counts as one signal.
+ (let [wif-signal (or wif-token wif-token-file-path)
+ oauth-signal (or oauth-client-id oauth-client-secret oauth-token-request-url)]
+ (when-not (= 1 (count (remove nil? [password private-key-id private-key-value private-key-path
+ wif-signal oauth-signal])))
(let [wif-details (when wif-signal
(-> details
+ (strip-other-mode-credentials "wif")
+ (dissoc :use-password)
(assoc :auth-mode "wif")
- (dissoc :password :private-key-id :private-key-value :private-key-path
- :private-key-options :private-key-passphrase :use-password)
(with-meta {:auth :wif})))
+ oauth-details (when oauth-signal
+ (-> details
+ (strip-other-mode-credentials "oauth-client-credentials")
+ (dissoc :use-password)
+ (assoc :auth-mode "oauth-client-credentials")
+ (with-meta {:auth :oauth-client-credentials})))
password-details (when password
(-> details
+ (strip-other-mode-credentials "password")
;; Setting private-key-value to nil will delete the secret
(assoc :use-password true :private-key-value nil :auth-mode "password")
- (dissoc :private-key-id :private-key-path :private-key-options
- :private-key-passphrase :wif-token :wif-token-file-path :wif-provider)
;; Add meta for testing
(with-meta {:auth :password})))
private-key-path-details (when private-key-path
(-> details
+ (strip-other-mode-credentials "key-pair")
+ (dissoc :private-key-value)
(assoc :use-password false :private-key-options "local" :auth-mode "key-pair")
- (dissoc :password :private-key-value :wif-token :wif-token-file-path :wif-provider)
(with-meta {:auth :private-key-path})))
private-key-value-details (when private-key-value
(-> details
+ (strip-other-mode-credentials "key-pair")
+ (dissoc :private-key-path)
(assoc :use-password false :private-key-options "uploaded" :auth-mode "key-pair")
- (dissoc :password :private-key-path :wif-token :wif-token-file-path :wif-provider)
(with-meta {:auth :private-key-value})))
private-key-id-details (when private-key-id
(-> details
+ (strip-other-mode-credentials "key-pair")
+ (dissoc :private-key-value :private-key-path :private-key-options)
(assoc :use-password false :auth-mode "key-pair")
- (dissoc :password :private-key-value :private-key-path :private-key-options
- :wif-token :wif-token-file-path :wif-provider)
(with-meta {:auth :private-key-id})))]
(cond-> []
(and (= "wif" auth-mode) wif-details)
(conj wif-details)
+ (and (= "oauth-client-credentials" auth-mode) oauth-details)
+ (conj oauth-details)
+
(and use-password password-details)
(conj password-details)
@@ -406,9 +430,12 @@
(and (not use-password) password-details)
(conj password-details)
- ;; WIF as last-resort when auth-mode isn't explicitly "wif" but WIF fields are present
+ ;; WIF / oauth as last-resort when their fields are present but auth-mode names a different mode
(and (not= "wif" auth-mode) wif-details)
- (conj wif-details))))))
+ (conj wif-details)
+
+ (and (not= "oauth-client-credentials" auth-mode) oauth-details)
+ (conj oauth-details))))))
(defn- normalize-additional-options [additional-options]
(when-not (str/blank? additional-options)
@@ -435,17 +462,19 @@
password "password"))
(defn- resolve-credentials
- "Set the JDBC credential properties. An explicit `:auth-mode` is preferred, because some can't be
- inferred from other keys in the details (e.g. WIF), and because the details can have credential
- keys from previous auth methods. Without an explicit `:auth-mode`, infer password vs key-pair
- from the current details."
+ "Return `details` with Metabase-side auth fields replaced by the Snowflake JDBC credential
+ properties of the mode selected by `:auth-mode`, falling back to inference when `:auth-mode`
+ is absent."
[{:keys [auth-mode] :as details}]
- (case (or (#{"wif" "password" "key-pair"} auth-mode)
- (infer-auth-mode details)
- "key-pair")
- "wif" (resolve-wif-credentials details)
- "password" (-> details (dissoc :private-key) resolve-private-key)
- "key-pair" (-> details (dissoc :password) resolve-private-key)))
+ (let [mode (or (#{"wif" "password" "key-pair" "oauth-client-credentials"} auth-mode)
+ (infer-auth-mode details)
+ "key-pair")]
+ (merge (strip-metabase-auth-keys details)
+ (case mode
+ "wif" (wif-jdbc-properties details)
+ "oauth-client-credentials" (oauth-client-credentials-jdbc-properties details)
+ "password" (password-jdbc-properties details)
+ "key-pair" (key-pair-jdbc-properties details)))))
(defmethod sql-jdbc.conn/connection-details->spec :snowflake
[_ {:keys [account additional-options host use-hostname], :as details}]
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index d93f0a7155f..125e513dcb5 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -416,6 +416,61 @@
(is (not (contains? spec :private_key_file)))
(is (nil? (:connection-uri spec))))))
+(def ^:private oauth-cc-base-details
+ {:account "acct.us-east-2.aws"
+ :warehouse "COMPUTE_WH"
+ :db "TESTDB"
+ :user "SERVICE_USER"
+ :auth-mode "oauth-client-credentials"
+ :oauth-client-id "metabase-client"
+ :oauth-client-secret "shh"
+ :oauth-token-request-url "https://idp.example.com/oauth2/v1/token"})
+
+(deftest ^:parallel connection-details->spec-oauth-client-credentials-test
+ (testing "oauth-client-credentials emits the Snowflake JDBC OAuth properties"
+ (let [spec (sql-jdbc.conn/connection-details->spec
+ :snowflake
+ (assoc oauth-cc-base-details :oauth-scope "session:role:ANALYST"))]
+ (is (= "oauth_client_credentials" (:authenticator spec)))
+ (is (= "metabase-client" (:oauthClientId spec)))
+ (is (= "shh" (:oauthClientSecret spec)))
+ (is (= "https://idp.example.com/oauth2/v1/token" (:oauthTokenRequestUrl spec)))
+ (is (= "session:role:ANALYST" (:oauthScope spec))))))
+
+(deftest ^:parallel connection-details->spec-oauth-client-credentials-scope-optional-test
+ (testing "omitting oauth-scope leaves :oauthScope absent"
+ (let [spec (sql-jdbc.conn/connection-details->spec :snowflake oauth-cc-base-details)]
+ (is (= "oauth_client_credentials" (:authenticator spec)))
+ (is (not (contains? spec :oauthScope)))))
+ (testing "a blank oauth-scope is treated as absent"
+ (let [spec (sql-jdbc.conn/connection-details->spec
+ :snowflake
+ (assoc oauth-cc-base-details :oauth-scope " "))]
+ (is (not (contains? spec :oauthScope))))))
+
+(deftest ^:parallel connection-details->spec-oauth-client-credentials-strips-legacy-creds-test
+ (testing "under oauth-client-credentials, stale password / private-key / wif fields do not leak"
+ (let [spec (sql-jdbc.conn/connection-details->spec
+ :snowflake
+ (assoc oauth-cc-base-details
+ :password "stale-password"
+ :private-key-value "stale-key"
+ :private-key-options "uploaded"
+ :wif-provider "OIDC"
+ :wif-token "stale-jwt"))]
+ (is (= "oauth_client_credentials" (:authenticator spec)))
+ (is (not (contains? spec :password)))
+ (is (not (contains? spec :private_key_file)))
+ (is (not (contains? spec :token)))
+ (is (not (contains? spec :workloadIdentityProvider)))
+ (is (nil? (:connection-uri spec)))))
+ (testing "Metabase-side oauth-* detail keys don't leak through alongside the JDBC property names"
+ (let [spec (sql-jdbc.conn/connection-details->spec :snowflake oauth-cc-base-details)]
+ (is (not (contains? spec :oauth-client-id)))
+ (is (not (contains? spec :oauth-client-secret)))
+ (is (not (contains? spec :oauth-token-request-url)))
+ (is (not (contains? spec :oauth-scope))))))
+
(deftest ^:parallel connection-details->spec-explicit-auth-mode-test
(testing "an explicit :auth-mode wins over stale credentials of the previous method"
(let [details {:account "acct.us-east-2.aws"
@@ -481,6 +536,16 @@
{:auth-mode "key-pair" :use-password true :password "abc" :private-key-id 1} :private-key-id
{:auth-mode "password" :use-password false :password "abc" :private-key-id 1} :password)))
+(deftest ^:parallel db-details-to-test-and-migrate-oauth-client-credentials-test
+ (testing "oauth-client-credentials-only details are unambiguous — no candidates returned"
+ (is (nil? (driver/db-details-to-test-and-migrate :snowflake oauth-cc-base-details))))
+ (testing "when oauth-client-credentials and password are both set, oauth is tried first"
+ (let [candidates (driver/db-details-to-test-and-migrate
+ :snowflake
+ (assoc oauth-cc-base-details :password "stale-pw"))]
+ (is (some? candidates))
+ (is (= :oauth-client-credentials (-> candidates first meta :auth))))))
+
(deftest ^:parallel db-details-to-test-and-migrate-wif-test
(testing "WIF-only details are unambiguous — no candidates returned"
(is (nil? (driver/db-details-to-test-and-migrate
@@ -599,6 +664,54 @@
(is (> new-exp initial-exp)
"new pool's :password-expiry-timestamp reflects the fresher token"))))))))))))
+(defn- oauth-cc-user-or-skip
+ "Returns the Snowflake service username for the OAuth client-credentials live test, or nil to
+ signal skip. If MB_SNOWFLAKE_TEST_OAUTH_CC_USER isn't set, records a test failure with
+ instructions. Set the env var to 'skip' to opt out explicitly (REPL, forks, release branches)."
+ []
+ (let [u (tx/db-test-env-var :snowflake :oauth-cc-user)]
+ (cond
+ (= "skip" u) nil
+ (str/blank? u) (do (is false
+ (str "MB_SNOWFLAKE_TEST_OAUTH_CC_USER isn't set. Set it to the "
+ "Snowflake OAuth service user (TYPE=SERVICE), or to 'skip' to "
+ "opt out."))
+ nil)
+ :else u)))
+
+(defn- oauth-cc-live-details
+ "Details map for the OAuth client-credentials live test. `:role` and `:oauth-scope` are added
+ only when their env vars are set."
+ [user]
+ (let [role (tx/db-test-env-var :snowflake :oauth-cc-role)
+ scope (tx/db-test-env-var :snowflake :oauth-cc-scope)]
+ (cond-> {:account (tx/db-test-env-var-or-throw :snowflake :oauth-cc-account)
+ :user user
+ :warehouse (tx/db-test-env-var-or-throw :snowflake :oauth-cc-warehouse)
+ :db (tx/db-test-env-var-or-throw :snowflake :oauth-cc-db)
+ :auth-mode "oauth-client-credentials"
+ :oauth-client-id (tx/db-test-env-var-or-throw :snowflake :oauth-cc-client-id)
+ :oauth-client-secret (tx/db-test-env-var-or-throw :snowflake :oauth-cc-client-secret)
+ :oauth-token-request-url (tx/db-test-env-var-or-throw :snowflake :oauth-cc-token-request-url)}
+ role (assoc :role role)
+ scope (assoc :oauth-scope scope))))
+
+(deftest ^:synchronized snowflake-oauth-client-credentials-live-test
+ ;; End-to-end test against a real Snowflake user via an External OAuth integration whose IdP
+ ;; (e.g. AWS Cognito) honors the OAuth 2.0 client-credentials grant. The Snowflake JDBC driver
+ ;; itself calls the IdP's token endpoint with `oauth-client-id` + `oauth-client-secret`,
+ ;; receives a JWT, and submits it to Snowflake per new connection.
+ (mt/test-driver
+ :snowflake
+ (when-let [user (oauth-cc-user-or-skip)]
+ (let [details (oauth-cc-live-details user)]
+ (testing "can-connect? via OAuth client-credentials"
+ (is (true? (driver/can-connect? :snowflake details))))
+ (testing "session identifies as the OAuth service user (proves auth flowed through OAuth)"
+ (let [spec (sql-jdbc.conn/connection-details->spec :snowflake details)
+ rows (jdbc/query spec ["SELECT CURRENT_USER() AS \"user\""])]
+ (is (= [{:user user}] rows))))))))
+
(defn- pem->private-key
[pem]
(let [encoded (-> pem
@@ -1983,7 +2096,7 @@
"Returns a SHA-256 digest of the resolved private key file for `db`."
[db]
(-> (:details db)
- (#'driver.snowflake/resolve-private-key)
+ (#'driver.snowflake/key-pair-jdbc-properties)
:private_key_file
slurp
(.getBytes "UTF-8")