Skip to content

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

[1 of 10] Hold a data app's ad-hoc queries to its cards and the Library (#85436)

Alexander Polyankin340be0e46f77
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 05efd1330df..86c6cfe76c0 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -140,6 +140,7 @@
api-scope
api.eid-translation
collections
+ data-apps
events
lib.schema
models
@@ -668,6 +669,7 @@
api-scope
api.eid-translation
collections
+ data-apps
documents
events
lib.schema
@@ -830,6 +832,7 @@
channel
collections
dashboards
+ data-apps
embedding
events
lib
@@ -866,7 +869,15 @@
data-apps
{:team "Embedding"
:api #{metabase.data-apps.core}
- :uses #{premium-features}}
+ :uses #{actions
+ analytics
+ api
+ api-scope
+ lib.be
+ lib.schema
+ parameters.schema
+ premium-features
+ util}}
data-studio
{:team "Gadget"
@@ -1531,6 +1542,7 @@
audit-app
channel
collections
+ data-apps
documents
driver
embedding
@@ -2233,6 +2245,7 @@
api-scope
api.eid-translation
collections
+ data-apps
events
lib
lib.be
@@ -2326,6 +2339,7 @@
batch-processing
cache
dashboards
+ data-apps
database-routing
driver
events
@@ -2632,6 +2646,7 @@
app-db
appearance
core.initialization-status
+ data-apps
driver
embedding
lib.be
@@ -3205,6 +3220,7 @@
metabase.user-key-value.init}
:uses #{api
api-scope
+ data-apps
lib.schema
util
util.config}}
@@ -3354,6 +3370,7 @@
api-scope
app-db
collections
+ data-apps
database-routing
driver
events
@@ -3447,6 +3464,7 @@
api-scope
app-db
collections
+ data-apps
database-routing
driver
events
@@ -3837,11 +3855,15 @@
api
api-scope
collections
+ data-apps
events
lib
+ lib.be
lib.schema
lib.types
models
+ parameters
+ parameters.schema
permissions
premium-features
remote-sync
@@ -3854,7 +3876,12 @@
:model-imports #{:model/Action
:model/Card
:model/Collection
+ :model/Dashboard
:model/DataPermissions
+ :model/Database
+ :model/Field
+ :model/Measure
+ :model/Segment
:model/Table}
:model-exports #{:model/DataApp}}
@@ -4240,6 +4267,7 @@
api-scope
app-db
audit-app
+ data-apps
driver
lib
lib.be
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
index 8885bed6372..2f97992f794 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
@@ -8,12 +8,12 @@
[metabase-enterprise.data-apps.db :as data-apps.db]
[metabase-enterprise.data-apps.group-access :as data-app.group-access]
[metabase-enterprise.data-apps.models.data-app :as data-app]
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
[metabase-enterprise.data-apps.typed-schema :as data-apps.typed-schema]
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
+ [metabase.data-apps.schema :as data-apps.schema]
[metabase.events.core :as events]
[metabase.remote-sync.core :as remote-sync]
[metabase.util.i18n :refer [tru]]
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/checks.clj b/enterprise/backend/src/metabase_enterprise/data_apps/checks.clj
new file mode 100644
index 00000000000..417adbb36f7
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/checks.clj
@@ -0,0 +1,242 @@
+(ns metabase-enterprise.data-apps.checks
+ "The checks that hold a data app's ad-hoc queries and card runs to its own cards and the Library, and its action runs
+ to the actions in its collection: no native or internal queries, cards from the app's collection, tables published to
+ the Library, Library metrics, segments and measures of published tables, and actions from the app's collection."
+ (:require
+ [clojure.set :as set]
+ [clojure.string :as str]
+ [metabase-enterprise.data-apps.db :as data-apps.db]
+ [metabase.actions.schema :as actions.schema]
+ [metabase.api.common :as api]
+ [metabase.collections.core :as collections]
+ [metabase.data-apps.core :as data-apps]
+ [metabase.data-apps.schema :as data-apps.schema]
+ [metabase.lib-be.schema :as lib-be.schema]
+ [metabase.lib.core :as lib]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.lib.schema.parameter :as lib.schema.parameter]
+ [metabase.parameters.params :as params]
+ [metabase.parameters.schema :as parameters.schema]
+ [metabase.premium-features.core :refer [defenterprise defenterprise-schema]]
+ [metabase.util.i18n :refer [tru]]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+(defn- data-app-error
+ "A 400 error for a data app's request that breaks a data app rule, with `message`."
+ [message]
+ (ex-info message {:status-code 400}))
+
+(defenterprise request-data-app
+ "The data app whose slug a data-app `request` names by [[data-apps/data-app-slug]], read-checked, so a missing app is
+ a 404 and an unreadable one a 403; nil for a request that is not a data app's."
+ :feature :data-apps
+ [request]
+ (when (data-apps/data-app-request? request)
+ (api/read-check (some-> (data-apps/data-app-slug request) data-apps.db/data-app-by-slug))))
+
+(defn- format-ids
+ "`ids` in order, for an error message."
+ [ids]
+ (str/join ", " (sort ids)))
+
+(defn- in-app-collection?
+ "Whether `entity` is in `data-app`'s collection."
+ [data-app entity]
+ (let [collection-id (:resource_collection_id data-app)]
+ (and (some? collection-id) (= collection-id (:collection_id entity)))))
+
+(defenterprise-schema check-query :- :nil
+ "Throws a 400 unless `query`, as the `/api/dataset` endpoints normalize it, is neither internal nor native and uses only
+ cards in `data-app`'s collection, tables published to the Library (joins included), Library metrics, and segments and
+ measures of published tables."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ query :- ::lib-be.schema/maybe-legacy-or-internal-query]
+ (when (= :internal (keyword (:type query)))
+ (throw (data-app-error (tru "A data app cannot run internal queries."))))
+ (let [metric-ids (lib/all-metric-ids query)
+ card-ids (set/difference (set (lib/all-source-card-ids query)) metric-ids)]
+ (when (lib/any-native-stage? query)
+ (throw (data-app-error (tru "A data app cannot run native queries."))))
+ (when-let [external (some->> (not-empty card-ids)
+ (data-apps.db/external-card-ids (:resource_collection_id data-app))
+ not-empty)]
+ (throw (data-app-error (tru "A data app can use only the cards in its own collection, not the cards {0}."
+ (format-ids external)))))
+ (when-let [external (some-> metric-ids data-apps.db/external-metric-ids not-empty)]
+ (throw (data-app-error (tru "A data app can use only metrics from the Library, not the metrics {0}." (format-ids external)))))
+ (when-let [external (some-> (lib/all-source-table-ids query) data-apps.db/external-table-ids not-empty)]
+ (throw (data-app-error (tru "A data app can read only tables published to the Library, not the tables {0}."
+ (format-ids external)))))
+ (when-let [external (some-> (lib/all-segment-ids query) data-apps.db/external-segment-ids not-empty)]
+ (throw (data-app-error (tru "A data app can use only segments of tables published to the Library, not the segments {0}."
+ (format-ids external)))))
+ (when-let [external (some-> (lib/all-measure-ids query) data-apps.db/external-measure-ids not-empty)]
+ (throw (data-app-error (tru "A data app can use only measures of tables published to the Library, not the measures {0}."
+ (format-ids external))))))
+ nil)
+
+(defn- in-library?
+ "Whether `entity` is in the Library or one of its collections."
+ [entity]
+ (contains? collections/library-collection-types (-> entity (t2/hydrate :collection) :collection :type)))
+
+(defn- read-checked-app-card
+ "The card with `card-id`, after a read check; throws a 400 unless it is in `data-app`'s collection or in the Library."
+ [data-app card-id]
+ (let [card (api/read-check :model/Card card-id)]
+ (when-not (or (in-app-collection? data-app card) (in-library? card))
+ (throw (data-app-error (tru "A data app can use only the cards in its own collection and in the Library, not the card {0}."
+ (str card-id)))))
+ card))
+
+(defn- check-values-source-card
+ "Throws a 400 unless the card with `card-id` is one `data-app` may use and not a dashboard question, after a read
+ check of the card."
+ [data-app card-id]
+ (when (:dashboard_id (read-checked-app-card data-app card-id))
+ (throw (data-app-error (tru "A data app cannot take parameter values from the dashboard question {0}." (str card-id))))))
+
+(defenterprise-schema check-card :- :nil
+ "Throws a 400 unless the card with `card-id` is in `data-app`'s collection or in the Library, after a read check of
+ the card."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ card-id :- ::lib.schema.id/card]
+ (read-checked-app-card data-app card-id)
+ nil)
+
+(defn- published-table?
+ "Whether the table with `table-id` is published to the Library."
+ [table-id]
+ (empty? (data-apps.db/external-table-ids #{table-id})))
+
+(defn- check-published-field
+ "Throws a 400 unless the field with `field-id` belongs to a table published to the Library, after a read check of
+ the field."
+ [field-id]
+ (when-not (published-table? (:table_id (api/read-check :model/Field field-id)))
+ (throw (data-app-error (tru "A data app can read only fields of tables published to the Library, not the field {0}."
+ (str field-id))))))
+
+(defenterprise-schema check-card-parameter :- :nil
+ "Throws a 400 unless the card with `card-id` is in `data-app`'s collection or the Library and its parameter
+ `param-key` takes its values from a static list, such a card other than a dashboard question, or a field of a table
+ published to the Library, after read checks of each."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ card-id :- ::lib.schema.id/card
+ param-key :- ::lib.schema.parameter/id]
+ (let [card (read-checked-app-card data-app card-id)
+ parameter (some #(when (= param-key (:id %)) %) (:parameters card))]
+ (case (some-> (:values_source_type parameter) keyword)
+ :static-list nil
+ :card (some->> (get-in parameter [:values_source_config :card_id]) (check-values-source-card data-app))
+ (some-> (params/param-target->field-id (:target parameter) card) check-published-field)))
+ nil)
+
+(defenterprise-schema check-parameter :- :nil
+ "Throws a 400 unless the ad-hoc `parameter` takes its values from a static list, a card `data-app` may use other than
+ a dashboard question, or the fields with `field-ids`, each of a table published to the Library, after read checks
+ of each."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ parameter :- ::parameters.schema/parameter
+ field-ids :- [:maybe [:sequential ::lib.schema.id/field]]]
+ (case (some-> (:values_source_type parameter) keyword)
+ :static-list nil
+ :card (some->> (get-in parameter [:values_source_config :card_id]) (check-values-source-card data-app))
+ (run! check-published-field field-ids))
+ nil)
+
+(defenterprise-schema check-action :- :nil
+ "Throws a 400 unless the action with `action-id` is in `data-app`'s collection, after a read check of the action."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ action-id :- ::lib.schema.id/action]
+ (when-not (in-app-collection? data-app (api/read-check :model/Action action-id))
+ (throw (data-app-error (tru "A data app can run only the actions in its own collection, not the action {0}."
+ (str action-id)))))
+ nil)
+
+(defenterprise-schema data-app-actions :- [:sequential ::actions.schema/action]
+ "The `actions` in `data-app`'s collection."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ actions :- [:sequential ::actions.schema/action]]
+ (filterv (partial in-app-collection? data-app) actions))
+
+(defenterprise-schema check-table :- :nil
+ "Throws a 400 unless the table with `table-id` is published to the Library, after a read check of the table."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app
+ table-id :- ::lib.schema.id/table]
+ (api/read-check :model/Table table-id)
+ (when-not (published-table? table-id)
+ (throw (data-app-error (tru "A data app can read only tables published to the Library, not the table {0}." (str table-id)))))
+ nil)
+
+(defenterprise-schema check-field :- :nil
+ "Throws a 400 unless the field with `field-id` belongs to a table published to the Library, after a read check of
+ the field."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app
+ field-id :- ::lib.schema.id/field]
+ (check-published-field field-id)
+ nil)
+
+(defenterprise-schema check-database :- :nil
+ "Throws a 400 unless the database with `database-id` has a table published to the Library, after a read check of the
+ database."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app
+ database-id :- ::lib.schema.id/database]
+ (api/read-check :model/Database database-id)
+ (when-not (some #(= database-id (:db_id %)) (data-apps.db/published-tables))
+ (throw (data-app-error (tru "A data app can read only databases with tables published to the Library, not the database {0}."
+ (str database-id)))))
+ nil)
+
+(def ^:private sdk-user-key-values
+ "The user key-values, as `[namespace key]`, that the SDK reads and writes inside a data app."
+ #{["last_download_format" "download_format_preference"]})
+
+(defenterprise-schema check-user-key-value :- :nil
+ "Throws a 400 unless the user key-value `k` in `nmspace` is one the SDK uses inside a data app."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app
+ nmspace :- :string
+ k :- :string]
+ (when-not (contains? sdk-user-key-values [nmspace k])
+ (throw (data-app-error (tru "A data app can use only the user key-values of the SDK, not the key {0} in the namespace {1}."
+ k nmspace))))
+ nil)
+
+(defenterprise-schema data-app-collection-types :- [:maybe [:set :string]]
+ "The types of the Library and of its collections, which their subcollections inherit."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app]
+ collections/library-collection-types)
+
+(defenterprise-schema check-collection :- :nil
+ "Throws a 400 unless the collection with `collection-id` is of a type in [[data-app-collection-types]], after a read
+ check of the collection."
+ :feature :data-apps
+ [data-app :- ::data-apps.schema/data-app
+ collection-id :- ::lib.schema.id/collection]
+ (when-not (contains? (data-app-collection-types data-app) (:type (api/read-check :model/Collection collection-id)))
+ (throw (data-app-error (tru "A data app can see only the Library and its collections, not the collection {0}."
+ (str collection-id)))))
+ nil)
+
+(defenterprise-schema check-dashboard :- :nil
+ "Throws a 400 unless the dashboard with `dashboard-id` is in the Library, after a read check of the dashboard."
+ :feature :data-apps
+ [_data-app :- ::data-apps.schema/data-app
+ dashboard-id :- ::lib.schema.id/dashboard]
+ (when-not (in-library? (api/read-check :model/Dashboard dashboard-id))
+ (throw (data-app-error (tru "A data app can use only the dashboards in the Library, not the dashboard {0}."
+ (str dashboard-id)))))
+ nil)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/config.clj b/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
index 2ab6f134b28..e0d3583cd10 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/config.clj
@@ -1,7 +1,7 @@
(ns metabase-enterprise.data-apps.config
"Constants of a data app's manifest, which a serialized app keeps in the `data_app.yaml` in its own directory
under `data_apps/`; the columns' schemas are in
- [[metabase-enterprise.data-apps.schema]]:
+ [[metabase.data-apps.schema]]:
slug: sales # the slug the app is served at (column `name`)
name: Sales dashboard # display name (column `display_name`)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
index 3c0094d21f6..4a66546d67d 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj
@@ -1,9 +1,10 @@
(ns metabase-enterprise.data-apps.db
- "Application database queries for the data-apps module. Every function here is a direct Toucan 2 call with no
- additional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."
+ "Application database queries for the data-apps module, so the rest of the module only touches `toucan2.core` for
+ model definitions and hydration methods."
(:require
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
+ [clojure.set :as set]
[metabase.collections.models.collection :as collection]
+ [metabase.data-apps.schema :as data-apps.schema]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -167,3 +168,49 @@
[collection-id :- ::lib.schema.id/collection]
(t2/select-pks-vec :model/Action {:where [:and [:= :collection_id collection-id] [:= :archived false]]
:order-by [[:name :asc] [:id :asc]]}))
+
+(mu/defn external-card-ids :- [:set ::lib.schema.id/card]
+ "Among `card-ids`, those of no Card in the collection with `collection-id`."
+ [collection-id :- [:maybe ::lib.schema.id/collection]
+ card-ids :- [:set {:min 1} ::lib.schema.id/card]]
+ (set/difference card-ids (when collection-id
+ (t2/select-pks-set :model/Card :id [:in card-ids] :collection_id collection-id))))
+
+(mu/defn external-metric-ids :- [:set ::lib.schema.id/card]
+ "Among `card-ids`, those of no metric in the Library's Metrics collection or one of its subcollections."
+ [card-ids :- [:set {:min 1} ::lib.schema.id/card]]
+ (set/difference card-ids (t2/select-fn-set :id [:model/Card :report_card.id]
+ {:join [:collection [:= :collection.id :report_card.collection_id]]
+ :where [:and
+ [:in :report_card.id card-ids]
+ [:= :report_card.type "metric"]
+ [:= :collection.type collection/library-metrics-collection-type]]})))
+
+(mu/defn external-table-ids :- [:set ::lib.schema.id/table]
+ "Among `table-ids`, those of no Table published to the Library."
+ [table-ids :- [:set {:min 1} ::lib.schema.id/table]]
+ (set/difference table-ids (t2/select-pks-set :model/Table {:from [(warehouse-schema-overlay/table-query)]
+ :where [:and [:in :id table-ids] [:= :is_published true]]})))
+
+(mu/defn published-tables :- [:sequential :metabase.warehouse-schema.schema/table]
+ "The Tables published to the Library, with their databases and schemas."
+ []
+ (t2/select [:model/Table :id :db_id :schema]
+ {:from [(warehouse-schema-overlay/table-query)]
+ :where [:= :is_published true]}))
+
+(mu/defn external-segment-ids :- [:set ::lib.schema.id/segment]
+ "Among `segment-ids`, those of no Segment of a Table published to the Library."
+ [segment-ids :- [:set {:min 1} ::lib.schema.id/segment]]
+ (set/difference segment-ids (t2/select-fn-set :id [:model/Segment :segment.id]
+ {:join [(warehouse-schema-overlay/table-query {:alias :table})
+ [:= :table.id :segment.table_id]]
+ :where [:and [:in :segment.id segment-ids] [:= :table.is_published true]]})))
+
+(mu/defn external-measure-ids :- [:set ::lib.schema.id/measure]
+ "Among `measure-ids`, those of no Measure of a Table published to the Library."
+ [measure-ids :- [:set {:min 1} ::lib.schema.id/measure]]
+ (set/difference measure-ids (t2/select-fn-set :id [:model/Measure :measure.id]
+ {:join [(warehouse-schema-overlay/table-query {:alias :table})
+ [:= :table.id :measure.table_id]]
+ :where [:and [:in :measure.id measure-ids] [:= :table.is_published true]]})))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/init.clj b/enterprise/backend/src/metabase_enterprise/data_apps/init.clj
index 2a02e6ec974..f19e3bb925c 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/init.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/init.clj
@@ -2,6 +2,7 @@
(:require
[metabase-enterprise.data-apps.api]
[metabase-enterprise.data-apps.apps]
+ [metabase-enterprise.data-apps.checks]
[metabase-enterprise.data-apps.config]
[metabase-enterprise.data-apps.csp]
[metabase-enterprise.data-apps.models.data-app]))
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj
index 9dcf4048b79..d51e0723eec 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj
@@ -2,8 +2,8 @@
(:require
[metabase-enterprise.data-apps.config :as data-app.config]
[metabase-enterprise.data-apps.db :as data-apps.db]
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
[metabase.api.common :as api]
+ [metabase.data-apps.schema :as data-apps.schema]
[metabase.events.core :as events]
[metabase.lib.core :as lib]
[metabase.models.interface :as mi]
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
index 9735db4aef0..55b0a4c112f 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
@@ -240,7 +240,7 @@
:metabase.segments.schema/segment
:metabase.measures.schema/measure
::actions.schema/action
- :metabase-enterprise.data-apps.schema/data-app])
+ :metabase.data-apps.schema/data-app])
(mu/defn- entity-value :- ::entity
[entity-type :- ::deps.dependency-types/dependency-types
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj
index edd3bf20669..29aaa564622 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj
@@ -40,7 +40,7 @@
:metabase.measures.schema/measure
::actions.schema/action
[:merge
- :metabase-enterprise.data-apps.schema/data-app
+ :metabase.data-apps.schema/data-app
[:map
[:card_ids [:set ::lib.schema.id/card]]
[:action_ids [:set ::lib.schema.id/action]]]]])
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/api/table.clj b/enterprise/backend/src/metabase_enterprise/sandbox/api/table.clj
index 8d0406bf8ff..382d0fbe032 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/api/table.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/api/table.clj
@@ -6,6 +6,7 @@
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.data-apps.core :as data-apps]
[metabase.permissions.core :as perms]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util :as u]
@@ -85,7 +86,10 @@
:- [:map {:closed true}
[:include_sensitive_fields {:default false} [:maybe ms/BooleanValue]]
[:include_hidden_fields {:default false} [:maybe ms/BooleanValue]]
- [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]]]
+ [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-table id))
(fetch-table-query-metadata id {:include-sensitive-fields? include_sensitive_fields
:include-hidden-fields? include_hidden_fields
:include-editable-data-model? include_editable_data_model}))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/checks_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/checks_test.clj
new file mode 100644
index 00000000000..56fcef96b96
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/checks_test.clj
@@ -0,0 +1,913 @@
+(ns metabase-enterprise.data-apps.checks-test
+ "The checks that hold a data app's ad-hoc queries and card runs to its own cards and the Library, and its action runs
+ to its own actions, through the endpoints that run them."
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [metabase-enterprise.data-apps.test-util :as data-apps.tu :refer [do-with-app-collection]]
+ [metabase.collections.core :as collections]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.search.test-util :as search.tu]
+ [metabase.test :as mt]
+ [metabase.util.json :as json]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+(defn- as-app
+ "Request options that mark a request as the data app's named `app-name`, or as an unnamed data app's when nil."
+ [app-name]
+ {:request-options {:headers (cond-> {"x-metabase-client" "data-app"}
+ app-name (assoc "x-metabase-client-identifier" app-name))}})
+
+(def ^:private as-data-app
+ "Request options that mark a request as from the data app of [[do-with-app-collection]]."
+ (as-app "birds"))
+
+(def ^:private error-formats
+ "The format of the error message for each kind of entity a data app may not use."
+ {:cards "A data app can use only the cards in its own collection, not the cards %s."
+ :metrics "A data app can use only metrics from the Library, not the metrics %s."
+ :tables "A data app can read only tables published to the Library, not the tables %s."
+ :segments "A data app can use only segments of tables published to the Library, not the segments %s."
+ :measures "A data app can use only measures of tables published to the Library, not the measures %s."})
+
+(defn- error-message
+ "The error message for a data app's use of the entities of `kind` with `ids`."
+ [kind & ids]
+ (format (error-formats kind) (str/join ", " (sort ids))))
+
+(defn- card-error
+ "The error message for a data app's run of the card with `card-id`."
+ [card-id]
+ (format "A data app can use only the cards in its own collection and in the Library, not the card %d." card-id))
+
+(defn- action-error
+ "The error message for a data app's run of the action with `action-id`."
+ [action-id]
+ (format "A data app can run only the actions in its own collection, not the action %d." action-id))
+
+(defn- run-query
+ "The response to a data app's request to run `query` through `POST /api/dataset`, expecting `status`."
+ ([status query]
+ (run-query status query as-data-app))
+ ([status query options]
+ (mt/user-http-request :crowberto :post status "dataset" options query)))
+
+(defn- table-query
+ "A query of the table named `table-key`."
+ [table-key]
+ (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/table mp (mt/id table-key)))))
+
+(defn- card-query
+ "A query whose source is the card with `card-id`."
+ [card-id]
+ (let [mp (mt/metadata-provider)]
+ (lib/query mp (lib.metadata/card mp card-id))))
+
+(defn- join
+ "`query` joined to `joinable`, on the conditions the foreign keys suggest."
+ [query joinable]
+ (lib/join query (lib/join-clause joinable)))
+
+(defn- do-with-app-card!
+ "Call `f` with the ID of a card on the published VENUES table in the collection of the data app of
+ [[do-with-app-collection]], and with that collection's ID."
+ [f]
+ (do-with-app-collection
+ (fn [collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (mt/with-temp [:model/Card {card-id :id} {:name "Venues"
+ :collection_id collection-id
+ :dataset_query (data-apps.tu/venues-query)}]
+ (f card-id collection-id))))))
+
+(deftest app-card-and-published-table-run-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (doseq [[label query] [["the app's card" (lib/limit (card-query card-id) 1)]
+ ["a published table" (lib/limit (table-query :venues) 1)]]]
+ (testing (str "a query on " label " runs, pivots, compiles and has metadata")
+ (is (= "completed" (:status (run-query 202 query))))
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202 "dataset/pivot" as-data-app
+ (lib/aggregate query (lib/count))))))
+ (is (string? (:query (mt/user-http-request :crowberto :post 200 "dataset/native" as-data-app query))))
+ (is (seq (:tables (mt/user-http-request :crowberto :post 200 "dataset/query_metadata" as-data-app query)))))
+ (testing (str "an export of a query on " label ", which a form submission sends as a JSON string")
+ (is (mt/user-http-request :crowberto :post 200 "dataset/csv"
+ (assoc-in as-data-app [:request-options :content-type]
+ "application/x-www-form-urlencoded")
+ {:query (json/encode (lib/->legacy-MBQL query))})))))))
+
+(deftest request-must-name-readable-app-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (testing "a data app's request that names no app, or an app that doesn't exist, is not found"
+ (doseq [options [(as-app nil) (as-app "no-such-app")]]
+ (is (= "Not found." (run-query 404 (card-query card-id) options)))))
+ (testing "an app the user can't read is refused"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :post 403 "dataset" as-data-app (card-query card-id))))))))
+
+(deftest unpublished-tables-are-refused-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (testing "every endpoint refuses a query on an unpublished table"
+ (is (= (error-message :tables (mt/id :categories)) (run-query 400 (table-query :categories))))
+ (doseq [endpoint ["dataset/pivot" "dataset/native" "dataset/query_metadata"]]
+ (is (= (error-message :tables (mt/id :categories)) (mt/user-http-request :crowberto :post 400 endpoint as-data-app
+ (table-query :categories)))
+ endpoint))
+ (is (= (error-message :tables (mt/id :categories)) (mt/user-http-request :crowberto :post 400 "dataset/csv" as-data-app
+ {:query (table-query :categories)}))))
+ (testing "a join of an unpublished table is refused, from a published table or the app's card"
+ (doseq [query [(table-query :venues) (card-query card-id)]]
+ (is (= (error-message :tables (mt/id :categories))
+ (run-query 400 (join query (lib.metadata/table (mt/metadata-provider) (mt/id :categories))))))))
+ (testing "the join runs once its table is published"
+ (data-apps.tu/with-published-tables! [(mt/id :categories)]
+ (is (= "completed" (:status (run-query 202 (-> (table-query :venues)
+ (join (lib.metadata/table (mt/metadata-provider)
+ (mt/id :categories)))
+ (lib/limit 1)))))))))))
+
+(deftest other-cards-are-refused-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (mt/with-temp [:model/DataApp {other-collection-id :resource_collection_id} {:name "bees"
+ :display_name "Bees"
+ :bundle_path "bees.js"}
+ :model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}
+ :model/Card {categories-id :id} {:name "Categories elsewhere" :dataset_query (table-query :categories)}
+ :model/Card {other-app-id :id} {:name "Venues of bees"
+ :collection_id other-collection-id
+ :dataset_query (data-apps.tu/venues-query)}]
+ (testing "a card outside the app's collection, or of another app, is refused"
+ (doseq [other-id [elsewhere-id other-app-id]]
+ (is (= (error-message :cards other-id)
+ (run-query 400 (card-query other-id))))))
+ (testing "a join of a card outside the app's collection is refused"
+ (is (= (error-message :cards categories-id)
+ (run-query 400 (join (card-query card-id)
+ (lib.metadata/card (mt/metadata-provider) categories-id))))))
+ (testing "the other app runs its own card"
+ (is (= "completed" (:status (run-query 202 (lib/limit (card-query other-app-id) 1) (as-app "bees"))))))))))
+
+(deftest app-without-collection-uses-no-card-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (mt/with-temp [:model/DataApp app {:name "wasps"}
+ :model/Card {elsewhere-id :id} {:dataset_query (data-apps.tu/venues-query)}]
+ (t2/delete! :model/Collection (:resource_collection_id app))
+ (testing "an app without a collection can use no card, not even one in the root collection"
+ (is (= (error-message :cards elsewhere-id)
+ (run-query 400 (card-query elsewhere-id) (as-app "wasps")))))
+ (testing "it still reads published tables"
+ (is (= "completed" (:status (run-query 202 (lib/limit (table-query :venues) 1) (as-app "wasps"))))))))))
+
+(deftest missing-entities-are-refused-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (testing "an ID of no entity gets the refusal an entity the app may not use gets, so it tells nothing of what exists"
+ (doseq [[kind query] [[:tables {:database (mt/id) :type :query :query {:source-table 999999999}}]
+ [:cards {:database (mt/id) :type :query :query {:source-table "card__999999999"}}]
+ [:metrics {:database (mt/id) :type :query :query {:source-table (mt/id :venues)
+ :aggregation [[:metric 999999999]]}}]
+ [:segments {:database (mt/id) :type :query :query {:source-table (mt/id :venues)
+ :filter [:segment 999999999]}}]
+ [:measures {:database (mt/id) :type :query :query {:source-table (mt/id :venues)
+ :aggregation [[:measure 999999999]]}}]]]
+ (is (= (error-message kind 999999999) (run-query 400 query)) (name kind)))))))
+
+(deftest native-query-is-refused-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (testing "in its MBQL 5, legacy and nested legacy forms"
+ (doseq [query [(lib/native-query (mt/metadata-provider) "SELECT * FROM VENUES")
+ {:database (mt/id) :type :native :native {:query "SELECT * FROM VENUES"}}
+ {:database (mt/id)
+ :type :query
+ :query {:source-query {:native "SELECT * FROM VENUES"}}}]]
+ (is (re-find #"cannot run native queries" (run-query 400 query)))))
+ (testing "a request that is not a data app's still runs native SQL"
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202 "dataset"
+ (lib/native-query (mt/metadata-provider) "SELECT 1")))))))))
+
+(deftest internal-query-is-refused-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (is (re-find #"cannot run internal queries"
+ (run-query 400 {:type :internal
+ :fn "metabase-enterprise.audit-app.pages.dashboards/most-popular-with-avg-speed"}))))))
+
+(deftest metrics-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (data-apps.tu/do-with-sources!
+ (fn [{:keys [metric-id]}]
+ (testing "a Library metric runs"
+ (is (= "completed" (:status (run-query 202 (lib/aggregate (card-query card-id)
+ (lib.metadata/metric (mt/metadata-provider)
+ metric-id)))))))
+ (mt/with-temp [:model/Card {outside-id :id} {:name "Venue count elsewhere"
+ :type :metric
+ :database_id (mt/id)
+ :dataset_query (lib/aggregate (table-query :venues) (lib/count))}]
+ (testing "a metric outside the Library is refused"
+ (is (= (error-message :metrics outside-id)
+ (run-query 400 (lib/aggregate (card-query card-id)
+ (lib.metadata/metric (mt/metadata-provider) outside-id))))))))))))
+
+(deftest segments-and-measures-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (let [categories (table-query :categories)
+ name-col (some #(when (= (mt/id :categories :name) (:id %)) %) (lib/filterable-columns categories))]
+ (mt/with-temp [:model/Segment {segment-id :id} {:name "Bars"
+ :table_id (mt/id :categories)
+ :definition (lib/filter categories (lib/= name-col "Bar"))}
+ :model/Measure {measure-id :id} {:name "Names"
+ :table_id (mt/id :categories)
+ :definition (lib/aggregate categories (lib/distinct name-col))}]
+ (let [mp (mt/metadata-provider)
+ with-segment #(lib/filter % (lib.metadata/segment mp segment-id))
+ with-measure #(lib/aggregate % (lib.metadata/measure mp measure-id))]
+ (testing "a segment or measure of an unpublished table is refused, even from a published table"
+ (is (= (error-message :segments segment-id) (run-query 400 (with-segment (table-query :venues)))))
+ (is (= (error-message :measures measure-id) (run-query 400 (with-measure (table-query :venues))))))
+ (testing "a segment or measure of a published table runs"
+ (data-apps.tu/with-published-tables! [(mt/id :categories)]
+ (doseq [query [(with-segment categories) (with-measure categories)]]
+ (is (= "completed" (:status (run-query 202 query)))))))))))))
+
+(deftest card-endpoints-run-app-card-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (testing "the app's card runs, pivots and exports"
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202 (format "card/%d/query" card-id)
+ as-data-app))))
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202 (format "card/pivot/%d/query" card-id)
+ as-data-app))))
+ (is (some? (mt/user-http-request :crowberto :post 200 (format "card/%d/query/csv" card-id) as-data-app))))
+ (testing "by its entity ID too"
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202
+ (format "card/%s/query"
+ (t2/select-one-fn :entity_id :model/Card :id card-id))
+ as-data-app))))))))
+
+(deftest card-endpoints-refuse-other-cards-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (mt/with-temp [:model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}]
+ (testing "a card outside the app's collection is refused"
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) as-data-app)))
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "card/pivot/%d/query" elsewhere-id) as-data-app)))
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "card/%d/query/csv" elsewhere-id) as-data-app))))
+ (testing "an app without a collection runs no card, not even one in the root collection"
+ (mt/with-temp [:model/DataApp app {:name "wasps"}]
+ (t2/delete! :model/Collection (:resource_collection_id app))
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) (as-app "wasps"))))))
+ (testing "an ID of no card is not found, as for any request"
+ (is (= "Not found." (mt/user-http-request :crowberto :post 404 "card/999999999/query" as-data-app))))
+ (testing "a request that is not a data app's still runs the card"
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202
+ (format "card/%d/query" elsewhere-id))))))))))
+
+(deftest card-read-endpoints-check-card-test
+ (do-with-app-card!
+ (fn [card-id _collection-id]
+ (mt/with-temp [:model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}]
+ (testing "the app's card and its metadata are readable"
+ (is (=? {:id card-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" card-id) as-data-app)))
+ (is (map? (mt/user-http-request :crowberto :get 200 (format "card/%d/query_metadata" card-id) as-data-app)))
+ (is (=? {:id (str "card__" card-id)}
+ (mt/user-http-request :crowberto :get 200 (format "table/card__%d/query_metadata" card-id) as-data-app))))
+ (testing "a card outside the app's collection is refused"
+ (doseq [url ["card/%d" "card/%d/query_metadata" "table/card__%d/query_metadata"]]
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format url elsewhere-id) as-data-app))
+ url)))
+ (testing "a request that is not a data app's still reads it"
+ (is (=? {:id elsewhere-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" elsewhere-id)))))))))
+
+(def ^:private static-parameter
+ "A parameter whose values are a static list, so loading them runs no query."
+ {:id "kind"
+ :slug "kind"
+ :name "Kind"
+ :type :string/=
+ :values_source_type :static-list
+ :values_source_config {:values ["bar" "cafe"]}})
+
+(deftest card-parameter-endpoints-check-card-test
+ (do-with-app-collection
+ (fn [collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (mt/with-temp [:model/Card {app-card-id :id} {:collection_id collection-id
+ :parameters [static-parameter]
+ :dataset_query (data-apps.tu/venues-query)}
+ :model/Card {elsewhere-id :id} {:parameters [static-parameter]
+ :dataset_query (data-apps.tu/venues-query)}]
+ (testing "the app's card loads its parameter values"
+ (is (=? {:values [["bar"] ["cafe"]]}
+ (mt/user-http-request :crowberto :get 200 (format "card/%d/params/kind/values" app-card-id) as-data-app)))
+ (is (=? {:values [["cafe"]]}
+ (mt/user-http-request :crowberto :get 200 (format "card/%d/params/kind/search/caf" app-card-id)
+ as-data-app))))
+ (testing "a card outside the app's collection is refused"
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format "card/%d/params/kind/values" elsewhere-id) as-data-app)))
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format "card/%d/params/kind/search/caf" elsewhere-id)
+ as-data-app)))
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format "card/%d/params/kind/remapping?value=bar" elsewhere-id)
+ as-data-app))))
+ (testing "a request that is not a data app's still loads the card's parameter values"
+ (is (=? {:values [["bar"] ["cafe"]]}
+ (mt/user-http-request :crowberto :get 200 (format "card/%d/params/kind/values" elsewhere-id))))))))))
+
+(defn- legacy-field-ref
+ "The legacy ref of the field with `field-id`, as parameters store it."
+ [field-id]
+ (let [mp (mt/metadata-provider)]
+ (lib/->legacy-MBQL (lib/ref (lib.metadata/field mp field-id)))))
+
+(defn- card-source-parameter
+ "A parameter whose values come from the names of the venues card with `card-id`."
+ [card-id]
+ {:id "venue"
+ :slug "venue"
+ :name "Venue"
+ :type :string/=
+ :values_source_type :card
+ :values_source_config {:card_id card-id :value_field (legacy-field-ref (mt/id :venues :name))}})
+
+(deftest card-endpoints-allow-library-metrics-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (data-apps.tu/do-with-sources!
+ (fn [{:keys [metric-id]}]
+ (testing "a Library metric, its metadata and its run are open to a data app"
+ (is (=? {:id metric-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" metric-id) as-data-app)))
+ (is (map? (mt/user-http-request :crowberto :get 200 (format "card/%d/query_metadata" metric-id) as-data-app)))
+ (is (=? {:id (str "card__" metric-id)}
+ (mt/user-http-request :crowberto :get 200 (format "table/card__%d/query_metadata" metric-id) as-data-app)))
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202 (format "card/%d/query" metric-id)
+ as-data-app)))))
+ (mt/with-temp [:model/Card {outside-id :id} {:name "Venue count elsewhere"
+ :type :metric
+ :database_id (mt/id)
+ :dataset_query (lib/aggregate (data-apps.tu/venues-query) (lib/count))}]
+ (testing "a metric outside the Library is refused"
+ (doseq [url ["card/%d" "card/%d/query_metadata" "table/card__%d/query_metadata"]]
+ (is (= (card-error outside-id)
+ (mt/user-http-request :crowberto :get 400 (format url outside-id) as-data-app))
+ url))))
+ (let [library-id (:id (collections/library-collection))
+ dashboards-id (t2/select-one-pk :model/Collection
+ :location (str "/" library-id "/")
+ :type collections/library-dashboards-collection-type)]
+ (mt/with-temp [:model/Dashboard dashboard {:collection_id dashboards-id}
+ :model/Card {question-id :id} {:collection_id dashboards-id
+ :dashboard_id (:id dashboard)
+ :dataset_query (data-apps.tu/venues-query)}]
+ (testing "a dashboard question in the Library is open to the card endpoints"
+ (is (=? {:id question-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" question-id) as-data-app)))
+ (is (=? {:id (str "card__" question-id)}
+ (mt/user-http-request :crowberto :get 200 (format "table/card__%d/query_metadata" question-id)
+ as-data-app))))
+ (testing "but not as a source of parameter values"
+ (is (= (format "A data app cannot take parameter values from the dashboard question %d." question-id)
+ (mt/user-http-request :crowberto :post 400 "dataset/parameter/values" as-data-app
+ {:parameter (card-source-parameter question-id)}))))
+ (testing "nor as a source card of a query"
+ (is (= (error-message :cards question-id)
+ (run-query 400 (card-query question-id))))))))))))
+
+(deftest adhoc-parameter-endpoints-check-values-source-test
+ (do-with-app-collection
+ (fn [collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (mt/with-temp [:model/Card {source-id :id} {:collection_id collection-id
+ :dataset_query (data-apps.tu/venues-query)}
+ :model/Card {elsewhere-id :id} {:dataset_query (data-apps.tu/venues-query)}]
+ (testing "a static list loads as is"
+ (is (=? {:values [["bar"] ["cafe"]]}
+ (mt/user-http-request :crowberto :post 200 "dataset/parameter/values" as-data-app
+ {:parameter static-parameter}))))
+ (testing "values from a card of the app load, from any other card are refused"
+ (is (seq (:values (mt/user-http-request :crowberto :post 200 "dataset/parameter/values" as-data-app
+ {:parameter (card-source-parameter source-id)}))))
+ (doseq [[url body] [["dataset/parameter/values" {}]
+ ["dataset/parameter/search/Red" {}]
+ ["dataset/parameter/remapping" {:value "Red"}]]]
+ (is (= (card-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 url as-data-app
+ (assoc body :parameter (card-source-parameter elsewhere-id))))
+ url)))
+ (let [parameter {:id "name" :type :string/=}]
+ (testing "values from a field of a published table load, from any other field are refused"
+ (is (seq (:values (mt/user-http-request :crowberto :post 200 "dataset/parameter/values" as-data-app
+ {:parameter parameter :field_ids [(mt/id :venues :name)]}))))
+ (doseq [[url body] [["dataset/parameter/values" {}]
+ ["dataset/parameter/search/Red" {}]
+ ["dataset/parameter/remapping" {:value "Red"}]]]
+ (is (= (format "A data app can read only fields of tables published to the Library, not the field %d."
+ (mt/id :categories :name))
+ (mt/user-http-request :crowberto :post 400 url as-data-app
+ (assoc body :parameter parameter :field_ids [(mt/id :categories :name)])))
+ url)))
+ (testing "a request that is not a data app's still loads the values of any field"
+ (is (seq (:values (mt/user-http-request :crowberto :post 200 "dataset/parameter/values"
+ {:parameter parameter :field_ids [(mt/id :categories :name)]})))))))))))
+
+(deftest card-parameter-endpoints-check-values-source-card-test
+ (do-with-app-collection
+ (fn [collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (mt/with-temp [:model/Card {source-id :id} {:collection_id collection-id
+ :dataset_query (data-apps.tu/venues-query)}
+ :model/Card {card-id :id} {:collection_id collection-id
+ :parameters [(card-source-parameter source-id)]
+ :dataset_query (data-apps.tu/venues-query)}]
+ (testing "a parameter whose values come from a card of the app loads them"
+ (is (seq (:values (mt/user-http-request :crowberto :get 200 (format "card/%d/params/venue/values" card-id)
+ as-data-app)))))
+ (testing "a parameter whose values come from a card outside the app's collection is refused"
+ (t2/update! :model/Card source-id {:collection_id nil})
+ (doseq [url ["card/%d/params/venue/values" "card/%d/params/venue/search/Red" "card/%d/params/venue/remapping?value=Red"]]
+ (is (= (card-error source-id)
+ (mt/user-http-request :crowberto :get 400 (format url card-id) as-data-app))
+ url))))))))
+
+(defn- field-parameter
+ "A parameter whose values come from the field with `field-id`."
+ [field-id]
+ {:id "field"
+ :slug "field"
+ :name "Field"
+ :type :string/=
+ :target [:dimension (legacy-field-ref field-id)]})
+
+(deftest card-parameter-endpoints-check-values-source-field-test
+ (do-with-app-collection
+ (fn [collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (mt/with-temp [:model/Card {published-id :id} {:collection_id collection-id
+ :parameters [(field-parameter (mt/id :venues :name))]
+ :dataset_query (data-apps.tu/venues-query)}
+ :model/Card {unpublished-id :id} {:collection_id collection-id
+ :parameters [(field-parameter (mt/id :checkins :date))]
+ :dataset_query (data-apps.tu/venues-query)}]
+ (testing "a parameter whose values come from a field of a published table loads them"
+ (is (seq (:values (mt/user-http-request :crowberto :get 200 (format "card/%d/params/field/values" published-id)
+ as-data-app)))))
+ (testing "a parameter whose values come from a field of an unpublished table is refused"
+ (is (= (format "A data app can read only fields of tables published to the Library, not the field %d."
+ (mt/id :checkins :date))
+ (mt/user-http-request :crowberto :get 400 (format "card/%d/params/field/values" unpublished-id)
+ as-data-app)))))))))
+
+(deftest refusal-names-large-id-without-grouping-test
+ (do-with-app-card!
+ (fn [_card-id _collection-id]
+ (mt/with-temp [:model/Card {elsewhere-id :id} {:id 1234567 :dataset_query (data-apps.tu/venues-query)}]
+ (testing "an ID of a thousand or more appears as it is, not as 1,234,567"
+ (is (= "A data app can use only the cards in its own collection and in the Library, not the card 1234567."
+ (mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) as-data-app))))))))
+
+(defn- do-with-rename-action!
+ "Call `f` with the ID of a temp query action, on no model, that renames a category to itself, in the collection with
+ `collection-id`."
+ [collection-id f]
+ (mt/with-temp [:model/Action {action-id :id} {:name "Rename category"
+ :type :query
+ :collection_id collection-id}
+ :model/QueryAction _ {:action_id action-id
+ :database_id (mt/id)
+ :dataset_query (lib/native-query
+ (mt/metadata-provider)
+ "UPDATE categories SET name = name WHERE id = 1")}]
+ (f action-id)))
+
+(deftest action-endpoints-run-app-action-test
+ (mt/with-actions-test-data-and-actions-enabled
+ (do-with-app-collection
+ (fn [collection-id]
+ (do-with-rename-action!
+ collection-id
+ (fn [action-id]
+ (testing "the app's action runs, and its values endpoint answers"
+ (is (= {:rows-affected 1}
+ (mt/user-http-request :crowberto :post 200 (format "action/%d/execute" action-id) as-data-app
+ {:parameters {}})))
+ (is (= {}
+ (mt/user-http-request :crowberto :post 200 (format "action/%d/execute/values" action-id)
+ as-data-app {:parameters {}}))))
+ (testing "by its entity ID too"
+ (is (= {:rows-affected 1}
+ (mt/user-http-request :crowberto :post 200
+ (format "action/%s/execute"
+ (t2/select-one-fn :entity_id :model/Action :id action-id))
+ as-data-app
+ {:parameters {}}))))))))))
+
+(deftest action-endpoints-refuse-other-actions-test
+ (mt/with-actions-test-data-and-actions-enabled
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-temp [:model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
+ (do-with-rename-action!
+ elsewhere-collection-id
+ (fn [elsewhere-id]
+ (testing "an action outside the app's collection is refused"
+ (is (= (action-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "action/%d/execute" elsewhere-id) as-data-app
+ {:parameters {}})))
+ (is (= (action-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "action/%d/execute/values" elsewhere-id)
+ as-data-app {:parameters {}}))))
+ (testing "an ID of no action is not found, as for any request"
+ (is (= "Not found." (mt/user-http-request :crowberto :post 404 "action/999999999/execute" as-data-app
+ {:parameters {}}))))
+ (testing "a request that is not a data app's still runs the action"
+ (is (= {:rows-affected 1}
+ (mt/user-http-request :crowberto :post 200 (format "action/%d/execute" elsewhere-id)
+ {:parameters {}})))))))))))
+
+(deftest action-read-endpoints-hold-to-app-actions-test
+ (mt/with-actions-test-data-and-actions-enabled
+ (do-with-app-collection
+ (fn [collection-id]
+ (do-with-rename-action!
+ collection-id
+ (fn [action-id]
+ (mt/with-temp [:model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
+ (do-with-rename-action!
+ elsewhere-collection-id
+ (fn [elsewhere-id]
+ (testing "the action list holds only the app's actions"
+ (is (= [action-id] (map :id (mt/user-http-request :crowberto :get 200 "action" as-data-app)))))
+ (testing "the app's action is readable"
+ (is (=? {:id action-id} (mt/user-http-request :crowberto :get 200 (format "action/%d" action-id) as-data-app))))
+ (testing "an action outside the app's collection is refused"
+ (is (= (action-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format "action/%d" elsewhere-id) as-data-app))))
+ (testing "a request that is not a data app's lists and reads both"
+ (is (= #{action-id elsewhere-id}
+ (set (filter #{action-id elsewhere-id} (map :id (mt/user-http-request :crowberto :get 200 "action"))))))
+ (is (=? {:id elsewhere-id} (mt/user-http-request :crowberto :get 200 (format "action/%d" elsewhere-id))))))))))))))
+
+(deftest app-without-collection-uses-no-action-test
+ (mt/with-actions-test-data-and-actions-enabled
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-temp [:model/DataApp app {:name "wasps"}
+ :model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
+ (do-with-rename-action!
+ elsewhere-collection-id
+ (fn [elsewhere-id]
+ (t2/delete! :model/Collection (:resource_collection_id app))
+ (testing "an app without a collection lists no action"
+ (is (= [] (mt/user-http-request :crowberto :get 200 "action" (as-app "wasps")))))
+ (testing "and reads or runs none"
+ (is (= (action-error elsewhere-id)
+ (mt/user-http-request :crowberto :get 400 (format "action/%d" elsewhere-id) (as-app "wasps"))))
+ (is (= (action-error elsewhere-id)
+ (mt/user-http-request :crowberto :post 400 (format "action/%d/execute" elsewhere-id) (as-app "wasps")
+ {:parameters {}})))))))))))
+
+(deftest table-and-field-endpoints-check-published-tables-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (let [venues (mt/id :venues)
+ id (mt/id :venues :id)
+ vname (mt/id :venues :name)
+ checkins (mt/id :checkins)
+ date (mt/id :checkins :date)]
+ (testing "a published table and its fields are readable"
+ (doseq [url [(format "table/%d" venues)
+ (format "table/%d/query_metadata" venues)
+ (format "table/%d/fks" venues)
+ (format "field/%d" vname)
+ (format "field/%d/summary" vname)
+ (format "field/%d/values" vname)
+ (format "field/%d/search/%d?value=Re" vname vname)
+ (format "field/%d/remapping/%d?value=1" id vname)
+ (format "field/%d/related" vname)]]
+ (mt/user-http-request :crowberto :get 200 url as-data-app)))
+ (testing "an unpublished table and its fields are refused"
+ (doseq [url [(format "table/%d" checkins)
+ (format "table/%d/query_metadata" checkins)
+ (format "table/%d/fks" checkins)]]
+ (is (= (format "A data app can read only tables published to the Library, not the table %d." checkins)
+ (mt/user-http-request :crowberto :get 400 url as-data-app))
+ url))
+ (doseq [url [(format "field/%d" date)
+ (format "field/%d/summary" date)
+ (format "field/%d/values" date)
+ (format "field/%d/related" date)
+ (format "field/%d/search/%d?value=Re" vname date)
+ (format "field/%d/remapping/%d?value=1" id date)]]
+ (is (= (format "A data app can read only fields of tables published to the Library, not the field %d." date)
+ (mt/user-http-request :crowberto :get 400 url as-data-app))
+ url)))
+ (testing "a request that is not a data app's still reads an unpublished table"
+ (mt/user-http-request :crowberto :get 200 (format "table/%d" checkins))))))))
+
+(deftest database-endpoints-narrow-to-published-tables-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-temp [:model/Database db {:engine :h2 :details {}}
+ :model/Table published {:db_id (:id db) :name "PUBLISHED" :schema "PUBLIC"}
+ :model/Table unpublished {:db_id (:id db) :name "UNPUBLISHED" :schema "PRIVATE"}
+ :model/Database other {:engine :h2 :details {}}
+ :model/Table _ {:db_id (:id other) :name "ELSEWHERE" :schema "PUBLIC"}]
+ (data-apps.tu/with-published-tables! [(:id published)]
+ (let [list-dbs (fn [url] (:data (mt/user-http-request :crowberto :get 200 url as-data-app)))
+ listed (fn [dbs] (some #(when (= (:id db) (:id %)) %) dbs))]
+ (testing "the database list holds the database with a published table, with only that table and its schema"
+ (is (= [(:id published)] (map :id (:tables (listed (list-dbs "database?include=tables"))))))
+ (is (= ["PUBLIC"] (:schemas (listed (list-dbs "database?include=schemas"))))))
+ (testing "but not a database without published tables, nor the saved-questions virtual database"
+ (doseq [id [(:id other) lib.schema.id/saved-questions-virtual-database-id]]
+ (is (not-any? #(= id (:id %)) (list-dbs "database?saved=true")) id)))
+ (testing "a database with a published table lists only that table and its schema"
+ (is (= [(:id published)]
+ (map :id (:tables (mt/user-http-request :crowberto :get 200 (format "database/%d?include=tables" (:id db))
+ as-data-app)))))
+ (is (= ["PUBLIC"] (mt/user-http-request :crowberto :get 200 (format "database/%d/schemas" (:id db)) as-data-app)))
+ (is (= [(:id published)]
+ (map :id (mt/user-http-request :crowberto :get 200 (format "database/%d/schema/PUBLIC" (:id db)) as-data-app))))
+ (is (= "Not found."
+ (mt/user-http-request :crowberto :get 404 (format "database/%d/schema/PRIVATE" (:id db)) as-data-app))))
+ (testing "a database without published tables is refused"
+ (doseq [url ["database/%d" "database/%d/schemas" "database/%d/schema/PUBLIC"]]
+ (is (= (format "A data app can read only databases with tables published to the Library, not the database %d."
+ (:id other))
+ (mt/user-http-request :crowberto :get 400 (format url (:id other)) as-data-app))
+ url)))
+ (testing "a request that is not a data app's still lists the unpublished table"
+ (is (= [(:id unpublished)]
+ (map :id (mt/user-http-request :crowberto :get 200 (format "database/%d/schema/PRIVATE" (:id db)))))))))))))
+
+(deftest database-endpoints-without-published-tables-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-temp [:model/Database db {:engine :h2 :details {}}
+ :model/Table _ {:db_id (:id db) :name "UNPUBLISHED" :schema "PUBLIC"}]
+ (testing "a database whose tables are all unpublished is not listed"
+ (is (not-any? #(= (:id db) (:id %))
+ (:data (mt/user-http-request :crowberto :get 200 "database?include=tables" as-data-app)))))
+ (testing "and is refused by its ID"
+ (is (= (format "A data app can read only databases with tables published to the Library, not the database %d." (:id db))
+ (mt/user-http-request :crowberto :get 400 (format "database/%d" (:id db)) as-data-app))))))))
+
+(deftest database-autocomplete-endpoints-suggest-only-published-tables-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-temp [:model/Database db {:engine :h2 :details {}}
+ :model/Table published {:db_id (:id db) :name "ZEBRA_PUBLISHED"}
+ :model/Table unpublished {:db_id (:id db) :name "ZEBRA_UNPUBLISHED"}
+ :model/Field _published-field {:table_id (:id published) :name "ZEBRA_SEEN"}
+ :model/Field _unpublished-field {:table_id (:id unpublished) :name "ZEBRA_HIDDEN"}
+ :model/Card card {:name "Zebra autocomplete card"}]
+ (data-apps.tu/with-published-tables! [(:id published)]
+ (let [url (format "database/%d/autocomplete_suggestions" (:id db))]
+ (testing "table and field suggestions hold only published tables and their fields"
+ (is (= #{"ZEBRA_PUBLISHED" "ZEBRA_SEEN"}
+ (set (map first (mt/user-http-request :crowberto :get 200 url as-data-app :prefix "zebra")))))
+ (is (= #{"ZEBRA_PUBLISHED" "ZEBRA_SEEN"}
+ (set (map first (mt/user-http-request :crowberto :get 200 url as-data-app :substring "ebra"))))))
+ (testing "a request that is not a data app's still suggests unpublished tables and their fields"
+ (is (= #{"ZEBRA_PUBLISHED" "ZEBRA_UNPUBLISHED" "ZEBRA_SEEN" "ZEBRA_HIDDEN"}
+ (set (map first (mt/user-http-request :crowberto :get 200 url :prefix "zebra"))))))))
+ (let [url (format "database/%d/card_autocomplete_suggestions" (mt/id))]
+ (testing "card suggestions are empty"
+ (is (= [] (mt/user-http-request :crowberto :get 200 url as-data-app :query "zebra autocomplete"))))
+ (testing "a request that is not a data app's still suggests cards"
+ (is (some #(= (:id card) (:id %)) (mt/user-http-request :crowberto :get 200 url :query "zebra autocomplete")))))))))
+
+(defn- do-with-zebras!
+ "Call `f` with a temp table published to the Library, one unpublished, a metric in the Library and one outside it, all
+ named Zebra, as `{:published :unpublished :metric :outside-metric}`."
+ [f]
+ (data-apps.tu/do-with-library!
+ (fn [{:keys [metrics-id]}]
+ (mt/with-temp [:model/Database db {:engine :h2 :details {}}
+ :model/Table published {:db_id (:id db) :name "ZEBRA_PUBLISHED" :display_name "Zebra published"}
+ :model/Table unpublished {:db_id (:id db) :name "ZEBRA_UNPUBLISHED" :display_name "Zebra unpublished"}
+ :model/Card metric {:name "Zebra metric"
+ :type :metric
+ :collection_id metrics-id
+ :dataset_query (lib/aggregate (data-apps.tu/venues-query) (lib/count))}
+ :model/Card outside-metric {:name "Zebra outside metric"
+ :type :metric
+ :dataset_query (lib/aggregate (data-apps.tu/venues-query) (lib/count))}]
+ (data-apps.tu/with-published-tables! [(:id published)]
+ (f {:published published :unpublished unpublished :metric metric :outside-metric outside-metric}))))))
+
+(defn- search-found
+ "The `[model id]` pairs of what a search for zebras finds, as the request with `options` does."
+ [options & params]
+ (set (map (juxt :model :id) (:data (apply mt/user-http-request :crowberto :get 200 "search" options :q "zebra" params)))))
+
+(deftest search-finds-library-content-test
+ (search.tu/with-appdb-search-and-legacy-search
+ (search.tu/with-sync-search-indexing
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-premium-features #{:data-apps :library}
+ (do-with-zebras!
+ (fn [{:keys [published unpublished metric outside-metric]}]
+ (let [published ["table" (:id published)]
+ unpublished ["table" (:id unpublished)]
+ metric ["metric" (:id metric)]
+ outside-metric ["metric" (:id outside-metric)]]
+ (testing "a data app finds the Library's content, and nothing else"
+ (let [found (search-found as-data-app)]
+ (is (contains? found published))
+ (is (contains? found metric))
+ (is (not-any? found [unpublished outside-metric]))))
+ (testing "the models it asks for narrow the search"
+ (let [found (search-found as-data-app :models "metric")]
+ (is (contains? found metric))
+ (is (not (contains? found published))))
+ (let [found (search-found as-data-app :models "table")]
+ (is (contains? found published))
+ (is (not-any? found [unpublished metric]))))
+ (testing "a request that is not a data app's finds the unpublished table and the outside metric too"
+ (let [found (search-found {})]
+ (is (contains? found unpublished))
+ (is (contains? found outside-metric)))))))))))))
+
+(deftest search-without-library-finds-nothing-test
+ (search.tu/with-appdb-search-and-legacy-search
+ (search.tu/with-sync-search-indexing
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-premium-features #{:data-apps :library}
+ (do-with-zebras!
+ (fn [{:keys [published metric]}]
+ (mt/with-dynamic-fn-redefs [collections/library-collection (constantly nil)]
+ (testing "without a Library, a data app finds not even a published table or a Library metric"
+ (is (not-any? (search-found as-data-app) [["table" (:id published)] ["metric" (:id metric)]]))))))))))))
+
+(deftest search-without-published-tables-finds-nothing-test
+ (search.tu/with-appdb-search-and-legacy-search
+ (search.tu/with-sync-search-indexing
+ (do-with-app-collection
+ (fn [_collection-id]
+ (mt/with-premium-features #{:data-apps :library}
+ (mt/with-temp [:model/Database db {:engine :h2 :details {}}
+ :model/Table unpublished {:db_id (:id db) :name "ZEBRA_UNPUBLISHED" :display_name "Zebra unpublished"}]
+ (data-apps.tu/do-with-library!
+ (fn [_library]
+ (testing "with a Library that does not publish the table, a data app does not find it"
+ (is (not (contains? (search-found as-data-app) ["table" (:id unpublished)])))))))))))))
+
+(deftest user-key-value-endpoints-allow-only-sdk-keys-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (let [sdk-key "user-key-value/namespace/last_download_format/key/download_format_preference"
+ other "user-key-value/namespace/user_acknowledgement/key/seen"]
+ (testing "a data app reads, writes and clears the key the SDK uses"
+ (mt/user-http-request :crowberto :put 200 sdk-key as-data-app {:value {:last_download_format "csv"
+ :last_table_download_format "csv"}})
+ (is (= {:last_download_format "csv" :last_table_download_format "csv"}
+ (mt/user-http-request :crowberto :get 200 sdk-key as-data-app)))
+ (mt/user-http-request :crowberto :delete 200 sdk-key as-data-app))
+ (testing "a data app is refused any other key"
+ (doseq [[method body] [[:put {:value true}] [:get nil] [:delete nil]]]
+ (is (= "A data app can use only the user key-values of the SDK, not the key seen in the namespace user_acknowledgement."
+ (apply mt/user-http-request :crowberto method 400 other as-data-app (when body [body])))
+ (name method))))))))
+
+(defn- item-ids
+ "The `[model id]` pairs of the items that the request to `endpoint` with `options` finds."
+ [options endpoint]
+ (set (map (juxt :model :id) (:data (mt/user-http-request :crowberto :get 200 endpoint options)))))
+
+(deftest collection-endpoints-show-only-library-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (data-apps.tu/do-with-library!
+ (fn [{:keys [data-id metrics-id]}]
+ (mt/with-model-cleanup [:model/Collection]
+ (mt/with-temp [:model/Collection outside {:name "Birds outside"}
+ :model/Card root-card {:name "Birds root card"}]
+ (let [library-id (:id (collections/library-collection))
+ dashboards-id (t2/select-one-pk :model/Collection
+ :location (str "/" library-id "/")
+ :type collections/library-dashboards-collection-type)
+ data-child (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds data" :parent_id data-id})
+ data-grandchild (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds data nested"
+ :parent_id (:id data-child)})
+ metrics-child (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds metrics" :parent_id metrics-id})
+ metrics-grandchild (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds metrics nested"
+ :parent_id (:id metrics-child)})]
+ (testing "the root items of a data app are the Library alone"
+ (let [found (item-ids as-data-app "collection/root/items")]
+ (is (contains? found ["collection" library-id]))
+ (is (not-any? found [["collection" (:id outside)] ["card" (:id root-card)]]))))
+ (testing "the Library holds its Data, Metrics and Dashboards collections"
+ (let [found (item-ids as-data-app (str "collection/" library-id "/items"))]
+ (is (contains? found ["collection" data-id]))
+ (is (contains? found ["collection" metrics-id]))
+ (is (contains? found ["collection" dashboards-id]))))
+ (testing "Data and Metrics hold their nested collections at every level"
+ (doseq [[parent child] [[data-id data-child]
+ [(:id data-child) data-grandchild]
+ [metrics-id metrics-child]
+ [(:id metrics-child) metrics-grandchild]]]
+ (is (contains? (item-ids as-data-app (str "collection/" parent "/items")) ["collection" (:id child)])
+ (:name child))))
+ (testing "a data app reads the Library, its collections and their nested collections"
+ (doseq [id (into [library-id data-id metrics-id dashboards-id]
+ (map :id)
+ [data-child data-grandchild metrics-child metrics-grandchild])]
+ (is (= id (:id (mt/user-http-request :crowberto :get 200 (str "collection/" id) as-data-app))))))
+ (testing "the collection list and the tree of a data app hold the Library, its collections and their nested collections"
+ (let [visible [library-id data-id metrics-id dashboards-id (:id data-grandchild) (:id metrics-grandchild)]
+ hidden [(:id outside)]]
+ (doseq [[label found] [["list" (set (map :id (mt/user-http-request :crowberto :get 200 "collection" as-data-app)))]
+ ["tree" (set (map :id (mapcat #(tree-seq (comp seq :children) :children %)
+ (mt/user-http-request :crowberto :get 200 "collection/tree" as-data-app))))]]]
+ (is (every? found visible) label)
+ (is (not-any? found hidden) label))))
+ (testing "the shallow tree of a data app holds the nested collections of Data"
+ (let [found (set (map :id (mt/user-http-request :crowberto :get 200 "collection/tree" as-data-app
+ :shallow true :collection-id data-id)))]
+ (is (contains? found (:id data-child)))))
+ (testing "a data app is refused any other collection and its items"
+ (doseq [endpoint [(str "collection/" (:id outside)) (str "collection/" (:id outside) "/items")]]
+ (is (= (format "A data app can see only the Library and its collections, not the collection %s." (:id outside))
+ (mt/user-http-request :crowberto :get 400 endpoint as-data-app))
+ endpoint)))
+ (testing "a request that is not a data app's finds the other collection and the root card"
+ (let [found (item-ids {} "collection/root/items")]
+ (is (contains? found ["collection" (:id outside)]))
+ (is (contains? found ["card" (:id root-card)])))
+ (is (contains? (set (map :id (mt/user-http-request :crowberto :get 200 "collection"))) (:id outside))))))))))))
+
+(deftest dashboard-endpoints-allow-only-library-dashboards-test
+ (do-with-app-collection
+ (fn [_collection-id]
+ (data-apps.tu/do-with-library!
+ (fn [_library]
+ (data-apps.tu/with-published-tables! [(mt/id :venues)]
+ (let [library-id (:id (collections/library-collection))
+ dashboards-id (t2/select-one-pk :model/Collection
+ :location (str "/" library-id "/")
+ :type collections/library-dashboards-collection-type)]
+ (mt/with-temp [:model/Dashboard library {:collection_id dashboards-id :parameters [static-parameter]}
+ :model/Card question {:collection_id dashboards-id
+ :dashboard_id (:id library)
+ :dataset_query (data-apps.tu/venues-query)}
+ :model/DashboardCard dashcard {:dashboard_id (:id library) :card_id (:id question)}
+ :model/Dashboard outside {:parameters [static-parameter]}
+ :model/Card card {:dataset_query (data-apps.tu/venues-query)}
+ :model/DashboardCard outside-dashcard {:dashboard_id (:id outside) :card_id (:id card)}]
+ (testing "a dashboard in the Library, its metadata, parameter values and dashcard runs are open to a data app"
+ (is (=? {:id (:id library)} (mt/user-http-request :crowberto :get 200 (format "dashboard/%d" (:id library)) as-data-app)))
+ (is (map? (mt/user-http-request :crowberto :get 200 (format "dashboard/%d/query_metadata" (:id library)) as-data-app)))
+ (is (=? {:values [["bar"] ["cafe"]]}
+ (mt/user-http-request :crowberto :get 200 (format "dashboard/%d/params/kind/values" (:id library))
+ as-data-app)))
+ (is (= "completed" (:status (mt/user-http-request :crowberto :post 202
+ (format "dashboard/%d/dashcard/%d/card/%d/query"
+ (:id library) (:id dashcard) (:id question))
+ as-data-app)))))
+ (testing "a dashboard outside the Library is refused"
+ (doseq [[method url] [[:get (format "dashboard/%d" (:id outside))]
+ [:get (format "dashboard/%d/query_metadata" (:id outside))]
+ [:get (format "dashboard/%d/params/kind/values" (:id outside))]
+ [:get (format "dashboard/%d/params/kind/search/caf" (:id outside))]
+ [:post (format "dashboard/%d/dashcard/%d/card/%d/query"
+ (:id outside) (:id outside-dashcard) (:id card))]]]
+ (is (= (format "A data app can use only the dashboards in the Library, not the dashboard %d." (:id outside))
+ (mt/user-http-request :crowberto method 400 url as-data-app))
+ url)))
+ (testing "valid filter fields are open only for fields of published tables"
+ (is (map? (mt/user-http-request :crowberto :get 200 "dashboard/params/valid-filter-fields" as-data-app
+ :filtered (mt/id :venues :name) :filtering (mt/id :venues :price))))
+ (is (= (format "A data app can read only fields of tables published to the Library, not the field %d."
+ (mt/id :categories :name))
+ (mt/user-http-request :crowberto :get 400 "dashboard/params/valid-filter-fields" as-data-app
+ :filtered (mt/id :categories :name) :filtering (mt/id :venues :price)))))
+ (testing "a request that is not a data app's still reads the other dashboard"
+ (is (=? {:id (:id outside)} (mt/user-http-request :crowberto :get 200 (format "dashboard/%d" (:id outside))))))))))))))
+
+(deftest without-data-apps-feature-test
+ (testing "without the data-apps feature a data app's queries are not checked"
+ (mt/with-premium-features #{}
+ (is (= "completed" (:status (run-query 202 (lib/limit (data-apps.tu/venues-query) 1))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/config_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/config_test.clj
new file mode 100644
index 00000000000..4000e7ad60e
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/config_test.clj
@@ -0,0 +1,12 @@
+(ns metabase-enterprise.data-apps.config-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase-enterprise.data-apps.config :as data-app.config]))
+
+(set! *warn-on-reflection* true)
+
+(deftest outdated-test
+ (testing "an app below the supported version is outdated; one at it is not"
+ (with-redefs [data-app.config/supported-app-version 2]
+ (is (data-app.config/outdated? {:version 1}))
+ (is (not (data-app.config/outdated? {:version 2}))))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj
index 30985d3640b..b40caa8c564 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj
@@ -4,7 +4,7 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
- [metabase-enterprise.data-apps.test-util :as data-apps.tu]
+ [metabase-enterprise.data-apps.test-util :as data-apps.tu :refer [do-with-app-collection]]
[metabase.collections.models.collection :as collection]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -13,13 +13,6 @@
(set! *warn-on-reflection* true)
-(defn- do-with-app-collection
- "Call `f` with the id of a data app's resource collection; deleting the app afterwards deletes what it holds."
- [f]
- (mt/with-premium-features #{:data-apps}
- (mt/with-temp [:model/DataApp {collection-id :resource_collection_id} {}]
- (f collection-id))))
-
(defn- reading-card
"A native query of the action kind that reads the card with `card-id` through a card template tag."
[card-id]
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj b/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj
index e459d184731..a21317362c9 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj
@@ -155,6 +155,14 @@
(extract "Action" (action-copy action (copy-id action)))))))]
(into (sorted-map) (map (juxt (partial file-path collection-name) serialization.dump/yaml-content)) entities)))
+(defn do-with-app-collection
+ "Call `f` with the id of the resource collection of a data app named `birds`, with the data-apps feature on; deleting
+ the app afterwards deletes what it holds."
+ [f]
+ (mt/with-premium-features #{:data-apps}
+ (mt/with-temp [:model/DataApp {collection-id :resource_collection_id} {:name "birds"}]
+ (f collection-id))))
+
(defn do-with-library!
"Call `f` with the IDs of the root data library (`:data-id`) and metrics library (`:metrics-id`) collections, creating the
library for the call when the instance has none."
diff --git a/src/metabase/actions_rest/api.clj b/src/metabase/actions_rest/api.clj
index 82b41c6e14c..2b30e2f2307 100644
--- a/src/metabase/actions_rest/api.clj
+++ b/src/metabase/actions_rest/api.clj
@@ -8,6 +8,7 @@
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.data-apps.core :as data-apps]
[metabase.eid-translation.core :as eid-translation]
[metabase.events.core :as events]
[metabase.lib.schema.id :as lib.schema.id]
@@ -31,14 +32,19 @@
action-type :type} :- [:map {:closed true}
[:model-id {:optional true} [:maybe ::lib.schema.id/card]]
[:type {:optional true} [:maybe ::actions.schema/type]]
- [:archived {:default false} :boolean]]]
+ [:archived {:default false} :boolean]]
+ _body
+ request]
(when model-id
(api/read-check :model/Card model-id))
- (let [action-ids (actions-rest.db/action-ids-visible-to-user
+ (let [data-app (data-apps/request-data-app request)
+ action-ids (actions-rest.db/action-ids-visible-to-user
{:type action-type, :model-id model-id, :archived archived})
actions (when (seq action-ids)
(actions/select-actions-for-ids action-ids))]
- (t2/hydrate (filterv mi/can-read? actions) :creator :can_write)))
+ (t2/hydrate (cond->> (filterv mi/can-read? actions)
+ data-app (data-apps/data-app-actions data-app))
+ :creator :can_write)))
(api.macros/defendpoint :get "/public" :- [:sequential ::actions.schema/action]
"Fetch a list of Actions with public UUIDs. These actions are publicly-accessible *if* public sharing is enabled."
@@ -51,7 +57,11 @@
"Fetch an Action."
{:scope api-scope/data-app}
[{:keys [action-id]} :- [:map {:closed true}
- [:action-id ms/PositiveInt]]]
+ [:action-id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-action action-id))
(-> (actions/select-action :id action-id :archived false)
(t2/hydrate :creator :can_write)
api/read-check))
@@ -176,7 +186,9 @@
[:action-id ms/PositiveInt]]
_query-params
{:keys [parameters]} :- [:map {:closed true}
- [:parameters ::actions.schema/prefetch-parameter-values]]]
+ [:parameters ::actions.schema/prefetch-parameter-values]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-action action-id))
(actions/check-actions-enabled action-id)
(-> (actions/select-action :id action-id :archived false)
api/read-check
@@ -219,8 +231,10 @@
[:id [:or ::actions.schema/id ms/NanoIdString]]]
_query-params
{:keys [parameters], :as _body} :- [:maybe [:map {:closed true}
- [:parameters {:optional true} [:maybe ::actions.schema/execute-parameter-values]]]]]
+ [:parameters {:optional true} [:maybe ::actions.schema/execute-parameter-values]]]]
+ request]
(let [resolved-id (eid-translation/->id-or-404 :action id)
+ _ (some-> (data-apps/request-data-app request) (data-apps/check-action resolved-id))
{:keys [type] :as action} (api/read-check (actions/select-action :id resolved-id :archived false))]
(analytics/track-event! :snowplow/action
{:event :action-executed
diff --git a/src/metabase/api_scope/data_app.clj b/src/metabase/api_scope/data_app.clj
index bac39c62913..94e2857410b 100644
--- a/src/metabase/api_scope/data_app.clj
+++ b/src/metabase/api_scope/data_app.clj
@@ -19,9 +19,9 @@
What gets tagged: a route is tagged when the SDK actually calls it from inside a data app.
Untagged is the default and is not a judgement that a route is dangerous — most untagged
routes are writes, admin or auth surfaces, and the rest simply have no SDK caller. Where the
- omission is genuinely surprising — a read route sitting next to a tagged sibling, such as
- `GET /api/action/:action-id` beside the tagged execute routes — the reason is written at the
- endpoint itself, so \"why isn't this one tagged?\" is answered where the question is asked.
+ omission is genuinely surprising, such as a read route sitting next to a tagged sibling, the
+ reason is written at the endpoint itself, so \"why isn't this one tagged?\" is answered where
+ the question is asked.
Declared here (loaded early by [[metabase.api-scope.init]]) so the scope is registered before
any endpoint namespace resolves the tag, mirroring how metabot declares its `agent:*` scopes
diff --git a/src/metabase/collections/children.clj b/src/metabase/collections/children.clj
index aa08caf5119..5ae1596600c 100644
--- a/src/metabase/collections/children.clj
+++ b/src/metabase/collections/children.clj
@@ -64,6 +64,25 @@
(personal-ids first-parent-collection-id)))]
(remove personal-descendant? collections)))
+(def CollectionType
+ "Collection types that the root/items endpoint can filter on"
+ [:enum "remote-synced"])
+
+(def ^:private FilterableCollectionType
+ "Collection types that [[collection-children]] and [[select-collections]] can filter on: remote-synced collections,
+ or those of a Library type."
+ (into [:enum "remote-synced"] (sort collection/library-collection-types)))
+
+(defn- collection-types-clause
+ "A HoneySQL clause that keeps the collections of one of `collection-types`, \"remote-synced\" meaning the
+ remote-synced ones, or nil when there are none."
+ [collection-types]
+ (when (seq collection-types)
+ (into [:or] (for [collection-type (sort collection-types)]
+ (if (= collection-type "remote-synced")
+ [:= :is_remote_synced true]
+ [:= :type collection-type])))))
+
(defn select-collections
"Select collections based off certain parameters. If `shallow` is true, we select only the requested collection (or
the root, if `collection-id` is `nil`) and its immediate children, to avoid reading the entire collection tree when it
@@ -77,8 +96,11 @@
This will select only collections where `personal_owner_id` is not `nil`.
To include library collections and their descendants, pass in `include-library?` as `true`.
- By default, library-type collections are excluded. "
- [{:keys [archived exclude-other-user-collections namespaces shallow collection-id personal-only include-library?]}]
+ By default, library-type collections are excluded.
+
+ To select only collections of some types, pass them as the set `collection-types`. "
+ [{:keys [archived exclude-other-user-collections namespaces shallow collection-id personal-only include-library?
+ collection-types]}]
(cond->>
(collections.db/collections-matching
{:where [:and
@@ -94,6 +116,7 @@
(location-from-collection-id-clause collection-id))
(when personal-only
[:!= :personal_owner_id nil])
+ (collection-types-clause collection-types)
(when exclude-other-user-collections
[:or [:= :personal_owner_id nil] [:= :personal_owner_id api/*current-user-id*]])
(when-not include-library?
@@ -193,16 +216,12 @@
[w]
(when w (keyword (str/replace w #"_" "-"))))
-(def CollectionType
- "Collection types that the root/items endpoint can filter on"
- [:enum "remote-synced"])
-
(def CollectionChildrenOptions
"The `options` map accepted by [[collection-children]] and [[collection-filter-metadata]]."
[:map {:closed true}
[:show-dashboard-questions? :boolean]
[:show-exploration-documents? {:optional true} [:maybe :boolean]]
- [:collection-type {:optional true} [:maybe CollectionType]]
+ [:collection-types {:optional true} [:maybe [:set FilterableCollectionType]]]
[:archived? :boolean]
[:include-library? {:optional true} [:maybe :boolean]]
[:pinned-state {:optional true} [:maybe (into [:enum] (map keyword) valid-pinned-state-values)]]
@@ -714,16 +733,13 @@
[:not= :namespace (u/qualified-name "snippets")]])
(defn- collection-query
- [collection {:keys [archived? collection-namespace pinned-state collection-type include-library?]}]
+ [collection {:keys [archived? collection-namespace pinned-state collection-types include-library?]}]
(-> (assoc
(collection/effective-children-query
collection
{:cte-name :visible_collection_ids}
[:and
- (when collection-type
- (if (= collection-type "remote-synced")
- [:= :is_remote_synced true]
- [:= :type collection-type]))
+ (collection-types-clause collection-types)
(when-not include-library?
[:or [:= nil :type]
[:not [:in :type (vec collection/library-collection-types)]]])
diff --git a/src/metabase/collections/core.clj b/src/metabase/collections/core.clj
index 802154de959..ef4cf299bcc 100644
--- a/src/metabase/collections/core.clj
+++ b/src/metabase/collections/core.clj
@@ -49,6 +49,7 @@
shared-tenant-collection?
slugify
transforms-ns
+ trash-collection-id
data-apps-ns
data-actions-ns
default-allowed-namespaces]
diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.clj
index 8ed0f8a594e..47a5f978b44 100644
--- a/src/metabase/collections_rest/api.clj
+++ b/src/metabase/collections_rest/api.clj
@@ -14,6 +14,7 @@
[metabase.collections.core :as collections]
[metabase.collections.models.collection :as collection]
[metabase.collections.models.collection.root :as collection.root]
+ [metabase.data-apps.core :as data-apps]
[metabase.documents.core :as documents]
[metabase.eid-translation.core :as eid-translation]
[metabase.events.core :as events]
@@ -77,9 +78,12 @@
[:archived {:default false} [:maybe ms/BooleanValue]]
[:exclude-other-user-collections {:default false} [:maybe ms/BooleanValue]]
[:namespace {:optional true} [:maybe ms/NonBlankString]]
- [:personal-only {:default false} [:maybe ms/BooleanValue]]]]
+ [:personal-only {:default false} [:maybe ms/BooleanValue]]]
+ _body
+ request]
(as->
(collections.children/select-collections {:archived (boolean archived)
+ :collection-types (some-> (data-apps/request-data-app request) data-apps/data-app-collection-types)
:exclude-other-user-collections exclude-other-user-collections
:namespaces (cond
namespace [namespace]
@@ -148,10 +152,13 @@
[:namespace {:optional true} [:maybe ms/NonBlankString]]
[:namespaces {:optional true} [:maybe [:vector {:decode/string (fn [x] (cond (vector? x) x x [x]))} :string]]]
[:shallow {:default false} [:maybe :boolean]]
- [:collection-id {:optional true} [:maybe ms/PositiveInt]]]]
+ [:collection-id {:optional true} [:maybe ms/PositiveInt]]]
+ _body
+ request]
(api/check-400
(not (and namespace (seq namespaces))))
- (let [archived (if exclude-archived false nil)
+ (let [data-app (data-apps/request-data-app request)
+ archived (if exclude-archived false nil)
namespaces (cond
namespace #{namespace}
(seq namespaces) (into #{} (map not-empty namespaces))
@@ -162,7 +169,8 @@
:namespaces namespaces
:shallow shallow
:collection-id collection-id
- :include-library? include-library})
+ :collection-types (some-> data-app data-apps/data-app-collection-types)
+ :include-library? (or include-library (some? data-app))})
(t2/hydrate :can_write))]
(if shallow
(collections.children/shallow-tree-from-collection-id collections)
@@ -409,21 +417,26 @@
[:show-dashboard-questions {:optional true} [:maybe ms/MaybeBooleanValue]]
[:q {:optional true} [:maybe :string]]
[:include-available-models {:default false} [:maybe ms/BooleanValue]]
- [:show-exploration-documents {:optional true} [:maybe ms/MaybeBooleanValue]]]]
+ [:show-exploration-documents {:optional true} [:maybe ms/MaybeBooleanValue]]]
+ _body
+ request]
;; Return collection contents, including Collections that have an effective location of being in the Root
;; Collection for the Current User.
- (let [root-collection (assoc collection/root-collection :namespace namespace)
+ (let [data-app (data-apps/request-data-app request)
+ root-collection (assoc collection/root-collection :namespace namespace)
model-set (set (map keyword (u/one-or-many models)))
model-kwds (collections.children/visible-model-kwds root-collection model-set)
- restrict-models (when (or (not (contains? collections.children/namespaces-holding-non-collection-types namespace))
+ restrict-models (when (or data-app
+ (not (contains? collections.children/namespaces-holding-non-collection-types namespace))
(not (mi/can-read? root-collection)))
#{:collection})
options {:archived? (boolean archived)
:show-dashboard-questions? (boolean show-dashboard-questions)
:show-exploration-documents? (boolean show-exploration-documents)
- :collection-type collection-type
- :include-library? include-library
- :models (if-not (contains? collections.children/namespaces-holding-non-collection-types namespace)
+ :collection-types (or (some-> data-app data-apps/data-app-collection-types) (some-> collection-type hash-set))
+ :include-library? (or include-library (some? data-app))
+ :models (if (or data-app
+ (not (contains? collections.children/namespaces-holding-non-collection-types namespace)))
#{:collection}
model-kwds)
:pinned-state (keyword pinned-state)
@@ -463,7 +476,7 @@
{:archived? (boolean archived)
:show-dashboard-questions? (boolean show-dashboard-questions)
:show-exploration-documents? (boolean show-exploration-documents)
- :collection-type collection-type
+ :collection-types (some-> collection-type hash-set)
:include-library? include-library
:pinned-state (keyword pinned-state)
:sort-info {:sort-column :name
@@ -597,8 +610,12 @@
"Fetch a specific Collection with standard details added"
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id [:or ms/PositiveInt ms/NanoIdString]]]]
+ [:id [:or ms/PositiveInt ms/NanoIdString]]]
+ _query-params
+ _body
+ request]
(let [resolved-id (eid-translation/->id-or-404 :collection id)]
+ (some-> (data-apps/request-data-app request) (data-apps/check-collection resolved-id))
(collection-detail (api/read-check :model/Collection resolved-id))))
(api.macros/defendpoint :put "/:id" :- ::Collection
@@ -665,13 +682,18 @@
[:show-dashboard-questions {:default false} [:maybe ms/BooleanValue]]
[:show-exploration-documents {:default false} [:maybe ms/BooleanValue]]
[:q {:optional true} [:maybe :string]]
- [:include-available-models {:default false} [:maybe ms/BooleanValue]]]]
+ [:include-available-models {:default false} [:maybe ms/BooleanValue]]]
+ _body
+ request]
(let [resolved-id (eid-translation/->id-or-404 :collection id)
+ data-app (data-apps/request-data-app request)
+ _ (some-> data-app (data-apps/check-collection resolved-id))
model-kwds (set (map keyword (u/one-or-many models)))
collection (api/read-check :model/Collection resolved-id)
options {:show-dashboard-questions? show-dashboard-questions
:show-exploration-documents? show-exploration-documents
:models model-kwds
+ :collection-types (some-> data-app data-apps/data-app-collection-types)
:include-library? true
:archived? (or archived (:archived collection) (collection/is-trash? collection))
:pinned-state (keyword pinned-state)
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index fcbf1640d2d..dd95d92f5ee 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -22,6 +22,7 @@
[metabase.dashboards.models.dashboard-card :as dashboard-card]
[metabase.dashboards.models.dashboard-tab :as dashboard-tab]
[metabase.dashboards.settings :as dashboards.settings]
+ [metabase.data-apps.core :as data-apps]
[metabase.eid-translation.core :as eid-translation]
[metabase.embedding.validation :as embedding.validation]
[metabase.events.core :as events]
@@ -692,9 +693,12 @@
[{:keys [id]} :- [:map {:closed true}
[:id [:or ms/PositiveInt ms/NanoIdString]]]
{dashboard-load-id :dashboard_load_id} :- [:map {:closed true}
- [:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]]]]
+ [:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]]]
+ _body
+ request]
(with-dashboard-load-id dashboard-load-id
(let [resolved-id (eid-translation/->id-or-404 :dashboard id)
+ _ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard resolved-id))
dashboard (get-dashboard resolved-id)]
(u/prog1 (first (revisions/with-last-edit-info [dashboard] :dashboard))
(events/publish-event! :event/dashboard-read {:object-id (:id dashboard) :user-id api/*current-user-id*})))))
@@ -1193,10 +1197,13 @@
[{:keys [id]} :- [:map {:closed true}
[:id [:or ms/PositiveInt ms/NanoIdString]]]
{dashboard-load-id :dashboard_load_id} :- [:map {:closed true}
- [:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]]]]
+ [:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]]]
+ _body
+ request]
(with-dashboard-load-id dashboard-load-id
(perms/with-relevant-permissions-for-user api/*current-user-id*
(let [resolved-id (eid-translation/->id-or-404 :dashboard id)
+ _ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard resolved-id))
dashboard (get-dashboard resolved-id)]
(queries/batch-fetch-dashboard-metadata [dashboard])))))
@@ -1348,7 +1355,10 @@
[{:keys [id param-key]} :- [:map {:closed true}
[:id ms/PositiveInt]
[:param-key ms/NonBlankString]]
- constraint-param-key->value :- (ms/string-keyed-map [:ref ::lib.schema.parameter/parameter.value])]
+ constraint-param-key->value :- (ms/string-keyed-map [:ref ::lib.schema.parameter/parameter.value])
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard id))
(let [dashboard (api/read-check :model/Dashboard id)]
;; If a user can read the dashboard, then they can lookup filters. This also works with sandboxing.
(binding [qp.perms/*param-values-query* true]
@@ -1372,7 +1382,10 @@
[:id ms/PositiveInt]
[:param-key ms/NonBlankString]
[:query ms/NonBlankString]]
- constraint-param-key->value :- (ms/string-keyed-map [:ref ::lib.schema.parameter/parameter.value])]
+ constraint-param-key->value :- (ms/string-keyed-map [:ref ::lib.schema.parameter/parameter.value])
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard id))
(let [dashboard (api/read-check :model/Dashboard id)]
;; If a user can read the dashboard, then they can lookup filters. This also works with sandboxing.
(binding [qp.perms/*param-values-query* true
@@ -1392,7 +1405,10 @@
[{:keys [id param-key]} :- [:map {:closed true}
[:id ms/PositiveInt]
[:param-key ms/NonBlankString]]
- {:keys [value]} :- [:map {:closed true} [:value :string]]]
+ {:keys [value]} :- [:map {:closed true} [:value :string]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard id))
(let [dashboard (api/read-check :model/Dashboard id)]
(binding [qp.perms/*param-values-query* true]
(parameters.dashboard/dashboard-param-remapped-value dashboard param-key (codec/url-decode value)))))
@@ -1426,11 +1442,16 @@
[_route-params
{:keys [filtered filtering]} :- [:map {:closed true}
[:filtered (ms/QueryVectorOf ::lib.schema.id/field)]
- [:filtering {:optional true} [:maybe (ms/QueryVectorOf ::lib.schema.id/field)]]]]
+ [:filtering {:optional true} [:maybe (ms/QueryVectorOf ::lib.schema.id/field)]]]
+ _body
+ request]
(let [filtered-field-ids (if (sequential? filtered) (set filtered) #{filtered})
- filtering-field-ids (if (sequential? filtering) (set filtering) #{filtering})]
+ filtering-field-ids (if (sequential? filtering) (set filtering) #{filtering})
+ data-app (data-apps/request-data-app request)]
(doseq [field-id (set/union filtered-field-ids filtering-field-ids)]
- (api/read-check :model/Field field-id))
+ (if data-app
+ (data-apps/check-field data-app field-id)
+ (api/read-check :model/Field field-id)))
(into {} (for [field-id filtered-field-ids]
[field-id (sort (chain-filter/filterable-field-ids field-id filtering-field-ids))]))))
@@ -1446,7 +1467,9 @@
[:dashcard-id ms/PositiveInt]]
_query-params
{:keys [parameters]} :- [:map {:closed true}
- [:parameters {:optional true} ::actions.schema/prefetch-parameter-values]]]
+ [:parameters {:optional true} ::actions.schema/prefetch-parameter-values]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard dashboard-id))
(api/read-check :model/Dashboard dashboard-id)
(let [dashcard (api/check-404 (dashboards-rest.db/dashcard-in-dashboard dashcard-id dashboard-id))]
(actions/fetch-values
@@ -1469,7 +1492,9 @@
_query-params
{:keys [parameters]} :- [:map {:closed true}
[:parameters {:optional true}
- [:maybe ::actions.schema/execute-parameter-values.string-keys]]]]
+ [:maybe ::actions.schema/execute-parameter-values.string-keys]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard dashboard-id))
(api/read-check :model/Dashboard dashboard-id)
(actions/execute-dashcard! dashboard-id dashcard-id parameters))
@@ -1489,7 +1514,9 @@
_query-params
{:keys [dashboard_load_id], :as body} :- [:map {:closed true}
[:dashboard_load_id {:optional true} [:maybe ms/NonBlankString]]
- [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]]
+ [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard dashboard-id))
(with-dashboard-load-id dashboard_load_id
(m/mapply qp.dashboard/process-query-for-dashcard
(merge
@@ -1521,7 +1548,9 @@
:- [:map {:closed true}
[:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
- [:pivot_results {:default false} ms/BooleanValue]]]
+ [:pivot_results {:default false} ms/BooleanValue]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard dashboard-id))
(m/mapply qp.dashboard/process-query-for-dashcard
{:dashboard (api/check-404 (dashboards-rest.db/dashboard dashboard-id))
:card (api/check-404 (dashboards-rest.db/card card-id))
@@ -1553,7 +1582,9 @@
[:card-id ms/PositiveInt]]
_query-params
body :- [:map {:closed true}
- [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]]
+ [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-dashboard dashboard-id))
(m/mapply qp.dashboard/process-query-for-dashcard
(merge
body
diff --git a/src/metabase/data_apps/core.clj b/src/metabase/data_apps/core.clj
index d6fad5d9f63..5747b7a3e22 100644
--- a/src/metabase/data_apps/core.clj
+++ b/src/metabase/data_apps/core.clj
@@ -1,6 +1,15 @@
(ns metabase.data-apps.core
(:require
- [metabase.premium-features.core :refer [defenterprise]]))
+ [metabase.actions.schema :as actions.schema]
+ [metabase.analytics.core :as analytics]
+ [metabase.api-scope.data-app :as api-scope]
+ [metabase.api.macros.scope :as scope]
+ [metabase.data-apps.schema :as data-apps.schema]
+ [metabase.lib-be.schema :as lib-be.schema]
+ [metabase.lib.schema.id :as lib.schema.id]
+ [metabase.lib.schema.parameter :as lib.schema.parameter]
+ [metabase.parameters.schema :as parameters.schema]
+ [metabase.premium-features.core :refer [defenterprise defenterprise-schema]]))
(defenterprise visible-data-app-filter-select
"A HoneySQL query of the ids of the data apps that the user described by `user-info` (`:user-id`, `:is-superuser?`)
@@ -8,3 +17,142 @@
metabase-enterprise.data-apps.core
[_user-info]
nil)
+
+(defn data-app-header?
+ "Whether `request` carries the `X-Metabase-Client: data-app` header, which a data app sends both published and in its
+ dev preview."
+ [request]
+ (analytics/data-app-client? (get-in request [:headers "x-metabase-client"])))
+
+(defn data-app-slug
+ "The slug of the data app that `request` names in its `X-Metabase-Client-Identifier` header, or nil."
+ [request]
+ (not-empty (get-in request [:headers "x-metabase-client-identifier"])))
+
+(defn full-access?
+ "Whether `token-scopes` grant the whole REST API: a session's nil scopes, an unrestricted credential, or a granted
+ `\"*\"`, which [[metabase.api-scope.core/scope-matches?]] treats as satisfying every required scope."
+ [token-scopes]
+ (or (nil? token-scopes)
+ (contains? token-scopes ::scope/unrestricted)
+ (contains? token-scopes "*")))
+
+(defn data-app-request?
+ "Whether `request` is confined to the data-app scope: marked by the data-app scope middleware, or authorized by a token
+ whose scopes, short of full access, grant it."
+ [{:keys [data-app-scoped? token-scopes]}]
+ (boolean
+ (or data-app-scoped?
+ (and (not (full-access? token-scopes))
+ (scope/scope-satisfied? (into #{} (filter string?) token-scopes) api-scope/data-app)))))
+
+(defenterprise request-data-app
+ "The data app whose slug a data-app `request` names by [[data-app-slug]], read-checked; nil for a request that is not
+ a data app's, or without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_request]
+ nil)
+
+(defenterprise-schema check-query :- :nil
+ "Throws a 400 unless `data-app` may run `query`, an MBQL or internal query as the `/api/dataset` endpoints take it;
+ does nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _query :- ::lib-be.schema/maybe-legacy-or-internal-query]
+ nil)
+
+(defenterprise-schema check-card :- :nil
+ "Throws a 400 unless `data-app` may use the card with `card-id`, after a read check of the card; does nothing without
+ the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _card-id :- ::lib.schema.id/card]
+ nil)
+
+(defenterprise-schema check-card-parameter :- :nil
+ "Throws a 400 unless `data-app` may use the card with `card-id` and the card its parameter `param-key` takes its
+ values from, after read checks of both; does nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _card-id :- ::lib.schema.id/card
+ _param-key :- ::lib.schema.parameter/id]
+ nil)
+
+(defenterprise-schema check-parameter :- :nil
+ "Throws a 400 unless `data-app` may load values for the ad-hoc `parameter` from its source or the fields with
+ `field-ids`; does nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _parameter :- ::parameters.schema/parameter
+ _field-ids :- [:maybe [:sequential ::lib.schema.id/field]]]
+ nil)
+
+(defenterprise-schema check-action :- :nil
+ "Throws a 400 unless `data-app` may run the action with `action-id`, after a read check of the action; does nothing
+ without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _action-id :- ::lib.schema.id/action]
+ nil)
+
+(defenterprise-schema data-app-actions :- [:sequential ::actions.schema/action]
+ "The `actions` that `data-app` may use; all of them without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ actions :- [:sequential ::actions.schema/action]]
+ actions)
+
+(defenterprise-schema check-table :- :nil
+ "Throws a 400 unless `data-app` may read the table with `table-id`, after a read check of the table; does nothing
+ without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _table-id :- ::lib.schema.id/table]
+ nil)
+
+(defenterprise-schema check-field :- :nil
+ "Throws a 400 unless `data-app` may read the field with `field-id`, after a read check of the field; does nothing
+ without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _field-id :- ::lib.schema.id/field]
+ nil)
+
+(defenterprise-schema check-database :- :nil
+ "Throws a 400 unless `data-app` may read the database with `database-id`, after a read check of the database; does
+ nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _database-id :- ::lib.schema.id/database]
+ nil)
+
+(defenterprise-schema check-user-key-value :- :nil
+ "Throws a 400 unless `data-app` may use the user key-value `k` in `nmspace`; does nothing without the data-apps
+ feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _nmspace :- :string
+ _k :- :string]
+ nil)
+
+(defenterprise-schema data-app-collection-types :- [:maybe [:set :string]]
+ "The types of the collections that `data-app` may see, or nil, meaning any, without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app]
+ nil)
+
+(defenterprise-schema check-collection :- :nil
+ "Throws a 400 unless `data-app` may see the collection with `collection-id`, after a read check of the collection;
+ does nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _collection-id :- ::lib.schema.id/collection]
+ nil)
+
+(defenterprise-schema check-dashboard :- :nil
+ "Throws a 400 unless `data-app` may use the dashboard with `dashboard-id`, after a read check of the dashboard; does
+ nothing without the data-apps feature."
+ metabase-enterprise.data-apps.checks
+ [_data-app :- ::data-apps.schema/data-app
+ _dashboard-id :- ::lib.schema.id/dashboard]
+ nil)
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj b/src/metabase/data_apps/schema.clj
similarity index 99%
rename from enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
rename to src/metabase/data_apps/schema.clj
index 8c7ae3b2392..f903de7e461 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj
+++ b/src/metabase/data_apps/schema.clj
@@ -1,4 +1,4 @@
-(ns metabase-enterprise.data-apps.schema
+(ns metabase.data-apps.schema
"Malli schemas for the data-apps module."
(:require
[clojure.string :as str]
diff --git a/src/metabase/embedding/util.clj b/src/metabase/embedding/util.clj
index dc2838f2c12..a6bd4dd6a4c 100644
--- a/src/metabase/embedding/util.clj
+++ b/src/metabase/embedding/util.clj
@@ -1,7 +1,5 @@
(ns metabase.embedding.util
- "Utility functions for common operations related to embedding."
- (:require
- [metabase.analytics.core :as analytics]))
+ "Utility functions for common operations related to embedding.")
(def ^:private embedding-sdk-client "embedding-sdk-react")
(def ^:private embedded-analytics-js-client "embedding-simple")
@@ -11,15 +9,6 @@
[request]
(= (get-in request [:headers "x-metabase-client"]) embedding-sdk-client))
-(defn has-data-app-header?
- "Check if the client has indicated it is a sandboxed data app.
-
- Unlike its siblings here this one gates authorization, not behaviour:
- [[metabase.server.middleware.data-app-scope]] confines a request to the `data-app` scope
- on the strength of it."
- [request]
- (analytics/data-app-client? (get-in request [:headers "x-metabase-client"])))
-
(defn has-embedded-analytics-js-header?
"Check if the client has indicated it is from modular embedding."
[request]
diff --git a/src/metabase/metabot/context.clj b/src/metabase/metabot/context.clj
index cc7ab1662f8..de392d96cfd 100644
--- a/src/metabase/metabot/context.clj
+++ b/src/metabase/metabot/context.clj
@@ -5,6 +5,7 @@
[metabase.activity-feed.core :as activity-feed]
[metabase.api.common :as api]
[metabase.config.core :as config]
+ [metabase.data-apps.core :as data-apps]
[metabase.embedding.util :as embedding.util]
[metabase.lib-be.core :as lib-be]
[metabase.lib-be.schema :as lib-be.schema]
@@ -281,7 +282,7 @@
[request]
(boolean (and request
(or (embedding.util/is-modular-embedding-request? request)
- (embedding.util/has-data-app-header? request)))))
+ (data-apps/data-app-header? request)))))
(defn- query-for-sql-parsing
"Return the native query in a viewing-context item, or nil."
diff --git a/src/metabase/models/db.clj b/src/metabase/models/db.clj
index df9f956885e..3d2819c05e3 100644
--- a/src/metabase/models/db.clj
+++ b/src/metabase/models/db.clj
@@ -60,7 +60,7 @@
:model/DashboardCard :metabase.dashboards.schema/dashboard-card.update
:model/DashboardCardSeries :metabase.dashboards.schema/dashboard-card-series.update
:model/DashboardTab :metabase.dashboards.schema/dashboard-tab.update
- :model/DataApp :metabase-enterprise.data-apps.schema/data-app.update
+ :model/DataApp :metabase.data-apps.schema/data-app.update
:model/Database :metabase.warehouses.schema/database.update
:model/Dimension :metabase.warehouse-schema.schema/dimension.update
:model/Document :metabase.documents.schema/document.update
diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.clj
index 71ae3d9774d..5644fe2fac4 100644
--- a/src/metabase/queries_rest/api/card.clj
+++ b/src/metabase/queries_rest/api/card.clj
@@ -6,6 +6,7 @@
[metabase.api.macros :as api.macros]
[metabase.collections.models.collection :as collection]
[metabase.collections.models.collection.root :as collection.root]
+ [metabase.data-apps.core :as data-apps]
[metabase.eid-translation.core :as eid-translation]
[metabase.events.core :as events]
[metabase.lib-be.core :as lib-be]
@@ -231,8 +232,11 @@
[{:keys [id]} :- [:map {:closed true}
[:id [:or ms/PositiveInt ms/NanoIdString]]]
{legacy-mbql? :legacy-mbql
- :keys []} :- [:map {:closed true} [:legacy-mbql {:optional true, :default false} [:maybe :boolean]]]]
+ :keys []} :- [:map {:closed true} [:legacy-mbql {:optional true, :default false} [:maybe :boolean]]]
+ _body
+ request]
(let [resolved-id (eid-translation/->id-or-404 :card id)
+ _ (some-> (data-apps/request-data-app request) (data-apps/check-card resolved-id))
card (get-card resolved-id)]
(cond-> card
legacy-mbql?
@@ -656,8 +660,12 @@
"Get all of the required query metadata for a card."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id [:or ms/PositiveInt ms/NanoIdString]]]]
+ [:id [:or ms/PositiveInt ms/NanoIdString]]]
+ _query-params
+ _body
+ request]
(let [resolved-id (eid-translation/->id-or-404 :card id)]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card resolved-id))
(queries/batch-fetch-card-metadata [(get-card resolved-id)])))
;;; ------------------------------------------------- Deleting Cards -------------------------------------------------
@@ -813,8 +821,10 @@
[:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]
[:stored_result_id {:optional true} [:maybe ms/PositiveInt]]
[:sort {:optional true}
- [:maybe [:enum "value_asc" "value_desc" "label_asc" "label_desc"]]]]]
+ [:maybe [:enum "value_asc" "value_desc" "label_asc" "label_desc"]]]]
+ request]
(let [resolved-card-id (eid-translation/->id-or-404 :card card-id)
+ _ (some-> (data-apps/request-data-app request) (data-apps/check-card resolved-card-id))
card (api/check-404 (queries-rest.db/card resolved-card-id))]
(if stored_result_id
(do
@@ -865,7 +875,9 @@
[:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]
- [:csv_include_bom {:default false} ms/BooleanValue]]]
+ [:csv_include_bom {:default false} ms/BooleanValue]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card card-id))
(qp.card/process-query-for-card
(api/check-404 (queries-rest.db/card card-id)) export-format
:parameters parameters
@@ -949,7 +961,9 @@
:or {ignore_cache false}} :- [:map {:closed true}
[:ignore_cache {:optional true} [:maybe :boolean]]
[:dashboard_id {:optional true} [:maybe ms/PositiveInt]]
- [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]]
+ [:parameters {:optional true} [:maybe [:sequential ::parameters.schema/parameter-with-value]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card card-id))
(let [card (api/check-404 (queries-rest.db/card card-id))]
(when dashboard_id
(api/read-check :model/Dashboard dashboard_id))
@@ -973,7 +987,11 @@
{:scope api-scope/data-app}
[{:keys [card-id param-key]} :- [:map {:closed true}
[:card-id ms/PositiveInt]
- [:param-key ::lib.schema.parameter/id]]]
+ [:param-key ::lib.schema.parameter/id]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card-parameter card-id param-key))
(binding [qp.perms/*param-values-query* true]
(queries/card-param-values (api/read-check :model/Card card-id) param-key)))
@@ -992,7 +1010,11 @@
[{:keys [card-id param-key query]} :- [:map {:closed true}
[:card-id ms/PositiveInt]
[:param-key ::lib.schema.parameter/id]
- [:query ms/NonBlankString]]]
+ [:query ms/NonBlankString]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card-parameter card-id param-key))
(binding [qp.perms/*param-values-query* true]
(queries/card-param-values (api/read-check :model/Card card-id) param-key query)))
@@ -1009,7 +1031,10 @@
[{:keys [id param-key]} :- [:map {:closed true}
[:id ::lib.schema.id/card]
[:param-key ::lib.schema.parameter/id]]
- {:keys [value]} :- [:map {:closed true} [:value :string]]]
+ {:keys [value]} :- [:map {:closed true} [:value :string]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card-parameter id param-key))
(binding [qp.perms/*param-values-query* true]
(-> (api/read-check :model/Card id)
(queries/card-param-remapped-value param-key (codec/url-decode value)))))
diff --git a/src/metabase/query_processor/api.clj b/src/metabase/query_processor/api.clj
index ff29ee60045..f5379ed021c 100644
--- a/src/metabase/query_processor/api.clj
+++ b/src/metabase/query_processor/api.clj
@@ -6,6 +6,7 @@
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.data-apps.core :as data-apps]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.events.core :as events]
@@ -106,7 +107,9 @@
{:scope api-scope/data-app}
[_route-params
_query-params
- query :- ::lib-be.schema/maybe-legacy-or-internal-query]
+ query :- ::lib-be.schema/maybe-legacy-or-internal-query
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-query query))
(run-streaming-query
(-> query
(dissoc :cache-strategy)
@@ -159,7 +162,9 @@
(string? x) (json/decode viz-setting-key-fn)))})]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]
- [:csv_include_bom {:default false} ms/BooleanValue]]]
+ [:csv_include_bom {:default false} ms/BooleanValue]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-query query))
(let [viz-settings (-> visualization-settings
mi/normalize-visualization-settings
mb.viz/norm->db)
@@ -196,7 +201,9 @@
{:scope api-scope/data-app}
[_route-params
_query-params
- query :- ::lib-be.schema/maybe-legacy-query]
+ query :- ::lib-be.schema/maybe-legacy-query
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-query query))
(queries/batch-fetch-query-metadata
[query]
(when-some [include-sensitive-fields (get-in query [:settings :include-sensitive-fields])]
@@ -213,7 +220,9 @@
_query-params
{:keys [database pretty] :as query} :- [:map {:closed true}
[:pretty {:default true} [:maybe :boolean]]
- [::mc/default ::lib-be.schema/maybe-legacy-query]]]
+ [::mc/default ::lib-be.schema/maybe-legacy-query]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-query (dissoc query :pretty)))
(model-persistence/with-persisted-substituion-disabled
(let [query (-> (lib-be/normalize-query (dissoc query :pretty))
(dissoc :constraints :middleware)
@@ -246,7 +255,9 @@
{:scope api-scope/data-app}
[_route-params
_query-params
- {:keys [database] :as query} :- ::lib-be.schema/maybe-legacy-query]
+ {:keys [database] :as query} :- ::lib-be.schema/maybe-legacy-query
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-query query))
(api/read-check :model/Database database)
(let [info {:executed-by api/*current-user-id*
:context :ad-hoc}]
@@ -294,7 +305,9 @@
{:keys [parameter]
field-ids :field_ids} :- [:map {:closed true}
[:parameter ::parameters.schema/parameter]
- [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]]
+ [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-parameter parameter field-ids))
(parameter-values parameter field-ids nil))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
@@ -310,7 +323,9 @@
{:keys [parameter]
field-ids :field_ids} :- [:map {:closed true}
[:parameter ::parameters.schema/parameter]
- [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]]
+ [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-parameter parameter field-ids))
(parameter-values parameter field-ids query))
(defn param-remapped-value
@@ -339,5 +354,7 @@
{:keys [parameter value field_ids]} :- [:map {:closed true}
[:parameter ::parameters.schema/parameter]
[:value [:ref ::lib.schema.parameter/parameter.value]]
- [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]]
+ [:field_ids {:optional true} [:maybe [:sequential ::lib.schema.id/field]]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-parameter parameter field_ids))
(param-remapped-value field_ids parameter value))
diff --git a/src/metabase/search/api.clj b/src/metabase/search/api.clj
index 138cf6347c5..ecadf2c67f4 100644
--- a/src/metabase/search/api.clj
+++ b/src/metabase/search/api.clj
@@ -7,7 +7,9 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.open-api :as open-api]
+ [metabase.collections.core :as collections]
[metabase.config.core :as config]
+ [metabase.data-apps.core :as data-apps]
[metabase.permissions.core :as perms]
[metabase.request.core :as request]
[metabase.search.config :as search.config]
@@ -298,6 +300,18 @@
:include-metadata? include-metadata
:display-type (set display-type)}))
+(defn- data-app-search-params
+ "`query-params` narrowed, for a data-app `request`, to the Library's content; without a Library, the search looks for
+ tables in the Trash, which holds none."
+ [request query-params]
+ (if (data-apps/request-data-app request)
+ (if-let [library (collections/library-collection)]
+ (assoc query-params :collection (:id library))
+ (-> query-params
+ (dissoc :ids)
+ (assoc :models ["table"] :collection (collections/trash-collection-id))))
+ query-params))
+
#_{:clj-kondo/ignore [:metabase/validate-defendpoint-query-params-use-kebab-case
:metabase/validate-defendpoint-has-response-schema]}
(api.macros/defendpoint :get "/"
@@ -336,7 +350,9 @@
A search query that has both filters applied will only return models and cards."
{:scope api-scope/data-app}
[_route-params
- query-params :- search-request-schema]
+ query-params :- search-request-schema
+ _body
+ request]
(api/check-valid-page-params (request/limit) (request/offset))
;; tuning/diagnostic knobs are admin-only: explain re-executes the vector scan and counts the whole index
;; table per request, and the scan-budget knobs let a request inflate its own cost
@@ -345,7 +361,7 @@
(some? (:vector_search_explain query-params)))
(api/check-superuser))
(try
- (u/prog1 (search/search (params->search-context query-params))
+ (u/prog1 (search/search (params->search-context (data-app-search-params request query-params)))
(analytics/inc! :metabase-search/response-ok)
(analytics/observe! :metabase-search/response-results (:total <>)))
(catch Exception e
diff --git a/src/metabase/search/in_place/legacy.clj b/src/metabase/search/in_place/legacy.clj
index 3fbd7f109b6..3e8c49d34eb 100644
--- a/src/metabase/search/in_place/legacy.clj
+++ b/src/metabase/search/in_place/legacy.clj
@@ -471,10 +471,12 @@
cols-or-nils (canonical-columns model column-alias->honeysql-clause)]
cols-or-nils))
-(mu/defn- from-clause-for-model :- [:tuple [:tuple :keyword :keyword]]
+(mu/defn- from-clause-for-model :- [:tuple [:tuple :any :keyword]]
[model :- SearchableModel]
(let [{:keys [db-model alias]} (get search.config/model-to-db-model model)]
- [[(t2/table-name db-model) alias]]))
+ (if (= model "table")
+ [(warehouse-schema-overlay/table-query {:alias alias})]
+ [[(t2/table-name db-model) alias]])))
(mu/defn- base-query-for-model :- [:map {:closed true}
[:select :any]
diff --git a/src/metabase/server/middleware/data_app_scope.clj b/src/metabase/server/middleware/data_app_scope.clj
index a05f0fc9d68..6966d221b3c 100644
--- a/src/metabase/server/middleware/data_app_scope.clj
+++ b/src/metabase/server/middleware/data_app_scope.clj
@@ -24,8 +24,7 @@
decision made here from an absent one and refuse to widen it back out."
(:require
[metabase.api-scope.data-app :as api-scope]
- [metabase.api.macros.scope :as scope]
- [metabase.embedding.util :as embedding.util]))
+ [metabase.data-apps.core :as data-apps]))
(set! *warn-on-reflection* true)
@@ -35,19 +34,6 @@
tags."
#{api-scope/data-app})
-(defn- full-access?
- "True when `token-scopes` represents an unrestricted credential — a normal session, or a
- bearer token granted access to the whole REST API. Only such requests may be narrowed by
- the marker; an already-scoped credential (an OAuth or agent token) must not be broadened,
- so it stays untouched.
-
- `\"*\"` counts as full access because [[metabase.api-scope.core/scope-matches?]] treats a
- granted `\"*\"` as satisfying every required scope."
- [token-scopes]
- (or (nil? token-scopes)
- (contains? token-scopes ::scope/unrestricted)
- (contains? token-scopes "*")))
-
(defn- narrow-to-data-app?
"True when `request` carries the data-app client marker on a credential this may confine.
@@ -57,9 +43,9 @@
rather than the scope set. Narrowing one would cost it the `/api/embed-mcp` routes, which
carry no `data-app` tag, and it is already confined by that allowlist either way."
[request]
- (and (embedding.util/has-data-app-header? request)
+ (and (data-apps/data-app-header? request)
(not (:mcp-ui-credential request))
- (full-access? (:token-scopes request))))
+ (data-apps/full-access? (:token-scopes request))))
(defn wrap-data-app-scope
"When a request comes from a sandboxed data app (`X-Metabase-Client: data-app`) AND is
diff --git a/src/metabase/user_key_value/api.clj b/src/metabase/user_key_value/api.clj
index f37c218160a..a1603b491b1 100644
--- a/src/metabase/user_key_value/api.clj
+++ b/src/metabase/user_key_value/api.clj
@@ -6,6 +6,7 @@
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.data-apps.core :as data-apps]
[metabase.lib.schema.literal]
[metabase.user-key-value.models.user-key-value :as user-key-value]
[metabase.user-key-value.models.user-key-value.types :as types]
@@ -24,7 +25,9 @@
_query-params
{v :value, expires-at :expires_at} :- [:map {:closed true}
[:value {:optional true} [:maybe [:or :string number? :boolean ms/OpaqueJSONObject [:sequential [:or :string number? :boolean ms/OpaqueJSONObject]]]]]
- [:expires_at {:optional true} [:maybe :metabase.lib.schema.literal/string.datetime]]]]
+ [:expires_at {:optional true} [:maybe :metabase.lib.schema.literal/string.datetime]]]
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-user-key-value nmspace k))
(try
(user-key-value/put! api/*current-user-id* (mc/coerce ::types/user-key-value
{:key k
@@ -51,7 +54,11 @@
{:scope api-scope/data-app}
[{nmspace :namespace, k :key} :- [:map {:closed true}
[:key ms/NonBlankString]
- [:namespace ms/NonBlankString]]]
+ [:namespace ms/NonBlankString]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-user-key-value nmspace k))
(user-key-value/retrieve api/*current-user-id* nmspace k))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
@@ -76,5 +83,9 @@
{:scope api-scope/data-app}
[{nmspace :namespace, k :key} :- [:map {:closed true}
[:key ms/NonBlankString]
- [:namespace ms/NonBlankString]]]
+ [:namespace ms/NonBlankString]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-user-key-value nmspace k))
(user-key-value/delete! api/*current-user-id* nmspace k))
diff --git a/src/metabase/warehouse_schema/table.clj b/src/metabase/warehouse_schema/table.clj
index fa06df20c3d..28a7748941a 100644
--- a/src/metabase/warehouse_schema/table.clj
+++ b/src/metabase/warehouse_schema/table.clj
@@ -198,7 +198,7 @@
(defn database-schemas
"Returns a list of all the schemas with tables found for the database `id`. Excludes schemas with no tables."
- [id {:keys [include-editable-data-model? include-hidden? can-query?]}]
+ [id {:keys [include-editable-data-model? include-hidden? can-query? exclude-unpublished-tables?]}]
(let [filter-schemas (fn [schemas]
(if include-editable-data-model?
(->> schemas
@@ -207,10 +207,12 @@
(map :schema))
(filter (partial can-read-schema? id) schemas)))
filter-schemas-by-tables (fn [schemas]
- (if can-query?
+ (if (or can-query? exclude-unpublished-tables?)
(let [tables (warehouse-schema.db/active-tables-for-database id)
_ (perms/prime-table-perms-cache {:db-ids #{id}})
- filtered-tables (filter mi/can-query? tables)
+ filtered-tables (cond->> tables
+ can-query? (filter mi/can-query?)
+ exclude-unpublished-tables? (filter :is_published))
allowed-schemas (set (map :schema filtered-tables))]
(filter #(contains? allowed-schemas %) schemas))
schemas))]
@@ -229,7 +231,7 @@
`include-editable-data-model?` routes the filtering through data-model permissions instead."
([db-id schema]
(schema-tables-list db-id schema {}))
- ([db-id schema {:keys [include-hidden? include-editable-data-model? can-query? include-measures?]}]
+ ([db-id schema {:keys [include-hidden? include-editable-data-model? can-query? include-measures? exclude-unpublished-tables?]}]
(when-not include-editable-data-model?
(api/read-check :model/Database db-id)
(api/check-403 (can-read-schema? db-id schema)))
@@ -240,7 +242,8 @@
filtered-tables (cond->> (if include-editable-data-model?
(filter-tables-by-data-model-perms candidate-tables)
(filter mi/can-read? candidate-tables))
- can-query? (filter mi/can-query?))
+ can-query? (filter mi/can-query?)
+ exclude-unpublished-tables? (filter :is_published))
hydration-keys (cond-> []
(premium-features/any-transforms-enabled?) (conj :transform)
include-measures? (conj :measures))]
diff --git a/src/metabase/warehouse_schema_rest/api/field.clj b/src/metabase/warehouse_schema_rest/api/field.clj
index 33e5b8d4043..f490555e386 100644
--- a/src/metabase/warehouse_schema_rest/api/field.clj
+++ b/src/metabase/warehouse_schema_rest/api/field.clj
@@ -4,6 +4,7 @@
[metabase.api-scope.data-app :as api-scope]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.data-apps.core :as data-apps]
[metabase.events.core :as events]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.models.interface :as mi]
@@ -58,7 +59,10 @@
[{:keys [id]} :- [:map {:closed true}
[:id ms/PositiveInt]]
{include-editable-data-model? :include_editable_data_model} :- [:map {:closed true}
- [:include_editable_data_model {:default false} ms/BooleanValue]]]
+ [:include_editable_data_model {:default false} ms/BooleanValue]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-field id))
(schema.field/get-field id {:include-editable-data-model? include-editable-data-model?}))
(api.macros/defendpoint :post "/table-ids" :- [:map
@@ -232,7 +236,11 @@
"Get the count and distinct count of `Field` with ID."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id ms/PositiveInt]]]
+ [:id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-field id))
(let [field (api/read-check :model/Field id)]
[[:count (metadata-from-qp/field-count field)]
[:distincts (metadata-from-qp/field-distinct-count field)]]))
@@ -300,7 +308,11 @@
`:list`, checks whether we should create FieldValues for this Field; if so, creates and returns them."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id ms/PositiveInt]]]
+ [:id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-field id))
(let [field (api/query-check (warehouse-schema-rest.db/field id))]
(parameters.field/field->values field)))
@@ -394,7 +406,12 @@
[:id ms/PositiveInt]
[:search-id ms/PositiveInt]]
{:keys [value]} :- [:map {:closed true}
- [:value {:optional true} ms/NonBlankString]]]
+ [:value {:optional true} ms/NonBlankString]]
+ _body
+ request]
+ (when-let [data-app (data-apps/request-data-app request)]
+ (data-apps/check-field data-app id)
+ (data-apps/check-field data-app search-id))
(when-not value
(api/check-400 (request/limit) "Limit required if value is omitted"))
(let [field (api/check-404 (warehouse-schema-rest.db/field id))
@@ -414,7 +431,12 @@
[:id ms/PositiveInt]
[:remapped-id ms/PositiveInt]]
{:keys [value]} :- [:map {:closed true}
- [:value ms/NonBlankString]]]
+ [:value ms/NonBlankString]]
+ _body
+ request]
+ (when-let [data-app (data-apps/request-data-app request)]
+ (data-apps/check-field data-app id)
+ (data-apps/check-field data-app remapped-id))
(let [field (-> (warehouse-schema-rest.db/field id) api/check-404 api/read-check)
remapped-field (-> (warehouse-schema-rest.db/field remapped-id) api/check-404 api/read-check)
value (parameters.field/parse-query-param-value-for-field field value)]
@@ -428,5 +450,9 @@
"Return related entities."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id ms/PositiveInt]]]
+ [:id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-field id))
(-> (warehouse-schema-rest.db/field id) api/read-check xrays/related))
diff --git a/src/metabase/warehouse_schema_rest/api/table.clj b/src/metabase/warehouse_schema_rest/api/table.clj
index c051812bc15..21c3aa0fb62 100644
--- a/src/metabase/warehouse_schema_rest/api/table.clj
+++ b/src/metabase/warehouse_schema_rest/api/table.clj
@@ -6,6 +6,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.collections.core :as collections]
+ [metabase.data-apps.core :as data-apps]
[metabase.database-routing.core :as database-routing]
[metabase.driver.settings :as driver.settings]
[metabase.driver.util :as driver.u]
@@ -136,10 +137,13 @@
[:id ms/PositiveInt]]
{:keys [include_editable_data_model]}
:- [:map {:closed true}
- [:include_editable_data_model {:optional true} [:maybe :boolean]]]]
+ [:include_editable_data_model {:optional true} [:maybe :boolean]]]
+ _body
+ request]
;; partial schema only
:- [:map {:closed false}
[:data_authority ::data-authority-read]]
+ (some-> (data-apps/request-data-app request) (data-apps/check-table id))
(let [api-perm-check-fn (if include_editable_data_model
schema.table/check-data-model-perms
api/read-check)]
@@ -337,7 +341,10 @@
:- [:map {:closed true}
[:include_sensitive_fields {:default false} [:maybe ms/BooleanValue]]
[:include_hidden_fields {:default false} [:maybe ms/BooleanValue]]
- [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]]]
+ [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-table id))
(schema.table/fetch-table-query-metadata id {:include-sensitive-fields? include_sensitive_fields
:include-hidden-fields? include_hidden_fields
:include-editable-data-model? include_editable_data_model}))
@@ -353,7 +360,11 @@
"Return metadata for the 'virtual' table for a Card."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id ms/PositiveInt]]]
+ [:id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-card id))
(first (schema.table/batch-fetch-card-query-metadatas [id] {:include-database? true})))
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
@@ -378,7 +389,11 @@
"Get all foreign keys whose destination is a `Field` that belongs to this `Table`."
{:scope api-scope/data-app}
[{:keys [id]} :- [:map {:closed true}
- [:id ms/PositiveInt]]]
+ [:id ms/PositiveInt]]
+ _query-params
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-table id))
(api/read-check :model/Table id)
(when-let [field-ids (seq (warehouse-schema-rest.db/active-unretired-field-ids-for-table id))]
(for [origin-field (warehouse-schema-rest.db/active-fields-targeting field-ids)
diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.clj
index d1edbab3e39..cdb6ddc978d 100644
--- a/src/metabase/warehouses_rest/api.clj
+++ b/src/metabase/warehouses_rest/api.clj
@@ -8,6 +8,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.config.core :as config]
+ [metabase.data-apps.core :as data-apps]
[metabase.database-routing.core :as database-routing]
[metabase.driver :as driver]
[metabase.driver.connection :as driver.conn]
@@ -64,11 +65,12 @@
(defn- add-tables
"Hydrate tables for each database. An optional `can-query?` filter keeps only the tables the user can query."
- [dbs & {:keys [can-query?]}]
+ [dbs & {:keys [can-query? exclude-unpublished-tables?]}]
(let [all-tables (warehouses-rest.db/active-visible-tables-for-databases (map :id dbs))
_ (perms/prime-table-perms-cache {:db-ids (into #{} (map :id) dbs)})
filtered-tables (cond->> (filter mi/can-read? all-tables)
- can-query? (filter mi/can-query?))
+ can-query? (filter mi/can-query?)
+ exclude-unpublished-tables? (filter :is_published))
db-id->tables (group-by :db_id filtered-tables)]
(for [db dbs]
(assoc db :tables (get db-id->tables (:id db) [])))))
@@ -79,11 +81,12 @@
rules the single-DB `/api/database/:id/schemas` endpoint applies. One SQL
query covers the whole list so this is cheaper than the N round-trips a
client would otherwise make to populate a database-picker dropdown."
- [dbs]
+ [dbs & {:keys [exclude-unpublished-tables?]}]
(if (empty? dbs)
dbs
(let [db-ids (map :id dbs)
- rows (warehouses-rest.db/active-visible-schemas-for-databases db-ids)
+ rows (cond->> (warehouses-rest.db/active-visible-schemas-for-databases db-ids)
+ exclude-unpublished-tables? (filter :is_published))
schemas-by-db (reduce
(fn [m {:keys [db_id schema]}]
(update m db_id (fnil conj []) schema))
@@ -100,6 +103,14 @@
vec)]
(assoc db :schemas readable))))))
+(defn- keep-databases-with-published-tables
+ "`dbs` narrowed to those with an active, visible table published to the Library."
+ [dbs]
+ (let [db-ids (into #{}
+ (comp (filter :is_published) (map :db_id))
+ (warehouses-rest.db/active-visible-tables-for-databases (map :id dbs)))]
+ (filter (comp db-ids :id) dbs)))
+
(mu/defn- add-native-perms-info :- [:maybe
[:sequential
[:map
@@ -291,7 +302,8 @@
exclude-uneditable-details?
include-only-uploadable?
router-database-id
- can-query?]}]
+ can-query?
+ exclude-unpublished-tables?]}]
(let [filter-on-router-database-id (when (some->> router-database-id
(perms/user-has-permission-for-database? api/*current-user-id* :perms/manage-database :yes))
router-database-id)
@@ -304,14 +316,16 @@
;; everything below walks the list one database at a time
_ (perms/prime-database-perms-cache {:db-ids (into #{} (map :id) dbs)})]
(cond-> (-> dbs add-native-perms-info add-transforms-perms-info)
- include-tables? (add-tables :can-query? can-query?)
- include-schemas? add-schemas
+ exclude-unpublished-tables? keep-databases-with-published-tables
+ include-tables? (add-tables :can-query? can-query? :exclude-unpublished-tables? exclude-unpublished-tables?)
+ include-schemas? (add-schemas :exclude-unpublished-tables? exclude-unpublished-tables?)
can-query? (#(filter mi/can-query? %))
true add-can-upload-to-dbs
true (t2/hydrate :router_user_attribute)
include-editable-data-model? filter-databases-by-data-model-perms
exclude-uneditable-details? (#(filter (some-fn :is_attached_dwh mi/can-write?) %))
- include-saved-questions-db? (add-saved-questions-virtual-database :include-tables? include-saved-questions-tables?)
+ (and include-saved-questions-db?
+ (not exclude-unpublished-tables?)) (add-saved-questions-virtual-database :include-tables? include-saved-questions-tables?)
;; Perms checks for uploadable DBs are handled by exclude-uneditable-details? (see below)
include-only-uploadable? (#(filter uploadable-db? %)))))
@@ -367,7 +381,9 @@
[:exclude_uneditable_details {:default false} [:maybe :boolean]]
[:include_only_uploadable {:default false} [:maybe :boolean]]
[:router_database_id {:optional true} [:maybe ms/PositiveInt]]
- [:can-query {:optional true} [:maybe :boolean]]]]
+ [:can-query {:optional true} [:maybe :boolean]]]
+ _body
+ request]
(let [include-tables? (= include "tables")
include-schemas? (= include "schemas")
include-saved-questions-tables? (and saved include-tables?)
@@ -382,7 +398,8 @@
:include-stubs? include_stubs
:include-only-uploadable? include_only_uploadable
:router-database-id router_database_id
- :can-query? can-query)
+ :can-query? can-query
+ :exclude-unpublished-tables? (some? (data-apps/request-data-app request)))
[])]
{:data db-list-res
:total (count db-list-res)}))
@@ -413,7 +430,7 @@
(defn- get-database-hydrate-include
"If URL param `?include=` was passed to `GET /api/database/:id`, hydrate the Database appropriately."
- [db include]
+ [db include exclude-unpublished-tables?]
(if-not include
db
(-> (case include
@@ -423,6 +440,7 @@
(cond->> tables
; filter hidden tables
true (filter (every-pred (complement :visibility_type) mi/can-read?))
+ exclude-unpublished-tables? (filter :is_published)
true (map (fn [table] (update table :schema str)))
; filter hidden fields
(= include "tables.fields") (map #(update % :fields filter-sensitive-fields))
@@ -441,11 +459,11 @@
(defn- present-database
"Get a single Database with `id`."
- [db {:keys [include include-editable-data-model?]}]
+ [db {:keys [include include-editable-data-model? exclude-unpublished-tables?]}]
(cond-> db
true (t2/hydrate :router_user_attribute)
true add-expanded-schedules
- true (get-database-hydrate-include include)
+ true (get-database-hydrate-include include exclude-unpublished-tables?)
true add-can-upload
include-editable-data-model? check-db-data-model-perms
(mi/can-write? db) (assoc :can-manage true)))
@@ -481,14 +499,18 @@
:- [:map {:closed true}
[:include {:optional true} [:maybe [:enum "tables" "tables.fields"]]]
[:include_editable_data_model {:optional true} ms/MaybeBooleanValue]
- [:exclude_uneditable_details {:optional true} ms/MaybeBooleanValue]]]
+ [:exclude_uneditable_details {:optional true} ms/MaybeBooleanValue]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-database id))
(present-database
(warehouses/get-database id {:include-editable-data-model? include_editable_data_model
:exclude-uneditable-details? exclude_uneditable_details
:include-destination-databases? true})
{:include include
:include-editable-data-model? include_editable_data_model
- :exclude-uneditable-details? exclude_uneditable_details}))
+ :exclude-uneditable-details? exclude_uneditable_details
+ :exclude-unpublished-tables? (some? (data-apps/request-data-app request))}))
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
;;
@@ -600,8 +622,8 @@
;;; --------------------------------- GET /api/database/:id/autocomplete_suggestions ---------------------------------
-(defn- autocomplete-tables [db-id like-pattern limit]
- (warehouses-rest.db/autocomplete-tables db-id like-pattern limit))
+(defn- autocomplete-tables [db-id like-pattern limit published-only?]
+ (warehouses-rest.db/autocomplete-tables db-id like-pattern limit published-only?))
(defn- autocomplete-cards
"Returns cards that match the search string in the given database, ordered by id.
@@ -614,8 +636,8 @@
[database-id search-card-slug include-dashboard-questions?]
(warehouses-rest.db/autocomplete-cards database-id search-card-slug include-dashboard-questions?))
-(defn- autocomplete-fields [db-id like-pattern limit]
- (warehouses-rest.db/autocomplete-fields db-id like-pattern limit))
+(defn- autocomplete-fields [db-id like-pattern limit published-only?]
+ (warehouses-rest.db/autocomplete-fields db-id like-pattern limit published-only?))
(defn- autocomplete-results [tables fields limit]
(let [tbl-count (count tables)
@@ -633,11 +655,11 @@
(defn- autocomplete-suggestions
"`like-pattern` is a `LIKE` right-hand side (see [[h2x/like-substring]] and [[h2x/like-prefix]]) matched against
- lowercased table and field names."
- [db-id like-pattern]
+ lowercased table and field names; only published tables and their fields when `published-only?`."
+ [db-id like-pattern published-only?]
(let [limit 50
- tables (filter mi/can-read? (autocomplete-tables db-id like-pattern limit))
- fields (readable-fields-only (autocomplete-fields db-id like-pattern limit))]
+ tables (filter mi/can-read? (autocomplete-tables db-id like-pattern limit published-only?))
+ fields (readable-fields-only (autocomplete-fields db-id like-pattern limit published-only?))]
(autocomplete-results tables fields limit)))
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
@@ -662,23 +684,26 @@
[:id ms/PositiveInt]]
{:keys [prefix substring]} :- [:map {:closed true}
[:prefix {:optional true} [:maybe ms/NonBlankString]]
- [:substring {:optional true} [:maybe ms/NonBlankString]]]]
+ [:substring {:optional true} [:maybe ms/NonBlankString]]]
+ _body
+ request]
(api/read-check (warehouses/get-database id))
(when (and (str/blank? prefix) (str/blank? substring))
(throw (ex-info (tru "Must include prefix or search") {:status-code 400})))
- (try
- {:status 200
- ;; Presumably user will repeat same prefixes many times writing the query,
- ;; so let them cache response to make autocomplete feel fast. 60 seconds
- ;; is not enough to be a nuisance when schema or permissions change. Cache
- ;; is user-specific since we're checking for permissions.
- :headers {"Cache-Control" "public, max-age=60"
- "Vary" "Cookie"}
- :body (cond
- substring (autocomplete-suggestions id (h2x/like-substring substring))
- prefix (autocomplete-suggestions id (h2x/like-prefix prefix)))}
- (catch Throwable e
- (log/warnf "Error with autocomplete: %s" (ex-message e)))))
+ (let [published-only? (some? (data-apps/request-data-app request))]
+ (try
+ {:status 200
+ ;; Presumably user will repeat same prefixes many times writing the query,
+ ;; so let them cache response to make autocomplete feel fast. 60 seconds
+ ;; is not enough to be a nuisance when schema or permissions change. Cache
+ ;; is user-specific since we're checking for permissions.
+ :headers {"Cache-Control" "public, max-age=60"
+ "Vary" "Cookie, X-Metabase-Client"}
+ :body (cond
+ substring (autocomplete-suggestions id (h2x/like-substring substring) published-only?)
+ prefix (autocomplete-suggestions id (h2x/like-prefix prefix) published-only?))}
+ (catch Throwable e
+ (log/warnf "Error with autocomplete: %s" (ex-message e))))))
;; TODO (Cam 10/28/25) -- fix this endpoint route to use kebab-case for consistency with the rest of our REST API
;;
@@ -699,14 +724,18 @@
[:id ms/PositiveInt]]
{:keys [query include_dashboard_questions]} :- [:map {:closed true}
[:query ms/NonBlankString]
- [:include_dashboard_questions {:optional true} ms/BooleanValue]]]
+ [:include_dashboard_questions {:optional true} ms/BooleanValue]]
+ _body
+ request]
(api/read-check (warehouses/get-database id))
- (try
- (->> (autocomplete-cards id query include_dashboard_questions)
- (filter mi/can-read?)
- (map #(select-keys % [:id :name :type :collection_name])))
- (catch Throwable e
- (log/warnf "Error with autocomplete: %s" (ex-message e)))))
+ (if (some? (data-apps/request-data-app request))
+ []
+ (try
+ (->> (autocomplete-cards id query include_dashboard_questions)
+ (filter mi/can-read?)
+ (map #(select-keys % [:id :name :type :collection_name])))
+ (catch Throwable e
+ (log/warnf "Error with autocomplete: %s" (ex-message e))))))
;;; ------------------------------------------ GET /api/database/:id/fields ------------------------------------------
@@ -1277,10 +1306,14 @@
can-query]} :- [:map {:closed true}
[:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]
[:include_hidden {:default false} [:maybe ms/BooleanValue]]
- [:can-query {:optional true} [:maybe :boolean]]]]
+ [:can-query {:optional true} [:maybe :boolean]]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-database id))
(schema.table/database-schemas id {:include-editable-data-model? include_editable_data_model
:include-hidden? include_hidden
- :can-query? can-query}))
+ :can-query? can-query
+ :exclude-unpublished-tables? (some? (data-apps/request-data-app request))}))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -1337,14 +1370,18 @@
[:include_hidden {:default false} [:maybe ms/BooleanValue]]
[:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]
[:can-query {:optional true} [:maybe :boolean]]
- [:include_measures {:optional true} [:maybe :boolean]]]]
+ [:include_measures {:optional true} [:maybe :boolean]]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-database id))
(api/check-404 (seq (schema.table/schema-tables-list
id
schema
{:include-hidden? include_hidden
:include-editable-data-model? include_editable_data_model
:can-query? can-query
- :include-measures? include_measures}))))
+ :include-measures? include_measures
+ :exclude-unpublished-tables? (some? (data-apps/request-data-app request))}))))
;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
;; of the REST API
@@ -1373,11 +1410,15 @@
[:include_hidden {:default false} [:maybe ms/BooleanValue]]
[:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]
[:can-query {:optional true} [:maybe :boolean]]
- [:include_measures {:optional true} [:maybe :boolean]]]]
+ [:include_measures {:optional true} [:maybe :boolean]]]
+ _body
+ request]
+ (some-> (data-apps/request-data-app request) (data-apps/check-database id))
(let [opts {:include-hidden? include_hidden
:include-editable-data-model? include_editable_data_model
:can-query? can-query
- :include-measures? include_measures}]
+ :include-measures? include_measures
+ :exclude-unpublished-tables? (some? (data-apps/request-data-app request))}]
(api/check-404 (seq (if (str/blank? schema)
(concat (schema.table/schema-tables-list id nil opts)
(schema.table/schema-tables-list id "" opts))
diff --git a/src/metabase/warehouses_rest/db.clj b/src/metabase/warehouses_rest/db.clj
index 6b9470bf8a6..113cbfe02b4 100644
--- a/src/metabase/warehouses_rest/db.clj
+++ b/src/metabase/warehouses_rest/db.clj
@@ -27,14 +27,13 @@
[:%lower.display_name :asc]]}))
(mu/defn active-visible-schemas-for-databases
- "The distinct Database id and schema of the active, visible Tables of the Databases with `database-ids`."
+ "The Database id, schema and published state of each active, visible Table of the Databases with `database-ids`."
[database-ids :- [:sequential ::lib.schema.id/database]]
- (t2/query {:select-distinct [:db_id :schema]
- :from [(warehouse-schema-overlay/table-query)]
- :where [:and
- [:in :db_id database-ids]
- [:= :active true]
- [:= :visibility_type nil]]}))
+ (t2/select [:model/Table :db_id :schema :is_published]
+ :db_id [:in database-ids]
+ :active true
+ :visibility_type nil
+ {:from [(warehouse-schema-overlay/table-query)]}))
(mu/defn database-engines
"The id and engine of every Database."
@@ -126,16 +125,20 @@
(mu/defn autocomplete-tables
"Up to `limit` id, Database id, schema, and name rows of the active, visible Tables of the Database with
`database-id` whose lower-cased name matches the SQL LIKE `like-pattern` (a Honey SQL LIKE right-hand side, see
- `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in name order."
- [database-id :- ::lib.schema.id/database
- like-pattern :- [:or :string vector?]
- limit :- ms/PositiveInt]
+ `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in name order; only published Tables when
+ `published-only?`."
+ [database-id :- ::lib.schema.id/database
+ like-pattern :- [:or :string vector?]
+ limit :- ms/PositiveInt
+ published-only? :- :boolean]
(t2/select [:model/Table :id :db_id :schema :name]
{:from [(warehouse-schema-overlay/table-query)]
:where [:and [:= :db_id database-id]
[:= :active true]
[:like :%lower.name like-pattern]
- [:= :visibility_type nil]]
+ [:= :visibility_type nil]
+ (when published-only?
+ [:= :is_published true])]
:order-by [[:%lower.name :asc]]
:limit limit}))
@@ -198,10 +201,12 @@
(mu/defn autocomplete-fields
"Up to `limit` name, type, id, and Table of the active, non-sensitive Fields of active Tables of the Database with
`database-id` whose lower-cased name matches the SQL LIKE `like-pattern` (a Honey SQL LIKE right-hand side, see
- `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in field then table name order."
- [database-id :- ::lib.schema.id/database
- like-pattern :- [:or :string vector?]
- limit :- ms/PositiveInt]
+ `metabase.util.honey-sql-2/like-substring`/`like-prefix`), in field then table name order; only Fields of
+ published Tables when `published-only?`."
+ [database-id :- ::lib.schema.id/database
+ like-pattern :- [:or :string vector?]
+ limit :- ms/PositiveInt
+ published-only? :- :boolean]
;; NOTE: measuring showed that this query performance is improved ~4x when adding trgm index in pgsql and ~10x when
;; adding a index on `lower(metabase_field.name)` for ordering (trgm index having on impact on queries with index).
;; Pgsql now has an index on that (see migration `v49.2023-01-24T12:00:00`) as other dbms do not support indexes on
@@ -216,7 +221,9 @@
[[:lower :table.name] :asc]]
;; checking for table.active in join makes query faster when there are a lot of inactive tables
:inner-join [(warehouse-schema-overlay/table-query {:alias :table}) [:and :table.active
- [:= :table.id :metabase_field.table_id]]]
+ [:= :table.id :metabase_field.table_id]
+ (when published-only?
+ :table.is_published)]]
:limit limit}))
(mu/defn table-ids-for-database
diff --git a/test/metabase/analytics/sdk_persistence_test.clj b/test/metabase/analytics/sdk_persistence_test.clj
index 2660575f30d..227be3d6c35 100644
--- a/test/metabase/analytics/sdk_persistence_test.clj
+++ b/test/metabase/analytics/sdk_persistence_test.clj
@@ -23,22 +23,23 @@
(deftest query-execution-embedding-client-test
(mt/with-temporary-setting-values [synchronous-batch-updates true]
- (testing "the embedding client and identifier of the request are persisted on the QueryExecution row"
- (mt/user-http-request :crowberto :post 202 "dataset"
- {:request-options
- {:headers {"x-metabase-client" "data-app"
- "x-metabase-client-identifier" "sales"}}}
- (venues-query 1))
- (is (=? {:embedding_client "data-app"
- :embedding_client_identifier "sales"}
- (latest-query-execution "data-app"))))
- (testing "a dev data app (preview header) is persisted as the data-app-preview client"
- (mt/user-http-request :crowberto :post 202 "dataset"
- {:request-options
- {:headers {"x-metabase-client" "data-app"
- "x-metabase-client-identifier" "sales"
- "x-metabase-embedded-preview" "true"}}}
- (venues-query 2))
- (is (=? {:embedding_client "data-app-preview"
- :embedding_client_identifier "sales"}
- (latest-query-execution "data-app-preview"))))))
+ (mt/with-premium-features #{}
+ (testing "the embedding client and identifier of the request are persisted on the QueryExecution row"
+ (mt/user-http-request :crowberto :post 202 "dataset"
+ {:request-options
+ {:headers {"x-metabase-client" "data-app"
+ "x-metabase-client-identifier" "sales"}}}
+ (venues-query 1))
+ (is (=? {:embedding_client "data-app"
+ :embedding_client_identifier "sales"}
+ (latest-query-execution "data-app"))))
+ (testing "a dev data app (preview header) is persisted as the data-app-preview client"
+ (mt/user-http-request :crowberto :post 202 "dataset"
+ {:request-options
+ {:headers {"x-metabase-client" "data-app"
+ "x-metabase-client-identifier" "sales"
+ "x-metabase-embedded-preview" "true"}}}
+ (venues-query 2))
+ (is (=? {:embedding_client "data-app-preview"
+ :embedding_client_identifier "sales"}
+ (latest-query-execution "data-app-preview")))))))
diff --git a/test/metabase/data_apps/core_test.clj b/test/metabase/data_apps/core_test.clj
new file mode 100644
index 00000000000..31dcaf970b4
--- /dev/null
+++ b/test/metabase/data_apps/core_test.clj
@@ -0,0 +1,36 @@
+(ns metabase.data-apps.core-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.data-apps.core :as data-apps]))
+
+(set! *warn-on-reflection* true)
+
+(deftest ^:parallel data-app-request?-test
+ (testing "a request marked by the data-app scope middleware is a data app's"
+ (is (data-apps/data-app-request? {:data-app-scoped? true})))
+ (testing "a request whose token scopes grant the data-app scope short of full access is a data app's"
+ (doseq [scopes [#{"data-apps:base"} #{"data-apps:*"}]]
+ (is (data-apps/data-app-request? {:token-scopes scopes}) (pr-str scopes))))
+ (testing "a full-access request, or one whose token scopes don't grant the data-app scope, isn't"
+ (doseq [scopes [nil #{:metabase.api.macros.scope/unrestricted} #{"*"} #{"agent:search"}]]
+ (is (not (data-apps/data-app-request? {:token-scopes scopes})) (pr-str scopes)))))
+
+(deftest ^:parallel full-access?-test
+ (testing "a session, an unrestricted credential, or a granted \"*\" has full access"
+ (doseq [scopes [nil #{:metabase.api.macros.scope/unrestricted} #{"*"} #{"*" "agent:search"}]]
+ (is (data-apps/full-access? scopes) (pr-str scopes))))
+ (testing "scopes short of \"*\" don't"
+ (doseq [scopes [#{} #{"data-apps:base"} #{"agent:search"}]]
+ (is (not (data-apps/full-access? scopes)) (pr-str scopes)))))
+
+(deftest ^:parallel data-app-header?-test
+ (testing "only the data-app client header marks a request as a data app's"
+ (is (data-apps/data-app-header? {:headers {"x-metabase-client" "data-app"}}))
+ (doseq [client [nil "embedding-sdk-react" "data-app-dev"]]
+ (is (not (data-apps/data-app-header? {:headers {"x-metabase-client" client}})) (pr-str client)))))
+
+(deftest ^:parallel data-app-slug-test
+ (testing "a request names its data app by its identifier header"
+ (is (= "birds" (data-apps/data-app-slug {:headers {"x-metabase-client-identifier" "birds"}})))
+ (doseq [headers [{} {"x-metabase-client-identifier" ""}]]
+ (is (nil? (data-apps/data-app-slug {:headers headers})) (pr-str headers)))))
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj b/test/metabase/data_apps/schema_test.clj
similarity index 91%
rename from enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj
rename to test/metabase/data_apps/schema_test.clj
index 8524ba5f1ae..6520cea0d1e 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/schema_test.clj
+++ b/test/metabase/data_apps/schema_test.clj
@@ -1,8 +1,7 @@
-(ns metabase-enterprise.data-apps.schema-test
+(ns metabase.data-apps.schema-test
(:require
[clojure.test :refer :all]
- [metabase-enterprise.data-apps.config :as data-app.config]
- [metabase-enterprise.data-apps.schema :as data-apps.schema]
+ [metabase.data-apps.schema :as data-apps.schema]
[metabase.lib.core :as lib]
[metabase.util.malli.registry :as mr]))
@@ -36,12 +35,6 @@
(doseq [bad [0 -1 1.5 "1" "one" [1]]]
(is (not (valid? ::data-apps.schema/version bad)) (str "should reject: " (pr-str bad))))))
-(deftest outdated-test
- (testing "an app below the supported version is outdated; one at it is not"
- (with-redefs [data-app.config/supported-app-version 2]
- (is (data-app.config/outdated? {:version 1}))
- (is (not (data-app.config/outdated? {:version 2}))))))
-
(deftest description-test
(testing "an optional one-liner is trimmed, and a multi-line value folded onto one line"
(is (= "Pipeline health by region" (normalize ::data-apps.schema/description " Pipeline health by region ")))
diff --git a/test/metabase/server/middleware/data_app_scope_test.clj b/test/metabase/server/middleware/data_app_scope_test.clj
index 3c1792c68df..a91667935bf 100644
--- a/test/metabase/server/middleware/data_app_scope_test.clj
+++ b/test/metabase/server/middleware/data_app_scope_test.clj
@@ -84,9 +84,10 @@
;; POST /api/action/:id/execute (write-back for `useAction`) is tagged too, but needs an
;; action fixture — actions.cy.spec.ts is its functional guard under enforcement.
;;
- ;; 204 (not scope_not_permitted) proves the marked request reached the handler; the key is unset.
- (is (nil? (mt/user-http-request :crowberto :get 204
- "user-key-value/namespace/data-app-test/key/format" as-data-app))))
+ (is (not= "scope_not_permitted"
+ (:error (mt/user-http-request :crowberto :get
+ "user-key-value/namespace/last_download_format/key/download_format_preference"
+ as-data-app)))))
(testing "alerts and subscriptions are not part of the data-app surface"
;; Scheduled email delivered out of band is not something a data app renders, so neither
;; the probe the SDK buttons gate on nor the CRUD behind them is reachable. The buttons are
@@ -100,7 +101,7 @@
;; The delete body is irrelevant — reaching the handler at all is what the tag buys.
(is (not= "scope_not_permitted"
(:error (mt/user-http-request :crowberto :delete
- "user-key-value/namespace/data-app-test/key/format"
+ "user-key-value/namespace/last_download_format/key/download_format_preference"
as-data-app)))))
(testing "the InteractiveQuestion.Editor data-source surface is reachable when marked"
(is (mt/user-http-request :crowberto :get 200 "database" as-data-app)))
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 73228862a3a..101af719777 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -1134,7 +1134,7 @@
(testing "GET /api/database/:id/autocomplete_suggestions"
(testing " returns sane Cache-Control headers"
(is (=? {"Cache-Control" "public, max-age=60"
- "Vary" "Cookie"}
+ "Vary" "Cookie, X-Metabase-Client"}
(-> (client/client-full-response (test.users/username->token :rasta) :get 200
(format "database/%s/autocomplete_suggestions" (mt/id))
:prefix "u")