Skip to content

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

Exclude routed users from indexed-entity search results (#82702)

Nathan Voxlandbcd475684f2e
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index c60f247ebe5..16446627082 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3863,6 +3863,7 @@
lib.metadata
lib.schema
models
+ permissions
premium-features
query-processor
settings
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/common.clj b/enterprise/backend/src/metabase_enterprise/database_routing/common.clj
index 21684d4fcc7..4b503712e3e 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/common.clj
@@ -2,6 +2,7 @@
(:require
[metabase-enterprise.database-routing.db :as database-routing.db]
[metabase.api.common :as api]
+ [metabase.permissions.core :as perms]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]))
@@ -63,6 +64,41 @@
api/*is-superuser?*
db-or-id))
+(defenterprise routed-user?
+ "Whether the current user's queries against ANY router database they can view data from resolve to a destination
+ database rather than the router itself, mirroring [[router-db-or-id->destination-db-id*]]. Routers whose
+ `:perms/view-data` is `:blocked` for the user are ignored: they cannot run anything against them, so routing there
+ doesn't change what they see. A user whose attribute is the `__METABASE_ROUTER__` sentinel is not routed. A superuser
+ missing the attribute defaults to the router, so is not routed, but a superuser whose attribute names a destination
+ database is routed like anyone else. A non-admin missing the attribute would be refused at query time, so counts as
+ routed here (fail closed). Uses `:feature :none` so a routed user is recognized even when the `:database-routing`
+ feature is momentarily unavailable. Throws if no current user is bound."
+ :feature :none
+ []
+ (boolean
+ (if api/*current-user-id*
+ (let [attributes (api/current-user-attributes)
+ routes-user? (fn [attribute-name]
+ (let [value (get attributes attribute-name)]
+ (if (nil? value)
+ (not api/*is-superuser?*)
+ (not= "__METABASE_ROUTER__" value))))
+ routing-db-ids (into []
+ (keep (fn [[db-id attribute-name]]
+ (when (routes-user? attribute-name)
+ db-id)))
+ (database-routing.db/all-router-user-attributes))]
+ (perms/prime-database-perms-cache {:db-ids routing-db-ids})
+ (some (fn [db-id]
+ (not= :blocked
+ (perms/most-permissive-database-permission-for-user
+ api/*current-user-id* :perms/view-data db-id)))
+ routing-db-ids))
+ ;; If no *current-user-id* is bound we can't check for routing, so we should throw in this case to avoid
+ ;; returning `false` for users who should actually be routed.
+ (throw (ex-info (str (tru "No current user found"))
+ {:status-code 403})))))
+
(defenterprise routing-token-for-db
"Database-routing fingerprint for the current user on router `db-id` (the resolved destination
database id), or nil when the user resolves to the router db itself (admins, or non-admins
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/db.clj b/enterprise/backend/src/metabase_enterprise/database_routing/db.clj
index d9b401444ff..074035fa6c9 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/db.clj
@@ -60,6 +60,11 @@
[database-id :- ::lib.schema.id/database]
(t2/select-one-fn :user_attribute :model/DatabaseRouter :database_id database-id))
+(mu/defn all-router-user-attributes :- [:maybe [:map-of ::lib.schema.id/database :string]]
+ "A map of router Database ID to routing user attribute for every DatabaseRouter."
+ []
+ (t2/select-fn->fn :database_id :user_attribute :model/DatabaseRouter))
+
(mu/defn router-user-attributes-by-database
"A map of Database ID to routing user attribute for `database-ids`."
[database-ids :- [:sequential ::lib.schema.id/database]]
diff --git a/enterprise/backend/test/metabase_enterprise/database_routing/search_test.clj b/enterprise/backend/test/metabase_enterprise/database_routing/search_test.clj
new file mode 100644
index 00000000000..74521044e4a
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/database_routing/search_test.clj
@@ -0,0 +1,117 @@
+(ns metabase-enterprise.database-routing.search-test
+ "Indexed entity values are captured once, against the router database, and shared by every reader. A user whose
+ queries are routed to a destination database sees different rows than the router holds, so they must not be shown
+ those values in search results -- the same rule that already excludes sandboxed and impersonated users."
+ (:require
+ [clojure.test :refer :all]
+ [metabase-enterprise.test :as met]
+ [metabase.indexed-entities.models.model-index :as model-index]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.permissions.core :as perms]
+ [metabase.search.ingestion :as search.ingestion]
+ [metabase.search.test-util :as search.tu]
+ [metabase.test :as mt]))
+
+(use-fixtures :each (fn [thunk] (binding [search.ingestion/*force-sync* true] (thunk))))
+
+(deftest routed-user?-test
+ (testing "with no router database nobody is routed"
+ (mt/with-test-user :rasta
+ (is (false? (perms/routed-user?)))))
+ (mt/with-temp [:model/Database _ {:name "sr-dest" :router_database_id (mt/id)}
+ :model/DatabaseRouter _ {:database_id (mt/id) :user_attribute "db_name"}]
+ (testing "a non-admin whose attribute resolves to a destination database is routed"
+ (met/with-user-attributes! :rasta {"db_name" "sr-dest"}
+ (mt/with-test-user :rasta
+ (is (true? (perms/routed-user?))))))
+ (testing "the __METABASE_ROUTER__ sentinel resolves to the router itself: not routed"
+ (met/with-user-attributes! :rasta {"db_name" "__METABASE_ROUTER__"}
+ (mt/with-test-user :rasta
+ (is (false? (perms/routed-user?))))))
+ (testing "a non-admin missing the attribute would be refused at query time, so counts as routed (fail closed)"
+ (met/with-user-attributes! :rasta {}
+ (mt/with-test-user :rasta
+ (is (true? (perms/routed-user?))))))
+ (testing "a superuser with no attribute defaults to the router itself: not routed"
+ (mt/with-test-user :crowberto
+ (is (false? (perms/routed-user?)))))
+ (testing "a superuser whose attribute names a destination database is routed, same as at query time"
+ (met/with-user-attributes! :crowberto {"db_name" "sr-dest"}
+ (mt/with-test-user :crowberto
+ (is (true? (perms/routed-user?))))))
+ (testing "a superuser with the sentinel attribute is not routed"
+ (met/with-user-attributes! :crowberto {"db_name" "__METABASE_ROUTER__"}
+ (mt/with-test-user :crowberto
+ (is (false? (perms/routed-user?))))))
+ (testing "only routers the user can view data from count: a router they are blocked from never routes them"
+ (mt/with-all-users-data-perms-graph! {(mt/id) {:view-data :blocked :create-queries :no}}
+ (testing "missing the attribute"
+ (met/with-user-attributes! :rasta {}
+ (mt/with-test-user :rasta
+ (is (false? (perms/routed-user?))))))
+ (testing "attribute naming a destination database"
+ (met/with-user-attributes! :rasta {"db_name" "sr-dest"}
+ (mt/with-test-user :rasta
+ (is (false? (perms/routed-user?))))))))
+ (testing "throws without a current user, like its sandbox / impersonation siblings"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"No current user found"
+ (perms/routed-user?))))))
+
+(deftest routed-users-do-not-see-indexed-entities-test
+ (mt/dataset airports
+ ;; the engine fixture goes outside `with-temp`: a temp Database created around it is not cleaned up on H2
+ (search.tu/with-appdb-search-and-legacy-search
+ (mt/with-temp [:model/Card model {:type :model
+ :dataset_query (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/table mp (mt/id :municipality))))
+ :collection_id nil}
+ :model/Database _ {:name "sr-dest" :router_database_id (mt/id)}
+ :model/DatabaseRouter _ {:database_id (mt/id) :user_attribute "db_name"}]
+ (mt/with-all-users-data-perms-graph! {(mt/id) {:view-data :unrestricted
+ :create-queries :query-builder-and-native}}
+ (let [model-index (model-index/create (mt/$ids {:model-id (:id model)
+ :pk-ref $municipality.id
+ :value-ref $municipality.name
+ :creator-id (mt/user->id :crowberto)}))
+ search! (fn [user]
+ (->> (mt/user-http-request user :get 200 "search" :q "rome" :models "indexed-entity")
+ :data
+ (filter (comp #{(:id model)} :model_id))
+ (map :name)
+ set))]
+ (mt/with-test-user :crowberto (model-index/add-values! model-index))
+ (testing "a non-admin who resolves to the router database sees the indexed values"
+ (met/with-user-attributes! :rasta {"db_name" "__METABASE_ROUTER__"}
+ (is (= #{"Rome"} (search! :rasta)))))
+ (testing "the same user, routed to a destination database, does not"
+ (met/with-user-attributes! :rasta {"db_name" "sr-dest"}
+ (is (= #{} (search! :rasta)))))))))))
+
+(deftest unrelated-router-does-not-hide-indexed-entities-test
+ (testing "a router on a database the user is blocked from does not hide indexed values from other databases"
+ (mt/dataset airports
+ (search.tu/with-appdb-search-and-legacy-search
+ (mt/with-temp [:model/Card model {:type :model
+ :dataset_query (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/table mp (mt/id :municipality))))
+ :collection_id nil}
+ :model/Database router {:name "sr-unrelated-router"}
+ :model/Database _ {:name "sr-dest" :router_database_id (:id router)}
+ :model/DatabaseRouter _ {:database_id (:id router) :user_attribute "tenant_db"}]
+ (mt/with-all-users-data-perms-graph! {(mt/id) {:view-data :unrestricted
+ :create-queries :query-builder-and-native}
+ (:id router) {:view-data :blocked
+ :create-queries :no}}
+ (let [model-index (model-index/create (mt/$ids {:model-id (:id model)
+ :pk-ref $municipality.id
+ :value-ref $municipality.name
+ :creator-id (mt/user->id :crowberto)}))]
+ (mt/with-test-user :crowberto (model-index/add-values! model-index))
+ (met/with-user-attributes! :rasta {}
+ (is (= #{"Rome"}
+ (->> (mt/user-http-request :rasta :get 200 "search" :q "rome" :models "indexed-entity")
+ :data
+ (filter (comp #{(:id model)} :model_id))
+ (map :name)
+ set)))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
index e0225e81436..95aedee841a 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
@@ -208,6 +208,7 @@
:is-superuser? true
:is-impersonated-user? false
:is-sandboxed-user? false
+ :is-routed-user? false
:models #{model}
:current-user-perms #{"/"}
:search-string search-string}))))
diff --git a/src/metabase/metabot/tools/search.clj b/src/metabase/metabot/tools/search.clj
index 8d5c4a3bd28..31819966c91 100644
--- a/src/metabase/metabot/tools/search.clj
+++ b/src/metabase/metabot/tools/search.clj
@@ -369,21 +369,25 @@
;; never returns them, even though the search tools list them as entity types.
collection-id (or confined-id collection-id)
limit (or limit 50)
+ ;; the user's data lens, computed once for every context built below
+ lens {:is-impersonated-user? (perms/impersonated-user?)
+ :is-sandboxed-user? (perms/sandboxed-user?)
+ :is-routed-user? (perms/routed-user?)}
ranked-fn (fn [search-string search-engine]
(let [search-context (search/search-context
- (cond-> {:search-string search-string
- :models search-models
- :table-db-id database-id
- :created-at created-at
- :last-edited-at last-edited-at
- :current-user-id api/*current-user-id*
- :is-impersonated-user? (perms/impersonated-user?)
- :is-sandboxed-user? (perms/sandboxed-user?)
- :is-superuser? api/*is-superuser?*
- :current-user-perms @api/*current-user-permissions-set*
- :filter-items-in-personal-collection "exclude-others"
- :context :metabot
- :archived (boolean archived)}
+ (cond-> (merge
+ lens
+ {:search-string search-string
+ :models search-models
+ :table-db-id database-id
+ :created-at created-at
+ :last-edited-at last-edited-at
+ :current-user-id api/*current-user-id*
+ :is-superuser? api/*is-superuser?*
+ :current-user-perms @api/*current-user-permissions-set*
+ :filter-items-in-personal-collection "exclude-others"
+ :context :metabot
+ :archived (boolean archived)})
;; Don't include search-native-query key if nil so that we don't
;; inadvertently filter out search models that don't support it
search-native-query
@@ -437,13 +441,14 @@
;; [offset, offset+limit) and reports `:total` as the size of the full fused set — so the
;; total is knowable even under multi-query fusion, and only the returned page is hydrated.
{:keys [data total]} (search/search-results
- (search/search-context {:search-string nil
- :models search-models
- :current-user-id api/*current-user-id*
- :current-user-perms @api/*current-user-permissions-set*
- :is-superuser? api/*is-superuser?*
- :offset (or offset 0)
- :limit limit})
+ (search/search-context (merge lens
+ {:search-string nil
+ :models search-models
+ :current-user-id api/*current-user-id*
+ :current-user-perms @api/*current-user-permissions-set*
+ :is-superuser? api/*is-superuser?*
+ :offset (or offset 0)
+ :limit limit}))
search/model-set
(vec fused-ranked))]
;; validate-and-enrich-documents drops stale/unreadable document hits and attaches live write
diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.clj
index df729a10217..1b6e538db6d 100644
--- a/src/metabase/permissions/core.clj
+++ b/src/metabase/permissions/core.clj
@@ -143,6 +143,7 @@
impersonated-user?
impersonation-enforced-for-db?
log-permissions-changes
+ routed-user?
sandboxed-or-impersonated-user?
sandboxed-user?
sandboxed-user-for-db?
diff --git a/src/metabase/permissions/util.clj b/src/metabase/permissions/util.clj
index e44fd6b0c1c..fb4c7db8dcd 100644
--- a/src/metabase/permissions/util.clj
+++ b/src/metabase/permissions/util.clj
@@ -365,6 +365,20 @@
{:status-code 403})))
false)
+(defenterprise routed-user?
+ "Returns a boolean if the current user is routed to a destination database for any router database they can view
+ data from. In OSS this is always false. Will throw an error if [[api/*current-user-id*]] is not bound."
+ metabase-enterprise.database-routing.common
+ []
+ (when-not api/*current-user-id*
+ ;; If no *current-user-id* is bound we can't check for routing, so we should throw in this case to avoid returning
+ ;; `false` for users who should actually be routed.
+ (throw (ex-info (str (tru "No current user found"))
+ {:status-code 403})))
+ ;; oss doesn't have database routing. But we throw if no current-user-id so the behavior doesn't change when the ee
+ ;; version becomes available
+ false)
+
(defn sandboxed-or-impersonated-user?
"Returns a boolean if the current user uses sandboxing or connection impersonation for any database. In OSS is always
false. Will throw an error if [[api/*current-user-id*]] is not bound."
diff --git a/src/metabase/search/api.clj b/src/metabase/search/api.clj
index e03f354926f..138cf6347c5 100644
--- a/src/metabase/search/api.clj
+++ b/src/metabase/search/api.clj
@@ -273,6 +273,7 @@
:current-user-id api/*current-user-id*
:is-impersonated-user? (perms/impersonated-user?)
:is-sandboxed-user? (perms/sandboxed-user?)
+ :is-routed-user? (perms/routed-user?)
:is-superuser? api/*is-superuser?*
:current-user-perms @api/*current-user-permissions-set*
:filter-items-in-personal-collection filter-items-in-personal-collection
diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.clj
index 8356c062e57..669e0834cbf 100644
--- a/src/metabase/search/config.clj
+++ b/src/metabase/search/config.clj
@@ -502,10 +502,13 @@
[:current-user-id pos-int?]
[:is-superuser? :boolean]
[:is-data-analyst? :boolean]
+ ;; whether a lens -- sandboxing, impersonation, routing -- narrows what this user may see; see
+ ;; [[metabase.search.permissions/sandboxed-impersonated-or-routed-user?]]
+ [:is-impersonated-user? :boolean]
+ [:is-sandboxed-user? :boolean]
+ [:is-routed-user? :boolean]
;; TODO only optional and maybe for tests, clean that up!
[:context {:optional true} [:maybe :keyword]]
- [:is-impersonated-user? {:optional true} [:maybe :boolean]]
- [:is-sandboxed-user? {:optional true} [:maybe :boolean]]
[:current-user-perms [:set perms/PathSchema]]
[:model-ancestors? :boolean]
[:models [:set SearchableModel]]
diff --git a/src/metabase/search/filter.clj b/src/metabase/search/filter.clj
index 51f7d5dcc17..27aa3dfeafe 100644
--- a/src/metabase/search/filter.clj
+++ b/src/metabase/search/filter.clj
@@ -23,7 +23,7 @@
[search-ctx {:keys [visibility] :as _spec}]
(case visibility
:all true
- :app-user (not (search.permissions/sandboxed-or-impersonated-user? search-ctx))
+ :app-user (not (search.permissions/sandboxed-impersonated-or-routed-user? search-ctx))
:superuser (:is-superuser? search-ctx)))
(def ^:private context-key->filter
diff --git a/src/metabase/search/impl.clj b/src/metabase/search/impl.clj
index a601c5823b6..eec314021f5 100644
--- a/src/metabase/search/impl.clj
+++ b/src/metabase/search/impl.clj
@@ -277,8 +277,9 @@
[:context {:optional true} [:maybe :keyword]]
[:models [:maybe [:set SearchableModel]]]
[:current-user-id pos-int?]
- [:is-impersonated-user? {:optional true} :boolean]
- [:is-sandboxed-user? {:optional true} :boolean]
+ [:is-impersonated-user? :boolean]
+ [:is-sandboxed-user? :boolean]
+ [:is-routed-user? :boolean]
[:is-superuser? :boolean]
[:is-data-analyst? {:optional true} :boolean]
[:current-user-perms [:set perms/PathSchema]]
@@ -324,6 +325,7 @@
ids
is-impersonated-user?
is-sandboxed-user?
+ is-routed-user?
include-dashboard-questions?
include-metadata?
is-superuser?
@@ -366,6 +368,7 @@
(fvalue :filter-items-in-personal-collection))
:is-impersonated-user? is-impersonated-user?
:is-sandboxed-user? is-sandboxed-user?
+ :is-routed-user? is-routed-user?
:is-superuser? is-superuser?
:is-data-analyst? (boolean is-data-analyst?)
:models models
diff --git a/src/metabase/search/in_place/filter.clj b/src/metabase/search/in_place/filter.clj
index 7304a0d4918..97c5fa2b89b 100644
--- a/src/metabase/search/in_place/filter.clj
+++ b/src/metabase/search/in_place/filter.clj
@@ -102,7 +102,7 @@
search.util/tokenize
(map search.util/wildcard-match))]
(cond
- (and (= model "indexed-entity") (search.permissions/sandboxed-or-impersonated-user? search-context))
+ (and (= model "indexed-entity") (search.permissions/sandboxed-impersonated-or-routed-user? search-context))
[:= 0 1]
(and (#{"card" "dataset"} model) (= column (search.config/column-with-model-alias model :dataset_query)))
diff --git a/src/metabase/search/permissions.clj b/src/metabase/search/permissions.clj
index 5429f92d437..d77ff1df2f5 100644
--- a/src/metabase/search/permissions.clj
+++ b/src/metabase/search/permissions.clj
@@ -1,34 +1,21 @@
(ns metabase.search.permissions
(:require
- [metabase.api.common :as api]
[metabase.collections.models.collection :as collection]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
[metabase.search.config :refer [SearchContext]]
[metabase.util.malli :as mu]))
-(defn- assert-current-user! [missing-param]
- (assert @api/*current-user*
- (format "metabase.api.common/*current-user* must be bound if %s is missing from search-ctx" missing-param)))
-
-(defn- impersonated-user? [{:keys [is-impersonated-user?] :as _search-ctx}]
- (or is-impersonated-user?
- ;; TODO Make this parameter non-optional, and fix code paths that omit it. Then remove this fallback.
- (when (nil? is-impersonated-user?)
- (assert-current-user! :is-impersonated-user?)
- (perms/impersonated-user?))))
-
-(defn- sandboxed-user? [{:keys [is-sandboxed-user?] :as _search-ctx}]
- (or is-sandboxed-user?
- ;; TODO Make this parameter non-optional, and fix code paths that omit it. Then remove this fallback.
- (when (nil? is-sandboxed-user?)
- (assert-current-user! :is-sandboxed-user?)
- (perms/sandboxed-user?))))
-
-(defn sandboxed-or-impersonated-user?
- "Is the current user sandboxed or impersonated?"
- [search-ctx]
- (or (impersonated-user? search-ctx) (sandboxed-user? search-ctx)))
+(defn sandboxed-impersonated-or-routed-user?
+ "Is the current user's view of warehouse data narrowed by a lens -- sandboxing, connection impersonation, or database
+ routing? Content captured under the unrestricted lens (e.g. indexed entity values) must not be shown to such a user.
+ The three flags are computed once, when the search context is built, see [[metabase.search.impl/search-context]].
+ Fails closed: a flag the caller left unset (nil) counts as restricted rather than unrestricted, so we don't depend
+ on the `SearchContext` schema being enforced."
+ [{:keys [is-impersonated-user? is-sandboxed-user? is-routed-user?] :as _search-ctx}]
+ (not (and (false? is-impersonated-user?)
+ (false? is-sandboxed-user?)
+ (false? is-routed-user?))))
(mu/defn permitted-collections-clause
"Build the WHERE clause corresponding to which collections the given user has access to."
diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.clj
index 1cdd1794042..e21875d3cfd 100644
--- a/src/metabase/search/spec.clj
+++ b/src/metabase/search/spec.clj
@@ -423,7 +423,7 @@
embedding text (they remain in full-text search). Use for fields whose raw value is
not suitable for semantic search.
- `:render-terms` - Additional attributes needed for display (required)
- - `:visibility` - `:all` (default) or `:app-user` (non-sandboxed, non-impersonated users only)
+ - `:visibility` - `:all` (default) or `:app-user` (non-sandboxed, non-impersonated, non-routed users only)
- `:where` - HoneySQL where clause to filter indexed records
- `:bookmark` - HoneySQL join expression to detect if entity is bookmarked by current user
- `:joins` - Map of join aliases to [model join-condition] tuples
diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.clj
index 87c088146c2..f00b1b1fb64 100644
--- a/test/metabase/search/api_test.clj
+++ b/test/metabase/search/api_test.clj
@@ -963,10 +963,12 @@
(is (= #{}
(into #{} (comp relevant-2 (map (juxt :name normalize)))
(search! "rom" :rasta))))))
- (testing "Sandboxed users do not see indexed entities in search"
+ (testing "Sandboxed, impersonated and routed users do not see indexed entities in search"
(mt/with-dynamic-fn-redefs [perms-util/impersonated-user? (constantly true)]
(is (empty? (into #{} (comp relevant-1 (map :name)) (search! "fort")))))
(mt/with-dynamic-fn-redefs [perms-util/sandboxed-user? (constantly true)]
+ (is (empty? (into #{} (comp relevant-1 (map :name)) (search! "fort")))))
+ (mt/with-dynamic-fn-redefs [perms-util/routed-user? (constantly true)]
(is (empty? (into #{} (comp relevant-1 (map :name)) (search! "fort")))))))))))
(defn- archived-collection [m]
diff --git a/test/metabase/search/debug_test.clj b/test/metabase/search/debug_test.clj
index 2491ba16f7e..7a359682ada 100644
--- a/test/metabase/search/debug_test.clj
+++ b/test/metabase/search/debug_test.clj
@@ -29,6 +29,7 @@
:is-superuser? api/*is-superuser?*
:is-impersonated-user? (perms-util/impersonated-user?)
:is-sandboxed-user? (perms-util/sandboxed-user?)
+ :is-routed-user? (perms-util/routed-user?)
:archived false
:context :default
:search-string nil
diff --git a/test/metabase/search/filter_test.clj b/test/metabase/search/filter_test.clj
index 6d8faca0636..64b54d00cbf 100644
--- a/test/metabase/search/filter_test.clj
+++ b/test/metabase/search/filter_test.clj
@@ -31,10 +31,13 @@
:enabled-transform-source-types #{"mbql"}))
(defn- with-all-models-and-regular-user [search-ctx]
- (with-all-models (assoc search-ctx :is-impersonated-user? false :is-sandboxed-user? false)))
+ (with-all-models (assoc search-ctx :is-impersonated-user? false :is-sandboxed-user? false :is-routed-user? false)))
(defn- with-all-models-and-sandboxed-user [search-ctx]
- (with-all-models (assoc search-ctx :is-impersonated-user? false :is-sandboxed-user? true)))
+ (with-all-models (assoc search-ctx :is-impersonated-user? false :is-sandboxed-user? true :is-routed-user? false)))
+
+(defn- with-all-models-and-routed-user [search-ctx]
+ (with-all-models (assoc search-ctx :is-impersonated-user? false :is-sandboxed-user? false :is-routed-user? true)))
(defn- with-all-models-and-superuser [search-ctx]
(-> search-ctx
@@ -73,6 +76,9 @@
(testing "Indexed entities and transforms (which are admin-only) are not visible for sandboxed users"
(is (= (disj search.config/all-models "indexed-entity" "transform")
(search.filter/search-context->applicable-models (with-all-models-and-sandboxed-user {:archived? false})))))
+ (testing "Indexed entities and transforms (which are admin-only) are not visible for users routed to a destination database"
+ (is (= (disj search.config/all-models "indexed-entity" "transform")
+ (search.filter/search-context->applicable-models (with-all-models-and-routed-user {:archived? false})))))
(testing "All models including transforms are visible for superusers"
(is (= search.config/all-models
(search.filter/search-context->applicable-models (with-all-models-and-superuser {:archived? false})))))
diff --git a/test/metabase/search/impl_test.clj b/test/metabase/search/impl_test.clj
index 600725ffd5a..adde9ac62c8 100644
--- a/test/metabase/search/impl_test.clj
+++ b/test/metabase/search/impl_test.clj
@@ -47,6 +47,7 @@
:is-superuser? true
:is-impersonated-user? false
:is-sandboxed-user? false
+ :is-routed-user? false
:models nil
:search-string "x"})]
(is (not-any? ctx [:vector-search-strategy :vector-search-ef-search :vector-search-max-scan-tuples
@@ -104,6 +105,9 @@
:models search.config/all-models
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:is-data-analyst? false
:current-user-perms #{"/"}
:model-ancestors? false
@@ -184,6 +188,9 @@
:created-at created-at
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:current-user-perms @api/*current-user-permissions-set*}))
:data
(map (juxt :model :id))
@@ -270,6 +277,9 @@
:last-edited-at last-edited-at
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:current-user-perms @api/*current-user-permissions-set*}))
:data
(map (juxt :model :id))
@@ -315,6 +325,9 @@
:models search.config/all-models
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:current-user-perms @api/*current-user-permissions-set*}))
:data
(map (juxt :model :id))
@@ -329,6 +342,9 @@
:models search.config/all-models
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:current-user-perms @api/*current-user-permissions-set*}))
:data
(map (juxt :model :id))
@@ -365,6 +381,9 @@
:models search.config/all-models
:current-user-id (mt/user->id :crowberto)
:is-superuser? true
+ :is-impersonated-user? false
+ :is-sandboxed-user? false
+ :is-routed-user? false
:current-user-perms @api/*current-user-permissions-set*}))
:data
(map (juxt :model :id))
diff --git a/test/metabase/search/in_place/filter_test.clj b/test/metabase/search/in_place/filter_test.clj
index 3ab910fea92..ec094f81142 100644
--- a/test/metabase/search/in_place/filter_test.clj
+++ b/test/metabase/search/in_place/filter_test.clj
@@ -20,6 +20,7 @@
:calculate-available-models? false
:is-sandboxed-user? false
:is-impersonated-user? false
+ :is-routed-user? false
:enabled-transform-source-types #{"mbql"}})
(deftest ^:parallel ->applicable-models-test
@@ -387,7 +388,7 @@
(deftest build-filters-indexed-entity-test
(testing "users that are not sandboxed or impersonated can search for indexed entity"
- (mt/with-dynamic-fn-redefs [search.permissions/sandboxed-or-impersonated-user? (constantly false)]
+ (mt/with-dynamic-fn-redefs [search.permissions/sandboxed-impersonated-or-routed-user? (constantly false)]
(is (= [:and
[:or [:like [:lower :model-index-value.name] (h2x/like-substring "foo")]]
[:= [:inline 1] [:inline 1]]]
@@ -398,7 +399,7 @@
(deftest build-filters-indexed-entity-test-2
(testing "otherwise search result is empty"
- (mt/with-dynamic-fn-redefs [search.permissions/sandboxed-or-impersonated-user? (constantly true)]
+ (mt/with-dynamic-fn-redefs [search.permissions/sandboxed-impersonated-or-routed-user? (constantly true)]
(is (= [:and
[:or [:= 0 1]]
[:= [:inline 1] [:inline 1]]]
diff --git a/test/metabase/search/permissions_test.clj b/test/metabase/search/permissions_test.clj
new file mode 100644
index 00000000000..aad6eb7c6aa
--- /dev/null
+++ b/test/metabase/search/permissions_test.clj
@@ -0,0 +1,15 @@
+(ns metabase.search.permissions-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.search.permissions :as search.permissions]))
+
+(deftest ^:parallel sandboxed-impersonated-or-routed-user?-test
+ (let [ctx {:is-impersonated-user? false :is-sandboxed-user? false :is-routed-user? false}]
+ (testing "all three flags false: not restricted"
+ (is (not (search.permissions/sandboxed-impersonated-or-routed-user? ctx))))
+ (doseq [flag [:is-impersonated-user? :is-sandboxed-user? :is-routed-user?]]
+ (testing (str flag " true: restricted")
+ (is (search.permissions/sandboxed-impersonated-or-routed-user? (assoc ctx flag true))))
+ (testing (str flag " unset or nil: restricted (fail closed)")
+ (is (search.permissions/sandboxed-impersonated-or-routed-user? (dissoc ctx flag)))
+ (is (search.permissions/sandboxed-impersonated-or-routed-user? (assoc ctx flag nil)))))))
diff --git a/test/metabase/search/test_util.clj b/test/metabase/search/test_util.clj
index c95104d893e..1059eaefbe7 100644
--- a/test/metabase/search/test_util.clj
+++ b/test/metabase/search/test_util.clj
@@ -129,11 +129,13 @@
:current-user-perms #{"/"}
:is-superuser? true
:is-sandboxed-user? false
- :is-impersonated-user? false}
+ :is-impersonated-user? false
+ :is-routed-user? false}
(select-keys raw-ctx# [:current-user-perms
:is-superuser?
:is-sandboxed-user?
- :is-impersonated-user?]))]
+ :is-impersonated-user?
+ :is-routed-user?]))]
~@body))
(mt/with-test-user :crowberto ~@body))))
@@ -150,7 +152,8 @@
:current-user-perms @api/*current-user-permissions-set*
:is-superuser? api/*is-superuser?*
:is-impersonated-user? (perms-util/impersonated-user?)
- :is-sandboxed-user? (perms-util/impersonated-user?)})
+ :is-sandboxed-user? (perms-util/sandboxed-user?)
+ :is-routed-user? (perms-util/routed-user?)})
{:archived false
:context :default
:search-string search-string