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
Keep curated-only Metabots within curated content in every tool that names an entity (#84761)
lbrdnk32d0cbcedab4
diff --git a/src/metabase/metabot/curation.clj b/src/metabase/metabot/curation.cljindex 79e421a572c..303e3f66623 100644--- a/src/metabase/metabot/curation.clj+++ b/src/metabase/metabot/curation.clj@@ -1,5 +1,6 @@(ns metabase.metabot.curation- "Source-of-truth curation check for Metabot's recent-view context.+ "Source-of-truth curation checks for Metabot: the recent-view context, and the tools that must stay within curated+ content when a Metabot has `use_verified_content` on (see [[curated-only?]]).Reads each item's curation signals straight from the source tables and applies the canonical[[metabase.collections.curation/curated?]] predicate, so it has no dependency on the search index beingpresent, fresh, or complete, and can't drift from the rule.@@ -7,14 +8,54 @@are higher-level models a foundational module shouldn't reference; a consistency test pins it to theindex's precomputed `curated` column."(:require+ [clojure.set :as set]+ [metabase.audit-app.core :as audit-app][metabase.collections.curation :as curation][metabase.collections.models.collection :as collection]- [metabase.metabot.db :as metabot.db]))+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.walk :as lib.walk]+ [metabase.metabot.db :as metabot.db]+ [metabase.metabot.tools.recoverable.common :as recoverable.common]+ [metabase.models.interface :as mi]+ [metabase.premium-features.core :as premium-features]+ [metabase.util :as u]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++;;; ----- Session -----++(def curation-exempt-profiles+ "Profiles whose tools aren't restricted by `use_verified_content`. The nlq profile discovers data through the+ Library index, which carries its own scope; `nlq-fallback` serves the same profile with general search. The sql+ profile writes SQL, which the product decision leaves outside the rule, and reads raw tables' columns to do it."+ #{:nlq :nlq-fallback :sql})++(defn curated-only?+ "Whether a Metabot tool running for the Metabot row `metabot` under `profile-id` may only reach curated content:+ the Metabot has `use_verified_content` on and the profile isn't in [[curation-exempt-profiles]]."+ [metabot profile-id]+ (boolean+ (and (:use_verified_content metabot)+ (not (contains? curation-exempt-profiles (some-> profile-id name keyword))))))++;;; ----- Curation signals -----(def ^:private report-card-models"Search-model strings backed by `report_card`, moderated as \"card\"."#{"card" "dataset" "metric"})+(defn- feature-gates+ "Which curation signals the licence currently grants. The signals are read live from the source tables, where a+ review, an official collection, or a Library stays after a downgrade, so each one counts only while its feature+ is active, as in [[metabot.db/metabot-metrics-and-models]] and the in-place search filter."+ []+ {:verified? (premium-features/has-feature? :content-verification)+ :official? (premium-features/has-feature? :official-collections)+ :library? (premium-features/has-feature? :library)})+(defn- verified-item-ids"Subset of `ids` whose most-recent moderation review for `item-type` is \"verified\"."[ids item-type]@@ -37,34 +78,46 @@:collection_type type})))(defn- moderatable-curation-signals- "`[id signal-map]` pairs for collection-housed, moderatable models (report_card-backed and dashboards)."+ "`[id signal-map]` pairs for collection-housed, moderatable models (report_card-backed and dashboards). An archived+ item, or a Card owned by a document, gets no pair: search never returns either, so neither is curated."[search-model t2-model item-type ids]- (let [rows (case t2-model- :model/Card (metabot.db/card-collection-ids ids)- :model/Dashboard (metabot.db/dashboard-collection-ids ids))+ (let [{:keys [verified? official? library?]} (feature-gates)+ rows (->> (case t2-model+ :model/Card (metabot.db/card-curation-rows ids)+ :model/Dashboard (metabot.db/dashboard-curation-rows ids))+ (remove #(or (:archived %) (:document_id %))))coll (collection-info (into #{} (keep :collection_id) rows))- verified (verified-item-ids ids item-type)]+ verified (if verified? (verified-item-ids (mapv :id rows) item-type) #{})](for [{:keys [id collection_id]} rows][id {:model search-model:verified (contains? verified id);; authority_level is keyword-transformed on read (collection :type is not — it stays a string,;; which is what curated? and library-root-collection-types expect)- :official_collection (= :official (:authority_level (get coll collection_id)))- :root_collection_type (root-collection-type-of coll collection_id)}])))+ :official_collection (and official? (= :official (:authority_level (get coll collection_id))))+ :root_collection_type (when library? (root-collection-type-of coll collection_id))}])))(defn- table-curation-signals- "`[id signal-map]` pairs for tables, whose curation comes from is_published, data_layer, and data_authority.+ "`[id signal-map]` pairs for tables, whose curation comes from is_published, data_layer, and data_authority. An+ inactive or hidden table, or one on a routing destination or the audit database, gets no pair: search never+ returns it, so it isn't curated.root_collection_type is intentionally omitted: the table search spec joins the collection only forpublished tables, and for a published table is_published already decides curation, so the root type neverchanges a table's verdict. Omitting it avoids re-deriving from a possibly-stale collection_id, and letscurated? coerce is_published consistently (only true/1 count)."[ids]- (for [{:keys [id is_published data_layer data_authority]}- (metabot.db/table-curation-rows ids)]- [id {:model "table"- :is_published is_published- :data_layer data_layer- :data_authority data_authority}]))+ (let [{:keys [library?]} (feature-gates)+ rows (metabot.db/table-curation-rows ids)+ destinations (let [db-ids (into #{} (keep :db_id) rows)]+ (if (seq db-ids) (metabot.db/destination-database-ids db-ids) #{}))]+ (for [{:keys [id db_id active visibility_type is_published data_layer data_authority]} rows+ :when (and active+ (nil? visibility_type)+ (not (contains? destinations db_id))+ (not= db_id audit-app/audit-db-id))]+ [id {:model "table"+ :is_published (and library? is_published)+ :data_layer data_layer+ :data_authority data_authority}])))(defn- curation-signals"`[id signal-map]` pairs for the given search-model string and ids, read from source-of-truth tables."@@ -74,13 +127,348 @@(= "dashboard" model) (moderatable-curation-signals model :model/Dashboard "dashboard" ids)(= "table" model) (table-curation-signals ids)))+(def ^:private curation-batch-size+ "How many ids one curation lookup reads at a time. Curated-only listings can judge every table in a warehouse, and+ one `IN (...)` over all of them could exceed the app DB's bind-parameter limit (65535 on Postgres)."+ 10000)+(defn curated-ids"Of `model+ids` (`[search-model-string id]` pairs), return the subset that are curated.Reads each item's signals from the source tables and applies [[curation/curated?]], with no search-indexdependency. Recognizes the Metabot recent-view models: card/dataset/metric, dashboard, table."[model+ids](into #{}- (for [[model ids] (update-vals (group-by first model+ids) #(mapv second %))- [id signals] (curation-signals model ids)+ (for [[model ids] (update-vals (group-by first model+ids) #(into [] (comp (map second) (distinct)) %))+ batch (partition-all curation-batch-size ids)+ [id signals] (curation-signals model (vec batch)):when (curation/curated? signals)][model id])))++;;; ----- Query sources -----+;;;+;;; One rule for what a curated-only Metabot may query, shared by `construct_notebook_query`, `run_query` over a+;;; viewed saved question, and the metric reads of `read_resource`, `list_available_fields`, and `get_field_values`,+;;; so an uncurated Table can't be reached by naming it directly rather than finding it through search (BOT-1649).+;;; A curated entity covers what `read_resource` exposes through it:+;;; - a curated Table or Card covers the Tables one FK hop away (its related tables) as joins: their rows and+;;; columns are exposed through the join, as `read_resource` exposes them through the curated entity, but the+;;; query's own source must be curated itself, or be what a curated metric in the query is defined on;+;;; - a curated metric covers the Table or Card it's defined on (its primary source) and that source's related+;;; tables; the Tables its definition merely joins are reachable as joins only, and widen coverage no further;+;;; - a curated Table or Card covers the metrics defined on it: an uncurated metric may be used when its definition+;;; passes this rule standing alone, which is also how a metric read is judged, so the two sides agree by+;;; construction. The metrics a definition references are judged the same way in turn;+;;; - a measure or segment is usable when its Table, and every Table its definition reads, is curated or covered.++(declare uncurated-query-sources)++(def ^:private max-metric-nesting+ "Deepest chain of metric references the curation check follows. Deeper, or cyclic, references fail closed."+ 10)++(defn- definition-query+ "`definition` (a Card's dataset query, or a measure's or segment's definition, legacy or MBQL 5) as a query against+ `mp`, or nil when it's empty or can't be built, which the callers treat as uncurated."+ [mp definition]+ (when (seq definition)+ (try+ (lib/query mp definition)+ (catch Exception e+ (log/debug e "Curation check can't build a definition query; treating it as uncurated")+ nil))))++(defn- load-metric-definitions!+ "Load into `definitions` (the atom of metric id -> definition query, or nil for a metric whose Card is missing,+ archived, not a metric, or has no usable definition, that a [[metric-context]] shares across one judgement) the+ definitions of `metric-ids` and of the metrics they reference, transitively, `levels` levels deep. Each Card is+ loaded once per judgement: a nested call finds what the top-level walk already loaded, and the walk stops where+ [[max-metric-nesting]] would fail the chain anyway."+ [mp definitions metric-ids levels]+ (loop [todo (set metric-ids)+ levels levels]+ (when (and (seq todo) (pos? levels))+ (let [unloaded (into [] (remove #(contains? @definitions %)) todo)+ archived (if (seq unloaded) (metabot.db/archived-card-ids unloaded) #{})+ loaded (into (zipmap unloaded (repeat nil))+ (map (fn [{:keys [id type dataset-query]}]+ ;; only a live metric Card is judged by its definition; a reference to a Card of+ ;; another type counts as missing, the way [[curated-metric?]] treats it+ [id (when (and (= type :metric) (not (contains? archived id)))+ (definition-query mp dataset-query))]))+ (lib.metadata/bulk-metadata mp :metadata/card unloaded))+ _ (swap! definitions merge loaded)+ nested (into #{}+ (comp (keep @definitions)+ (mapcat #(:metric (lib/all-referenced-entity-ids [%]))))+ todo)]+ (recur (set/difference nested (set (keys @definitions))) (dec levels))))))++(defn- metric-closure+ "Map of metric id -> definition query for `metric-ids` and every metric their definitions reference, as far as+ `definitions` holds them, so curated metrics count toward coverage at any depth. Missing metrics are left out."+ [definitions metric-ids]+ (loop [acc {}+ todo (set metric-ids)]+ (if-let [id (first todo)]+ (let [q (get definitions id)+ nested (when q (:metric (lib/all-referenced-entity-ids [q])))]+ (recur (cond-> acc q (assoc id q))+ (into (disj todo id) (remove #(or (contains? acc %) (= % id))) nested)))+ acc)))++(defn- metric-context+ "State for one judgement of a query's metrics: `:seen`, the metrics being judged up the current reference chain+ (the cycle guard); `:verdicts`, an atom memoizing each metric's verdict by `[id depth]` so a metric reachable+ through several references is judged once per depth; and `:definitions`, the atom [[load-metric-definitions!]]+ fills so each Card is loaded once."+ []+ {:seen #{} :verdicts (atom {}) :definitions (atom {})})++(defn- definition-readable?+ "Whether the current user may query every Table and read every Card the definition query `definition` names. An+ uncurated metric is judged by its definition only when they may: otherwise the verdict would tell them whether a+ Table they cannot see is curated, so the metric fails closed instead."+ [definition]+ (let [{:keys [table card]} (lib/all-referenced-entity-ids [definition])]+ (and (every? #(mi/can-query? :model/Table %) table)+ (every? #(mi/can-read? :model/Card %) card))))++(defn- metric-ok?+ "Whether the metric `id`, whose definition query is `definition` (nil when unusable), may be used: it's curated,+ or its definition is structured, reads only what the current user may ([[definition-readable?]]), and passes+ [[uncurated-query-sources]] on its own. A missing metric, a native definition, a definition over something the+ user cannot read, a reference cycle, or a chain deeper than [[max-metric-nesting]] fails closed. A native+ definition references no tables, so there's nothing to judge it by: like the permissions code, treat it as its own+ thing rather than as covered by default."+ [curated? id definition {:keys [seen verdicts] :as ctx}]+ ;; keyed by depth as well as id: the depth guard is a property of the path, so the same metric may pass when+ ;; referenced directly and fail at the end of a long chain+ (let [k [id (count seen)]]+ (if-some [verdict (get @verdicts k)]+ verdict+ (u/prog1 (boolean+ (or (curated? "card" id)+ (and (some? definition)+ (not (lib/any-native-stage? definition))+ (not (seen id))+ (< (count seen) max-metric-nesting)+ (definition-readable? definition)+ (empty? (uncurated-query-sources definition (update ctx :seen conj id))))))+ (swap! verdicts assoc k <>)))))++(defn- entity-queries+ "Queries over the Tables `table-ids` and the Cards `card-ids` that `mp` can resolve."+ [mp table-ids card-ids]+ (concat (keep #(some->> (lib.metadata/table mp %) (lib/query mp)) table-ids)+ (keep #(some->> (lib.metadata/card mp %) (lib/query mp)) card-ids)))++(defn- query-coverage+ "What the curated content among the `table`s, `card`s, and `curated-metric-queries` a query reads exposes:+ `:covered`, the Tables and Cards reachable through it, and `:metric-sources`, what the curated metrics are+ defined on."+ [mp curated? table card curated-metric-queries]+ (let [metric-tables (into #{} (keep lib/primary-source-table-id) curated-metric-queries)+ metric-cards (into #{} (keep lib/primary-source-card-id) curated-metric-queries)+ ;; a curated table or card, or a curated metric's primary source, covers itself and one FK hop out of it+ primary (entity-queries mp+ (into metric-tables (filter #(curated? "table" %)) table)+ (into metric-cards (filter #(curated? "card" %)) card))+ ;; a curated metric's definition covers the tables it joins, as joins, and nothing beyond them+ joined (if (seq curated-metric-queries)+ (lib/all-referenced-entity-ids (vec curated-metric-queries))+ {})]+ {:metric-sources {:table metric-tables :card metric-cards}+ :covered (merge-with set/union+ (if (seq primary)+ (lib/all-referenced-entity-ids (vec primary) {:include-implicitly-joinable? true})+ {})+ joined)}))++(defn- measure-and-segment-tables+ "Map of `[kind id]` (`:measure` or `:segment`) to the set of Table ids the entity depends on: its own Table and the+ Tables its definition reads. An id `mp` can't resolve, one in the trash, or one whose definition can't be built,+ maps to nil, so the caller fails closed on it."+ [mp measure-ids segment-ids]+ (let [tables-of (fn [{:keys [table-id definition]}]+ (when-let [definition-tables (some->> (definition-query mp definition)+ vector+ lib/all-referenced-entity-ids+ :table)]+ (cond-> (set definition-tables)+ table-id (conj table-id))))+ ;; a by-id lookup returns trashed rows too; one in the trash is judged like one that is missing+ found (fn [kind metadata-type ids]+ (let [by-id (u/index-by :id (remove :archived (lib.metadata/bulk-metadata mp metadata-type ids)))]+ (into {} (map (fn [id] [[kind id] (some-> (get by-id id) tables-of)])) ids)))]+ (merge (found :measure :metadata/measure measure-ids)+ (found :segment :metadata/segment segment-ids))))++(defn- uncurated-sources*+ "[[uncurated-sources]] within the judgement `ctx`, a [[metric-context]]."+ [mp {:keys [source-table source-card table card metric measure segment]} {:keys [seen definitions] :as ctx}]+ (let [;; `:card` also lists the metrics the query references, and a metric Card may be a source Card; judge+ ;; both as metrics, not as source Cards+ card (set/difference (set card) (set metric))+ source-metrics (into #{}+ (comp (filter #(= :metric (:type %))) (map :id))+ (lib.metadata/bulk-metadata mp :metadata/card card))+ metric (into (set metric) source-metrics)+ card (set/difference card source-metrics)+ _ (load-metric-definitions! mp definitions metric (- max-metric-nesting (count seen)))+ id->definition (metric-closure @definitions metric)+ dependents (measure-and-segment-tables mp measure segment)+ ;; `metric` as well as the loaded closure: a curated metric passes on its curation alone, including one+ ;; the walk stopped short of loading+ curated (curated-ids (concat (for [id (into (set table) (mapcat val) dependents)] ["table" id])+ (for [id (concat card metric (keys id->definition))] ["card" id])))+ curated? (fn [model id] (contains? curated [model id]))+ curated-metric-queries (keep (fn [[id q]] (when (curated? "card" id) q)) id->definition)+ {:keys [covered metric-sources]} (query-coverage mp curated? table card curated-metric-queries)+ allowed? (fn [model id] (or (curated? model id) (contains? (get covered (keyword model)) id)))+ source-allowed? (fn [model id] (or (curated? model id) (contains? (get metric-sources (keyword model)) id)))]+ (into []+ cat+ [(for [id table+ :when (not (if (= id source-table) (source-allowed? "table" id) (allowed? "table" id)))]+ ["table" id])+ (for [id card+ :when (not (if (= id source-card) (source-allowed? "card" id) (allowed? "card" id)))]+ ["card" id])+ (for [id metric :when (not (metric-ok? curated? id (id->definition id) ctx))]+ ["card" id])+ (for [[subject tables] dependents+ :when (or (nil? tables) (not (every? #(allowed? "table" %) tables)))]+ subject)])))++(defn uncurated-sources+ "The `[kind id]` pairs (`\"table\"`, `\"card\"`, `:measure`, `:segment`) among `refs` that a curated-only Metabot+ may not read, or empty when a query over them may run. `refs` names what a query reads, resolved against `mp`:+ `:source-table` or `:source-card`, the query's own source, and the sets `:table`, `:card`, `:metric`,+ `:measure`, and `:segment` of everything it references. [[uncurated-query-sources]] applies the same rule to a+ resolved query; this entry point is for a caller that has only ids, so that the two can't disagree."+ [mp refs]+ (uncurated-sources* mp refs (metric-context)))++(defn uncurated-query-sources+ "[[uncurated-sources]] for `query`: its source, joined, and implicitly joined Tables, source Cards, metrics,+ measures, and segments. `ctx` is the [[metric-context]] of the judgement this call is part of."+ ([query]+ (uncurated-query-sources query (metric-context)))+ ([query ctx]+ (uncurated-sources* query+ (assoc (lib/all-referenced-entity-ids [query])+ :source-table (lib/primary-source-table-id query)+ :source-card (lib/primary-source-card-id query))+ ctx)))++(defn covered-table-ids+ "The Tables one FK hop away from the Tables `table-ids` and the result columns of the Cards `card-ids`, plus the+ Tables themselves: what a curated-only Metabot may join when those entities are curated."+ [mp table-ids card-ids]+ (let [queries (entity-queries mp table-ids card-ids)]+ (if (seq queries)+ (:table (lib/all-referenced-entity-ids (vec queries) {:include-implicitly-joinable? true}))+ #{})))++(defn curated-metric?+ "Whether a curated-only Metabot may read the Card with `metric-id` where a metric is expected: the same judgement+ [[uncurated-query-sources]] makes for a metric a query uses (see [[metric-ok?]]). A curated Card of any type+ passes on its own curation; only a live metric Card is judged by its definition, so an uncurated question or+ model named here is denied like anywhere else, and a missing or archived Card is. So is an uncurated metric whose+ definition reads a Table or Card the user cannot ([[definition-readable?]]): judging it would tell them whether+ that Table is curated."+ [metric-id]+ (let [curated? (fn [model id] (contains? (curated-ids [[model id]]) [model id]))+ definition (when-let [{:keys [database_id dataset_query archived type]} (metabot.db/card metric-id)]+ (when (and (= :metric type) (not archived))+ (definition-query (lib-be/application-database-metadata-provider database_id) dataset_query)))]+ (boolean (metric-ok? curated? metric-id definition (metric-context)))))++(defn usable-metric-ids+ "The subset of the metric Card ids `ids` a curated-only Metabot may use: curated ones, and uncurated ones whose+ definition passes the rule (see [[curated-metric?]])."+ [ids]+ (let [curated (curated-ids (map #(vector "card" %) ids))]+ (into #{}+ (filter (fn [id] (or (contains? curated ["card" id]) (curated-metric? id))))+ ids)))++(defn measure-or-segment-curated?+ "Whether a curated-only Metabot may read the measure or segment `id` (`kind` is `:measure` or `:segment`): its+ Table is curated, and every Table its definition reads is curated or one FK hop from its Table, the way+ [[uncurated-query-sources]] judges a measure or segment a query uses. A missing or trashed one, or one whose+ definition can't be built, fails closed."+ [kind id]+ (when-let [table-id (case kind+ :measure (metabot.db/live-measure-table-id id)+ :segment (metabot.db/live-segment-table-id id))]+ (let [mp (lib-be/application-database-metadata-provider (metabot.db/table-database-id table-id))+ tables (get (measure-and-segment-tables mp+ (when (= kind :measure) [id])+ (when (= kind :segment) [id]))+ [kind id])]+ (when (seq tables)+ (let [curated (curated-ids (map #(vector "table" %) tables))+ curated? (fn [tid] (contains? curated ["table" tid]))]+ (boolean+ (and (curated? table-id)+ (let [covered (covered-table-ids mp (filter curated? tables) [])]+ (every? #(or (curated? %) (contains? covered %)) tables)))))))))++(defn subject-curated?+ "Whether `subject`, a `[model id]` pair, is curated: `\"table\"`, `\"card\"`, and `\"dashboard\"` by their own+ signals; `\"metric\"` as [[curated-metric?]] judges a Card named where a metric is expected; `:measure` and+ `:segment` as [[measure-or-segment-curated?]]; anything else (`\"transform\"`) never."+ [[model id :as subject]]+ (case model+ "metric" (curated-metric? id)+ (:measure :segment) (measure-or-segment-curated? model id)+ (contains? (curated-ids [subject]) subject)))++(defn check-curated-subject!+ "Reject `subject` — a `[model id]` pair, see [[subject-curated?]] — unless it's curated, naming it by `label` (a+ URI or a short description such as \"table 12\") rather than by the entity's own name. Callers decide whether+ the session is curated-only. A nil `subject` passes."+ [label subject]+ (when (and subject (not (subject-curated? subject)))+ (recoverable.common/not-curated! {:label label})))++(defn check-curated-readable-subject!+ "[[check-curated-subject!]] for a tool that takes a bare id and has no pre-checks of its own: the judgement runs+ only when the entity exists and the current user can read it, so a missing or unreadable id gets the handler's+ own answer rather than a curation verdict, and an unreadable entity doesn't reveal whether it's curated."+ [label [model id :as subject]]+ (let [row (case model+ "table" (metabot.db/table id)+ ("card" "metric") (metabot.db/card id)+ "dashboard" (metabot.db/dashboard id)+ nil)]+ (when (some-> row mi/can-read?)+ (check-curated-subject! label subject))))++(defn- without-native-refs+ "`query` with the template tags of its native stages removed, in joins too, so that the entity walk sees only what+ the structured stages name."+ [query]+ (lib.walk/walk-stages query+ (fn [_query _path stage]+ (when (= :mbql.stage/native (:lib/type stage))+ (dissoc stage :template-tags)))))++(defn check-curated-query!+ "Reject `query` — a query the conversation holds, as a lib query or a query map with `:database` — unless everything+ it reads is curated or covered (see [[uncurated-query-sources]]), naming it by `label`. A native stage is left to+ the SQL tools' own gates: what it and its template tags name is set aside, and the structured stages around it,+ a notebook joined to SQL or built on a SQL question, are judged like any other. Callers decide whether the session+ is curated-only.++ The rule runs before the query processor checks the user's permissions on the query. A query the user may not+ run is refused here when it reads an uncurated entity, and by the processor otherwise, so the shape of the refusal+ tells a user who already holds the query whether its entities are curated. Accepted: that user put the query in+ the conversation, and the alternative is a second permission walk ahead of the processor's own."+ [label query]+ (let [query (cond-> query+ (not (:lib/metadata query))+ (->> (lib/query (lib-be/application-database-metadata-provider (:database query)))))]+ (when (seq (uncurated-query-sources (without-native-refs query)))+ (recoverable.common/not-curated! {:label label}))))diff --git a/src/metabase/metabot/db.clj b/src/metabase/metabot/db.cljindex 25b1fdcfeec..7945de10154 100644--- a/src/metabase/metabot/db.clj+++ b/src/metabase/metabot/db.clj@@ -516,9 +516,12 @@{:from [(warehouse-schema-overlay/table-query)]}))(mu/defn table-curation-rows- "The ID, published flag, data layer, and data authority of the Tables with `table-ids`."+ "The ID, Database ID, active flag, visibility type, published flag, data layer, and data authority of the Tables with+ `table-ids`."[table-ids :- [:sequential ::lib.schema.id/table]]- (t2/select [:model/Table :id :is_published :data_layer :data_authority] :id [:in table-ids] {:from [(warehouse-schema-overlay/table-query)]}))+ (t2/select [:model/Table :id :db_id :active :visibility_type :is_published :data_layer :data_authority]+ :id [:in table-ids]+ {:from [(warehouse-schema-overlay/table-query)]}))(mu/defn visible-table-summaries"The ID, name, schema, and description of the active, unhidden Tables among `table-ids` in the Database with@@ -1061,15 +1064,20 @@[user-ids :- [:sequential ::lib.schema.id/user]](t2/select-pk->fn identity [:model/User :id :email :first_name :last_name] :id [:in user-ids]))-(mu/defn card-collection-ids- "The ID and Collection ID of the Cards with `ids`."+(mu/defn archived-card-ids+ "The subset of `ids` that are archived Cards."+ [ids :- [:sequential ms/PositiveInt]]+ (t2/select-fn-set :id :model/Card :id [:in ids] :archived true))++(mu/defn card-curation-rows+ "The ID, Collection ID, archived flag, and Document ID of the Cards with `ids`."[ids :- [:sequential ms/PositiveInt]]- (t2/select [:model/Card :id :collection_id] :id [:in ids]))+ (t2/select [:model/Card :id :collection_id :archived :document_id] :id [:in ids]))-(mu/defn dashboard-collection-ids- "The ID and Collection ID of the Dashboards with `ids`."+(mu/defn dashboard-curation-rows+ "The ID, Collection ID, and archived flag of the Dashboards with `ids`."[ids :- [:sequential ms/PositiveInt]]- (t2/select [:model/Dashboard :id :collection_id] :id [:in ids]))+ (t2/select [:model/Dashboard :id :collection_id :archived] :id [:in ids]))(mu/defn measure-table-id"The Table ID of the Measure with `id`, or nil."@@ -1081,6 +1089,16 @@[id :- ::lib.schema.id/segment](t2/select-one-fn :table_id :model/Segment :id id))+(mu/defn live-measure-table-id+ "The Table ID of the Measure with `id`, or nil when it is missing or in the trash."+ [id :- ::lib.schema.id/measure]+ (t2/select-one-fn :table_id :model/Measure :id id :archived false))++(mu/defn live-segment-table-id+ "The Table ID of the Segment with `id`, or nil when it is missing or in the trash."+ [id :- ::lib.schema.id/segment]+ (t2/select-one-fn :table_id :model/Segment :id id :archived false))+(mu/defn measure-entity-id"The entity ID of the Measure with `id`."[id :- ::lib.schema.id/measure]diff --git a/src/metabase/metabot/tools/autogen_dashboard.clj b/src/metabase/metabot/tools/autogen_dashboard.cljindex bcaeaa705d9..a179a6f5b20 100644--- a/src/metabase/metabot/tools/autogen_dashboard.clj+++ b/src/metabase/metabot/tools/autogen_dashboard.clj@@ -7,6 +7,7 @@[metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.metabot.agent.streaming :as streaming]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools]@@ -59,7 +60,8 @@:text "Call `search` to find a table, model, metric or saved question to X-ray instead."}]})(defn- readable-source- "The `kind` entity with `id` and its X-ray path, if the current user can read it and it is still in use."+ "The `kind` entity with `id` and its X-ray path, if the current user can read it, it is still in use, and it+ is curated when the Metabot is curated-only."[kind id](let [entity (tools/with-entity {:kind kind :id id}(api/read-check (if (= :table kind)@@ -67,6 +69,9 @@(metabot.db/card-of-type id kind))))](when (or (:archived entity) (false? (:active entity)))(unavailable-source! {:kind kind :id id}))+ (when (shared/curated-only?)+ (curation/check-curated-subject! (str (name kind) " " id)+ [(case kind :table "table" :metric "metric" "card") id])){:entity entity;; X-ray has no page for metrics, but its question page accepts any card type:path (str "/auto/dashboard/" (name (if (= :metric kind) :question kind)) "/" id)}))@@ -121,6 +126,9 @@(xrays/adhoc-query-read-check {:dataset_query query}))](when (lib/native? mbql-query)(unsupported-native-query! {:query-id query_id}))+ ;; a query the conversation holds may come from the user's viewing context, which nothing judged+ (when (shared/curated-only?)+ (curation/check-curated-query! (str "query " query_id) query)){:entity entity:path (str "/auto/dashboard/adhoc/" (xrays/encode-base64-json query))})))))diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.cljindex b88e9a96d9e..3438a204e40 100644--- a/src/metabase/metabot/tools/construct.clj+++ b/src/metabase/metabot/tools/construct.clj@@ -18,6 +18,7 @@[metabase.lib.walk :as lib.walk][metabase.metabot.agent.links :as links][metabase.metabot.agent.streaming :as streaming]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.metadata-perms :as metabot.perms][metabase.metabot.scope :as scope]@@ -541,6 +542,95 @@table-fks))already-checked)))+(defn- uncurated-source!+ "Reject the query: a curated-only Metabot may not read `uncurated`, the `[kind id]` pairs it names."+ [uncurated]+ (throw (ex-info (str "This Metabot only uses curated content (verified, official, or Library content), "+ "and the query reads a table, model, or metric that is not curated.")+ {:agent-error? true+ :status-code 400+ :error :uncurated-source+ :uncurated (vec uncurated)})))++(defn- portable-metric-refs+ "The ids the metric references in `parsed-query` carry: `[\"metric\" opts id]` or `[\"metric\" id]`, with `id` an+ entity id or, on the numeric-id surface, a card id."+ [parsed-query]+ (into []+ (comp (filter #(and (vector? %) (not (map-entry? %)) (= "metric" (nth % 0 nil))))+ (keep #(let [id (if (map? (nth % 1 nil)) (nth % 2 nil) (nth % 1 nil))]+ (when (or (string? id) (pos-int? id)) id)))+ (distinct))+ (tree-seq coll? seq parsed-query)))++(defn- source-card-refs+ "Every `source-card:` in `parsed-query`, first stage, later stages, and joins: entity ids, or card ids on the+ numeric-id surface."+ [parsed-query]+ (into []+ (comp (filter map?)+ (keep #(get % "source-card"))+ (filter #(or (string? %) (pos-int? %)))+ (distinct))+ (tree-seq coll? seq parsed-query)))++(defn- readable-card-by-ref+ "The Card `ref` names, when it exists and the current user can read it; nil otherwise, which leaves a missing or+ unreadable Card to resolve's own answer."+ [ref]+ (when-let [card (cond+ (string? ref) (metabot.db/card-by-entity-id ref)+ serdes.resolve/*numeric-ids-allowed?* (metabot.db/card ref))]+ (when (mi/can-read? card)+ card)))++(defn- check-curated-portable-sources!+ "Under [[shared/curated-only?]], before repair reads the query: reject it when its first-stage source, a Card it+ reads, or a table it references is neither curated nor reachable from curated content it references. Repair's+ errors name the columns of the tables and cards they touch, so an uncurated source has to be refused before it+ runs. This is the coarse gate: it judges the ids the portable query names by the rule of+ [[curation/uncurated-sources]], the tables every field reference names included and measures and segments+ excluded; [[check-curated-query-sources!]] judges the resolved query by the same rule. `table-ids` are the tables+ [[check-source-table-query-permissions!]] resolved."+ [metadata-provider parsed-query table-ids]+ (when (shared/curated-only?)+ (let [cards (into [] (keep readable-card-by-ref) (source-card-refs parsed-query))+ metric-ids (into #{}+ (comp (keep readable-card-by-ref) (map :id))+ (portable-metric-refs parsed-query))+ ;; a metric Card used as a source is judged as a metric, as the full rule judges it+ metric-ids (into metric-ids (comp (filter #(= :metric (:type %))) (map :id)) cards)+ card-ids (into #{} (comp (remove #(= :metric (:type %))) (map :id)) cards)+ source-card (some-> (or (first-stage-source-card-eid parsed-query)+ (when serdes.resolve/*numeric-ids-allowed?*+ (first-stage-numeric-source-card parsed-query)))+ readable-card-by-ref+ :id)+ source-table (when-let [fk (or (first-stage-source-table-fk parsed-query)+ (when serdes.resolve/*numeric-ids-allowed?*+ (first-stage-numeric-source-table parsed-query)))]+ (resolve-table-fk metadata-provider+ (resolve.mp/import-resolver metadata-provider+ permission-aware-content-store)+ fk))+ uncurated (curation/uncurated-sources metadata-provider+ {:source-table source-table+ :source-card source-card+ :table (set table-ids)+ :card card-ids+ :metric metric-ids})]+ (when (seq uncurated)+ (uncurated-source! uncurated)))))++(defn- check-curated-query-sources!+ "Under [[shared/curated-only?]], reject `pmbql-query` unless everything it reads is curated, or covered by curated+ content it reads (see [[curation/uncurated-query-sources]] for the rule)."+ [metadata-provider pmbql-query]+ (when (shared/curated-only?)+ (let [uncurated (curation/uncurated-query-sources (lib/query metadata-provider pmbql-query))]+ (when (seq uncurated)+ (uncurated-source! uncurated)))))+(defn resolve-database-id-from-first-stage"Resolve the application database id from the first stage's source.@@ -1038,7 +1128,8 @@_refs (assert-numeric-ids-are-referenceable! mp parsed);; Permission checks happen before repair/resolve so the metadata-provider-backed pipeline;; never inspects table/card metadata that the current user cannot use.- checked (check-source-table-query-permissions! mp parsed)]+ checked (check-source-table-query-permissions! mp parsed)+ _curated (check-curated-portable-sources! mp parsed checked)];; Relay query-input errors while preserving database and permission failures.(try(let [;; Repair's mini-resolve lookups are best-effort - a failure is logged at debug and the@@ -1049,6 +1140,7 @@_perms (check-source-table-query-permissions! mp repaired checked)_validated (repr/validate-query repaired)pmbql-query (repr.resolve/resolve-query mp repaired permission-aware-content-store)+ _curated (check-curated-query-sources! mp pmbql-query)_runnable (when-let [why (query-not-runnable-explanation pmbql-query repaired)](throw (ex-info (format "The constructed query is not runnable - it would fail the query builder's validation, so it cannot be visualized or saved. Failing paths: %s."why)diff --git a/src/metabase/metabot/tools/create_alert.clj b/src/metabase/metabot/tools/create_alert.cljindex c5a1f1fbb2d..0d95bef52ca 100644--- a/src/metabase/metabot/tools/create_alert.clj+++ b/src/metabase/metabot/tools/create_alert.clj@@ -5,9 +5,11 @@(:require[metabase.api.common :as api][metabase.channel.settings :as channel.settings]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.util :as metabot.tools.u][metabase.notification.api :as notification.api][metabase.util.cron :as u.cron]@@ -141,6 +143,8 @@(let [channel (current-slack-channel ctx)card (tools/with-entity {:kind :card :id card_id}(api/read-check (metabot.db/card card_id)))+ _ (when (shared/curated-only?)+ (curation/check-curated-subject! (str "card " card_id) ["card" card_id]))alert (create-alert! {:card-id card_id:channel channel:schedule schedulediff --git a/src/metabase/metabot/tools/entity_details.clj b/src/metabase/metabot/tools/entity_details.cljindex 86aa8ff3f77..81cea1c54d0 100644--- a/src/metabase/metabot/tools/entity_details.clj+++ b/src/metabase/metabot/tools/entity_details.clj@@ -10,6 +10,7 @@[metabase.lib.types.isa :as lib.types.isa][metabase.metabot.db :as metabot.db][metabase.metabot.metadata-perms :as metabot.perms]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.shared.content-store :as shared.content-store][metabase.metabot.tools.util :as metabot.tools.u][metabase.models.interface :as mi]@@ -398,7 +399,9 @@;; Definition query (WITH the metric's own joins) — surfaces FK-less join dimensions that;; the consumer-framed `base-query` drops. Built from the card's `:dataset-query`, not from;; `metric-query`, which frames the metric as a source and hides its definition joins.- join-required-dims (when (and query-needed? with-queryable-dimensions?)+ ;; Only a metric's definition joins are dimensions to offer; a question or model read through the+ ;; metric path exposes its results, as through its own URI, not the tables its definition reads.+ join-required-dims (when (and query-needed? with-queryable-dimensions? (= :metric (:type metric-card)))(metric-join-required-dimensionsmetadata-provider(some->> (:dataset-query metric-card) (lib/query metadata-provider))@@ -422,7 +425,9 @@;; `[metric, {}, <entity_id>]` aggregation clause to reference this metric.:portable_entity_id (metabot.tools.u/metric-card-shape-get card :entity-id :entity_id):verified (verified-review? id "card")}- true+ ;; Under curated-only, only a metric names what it reads: a question or model read through this path+ ;; exposes its results, as through its own URI, not the table or card its definition reads.+ (or (not (shared/curated-only?)) (= :metric (:type metric-card)))(merge (metabot.tools.u/metric-source-fields{:source-card (when source-card{:id (:id source-card)diff --git a/src/metabase/metabot/tools/explorations.clj b/src/metabase/metabot/tools/explorations.cljindex 7ba639baa7c..b7f1294395c 100644--- a/src/metabase/metabot/tools/explorations.clj+++ b/src/metabase/metabot/tools/explorations.clj@@ -13,9 +13,11 @@[clojure.string :as str][metabase.explorations.core :as explorations][metabase.metabot.agent.streaming :as streaming]+ [metabase.metabot.curation :as curation][metabase.metabot.scope :as scope][metabase.metabot.tmpl :as te][metabase.metabot.tools.core :as tools]+ [metabase.metabot.tools.shared :as shared][metabase.timeline.core :as timeline][metabase.util.json :as json]))@@ -71,6 +73,42 @@[:map {:closed true}[:q {:optional true} [:maybe :string]]])+(defn- curated-only-index+ "Under [[shared/curated-only?]], keep only the metrics of the research `index` this Metabot may use (see+ [[curation/usable-metric-ids]]). The truncation stamp is left as the catalog computed it."+ [index]+ (if (shared/curated-only?)+ (let [usable (curation/usable-metric-ids (map :id (:metrics index)))]+ (update index :metrics (fn [rows] (filterv #(contains? usable (:id %)) rows))))+ index))++(defn- check-curated-metrics!+ "Under [[shared/curated-only?]], reject the first of `metric-ids` this Metabot may not use. A missing or unreadable+ id is left to the catalog, which reports it as missing."+ [metric-ids]+ (when (shared/curated-only?)+ (doseq [id metric-ids]+ (curation/check-curated-readable-subject! (str "metric " id) ["metric" id]))))++(defn- curated-only-candidates+ "Under [[shared/curated-only?]], keep only the metrics of the research `candidates` this Metabot may use, and the+ dimension groups that still slice one of them. A `q` search reaches every readable metric the catalog matches,+ so the result is filtered the way [[curated-only-index]] filters the index."+ [candidates]+ (if (shared/curated-only?)+ (let [usable (curation/usable-metric-ids (map :id (:metrics candidates)))]+ (-> candidates+ (update :metrics (fn [rows] (filterv #(contains? usable (:id %)) rows)))+ (update :dimension_groups+ (fn [groups]+ (into []+ (keep (fn [group]+ (let [ids (filterv usable (:metric_ids group))]+ (when (seq ids)+ (assoc group :metric_ids ids)))))+ groups)))))+ candidates))+(defrecord ListResearchMetricsTool []tools/Tool(declaration [_]@@ -88,7 +126,7 @@:args list-research-metrics-schema})(handle [_ {:keys [q]} _ctx]- {:output (json/encode (explorations/research-metric-index {:q q}))}))+ {:output (json/encode (curated-only-index (explorations/research-metric-index {:q q})))}))(def list-research-metrics-tool"The `list_research_metrics` tool."@@ -133,7 +171,9 @@(handle [_ {:keys [q metric_ids]} _ctx](when (and (str/blank? q) (empty? metric_ids))(needs-metric-ids-or-q! {}))- {:output (json/encode (explorations/research-candidates {:metric-ids metric_ids :q q}))}))+ (check-curated-metrics! metric_ids)+ {:output (json/encode (curated-only-candidates+ (explorations/research-candidates {:metric-ids metric_ids :q q})))}))(def get-research-candidates-tool"The `get_research_candidates` tool."@@ -346,6 +386,7 @@:args add-research-groups-schema})(handle [_ {:keys [groups]} _ctx]+ (check-curated-metrics! (map :metric_id groups))(let [payload (research-groups-payload groups)]{:output (format-added-groups payload):data-parts [(streaming/research-plan-update-part payload)]})))diff --git a/src/metabase/metabot/tools/field_stats.clj b/src/metabase/metabot/tools/field_stats.cljindex 823dddbde36..e39cc657527 100644--- a/src/metabase/metabot/tools/field_stats.clj+++ b/src/metabase/metabot/tools/field_stats.clj@@ -7,6 +7,8 @@[metabase.metabot.metadata-perms :as metabot.perms][metabase.metabot.tools.core :as tools][metabase.metabot.tools.entity-details :as entity-details]+ [metabase.metabot.tools.recoverable.common :as recoverable.common]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.util :as metabot.tools.u][metabase.parameters.field-values :as params.field-values][metabase.permissions.core :as perms]@@ -134,6 +136,18 @@:available-ids (vec (take max-suggested-field-ids available-ids)):remaining-count (max 0 (- (count available-ids) max-suggested-field-ids))})))))+(defn- check-curated-field-owner!+ "Under [[shared/curated-only?]], require that the Field behind `col`, a result column of the card `card-id`, belongs+ to a Table the card's definition reads or one FK hop from it. A card's result metadata can map a column to any+ Field, so without this a curated model could serve, and fingerprint, a column of a table it never reads."+ [card-id col]+ (when (shared/curated-only?)+ (let [field-id (:id col)+ owner (when (pos-int? field-id)+ (get (metabot.perms/field-id->table-id #{field-id}) field-id))]+ (when (and owner (not (contains? (metabot.tools.u/card-definition-table-ids card-id) owner)))+ (recoverable.common/not-curated! {:label (str "field " field-id)})))))+(defn- table-field-stats[table-id field-id limit](let [query (metabot.tools.u/table-query table-id)@@ -142,17 +156,25 @@(defn- card-field-stats[entity-type card-id field-id limit]- (let [query (metabot.tools.u/card-query card-id (keyword entity-type))+ (let [;; A curated-only read may see what a curated card exposes, its result columns and their FK neighbours,+ ;; which is what `construct_notebook_query` lets a curated card cover (see+ ;; `metabase.metabot.curation`). The definition of a question reads more: a verified `count` over a raw+ ;; table would otherwise serve every column of that table.+ query (if (shared/curated-only?)+ (metabot.tools.u/card-results-query card-id (keyword entity-type))+ (metabot.tools.u/card-query card-id (keyword entity-type)))columns (if (= entity-type "question")(lib/returned-columns query)(distinct (concat (lib/returned-columns query) (lib/visible-columns query))))- col (field-column entity-type card-id field-id columns)]+ col (field-column entity-type card-id field-id columns)]+ (check-curated-field-owner! card-id col)(field-metadata-output query col field-id limit)))(defn- metric-field-stats[metric-id field-id limit](let [query (metabot.tools.u/metric-query metric-id)col (field-column "metric" metric-id field-id (lib/filterable-columns query))]+ (check-curated-field-owner! metric-id col)(field-metadata-output query col field-id limit)))(defn field-valuesdiff --git a/src/metabase/metabot/tools/metadata.clj b/src/metabase/metabot/tools/metadata.cljindex bbb83d0f82f..8c1d93bb886 100644--- a/src/metabase/metabot/tools/metadata.clj+++ b/src/metabase/metabot/tools/metadata.clj@@ -3,12 +3,14 @@(:require[clojure.string :as str][medley.core :as m]+ [metabase.metabot.curation :as curation][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools][metabase.metabot.tools.entity-details :as entity-details-tools][metabase.metabot.tools.field-stats :as field-stats-tools][metabase.metabot.tools.recoverable.common :as recoverable.common][metabase.metabot.tools.resources :as resources-tools]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.shared.instructions :as instructions][metabase.metabot.tools.shared.llm-shape :as llm-shape][metabase.metabot.tools.util :as tools.u]@@ -42,27 +44,38 @@(defn- source-metadata[{:keys [entity-type entity-id]}](if (= entity-type :table)- (resources-tools/check-table-resource-database entity-id)+ (do+ (resources-tools/check-table-resource-database entity-id)+ ;; an inactive table is missing, as the details treat it, not uncurated+ (tools.u/get-table entity-id :db_id))(do(resources-tools/check-card-resource-database entity-id)(tools.u/get-card entity-id entity-type)))- (if (= entity-type :metric)- (entity-details-tools/get-metric-details- {:metric-id entity-id- :with-default-temporal-breakout? false- :with-field-values? false- :with-queryable-dimensions? false- :with-segments? true})- (entity-details-tools/get-table-details- {:entity-type entity-type- :entity-id entity-id- :with-fields? true- :with-field-values? false- :with-related-tables? false- :with-metrics? false- :with-default-temporal-breakout? false- :with-measures? true- :with-segments? true})))+ (when (shared/curated-only?)+ (curation/check-curated-subject! (str (name entity-type) " " entity-id)+ (case entity-type+ :table ["table" entity-id]+ :model ["card" entity-id]+ :metric ["metric" entity-id])))+ (cond-> (if (= entity-type :metric)+ (entity-details-tools/get-metric-details+ {:metric-id entity-id+ :with-default-temporal-breakout? false+ :with-field-values? false+ :with-queryable-dimensions? false+ :with-segments? true})+ (entity-details-tools/get-table-details+ {:entity-type entity-type+ :entity-id entity-id+ :with-fields? true+ :with-field-values? false+ :with-related-tables? false+ :with-metrics? false+ :with-default-temporal-breakout? false+ :with-measures? true+ :with-segments? true}))+ ;; under curated-only a card's details omit its definition, as read_resource's do+ (shared/curated-only?) (update :structured-output dissoc :query_json)))(defn- metadata-fault-entry[{:keys [entity-type entity-id] :as item} e]@@ -163,10 +176,17 @@(when-not (:id metabot)(unknown-metabot! {}))(add-output- (entity-details-tools/answer-sources {:metabot metabot- :with-field-values? false- :with-measures? true- :with-segments? true})+ (cond-> (entity-details-tools/answer-sources {:metabot metabot+ :with-field-values? false+ :with-measures? true+ :with-segments? true})+ ;; under curated-only a card's details omit its definition, as read_resource's do+ (shared/curated-only?)+ (update :structured-output+ (fn [so]+ (-> so+ (update :metrics (fn [rows] (mapv #(dissoc % :query_json) rows)))+ (update :models (fn [rows] (mapv #(dissoc % :query_json) rows)))))))format-answer-sources-output)))(def list-available-data-sources-tool@@ -205,8 +225,17 @@(handle [_ {data-source :data_source source-id :source_id field-id :field_id} _ctx](tools/with-entity {:kind (if (= data-source "report") :question (keyword data-source)) :id source-id}(if (= data-source "table")- (resources-tools/check-table-resource-database source-id)+ (do+ (resources-tools/check-table-resource-database source-id)+ ;; an inactive table is missing, as `field-values` treats it, not uncurated+ (tools.u/get-table source-id :db_id))(resources-tools/check-card-resource-database source-id)))+ (when (shared/curated-only?)+ (curation/check-curated-subject! (str data-source " " source-id)+ (case data-source+ "table" ["table" source-id]+ "metric" ["metric" source-id]+ ("model" "question" "report") ["card" source-id])))(tools/with-entity {:kind :field :id field-id}(add-output (field-stats-tools/field-values {:entity-type data-source:entity-id source-iddiff --git a/src/metabase/metabot/tools/recoverable/common.clj b/src/metabase/metabot/tools/recoverable/common.cljindex 924c45396ee..4c353a01678 100644--- a/src/metabase/metabot/tools/recoverable/common.clj+++ b/src/metabase/metabot/tools/recoverable/common.clj@@ -160,3 +160,17 @@:text "To make a new chart, call `construct_notebook_query` and use the chart ID that it returns."}{:uses #{"create_chart"}:text "To chart a query from this conversation, call `create_chart` with its query ID."}]}))++(defrecoverable not-curated!+ "The entity exists and the user can read it, but this Metabot only uses curated content and the entity isn't+ curated. Raised by `read_resource`, `list_available_fields`, `get_field_values`, `run_query`, and the tools that+ take an entity id, so one sentence covers every direct read."+ {:payload [:map {:closed true}+ ;; a URI or a short description such as "table 12": the entity is named by the handle the agent used,+ ;; never by its own name+ [:label :string]]}+ [{:keys [label]}]+ {:message (str "`" label "` is not available: this Metabot only uses curated content (verified, official, or "+ "Library content).")+ :recovery [{:uses #{"search"}+ :text "Call `search` to find curated tables, models, or metrics and use one of those instead."}]})diff --git a/src/metabase/metabot/tools/recoverable/pipeline.clj b/src/metabase/metabot/tools/recoverable/pipeline.cljindex 4925c580884..74337d35279 100644--- a/src/metabase/metabot/tools/recoverable/pipeline.clj+++ b/src/metabase/metabot/tools/recoverable/pipeline.clj@@ -182,6 +182,14 @@{:message (:message payload#):recovery ~recovery}))+;;; Curation++(defpipeline-error uncurated-source!+ "The query reads a table, model, metric, measure, or segment that a curated-only Metabot may not use."+ [{:uses #{"search"}+ :text (str "Call `search` to find curated tables, models, or metrics and build the query on those instead. "+ "A table related to a curated one may be joined, but not used as the query's source.")}])+;;; Table resolution(defrecoverable unknown-table!diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.cljindex 9334204284d..e6f7097c818 100644--- a/src/metabase/metabot/tools/resources.clj+++ b/src/metabase/metabot/tools/resources.clj@@ -72,6 +72,7 @@[metabase.documents.core :as documents][metabase.documents.prose-mirror :as prose-mirror][metabase.metabot.agent.streaming :as streaming]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.query-export :as query-export][metabase.metabot.scope :as scope]@@ -330,13 +331,46 @@:page page:pages pages}))+(def ^:private item-type->curation-model+ "List-item `:type` -> the model [[curation/curated-ids]] judges it as, for data-bearing items that carry a+ curation signal."+ {"table" "table"+ "model" "card"+ "question" "card"+ "metric" "card"+ "dashboard" "dashboard"})++(def ^:private uncuratable-item-types+ "Data-bearing list-item types that can never be curated, so they're hidden under [[shared/curated-only?]]."+ #{"transform"})++(defn- curation-key+ "The `[curation-model id]` pair [[curation/curated-ids]] judges `item` by, or nil for items without a curation+ signal."+ [{:keys [type id]}]+ (when-let [model (item-type->curation-model type)]+ [model id]))++(defn- curated-items+ "Keep the `items` a curated-only read may show: curated tables/cards/dashboards, plus navigation items (databases,+ schemas, collections, documents, dashcards without a card, ...) that carry no data of their own."+ [items]+ (let [curated (curation/curated-ids (keep curation-key items))]+ (filterv (fn [item]+ (if-let [k (curation-key item)]+ (contains? curated k)+ (not (uncuratable-item-types (:type item)))))+ items)))+(defn- list-result"Build a structured-output map for a list of items.`list-type` is a keyword like :databases, :collection-items, :recents, etc.- `query-params` is the parsed URI query-param map; `:page` selects the page (1-indexed string)."+ `query-params` is the parsed URI query-param map; `:page` selects the page (1-indexed string).+ Under [[shared/curated-only?]], uncurated items are dropped before paginating so totals stay accurate."([list-type items] (list-result list-type items nil))([list-type items query-params]- (let [{:keys [items total page pages]} (paginate-list items (:page query-params))]+ (let [items (cond-> items (shared/curated-only?) curated-items)+ {:keys [items total page pages]} (paginate-list items (:page query-params))]{:structured-output{:result-type :metabot-list:list-type list-type@@ -567,14 +601,17 @@(let [recents (or (-> (activity-feed/get-recents api/*current-user-id* [:views]):recents)[])- items (mapv (fn [{:keys [id name model timestamp]}]- (let [type (case model- :card "question"- :dataset "model"- (clojure.core/name (or model :item)))]+ items (mapv (fn [{:keys [id model timestamp] item-name :name}]+ ;; recents carry keyword models (`:card`, `:dataset`, ...); list items carry the public+ ;; string type, which [[curated-items]] keys on+ (let [type (case (some-> model name)+ "card" "question"+ "dataset" "model"+ nil "item"+ (name model))]{:type type:id id- :name name+ :name item-name:timestamp timestamp:uri (llm-shape/metabase-uri (keyword type) id)}))recents)]@@ -603,12 +640,24 @@(mapv present-card))](list-result :database-models models query-params)))+(defn- curated-table-schemas+ "The schemas of the Database with `db-id` that hold at least one curated active Table the current user can read.+ Readable first, then curated, so the listing reveals nothing about a Table the user cannot read."+ [db-id]+ (let [_ (perms/prime-table-perms-cache {:db-ids #{db-id}})+ tables (filter mi/can-read? (metabot.db/active-tables-for-database db-id))+ curated (curation/curated-ids (map (fn [{:keys [id]}] ["table" id]) tables))]+ (into #{}+ (comp (filter (fn [{:keys [id]}] (contains? curated ["table" id])))+ (keep :schema))+ tables)))+(defn- fetch-database-schemas [id-str query-params](let [db-id (parse-long id-str)_ (warehouses/get-database db-id)rows (metabot.db/active-schemas-for-database db-id)- schemas (->> rows- (keep :schema)+ schemas (->> (cond->> (keep :schema rows)+ (shared/curated-only?) (filter (curated-table-schemas db-id)))(mapv (fn [s]{:type "schema":name s@@ -720,6 +769,26 @@:uri (apply llm-shape/metabase-uri actual id path-segments)})))card))+(defn- usable-metrics+ "The `metrics` embedded in a table's or card's details that a `metric/{id}` read would serve (see+ [[curation/usable-metric-ids]]). Only metrics the current user can read are judged, so the listing reveals nothing+ about the curation of one they cannot."+ [metrics]+ (let [readable (filterv (fn [{:keys [id]}] (mi/can-read? :model/Card id)) metrics)+ usable (curation/usable-metric-ids (map :id readable))]+ (filterv (fn [{:keys [id]}] (contains? usable id)) readable)))++(defn- curated-only-details+ "Under [[shared/curated-only?]], trim a table's or card's details `result` to what this Metabot may use: the+ embedded metrics `metric/{id}` would deny are dropped, and a card's exported definition (`:query_json`) is+ withheld, since it names the tables the definition reads rather than the result columns the card covers."+ [result]+ (cond-> result+ (and (shared/curated-only?) (seq (get-in result [:structured-output :metrics])))+ (update-in [:structured-output :metrics] usable-metrics)+ (shared/curated-only?)+ (update :structured-output dissoc :query_json)))+(defn- table-details"Shared `entity-details/get-table-details` call for both /table/{id} and /table/{id}/fields.`entity-type` is :table, :model, or :question."@@ -727,13 +796,14 @@(case entity-type:table (active-table-resource id)(:model :question) (apply check-card-kind id entity-type (when with-fields? ["fields"])))- (entity-details/get-table-details {:entity-type entity-type- :entity-id id- :with-fields? with-fields?- :with-field-values? false- :with-related-tables? (= entity-type :table)- :with-measures? true- :with-segments? true}))+ (curated-only-details+ (entity-details/get-table-details {:entity-type entity-type+ :entity-id id+ :with-fields? with-fields?+ :with-field-values? false+ :with-related-tables? (= entity-type :table)+ :with-measures? true+ :with-segments? true})))(defn- fetch-table [id-str](table-details :table (parse-long id-str) false))@@ -794,9 +864,21 @@:field-id field-id:limit 30})))+(defn- readable-source-item?+ "Whether the current user can read the table or card a `card-sources-items` entry names."+ [{:keys [type id]}]+ (case type+ "table" (mi/can-read? :model/Table id)+ ("model" "metric" "question") (mi/can-read? :model/Card id)+ true))+(defn- fetch-card-sources [type-str id-str query-params](let [card (check-card-kind (parse-long id-str) (keyword type-str) "sources")]- (list-result :card-sources (card-sources-items card) query-params)))+ ;; under curated-only the list is filtered by curation, so what the user cannot read is dropped first+ (list-result :card-sources+ (cond->> (card-sources-items card)+ (shared/curated-only?) (filterv readable-source-item?))+ query-params)));; ----- Metric -----@@ -936,6 +1018,13 @@(->> (metabot.db/unarchived-card-summaries card-ids)(filter mi/can-read?)(into {} (map (juxt :id identity)))))+ ;; An action button's uri points at its backing model, which [[list-result]]'s curation filter can't see+ ;; through, so under [[shared/curated-only?]] only curated models keep their uri.+ linkable (if (shared/curated-only?)+ (let [action-card-ids (into [] (keep #(when (:action_id %) (:card_id %))) dashcards)]+ (select-keys readable (map second (curation/curated-ids (map #(vector "card" %)+ action-card-ids)))))+ readable)->item (fn [{:keys [id card_id action_id] :as dashcard}];; action_id wins over card_id: an action button may reference its backing;; model through card_id but renders as a button — with a uri to that model@@ -944,7 +1033,7 @@(when-let [card (get readable card_id)](assoc (present-card card) :dashcard_id id))(cond-> (present-non-question-dashcard dashcard)- (get readable card_id) (assoc :uri (:uri (present-card (get readable card_id)))))))+ (get linkable card_id) (assoc :uri (:uri (present-card (get linkable card_id)))))))items (if (seq tabs);; group by tab without emitting tab pseudo-items — those would inflate the;; paginated total; the tab list rides on the response as `:tabs` instead.@@ -1100,6 +1189,45 @@(and (= type-seg "collection") (= id-seg "root")) (assoc :id "root")){}))+(defn- curation-subject+ "What a single-entity URI must be curated as under [[shared/curated-only?]]: a `[model id]` pair — `\"table\"`,+ `\"card\"`, `\"dashboard\"`, `\"metric\"` (judged by [[curation/curated-metric?]]), `:measure` and `:segment`+ (judged by [[curation/measure-or-segment-curated?]]: their Table, and the Tables their definition reads), or+ `\"transform\"` (never curated) — or nil when the URI isn't gated: navigation, collections, documents,+ conversation state, and `table/{id}/derived` (whose listing is filtered instead), plus measures and segments+ that don't exist (left to their handler's 404)."+ [[type-seg id-seg aspect]]+ (let [id (some-> id-seg parse-long)]+ (when (and id (pos? id))+ (case type-seg+ "table" (when-not (= aspect "derived") ["table" id])+ ("model" "question") ["card" id]+ "metric" ["metric" id]+ "dashboard" ["dashboard" id]+ "measure" (when (metabot.db/measure-table-id id) [:measure id])+ "segment" (when (metabot.db/segment-table-id id) [:segment id])+ "transform" ["transform" id]+ nil))))++(defn- check-curated-uri!+ "Under [[shared/curated-only?]], reject a URI naming an entity that isn't curated, without naming the entity. Runs+ before the handler, so a denied read does none of its work: `table/{id}/fields/{field}` would otherwise compute+ and persist field values and a fingerprint for a table this Metabot may not use. The handler's own existence,+ read, and resource-database checks run first, so a missing or routing-destination entity still reads as missing+ and an inactive table as inactive, rather than uncurated, and whether an unreadable one is curated isn't+ revealed."+ [uri segments]+ (when (shared/curated-only?)+ (when-let [[model id :as subject] (curation-subject segments)]+ (case model+ "table" (active-table-resource id)+ ("card" "metric") (check-card-resource-database id)+ "dashboard" (api/read-check :model/Dashboard id)+ :measure (check-measure-or-segment-resource-database :model/Measure id)+ :segment (check-measure-or-segment-resource-database :model/Segment id)+ "transform" (transforms/get-transform id))+ (curation/check-curated-subject! uri subject))))+(defn- dispatch"Route a parsed URI to the right fetch handler. The match-one table is the canonicallist of supported URI shapes — adding a new URI = adding a clause here + a handler.@@ -1115,6 +1243,7 @@(let [{:keys [segments query-params]} (parse-uri uri)](check-numeric-id-segment! uri segments)(->> (tools/with-entity (uri-entity segments)+ (check-curated-uri! uri segments)(match/match-one segments;; Navigation["databases"] (fetch-databases-list query-params)diff --git a/src/metabase/metabot/tools/run_query.clj b/src/metabase/metabot/tools/run_query.cljindex faf30a8a62b..18cd84001c3 100644--- a/src/metabase/metabot/tools/run_query.clj+++ b/src/metabase/metabot/tools/run_query.clj@@ -9,6 +9,7 @@[metabase.lib-be.core :as lib-be][metabase.lib.core :as lib][metabase.metabot.config :as metabot.config]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.query-execution :as query-execution][metabase.metabot.scope :as scope]@@ -252,6 +253,10 @@(when (some-> card mi/can-read?)(when (= :metric (:type card))(metric-not-runnable! {:query-id query-id, :source (metric-source card)}))+ ;; The rows go to the model: a curated-only Metabot runs a saved question only when `read_resource` would+ ;; read it.+ (when (shared/curated-only?)+ (curation/check-curated-subject! (str "question " card-id) ["card" card-id])){:saved-card card:query {:lib/type :mbql/query:database (:database_id card)@@ -697,6 +702,12 @@(check-private-conversation!)(let [{:keys [query saved-card]} (stored-query query_id allow-non-sql?)runnable (runnable-query query_id query allow-non-sql?)]+ ;; A query the conversation holds came from construct_notebook_query, which judged it, or from the user's+ ;; viewing context, which nothing judged: the rows go to the model either way. A saved question was judged+ ;; in [[saved-question]]. SQL the query holds itself is left to the SQL gates above; its structured stages,+ ;; and a notebook query over a SQL question Metabot saved, are judged like any other.+ (when (and (shared/curated-only?) (nil? saved-card))+ (curation/check-curated-query! (str "query " query_id) (:query runnable)))(when-not (or allow-non-sql? (:checked-sql runnable))(not-sql! {:query-id query_id, :hint (recovery-hint allow-non-sql?)}))(let [page (execute-page! query_id runnable (or row_limit default-row-limit)diff --git a/src/metabase/metabot/tools/shared.clj b/src/metabase/metabot/tools/shared.cljindex 0d036cc3c72..d5bb9c51381 100644--- a/src/metabase/metabot/tools/shared.clj+++ b/src/metabase/metabot/tools/shared.clj@@ -1,6 +1,7 @@(ns metabase.metabot.tools.shared"Shared tool helpers and state accessors."(:require+ [metabase.metabot.curation :as curation][metabase.util.log :as log]))(set! *warn-on-reflection* true)@@ -30,6 +31,13 @@agent loop so that tools can adapt their output to the active profile."nil)+(defn curated-only?+ "Whether the current session's tools may only reach curated content: its Metabot has `use_verified_content` on and+ its profile isn't exempt (see [[curation/curated-only?]]). False outside an agent session, where no Metabot is+ bound."+ []+ (curation/curated-only? *metabot* *profile-id*))+(defn current-memory"Returns the current agent memory map, or nil if not in an agent context."[]diff --git a/src/metabase/metabot/tools/static_viz.clj b/src/metabase/metabot/tools/static_viz.cljindex e0047b4fbb8..6ebc5c2f78c 100644--- a/src/metabase/metabot/tools/static_viz.clj+++ b/src/metabase/metabot/tools/static_viz.clj@@ -3,9 +3,11 @@(:require[metabase.api.common :as api][metabase.metabot.agent.streaming :as streaming]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.shared.llm-shape :as llm-shape]))(set! *warn-on-reflection* true)@@ -34,6 +36,9 @@(handle [_ {:keys [entity_id]} _ctx](let [card (tools/with-entity {:kind :card :id entity_id}(api/read-check (metabot.db/card entity_id)))]+ ;; a question or a metric: judged as a metric read is, a curated card on its own or a metric by its definition+ (when (shared/curated-only?)+ (curation/check-curated-subject! (str (name (:type card)) " " entity_id) ["metric" entity_id])){:output (queued-output card):data-parts [(streaming/static-viz-part {:entity_id entity_id})]})))diff --git a/src/metabase/metabot/tools/subscriptions.clj b/src/metabase/metabot/tools/subscriptions.cljindex c15ef327877..88c11b81dbf 100644--- a/src/metabase/metabot/tools/subscriptions.clj+++ b/src/metabase/metabot/tools/subscriptions.clj@@ -5,10 +5,12 @@(:require[metabase.api.common :as api][metabase.channel.settings :as channel.settings]+ [metabase.metabot.curation :as curation][metabase.metabot.db :as metabot.db][metabase.metabot.scope :as scope][metabase.metabot.tools.core :as tools][metabase.metabot.tools.create-alert :as tools.create-alert]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.util :as metabot.tools.u][metabase.models.interface :as mi][metabase.pulse.api :as pulse.api]@@ -57,6 +59,8 @@(-> (metabot.db/dashboard dashboard-id)api/read-check(t2/hydrate [:dashcards :card])))+ _ (when (shared/curated-only?)+ (curation/check-curated-subject! (str "dashboard " dashboard-id) ["dashboard" dashboard-id]))dashcards (filter (comp int? :id :card) (:dashcards dashboard))](when-not (every? (comp mi/can-read? :card) dashcards)(tools/unrecoverable!diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.cljindex 93e91ba50f8..13bed5e65a5 100644--- a/src/metabase/metabot/tools/util.clj+++ b/src/metabase/metabot/tools/util.clj@@ -10,7 +10,8 @@[metabase.metabot.db :as metabot.db][metabase.metabot.tools.core :as tools][metabase.models.interface :as mi]- [metabase.util :as u]))+ [metabase.util :as u]+ [metabase.util.log :as log]))(defn handle-agent-error"Return an agent output for agent errors, re-throw `e` otherwise.@@ -218,6 +219,45 @@(let [mp (lib-be/application-database-metadata-provider (:database_id card))](lib/query mp (card-query-metadata card (lib.metadata/card mp card-id))))))+(defn card-results-query+ "Return a query over the result columns of the card with ID `card-id`, whatever its type: what the card exposes,+ rather than what its definition reads. [[card-query]] widens this to the definition for questions. With+ `expected-type`, the card must be of that type, as for [[card-query]]."+ ([card-id]+ (card-results-query card-id nil))+ ([card-id expected-type]+ (when-let [card (if expected-type (get-card card-id expected-type) (get-card card-id))]+ (let [mp (lib-be/application-database-metadata-provider (:database_id card))]+ (lib/query mp (lib.metadata/card mp card-id))))))++(defn card-definition-table-ids+ "The ids of the Tables the definition of the card with ID `card-id` reads, plus the Tables one FK hop away from+ them, following source Cards to their own definitions (ten levels at most), or nil when the card is missing or+ its query can't be built. A native definition names no Tables and contributes none: its result metadata can map+ a column to any Field, so it is not trusted to say which Tables it exposes."+ [card-id]+ (when-let [card (get-card card-id)]+ (let [mp (lib-be/application-database-metadata-provider (:database_id card))]+ (try+ (loop [todo #{card-id} seen #{} tables #{} depth 0]+ (if (or (empty? todo) (= depth 10))+ tables+ (let [queries (into []+ (comp (keep (fn [{:keys [dataset-query]}] (some->> dataset-query (lib/query mp))))+ (remove lib/any-native-stage?))+ (lib.metadata/bulk-metadata mp :metadata/card todo))+ refs (if (seq queries)+ (lib/all-referenced-entity-ids queries {:include-implicitly-joinable? true})+ {})+ seen (into seen todo)]+ (recur (into #{} (remove seen) (:card refs))+ seen+ (into tables (:table refs))+ (inc depth)))))+ (catch Exception e+ (log/debug e "Can't walk the card's definition; treating its tables as unknown")+ nil)))))+(defn metric-query"Return a query based on the metric with ID `metric-id`.diff --git a/test/metabase/metabot/curation_test.clj b/test/metabase/metabot/curation_test.cljindex ddf49758f0d..b35453b625d 100644--- a/test/metabase/metabot/curation_test.clj+++ b/test/metabase/metabot/curation_test.clj@@ -11,6 +11,13 @@(use-fixtures :once (fixtures/initialize :db))+(def ^:private curation-features+ "The features the verified, official, and Library curation signals are gated on; the index precomputes `curated`+ without a gate, so the two agree only while the features are active."+ #{:content-verification :official-collections :library})++(use-fixtures :each (fn [thunk] (mt/with-additional-premium-features curation-features (thunk))))+(deftest curated-ids-consistent-with-index-test(testing "curated-ids reads the same verdict from source-of-truth tables that the search indexprecomputes from the specs, so the two definitions stay consistent (BOT-1570)"@@ -49,3 +56,47 @@"source-of-truth marks verified/official cards and published-final/authoritative tables curated")(is (= (->strs source) (->strs indexed))"source-of-truth agrees with the index's precomputed curated column"))))))++(deftest curated-ids-parity-with-search-filter-test+ (testing "curated-ids agrees with `search :curated true` over every model it recognizes, in more than one batch,+ and on the rows search excludes (archived, hidden, document-owned)"+ (search.tu/with-appdb-search-if-available-without-fallback+ (mt/with-temp [:model/Database {db :id} {}+ :model/Collection {off :id} {:authority_level "official"}+ :model/Collection {lib :id} {:type "library-metrics" :name "fixturelibcoll two"}+ :model/Card {vq :id} {:name "parityfixture verified question"}+ :model/Card {oq :id} {:name "parityfixture official question" :collection_id off}+ :model/Card {aq :id} {:name "parityfixture archived official" :collection_id off :archived true}+ :model/Card {lm :id} {:name "parityfixture library metric" :type :metric :collection_id lib}+ :model/Card {om :id} {:name "parityfixture official metric" :type :metric :collection_id off}+ :model/Card {pm :id} {:name "parityfixture plain metric" :type :metric}+ :model/Dashboard {od :id} {:name "parityfixture official dashboard" :collection_id off}+ :model/Dashboard {pd :id} {:name "parityfixture plain dashboard"}+ :model/Table {pt :id} {:db_id db :name "parityfixture published" :active true+ :is_published true :data_layer :final}+ :model/Table {st :id} {:db_id db :name "parityfixture staged" :active true+ :is_published true :data_layer :internal}+ :model/Table {at :id} {:db_id db :name "parityfixture authoritative" :active true+ :data_authority :authoritative}+ :model/Table {ht :id} {:db_id db :name "parityfixture hidden" :active true+ :visibility_type :hidden :data_authority :authoritative}+ :model/Table {xt :id} {:db_id db :name "parityfixture plaintable" :active true}]+ (moderation/create-review! {:moderated_item_id vq+ :moderated_item_type "card"+ :moderator_id (mt/user->id :crowberto)+ :status "verified"})+ (search/update! (t2/select-one :model/Card :id vq) true)+ (let [items [["card" vq] ["card" oq] ["card" aq] ["metric" lm] ["metric" om] ["metric" pm]+ ["dashboard" od] ["dashboard" pd]+ ["table" pt] ["table" st] ["table" at] ["table" ht] ["table" xt]+ ;; a duplicate id is judged once and returned once+ ["card" vq]]+ source (with-redefs [metabot.curation/curation-batch-size 2]+ (metabot.curation/curated-ids items))+ indexed (into #{} (comp (filter :curated) (map (juxt :model :id)))+ (search.tu/search-results "parityfixture"))+ ->strs #(into #{} (map (fn [[m id]] [m (str id)])) %)]+ (is (= #{["card" vq] ["card" oq] ["metric" lm] ["metric" om] ["dashboard" od] ["table" pt] ["table" at]}+ source))+ (is (= (->strs source) (->strs indexed))+ "source-of-truth agrees with the index's precomputed curated column"))))))diff --git a/test/metabase/metabot/tools/curated_content_test.clj b/test/metabase/metabot/tools/curated_content_test.cljnew file mode 100644index 00000000000..e810db5d9b1--- /dev/null+++ b/test/metabase/metabot/tools/curated_content_test.clj@@ -0,0 +1,1453 @@+(ns metabase.metabot.tools.curated-content-test+ "BOT-1649: with `use_verified_content` on, Metabot tools that read or query entities directly stay within curated+ content, matching the `:curated` filter search applies."+ (:require+ [clojure.set :as set]+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.activity-feed.core :as activity-feed]+ [metabase.api-scope.core :as api-scope]+ [metabase.channel.settings :as channel.settings]+ [metabase.config.core :as config]+ [metabase.content-verification.core :as moderation]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.metabot.curation :as curation]+ [metabase.metabot.scope :as scope]+ [metabase.metabot.tools.autogen-dashboard :as autogen]+ [metabase.metabot.tools.construct :as construct]+ [metabase.metabot.tools.core :as tools]+ [metabase.metabot.tools.create-alert :as create-alert]+ [metabase.metabot.tools.explorations :as explorations-tools]+ [metabase.metabot.tools.field-stats :as field-stats]+ [metabase.metabot.tools.metadata :as metadata-tools]+ [metabase.metabot.tools.resources :as read-resource]+ [metabase.metabot.tools.run-query :as run-query]+ [metabase.metabot.tools.runtime :as tools.runtime]+ [metabase.metabot.tools.shared :as tools.shared]+ [metabase.metabot.tools.static-viz :as static-viz]+ [metabase.metabot.tools.subscriptions :as subscriptions]+ [metabase.permissions.core :as perms]+ [metabase.permissions.models.permissions-group :as perms-group]+ [metabase.test :as mt]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(def ^:private curation-features+ "The features the verified, official, and Library curation signals are gated on. The test instance holds no+ token, so every test here grants them; [[premium-gates-test]] takes them away again."+ #{:content-verification :official-collections :library})++(use-fixtures :each (fn [thunk] (mt/with-additional-premium-features curation-features (thunk))))++(defn- verify-card! [card-id]+ (moderation/create-review! {:moderated_item_id card-id+ :moderated_item_type "card"+ :moderator_id (mt/user->id :crowberto)+ :status "verified"}))++(defn- orders-query []+ (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :orders)))))++(defn- orders-joined-query+ "ORDERS explicitly joined to `table-kw` on `ORDERS.<fk-kw> = <table-kw>.<pk-kw>`."+ [table-kw fk-kw pk-kw]+ (let [mp (mt/metadata-provider)]+ (lib/join (orders-query)+ (lib/join-clause (lib.metadata/table mp (mt/id table-kw))+ [(lib/= (lib.metadata/field mp (mt/id :orders fk-kw))+ (lib.metadata/field mp (mt/id table-kw pk-kw)))]))))++(defn- count-metric-query [query]+ (lib/aggregate query (lib/count)))++(defn- as-metabot+ "Call `f` as a tool of the Metabot row `metabot` under `profile-id`, with tool state bound the way the runtime+ binds it."+ [metabot profile-id f]+ (binding [tools.shared/*metabot* metabot+ tools.shared/*profile-id* profile-id]+ (f)))++(defn- call+ "Call `tool` with `args` through the tool contract, as the runtime does. `ctx` is laid over a ctx that holds the+ bound memory, so a tool reads the conversation the test bound."+ ([tool args] (call tool args nil))+ ([tool args ctx]+ (tools/call tool args (merge {:tool-names #{"search"} :memory-atom tools.shared/*memory-atom*} ctx))))++(def ^:private fake-slack+ "A connected Slack with one channel, for the alert and subscription tools."+ {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})++(defmacro ^:private with-slack [& body]+ `(mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)+ channel.settings/slack-cached-channels-and-usernames (constantly fake-slack)]+ ~@body))++(defn- slack-ctx+ "The ctx of a call that came from the Slack conversation `C123` of [[fake-slack]]."+ []+ {:memory-atom (atom {:context {:slack_channel_id "C123"}})})++(defn- fields-args+ "`list_available_fields` arguments naming `ids` of one `kind`: `:table_ids`, `:model_ids`, or `:metric_ids`."+ [kind ids]+ (assoc {:table_ids [] :model_ids [] :metric_ids []} kind ids))++(defn- metadata-errors+ "The error texts of a `list_available_fields` call for `args`: the per-item errors when some item loaded, or the+ message of the failed call when none did."+ [args]+ (try+ (get-in (call metadata-tools/list-available-fields-tool args) [:structured-output :errors])+ (catch clojure.lang.ExceptionInfo e+ [(ex-message e)])))++(def ^:private read-ctx+ {:tool-names #{"read_resource" "search"}})++(defn- read-uris+ "Run `read_resource` on `uris` as a tool of `metabot` under `profile-id`, one entry per URI, as+ `POST /v1/read-resource` reads them."+ [metabot profile-id & uris]+ (as-metabot metabot profile-id+ #(tools/with-batched-entries read-resource/read-resource-tool {:uris (vec uris)} read-ctx nil+ (fn [entries]+ (mapv (fn [{:keys [item output failed? structured-output]}]+ (cond-> {:uri (:uri item)}+ failed? (assoc :error (tools/render-text output))+ (not failed?) (assoc :content {:structured-output structured-output})))+ entries)))))++(defn- item-ids [resource]+ (into #{} (map :id) (get-in resource [:content :structured-output :items])))++(defn- denied? [resource]+ (boolean (some-> (:error resource) (str/includes? "only uses curated content"))))++(defn- db-name []+ (t2/select-one-fn :name :model/Database :id (mt/id)))++(deftest read-resource-curated-only-tables-test+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {published :id} {:db_id db-id :name "published_orders" :schema "curated_schema"+ :active true :is_published true :data_layer :final}+ :model/Table {authoritative :id} {:db_id db-id :name "authoritative_people" :schema "curated_schema"+ :active true :data_authority :authoritative}+ :model/Table {raw :id} {:db_id db-id :name "raw_products" :schema "raw_schema" :active true}+ :model/Metabot curated-metabot {:name "curated metabot" :use_verified_content true}+ :model/Metabot open-metabot {:name "open metabot" :use_verified_content false}]+ (testing "curated-only Metabot"+ (testing "denies uncurated tables and their sub-resources without naming them"+ (doseq [uri [(str "metabase://table/" raw)+ (str "metabase://table/" raw "/fields")]]+ (let [[resource] (read-uris curated-metabot :internal uri)]+ (is (denied? resource) uri)+ (is (not (str/includes? (:error resource) "raw_products"))))))+ (testing "reads curated tables"+ (is (=? [{:content {:structured-output map?}} {:content {:structured-output map?}}]+ (read-uris curated-metabot :internal+ (str "metabase://table/" published)+ (str "metabase://table/" authoritative)))))+ (testing "lists only curated tables"+ (is (= #{published authoritative}+ (item-ids (first (read-uris curated-metabot :internal (str "metabase://database/" db-id "/tables"))))))+ (is (= #{}+ (item-ids (first (read-uris curated-metabot :internal+ (str "metabase://database/" db-id "/schemas/raw_schema/tables")))))))+ (testing "lists only schemas holding a curated table"+ (is (= ["curated_schema"]+ (->> (read-uris curated-metabot :internal (str "metabase://database/" db-id "/schemas"))+ first :content :structured-output :items (map :name))))))+ (testing "reads and lists everything readable when the Metabot isn't restricted, when no Metabot is bound+ (e.g. the agent API), and for the nlq profile"+ (doseq [[metabot profile-id] [[open-metabot :internal] [nil nil] [curated-metabot :nlq]]]+ (is (not (denied? (first (read-uris metabot profile-id (str "metabase://table/" raw))))))+ (is (= #{published authoritative raw}+ (item-ids (first (read-uris metabot profile-id+ (str "metabase://database/" db-id "/tables")))))))))))++(deftest read-resource-curated-only-cards-test+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Collection {coll-id :id} {:name "curation test collection"}+ :model/Card {verified-model :id} {:type :model :name "verified model" :collection_id coll-id+ :dataset_query (orders-query)}+ :model/Card {plain-model :id} {:type :model :name "plain model" :collection_id coll-id+ :dataset_query (orders-query)}+ :model/Card {plain-metric :id} {:type :metric :name "plain metric" :collection_id coll-id+ :dataset_query (count-metric-query (orders-query))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! verified-model)+ (testing "reads an uncurated metric only when what it's defined on is curated, as construct_notebook_query does"+ (is (denied? (first (read-uris metabot :internal (str "metabase://metric/" plain-metric)))))+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (not (denied? (first (read-uris metabot :internal (str "metabase://metric/" plain-metric))))))))+ (testing "denies uncurated cards"+ (is (denied? (first (read-uris metabot :internal (str "metabase://model/" plain-model "/fields"))))))+ (testing "reads curated cards, without their exported definition"+ (let [[resource] (read-uris metabot :internal (str "metabase://model/" verified-model "/fields"))]+ (is (not (denied? resource)))+ (is (map? (get-in resource [:content :structured-output])))+ (is (not (contains? (get-in resource [:content :structured-output]) :query_json))))+ (testing "(the unrestricted Metabot gets the definition)"+ (is (contains? (get-in (first (read-uris nil nil (str "metabase://model/" verified-model)))+ [:content :structured-output])+ :query_json))))+ (testing "lists only curated cards"+ (let [models (item-ids (first (read-uris metabot :internal (str "metabase://database/" (mt/id) "/models"))))]+ (is (contains? models verified-model))+ (is (not (contains? models plain-model))))+ (is (= #{verified-model}+ (item-ids (first (read-uris metabot :internal (str "metabase://collection/" coll-id "/items"))))))))))++(deftest read-resource-curated-only-dashboards-test+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Collection {official :id} {:authority_level "official"}+ :model/Dashboard {curated-dash :id} {:name "official dashboard" :collection_id official}+ :model/Dashboard {plain-dash :id} {:name "plain dashboard"}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (testing "an uncurated dashboard and its items are denied"+ (is (denied? (first (read-uris metabot :internal (str "metabase://dashboard/" plain-dash)))))+ (is (denied? (first (read-uris metabot :internal (str "metabase://dashboard/" plain-dash "/items"))))))+ (testing "a curated dashboard reads"+ (is (not (denied? (first (read-uris metabot :internal (str "metabase://dashboard/" curated-dash))))))))))++(deftest excluded-signals-test+ (testing "search never returns these, so they aren't curated whatever their signals"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Collection {official :id} {:authority_level "official"}+ :model/Card {archived :id} {:collection_id official :archived true :dataset_query (orders-query)}+ :model/Dashboard {archived-dash :id} {:collection_id official :archived true}+ :model/Document {doc-id :id} {:collection_id official}+ :model/Card {doc-card :id} {:collection_id official :document_id doc-id+ :dataset_query (orders-query)}+ :model/Table {hidden :id} {:db_id (mt/id) :name "hidden_authoritative" :active true+ :visibility_type :hidden :data_authority :authoritative}+ :model/Table {live :id} {:db_id (mt/id) :name "live_authoritative" :active true+ :data_authority :authoritative}]+ (is (= #{["table" live]}+ (curation/curated-ids [["card" archived] ["dashboard" archived-dash] ["card" doc-card]+ ["table" hidden] ["table" live]])))))))++(deftest premium-gates-test+ (testing "verified, official, and Library signals count only while their feature is active; authoritative always"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Collection {official :id} {:authority_level "official"}+ :model/Card {verified :id} {:dataset_query (orders-query)}+ :model/Card {in-official :id} {:collection_id official :dataset_query (orders-query)}+ :model/Table {published :id} {:db_id (mt/id) :name "gated_published" :active true+ :is_published true :data_layer :final}+ :model/Table {authoritative :id} {:db_id (mt/id) :name "gated_authoritative" :active true+ :data_authority :authoritative}]+ (verify-card! verified)+ (let [items [["card" verified] ["card" in-official] ["table" published] ["table" authoritative]]]+ (is (= (set items) (curation/curated-ids items)))+ (mt/with-premium-features #{}+ (is (= #{["table" authoritative]} (curation/curated-ids items)))))))))++(deftest metric-read-judges-metric-cards-only-test+ (testing "the metric rule judges metric Cards with structured definitions, and nothing else passes through it"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/Card {native-question :id} {:type :question :name "native question"+ :dataset_query (mt/native-query {:query "SELECT 1"})}+ :model/Card {plain-model :id} {:type :model :name "plain model"+ :dataset_query (orders-query)}+ :model/Card {native-metric :id native-metric-eid :entity_id}+ {:type :metric :name "native metric"+ :dataset_query (mt/native-query {:query "SELECT count(*) FROM ORDERS"})}+ :model/Card {verified-native-metric :id}+ {:type :metric :name "verified native metric"+ :dataset_query (mt/native-query {:query "SELECT count(*) FROM ORDERS"})}+ :model/Card {archived-metric :id}+ {:type :metric :name "archived metric" :archived true+ :dataset_query (count-metric-query (orders-query))}]+ (verify-card! verified-native-metric)+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (let [read (fn [id] (first (read-uris metabot :internal (str "metabase://metric/" id))))+ metadata (fn [id] {:errors (as-metabot metabot :internal+ #(metadata-errors (fields-args :metric_ids [id])))})+ query (fn [metric-eid]+ {:lib/type "mbql/query"+ :stages [{:lib/type "mbql.stage/mbql"+ :source-table [(db-name) "PUBLIC" "ORDERS"]+ :aggregation [["metric" {} metric-eid]]}]})]+ (testing "an uncurated native question isn't readable through the metric URI"+ (is (denied? (read native-question)))+ (is (some #(str/includes? % "not a metric") (:errors (metadata native-question)))+ "list_available_fields checks the kind first"))+ (testing "an uncurated model on a curated table isn't readable through the metric URI"+ (is (denied? (read plain-model)))+ (is (some #(str/includes? % "not a metric") (:errors (metadata plain-model)))))+ (testing "an uncurated metric with a native definition is denied: it references no tables to judge it by"+ (is (denied? (read native-metric)))+ (testing "and construct_notebook_query refuses it before repair as an uncurated source"+ (is (= :uncurated-source+ (try+ (as-metabot metabot :internal+ #(construct/execute-representations-query (query native-metric-eid)))+ nil+ (catch clojure.lang.ExceptionInfo e+ (:error (ex-data e))))))))+ (testing "an archived uncurated metric on a curated table is denied"+ (is (denied? (read archived-metric))))+ (testing "a curated metric with a native definition is readable, being curated itself"+ (is (not (denied? (read verified-native-metric)))))))))))++(deftest curated-non-metric-card-through-metric-paths-test+ (testing "a curated Card named where a metric is expected passes on its own curation; the metric paths then refuse+ it by kind, as they do for the unrestricted Metabot, and an uncurated question referenced as a metric+ fails closed"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {model-id :id} {:type :model :name "verified model" :dataset_query (orders-query)}+ :model/Card {question-id :id} {:type :question :name "plain question"+ :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! model-id)+ (testing "read_resource metric URI"+ (let [read (fn [mb] (:error (first (read-uris mb :internal (str "metabase://metric/" model-id)))))]+ (is (str/includes? (read metabot) "not a metric"))+ (is (= (read nil) (read metabot)))))+ (testing "list_available_fields metric_ids"+ (let [errors (fn [mb] (as-metabot mb :internal #(metadata-errors (fields-args :metric_ids [model-id]))))]+ (is (some #(str/includes? % "not a metric") (errors metabot)))+ (is (= (errors nil) (errors metabot)))))+ (testing "a question referenced as a metric in a query is judged as missing, not by its definition"+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (= [["card" question-id]]+ (curation/uncurated-query-sources+ (lib/query (mt/metadata-provider)+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [["metric" question-id]]}}))))))))))++(deftest curated-question-fields-stay-within-its-results-test+ (testing "a verified question's field reads expose its own result columns, not every column of the raw table it+ reads: REVIEWS is neither curated nor FK-related to the question's results, and construct_notebook_query+ rejects joining it to the question"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {question-id :id} {:type :question+ :name "verified review count"+ :dataset_query (lib/aggregate+ (lib/query mp (lib.metadata/table+ mp (mt/id :reviews)))+ (lib/count))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! question-id)+ (let [rating (mt/id :reviews :rating)]+ (testing "construct_notebook_query's rule rejects the raw table joined to the question"+ (let [on-question (lib/query mp (lib.metadata/card mp question-id))+ joined (lib/join on-question+ (lib/join-clause (lib.metadata/table mp (mt/id :reviews))+ [(lib/= (first (lib/returned-columns on-question))+ (lib.metadata/field mp (mt/id :reviews :id)))]))]+ (is (= [["table" (mt/id :reviews)]] (curation/uncurated-query-sources joined)))))+ (testing "read_resource doesn't serve the raw table's column through the question"+ (let [[resource] (read-uris metabot :internal+ (str "metabase://question/" question-id "/fields/" rating))]+ (is (nil? (get-in resource [:content :structured-output :value_metadata])))))+ (testing "get_field_values doesn't either: the column isn't among the question's fields"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"was not found in question"+ (as-metabot metabot :internal+ #(call metadata-tools/get-field-values-tool {:data_source "question"+ :source_id question-id+ :field_id rating})))))))))))++(deftest field-values-follow-the-definition-not-edited-metadata-test+ (testing "a curated model whose result metadata maps a column to a field of a table its definition never reads+ doesn't serve that field's values"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {model-id :id} {:type :model :name "verified orders model"+ :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [rating (mt/id :reviews :rating)+ total (mt/id :orders :total)+ ;; point one result column at REVIEWS.RATING, a field no FK from ORDERS reaches; verified afterwards,+ ;; since editing a verified card unverifies it+ _ (t2/update! :model/Card :id model-id+ {:result_metadata [{:name "TOTAL" :display_name "Total" :base_type :type/Float+ :id total :table_id (mt/id :orders)}+ {:name "RATING" :display_name "Rating" :base_type :type/Integer+ :id rating :table_id (mt/id :reviews)}]})+ _ (verify-card! model-id)+ read (fn [field-id]+ (as-metabot metabot :internal+ #(call metadata-tools/get-field-values-tool {:data_source "model"+ :source_id model-id+ :field_id field-id})))]+ (is (some? (get-in (read total) [:structured-output :value_metadata])))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content" (read rating)))+ (testing "(the unrestricted Metabot serves it)"+ (is (some? (get-in (as-metabot nil nil+ #(call metadata-tools/get-field-values-tool {:data_source "model"+ :source_id model-id+ :field_id rating}))+ [:structured-output :value_metadata])))))))))++(deftest covered-metric-as-source-card-test+ (testing "an uncurated metric that passes the metric rule may be a query's source card, as it may be read and used in+ an aggregation; one that doesn't pass is still rejected"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {metric-id :id metric-eid :entity_id}+ {:type :metric+ :name "plain metric"+ :dataset_query (count-metric-query (orders-query))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [q {:lib/type "mbql/query"+ :stages [{:lib/type "mbql.stage/mbql" :source-card metric-eid}]}+ construct (fn [metabot profile-id]+ (as-metabot metabot profile-id #(construct/execute-representations-query q)))+ rejected? (fn []+ (try+ (construct metabot :internal)+ false+ (catch clojure.lang.ExceptionInfo e+ (if (= :uncurated-source (:error (ex-data e))) true (throw e)))))]+ (testing "the query is valid without a restricted Metabot"+ (is (some? (:structured-output (construct nil nil)))))+ (testing "over a raw table the metric is rejected as a source"+ (is (rejected?)))+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (testing "over a curated table it is readable"+ (is (not (denied? (first (read-uris metabot :internal (str "metabase://metric/" metric-id)))))))+ (testing "and queryable as a source card"+ (is (not (rejected?))))))))))++(deftest inactive-table-reads-as-missing-test+ (testing "a deactivated table reads as missing, as it does for the unrestricted Metabot, not as uncurated"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Table {inactive :id} {:db_id (mt/id) :name "dropped_raw_table" :active false}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [uri (str "metabase://table/" inactive)]+ (testing "read_resource"+ (let [[resource] (read-uris metabot :internal uri)]+ (is (some? (:error resource)))+ (is (not (denied? resource)))+ (is (= (:error (first (read-uris nil nil uri))) (:error resource)))))+ (testing "list_available_fields"+ (is (not-any? #(str/includes? % "only uses curated content")+ (as-metabot metabot :internal+ #(metadata-errors (fields-args :table_ids [inactive])))))))))))++(deftest curated-entity-details-list-only-usable-metrics-test+ (testing "a curated table's or model's details list only the metrics this Metabot may use: a metric on curated+ ORDERS (or on a verified model over it) that joins raw REVIEWS fails the rule, so metric/{id} denies it and+ table/{ORDERS} and model/{id} must not list it; a metric that passes the rule stays listed"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {good-metric :id} {:type :metric+ :name "orders count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Card {bad-metric :id} {:type :metric+ :name "orders joined to raw reviews"+ :dataset_query (count-metric-query+ (orders-joined-query :reviews :product_id+ :product_id))}+ :model/Card {model-id :id} {:type :model :name "verified orders model"+ :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! model-id)+ (let [mp (mt/metadata-provider)+ on-model (lib/query mp (lib.metadata/card mp model-id))+ product-id (some #(when (= "PRODUCT_ID" (:name %)) %) (lib/returned-columns on-model))]+ (mt/with-temp [:model/Card {bad-model-metric :id}+ {:type :metric+ :name "model joined to raw reviews"+ :dataset_query (count-metric-query+ (lib/join on-model+ (lib/join-clause+ (lib.metadata/table mp (mt/id :reviews))+ [(lib/= product-id+ (lib.metadata/field mp (mt/id :reviews :product_id)))])))}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (let [listed (fn [uri]+ (let [so (get-in (first (read-uris metabot :internal uri))+ [:content :structured-output])]+ (is (map? so) uri)+ (into #{} (map :id) (:metrics so))))]+ (testing "the failing metrics themselves are denied"+ (is (denied? (first (read-uris metabot :internal (str "metabase://metric/" bad-metric)))))+ (is (denied? (first (read-uris metabot :internal (str "metabase://metric/" bad-model-metric))))))+ (testing "table/{ORDERS} lists the passing metric and not the failing one"+ (let [ids (listed (str "metabase://table/" (mt/id :orders)))]+ (is (contains? ids good-metric))+ (is (not (contains? ids bad-metric)))))+ (testing "model/{id} doesn't list the failing metric defined on it"+ (is (not (contains? (listed (str "metabase://model/" model-id)) bad-model-metric))))))))))))++(deftest read-resource-curated-only-recents-test+ (testing "recent items (which carry keyword models) are filtered like any other list"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-model-cleanup [:model/RecentViews]+ (mt/with-temp [:model/Card {verified-model :id} {:type :model :name "verified recent model"+ :dataset_query (orders-query)}+ :model/Card {plain-question :id} {:type :question :name "plain recent question"+ :dataset_query (orders-query)}+ :model/Table {raw-table :id} {:db_id (mt/id) :name "raw_recent_table" :active true}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! verified-model)+ (doseq [[model id] [[:model/Card verified-model] [:model/Card plain-question] [:model/Table raw-table]]]+ (activity-feed/update-users-recent-views! (mt/user->id :crowberto) model id :view))+ (let [items (fn [metabot]+ (filterv (comp #{["model" verified-model] ["question" plain-question] ["table" raw-table]}+ (juxt :type :id))+ (get-in (first (read-uris metabot :internal "metabase://user/recent-items"))+ [:content :structured-output :items])))]+ ;; key on type and id: a temp Card's id can coincide with a temp Table's+ (is (=? [{:type "model" :id verified-model}] (items metabot)))+ (testing "the unrestricted Metabot lists them all, with their public types and URIs"+ (is (=? [{:type "table" :uri (str "metabase://table/" raw-table)}+ {:type "question" :uri (str "metabase://question/" plain-question)}+ {:type "model" :uri (str "metabase://model/" verified-model)}]+ (items nil))))))))))++(deftest curation-subject-test+ (testing "transforms can never be curated"+ (is (= ["transform" 1]+ (#'read-resource/curation-subject ["transform" "1" "sources"]))))+ (testing "navigation and table dependents aren't gated per entity"+ (is (nil? (#'read-resource/curation-subject ["databases"])))+ (is (nil? (#'read-resource/curation-subject ["collection" "1" "items"])))+ (is (nil? (#'read-resource/curation-subject ["table" "1" "derived"]))))+ (testing "tables, cards, and dashboards are judged as themselves"+ (is (= ["table" 1] (#'read-resource/curation-subject ["table" "1" "fields" "c75"])))+ (is (= ["metric" 2] (#'read-resource/curation-subject ["metric" "2" "dimensions"])))+ (is (= ["dashboard" 3] (#'read-resource/curation-subject ["dashboard" "3" "items"])))))++(defn- rejected?+ "Whether `construct_notebook_query` rejects `q` as uncurated for `metabot` under `profile-id`."+ [metabot profile-id q]+ (try+ (as-metabot metabot profile-id #(construct/execute-representations-query q))+ false+ (catch clojure.lang.ExceptionInfo e+ (if (= :uncurated-source (:error (ex-data e)))+ true+ (throw e)))))++(deftest construct-query-curated-only-test+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {verified-model :id verified-eid :entity_id} {:type :model :name "verified model"+ :dataset_query (orders-query)}+ :model/Card {plain-eid :entity_id} {:type :model :name "plain model"+ :dataset_query (orders-query)}+ :model/Card {verified-metric :id verified-metric-eid :entity_id}+ {:type :metric :name "verified metric" :dataset_query (count-metric-query (orders-query))}+ :model/Card {plain-metric :id plain-metric-eid :entity_id}+ {:type :metric :name "plain metric" :dataset_query (count-metric-query (orders-query))}+ :model/Card {nested-metric-eid :entity_id}+ {:type :metric :name "plain metric built on the plain metric"+ :dataset_query {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [["metric" plain-metric]]}}}+ :model/Card {nested-on-verified :id nested-on-verified-eid :entity_id}+ {:type :metric :name "plain metric built on the verified metric"+ :dataset_query {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [["metric" verified-metric]]}}}+ :model/Card {verified-join-metric :id verified-join-metric-eid :entity_id}+ {:type :metric :name "verified metric joining an unrelated table"+ :dataset_query (count-metric-query (orders-joined-query :reviews :product_id :product_id))}+ :model/Card {related-join-metric-eid :entity_id}+ {:type :metric :name "plain metric joining a related table"+ :dataset_query (count-metric-query (orders-joined-query :products :product_id :id))}+ :model/Card {unrelated-join-metric-eid :entity_id}+ {:type :metric :name "plain metric joining an unrelated table"+ :dataset_query (count-metric-query (orders-joined-query :reviews :product_id :product_id))}+ :model/Card {model-metric-eid :entity_id}+ {:type :metric :name "plain metric on a model"+ :dataset_query (let [mp (mt/metadata-provider)]+ (count-metric-query (lib/query mp (lib.metadata/card mp verified-model))))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! verified-model)+ (verify-card! verified-metric)+ (verify-card! verified-join-metric)+ (let [db-name (db-name)+ stage (fn [source] (merge {:lib/type "mbql.stage/mbql" :aggregation [["count" {}]]} source))+ query (fn [source] {:lib/type "mbql/query" :stages [(stage source)]})+ orders-query (query {:source-table [db-name "PUBLIC" "ORDERS"]})+ products-query (query {:source-table [db-name "PUBLIC" "PRODUCTS"]})+ joined-query (assoc-in orders-query [:stages 0 :breakout]+ [["field" {:source-field [db-name "PUBLIC" "ORDERS" "PRODUCT_ID"]}+ [db-name "PUBLIC" "PRODUCTS" "CATEGORY"]]])+ ;; raw PRODUCTS as the source, explicitly joined to ORDERS+ products-joined-query+ (assoc-in products-query [:stages 0 :joins]+ [{:lib/type "mbql/join"+ :fields "all"+ :strategy "left-join"+ :alias "Orders"+ :conditions [["=" {}+ ["field" {} [db-name "PUBLIC" "PRODUCTS" "ID"]]+ ["field" {:join-alias "Orders"} [db-name "PUBLIC" "ORDERS" "PRODUCT_ID"]]]]+ :stages [{:lib/type "mbql.stage/mbql" :source-table [db-name "PUBLIC" "ORDERS"]}]}])+ metric-query (fn [metric-eid]+ (assoc-in orders-query [:stages 0 :aggregation] [["metric" {} metric-eid]]))+ construct (fn [metabot profile-id q]+ (as-metabot metabot profile-id #(construct/execute-representations-query q)))+ accepted? (fn [q] (some? (:structured-output (construct metabot :internal q))))]+ (testing "a raw table is rejected for a curated-only Metabot"+ (is (rejected? metabot :internal orders-query)))+ (testing "a raw table is queryable without a restricted Metabot, and for the nlq profile"+ (is (some? (:structured-output (construct nil nil orders-query))))+ (is (some? (:structured-output (construct metabot :nlq orders-query)))))+ (testing "a curated table is queryable"+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (accepted? orders-query))+ (testing "along with the related tables it exposes, joined implicitly"+ (is (accepted? joined-query)))+ (testing "and the metrics defined on it, curated or not"+ (is (accepted? (metric-query plain-metric-eid)))+ (testing "as long as everything the metric's definition reads is curated or covered"+ (is (accepted? (metric-query related-join-metric-eid)))+ (is (rejected? metabot :internal (metric-query unrelated-join-metric-eid))))+ (testing "including a metric built on another uncurated metric over the curated table"+ (is (accepted? (metric-query nested-metric-eid)))))+ (testing "but a related table is still not a source of its own"+ (is (rejected? metabot :internal products-query))+ (testing "even when joined to the curated table"+ (is (rejected? metabot :internal products-joined-query))))))+ (testing "an uncurated metric defined on a curated model is accepted"+ (is (accepted? (assoc-in (query {:source-card verified-eid}) [:stages 0 :aggregation]+ [["metric" {} model-metric-eid]]))))+ (testing "a curated metric covers the raw table it's defined on, and that table's related tables"+ (is (accepted? (metric-query verified-metric-eid)))+ (is (accepted? (assoc-in (metric-query verified-metric-eid) [:stages 0 :breakout]+ (get-in joined-query [:stages 0 :breakout])))))+ (testing "an uncurated metric on a raw table is rejected"+ (is (rejected? metabot :internal (metric-query plain-metric-eid))))+ (testing "read_resource and construct_notebook_query agree on metrics over a raw table"+ (testing "a plain metric next to the verified one is rejected on both sides"+ (is (rejected? metabot :internal+ (assoc-in orders-query [:stages 0 :aggregation]+ [["metric" {} verified-metric-eid] ["metric" {} plain-metric-eid]])))+ (is (denied? (first (read-uris metabot :internal (str "metabase://metric/" plain-metric))))))+ (testing "a plain metric built on the verified metric is accepted on both sides"+ (is (accepted? (metric-query nested-on-verified-eid)))+ (is (not (denied? (first (read-uris metabot :internal+ (str "metabase://metric/" nested-on-verified))))))))+ (testing "a table a curated metric only joins is not a source of its own"+ (is (rejected? metabot :internal+ (assoc-in (query {:source-table [db-name "PUBLIC" "REVIEWS"]}) [:stages 0 :aggregation]+ [["metric" {} verified-join-metric-eid]]))))+ (testing "a table a curated metric joins widens coverage no further: PRODUCTS is one hop from ORDERS, so a+ breakout on it passes, but REVIEWS' own FK targets don't become joinable through the metric"+ (is (accepted? (assoc-in (metric-query verified-join-metric-eid) [:stages 0 :breakout]+ (get-in joined-query [:stages 0 :breakout])))))+ (testing "source cards must be curated"+ (is (accepted? (query {:source-card verified-eid})))+ (is (rejected? metabot :internal (query {:source-card plain-eid}))))))))++(deftest construct-query-coarse-gate-runs-before-repair-test+ (testing "a query on a raw table with a column that doesn't exist is refused as uncurated, before repair can list+ the table's columns"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [db-name (db-name)+ q {:lib/type "mbql/query"+ :stages [{:lib/type "mbql.stage/mbql"+ :source-table [db-name "PUBLIC" "PRODUCTS"]+ :breakout [["field" {} [db-name "PUBLIC" "PRODUCTS" "NO_SUCH_COLUMN"]]]}]}]+ (is (rejected? metabot :internal q))+ (testing "(the unrestricted Metabot gets the column error)"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"(?i)column|field"+ (as-metabot nil nil #(construct/execute-representations-query q))))))))))++(deftest measures-and-segments-follow-their-tables-test+ (testing "a measure or segment is usable only when its table and the tables its definition reads are curated or+ covered"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)+ reviews-count (lib/aggregate (lib/query mp (lib.metadata/table mp (mt/id :reviews))) (lib/count))+ orders-count (count-metric-query (orders-query))]+ (mt/with-temp [:model/Measure {raw-measure :id} {:name "reviews count" :table_id (mt/id :reviews)+ :definition reviews-count}+ :model/Measure {orders-measure :id} {:name "orders count" :table_id (mt/id :orders)+ :definition orders-count}+ :model/Segment {raw-segment :id}+ {:name "rated"+ :table_id (mt/id :reviews)+ :definition (lib/filter (lib/query mp (lib.metadata/table mp (mt/id :reviews)))+ (lib/> (lib.metadata/field mp (mt/id :reviews :rating)) 3))}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (let [on-orders (fn [aggregation]+ (lib/query mp {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [aggregation]}}))]+ (is (= [] (curation/uncurated-query-sources (on-orders [:measure orders-measure]))))+ (is (= [[:measure raw-measure]] (curation/uncurated-query-sources (on-orders [:measure raw-measure]))))+ (is (= [[:measure Integer/MAX_VALUE]]+ (curation/uncurated-query-sources (on-orders [:measure Integer/MAX_VALUE])))+ "a missing measure fails closed")+ (is (= [[:segment raw-segment]]+ (curation/uncurated-query-sources+ (lib/query mp {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :filter [:segment raw-segment]}})))))))))))++(deftest run-query-viewed-question-test+ (testing "run_query reads a saved question the user is viewing only when read_resource would"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {verified :id} {:dataset_query (count-metric-query (orders-query))}+ :model/Card {plain :id} {:dataset_query (count-metric-query (orders-query))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :crowberto)}]+ (verify-card! verified)+ (let [run (fn [card-id]+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true]+ (as-metabot metabot :internal+ #(binding [tools.shared/*memory-atom*+ (atom {:conversation-id conversation-id+ :state {:queries {}}+ :context {:user_is_viewing [{:type "question"+ :id card-id}]}})]+ (tools/handle run-query/run-query-tool {:query_id (str card-id)} nil)))))]+ (is (str/includes? (:output (run verified)) "Count"))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content" (run plain))))))))++(deftest id-taking-tools-curated-only-test+ (with-slack+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {plain :id} {:dataset_query (orders-query)}+ :model/Dashboard {plain-dash :id} {}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (as-metabot metabot :internal+ (fn []+ (testing "create_autogenerated_dashboard"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call autogen/create-autogenerated-dashboard-tool+ {:source {:table_id (mt/id :orders)}})))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call autogen/create-autogenerated-dashboard-tool+ {:source {:report_id plain}}))))+ (testing "static_viz"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call static-viz/static-viz-tool {:entity_id plain}))))+ (testing "create_alert"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call create-alert/create-alert-tool+ {:card_id plain+ :send_condition "has_result"+ :schedule {:frequency "daily" :hour 9}}+ (slack-ctx)))))+ (testing "create_dashboard_subscription"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call subscriptions/create-dashboard-subscription-tool+ {:dashboard_id plain-dash+ :slack_channel "#general"+ :schedule {:frequency "daily" :hour 9}}))))))))))++(deftest read-resource-curated-check-ordering-test+ (testing "the curation check runs after the entity's existence and read checks, but before its handler"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (testing "a missing table reads as missing, not as uncurated"+ (let [[resource] (read-uris metabot :internal "metabase://table/2147483647")]+ (is (some? (:error resource)))+ (is (not (denied? resource)))))+ (testing "a missing transform reads as missing, not as uncurated"+ (let [[resource] (read-uris metabot :internal "metabase://transform/2147483647")]+ (is (some? (:error resource)))+ (is (not (denied? resource)))))+ (testing "a card on a routing destination database reads as missing, not as uncurated"+ (mt/with-temp [:model/Database {router-id :id} {}+ :model/Database {destination-id :id} {:router_database_id router-id}+ :model/Card {card-id :id} {:type :model :database_id destination-id}]+ (let [[resource] (read-uris metabot :internal (str "metabase://model/" card-id))]+ (is (some? (:error resource)))+ (is (not (denied? resource))))))+ (testing "a denied URI does none of its handler's work (field values and fingerprints aren't computed)"+ (mt/with-dynamic-fn-redefs [field-stats/field-values+ (fn [& _] (throw (ex-info "handler ran for a denied URI" {})))]+ (let [uri (str "metabase://table/" (mt/id :orders) "/fields/" (mt/id :orders :total))]+ (is (denied? (first (read-uris metabot :internal uri)))))))))+ (testing "an unreadable table doesn't reveal whether it's curated"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {published :id} {:db_id db-id :name "hidden_published" :active true+ :is_published true :data_layer :final}+ :model/Table {raw :id} {:db_id db-id :name "hidden_raw" :active true}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-no-data-perms-for-all-users!+ (mt/with-current-user (mt/user->id :rasta)+ (let [errors (mapv #(:error (first (read-uris metabot :internal (str "metabase://table/" %))))+ [published raw])]+ (is (every? some? errors))+ (is (not-any? #(str/includes? % "only uses curated content") errors))+ (is (every? #(str/includes? % "was not found") errors)))))))))++(deftest read-resource-curated-action-uri-test+ (testing "an action dashcard keeps its backing model's uri only when that model is curated"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Collection {official :id} {:authority_level "official"}]+ (mt/with-actions [{model-id :id} {:type :model :dataset_query (orders-query)}+ {:keys [action-id]} {:type :query :visualization_settings {}}]+ (mt/with-temp [:model/Dashboard {dash-id :id} {:collection_id official}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id model-id :action_id action-id+ :row 0 :col 0 :size_x 4 :size_y 1+ :visualization_settings {:button.label "Create Row"}}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [action-uris (fn []+ (into []+ (keep :uri)+ (get-in (first (read-uris metabot :internal+ (str "metabase://dashboard/" dash-id "/items")))+ [:content :structured-output :items])))]+ (is (= [] (action-uris)))+ (verify-card! model-id)+ (is (= [(str "metabase://model/" model-id)] (action-uris))))))))))++(deftest metadata-tools-curated-only-test+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/Card {plain-metric :id} {:type :metric :name "plain metric"+ :dataset_query (count-metric-query (orders-query))}]+ (as-metabot metabot :internal+ (fn []+ (testing "list_available_fields reports uncurated tables as errors"+ (is (some #(str/includes? % "only uses curated content")+ (metadata-errors (fields-args :table_ids [(mt/id :orders)])))))+ (testing "get_field_values rejects uncurated tables"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (call metadata-tools/get-field-values-tool+ {:data_source "table"+ :source_id (mt/id :orders)+ :field_id (mt/id :orders :total)}))))+ (testing "curated tables are readable"+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (=? {:structured-output {:tables [{:id (mt/id :orders)}]}}+ (call metadata-tools/list-available-fields-tool (fields-args :table_ids [(mt/id :orders)]))))+ (testing "and so are the metrics defined on them"+ (is (=? {:structured-output {:metrics [{:id plain-metric}]}}+ (call metadata-tools/list-available-fields-tool (fields-args :metric_ids [plain-metric])))))+ (is (some? (call metadata-tools/get-field-values-tool+ {:data_source "table"+ :source_id (mt/id :orders)+ :field_id (mt/id :orders :total)}))))))))))++(def ^:private curated-probe-tool+ "A tool that reports whether the session it runs in is restricted to curated content."+ (reify tools/Tool+ (declaration [_] {:name "curated_probe" :description "Reports the session's curation verdict."+ :args [:map {:closed true}]})+ (handle [_ _args _ctx] {:output (str (tools.shared/curated-only?))})))++(deftest runtime-binds-the-curated-only-verdict-test+ (testing "the tool runtime binds the session's Metabot and profile, from which curated-only? is derived"+ (mt/with-temp [:model/Metabot curated {:name "curated metabot" :use_verified_content true}+ :model/Metabot open {:name "open metabot" :use_verified_content false}]+ (let [entries (tools/entries [curated-probe-tool])+ probe (fn [metabot profile-id]+ (binding [scope/*current-user-scope* #{"agent:content:read"}]+ (:output (tools.runtime/invoke entries+ {:profile-id profile-id+ :metabot metabot+ :tool-names #{"curated_probe"}+ :user-scope api-scope/unrestricted}+ "curated_probe" {}))))]+ (is (false? (tools.shared/curated-only?)) "unbound outside a session")+ (is (= "true" (probe curated :internal)))+ (is (= "false" (probe curated :nlq)) "the nlq profile is exempt")+ (is (= "false" (probe curated :nlq-fallback)))+ (is (= "false" (probe curated :sql)) "the SQL editor's profile writes SQL, which is outside the rule")+ (is (= "false" (probe open :internal)))+ (is (= "false" (probe nil :internal)))))))++(deftest construct-coarse-gate-runs-before-repair-test+ (testing "the gate before repair rejects an uncurated source whatever else the query names, so repair never lists+ its columns: an unusable metric, a metric reference without options, or a joined uncurated card"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {raw-metric-eid :entity_id} {:type :metric+ :name "raw orders count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Card {verified-metric-eid :entity_id verified-metric :id}+ {:type :metric :name "verified orders count" :dataset_query (count-metric-query (orders-query))}+ :model/Card {plain-model-eid :entity_id} {:type :model+ :name "plain products model"+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :products)))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! verified-metric)+ (let [db-name (db-name)+ error-of (fn [q]+ (try+ (as-metabot metabot :internal #(construct/execute-representations-query q))+ nil+ (catch clojure.lang.ExceptionInfo e+ (ex-data e))))+ bad-field ["field" {} [db-name "PUBLIC" "ORDERS" "NO_SUCH_COLUMN"]]+ query (fn [stage]+ {:lib/type "mbql/query"+ :stages [(merge {:lib/type "mbql.stage/mbql" :source-table [db-name "PUBLIC" "ORDERS"]}+ stage)]})]+ (testing "a metric this Metabot may not use doesn't authorize its table: rejected before repair"+ (is (= :uncurated-source+ (:error (error-of (query {:aggregation [["metric" {} raw-metric-eid]] :breakout [bad-field]}))))))+ (testing "a metric reference without its options map is read the same way"+ (is (= :uncurated-source+ (:error (error-of (query {:aggregation [["metric" raw-metric-eid]] :breakout [bad-field]}))))))+ (testing "a curated metric authorizes its own table as the source, so the query reaches repair"+ (is (= :unknown-field+ (:error (error-of (query {:aggregation [["metric" {} verified-metric-eid]] :breakout [bad-field]}))))))+ (testing "a joined uncurated card is rejected before repair, even under a curated source"+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (= :uncurated-source+ (:error (error-of+ (query {:aggregation [["count" {}]]+ :joins [{:lib/type "mbql/join"+ :fields "all"+ :strategy "left-join"+ :alias "P"+ :conditions [["=" {}+ ["field" {} [db-name "PUBLIC" "ORDERS" "PRODUCT_ID"]]+ ["field" {:join-alias "P"} ["card" plain-model-eid "NO_SUCH"]]]]+ :stages [{:lib/type "mbql.stage/mbql" :source-card plain-model-eid}]}]})))))))+ (testing "each uncurated entity is listed once"+ (is (= [["table" (mt/id :orders)]]+ (:uncurated (error-of (query {:breakout [bad-field]}))))))))))))++(deftest construct-coarse-gate-agrees-with-the-full-rule-test+ (testing "the gate before repair and the rule after resolve share one coverage computation: a table the full rule+ denies is refused before repair can list its columns"+ ;; A curated metric on REVIEWS joins a model over ORDERS. One FK hop from REVIEWS reaches PRODUCTS only; the+ ;; model's result columns reach PEOPLE. The rule covers the metric's primary source and one hop from it, and the+ ;; joined model without a hop, so PEOPLE is not covered. A coarse set that hopped from every table or card the+ ;; definition reads would pass PEOPLE to repair, whose error names PEOPLE's columns.+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {model-id :id} {:type :model+ :name "orders model"+ :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [reviews (lib/query mp (lib.metadata/table mp (mt/id :reviews)))+ definition (-> reviews+ (lib/join (lib/join-clause (lib.metadata/card mp model-id)+ [(lib/= (lib.metadata/field mp (mt/id :reviews :product_id))+ (lib/with-join-alias+ (lib.metadata/field mp (mt/id :orders :product_id))+ "orders model"))]))+ (lib/aggregate (lib/count)))]+ (mt/with-temp [:model/Card {metric-id :id metric-eid :entity_id} {:type :metric+ :name "reviews count"+ :dataset_query definition}]+ (verify-card! metric-id)+ (mt/with-temp-vals-in-db :model/Table (mt/id :reviews) {:is_published true :data_layer :final}+ (let [db (db-name)+ people (fn [column] ["field" {:join-alias "J"} [db "PUBLIC" "PEOPLE" column]])+ query {:lib/type "mbql/query"+ :stages [{:lib/type "mbql.stage/mbql"+ :source-table [db "PUBLIC" "REVIEWS"]+ :aggregation [["metric" {} metric-eid]]+ :breakout [(people "NO_SUCH_COLUMN")]+ :joins [{:lib/type "mbql/join"+ :fields "all"+ :strategy "left-join"+ :alias "J"+ :conditions [["=" {} ["field" {} [db "PUBLIC" "REVIEWS" "ID"]]+ (people "ID")]]+ :stages [{:lib/type "mbql.stage/mbql"+ :source-table [db "PUBLIC" "PEOPLE"]}]}]}]}+ result (try+ (as-metabot metabot :internal #(construct/execute-representations-query query))+ (catch clojure.lang.ExceptionInfo e+ (ex-data e)))]+ (is (= :uncurated-source (:error result)) (pr-str (select-keys result [:error :message])))+ (is (= [["table" (mt/id :people)]] (:uncurated result))))))))))))++(deftest run-query-conversation-query-curated-only-test+ (testing "run_query and create_autogenerated_dashboard judge a query the conversation holds, which may come from the+ user's viewing context unjudged"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :crowberto)}]+ (let [orders-count {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders) :aggregation [[:count]]}}+ with-query (fn [f]+ (as-metabot metabot :internal+ #(binding [tools.shared/*memory-atom* (atom {:conversation-id conversation-id+ :state {:queries {"adhoc" orders-count}}+ :context {}})]+ (f))))+ run (fn []+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true]+ (with-query #(tools/handle run-query/run-query-tool {:query_id "adhoc"} nil))))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content" (run)))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (with-query #(call autogen/create-autogenerated-dashboard-tool {:source {:query_id "adhoc"}}))))+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (str/includes? (:output (run)) "Count"))))))))++(deftest conversation-query-rule-runs-before-permissions-test+ (testing "the curation rule judges a conversation query before the query processor checks permissions: an+ uncurated table the user may not query gets the curation refusal, a curated one the permission error"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {published :id} {:db_id db-id :name "hidden_published" :active true+ :is_published true :data_layer :final}+ :model/Table {raw :id} {:db_id db-id :name "hidden_raw" :active true}+ :model/Field _ {:table_id published :name "A" :base_type :type/Integer}+ :model/Field _ {:table_id raw :name "A" :base_type :type/Integer}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :rasta)}]+ (mt/with-no-data-perms-for-all-users!+ (mt/with-current-user (mt/user->id :rasta)+ (let [run (fn [table-id]+ (as-metabot metabot :internal+ #(binding [tools.shared/*memory-atom*+ (atom {:conversation-id conversation-id+ :state {:queries {"adhoc" {:database db-id+ :type :query+ :query {:source-table+ table-id}}}}+ :context {}})]+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true]+ (try+ (:output (tools/handle run-query/run-query-tool {:query_id "adhoc"} nil))+ (catch clojure.lang.ExceptionInfo e+ (ex-message e)))))))]+ (is (str/includes? (run raw) "only uses curated content"))+ (let [answer (run published)]+ (is (not (str/includes? answer "only uses curated content")))+ (is (re-find #"(?i)permission" answer)))))))))++(deftest library-table-read-grant-comes-from-user-settings-test+ ;; the Library grant exists only on EE+ (when config/ee-available?+ (testing "a table published through the Data Studio keeps its published flag in its user settings, and the read+ check sees it: a user with no query permission reads it through its Library collection"+ (mt/with-temp [:model/Collection {lib :id} {:type "library-data" :name "data library"}+ :model/Table {table-id :id} {:db_id (mt/id)+ :name "published_by_settings"+ :schema "LIBRARY_SCHEMA"+ :active true+ :data_layer :final}+ :model/TableUserSettings _ {:table_id table-id+ :is_published true+ :collection_id lib}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-no-data-perms-for-all-users!+ (perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)+ (mt/with-current-user (mt/user->id :rasta)+ (let [[resource] (read-uris metabot :internal (str "metabase://table/" table-id))]+ (is (nil? (:error resource)) (:error resource))+ (is (= table-id (get-in resource [:content :structured-output :id]))))+ (testing "and the listings, which keep the tables and schemas the user can read"+ (let [items (fn [uri]+ (let [[resource] (read-uris metabot :internal uri)]+ (is (nil? (:error resource)) (:error resource))+ (into #{} (map :name) (get-in resource [:content :structured-output :items]))))+ db (str "metabase://database/" (mt/id))]+ (is (contains? (items (str db "/tables")) "published_by_settings"))+ (is (contains? (items (str db "/schemas")) "LIBRARY_SCHEMA"))+ (is (contains? (items (str db "/schemas/LIBRARY_SCHEMA/tables")) "published_by_settings"))))))))))++(deftest check-curated-query-native-stages-test+ (testing "a native stage is left to the SQL gates and the structured stages around it are judged: a native join+ does not exempt the query, and what a native stage's template tags name is not judged"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {sql-card :id} {:dataset_query (mt/native-query {:query "SELECT 1 AS X"})}]+ (let [mp (mt/metadata-provider)+ orders (orders-query)+ order-id (lib.metadata/field mp (mt/id :orders :id))+ native-join {:lib/type :mbql/join+ :alias "N"+ :strategy :left-join+ :conditions [(lib/= order-id order-id)]+ :stages [{:lib/type :mbql.stage/native :native "SELECT 1 AS ID"}]}+ joined (assoc-in orders [:stages 0 :joins] [native-join])+ sql (lib/native-query mp "SELECT 1")+ tagged (-> (lib/native-query mp (str "SELECT * FROM {{#" sql-card "}}"))+ (lib/with-template-tags {(str "#" sql-card) {:type :card+ :name (str "#" sql-card)+ :display-name (str "#" sql-card)+ :card-id sql-card+ :id (str (random-uuid))}}))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (curation/check-curated-query! "q" joined))+ "raw ORDERS is judged although the query holds a native join")+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (nil? (curation/check-curated-query! "q" joined)) "curated ORDERS passes"))+ (is (nil? (curation/check-curated-query! "q" sql)) "SQL names nothing to judge")+ (is (nil? (curation/check-curated-query! "q" tagged))+ "a saved question a SQL template tag names is the SQL gates' business"))))))++(deftest run-query-judges-a-notebook-over-a-metabot-sql-question-test+ (testing "a notebook query the viewing context holds over a SQL question Metabot saved is judged like any other,+ while SQL the query holds itself is left to the SQL gates"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :crowberto)}+ :model/Card {sql-card :id} {:dataset_query (mt/native-query {:query "SELECT 1 AS X"})+ :metabot_conversation_id conversation-id}]+ (let [mp (mt/metadata-provider)+ over-card (lib/query mp (lib.metadata/card mp sql-card))+ run (fn [query]+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true+ metabot-sql-execution-enabled? true]+ (as-metabot metabot :internal+ #(binding [tools.shared/*memory-atom*+ (atom {:conversation-id conversation-id+ :state {:queries {"q" query}}+ :context {}})+ scope/*current-user-metabot-permissions* scope/all-yes-permissions+ scope/*current-user-scope*+ (scope/user-metabot-perms->scopes scope/all-yes-permissions)]+ (tools/handle run-query/run-query-tool {:query_id "q"} nil)))))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content" (run over-card)))+ (testing "inline SQL isn't judged here"+ (is (not (str/includes? (:output (run (mt/native-query {:query "SELECT 1 AS X"}))) "curated")))))))))++(deftest trashed-measures-fail-closed-test+ (testing "a measure in the trash is never curated"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [orders-count (lib/aggregate (orders-query) (lib/count))]+ (mt/with-temp [:model/Measure {live :id} {:name "orders count" :table_id (mt/id :orders)+ :definition orders-count}+ :model/Measure {trashed :id} {:name "trashed orders count" :table_id (mt/id :orders)+ :archived true :definition orders-count}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (true? (curation/measure-or-segment-curated? :measure live)))+ (is (not (curation/measure-or-segment-curated? :measure trashed)))+ (is (denied? (first (read-uris metabot :internal (str "metabase://measure/" trashed)))))))))))++(deftest trashed-measure-fails-closed-on-the-query-path-test+ (testing "a query using a measure in the trash is refused, as a read of that measure is"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [orders-count (lib/aggregate (orders-query) (lib/count))]+ (mt/with-temp [:model/Measure {live :id} {:name "orders count" :table_id (mt/id :orders)+ :definition orders-count}+ :model/Measure {trashed :id} {:name "trashed orders count" :table_id (mt/id :orders)+ :archived true :definition orders-count}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :crowberto)}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (let [using (fn [measure-id]+ (assoc-in (orders-query) [:stages 0 :aggregation]+ [[:measure {:lib/uuid (str (random-uuid))} measure-id]]))+ run (fn [query]+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true]+ (as-metabot metabot :internal+ #(binding [tools.shared/*memory-atom*+ (atom {:conversation-id conversation-id+ :state {:queries {"q" query}}+ :context {}})]+ (tools/handle run-query/run-query-tool {:query_id "q"} nil)))))]+ (is (= [] (curation/uncurated-query-sources (using live))))+ (is (= [[:measure trashed]] (curation/uncurated-query-sources (using trashed))))+ (is (nil? (curation/check-curated-query! "q" (using live))))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (curation/check-curated-query! "q" (using trashed))))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content" (run (using trashed)))+ "run_query refuses it, which the query processor would not"))))))))++(deftest unreadable-entities-answer-the-same-whether-curated-test+ (testing "for a user who can't read the entity, every gated path answers as it would unrestricted, the same for a+ curated and an uncurated entity, so no answer reveals curation"+ (with-slack+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp [:model/Collection {official :id} {:name "official" :authority_level "official"}+ :model/Collection {plain :id} {:name "plain"}+ :model/Card {curated-card :id curated-eid :entity_id}+ {:collection_id official :type :model :name "official model" :dataset_query (orders-query)}+ :model/Card {plain-card :id plain-eid :entity_id}+ {:collection_id plain :type :model :name "plain model" :dataset_query (orders-query)}+ :model/Dashboard {curated-dash :id} {:collection_id official}+ :model/Dashboard {plain-dash :id} {:collection_id plain}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}+ :model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :rasta)}]+ (mt/with-current-user (mt/user->id :rasta)+ (let [answer (fn [id f]+ (-> (try+ (let [r (as-metabot metabot :internal #(f id))]+ (or (:output r) (:error r) (pr-str (dissoc r :structured-output))))+ (catch Exception e+ (ex-message e)))+ (str/replace (str id) "<ID>")))+ same? (fn [label f curated-id plain-id]+ (let [a (answer curated-id f)+ b (answer plain-id f)]+ (testing label+ (is (= a b))+ (is (not (str/includes? a "only uses curated content")) a))))+ run-query (fn [id]+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true]+ (binding [tools.shared/*memory-atom* (atom {:conversation-id conversation-id+ :state {:queries {}}+ :context {}})]+ (tools/handle run-query/run-query-tool {:query_id (str id)} nil))))]+ (same? "static_viz" #(call static-viz/static-viz-tool {:entity_id %}) curated-card plain-card)+ (same? "create_alert" #(call create-alert/create-alert-tool {:card_id %+ :send_condition "has_result"+ :schedule {:frequency "daily" :hour 9}}+ (slack-ctx))+ curated-card plain-card)+ (same? "create_dashboard_subscription"+ #(call subscriptions/create-dashboard-subscription-tool {:dashboard_id %+ :slack_channel "#general"+ :schedule {:frequency "daily" :hour 9}})+ curated-dash plain-dash)+ (same? "create_autogenerated_dashboard"+ #(call autogen/create-autogenerated-dashboard-tool {:source {:model_id %}}) curated-card plain-card)+ (same? "run_query" run-query curated-card plain-card)+ (same? "get_field_values" #(call metadata-tools/get-field-values-tool {:data_source "model"+ :source_id %+ :field_id (mt/id :orders :total)})+ curated-card plain-card)+ (same? "list_available_fields" #(call metadata-tools/list-available-fields-tool {:table_ids []+ :model_ids [%]+ :metric_ids []})+ curated-card plain-card)+ (same? "read_resource" (fn [id] (first (read-uris metabot :internal (str "metabase://model/" id))))+ curated-card plain-card)+ (same? "construct_notebook_query"+ #(construct/execute-representations-query {:lib/type "mbql/query"+ :stages [{:lib/type "mbql.stage/mbql" :source-card %}]})+ curated-eid plain-eid))))))))++(deftest unreadable-entities-in-listings-answer-the-same-whether-curated-test+ ;; the user reads the published table through the Library grant, which exists only on EE+ (when config/ee-available?+ (testing "a listing under curated-only judges only what the user can read, so its membership reveals nothing about+ the curation of an entity they cannot read"+ (testing "the schema listing and a model's sources, with tables the user cannot read"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {published :id} {:db_id db-id :schema "A" :name "a_published" :active true+ :is_published true :data_layer :final}+ :model/Table {raw :id} {:db_id db-id :schema "B" :name "b_raw" :active true}+ :model/Field _ {:table_id published :name "X" :base_type :type/Integer}+ :model/Field _ {:table_id raw :name "X" :base_type :type/Integer}+ :model/Collection {official :id} {:name "official" :authority_level "official"}+ :model/Card {over-published :id} {:collection_id official :type :model :name "over published"+ :database_id db-id :table_id published+ :dataset_query {:database db-id :type :query+ :query {:source-table published}}}+ :model/Card {over-raw :id} {:collection_id official :type :model :name "over raw"+ :database_id db-id :table_id raw+ :dataset_query {:database db-id :type :query+ :query {:source-table raw}}}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-no-data-perms-for-all-users!+ (mt/with-current-user (mt/user->id :rasta)+ (let [items (fn [uri]+ (let [[resource] (read-uris metabot :internal uri)]+ (is (nil? (:error resource)) (:error resource))+ (get-in resource [:content :structured-output :items])))]+ (is (empty? (items (str "metabase://database/" db-id "/schemas")))+ "neither schema is listed: the user can read no table in them")+ (is (= (map :type (items (str "metabase://model/" over-published "/sources")))+ (map :type (items (str "metabase://model/" over-raw "/sources"))))+ "the sources listings of the two models have the same shape"))))))+ (testing "the metrics embedded in a table's details, with metrics the user cannot read"+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp [:model/Collection {official :id} {:name "official" :authority_level "official"}+ :model/Collection {plain :id} {:name "plain"}+ :model/Card {curated-metric :id} {:collection_id official :type :metric :name "official count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Card {plain-metric :id} {:collection_id plain :type :metric :name "plain count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (mt/with-current-user (mt/user->id :rasta)+ (let [[resource] (read-uris metabot :internal (str "metabase://table/" (mt/id :orders)))+ metric-ids (into #{} (map :id) (get-in resource [:content :structured-output :metrics]))]+ (is (nil? (:error resource)) (:error resource))+ (is (not (contains? metric-ids curated-metric)) "an unreadable curated metric is not listed")+ (is (not (contains? metric-ids plain-metric))))))))))))++(deftest question-named-as-metric-is-refused-by-kind-test+ (testing "the metric path serves only metrics: a curated question named there is refused as the wrong kind, the same+ as for the unrestricted Metabot, and a curated metric keeps its base table, which the rule covers"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)+ reviews (lib/query mp (lib.metadata/table mp (mt/id :reviews)))]+ (mt/with-temp [:model/Collection {official :id} {:name "official" :authority_level "official"}+ :model/Card {question :id} {:collection_id official :name "reviews count"+ :dataset_query (lib/aggregate reviews (lib/count))}+ :model/Card {metric :id} {:collection_id official :type :metric :name "reviews count metric"+ :dataset_query (lib/aggregate reviews (lib/count))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (let [read (fn [mb id] (first (read-uris mb :internal (str "metabase://metric/" id))))]+ (is (str/includes? (:error (read metabot question)) "not a metric"))+ (is (= (:error (read nil question)) (:error (read metabot question))))+ (let [resource (read metabot metric)]+ (is (nil? (:error resource)) (:error resource))+ (is (= (mt/id :reviews) (get-in resource [:content :structured-output :base_table_id]))+ "a metric's base table is its source"))))))))++(deftest uncurated-metric-over-an-unreadable-table-fails-closed-test+ ;; the user reads the published table through the Library grant, which exists only on EE+ (when config/ee-available?+ (testing "an uncurated metric the user can read is judged by its definition only when they can query its table: over+ a table they cannot, it is refused whether that table is curated or not, so the answer reveals nothing"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {published :id} {:db_id db-id :name "hidden_published" :active true+ :is_published true :data_layer :final}+ :model/Table {raw :id} {:db_id db-id :name "hidden_raw" :active true}+ :model/Field _ {:table_id published :name "A" :base_type :type/Integer}+ :model/Field _ {:table_id raw :name "A" :base_type :type/Integer}+ :model/Card {over-published :id} {:type :metric :name "count over published" :database_id db-id+ :table_id published+ :dataset_query {:database db-id :type :query+ :query {:source-table published+ :aggregation [[:count]]}}}+ :model/Card {over-raw :id} {:type :metric :name "count over raw" :database_id db-id :table_id raw+ :dataset_query {:database db-id :type :query+ :query {:source-table raw :aggregation [[:count]]}}}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-no-data-perms-for-all-users!+ (mt/with-current-user (mt/user->id :rasta)+ (let [read (fn [id] (first (read-uris metabot :internal (str "metabase://metric/" id))))+ mask (fn [resource id] (str/replace (str (:error resource)) (str id) "<ID>"))]+ (is (denied? (read over-raw)))+ (is (denied? (read over-published)))+ (is (= (mask (read over-raw) over-raw) (mask (read over-published) over-published))))))))))++(deftest id-taking-tools-missing-id-reads-as-missing-test+ (with-slack+ (testing "a missing id gets the handler's own answer, not a curation verdict"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (as-metabot metabot :internal+ (fn []+ (let [missing 2147483647+ output-of (fn [f]+ (try+ (:output (f))+ (catch clojure.lang.ExceptionInfo e+ (ex-message e))))]+ (doseq [f [#(call autogen/create-autogenerated-dashboard-tool {:source {:report_id missing}})+ #(call autogen/create-autogenerated-dashboard-tool {:source {:table_id missing}})+ #(call create-alert/create-alert-tool {:card_id missing+ :send_condition "has_result"+ :schedule {:frequency "daily" :hour 9}}+ (slack-ctx))+ #(call subscriptions/create-dashboard-subscription-tool {:dashboard_id missing+ :slack_channel "#general"+ :schedule {:frequency "daily" :hour 9}})]]+ (let [text (output-of f)]+ (is (string? text))+ (is (not (str/includes? text "only uses curated content")) text)))))))))))++(deftest explorations-tools-curated-only-test+ (testing "the research catalog tools list and accept only the metrics this Metabot may use"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {raw-metric :id} {:type :metric+ :name "zz raw orders count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Card {verified-metric :id} {:type :metric+ :name "zz verified orders count"+ :dataset_query (count-metric-query (orders-query))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! verified-metric)+ (let [ours #{raw-metric verified-metric}+ metric-ids (fn [metabot profile-id tool args]+ (set/intersection ours+ (into #{}+ (map :id)+ (:metrics (json/decode+kw (:output (as-metabot metabot profile-id #(call tool args))))))))]+ (testing "list_research_metrics"+ (is (= ours (metric-ids nil nil explorations-tools/list-research-metrics-tool {:q "zz"})))+ (is (= #{verified-metric} (metric-ids metabot :internal explorations-tools/list-research-metrics-tool {:q "zz"}))))+ (testing "get_research_candidates by q"+ (is (= ours (metric-ids nil nil explorations-tools/get-research-candidates-tool {:q "zz"})))+ (is (= #{verified-metric} (metric-ids metabot :internal explorations-tools/get-research-candidates-tool {:q "zz"}))))+ (testing "get_research_candidates by id"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (as-metabot metabot :internal+ #(call explorations-tools/get-research-candidates-tool {:metric_ids [raw-metric]}))))+ (is (= #{verified-metric}+ (metric-ids metabot :internal explorations-tools/get-research-candidates-tool {:metric_ids [verified-metric]})))))))))++(deftest listings-omit-query-json-curated-only-test+ (testing "under curated-only a card's details omit its definition in list_available_fields, as in read_resource"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Card {model-id :id} {:type :model :name "verified orders model" :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (verify-card! model-id)+ (let [model-entry (fn [metabot profile-id]+ (first (get-in (as-metabot metabot profile-id+ #(call metadata-tools/list-available-fields-tool {:table_ids []+ :model_ids [model-id]+ :metric_ids []}))+ [:structured-output :models])))]+ (is (contains? (model-entry nil nil) :query_json))+ (is (not (contains? (model-entry metabot :internal) :query_json))))))))++(deftest field-values-follow-the-definition-through-cards-test+ (testing "the field-owner rule follows a card's definition through its source cards, trusts no native definition,+ and judges a metric's columns the same way"+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (let [mp (mt/metadata-provider)+ total (mt/id :orders :total)]+ (mt/with-temp [:model/Card {base-model :id} {:type :model :name "orders model" :dataset_query (orders-query)}+ :model/Card {stacked-model :id} {:type :model+ :name "model on model"+ :dataset_query (lib/query mp (lib.metadata/card mp base-model))}+ :model/Card {native-model :id} {:type :model+ :name "native model"+ :dataset_query (mt/native-query {:query "SELECT TOTAL FROM ORDERS"})}+ :model/Card {edited-native :id} {:type :model+ :name "edited native model"+ :dataset_query (mt/native-query+ {:query "SELECT TOTAL FROM ORDERS"})}+ :model/Card {edited-model :id} {:type :model :name "edited model" :dataset_query (orders-query)}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (t2/update! :model/Card :id native-model+ {:result_metadata [{:name "TOTAL" :display_name "Total" :base_type :type/Float+ :id total :table_id (mt/id :orders)}]})+ (doseq [id [edited-native edited-model]]+ (t2/update! :model/Card :id id+ {:result_metadata [{:name "RATING" :display_name "Rating" :base_type :type/Integer+ :id (mt/id :reviews :rating) :table_id (mt/id :reviews)}]}))+ (doseq [id [base-model stacked-model native-model edited-native edited-model]]+ (verify-card! id))+ (mt/with-temp [:model/Card {metric-id :id} {:type :metric+ :name "metric on edited model"+ :dataset_query (count-metric-query+ (lib/query mp (lib.metadata/card mp edited-model)))}]+ (let [values (fn [source-type source-id field-id]+ (as-metabot metabot :internal+ #(call metadata-tools/get-field-values-tool {:data_source source-type+ :source_id source-id+ :field_id field-id})))]+ (is (some? (get-in (values "model" stacked-model total) [:structured-output :value_metadata]))+ "a model built on a model reaches the tables its source reads")+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (values "model" native-model total))+ "a native definition names no tables, so none of its columns has a known owner")+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (values "model" edited-native (mt/id :reviews :rating)))+ "a native model's result metadata can point anywhere and is not trusted")+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"only uses curated content"+ (values "metric" metric-id (mt/id :reviews :rating)))+ "a metric's columns are judged by its definition, source cards included")))))))))++(deftest measure-reads-follow-their-definitions-test+ (testing "read_resource judges a measure by its table and by the tables its definition reads"+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)+ orders (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ price (first (filter #(= (:id %) (mt/id :products :price)) (lib/visible-columns orders)))]+ (mt/with-temp [:model/Measure {raw-measure :id} {:name "reviews count"+ :table_id (mt/id :reviews)+ :definition (lib/aggregate (lib/query mp (lib.metadata/table mp (mt/id :reviews)))+ (lib/count))}+ :model/Measure {hop-measure :id} {:name "orders product price"+ :table_id (mt/id :orders)+ :definition (lib/aggregate orders (lib/sum price))}+ :model/Metabot metabot {:name "curated metabot" :use_verified_content true}]+ (mt/with-temp-vals-in-db :model/Table (mt/id :orders) {:is_published true :data_layer :final}+ (is (denied? (first (read-uris metabot :internal (str "metabase://measure/" raw-measure)))))+ (is (not (denied? (first (read-uris metabot :internal (str "metabase://measure/" hop-measure)))))+ "PRODUCTS is one FK hop from curated ORDERS")+ (is (false? (curation/measure-or-segment-curated? :measure raw-measure)))+ (is (true? (curation/measure-or-segment-curated? :measure hop-measure)))))))))