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
Implement databricks WIF with generic OIDC token (file) (#85980)
metamben6f650e614e0c
diff --git a/.github/workflows/drivers.yml b/.github/workflows/drivers.ymlindex 64cf3ef0478..3d689edc7ef 100644--- a/.github/workflows/drivers.yml+++ b/.github/workflows/drivers.yml@@ -303,6 +303,9 @@ jobs:if: ${{ needs.determine-driver-skips.outputs.databricks-should-run == 'true' }}runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}timeout-minutes: 50+ permissions:+ id-token: write+ contents: readstrategy:fail-fast: falsematrix:@@ -312,6 +315,8 @@ jobs:DRIVERS: databricksMB_DATABRICKS_TEST_CATALOG: 'metabase_ci'MB_DATABRICKS_TEST_CATALOG_ROUTING: 'metabase_routing_catalog'+ MB_DATABRICKS_TEST_WIF_AUDIENCE: '568fc646-4a47-4762-b7d8-703c6969d396'+ MB_DATABRICKS_TEST_WIF_USER: '477845b9-ae9e-4e5c-822c-ad1475419119'MB_DATABRICKS_TEST_CLIENT_ID: ${{ secrets.MB_DATABRICKS_JDBC_TEST_CLIENT_ID }}MB_DATABRICKS_TEST_OAUTH_SECRET: ${{ secrets.MB_DATABRICKS_JDBC_TEST_OAUTH_SECRET }}name: Databricks Driver Tests ${{ matrix.runner-index }}diff --git a/modules/drivers/databricks/resources/metabase/databricks/metabase-plugin.yaml b/modules/drivers/databricks/resources/metabase/databricks/metabase-plugin.yamlindex 1220aa2e4ad..5b4025fb6b0 100644--- a/modules/drivers/databricks/resources/metabase/databricks/metabase-plugin.yaml+++ b/modules/drivers/databricks/resources/metabase/databricks/metabase-plugin.yaml@@ -1,7 +1,7 @@info:name: Metabase Databricks Driverversion: 1.2.0- description: Allows Metabase to connect to Databricks SQL warehouse with personal access tokens or OAuth M2M+ description: Allows Metabase to connect to Databricks SQL warehouse with personal access tokens, OAuth M2M, or workload identity federation (OIDC JWT)dependencies:- plugin: Metabase Hive Like Abstract Driverdriver:@@ -21,27 +21,31 @@ driver:helper-text: Found in SQL Warehouses > Connection detailsrequired: trueplaceholder: /sql/1.0/endpoints/abcdef1234567890- - name: use-m2m- display-name: Use Machine to Machine (M2M) authentication- type: boolean- default: true- helper-text: Use Personal Access Token for authentication.+ - name: auth-mode+ display-name: Authentication method+ type: select+ default: m2m+ required: true+ options:+ - {name: "OAuth M2M (service principal)", value: "m2m"}+ - {name: "Personal Access Token", value: "pat"}+ - {name: "Workload Identity Federation (JWT)", value: "wif"}- merge:- password- name: tokendisplay-name: Personal Access Tokenrequired: true- helper-text: Personal Access Token for authentication. Not required if using OAuth M2M.+ helper-text: Personal Access Token for authentication.placeholder: dapiXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXvisible-if:- use-m2m: false+ auth-mode: pat- name: client-iddisplay-name: Service Principal Client IDplaceholder: e26ce240-0a12-454f-934f-4c646603cb61- helper-text: For M2M OAuth authentication, corresponds to the service principal's Client ID.+ helper-text: For M2M OAuth, the service principal's Client ID.required: truevisible-if:- use-m2m: true+ auth-mode: m2m- merge:- password- name: oauth-secret@@ -50,7 +54,21 @@ driver:helper-text: OAuth Secret for M2M OAuth authentication.required: truevisible-if:- use-m2m: true+ auth-mode: m2m+ - merge:+ - password+ - name: wif-token+ display-name: OIDC token (inline, for testing)+ helper-text: Paste a JWT for a one-off test. Use a token file in production.+ placeholder: "eyJhbGciOi..."+ visible-if:+ auth-mode: wif+ - name: wif-token-file-path+ display-name: OIDC token file path+ helper-text: Path to a file containing an OIDC JWT.+ placeholder: "/var/run/secrets/tokens/oidc-token"+ visible-if:+ auth-mode: wif- merge:- multi-level-schema- display-name: Enable multiple catalogsdiff --git a/modules/drivers/databricks/src/metabase/driver/databricks.clj b/modules/drivers/databricks/src/metabase/driver/databricks.cljindex 3b6c7310381..9666bae2237 100644--- a/modules/drivers/databricks/src/metabase/driver/databricks.clj+++ b/modules/drivers/databricks/src/metabase/driver/databricks.clj@@ -5,6 +5,7 @@[clojure.string :as str][honey.sql :as sql][java-time.api :as t]+ [medley.core :as m][metabase.driver :as driver][metabase.driver-api.core :as driver-api][metabase.driver.connection :as driver.conn]@@ -17,6 +18,7 @@[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sync :as driver.s]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.honey-sql-2 :as h2x][metabase.util.log :as log]@@ -279,8 +281,46 @@(when (string? (not-empty additional-options))(str/replace-first additional-options #"^(?!;)" ";")))+(defn- infer-auth-mode+ [{:keys [auth-mode use-m2m]}]+ (or auth-mode+ (if use-m2m "m2m" "pat")))++(defn- normalize-details [details]+ (when details+ (cond-> details+ (not (contains? details :auth-mode))+ (assoc :auth-mode (infer-auth-mode details)))))++(defmethod driver/normalize-db-details :databricks+ [_driver database]+ (-> database+ (m/update-existing :details normalize-details)+ (m/update-existing :write_data_details normalize-details)))++(defmethod driver/db-details-to-test-and-migrate :databricks+ [_driver {:keys [auth-mode] :as details}]+ (when (nil? auth-mode)+ [(assoc details :auth-mode (infer-auth-mode details))]))++(defn- wif-jdbc-properties+ [{:keys [wif-token wif-token-file-path wif-client-id]}]+ (let [token (cond+ (not (str/blank? wif-token-file-path))+ (str/trim (slurp (driver-api/ensure-readable-path! wif-token-file-path)))++ (not (str/blank? wif-token))+ wif-token)+ expiry-ms (u/ignore-exceptions (some-> token driver.u/jwt-expiry-ms))]+ (cond-> {:AuthMech 11+ :Auth_Flow 0+ :Auth_AccessToken token}+ (not (str/blank? wif-client-id)) (assoc :Identity_Federation_Client_Id wif-client-id)+ expiry-ms (assoc :password-expiry-timestamp expiry-ms))))+(defmethod sql-jdbc.conn/connection-details->spec :databricks- [_driver {:keys [catalog host http-path use-m2m token client-id oauth-secret log-level additional-options] :as _details}]+ [_driver {:keys [catalog host http-path token client-id oauth-secret log-level additional-options]+ :as details}](assert (string? (not-empty catalog)) "Catalog is mandatory.")(let [base-spec{:classname "com.databricks.client.jdbc.Driver"@@ -301,16 +341,15 @@(merge base-spec(when log-level{:LogLevel log-level})- (if use-m2m- ;; M2M OAuth- {:AuthMech 11- :Auth_Flow 1- :OAuth2ClientId client-id- :OAuth2Secret oauth-secret}- ;; PAT authentication- {:AuthMech 3- :uid "token"- :pwd token}))))+ (case (infer-auth-mode details)+ "wif" (wif-jdbc-properties details)+ "m2m" {:AuthMech 11+ :Auth_Flow 1+ :OAuth2ClientId client-id+ :OAuth2Secret oauth-secret}+ "pat" {:AuthMech 3+ :uid "token"+ :pwd token}))))(defmethod sql.qp/quote-style :databricks[_driver]diff --git a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj b/modules/drivers/databricks/test/metabase/driver/databricks_test.cljindex f6af46dfca8..bacf5ac61a4 100644--- a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj+++ b/modules/drivers/databricks/test/metabase/driver/databricks_test.clj@@ -2,6 +2,7 @@{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase.driver.databricks-test]}metabase.test.data/run-mbql-query {:namespaces [metabase.driver.databricks-test]}}}}}}(:require+ [clj-http.client :as http][clojure.java.jdbc :as jdbc][clojure.string :as str][clojure.test :refer :all]@@ -21,8 +22,11 @@[metabase.test :as mt][metabase.test.data.interface :as tx][metabase.util :as u]+ [metabase.util.json :as json][toucan2.core :as t2]))+(set! *warn-on-reflection* true)+(def ^:dynamic *multi-level?* false);; certain tests need to be run twice; once with the multi-level schema and once without.@@ -557,11 +561,218 @@(is (true? (driver/can-connect?:databricks(-> (:details (mt/db))- (dissoc :token)+ ;; drop :auth-mode so :use-m2m takes effect; the fixture DB is set up for PAT and+ ;; normalize-db-details has injected :auth-mode "pat"+ (dissoc :token :auth-mode)(assoc :use-m2m true:client-id (tx/db-test-env-var-or-throw :databricks :client-id):oauth-secret (tx/db-test-env-var-or-throw :databricks :oauth-secret)))))))))+(def ^:private wif-base-details+ {:host "x.cloud.databricks.com"+ :http-path "/sql/1.0/warehouses/abc"+ :catalog "main"+ :auth-mode "wif"})++(deftest ^:parallel connection-details->spec-wif-inline-token-test+ (testing "WIF with an inline pasted JWT sets Auth_AccessToken via pass-through (AuthMech=11, Auth_Flow=0)"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details :wif-token "eyJhbGciOi.the-jwt.sig"))]+ (is (= 11 (:AuthMech spec)))+ (is (= 0 (:Auth_Flow spec)))+ (is (= "eyJhbGciOi.the-jwt.sig" (:Auth_AccessToken spec)))+ (testing "no PAT or M2M credentials leak into the spec"+ (is (not (contains? spec :uid)))+ (is (not (contains? spec :pwd)))+ (is (not (contains? spec :OAuth2ClientId)))+ (is (not (contains? spec :OAuth2Secret)))))))++(deftest connection-details->spec-wif-file-path-wins-test+ (testing "file path wins over inline token; trailing newline is stripped"+ (mt/with-temp-file [tok-path "wif-token"]+ (spit tok-path "from-file\n")+ (mt/with-temp-env-var-value! [mb-readable-paths (.getParent (java.io.File. ^String tok-path))]+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details+ :wif-token "from-inline"+ :wif-token-file-path tok-path))]+ (is (= "from-file" (:Auth_AccessToken spec))))))))++(deftest connection-details->spec-wif-readable-paths-test+ (testing "the token file is read only from a directory `readable-paths` allows"+ (mt/with-temp-file [tok-path "wif-token"]+ (spit tok-path "from-file")+ (let [spec (fn [] (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details :wif-token-file-path tok-path)))]+ (mt/with-temp-env-var-value! [mb-readable-paths "NONE"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed" (spec))))+ (mt/with-temp-env-var-value! [mb-readable-paths (.getParent (java.io.File. ^String tok-path))]+ (is (= "from-file" (:Auth_AccessToken (spec)))))))))++(defn- fake-jwt+ "A parseable (but not verifiable) JWT with `claims` as the payload."+ [claims]+ (let [encoder (java.util.Base64/getUrlEncoder)+ encode (fn [m] (String. (.encode encoder (.getBytes (json/encode m) "UTF-8")) "UTF-8"))]+ (str (encode {:alg "none"}) "." (encode claims) ".sig")))++(deftest ^:parallel connection-details->spec-wif-exp-sets-password-expiry-test+ (testing "when the WIF token carries `exp`, the spec gets :password-expiry-timestamp for pool auto-invalidation"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details :wif-token (fake-jwt {:exp 1700000000 :sub "s"})))]+ (is (= 1700000000000 (:password-expiry-timestamp spec)))))+ (testing "no `exp` claim → no :password-expiry-timestamp on the spec"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details :wif-token (fake-jwt {:sub "s"})))]+ (is (not (contains? spec :password-expiry-timestamp))))))++(deftest ^:parallel normalize-db-details-test+ (testing "legacy :use-m2m true (no :auth-mode) → :auth-mode \"m2m\" injected"+ (is (= {:details {:use-m2m true :client-id "cid" :oauth-secret "osec" :auth-mode "m2m"}}+ (driver/normalize-db-details+ :databricks+ {:details {:use-m2m true :client-id "cid" :oauth-secret "osec"}}))))+ (testing "legacy :use-m2m false (no :auth-mode) → :auth-mode \"pat\" injected"+ (is (= {:details {:use-m2m false :token "dapi" :auth-mode "pat"}}+ (driver/normalize-db-details+ :databricks+ {:details {:use-m2m false :token "dapi"}}))))+ (testing "explicit :auth-mode is preserved"+ (is (= {:details {:use-m2m true :auth-mode "wif" :wif-token "jwt"}}+ (driver/normalize-db-details+ :databricks+ {:details {:use-m2m true :auth-mode "wif" :wif-token "jwt"}}))))+ (testing ":write_data_details is normalized the same way as :details"+ (is (= {:details {:use-m2m true :auth-mode "m2m"}+ :write_data_details {:use-m2m false :auth-mode "pat"}}+ (driver/normalize-db-details+ :databricks+ {:details {:use-m2m true}+ :write_data_details {:use-m2m false}}))))+ (testing "database without :details is returned unchanged"+ (is (= {:engine :databricks}+ (driver/normalize-db-details :databricks {:engine :databricks})))))++(deftest ^:parallel db-details-to-test-and-migrate-test+ (testing "returns a single candidate with :auth-mode injected from :use-m2m"+ (is (= [{:use-m2m true :client-id "cid" :oauth-secret "osec" :auth-mode "m2m"}]+ (driver/db-details-to-test-and-migrate+ :databricks+ {:use-m2m true :client-id "cid" :oauth-secret "osec"})))+ (is (= [{:use-m2m false :token "dapi" :auth-mode "pat"}]+ (driver/db-details-to-test-and-migrate+ :databricks+ {:use-m2m false :token "dapi"}))))+ (testing "returns nil when :auth-mode is already present (nothing to migrate)"+ (is (nil? (driver/db-details-to-test-and-migrate+ :databricks+ {:auth-mode "wif" :wif-token "jwt"})))))++(deftest ^:parallel connection-details->spec-backward-compat-use-m2m-test+ (testing "legacy :use-m2m true produces an M2M OAuth spec"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ {:host "x.cloud.databricks.com"+ :http-path "/sql/1.0/warehouses/abc"+ :catalog "main"+ :use-m2m true+ :client-id "cid"+ :oauth-secret "osec"})]+ (is (= 11 (:AuthMech spec)))+ (is (= 1 (:Auth_Flow spec)))+ (is (= "cid" (:OAuth2ClientId spec)))+ (is (= "osec" (:OAuth2Secret spec)))))+ (testing "legacy :use-m2m false produces a PAT spec"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ {:host "x.cloud.databricks.com"+ :http-path "/sql/1.0/warehouses/abc"+ :catalog "main"+ :use-m2m false+ :token "dapiXXXX"})]+ (is (= 3 (:AuthMech spec)))+ (is (= "token" (:uid spec)))+ (is (= "dapiXXXX" (:pwd spec)))))+ (testing "explicit :auth-mode wins over :use-m2m"+ (let [spec (sql-jdbc.conn/connection-details->spec+ :databricks+ (assoc wif-base-details+ :use-m2m true+ :client-id "cid"+ :wif-token "eyJhbGciOi.jwt.sig"))]+ (is (= 11 (:AuthMech spec)))+ (is (= 0 (:Auth_Flow spec)) "WIF pass-through flow, not M2M")+ (is (= "eyJhbGciOi.jwt.sig" (:Auth_AccessToken spec))))))++(defn- mint-github-actions-oidc-token+ "Returns a JWT for `audience` from the GitHub Actions OIDC endpoint, or nil when not running+ under a GH Actions job with `id-token: write`."+ [audience]+ (let [req-token (System/getenv "ACTIONS_ID_TOKEN_REQUEST_TOKEN")+ req-url (System/getenv "ACTIONS_ID_TOKEN_REQUEST_URL")]+ (when (and (not (str/blank? req-token)) (not (str/blank? req-url)))+ (let [{:keys [status body]} (http/get (str req-url "&audience=" audience) ; naive, but expected to work+ {:headers {"Authorization" (str "Bearer " req-token)}+ :throw-exceptions false})]+ (when (= 200 status)+ (:value (json/decode+kw body)))))))++(defn- wif-user-or-skip+ "Returns the WIF service principal's Application ID (what `current_user()` returns for an SP+ session), or nil to signal skip. Fails loudly when `MB_DATABRICKS_TEST_WIF_USER` is unset; set+ to 'skip' to opt out explicitly (REPL, forks without a configured federation policy)."+ []+ (let [u (tx/db-test-env-var :databricks :wif-user)]+ (cond+ (= "skip" u) nil+ (str/blank? u) (do (is false+ (str "MB_DATABRICKS_TEST_WIF_USER isn't set. Set it to the Databricks "+ "WIF service principal's Application ID (UUID), or to 'skip' to "+ "opt out."))+ nil)+ :else u)))++(defn- wif-live-details-with-token+ "WIF details for the live tests, `token-source` merged in (`:wif-token` or `:wif-token-file-path`)."+ [token-source]+ (merge {:host (tx/db-test-env-var-or-throw :databricks :host)+ :http-path (tx/db-test-env-var-or-throw :databricks :http-path)+ :catalog (tx/db-test-env-var-or-throw :databricks :catalog)+ :auth-mode "wif"+ :wif-client-id (tx/db-test-env-var-or-throw :databricks :wif-user)}+ token-source))++(deftest ^:synchronized databricks-wif-live-test+ ;; End-to-end against a real Databricks workspace with a federation policy trusting GH Actions.+ ;; In CI (be-tests-databricks-ee with environment: ci) we mint a fresh OIDC token here — GH tokens+ ;; last ~15 min and the full suite can exceed that, so a token minted at job start would be stale.+ ;; Locally, MB_DATABRICKS_TEST_WIF_TOKEN_FILE can point to a JWT on disk as a fallback.+ (mt/test-driver+ :databricks+ (when-let [wif-user (wif-user-or-skip)]+ (let [audience (tx/db-test-env-var-or-throw :databricks :wif-audience)]+ (when-let [token (or (mint-github-actions-oidc-token audience)+ (some-> (tx/db-test-env-var :databricks :wif-token-file)+ slurp+ str/trim))]+ (testing "token claims match the federation policy"+ (is (= {:iss "https://token.actions.githubusercontent.com"+ :aud audience+ :repository "metabase/metabase"}+ (select-keys (driver.u/jwt-claims token) [:iss :aud :repository]))))+ (let [details (wif-live-details-with-token {:wif-token token})]+ (testing "can-connect? via WIF"+ (is (true? (driver/can-connect? :databricks details))))+ (testing "session identifies as the WIF service principal"+ (let [spec (sql-jdbc.conn/connection-details->spec :databricks details)+ rows (jdbc/query spec ["SELECT current_user() AS user"])]+ (is (= [{:user wif-user}] rows))))))))))+(deftest ^:parallel mulit-line-comment-test(mt/test-driver :databricks(testing "queries with multi line block comments work (#68667)"diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.cljindex 3279f1ce5f8..ceb1deff528 100644--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj@@ -226,16 +226,6 @@[details keep-mode](transduce cat dissoc details (vals (dissoc auth-mode->credential-keys keep-mode))))-(defn- jwt-expiry-ms- "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 #"\.")- json-str (String. (.decode (java.util.Base64/getUrlDecoder) ^String payload) "UTF-8")]- (some-> (json/decode+kw json-str) :exp long (* 1000)))- (catch Throwable _ nil)))-(def ^:private hosted-allowed-wif-providers;; OIDC is the only WIF mode on Metabase Cloud where the identity is admin-supplied (a token or a;; token file). Every other provider — AWS/AZURE/GCP today, and anything the Snowflake JDBC@@ -267,7 +257,7 @@(not (str/blank? wif-token))wif-token))- expiry-ms (some-> token jwt-expiry-ms)]+ expiry-ms (u/ignore-exceptions (some-> token driver.u/jwt-expiry-ms))](cond-> {:authenticator "WORKLOAD_IDENTITY":workloadIdentityProvider provider}token (assoc :token token)diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.cljindex ba434e36937..7a3015e7fea 100644--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj@@ -275,17 +275,18 @@(is (= "eyJhbGciOi.file-jwt.signature" (:token spec)) "trailing newline stripped")(is (not (contains? spec :token_file_path)))))))-(deftest ^:synchronized connection-details->spec-wif-oidc-file-path-wins-test+(deftest connection-details->spec-wif-oidc-file-path-wins-test(testing "file path wins over inline token"(mt/with-temp-file [tok-path "wif-token"](spit tok-path "from-file")- (let [spec (sql-jdbc.conn/connection-details->spec- :snowflake- (assoc wif-base-details- :wif-provider "OIDC"- :wif-token "from-inline"- :wif-token-file-path tok-path))]- (is (= "from-file" (:token spec)))))))+ (mt/with-temp-env-var-value! [mb-readable-paths (.getParent (java.io.File. ^String tok-path))]+ (let [spec (sql-jdbc.conn/connection-details->spec+ :snowflake+ (assoc wif-base-details+ :wif-provider "OIDC"+ :wif-token "from-inline"+ :wif-token-file-path tok-path))]+ (is (= "from-file" (:token spec))))))))(deftest ^:synchronized connection-details->spec-wif-oidc-file-path-readable-paths-test(testing "the token file is read only from a directory `readable-paths` allows"@@ -309,18 +310,6 @@encode (fn [m] (String. (.encode encoder (.getBytes (json/encode m) "UTF-8")) "UTF-8"))](str (encode {:alg "none"}) "." (encode claims) ".sig")))-(deftest ^:parallel jwt-expiry-ms-test- (testing "parses `exp` (seconds since epoch) into milliseconds"- (is (= 1700000000000 (#'driver.snowflake/jwt-expiry-ms (fake-jwt {:exp 1700000000 :sub "s"})))))- (testing "returns nil when the token has no `exp` claim"- (is (nil? (#'driver.snowflake/jwt-expiry-ms (fake-jwt {:sub "s"})))))- (testing "returns nil for malformed input (doesn't throw)"- (are [input] (nil? (#'driver.snowflake/jwt-expiry-ms input))- "not-a-jwt"- "onlyone.segment"- "a.b.c" ; middle segment isn't valid base64url JSON- (str "hdr." (String. (.encode (java.util.Base64/getUrlEncoder) (.getBytes "{not-json" "UTF-8")) "UTF-8") ".sig"))))-(deftest ^:parallel connection-details->spec-wif-sets-expiry-from-jwt-test(testing "when the OIDC token carries `exp`, spec gets :password-expiry-timestamp for pool auto-invalidation"(let [spec (sql-jdbc.conn/connection-details->specdiff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.cljindex f69a86c111d..71464049f31 100644--- a/src/metabase/driver/util.clj+++ b/src/metabase/driver/util.clj@@ -23,6 +23,7 @@[metabase.util :as u][metabase.util.http :as u.http][metabase.util.i18n :refer [deferred-tru trs]]+ [metabase.util.json :as json][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.performance :refer [mapv empty? some]]@@ -38,6 +39,22 @@(set! *warn-on-reflection* true)+(defn jwt-claims+ "Parse `jwt`'s payload as JSON and return the decoded claims. Throws on malformed input."+ [^String jwt]+ (let [^String payload (second (str/split jwt #"\." 3))+ json-str (String. (.decode (java.util.Base64/getUrlDecoder) payload) "UTF-8")]+ (json/decode+kw json-str)))++(defn jwt-expiry-ms+ "The `exp` claim from `jwt`, in ms since epoch.++ Returns nil when `exp` is missing or non-numeric. Throws on malformed JWT; see [[jwt-claims]]."+ [^String jwt]+ (let [exp (:exp (jwt-claims jwt))]+ (when (number? exp)+ (long (* exp 1000)))))+(def ^:private connection-error-messages"Generic error messages that drivers should return in their implementationof [[metabase.driver/humanize-connection-error-message]]."diff --git a/test/metabase/driver/util_test.clj b/test/metabase/driver/util_test.cljindex 0af4bd9285c..2fb286ebd60 100644--- a/test/metabase/driver/util_test.clj+++ b/test/metabase/driver/util_test.clj@@ -19,6 +19,7 @@[metabase.test :as mt][metabase.test.fixtures :as fixtures][metabase.util :as u]+ [metabase.util.json :as json][metabase.util.snake-hating-map :as snake-hating-map])(:import(javax.net.ssl SSLSocketFactory)))@@ -1058,3 +1059,31 @@(is (nil? (validation-error :h2 {:db "file:/tmp/x;USER=GUEST;PASSWORD=guest"})))(is (not-any? #{:db} (driver/url-building-detail-keys :sqlite)))(is (nil? (validation-error :sqlite {:db "/tmp/my data#1.db"}))))++(defn- fake-jwt+ "A parseable (not verifiable) JWT with `claims` as the payload."+ [claims]+ (let [encoder (java.util.Base64/getUrlEncoder)+ encode (fn [m] (.encodeToString encoder (.getBytes ^String (json/encode m) "UTF-8")))]+ (str (encode {:alg "none"}) "." (encode claims) ".sig")))++(deftest ^:parallel jwt-claims-test+ (testing "decodes the payload as a Clojure map with keyword keys"+ (is (= {:exp 1700000000 :sub "s"}+ (driver.u/jwt-claims (fake-jwt {:exp 1700000000 :sub "s"})))))+ (testing "throws on malformed input"+ (are [input] (thrown? Exception (driver.u/jwt-claims input))+ "not-a-jwt"+ "onlyone.segment"+ "a.b.c"+ (str "hdr." (.encodeToString (java.util.Base64/getUrlEncoder) (.getBytes "{not-json" "UTF-8")) ".sig"))))++(deftest ^:parallel jwt-expiry-ms-test+ (testing "converts `exp` to integer milliseconds; RFC 7519 NumericDate allows non-integer exp"+ (let [actual (driver.u/jwt-expiry-ms (fake-jwt {:exp 1700000000.5 :sub "s"}))]+ (is (= 1700000000500 actual))+ (is (int? actual))))+ (testing "returns nil when `exp` is missing or non-numeric"+ (are [claims] (nil? (driver.u/jwt-expiry-ms (fake-jwt claims)))+ {:sub "s"}+ {:exp "nope"})))