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
(ns metabase-enterprise.database-routing.api-test
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[metabase-enterprise.test :as met]
[metabase.driver :as driver]
[metabase.driver.settings :as driver.settings]
[metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.resources :as read-resource]
[metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
[metabase.test :as mt]
[toucan2.core :as t2]))
(defn with-h2-fixture [f]
(binding [driver.settings/*allow-testing-h2-connections* true]
(f)))
(defn with-premium-feature-fixture [f]
(mt/with-premium-features #{:database-routing}
(f)))
(defn with-test-db-is-router-db [f]
(mt/with-temp [:model/DatabaseRouter _ {:database_id (mt/id)
:user_attribute "meow"}]
(f)))
(use-fixtures :each with-h2-fixture with-premium-feature-fixture with-test-db-is-router-db)
(deftest creation-works
(mt/with-model-cleanup [:model/Database]
(let [[{db-id :id}] (mt/user-http-request :crowberto :post 200 "ee/database-routing/destination-database"
{:router_database_id (mt/id)
:destinations [{:name "destination database"
:details (:details (mt/db))}]})]
(is (t2/exists? :model/Database
:router_database_id (mt/id)
:id db-id)))))
(deftest invalid-details-doesnt-matter
(mt/with-model-cleanup [:model/Database]
(with-redefs [driver/can-connect? (fn [_ _]
(throw (ex-info "nope" {})))]
(let [[{db-id :id}] (mt/user-http-request :crowberto :post 200 "ee/database-routing/destination-database"
{:router_database_id (mt/id)
:destinations [{:name (str (random-uuid))
:details {:db "meow"}}]})]
(is (t2/exists? :model/Database
:router_database_id (mt/id)
:id db-id)))
(testing "unless you pass the `check_connection_details` flag"
(let [db-name (str (random-uuid))]
(is (= {(keyword db-name) {:message "nope"}}
(mt/user-http-request :crowberto :post 400 "ee/database-routing/destination-database?check_connection_details=true"
{:router_database_id (mt/id)
:destinations [{:name db-name
:details {:db "meow"}}]}))))))))
(deftest we-can-mark-an-existing-database-as-being-a-router-database
(mt/with-temp [:model/Database {db-id :id} {}]
(mt/with-model-cleanup [:model/DatabaseRouter]
(mt/user-http-request :crowberto :put 200 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "foo"})
(is (t2/exists? :model/DatabaseRouter :database_id db-id :user_attribute "foo")))))
(deftest marking-a-nonexistent-database-as-a-router-database-fails
(let [nonexistent-id 123456789]
(mt/with-model-cleanup [:model/DatabaseRouter]
(mt/user-http-request :crowberto :put 404 (str "ee/database-routing/router-database/" nonexistent-id)
{:user_attribute "foo"})
(is (not (t2/exists? :model/DatabaseRouter :database_id nonexistent-id :user_attribute "foo"))))))
(deftest we-can-update-existing-router-databases-to-point-to-a-new-user-attribute
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter {router-id :id} {:database_id db-id :user_attribute "foo"}]
(mt/user-http-request :crowberto :put 200 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "bar"})
(is (= "bar" (t2/select-one-fn :user_attribute :model/DatabaseRouter :id router-id)))))
(deftest deleting-database-routers-works
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter {router-id :id} {:database_id db-id :user_attribute "foo"}
:model/Database {destination-db-id :id} {:router_database_id db-id}]
(mt/user-http-request :crowberto :put 200 (str "ee/database-routing/router-database/" db-id) {:user_attribute nil})
;; the destination databases are left around
(is (t2/exists? :model/Database :id destination-db-id))
(is (not (t2/exists? :model/DatabaseRouter :id router-id)))))
(deftest endpoint-are-locked-down-to-superusers-only
(testing "POST /api/ee/database-routing/destination-database"
(mt/with-model-cleanup [:model/Database]
(mt/user-http-request :rasta :post 403 "ee/database-routing/destination-database"
{:router_database_id (mt/id)
:destinations [{:name "destination database"
:details (:details (mt/db))}]})))
(testing "PUT /api/ee/database-routing/router-database/:id"
(mt/with-temp [:model/Database {db-id :id} {}]
(mt/with-model-cleanup [:model/DatabaseRouter]
(mt/user-http-request :rasta :put 403 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "foo"})))))
(deftest router-databases-have-a-router-user-attribute-on-the-get-api
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foobar"}]
(is (= "foobar"
(:router_user_attribute (mt/user-http-request :crowberto :get 200 (str "database/" db-id)))))
(is (contains? (into #{} (map :router_user_attribute (:data (mt/user-http-request :crowberto :get 200 "database/"))))
"foobar"))))
(deftest cannot-create-duplicate-names
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foobar"}
:model/Database _ {:name "fluffy" :router_database_id db-id}]
(is (= "A destination database with that name already exists."
(mt/user-http-request :crowberto :post 400 "ee/database-routing/destination-database"
{:router_database_id db-id
:destinations [{:name "fluffy"
:details (:details (mt/db))}]})))))
(deftest cannot-enable-db-routing-when-other-features-enabled
(mt/with-temp [:model/Database {db-id :id} {:settings {:persist-models-enabled true}}]
(is (= "Cannot enable database routing for a database with model persistence enabled"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"}))))
(mt/with-temp [:model/Database {db-id :id} {:settings {:database-enable-actions true}}]
(is (= "Cannot enable database routing for a database with actions enabled"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"}))))
(mt/with-temp [:model/Database {db-id :id} {:uploads_enabled true}]
(is (= "Cannot enable database routing for a database with uploads enabled"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"}))))
(mt/with-temp [:model/Database {db-id :id} {:write_data_details {:host "other-host"}}]
(is (= "Cannot enable database routing for a database with a write connection configured"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"}))))
(mt/with-temp [:model/Database {router-db-id :id} {}
:model/Database {db-id :id} {:router_database_id router-db-id}]
(is (= "Cannot make a destination database a router database"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"}))))
(mt/with-temp [:model/Database {db-id :id} {}
:model/Transform _ {:source_database_id db-id
:name "Test Transform"}]
(is (= "Cannot enable database routing for a database with transforms"
(mt/user-http-request :crowberto :put 400 (str "ee/database-routing/router-database/" db-id)
{:user_attribute "db_name"})))))
(deftest can-delete-router-databases
(mt/with-temp [:model/Database {db-id :id} {}
:model/Database {dest-db-id :id} {:router_database_id db-id}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}]
(mt/user-http-request :crowberto :delete 204 (str "database/" db-id))
(is (not (t2/exists? :model/Database :id dest-db-id)))
(is (not (t2/exists? :model/Database :id db-id)))))
(deftest destination-databases-are-hidden-from-regular-database-api
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}
:model/Database {destination-db-id :id} {:router_database_id db-id}]
(testing "GET /database/:id"
(testing "Superusers can get destination DBs"
(mt/user-http-request :crowberto :get 200 (str "database/" destination-db-id)))
(testing "Regular users can not."
(mt/user-http-request :rasta :get 403 (str "database/" destination-db-id))))
(testing "GET /database/"
(is (not-any? #(= (:id %) destination-db-id)
(:data (mt/user-http-request :crowberto :get 200 "database/"))))
(testing "If we pass the `router_database_id` param it is included"
(is (some #(= (:id %) destination-db-id)
(:data (mt/user-http-request :crowberto :get 200 (str "database/?router_database_id=" db-id))))))
(testing "Regular users can't do this"
(is (not-any? #(= (:id %) destination-db-id)
(:data (mt/user-http-request :rasta :get 200 (str "database/?router_database_id=" db-id))))))
(testing "Unless they have manage-database permissions"
(mt/with-no-data-perms-for-all-users!
(perms/set-database-permission! (perms/all-users-group) db-id :perms/manage-database :yes)
(perms/set-database-permission! (perms/all-users-group) db-id :perms/create-queries :query-builder-and-native)
(t2/select :model/DataPermissions :db_id db-id :perm_type "perms/create-queries")
(is (some #(= (:id %) destination-db-id)
(:data (mt/user-http-request :rasta :get 200 (str "database/?router_database_id=" db-id)))))
(perms/set-database-permission! (perms/all-users-group) db-id :perms/manage-database :no)
(is (not (some #(= (:id %) destination-db-id)
(:data (mt/user-http-request :rasta :get 200 (str "database/?router_database_id=" db-id)))))))))
(testing "PUT /database/:id should work normally"
(mt/user-http-request :crowberto :put 200 (str "database/" destination-db-id)))
(testing "GET /database/:id/usage_info"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/usage_info")))
(testing "GET /database/:id/metadata"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/metadata")))
(testing "GET /database/:id/autocomplete_suggestions"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/autocomplete_suggestions")))
(testing "GET /database/:id/card_autocomplete_suggestions"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/card_autocomplete_suggestions?query=foobar")))
(testing "GET /database/:id/fields"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/fields")))
(testing "GET /database/:id/idfields"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/idfields")))
(testing "POST /database/:id/sync_schema"
(mt/user-http-request :crowberto :post 404 (str "database/" destination-db-id "/sync_schema")))
(testing "POST /database/:id/dismiss_spinner"
(mt/user-http-request :crowberto :post 404 (str "database/" destination-db-id "/dismiss_spinner")))
(testing "POST /database/:id/rescan_values"
(mt/user-http-request :crowberto :post 404 (str "database/" destination-db-id "/rescan_values")))
(testing "POST /database/:id/discard_values"
(mt/user-http-request :crowberto :post 404 (str "database/" destination-db-id "/discard_values")))
(testing "POST /database/:id/syncable_schemas"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/syncable_schemas")))
(testing "GET /database/:id/schemas"
(mt/user-http-request :crowberto :get 404 (str "database/" destination-db-id "/schemas")))))
(deftest routed-destination-database-names-are-hidden-in-metabot-resources
(mt/with-temp [:model/Database {router-id :id} {:name "Postgres Router"}
:model/DatabaseRouter _ {:database_id router-id :user_attribute "db_name"}
:model/Database {selected-destination-id :id} {:name "customer-a"
:router_database_id router-id}
:model/Database {other-destination-id :id} {:name "customer-b"
:router_database_id router-id}]
(met/with-user-attributes! :rasta {"db_name" "customer-a"}
(mt/with-current-user (mt/user->id :rasta)
(testing "database list includes the router and hides destination databases"
(let [{:keys [output]} (test-util/call-tool read-resource/read-resource-tool
{:uris ["metabase://databases"]})]
(is (str/includes? output "Postgres Router"))
(is (not (str/includes? output "customer-a")))
(is (not (str/includes? output "customer-b")))))
;; A read that found nothing is a failed call, so these assert the failure rather than
;; probing a per-URI `:error` — that list is the Agent API's shape, and the tool no longer
;; builds it.
(testing "a sibling destination cannot be read by URI"
(is (= :metabase.metabot.tools.core/all-items-failed
(:code (test-util/tool-failure
read-resource/read-resource-tool
{:uris [(str "metabase://database/" other-destination-id)]})))))
(testing "the current user's routed destination cannot be read by URI"
(is (= :metabase.metabot.tools.core/all-items-failed
(:code (test-util/tool-failure
read-resource/read-resource-tool
{:uris [(str "metabase://database/" selected-destination-id)]})))))))))
(deftest destination-databases-excluded-from-permissions-graph
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}
:model/Database {destination-db-id :id} {:router_database_id db-id}]
(testing "Permissions graph does not include destination databases"
(let [graph (data-perms.graph/data-permissions-graph)]
(is (not (some (fn [[_group-id db-perms]]
(contains? db-perms destination-db-id))
graph)))))
(testing "API permissions graph does not include destination databases"
(let [api-graph (:groups (data-perms.graph/api-graph))]
(is (not (some (fn [[_group-id db-perms]]
(contains? db-perms destination-db-id))
api-graph)))))))
(deftest new-group-does-not-get-destination-database-permissions
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}
:model/Database {destination-db-id :id} {:router_database_id db-id}]
(mt/with-temp [:model/PermissionsGroup {group-id :id} {:name "Test Group for Routing"}]
(testing "New group should have permissions for the router database"
(is (t2/exists? :model/DataPermissions :group_id group-id :db_id db-id)))
(testing "New group should NOT have permissions for the destination database"
(is (not (t2/exists? :model/DataPermissions :group_id group-id :db_id destination-db-id)))))))
(deftest manage-db-user-can-update-destination-database-test
(testing "PUT /api/database/:id on a routed destination honors manage-database perms held on its router database"
;; The permission lives on the router database; the destination itself carries no grant of its own. Without
;; `:advanced-permissions`, `current-user-can-write-db?` falls back to the OSS superuser check, so the grant would
;; be a no-op and the assertions below would pass for the wrong reason.
(mt/with-additional-premium-features #{:advanced-permissions}
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}
:model/Database {dest :id} {:router_database_id db-id :name "Destination DB 1"}]
(mt/with-no-data-perms-for-all-users!
(testing "without manage-database perms on the router the update is refused"
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :put 403 (str "database/" dest) {:name "Renamed"})))
(is (= "Destination DB 1" (t2/select-one-fn :name :model/Database :id dest))))
(perms/set-database-permission! (perms/all-users-group) db-id :perms/manage-database :yes)
(testing "with manage-database perms on the router the update succeeds"
(is (=? {:id dest}
(mt/user-http-request :rasta :put 200 (str "database/" dest) {:name "Renamed"})))
(is (= "Renamed" (t2/select-one-fn :name :model/Database :id dest)))))))))
(deftest manage-db-user-cannot-delete-destination-database-test
(testing "DELETE /api/database/:id is rejected (403) for a non-superuser with manage-database perms on a routed destination"
;; NOTE: `DELETE /api/database/:id` calls `api/check-superuser` unconditionally before it ever looks at the
;; database being deleted (see `metabase.warehouses-rest.api`), so this endpoint is admin-only full stop -- it
;; doesn't have any destination-specific carve-out to disprove. To show that manage-database perms are actually
;; being exercised here (and not just trivially irrelevant), we also enable `:advanced-permissions` -- without it,
;; `current-user-can-write-db?` falls back to the OSS `superuser?` check and the manage-database grant below would
;; be a no-op -- and we assert the *same* grant is refused identically for a regular database, proving there's no
;; accidental carve-out that would let a manage-database non-admin delete regular databases while destinations
;; stay protected (or vice versa).
(mt/with-additional-premium-features #{:advanced-permissions}
(mt/with-temp [:model/Database {db-id :id} {}
:model/DatabaseRouter _ {:database_id db-id :user_attribute "foo"}
:model/Database {dest :id} {:router_database_id db-id}
:model/Database {regular-db-id :id} {}]
(mt/with-no-data-perms-for-all-users!
(perms/set-database-permission! (perms/all-users-group) db-id :perms/manage-database :yes)
(perms/set-database-permission! (perms/all-users-group) db-id :perms/create-queries :query-builder-and-native)
(perms/set-database-permission! (perms/all-users-group) regular-db-id :perms/manage-database :yes)
(perms/set-database-permission! (perms/all-users-group) regular-db-id :perms/create-queries :query-builder-and-native)
(testing "manage-database perms don't allow deleting a regular database either"
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :delete 403 (str "database/" regular-db-id)))))
(testing "manage-database perms on the router database don't allow deleting its destination database"
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :delete 403 (str "database/" dest)))))
(is (t2/exists? :model/Database :id regular-db-id))
(is (t2/exists? :model/Database :id dest)))))))