Skip to content

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

Let Metabot recover when a legacy tool can't read an entity (#84816)

Andrei Simionescu00aa4cb049ce
diff --git a/src/metabase/metabot/tools/core.clj b/src/metabase/metabot/tools/core.clj
index de10da5184a..3d68608ad7f 100644
--- a/src/metabase/metabot/tools/core.clj
+++ b/src/metabase/metabot/tools/core.clj
@@ -430,7 +430,7 @@
;;; ------------------------------------------------ Converters ----------------------------------------------------
-(def ^:private refusal-status-codes
+(def refusal-status-codes
"The statuses that mean \"you may not see this\" or \"this is not here\". `api/read-check` raises a
403 for the first and a 404 for the second, and the converters collapse them."
#{403 404})
diff --git a/src/metabase/metabot/tools/legacy.clj b/src/metabase/metabot/tools/legacy.clj
index 31a61916c76..b6b2cf21f72 100644
--- a/src/metabase/metabot/tools/legacy.clj
+++ b/src/metabase/metabot/tools/legacy.clj
@@ -17,16 +17,17 @@
`satisfies? Tool` stops meaning anything and a var passed in by mistake fails somewhere inside
`declaration` rather than where it was registered. `adapt` refuses it by name, at the call site.
- Behaviour is preserved rather than improved. An unconverted tool that returns a success-shaped
- failure still looks like a success, because nothing here can tell `{:output \"Failed to read the
- card\"}` from `{:output \"<card>…</card>\"}`. That is the thing conversion fixes, one tool at a
- time.
+ Behaviour is preserved rather than improved, except that a read refusal becomes `not-found!`. An
+ unconverted tool that returns a success-shaped failure still looks like a success, because nothing
+ here can tell `{:output \"Failed to read the card\"}` from `{:output \"<card>…</card>\"}`. That
+ is the thing conversion fixes, one tool at a time.
This namespace is deleted when the last tool is converted."
(:require
[clojure.string :as str]
[metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.error :as tools.error]
+ [metabase.metabot.tools.recoverable.common :as recoverable.common]
[metabase.metabot.tools.recoverable.legacy :as recoverable.legacy]))
(set! *warn-on-reflection* true)
@@ -96,10 +97,11 @@
"Re-raise `e` from an old-shape tool as a declared error, or rethrow it.
`:terminal-error?` and `:agent-error?` are the two flags the old shape used to say who the message
- was for. Anything without one is a failure nobody wrote text for, so it passes through and becomes
- unrecoverable — the same judgement `with-pipeline-errors` makes."
+ was for. Anything without one is a failure nobody wrote text for: a read refusal becomes
+ `not-found!`, and the rest passes through and becomes unrecoverable. `with-pipeline-errors` makes
+ the same judgement."
[tool-name ^Throwable e]
- (let [{:keys [agent-error? terminal-error?]} (ex-data e)]
+ (let [{:keys [agent-error? terminal-error? status-code]} (ex-data e)]
(cond
;; Already a declared error: raised by a converted tool this one called, or by a shared
;; converter such as `with-pipeline-errors`. A declared recoverable carries `:agent-error?`
@@ -120,6 +122,9 @@
:tool-name tool-name}
{:cause e})
+ (contains? tools/refusal-status-codes status-code)
+ (tools.error/throw-recoverable! ::recoverable.common/not-found {} {:cause e})
+
:else
(throw e))))
diff --git a/src/metabase/metabot/tools/recoverable/common.clj b/src/metabase/metabot/tools/recoverable/common.clj
index 8d3b7e73f18..7d99fd21d96 100644
--- a/src/metabase/metabot/tools/recoverable/common.clj
+++ b/src/metabase/metabot/tools/recoverable/common.clj
@@ -20,10 +20,10 @@
(defrecoverable not-found!
"The entity does not exist, or the current user cannot read it."
{:payload [:map {:closed true}
- ;; Both optional because the two converters know different amounts.
- ;; `with-entity` wraps a specific lookup and names the entity; a 403 or 404 that
- ;; surfaces from deep inside the representations pipeline often does not say which
- ;; reference failed, and inventing a kind would be worse than omitting it.
+ ;; Both optional because the converters know different amounts. `with-entity`
+ ;; wraps a specific lookup and names the entity; a 403 or 404 that surfaces from
+ ;; deep inside the representations pipeline or an unconverted tool often does not
+ ;; say which reference failed, and inventing a kind would be worse than omitting it.
[:kind {:optional true} entity-kinds]
[:id {:optional true} [:or :string :int]]]
:status-code 404}
diff --git a/test/metabase/metabot/tools/legacy_test.clj b/test/metabase/metabot/tools/legacy_test.clj
index 8bffe97dc18..8c6969e4b67 100644
--- a/test/metabase/metabot/tools/legacy_test.clj
+++ b/test/metabase/metabot/tools/legacy_test.clj
@@ -3,6 +3,7 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.api.common :as api]
[metabase.metabot.scope :as scope]
[metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.error :as tools.error]
@@ -70,6 +71,26 @@
[_args :- [:map {:closed true}]]
{:output "Failed to read the card."})
+(defn- refuse-read!
+ "Throw what `api/read-check` throws for an entity the user can't read (403) or that doesn't exist (404)."
+ [status]
+ (if (= 403 status)
+ (api/check-403 false)
+ (api/check-404 nil)))
+
+(mu/defn ^{:tool-name "old_refused_read"}
+ old-refused-read-tool
+ "Lets a read refusal escape, as `get_field_values` does."
+ [{:keys [status]} :- [:map {:closed true} [:status [:enum 403 404]]]]
+ (refuse-read! status))
+
+(mu/defn ^{:tool-name "old_named_refused_read"}
+ old-named-refused-read-tool
+ "Names the table whose read was refused, with `with-entity`."
+ [{:keys [status]} :- [:map {:closed true} [:status [:enum 403 404]]]]
+ (tools/with-entity {:kind :table :id 5}
+ (refuse-read! status)))
+
;;; -------------------------------------------- A converted tool --------------------------------------------------
(tools.error/defrecoverable new-no-widget!
@@ -101,7 +122,7 @@
(tools/entries
(tools.legacy/adapt-all [#'old-plain-tool #'old-rich-tool #'old-agent-error-tool
#'old-terminal-error-tool #'old-terminal-result-tool #'old-crash-tool
- #'old-quiet-failure-tool
+ #'old-quiet-failure-tool #'old-refused-read-tool #'old-named-refused-read-tool
new-widget-tool])))
(defn- invoke
@@ -118,7 +139,7 @@
(deftest ^:parallel both-kinds-live-in-one-profile-test
(testing "entries accepts a var and a record side by side"
(is (= #{"old_plain" "old_rich" "old_agent_error" "old_terminal_error" "old_terminal_result"
- "old_crash" "old_quiet_failure" "new_widget"}
+ "old_crash" "old_quiet_failure" "old_refused_read" "old_named_refused_read" "new_widget"}
(set (keys entries)))))
(testing "and one call path reaches both"
(is (= {:output "plain 1"} (invoke "old_plain" {:n 1})))
@@ -211,6 +232,19 @@
(doseq [leak ["H2" "mb_admin" "SELECT"]]
(is (not (str/includes? (:output outcome) leak)) (str "leaked " leak))))))
+(deftest ^:parallel a-refused-read-is-not-found-test
+ (doseq [status [403 404]]
+ (testing (str "a " status " is the declared not-found error, worded the same for both, so the model can "
+ "recover without learning whether an entity it may not see exists")
+ (is (=? {:output (str "The entity referenced here was not found. It may not exist, or you may not have "
+ "access to it.\nCall `search` to find the entity you want and use an id from the "
+ "results.")
+ :error {:class :recoverable :code :metabase.metabot.tools.recoverable.common/not-found}}
+ (invoke "old_refused_read" {:status status}))))
+ (testing (str "a " status " that the tool attributes keeps the entity's kind and id")
+ (is (str/starts-with? (:output (invoke "old_named_refused_read" {:status status}))
+ "Table 5 was not found.")))))
+
(deftest ^:parallel behaviour-is-preserved-not-improved-test
(testing "a tool that catches its own error and returns it as `:output` still looks like a
success. Nothing here can tell that string from a real result. Converting the tool is
diff --git a/test/metabase/metabot/tools/metadata_test.clj b/test/metabase/metabot/tools/metadata_test.clj
index 79a1bb521ea..f43a04a088d 100644
--- a/test/metabase/metabot/tools/metadata_test.clj
+++ b/test/metabase/metabot/tools/metadata_test.clj
@@ -1,6 +1,9 @@
(ns metabase.metabot.tools.metadata-test
(:require
[clojure.test :refer :all]
+ [metabase.metabot.scope :as scope]
+ [metabase.metabot.test-util :as test-util]
+ [metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.metadata :as metadata-tools]
[metabase.models.interface :as mi]
[metabase.test :as mt]
@@ -60,6 +63,16 @@
(metadata-tools/get-field-values-tool
{:data_source data-source :source_id question-id :field_id 1}))))))))))
+(deftest get-field-values-tool-reports-a-refused-table-as-not-found-test
+ (testing "a table the user can't read and one that doesn't exist get the same error, which the model can recover from"
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (binding [scope/*current-user-scope* #{"*"}]
+ (doseq [table-id [(mt/id :venues) Integer/MAX_VALUE]]
+ (is (= "The entity referenced here was not found. It may not exist, or you may not have access to it."
+ (test-util/tool-boundary-error "get_field_values" #'metabot.tools/get-field-values-tool
+ {:data_source "table" :source_id table-id :field_id 1})))))))))
+
(deftest get-field-values-tool-unknown-data-source-test
(testing "an unexpected data_source -- e.g. because the schema that constrains it isn't enforced in
production -- doesn't throw before reaching field-values, which reports it gracefully"