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
Fail loudly in dev and test when permission checks run one query per entity (#79244)
Alexander Polyankin6908f26b766b
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 66d608ba4fb..ae563dabfa7 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2049,6 +2049,7 @@:model-imports #{:model/Card:model/Collection:model/Dashboard+ :model/Database:model/Table:model/User}}diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.cljindex f0589e1581b..bac53099ea7 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/block_permissions.clj@@ -26,9 +26,9 @@:feature :none[{database-id :database :as query}](or- (not= :blocked (perms/full-db-permission-for-user api/*current-user-id* :perms/view-data database-id))+ (not= :blocked (perms/full-database-permission-for-user api/*current-user-id* :perms/view-data database-id))(let [{:keys [table-ids]} (query-perms/query->source-ids query)]- (perms/prime-table-perms-cache {:table-ids table-ids})+ (perms/prime-table-perms-cache {:db-ids #{database-id} :table-ids table-ids})(= #{:unrestricted}(into #{}(map (partial perms/table-permission-for-user api/*current-user-id* :perms/view-data database-id))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.cljindex 33cac4b422c..60e5815ce5f 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/data_permissions.clj@@ -25,7 +25,7 @@;; download perms#{(perms/native-download-permission-for-user user-id db-id)}(do- (perms/prime-table-perms-cache {:table-ids table-ids})+ (perms/prime-table-perms-cache {:db-ids #{db-id} :table-ids table-ids})(set (map (fn table-perms-lookup [table-id](perms/table-permission-for-user user-id :perms/download-results db-id table-id))table-ids))))]diff --git a/enterprise/backend/src/metabase_enterprise/upload_management/api.clj b/enterprise/backend/src/metabase_enterprise/upload_management/api.cljindex 2d577bc7c09..008e07a12fe 100644--- a/enterprise/backend/src/metabase_enterprise/upload_management/api.clj+++ b/enterprise/backend/src/metabase_enterprise/upload_management/api.clj@@ -31,7 +31,8 @@;; See https://github.com/metabase/metabase/issues/41023(concat tables (attached-dwh-tables))(map #(update % :schema str) tables)- (do (perms/prime-table-perms-cache {:table-ids (into #{} (map :id) tables)}) tables)+ (do (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (map :id) tables)}) tables)(filter mi/can-read? tables)(sort-by :name tables) ;; Re-sort because we concat'ed data(vec tables)))diff --git a/src/metabase/activity_feed/models/recent_views.clj b/src/metabase/activity_feed/models/recent_views.cljindex 6fd17a9aa9b..357c1328c9e 100644--- a/src/metabase/activity_feed/models/recent_views.clj+++ b/src/metabase/activity_feed/models/recent_views.clj@@ -580,7 +580,8 @@(group-by (comp keyword :model) views)(update-vals views #(mapv :model_id %)))tables (table-recents table-ids)]- (perms/prime-table-perms-cache {:table-ids (into #{} (keep :id) tables)})+ (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (keep :id) tables)}){:card (m/index-by :id (card-recents card-ids)):dashboard (m/index-by :id (dashboard-recents dashboard-ids)):collection (m/index-by :id (collection-recents collection-ids))diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.cljindex 7f7302c8b47..323218eea5d 100644--- a/src/metabase/dashboards_rest/api.clj+++ b/src/metabase/dashboards_rest/api.clj@@ -985,6 +985,7 @@(t2/select-one [:model/Dashboard :id :parameters] dashboard-id):resolved-params)dashboard-params (set (keys resolved-params))]+ ;; ordered so the notifications go out in a stable order rather than whatever order the rows come back in(->> (t2/select :model/Pulse :dashboard_id dashboard-id :archived false {:order-by [[:id :asc]]})(keep (fn [{:keys [parameters] :as pulse}](let [bad-params (filtervdiff --git a/src/metabase/measures/api.clj b/src/metabase/measures/api.cljindex a89710a041a..4a3f7d899e4 100644--- a/src/metabase/measures/api.clj+++ b/src/metabase/measures/api.clj@@ -106,7 +106,9 @@[](let [measures (t2/select :model/Measure, :archived false, {:order-by [[:%lower.name :asc]]})table-ids (into #{} (keep :table_id) measures)]- (perms/prime-table-perms-cache {:table-ids table-ids})+ (perms/prime-table-perms-cache {:db-ids (when (seq table-ids)+ (t2/select-fn-set :db_id :model/Table :id [:in table-ids]))+ :table-ids table-ids})(-> (filterv mi/can-read? measures)(t2/hydrate :creator :definition_description))))diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.cljindex edb4f70c725..7109f98b96b 100644--- a/src/metabase/metabot/tools/resources.clj+++ b/src/metabase/metabot/tools/resources.clj@@ -296,7 +296,7 @@:is_audit false:router_database_id nil{:order-by [[:%lower.name :asc]]})- _ (perms/prime-db-perms-cache {:db-ids (into #{} (map :id) all)})+ _ (perms/prime-database-perms-cache {:db-ids (into #{} (map :id) all)})dbs (->> all(filter mi/can-read?)(mapv present-database))]diff --git a/src/metabase/model_persistence/api.clj b/src/metabase/model_persistence/api.cljindex 0f2636d8cdf..03518d38271 100644--- a/src/metabase/model_persistence/api.clj+++ b/src/metabase/model_persistence/api.clj@@ -72,7 +72,7 @@(perms/check-has-application-permission :monitoring)(let [db-ids (t2/select-fn-set :database_id :model/PersistedInfo)writable-db-ids (when (seq db-ids)- (perms/prime-db-perms-cache {:db-ids db-ids})+ (perms/prime-database-perms-cache {:db-ids db-ids})(->> (t2/select :model/Database :id [:in db-ids])(filter mi/can-write?)(map :id)diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.cljindex ab3cca5ddb1..913d7c279f6 100644--- a/src/metabase/permissions/core.clj+++ b/src/metabase/permissions/core.clj@@ -42,7 +42,7 @@batch-insert-permissions!disable-perms-cachedownload-perms-level- full-db-permission-for-user+ full-database-permission-for-userfull-schema-permission-for-usergroups-have-permission-for-table?index-database-permissions@@ -51,7 +51,7 @@most-permissive-database-permission-for-usernative-download-permission-for-userpermissions-for-user- prime-db-perms-cache+ prime-database-perms-cacheprime-schema-perms-cacheprime-table-perms-cachesandboxes-for-userdiff --git a/src/metabase/permissions/metric.clj b/src/metabase/permissions/metric.cljindex 9e896430e4e..ccef354929f 100644--- a/src/metabase/permissions/metric.clj+++ b/src/metabase/permissions/metric.clj@@ -141,6 +141,8 @@field-info (batch-field-info all-field-ids)table-ids (into #{} (keep :table_id) (vals field-info))table->db (batch-table-db-ids table-ids)+ _ (perms/prime-table-perms-cache {:db-ids (set (vals table->db))+ :table-ids table-ids})sandbox-map (sandbox-restricted-fields table-ids)allowed? (fn [dim](let [fid (get dim->field-id (:id dim))]diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.cljindex 7d2725139e7..c64add98d8c 100644--- a/src/metabase/permissions/models/data_permissions.clj+++ b/src/metabase/permissions/models/data_permissions.clj@@ -6,6 +6,7 @@[metabase.app-db.cluster-lock :as cluster-lock][metabase.app-db.core :as mdb][metabase.audit-app.core :as audit]+ [metabase.config.core :as config][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi][metabase.permissions.published-tables :as published-tables]@@ -250,6 +251,11 @@[(apply min (map first pairs))(apply max (map second pairs))]))+(def ^:private max-ids-per-query+ "How many IDs to list in one load. Each is a bound parameter and Postgres caps a statement at 65535 of them, so a+ caller passing a very large set is split across several queries rather than failing outright."+ 5000)+(def ^:dynamic *use-perms-cache?*"Bind to `false` to intentionally bypass the permissions caches and fetch data straight from the DB."true)@@ -266,6 +272,52 @@(and *use-perms-cache?*(= user-id api/*current-user-id*)))+(def ^:dynamic *perms-cache-misses-are-errors?*+ "Whether a permission check that has to load turns into an exception rather than a query. Bound to true by+ [[with-relevant-permissions-for-user]] in dev and test, so an unprimed batch of checks fails loudly there and+ passes silently -- as one query per entity -- in production."+ false)++(def ^:private max-cache-misses+ "How many loads one cache may serve in a request before the pattern is treated as an unprimed run rather than a few+ unrelated checks.++ A handful is ordinary. Something has to load the cache, and a request can legitimately check several unrelated+ things -- a dashboard whose parameters span two databases, an entity read followed by a chain-filter on a field+ elsewhere, a card touching a few tables. Those cost a bounded number of queries.++ Five is where measurement put the line. Across the search, sandbox, dashboard and sync suites -- discounting the+ failures those suites already have when batched together -- a limit of two flags 19 sites and three flags 10, all+ of them bounded: an entity read plus a chain-filter elsewhere, a dashboard whose parameters span two databases. At+ four and above none of them flag. So this is deliberately a backstop against runs that grow with the data, not a+ linter for every second check."+ 5)++(def ^:private prime-fn-for-kind+ "The prime function that covers each kind of check, for the message [[cache-miss!]] throws. Keyed by what the caller+ was asking about rather than by which cache missed -- a table check reads the database cache on its way through, and+ the fix for that is still `prime-table-perms-cache`.++ Spelled out rather than built from `kind`, so that renaming one of these functions turns up this map. They can't be+ var references: all three are defined further down, after the caches they load."+ {:table "prime-table-perms-cache"+ :schema "prime-schema-perms-cache"+ :database "prime-database-perms-cache"})++(defn- cache-miss!+ "Called from a read-through cache when a check has to load rather than read. Counts the loads and throws once they+ pass [[max-cache-misses]], so an unprimed run fails loudly in dev and test.++ `kind` names what was being checked rather than which cache missed; see [[prime-fn-for-kind]]. `id` is the entity+ the check was for, so the message points at a specific table or database rather than just a shape."+ [kind id cache]+ (when *perms-cache-misses-are-errors?*+ (let [{:keys [misses]} (swap! cache update :misses (fnil inc 0))]+ (when (>= misses max-cache-misses)+ (throw (ex-info (format "Permission checks are running one query per entity, this one for %s %s. Load the scope up front with [[%s]]."+ (name kind) id (prime-fn-for-kind kind))+ {:kind kind, :id id}))))))+(defn is-superuser?"Returns true if the given user ID is a superuser. Avoids a DB query when checking the current user."[user-id]@@ -280,104 +332,122 @@api/*is-data-analyst?*(t2/select-one-fn :is_data_analyst :model/User :id user-id)))-;;; ---------------------------------------------- Table level cache -----------------------------------------------+;;; --------------------------------------------- Database level cache ----------------------------------------------(def ^:dynamic *table-permission-cache*- "Request cache for [[table-permission-for-user]]:+(def ^:dynamic *db-permission-cache*+ "Request cache for every whole-database question:+ `{user-id {perm-type {db-id {:database v :every-table v :any-table v}}}}`.- {:db-ids #{db-id} -- every table in these databases is loaded- :table-ids #{table-id} -- these tables are loaded individually- :perms {user-id {perm-type {db-id {table-id value}}}}}+ The three differ in *how much of the database the value has to hold for*:- Only table-granular rows live here. The grant attached to the database itself is a property of the database, not of- any table, so it comes from [[*db-permission-cache*]]'s `:database` — which means a table with no rows of its- own, or no row in `metabase_table` at all, still resolves correctly.+ - `:database` — the grant attached to the database itself, ignoring per-table grants. It applies to every table+ in the database, so [[table-permission-for-user]] coalesces it with each table's own value. It+ lives here rather than in the table cache because it is a property of the database: a check+ against a table that doesn't exist still has to see it.+ - `:every-table` — the level the user holds over the whole database: within each group the most restrictive value+ across its tables, then the best of those across the user's groups. Answers \"may they do this+ to everything here?\" A database-level row counts as granting every table, which is exactly how+ [[set-table-permissions!]] stores a permission that is uniform across a database.+ - `:any-table` — the level the user holds on at least one table: the loosest value found anywhere in the database.+ Answers \"may they do this to anything here?\"- Nothing invalidates this within a request, so a permission written after a check has already loaded is not seen by- later checks in the same request. That was always true per database; it now spans every database the user can see.+ `:every-table` and `:any-table` bracket the truth: no table in the database is more restricted than the first, and+ none is more permissive than the second. Neither can be derived from the other, because values coalesce per group+ before they coalesce across groups — a user can hold `:unrestricted` on every table without any single group+ granting it on all of them.- Two completeness sets because a load comes in one of two shapes. A whole-database load answers for any table in it,- including tables with no rows of their own. A table-scoped load answers only for the tables it asked about — an- unrequested table is indistinguishable from one with no permissions, and answering for it would invent a- permission. So `:table-ids` records what was *asked for*, not what came back, and a scoped load never touches- `:db-ids`."- (atom {:db-ids #{} :table-ids #{} :perms {}}))+ All three are aggregations of the same rows (see [[perm-rows-query-base]]), so one query computes all of them, for+ every database it is asked about, at once. `:db-ids` records which those were: this cache only ever answers for a+ database it actually loaded, so one created later is fetched rather than read as having no permissions.-(def ^:private max-ids-per-query- "How many IDs to list in one load. Each is a bound parameter and Postgres caps a statement at 65535 of them, so a- caller passing a very large set is split across several queries rather than failing outright."- 5000)+ Checks that walk a list of databases should [[prime-database-perms-cache]] first, exactly as table checks do."+ (atom {:db-ids #{} :perms {}}))-(defn- load-table-permission-perms- "Table-granular permissions for one scope — either `{:table-ids …}` or `{:db-ids …}`. Database-level rows are- excluded; see [[*table-permission-cache*]]."- [user-id {:keys [db-ids table-ids]}]- (reduce (fn [m {:keys [perm_type db_id table_id mn mx]}]- (assoc-in m [(keyword perm_type) db_id table_id]- (ranks->most-permissive-value (keyword perm_type) [mn mx])))- {}- (t2/query (-> (perm-rows-query-base user-id (when-not (seq table-ids) db-ids))- (assoc :select [:p.perm_type :p.db_id :p.table_id- [[:min value-rank-case] :mn]- [[:max value-rank-case] :mx]]- :group-by [:p.perm_type :p.db_id :p.table_id])- (update :where conj [:not= :p.table_id nil])- (cond-> (seq table-ids) (update :where conj [:in :p.table_id table-ids]))))))+(def ^:dynamic *all-db-permission-cache*+ "Request cache for the questions that scan *every* database rather than asking about one --+ [[user-has-any-perms-of-type?]]. Shaped like [[*db-permission-cache*]]'s `:perms`, but loaded in full, so it needs+ no record of what it covers."+ (atom {}))-(defn- merge-table-perms- "Merge freshly loaded table permissions into cached ones, unioning the per-database table maps rather than replacing- them — the same database is loaded more than once, once per batch of tables."- [cached loaded]- (merge-with #(merge-with merge %1 %2) cached loaded))+(defn- load-database-perms+ "Whole-database values for `db-ids`, or for every database when nil."+ [user-id db-ids]+ (let [per-group (-> (perm-rows-query-base user-id db-ids)+ (assoc :select [:p.perm_type :p.db_id :p.group_id+ [[:min value-rank-case] :gmin]+ [[:max value-rank-case] :gmax]+ [[:min db-row-rank-case] :dbmin]+ [[:max db-row-rank-case] :dbmax]]+ :group-by [:p.perm_type :p.db_id :p.group_id]))+ value (fn [perm-type mn mx] (when mn (ranks->most-permissive-value perm-type [mn mx])))]+ (reduce (fn [m {:keys [perm_type db_id any_mn any_mx every_mn every_mx db_mn db_mx]}]+ (let [perm-type (keyword perm_type)]+ (assoc-in m [perm-type db_id]+ {:database (value perm-type db_mn db_mx)+ :every-table (value perm-type every_mn every_mx)+ :any-table (value perm-type any_mn any_mx)})))+ {}+ (t2/query {:select [:i.perm_type :i.db_id+ [[:min :i.gmin] :any_mn] [[:max :i.gmax] :any_mx]+ [[:min :i.gmax] :every_mn] [[:max :i.gmax] :every_mx]+ [[:min :i.dbmin] :db_mn] [[:max :i.dbmax] :db_mx]]+ :from [[per-group :i]]+ :group-by [:i.perm_type :i.db_id]}))))-(defn- load-table-perms!- "Load one scope into [[*table-permission-cache*]] and return the resulting per-user perms map."- [user-id {:keys [db-ids table-ids] :as scope}]- (let [loaded (load-table-permission-perms user-id scope)]- (-> (swap! *table-permission-cache*+(defn- load-database-perms!+ "Load `db-ids` into [[*db-permission-cache*]] and return the resulting per-user map."+ [user-id db-ids]+ (let [loaded (load-database-perms user-id db-ids)]+ (-> (swap! *db-permission-cache*(fn [cache](-> cache- (update (if (seq table-ids) :table-ids :db-ids) into (or (not-empty table-ids) db-ids))- (update-in [:perms user-id] merge-table-perms loaded))))+ (update :db-ids into db-ids)+ (update-in [:perms user-id] #(merge-with merge % loaded)))))(get-in [:perms user-id]))))-(defn prime-table-perms-cache- "Eagerly load table-granular permissions for the current user, so that a run of per-table checks costs one query- instead of one per table. A no-op for superusers, whose checks never read the cache.-- Takes `{:db-ids #{…} :table-ids #{…}}`, and `:table-ids` decides the shape:-- - **given** — load exactly those tables. Right when the candidate set is already small and known: the tables a- card's query reads, a page of recents, the tables behind a list of measures.- - **absent** — load every table in `:db-ids`. Right when the candidate set *is* a database or a schema of one, where- listing the IDs would just be a longhand way of naming the database.+(defn prime-database-perms-cache+ "Eagerly load whole-database permissions for `:db-ids` for the current user, so that a run of per-database checks+ costs one query instead of one per database. A no-op for superusers, whose checks never read the cache.- Supplying both loads the tables and ignores `:db-ids`, since the table list is the narrower scope. Note also that a- table already covered by a fully-loaded database is re-requested, because mapping a table ID back to its database- would itself take a query."- [{:keys [db-ids table-ids]}]+ The counterpart of [[prime-table-perms-cache]], for the checks that walk a list of databases."+ [{:keys [db-ids]}](when (and (use-cache? api/*current-user-id*)(not api/*is-superuser?*))- (let [user-id api/*current-user-id*- cache @*table-permission-cache*]- (if (seq table-ids)- (doseq [batch (partition-all max-ids-per-query- (into #{} (remove (:table-ids cache)) table-ids))]- (load-table-perms! user-id {:table-ids (set batch)}))- (when-let [missing-db-ids (not-empty (into #{} (remove (:db-ids cache)) db-ids))]- (load-table-perms! user-id {:db-ids missing-db-ids})))- nil)))+ (let [missing (into #{} (remove (:db-ids @*db-permission-cache*)) db-ids)]+ (doseq [batch (partition-all max-ids-per-query missing)]+ (load-database-perms! api/*current-user-id* (set batch))))+ nil))-(defn- cached-table-perms- "Read-through for [[*table-permission-cache*]]: the per-user `{perm-type {db-id {table-id value}}}` map, loading- `table-id` first if it isn't covered yet. Callers checking many tables should [[prime-table-perms-cache]] first."- [user-id db-id table-id]+(defn- cached-database-perms+ "The `{perm-type {db-id {:database v :every-table v :any-table v}}}` map for `user-id`, able to answer for+ `database-id`, loading it if this request hasn't yet. Loads straight through without caching when the request cache+ doesn't apply.++ `table-id` affects reporting only. [[table-permission-for-user]] reads this cache on its way to a table check, so a+ miss reached that way is a table check running one query per entity, and the fix is [[prime-table-perms-cache]] --+ not [[prime-database-perms-cache]], which would leave the table half of the check unprimed."+ ([user-id database-id]+ (cached-database-perms user-id database-id nil))+ ([user-id database-id table-id]+ (if (use-cache? user-id)+ (if (contains? (:db-ids @*db-permission-cache*) database-id)+ (get-in @*db-permission-cache* [:perms user-id])+ (do+ (if table-id+ (cache-miss! :table table-id *db-permission-cache*)+ (cache-miss! :database database-id *db-permission-cache*))+ (load-database-perms! user-id #{database-id})))+ (load-database-perms user-id [database-id]))))++(defn- all-database-perms+ "Like [[cached-database-perms]] but for every database at once, for the questions that scan them all."+ [user-id](if (use-cache? user-id)- (if (or (contains? (:db-ids @*table-permission-cache*) db-id)- (contains? (:table-ids @*table-permission-cache*) table-id))- (get-in @*table-permission-cache* [:perms user-id])- (load-table-perms! user-id {:table-ids #{table-id}}))- (load-table-permission-perms user-id {:table-ids #{table-id}})))+ (or (get @*all-db-permission-cache* user-id)+ (let [loaded (load-database-perms user-id nil)]+ (swap! *all-db-permission-cache* assoc user-id loaded)+ loaded))+ (load-database-perms user-id nil)));;; ---------------------------------------------- Schema level cache ----------------------------------------------@@ -423,6 +493,7 @@(do(let [missing-db-ids (into [] (remove (:db-ids @*schema-permission-cache*)) db-ids)](when (seq missing-db-ids)+ (cache-miss! :schema missing-db-ids *schema-permission-cache*)(let [loaded (load-schema-permission-perms user-id missing-db-ids)](swap! *schema-permission-cache*(fn [cache]@@ -432,112 +503,108 @@(get-in @*schema-permission-cache* [:perms user-id]))(load-schema-permission-perms user-id db-ids)))-;;; --------------------------------------------- Database level cache -----------------------------------------------(def ^:dynamic *db-permission-cache*- "Request cache for every whole-database question:- `{user-id {perm-type {db-id {:database v :every-table v :any-table v}}}}`.+;;; ---------------------------------------------- Table level cache ------------------------------------------------ The three differ in *how much of the database the value has to hold for*:+(def ^:dynamic *table-permission-cache*+ "Request cache for [[table-permission-for-user]]:- - `:database` — the grant attached to the database itself, ignoring per-table grants. It applies to every table- in the database, so [[table-permission-for-user]] coalesces it with each table's own value. It- lives here rather than in the table cache because it is a property of the database: a check- against a table that doesn't exist still has to see it.- - `:every-table` — the level the user holds over the whole database: within each group the most restrictive value- across its tables, then the best of those across the user's groups. Answers \"may they do this- to everything here?\" A database-level row counts as granting every table, which is exactly how- [[set-table-permissions!]] stores a permission that is uniform across a database.- - `:any-table` — the level the user holds on at least one table: the loosest value found anywhere in the database.- Answers \"may they do this to anything here?\"+ {:db-ids #{db-id} -- every table in these databases is loaded+ :table-ids #{table-id} -- these tables are loaded individually+ :perms {user-id {perm-type {db-id {table-id value}}}}}- `:every-table` and `:any-table` bracket the truth: no table in the database is more restricted than the first, and- none is more permissive than the second. Neither can be derived from the other, because values coalesce per group- before they coalesce across groups — a user can hold `:unrestricted` on every table without any single group- granting it on all of them.+ Only table-granular rows live here. The grant attached to the database itself is a property of the database, not of+ any table, so it comes from [[*db-permission-cache*]]'s `:database` — which means a table with no rows of its+ own, or no row in `metabase_table` at all, still resolves correctly.- All three are aggregations of the same rows (see [[perm-rows-query-base]]), so one query computes all of them, for- every database it is asked about, at once. `:db-ids` records which those were: this cache only ever answers for a- database it actually loaded, so one created later is fetched rather than read as having no permissions.+ Nothing invalidates this within a request, so a permission written after a check has already loaded is not seen by+ later checks in the same request. That was always true per database; it now spans every database the user can see.- Checks that walk a list of databases should [[prime-db-perms-cache]] first, exactly as table checks do."- (atom {:db-ids #{} :perms {}}))+ Two completeness sets because a load comes in one of two shapes. A whole-database load answers for any table in it,+ including tables with no rows of their own. A table-scoped load answers only for the tables it asked about — an+ unrequested table is indistinguishable from one with no permissions, and answering for it would invent a+ permission. So `:table-ids` records what was *asked for*, not what came back, and a scoped load never touches+ `:db-ids`."+ (atom {:db-ids #{} :table-ids #{} :perms {}}))-(def ^:dynamic *all-db-permission-cache*- "Request cache for the questions that scan *every* database rather than asking about one --- [[user-has-any-perms-of-type?]]. Shaped like [[*db-permission-cache*]]'s `:perms`, but loaded in full, so it needs- no record of what it covers."- (atom {}))+(defn- load-table-permission-perms+ "Table-granular permissions for one scope — either `{:table-ids …}` or `{:db-ids …}`. Database-level rows are+ excluded; see [[*table-permission-cache*]]."+ [user-id {:keys [db-ids table-ids]}]+ (reduce (fn [m {:keys [perm_type db_id table_id mn mx]}]+ (assoc-in m [(keyword perm_type) db_id table_id]+ (ranks->most-permissive-value (keyword perm_type) [mn mx])))+ {}+ (t2/query (-> (perm-rows-query-base user-id (when-not (seq table-ids) db-ids))+ (assoc :select [:p.perm_type :p.db_id :p.table_id+ [[:min value-rank-case] :mn]+ [[:max value-rank-case] :mx]]+ :group-by [:p.perm_type :p.db_id :p.table_id])+ (update :where conj [:not= :p.table_id nil])+ (cond-> (seq table-ids) (update :where conj [:in :p.table_id table-ids]))))))-(defn- load-db-perms- "Whole-database values for `db-ids`, or for every database when nil."- [user-id db-ids]- (let [per-group (-> (perm-rows-query-base user-id db-ids)- (assoc :select [:p.perm_type :p.db_id :p.group_id- [[:min value-rank-case] :gmin]- [[:max value-rank-case] :gmax]- [[:min db-row-rank-case] :dbmin]- [[:max db-row-rank-case] :dbmax]]- :group-by [:p.perm_type :p.db_id :p.group_id]))- value (fn [perm-type mn mx] (when mn (ranks->most-permissive-value perm-type [mn mx])))]- (reduce (fn [m {:keys [perm_type db_id any_mn any_mx every_mn every_mx db_mn db_mx]}]- (let [perm-type (keyword perm_type)]- (assoc-in m [perm-type db_id]- {:database (value perm-type db_mn db_mx)- :every-table (value perm-type every_mn every_mx)- :any-table (value perm-type any_mn any_mx)})))- {}- (t2/query {:select [:i.perm_type :i.db_id- [[:min :i.gmin] :any_mn] [[:max :i.gmax] :any_mx]- [[:min :i.gmax] :every_mn] [[:max :i.gmax] :every_mx]- [[:min :i.dbmin] :db_mn] [[:max :i.dbmax] :db_mx]]- :from [[per-group :i]]- :group-by [:i.perm_type :i.db_id]}))))+(defn- merge-table-perms+ "Merge freshly loaded table permissions into cached ones, unioning the per-database table maps rather than replacing+ them — the same database is loaded more than once, once per batch of tables."+ [cached loaded]+ (merge-with #(merge-with merge %1 %2) cached loaded))-(defn- load-db-perms!- "Load `db-ids` into [[*db-permission-cache*]] and return the resulting per-user map."- [user-id db-ids]- (let [loaded (load-db-perms user-id db-ids)]- (-> (swap! *db-permission-cache*+(defn- load-table-perms!+ "Load one scope into [[*table-permission-cache*]] and return the resulting per-user perms map."+ [user-id {:keys [db-ids table-ids] :as scope}]+ (let [loaded (load-table-permission-perms user-id scope)]+ (-> (swap! *table-permission-cache*(fn [cache](-> cache- (update :db-ids into db-ids)- (update-in [:perms user-id] #(merge-with merge % loaded)))))+ (update (if (seq table-ids) :table-ids :db-ids) into (or (not-empty table-ids) db-ids))+ (update-in [:perms user-id] merge-table-perms loaded))))(get-in [:perms user-id]))))-(defn prime-db-perms-cache- "Eagerly load whole-database permissions for `:db-ids` for the current user, so that a run of per-database checks- costs one query instead of one per database. A no-op for superusers, whose checks never read the cache.+(defn prime-table-perms-cache+ "Eagerly load table-granular permissions for the current user, so that a run of per-table checks costs one query+ instead of one per table. A no-op for superusers, whose checks never read the cache.- The counterpart of [[prime-table-perms-cache]], for the checks that walk a list of databases."- [{:keys [db-ids]}]+ Takes `{:db-ids #{…} :table-ids #{…}}`, and `:table-ids` decides the shape:++ - **given** — load exactly those tables. Right when the candidate set is already small and known: the tables a+ card's query reads, a page of recents, the tables behind a list of measures.+ - **absent** — load every table in `:db-ids`. Right when the candidate set *is* a database or a schema of one, where+ listing the IDs would just be a longhand way of naming the database.++ Supplying both loads the tables and ignores `:db-ids` *for the table load*, since the table list is the narrower+ scope. Note also that a table already covered by a fully-loaded database is re-requested, because mapping a table ID+ back to its database would itself take a query.++ `:db-ids` is never wasted, though: whichever shape is used, it also primes [[*db-permission-cache*]], because+ [[table-permission-for-user]] reads the database's own grant as well as the table's. A caller that passes only+ `:table-ids` still pays one query per distinct database, so pass `:db-ids` too whenever the rows are already in+ hand -- it costs nothing extra when they are."+ [{:keys [db-ids table-ids]}](when (and (use-cache? api/*current-user-id*)(not api/*is-superuser?*))- (let [missing (into #{} (remove (:db-ids @*db-permission-cache*)) db-ids)]- (doseq [batch (partition-all max-ids-per-query missing)]- (load-db-perms! api/*current-user-id* (set batch))))- nil))--(defn- cached-db-perms- "The `{perm-type {db-id {:database v :every-table v :any-table v}}}` map for `user-id`, able to answer for- `database-id`, loading it if this request hasn't yet. Loads straight through without caching when the request cache- doesn't apply."- [user-id database-id]- (if (use-cache? user-id)- (if (contains? (:db-ids @*db-permission-cache*) database-id)- (get-in @*db-permission-cache* [:perms user-id])- (load-db-perms! user-id #{database-id}))- (load-db-perms user-id [database-id])))+ (let [user-id api/*current-user-id*+ cache @*table-permission-cache*]+ (if (seq table-ids)+ (doseq [batch (partition-all max-ids-per-query+ (into #{} (remove (:table-ids cache)) table-ids))]+ (load-table-perms! user-id {:table-ids (set batch)}))+ (doseq [batch (partition-all max-ids-per-query+ (into #{} (remove (:db-ids cache)) db-ids))]+ (load-table-perms! user-id {:db-ids (set batch)})))+ (prime-database-perms-cache {:db-ids db-ids})+ nil)))-(defn- all-db-perms- "Like [[cached-db-perms]] but for every database at once, for the questions that scan them all."- [user-id]+(defn- cached-table-perms+ "Read-through for [[*table-permission-cache*]]: the per-user `{perm-type {db-id {table-id value}}}` map, loading+ `table-id` first if it isn't covered yet. Callers checking many tables should [[prime-table-perms-cache]] first."+ [user-id db-id table-id](if (use-cache? user-id)- (or (get @*all-db-permission-cache* user-id)- (let [loaded (load-db-perms user-id nil)]- (swap! *all-db-permission-cache* assoc user-id loaded)- loaded))- (load-db-perms user-id nil)))+ (if (or (contains? (:db-ids @*table-permission-cache*) db-id)+ (contains? (:table-ids @*table-permission-cache*) table-id))+ (get-in @*table-permission-cache* [:perms user-id])+ (do+ (cache-miss! :table table-id *table-permission-cache*)+ (load-table-perms! user-id {:table-ids #{table-id}})))+ (load-table-permission-perms user-id {:table-ids #{table-id}})));;; ---------------------------------------------- Table level checks ----------------------------------------------@@ -578,7 +645,7 @@(most-permissive-value perm-type):else- (let [db-perm (get-in (cached-db-perms user-id database-id) [perm-type database-id :database])+ (let [db-perm (get-in (cached-database-perms user-id database-id table-id) [perm-type database-id :database])table-perm (if (= db-perm (most-permissive-value perm-type))db-perm(coalesce perm-type@@ -613,7 +680,8 @@(when (and (use-cache? api/*current-user-id*)(not api/*is-superuser?*)(seq db-ids))- (cached-schema-perms api/*current-user-id* db-ids)+ (binding [*perms-cache-misses-are-errors?* false]+ (cached-schema-perms api/*current-user-id* db-ids))nil))(mu/defn schema-permission-for-user :- ::permissions.schema/data-permission-value@@ -706,7 +774,7 @@{perm-type (permissions.schema/data-permissions perm-type)})))(if (is-superuser? user-id)(most-permissive-value perm-type)- (or (get-in (cached-db-perms user-id database-id) [perm-type database-id :database])+ (or (get-in (cached-database-perms user-id database-id) [perm-type database-id :database])(least-permissive-value perm-type))))(mu/defn user-has-permission-for-database? :- :boolean@@ -717,7 +785,7 @@(database-permission-for-user user-id perm-type database-id)perm-value))-(mu/defn full-db-permission-for-user :- ::permissions.schema/data-permission-value+(mu/defn full-database-permission-for-user :- ::permissions.schema/data-permission-value"Returns the effective *db-level* permission value for a given user, permission type, and database ID. If the userhas multiple permissions for the given type in different groups, they are coalesced into a single value. Thedb-level permission is the *most* restrictive table-level permission within that database."@@ -734,16 +802,16 @@(most-permissive-value perm-type):else- (or (get-in (cached-db-perms user-id database-id) [perm-type database-id :every-table])+ (or (get-in (cached-database-perms user-id database-id) [perm-type database-id :every-table])(least-permissive-value perm-type))))(mu/defn native-download-permission-for-user :- ::permissions.schema/data-permission-value"Returns the effective download permission value for a given user and database ID, for native queries on the database.For each group, the native download permission for a database is equal to the lowest permission level of any table in- the database — exactly [[full-db-permission-for-user]] for :perms/download-results, whose cache it shares."+ the database — exactly [[full-database-permission-for-user]] for :perms/download-results, whose cache it shares."[user-id :- ::lib.schema.id/userdatabase-id :- ::lib.schema.id/database]- (full-db-permission-for-user user-id :perms/download-results database-id))+ (full-database-permission-for-user user-id :perms/download-results database-id))(mu/defn most-permissive-database-permission-for-user :- ::permissions.schema/data-permission-value"Similar to checking _partial_ permissions with permissions paths - what is the *most permissive* permission the@@ -767,12 +835,12 @@(most-permissive-value perm-type)database-id- (or (get-in (cached-db-perms user-id database-id) [perm-type database-id :any-table])+ (or (get-in (cached-database-perms user-id database-id) [perm-type database-id :any-table])(least-permissive-value perm-type)):else(let [ordered (-> permissions.schema/data-permissions perm-type :values)- values (into #{} (keep :any-table) (vals (get (all-db-perms user-id) perm-type)))]+ values (into #{} (keep :any-table) (vals (get (all-database-perms user-id) perm-type)))](or (first (filter values ordered))(least-permissive-value perm-type))))))@@ -793,7 +861,7 @@(boolean (some (fn [[db-id vals]](and (= (:any-table vals) value)(not (exclude? db-id))))- (get (all-db-perms user-id) perm-type))))))+ (get (all-database-perms user-id) perm-type))))));;; --------------------------------------- cache assembly ------------------------------------------------------------@@ -821,6 +889,7 @@*schema-permission-cache* (atom {:db-ids #{} :perms {}})*db-permission-cache* (atom {:db-ids #{} :perms {}})*all-db-permission-cache* (atom {})+ *perms-cache-misses-are-errors?* (or config/is-dev? config/is-test?)*sandboxes-for-user* (delay (enforced-sandboxes-for-user ~user-id))]~@body))@@ -859,7 +928,8 @@(defn- admin-permission-graph"Returns the graph representing admin permissions for all groups"[& {:keys [db-id perm-type]}]- (let [db-ids (if db-id [db-id] (t2/select-pks-vec :model/Database))+ ;; destination databases are reachable only through their router and never carry permissions of their own+ (let [db-ids (if db-id [db-id] (t2/select-pks-vec :model/Database :router_database_id nil))perm-types (if perm-type [perm-type] (keys permissions.schema/data-permissions))](into {} (map (fn [db-id][db-id (into {} (map (fn [perm] [perm (most-permissive-value perm)])diff --git a/src/metabase/queries/metadata.clj b/src/metabase/queries/metadata.cljindex b8877663d12..e92f366c8f8 100644--- a/src/metabase/queries/metadata.clj+++ b/src/metabase/queries/metadata.clj@@ -30,7 +30,7 @@[db :- [:map]](if (and (not (:is_audit db))(= :query-builder-and-native- (perms/full-db-permission-for-user+ (perms/full-database-permission-for-userapi/*current-user-id*:perms/create-queries(u/the-id db))))@@ -40,7 +40,7 @@(defn- get-databases[ids](when (seq ids)- (perms/prime-db-perms-cache {:db-ids (set ids)})+ (perms/prime-database-perms-cache {:db-ids (set ids)})(into [] (comp (filter mi/can-read?)(map #(assoc % :native_permissions (get-native-perms-info %))))(t2/select :model/Database :id [:in ids]))))diff --git a/src/metabase/query_permissions/impl.clj b/src/metabase/query_permissions/impl.cljindex 435ca3a3d45..bee2294fe37 100644--- a/src/metabase/query_permissions/impl.clj+++ b/src/metabase/query_permissions/impl.clj@@ -286,14 +286,14 @@"Checks that the current user has at least `required-perm` for the entire DB specified by `db-id`."[perm-type required-perm db-id](perms/at-least-as-permissive? perm-type- (perms/full-db-permission-for-user api/*current-user-id* perm-type db-id)+ (perms/full-database-permission-for-user api/*current-user-id* perm-type db-id)required-perm))(defn- has-perm-for-table?"Checks that the current user has the permissions for tables specified in `table-id->perm`. Returns true if accessis allowed, otherwise false."[perm-type table-id->required-perm db-id]- (perms/prime-table-perms-cache {:table-ids (set (keys table-id->required-perm))})+ (perms/prime-table-perms-cache {:db-ids #{db-id} :table-ids (set (keys table-id->required-perm))})(every? (fn [[table-id required-perm]](perms/user-has-permission-for-table?api/*current-user-id*@@ -324,7 +324,7 @@table-ids (into (set (keep (some-fn :table-id :table_id) result-metadata))(when (seq field-ids)(t2/select-fn-set :table_id :model/Field :id [:in field-ids])))]- (perms/prime-table-perms-cache {:table-ids table-ids})+ (perms/prime-table-perms-cache {:db-ids #{database-id} :table-ids table-ids})(run! #(when-not (perms/user-has-permission-for-table?api/*current-user-id*:perms/view-datadiff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.cljindex dc6378183b4..49f71f12e1a 100644--- a/src/metabase/query_processor/middleware/permissions.clj+++ b/src/metabase/query_processor/middleware/permissions.clj@@ -195,7 +195,7 @@[{database-id :database, :as _query}](or(not *current-user-id*)- (= (perms/full-db-permission-for-user *current-user-id* :perms/create-queries database-id)+ (= (perms/full-database-permission-for-user *current-user-id* :perms/create-queries database-id):query-builder-and-native)))(defn check-current-user-has-adhoc-native-query-permsdiff --git a/src/metabase/search/impl.clj b/src/metabase/search/impl.cljindex f9318a085d8..b976aa2b3fd 100644--- a/src/metabase/search/impl.clj+++ b/src/metabase/search/impl.clj@@ -79,8 +79,8 @@(let [user-id (:current-user-id search-ctx)db-id (:database_id instance)](and- (= :query-builder-and-native (perms/full-db-permission-for-user user-id :perms/create-queries db-id))- (= :unrestricted (perms/full-db-permission-for-user user-id :perms/view-data db-id)))))+ (= :query-builder-and-native (perms/full-database-permission-for-user user-id :perms/create-queries db-id))+ (= :unrestricted (perms/full-database-permission-for-user user-id :perms/view-data db-id)))))(defmethod check-permissions-for-model :metric[search-ctx instance]@@ -106,6 +106,26 @@(can-write? search-ctx instance)(can-read? search-ctx instance)))+(defn- prime-perms!+ "Load the permissions that the per-result checks are about to read.++ Only the `table` and `indexed-entity` methods of [[check-permissions-for-model]] consult them; the rest are answered+ in SQL or from the collection. A `table` result is checked with `can-query?`, which reads both the table's own grant+ and its database's, so this primes the table cache -- which covers the database cache too. `indexed-entity` needs+ only the database half.++ Results are consumed as a stream, so there is no point at which the databases they span are known -- and a search+ ranges over all of them anyway, so prime every database. Passing only `:db-ids` is what makes that affordable: the+ table cache holds `data_permissions` rows with a `table_id`, so the load is bounded by how many table-level grants+ the user's groups actually have, not by how many tables exist.++ Destination databases are skipped -- they are reachable only through their router and never carry+ `data_permissions` rows of their own, so priming them would just widen the load for nothing."+ [search-ctx]+ (when (and (not (:is-superuser? search-ctx))+ (some #{"table" "indexed-entity"} (:models search-ctx)))+ (perms/prime-table-perms-cache {:db-ids (t2/select-pks-set :model/Database :router_database_id nil)})))+(defn- hydrate-user-metadata"Hydrate common-name for last_edited_by and created_by for each result."[results]@@ -492,6 +512,7 @@Hydration is the expensive part, so it should only ever run on the rows actually returned.Pass the result to [[search-results]] for the API response shape; [[search]] composes the two."[search-ctx :- SearchContext]+ (prime-perms! search-ctx)(let [reducible-results (search.engine/results search-ctx)scoring-ctx (select-keys search-ctx [:search-engine :search-string :search-native-query])xf (compdiff --git a/src/metabase/segments/api.clj b/src/metabase/segments/api.cljindex 4fd50bbf173..9cafc1a7dc3 100644--- a/src/metabase/segments/api.clj+++ b/src/metabase/segments/api.clj@@ -80,7 +80,9 @@:archived false{:order-by [[:%lower.name :asc]]})table-ids (into #{} (keep :table_id) segments)]- (perms/prime-table-perms-cache {:table-ids table-ids})+ (perms/prime-table-perms-cache {:db-ids (when (seq table-ids)+ (t2/select-fn-set :db_id :model/Table :id [:in table-ids]))+ :table-ids table-ids})(-> (filterv mi/can-read? segments)(t2/hydrate :creator :definition_description))))diff --git a/src/metabase/transforms/models/transform.clj b/src/metabase/transforms/models/transform.cljindex 0a0ca0a95dd..2aae58ad1ac 100644--- a/src/metabase/transforms/models/transform.clj+++ b/src/metabase/transforms/models/transform.clj@@ -46,7 +46,7 @@(or (not (transforms-base.u/native-query-transform? instance))(and source-db-id(= :query-builder-and-native- (perms/full-db-permission-for-user api/*current-user-id* :perms/create-queries source-db-id)))))+ (perms/full-database-permission-for-user api/*current-user-id* :perms/create-queries source-db-id)))))(defn- transform-writable?"Whether the current user can write `instance`. Any extra `args` (an optional `models-cache`) arediff --git a/src/metabase/typed_schemas/api/schema/table.clj b/src/metabase/typed_schemas/api/schema/table.cljindex 8d091d23ea5..20fac8221bb 100644--- a/src/metabase/typed_schemas/api/schema/table.clj+++ b/src/metabase/typed_schemas/api/schema/table.clj@@ -41,7 +41,8 @@(defn- filter-readable-tables"Filters tables down to the ones the current user can read."[tables]- (perms/prime-table-perms-cache {:table-ids (into #{} (map :id) tables)})+ (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (map :id) tables)})(filter mi/can-read? tables))(defn select-tablesdiff --git a/src/metabase/warehouse_schema/models/field.clj b/src/metabase/warehouse_schema/models/field.cljindex 24f932832ea..bcabcce9418 100644--- a/src/metabase/warehouse_schema/models/field.clj+++ b/src/metabase/warehouse_schema/models/field.clj@@ -9,6 +9,7 @@[metabase.models.humanization :as humanization][metabase.models.interface :as mi][metabase.models.serialization :as serdes]+ [metabase.permissions.core :as perms][metabase.premium-features.core :refer [defenterprise]][metabase.remote-sync.core :as remote-sync][metabase.util :as u]@@ -377,11 +378,19 @@true)(defn readable-fields-only- "Efficiently checks if each field is readable and returns only readable fields"+ "Efficiently checks if each field is readable and returns only readable fields.++ Reading a Field delegates to its Table, so the tables are loaded up front -- otherwise this costs a query per+ distinct table, and another per distinct database, which is what makes it expensive for callers whose fields fan+ out across tables, such as hydrating `:target` over a dashboard's FK columns."[fields]- (for [field (t2/hydrate fields :table)- :when (mi/can-read? field)]- (dissoc field :table)))+ (let [fields (t2/hydrate fields :table)+ tables (into #{} (keep :table) fields)]+ (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (keep :id) tables)})+ (for [field fields+ :when (mi/can-read? field)]+ (dissoc field :table))))(mi/define-batched-hydration-method with-targets:targetdiff --git a/src/metabase/warehouse_schema/table.clj b/src/metabase/warehouse_schema/table.cljindex 9fd25c787dd..9a160eb4fda 100644--- a/src/metabase/warehouse_schema/table.clj+++ b/src/metabase/warehouse_schema/table.clj@@ -71,7 +71,8 @@([ids {:keys [include-sensitive-fields?]}](when (seq ids)(let [tables (t2/select :model/Table :id [:in ids])- _ (perms/prime-table-perms-cache {:table-ids (into #{} (map :id) tables)})+ _ (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (map :id) tables)})tables (filter can-access-table-for-query-metadata? tables)tables (t2/hydrate tables[:fields [:target :has_field_values] :has_field_values :dimensions :name_field]diff --git a/src/metabase/warehouse_schema_rest/api/table.clj b/src/metabase/warehouse_schema_rest/api/table.cljindex 3025807bbda..fc7081b08a1 100644--- a/src/metabase/warehouse_schema_rest/api/table.clj+++ b/src/metabase/warehouse_schema_rest/api/table.clj@@ -123,7 +123,8 @@(premium-features/any-transforms-enabled?) (conj :transform))](as-> (t2/select :model/Table query) tables(apply t2/hydrate tables hydrations)- (do (perms/prime-table-perms-cache {:table-ids (into #{} (map :id) tables)})+ (do (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (map :id) tables)})tables)(into [] (comp (filter mi/can-read?)(if can-query (filter mi/can-query?) identity)diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 22da5984fc0..ffd55e976e7 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -134,7 +134,7 @@(assoc db:native_permissions(if (= :query-builder-and-native- (perms/full-db-permission-for-user+ (perms/full-database-permission-for-userapi/*current-user-id*:perms/create-queries(u/the-id db)))@@ -351,7 +351,7 @@dbs (t2/select :model/Database {:order-by [:%lower.name :%lower.engine]:where where-clause});; everything below walks the list one database at a time- _ (perms/prime-db-perms-cache {:db-ids (into #{} (map :id) dbs)})]+ _ (perms/prime-database-perms-cache {:db-ids (into #{} (map :id) dbs)})](cond-> (-> dbs add-native-perms-info add-transforms-perms-info)include-tables? (add-tables :can-query? can-query? :can-write-metadata? can-write-metadata?)include-schemas? add-schemas@@ -1411,6 +1411,7 @@filter-schemas-by-tables (fn [schemas](if (or can-query? can-write-metadata?)(let [tables (t2/select :model/Table :db_id id :active true)+ _ (perms/prime-table-perms-cache {:db-ids #{id}})filtered-tables (cond->> tablescan-query? (filter mi/can-query?)can-write-metadata? (filter mi/can-write?))diff --git a/src/metabase/xrays/automagic_dashboards/core.clj b/src/metabase/xrays/automagic_dashboards/core.cljindex 7505f1b3d77..997118f4a9e 100644--- a/src/metabase/xrays/automagic_dashboards/core.clj+++ b/src/metabase/xrays/automagic_dashboards/core.clj@@ -979,7 +979,8 @@[:= :active true]]schema (conj [:= :schema schema])))];; the readable check below runs once per table, so load their permissions in one go- (perms/prime-table-perms-cache {:table-ids (into #{} (map :id) tables)})+ (perms/prime-table-perms-cache {:db-ids (into #{} (keep :db_id) tables)+ :table-ids (into #{} (map :id) tables)})(->> tables(filter mi/can-read?)(map (fn [table]diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.cljindex c0ed395d569..118938e865c 100644--- a/test/metabase/permissions/models/data_permissions_test.clj+++ b/test/metabase/permissions/models/data_permissions_test.clj@@ -1,5 +1,6 @@(ns metabase.permissions.models.data-permissions-test(:require+ [clojure.string :as str][clojure.test :refer :all][metabase.api.common :as api][metabase.app-db.core :as mdb]@@ -49,7 +50,7 @@(binding [api/*current-user-id* user-iddata-perms/*db-permission-cache* cachedata-perms/*all-db-permission-cache* (atom {})]- (mt/with-dynamic-fn-redefs [data-perms/load-db-perms+ (mt/with-dynamic-fn-redefs [data-perms/load-database-perms(fn [_user-id db-ids](swap! loads conj (some-> db-ids set)){:perms/manage-database {10 {:database :yes :every-table :yes :any-table :yes}@@ -59,7 +60,7 @@(testing "one load per database serves all three of its values"(is (true? (data-perms/user-has-permission-for-database? user-id :perms/manage-database :yes 10)))(is (= :query-builder- (data-perms/full-db-permission-for-user user-id :perms/create-queries 10)))+ (data-perms/full-database-permission-for-user user-id :perms/create-queries 10)))(is (= :query-builder-and-native(data-perms/most-permissive-database-permission-for-user user-id :perms/create-queries 10)))(is (= [#{10}] @loads)))@@ -68,7 +69,7 @@(is (= [#{10} #{11}] @loads)))(testing "priming a list of databases takes them in one go, skipping those already loaded"(reset! loads [])- (data-perms/prime-db-perms-cache {:db-ids #{10 12 13}})+ (data-perms/prime-database-perms-cache {:db-ids #{10 12 13}})(is (= [#{12 13}] @loads)))(testing "a database with no rows still counts as loaded, and is not re-queried"(reset! loads [])@@ -215,6 +216,31 @@(testing "an empty seq does not throw"(is (nil? (data-perms/assert-no-destination-db-permissions! [])))))))+(deftest cache-miss-error-names-its-prime-function-test+ (testing "the miss error points at the prime function for the kind of check, and names the entity"+ (binding [data-perms/*perms-cache-misses-are-errors?* true]+ ;; ids past the thousands, so a regression to formatting them as numbers -- "1,234,567" -- is caught+ (doseq [[kind id expected-fn] [[:table 1234567 "prime-table-perms-cache"]+ [:schema #{7654321} "prime-schema-perms-cache"]+ [:database 9876543 "prime-database-perms-cache"]]]+ (testing kind+ (let [cache (atom {})+ miss! #(#'data-perms/cache-miss! kind id cache)]+ (testing "a handful of misses is allowed"+ (dotimes [_ 4] (is (nil? (miss!)))))+ (let [e (is (thrown? ExceptionInfo (miss!)))]+ (is (str/includes? (ex-message e) (str "[[" expected-fn "]]")))+ (is (str/includes? (ex-message e) (str id)))+ (is (= {:kind kind :id id} (ex-data e))))))))))++(deftest admin-permission-graph-excludes-destination-dbs-test+ (testing "the admin permission graph covers routers but not their destinations"+ (mt/with-temp [:model/Database {router-db-id :id} {}+ :model/Database {destination-db-id :id} {:router_database_id router-db-id}]+ (let [graph (data-perms/permissions-for-user (mt/user->id :crowberto))]+ (is (contains? graph router-db-id))+ (is (not (contains? graph destination-db-id)))))))+(deftest destination-db-permissions-positive-control-test(testing "inserting a perm row for a normal (non-destination) db still succeeds"(mt/with-temp [:model/PermissionsGroup {group-id :id} {}@@ -379,9 +405,10 @@(is (= :yes (data-perms/database-permission-for-user user-id :perms/manage-database database-id-1)))(is (zero? (call-count))))(testing "a different database is a cache miss"- (t2/with-call-count [call-count]- (data-perms/database-permission-for-user user-id :perms/manage-database database-id-2)- (is (pos? (call-count)))))))))))+ (binding [data-perms/*perms-cache-misses-are-errors?* false]+ (t2/with-call-count [call-count]+ (data-perms/database-permission-for-user user-id :perms/manage-database database-id-2)+ (is (pos? (call-count))))))))))))(deftest table-permission-for-user-absent-table-still-sees-database-grant-test(testing "a check against a table that doesn't exist still sees the database-level grant"