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
Add a timeout for slow LDAP (#63852)
John Swansonb323ecd5658f
diff --git a/src/metabase/session/api.clj b/src/metabase/session/api.cljindex 48b23c2f4ba..2a065f0a9eb 100644--- a/src/metabase/session/api.clj+++ b/src/metabase/session/api.clj@@ -67,7 +67,15 @@{:status-code 401:errors {:_error disabled-account-snippet}})))))(catch LDAPSDKException e- (log/error e "Problem connecting to LDAP server, will fall back to local authentication")))))+ (log/error e "Problem connecting to LDAP server, will fall back to local authentication"))+ (catch dev.failsafe.TimeoutExceededException e+ (log/error e "Timeout from LDAP server, will fall back to local authentication"))+ ;; Failsafe wraps exceptions in a `FailsafeException` - unwrap these if necessary+ (catch dev.failsafe.FailsafeException e+ (let [cause (.getCause e)]+ (if (instance? LDAPSDKException cause)+ (log/error cause "Problem connecting to LDAP server, will fall back to local authentication")+ (throw e)))))))(mu/defn- email-login :- [:maybe [:map [:key ms/UUIDString]]]"Find a matching `User` if one exists and return a new Session for them, or `nil` if they couldn't be authenticated."diff --git a/src/metabase/sso/ldap.clj b/src/metabase/sso/ldap.cljindex 35a82a0563c..3c5b89f7912 100644--- a/src/metabase/sso/ldap.clj+++ b/src/metabase/sso/ldap.clj@@ -1,6 +1,7 @@(ns metabase.sso.ldap(:require[clj-ldap.client :as ldap]+ [diehard.core :as dh][metabase.settings.core :as setting][metabase.sso.ldap.default-implementation :as default-impl][metabase.sso.settings :as sso.settings]@@ -129,7 +130,9 @@([conn user-info password](let [dn (if (string? user-info) user-info (:dn user-info))]- (ldap/bind? conn dn password))))+ (dh/with-timeout {:timeout-ms (int (* 1000 (sso.settings/ldap-timeout-seconds)))+ :interrupt? true}+ (ldap/bind? conn dn password)))))(defn ldap-settings"A map of all ldap settings"@@ -151,7 +154,9 @@([ldap-connection :- (ms/InstanceOfClass LDAPConnectionPool)username :- ms/NonBlankString]- (default-impl/find-user ldap-connection username (ldap-settings))))+ (dh/with-timeout {:timeout-ms (int (* 1000 (sso.settings/ldap-timeout-seconds)))+ :interrupt? true}+ (default-impl/find-user ldap-connection username (ldap-settings)))))(mu/defn fetch-or-create-user! :- (ms/InstanceOf :model/User)"Using the `user-info` (from [[find-user]]) get the corresponding Metabase user, creating it if necessary."diff --git a/src/metabase/sso/settings.clj b/src/metabase/sso/settings.cljindex beb94202397..1cf40b2d2b7 100644--- a/src/metabase/sso/settings.clj+++ b/src/metabase/sso/settings.clj@@ -137,6 +137,13 @@:default false:audit :getter)+(defsetting ldap-timeout-seconds+ (deferred-tru "Maximum time, in seconds, to wait for LDAP server before falling back to local authentication")+ :type :double+ :encryption :no+ :export? false+ :default 15.0)+;;;;;; Google Auth;;;diff --git a/test/metabase/session/api_test.clj b/test/metabase/session/api_test.cljindex ba375e80bee..ded1394a724 100644--- a/test/metabase/session/api_test.clj+++ b/test/metabase/session/api_test.clj@@ -12,6 +12,7 @@[metabase.settings.core :as setting :refer [defsetting]][metabase.settings.models.setting][metabase.sso.ldap-test-util :as ldap.test]+ [metabase.sso.ldap.default-implementation][metabase.test :as mt][metabase.test.data.users :as test.users][metabase.test.fixtures :as fixtures]@@ -628,6 +629,17 @@(mt/client :post 200 "session" {:username "[email protected]":password "securedpassword"}))))))+ (testing "Test that login will fallback to local for slow LDAP"+ (mt/with-temporary-setting-values [ldap-timeout-seconds 0.01]+ ;; Kinda reaching deep into the guts here to create artificial slowness+ (mt/with-dynamic-fn-redefs [metabase.sso.ldap.default-implementation/search (fn [& _args]+ (Thread/sleep 500))]+ (mt/with-temp [:model/User _ {:email "[email protected]"+ :password "securedpassword"}]+ (is (malli= SessionResponse+ (mt/client :post 200 "session" {:username "[email protected]"+ :password "securedpassword"})))))))+(testing "Test that we can login with LDAP with new user"(try(is (malli= SessionResponse