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
(ns metabase-enterprise.sso.integrations.ldap-test
(:require
[clojure.test :refer :all]
[metabase-enterprise.sso.integrations.ldap :as ee.ldap]
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.appearance.settings :as appearance.settings]
[metabase.auth-identity.core :as auth-identity]
[metabase.sso.ldap :as ldap]
[metabase.sso.ldap-test-util :as ldap.test]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[toucan2.core :as t2]))
(use-fixtures :once (fixtures/initialize :db :test-users))
(deftest find-test
;; No attribute allowlist is configured, so `:attributes` is empty for every user.
(mt/with-premium-features #{:sso-ldap}
(ldap.test/with-ldap-server!
(testing "find by username"
(is (= {:dn "cn=John Smith,ou=People,dc=metabase,dc=com"
:first-name "John"
:last-name "Smith"
:email "[email protected]"
:attributes {}
:groups ["cn=Accounting,ou=Groups,dc=metabase,dc=com"]}
(ldap/find-user "jsmith1"))))
(testing "find by email"
(is (= {:dn "cn=John Smith,ou=People,dc=metabase,dc=com"
:first-name "John"
:last-name "Smith"
:email "[email protected]"
:attributes {}
:groups ["cn=Accounting,ou=Groups,dc=metabase,dc=com"]}
(ldap/find-user "[email protected]"))))
(testing "find by email, no groups"
(is (= {:dn "cn=Fred Taylor,ou=People,dc=metabase,dc=com"
:first-name "Fred"
:last-name "Taylor"
:email "[email protected]"
:attributes {}
:groups ["cn=Accounting,ou=Groups,dc=metabase,dc=com"]}
(ldap/find-user "[email protected]"))))
(testing "find by email, no givenName"
(is (= {:dn "cn=Jane Miller,ou=People,dc=metabase,dc=com"
:first-name nil
:last-name "Miller"
:email "[email protected]"
:attributes {}
:groups []}
(ldap/find-user "[email protected]"))))
(mt/with-temporary-setting-values [ldap-group-membership-filter "memberUid={uid}"]
(testing "find by username with custom group membership filter"
(is (= {:dn "cn=Sally Brown,ou=People,dc=metabase,dc=com"
:first-name "Sally"
:last-name "Brown"
:email "[email protected]"
:attributes {}
:groups ["cn=Engineering,ou=Groups,dc=metabase,dc=com"]}
(ldap/find-user "sbrown20"))))
(testing "find by email with custom group membership filter"
(is (= {:dn "cn=Sally Brown,ou=People,dc=metabase,dc=com"
:first-name "Sally"
:last-name "Brown"
:email "[email protected]"
:attributes {}
:groups ["cn=Engineering,ou=Groups,dc=metabase,dc=com"]}
(ldap/find-user "[email protected]"))))))))
(deftest syncable-user-attributes-test
(testing "with an empty allowlist no attributes are synced"
(mt/with-temporary-setting-values [ldap-sync-user-attributes true
ldap-sync-user-attributes-blacklist ["userPassword" "dn" "distinguishedName"]
ldap-sync-user-attributes-allowlist []]
(is (= {}
(#'ee.ldap/syncable-user-attributes
{:objectclass ["top" "person"] :mail "[email protected]" :extra "x" :role "admin"})))))
(testing "allowlisted directory attributes are filtered and normalized like SAML/JWT attributes"
(mt/with-temporary-setting-values [ldap-sync-user-attributes true
ldap-sync-user-attributes-blacklist ["userPassword" "dn" "distinguishedName"]
ldap-sync-user-attributes-allowlist ["mail" "extra" "userPassword"]]
(testing "objectclass and blacklisted keys are dropped (blacklist match is case-insensitive)"
(is (= {"mail" "[email protected]" "extra" "x"}
(#'ee.ldap/syncable-user-attributes
{:objectclass ["top" "person"] :userpassword "secret" :mail "[email protected]" :extra "x"})))))
(mt/with-temporary-setting-values [ldap-sync-user-attributes true
ldap-sync-user-attributes-blacklist []
ldap-sync-user-attributes-allowlist ["multi" "single" "blank" "nested" "@reserved"]]
(testing "multi-value attributes are joined; nil/map values are dropped; @-prefixed keys are reserved"
(is (= {"multi" "a,b" "single" "s"}
(#'ee.ldap/syncable-user-attributes
{:multi ["a" "b"] :single "s" :blank nil :nested {:k 1} (keyword "@reserved") "x"}))))))
(testing "an allowlist restricts syncing to named keys (allowlist match is case-insensitive)"
(mt/with-temporary-setting-values [ldap-sync-user-attributes true
ldap-sync-user-attributes-blacklist []
ldap-sync-user-attributes-allowlist ["Mail"]]
(is (= {"mail" "[email protected]"}
(#'ee.ldap/syncable-user-attributes {:mail "[email protected]" :other "nope"})))))
(testing "when attribute sync is disabled nothing is returned"
(mt/with-temporary-setting-values [ldap-sync-user-attributes false]
(is (nil? (#'ee.ldap/syncable-user-attributes {:mail "[email protected]"}))))))
(deftest attribute-sync-test
(mt/with-premium-features #{:sso-ldap}
(ldap.test/with-ldap-server!
(testing "find by email/username should return allowlisted attributes as well"
(mt/with-temporary-setting-values [ldap-sync-user-attributes-allowlist
["uid" "mail" "title" "givenName" "sn" "cn"]]
(is (= {:dn "cn=Lucky Pigeon,ou=Birds,dc=metabase,dc=com"
:first-name "Lucky"
:last-name "Pigeon"
:email "[email protected]"
:attributes {"uid" "lucky"
"mail" "[email protected]"
"title" "King Pigeon"
"givenname" "Lucky"
"sn" "Pigeon"
"cn" "Lucky Pigeon"}
:groups []}
(ldap/find-user "lucky")))))
(testing "blacklisted attributes should not be returned even when allowlisted"
(mt/with-temporary-setting-values [ldap-sync-user-attributes-allowlist
["uid" "mail" "title" "givenName" "sn" "cn"]
ldap-sync-user-attributes-blacklist
(cons "title" (sso-settings/ldap-sync-user-attributes-blacklist))]
(is (= {:dn "cn=Lucky Pigeon,ou=Birds,dc=metabase,dc=com"
:first-name "Lucky"
:last-name "Pigeon"
:email "[email protected]"
:attributes {"uid" "lucky"
"mail" "[email protected]"
"givenname" "Lucky"
"sn" "Pigeon"
"cn" "Lucky Pigeon"}
:groups []}
(ldap/find-user "lucky")))))
(testing "when an allowlist is set, only allowlisted attributes are returned"
(mt/with-temporary-setting-values [ldap-sync-user-attributes-allowlist ["uid" "mail"]]
(is (= {:dn "cn=Lucky Pigeon,ou=Birds,dc=metabase,dc=com"
:first-name "Lucky"
:last-name "Pigeon"
:email "[email protected]"
:attributes {"uid" "lucky"
"mail" "[email protected]"}
:groups []}
(ldap/find-user "lucky")))))
(testing "if attribute sync is disabled, no attributes should come back at all"
(mt/with-temporary-setting-values [ldap-sync-user-attributes false]
(is (= {:dn "cn=Lucky Pigeon,ou=Birds,dc=metabase,dc=com"
:first-name "Lucky"
:last-name "Pigeon"
:email "[email protected]"
:attributes nil
:groups []}
(ldap/find-user "lucky"))))))))
(deftest new-user-attributes-synced-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "when creating a new user via provider/login!, allowlisted user attributes should get synced"
(mt/with-temporary-setting-values [ldap-sync-user-attributes-allowlist
["uid" "mail" "givenName" "sn" "cn"]]
(let [result (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result)))
(is (= {:first_name "John"
:last_name "Smith"
:email "[email protected]"
:login_attributes {"uid" "jsmith1"
"mail" "[email protected]"
"givenname" "John"
"sn" "Smith"
"cn" "John Smith"}
:common_name "John Smith"}
(into {} (t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))))))))))
(deftest new-user-attributes-not-synced-when-disabled-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "when creating a new user via provider/login! and attribute sync is disabled, attributes should not be synced"
(mt/with-temporary-setting-values [ldap-sync-user-attributes false]
(let [result (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result)))
(is (= {:first_name "John"
:last_name "Smith"
:email "[email protected]"
:login_attributes nil
:common_name "John Smith"}
(into {} (t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))))))))))
(deftest update-attributes-on-login-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "Existing user's allowlisted attributes are updated via provider/login!"
(mt/with-temporary-setting-values [ldap-sync-user-attributes-allowlist
["uid" "mail" "givenName" "sn" "cn"]]
(let [result1 (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})
result2 (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result1)))
(is (true? (:success? result2)))
(is (= {:first_name "John"
:last_name "Smith"
:common_name "John Smith"
:email "[email protected]"
:login_attributes {"uid" "jsmith1"
"mail" "[email protected]"
"givenname" "John"
"sn" "Smith"
"cn" "John Smith"}}
(into {} (t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))))))))))
(deftest update-attributes-disabled-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "Existing user's attributes are not updated via provider/login! when attribute sync is disabled"
(mt/with-temporary-setting-values [ldap-sync-user-attributes false]
(let [result1 (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})
result2 (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result1)))
(is (true? (:success? result2)))
(is (= {:first_name "John"
:last_name "Smith"
:common_name "John Smith"
:email "[email protected]"
:login_attributes nil}
(into {} (t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))))))))))
(deftest create-new-user-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "a new user is created via provider/login! when they don't already exist"
(let [result (auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result)))
(is (= {:first_name "John"
:last_name "Smith"
:common_name "John Smith"
:email "[email protected]"}
(into {} (t2/select-one [:model/User :first_name :last_name :email] :email "[email protected]"))))))))))
(deftest create-user-without-givenname-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "a user without a givenName attribute has nil for that attribute"
(let [result (auth-identity/login! :provider/ldap
{:username "jmiller"
:password "n0peeking"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})]
(is (true? (:success? result)))
(is (= {:first_name nil
:last_name "Miller"
:common_name "Miller"}
(into {} (t2/select-one [:model/User :first_name :last_name] :email "[email protected]"))))))))))
(deftest ldap-no-user-provisioning-test
(mt/with-premium-features #{:sso-ldap}
(mt/with-model-cleanup [:model/User]
(ldap.test/with-ldap-server!
(testing "an error is thrown when a new user attempts to login via provider/login! and user provisioning is not enabled"
;; with-redefs (cross-thread): /auth/sso runs on Jetty workers that don't inherit *local-redefs*
;; [kondo-keep] suppresses a warning :redundant-ignore can't see; --audit rechecks
#_{:clj-kondo/ignore [:metabase/prefer-with-dynamic-fn-redefs]}
(with-redefs [sso-settings/ldap-user-provisioning-enabled? (constantly false)
appearance.settings/site-name (constantly "test")]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Sorry, but you'll need a test account to view this page. Please contact your administrator."
(auth-identity/login! :provider/ldap
{:username "jsmith1"
:password "strongpassword"
:device-info {:device_id "test-device"
:device_description "Test Device"
:ip_address "127.0.0.1"
:embedded false
:token_exchange false}})))))))))