The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
BOT-2542 SQL on the audit database ends Metabot's turn with "Something went wrong." (#86112)
Voytek Pituła302a8e0772cb
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/permissions.clj b/enterprise/backend/src/metabase_enterprise/audit_app/permissions.cljindex 0761c7834a1..415b39a9335 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/permissions.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/permissions.clj@@ -7,6 +7,7 @@[metabase.permissions.core :as perms][metabase.premium-features.core :refer [defenterprise]][metabase.query-permissions.core :as query-perms]+ [metabase.query-processor.error-type :as qp.error-type];; legacy usage -- don't do things like this going forward^{:clj-kondo/ignore [:deprecated-namespace :discouraged-namespace]} [metabase.query-processor.store :as qp.store][metabase.util :as u]@@ -45,24 +46,28 @@- queries that include tables that are not audit views":feature :audit-app[{query-type :type, database-id :database, query :query :as outer-query}]+ ;; Each refusal has a QP error type, so that agents (Metabot, MCP, the Agent API) tell the caller why the query+ ;; failed. They show the message of the invalid-query refusals. For the permissions refusal, some agents (Metabot+ ;; `run_query`, Slackbot) show their own permission message.+ ;; The query goes under `:query`, because its own `:type` key would take the place of the error type.;; Check if the user has access to the analytics collection, since this should be coupled with access to the;; audit database in general.(when-not (mi/can-read? (audit/default-audit-collection))- (throw (ex-info (tru "You do not have access to the audit database") outer-query)))+ (throw (query-perms/perms-exception (tru "You do not have access to the audit database") nil {:query outer-query})));; query->source-table-ids returns a set of table IDs or a map with the key `:native?`(when (= query-type :native)(throw (ex-info (tru "Native queries are not allowed on the audit database")- outer-query)))+ {:type qp.error-type/invalid-query, :query outer-query})))(let [{:keys [table-ids native?]} (query-perms/query->source-ids query)](when native?(throw (ex-info (tru "Native queries are not allowed on the audit database")- outer-query)))+ {:type qp.error-type/invalid-query, :query outer-query})))(qp.store/with-metadata-provider database-id(doseq [table-id table-ids](when-not (audit-db-view-names(u/lower-case-en (:name (lib.metadata/table (qp.store/metadata-provider) table-id))))(throw (ex-info (tru "Audit queries are only allowed on audit views")- outer-query)))))))+ {:type qp.error-type/invalid-query, :query outer-query})))))))(defenterprise update-audit-collection-permissions!"Updates audit db (AppDB) permissions when the instance analytics collection permissions change, and downgradesdiff --git a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.cljindex 289a121b9ff..49748efe442 100644--- a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj+++ b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj@@ -15,6 +15,7 @@[metabase.permissions.models.collection.graph :refer [update-graph!]][metabase.permissions.models.collection.graph-test :refer [graph]][metabase.permissions.models.data-permissions :as data-perms]+ [metabase.query-processor.error-type :as qp.error-type][metabase.query-processor.test :as qp][metabase.sync.core :as sync][metabase.test :as mt]@@ -60,31 +61,36 @@:type :query:query {:source-table (u/the-id audit-view)}})))))))))+(defn- refusal+ "Run `query` and return the message and the QP error type of the exception that refuses it, or nil when it runs."+ [query]+ (try+ (qp/process-query query)+ nil+ (catch clojure.lang.ExceptionInfo e+ {:message (ex-message e), :type (:type (ex-data e))})))+(deftest audit-db-disallowed-queries-test(audit-test/with-audit-db-restoration!(mt/with-premium-features #{:audit-app}(mt/with-test-user :crowberto(testing "Native queries are not allowed to be run on audit DB views, even by admins"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Native queries are not allowed on the audit database"- (qp/process-query- {:database audit/audit-db-id- :type :native- :native {:query "SELECT * FROM v_audit_log;"}}))))+ (is (= {:message "Native queries are not allowed on the audit database"+ :type qp.error-type/invalid-query}+ (refusal {:database audit/audit-db-id+ :type :native+ :native {:query "SELECT * FROM v_audit_log;"}}))))(testing "Non-native queries are not allowed to run on tables in the audit DB that are not views";; Nothing should be synced directly from the audit DB, just loaded via serialization, so only the views;; should have metadata present in the app DB in the first place. But in case this changes, we want to;; explicitly block other tables from being queried.(mt/with-temp [:model/Table table {:db_id audit/audit-db-id}:model/Field _ {:table_id (u/the-id table)}]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Audit queries are only allowed on audit views"- (qp/process-query- {:database audit/audit-db-id- :type :query- :query {:source-table (u/the-id table)}})))))+ (is (= {:message "Audit queries are only allowed on audit views"+ :type qp.error-type/invalid-query}+ (refusal {:database audit/audit-db-id+ :type :query+ :query {:source-table (u/the-id table)}})))))(testing "Users without access to the audit collection cannot run any queries on the audit DB, even if theyhave data perms for the audit DB"(mt/with-full-data-perms-for-all-users!@@ -92,13 +98,22 @@(binding [api/*current-user-permissions-set* (delay #{})](let [audit-view (->> (t2/select-one :model/Table :db_id audit/audit-db-id {:where [:like [:lower :name] "v_%"]})(tu/poll-until 5000))]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"You do not have access to the audit database"- (qp/process-query- {:database audit/audit-db-id- :type :query- :query {:source-table (u/the-id audit-view)}}))))))))))))+ (is (= {:message "You do not have access to the audit database"+ :type qp.error-type/missing-required-permissions}+ (refusal {:database audit/audit-db-id+ :type :query+ :query {:source-table (u/the-id audit-view)}}))))))))))))++(deftest audit-db-query-without-audit-app-feature-test+ (audit-test/with-audit-db-restoration!+ (mt/with-premium-features #{}+ (mt/with-test-user :crowberto+ (testing "Without the audit-app feature, the OSS check refuses every query on the audit DB"+ (is (= {:message "Querying this database requires the audit-app feature flag"+ :type qp.error-type/disabled-feature}+ (refusal {:database audit/audit-db-id+ :type :native+ :native {:query "SELECT 1;"}}))))))))(deftest analytics-permissions-test(mt/with-premium-features #{:audit-app}diff --git a/enterprise/backend/test/metabase_enterprise/metabot/tools/sql_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/tools/sql_test.cljindex aec774b5566..05a4e0383e4 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/tools/sql_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/tools/sql_test.clj@@ -1,12 +1,21 @@(ns metabase-enterprise.metabot.tools.sql-test(:require- [clojure.test :refer [deftest is]]+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.audit-app.audit-test :as audit-test][metabase-enterprise.sandbox.test-util :as sandbox.tu]+ [metabase.api.common :as api]+ [metabase.audit-app.core :as audit][metabase.lib.core :as lib]+ [metabase.metabot.scope :as scope]+ [metabase.metabot.tools :as tools]+ [metabase.metabot.tools.run-query :as run-query][metabase.metabot.tools.sql.common :as sql.common][metabase.permissions.core :as perms][metabase.query-processor :as qp]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]))++(use-fixtures :once (fixtures/initialize :db :plugins))(deftest sandboxed-user-has-no-native-query-access-test(sandbox.tu/with-gtaps! {:gtaps {:checkins {}}}@@ -17,3 +26,63 @@(is (thrown-with-msg? clojure.lang.ExceptionInfo #"You do not have permissions to run this query"(qp/process-query (lib/native-query (mt/metadata-provider) "SELECT 1"))))(is (false? (sql.common/native-query-access? (mt/id))))))++(defn- call-tool!+ "Call the tool `tool-name` of `tool-vars` with `args` as the current user, in a conversation of their own that+ holds `queries`, with all Metabot permissions. Return the tool's result, or+ `{:output <the text the model reads> :error {:class :code}}` on failure."+ [tool-vars queries tool-name args]+ (mt/with-temp [:model/MetabotConversation {conversation-id :id} {:user_id api/*current-user-id*}]+ (binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions+ scope/*current-user-scope* (scope/user-metabot-perms->scopes scope/all-yes-permissions)]+ (let [entries (tools/->entries tool-vars+ (atom {:conversation-id conversation-id, :state {:queries queries}})+ nil :internal)]+ ((:fn (get entries tool-name)) args)))))++(defn- run-query!+ "Call Chat's `run_query` on `query`. See [[call-tool!]]."+ [query]+ (call-tool! [#'tools/run-query-tool #'tools/construct-notebook-query-tool #'tools/create-sql-query-tool]+ {"q1" query}+ "run_query"+ {:query_id "q1"}))++(deftest audit-db-sql-test+ (audit-test/with-audit-db-restoration!+ (mt/with-premium-features #{:audit-app}+ (mt/with-temporary-setting-values [metabot-query-execution-enabled? true+ metabot-sql-execution-enabled? true]+ (mt/with-current-user (mt/user->id :crowberto)+ (testing "`run_query` refuses SQL on the audit database with a step the model can take"+ (let [result (run-query! {:database audit/audit-db-id+ :type :native+ :native {:query "SELECT * FROM v_audit_log"}})]+ (is (= {:class :recoverable, :code ::run-query/audit-db-sql}+ (:error result)))+ (testing "and does not call it a permission gap"+ (is (re-find #"is the audit database" (:output result)))+ (is (not (re-find #"(?i)you do not have permission" (:output result)))))))+ (testing "a SQL tool with no notebook fallback gets the QP's refusal, and the turn does not end"+ (let [result (call-tool! [#'tools/document-construct-sql-chart-tool]+ {}+ "document_construct_sql_chart"+ {:database_id audit/audit-db-id+ :name "Audit log"+ :description "Audit log rows"+ :analysis "Show the audit log"+ :approach "SQL"+ :sql "SELECT * FROM v_audit_log"+ :viz_settings {:chart_type "table"}})]+ (is (= {:class :recoverable, :code ::sql.common/query-failed}+ (:error result)))+ (is (re-find #"Native queries are not allowed on the audit database" (:output result)))))+ (testing "the QP's refusal of a table that is not an audit view reaches the model"+ (mt/with-temp [:model/Table table {:db_id audit/audit-db-id}+ :model/Field _ {:table_id (:id table)}]+ (let [result (run-query! {:database audit/audit-db-id+ :type :query+ :query {:source-table (:id table)}})]+ (is (= {:class :recoverable, :code ::sql.common/query-failed}+ (:error result)))+ (is (re-find #"Audit queries are only allowed on audit views" (:output result)))))))))))diff --git a/src/metabase/mcp/v2/tools/query.clj b/src/metabase/mcp/v2/tools/query.cljindex 30551e069be..cb9311ef93f 100644--- a/src/metabase/mcp/v2/tools/query.clj+++ b/src/metabase/mcp/v2/tools/query.clj@@ -283,6 +283,10 @@ Dialect (JSON): tables and columns go by NUMERIC ID (from browse_data list_table(v2.queries/check-execute-sql-enabled! "execute_sql")(when-not (mi/can-read? :model/Database database-id)(common/throw-not-found :model/Database database-id))+ ;; For an admin, this check says yes on the audit database. The QP refuses SQL there for all users+ ;; (`check-audit-db-permissions` in EE `audit-app.permissions`), so the caller gets that refusal later. Metabot's+ ;; `run_query` (`check-sql-runnable!`) copies the rule to refuse early. To refuse early here too, copy it as well,+ ;; or move the rule into the permissions layer and remove the copy.(when-not (qp.perms/current-user-has-adhoc-native-query-perms? {:database database-id})(common/throw-teaching-error(message/msg ["You do not have permission to run native queries against this database."])diff --git a/src/metabase/metabot/tools/run_query.clj b/src/metabase/metabot/tools/run_query.cljindex 18cd84001c3..320a89dbfa2 100644--- a/src/metabase/metabot/tools/run_query.clj+++ b/src/metabase/metabot/tools/run_query.clj@@ -5,6 +5,7 @@(:require[clojure.string :as str][metabase.api.common :as api]+ [metabase.audit-app.core :as audit][metabase.driver.util :as driver.u][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]@@ -151,6 +152,16 @@{:message (str "You do not have permission to run SQL against the database of query " query-id "."):recovery [(hint-steps hint)]})+(tools/defrecoverable audit-db-sql!+ "The query's database is the audit database, where the QP refuses SQL for all users, admins too."+ {:payload [:map {:closed true}+ [:query-id :string]+ [:hint [:enum :notebook-query :sql-card :no-values]]]}+ [{:keys [query-id hint]}]+ {:message (str "The database of query " query-id " is the audit database. SQL does not run there for any user, "+ "and no permission change can allow it.")+ :recovery [(hint-steps hint)]})+(tools/defrecoverable unreadable-query!"The query can't be normalized, though its database exists and the current user can read it."{:payload [:map {:closed true}@@ -372,6 +383,10 @@(sql-not-allowed! {:sql-card? sql-card?, :hint hint}))(when-not (readable-database? database-id)(database-not-found! {:query-id query-id}))+ ;; The QP refuses SQL on the audit database for all users, admins too, but `native-query-access?` says yes for+ ;; admins there.+ (when (= audit/audit-db-id database-id)+ (audit-db-sql! {:query-id query-id, :hint hint}))(when-not (sql.common/native-query-access? database-id)(no-sql-permission! {:query-id query-id, :hint hint}))))diff --git a/src/metabase/query_processor/middleware/permissions.clj b/src/metabase/query_processor/middleware/permissions.cljindex bdad34aa1aa..0593544576d 100644--- a/src/metabase/query_processor/middleware/permissions.clj+++ b/src/metabase/query_processor/middleware/permissions.clj@@ -68,7 +68,7 @@metabase-enterprise.audit-app.permissions[query](throw (ex-info (tru "Querying this database requires the audit-app feature flag")- query)))+ {:type qp.error-type/disabled-feature, :query query})))(mu/defn check-query-permissions*"Check that User with `user-id` has permissions to run `query`, or throw an exception."