Skip to content

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

Workspaces cleanup (#77875)

Alexander Polyankin05fa507ed7d7
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 51124a15da4..9ffffdb1df9 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -6,6 +6,17 @@ title: Driver interface changelog
## Metabase 0.64.0
+- `metabase.driver/workspace-isolation-details` `[driver database workspace]` -- new workspace-isolation
+ multimethod. Computes the isolation identifiers (`:schema`, and driver-specific `:database_details` such as
+ user/password) for a workspace *before* any warehouse work happens; `init-workspace-isolation!`,
+ `grant-workspace-read-access!`, and `destroy-workspace-isolation!` now receive those identifiers on the
+ `workspace` map instead of computing them internally. `:sql-jdbc` drivers inherit a default implementation
+ (deterministic schema/user names plus a random password); non-JDBC drivers that support workspaces must
+ implement it themselves.
+
+- `metabase.driver/check-isolation-permissions`, added in 0.59.0, has been removed. Permission problems now
+ surface as failures from `init-workspace-isolation!`/`grant-workspace-read-access!` themselves.
+
- Added a `:native-pivot-tables` driver feature flag for drivers that can compile a pivot query as a single
`GROUP BY GROUPING SETS (...)` statement instead of the legacy multi-query path. Drivers that opt in must also
derive from `:sql-mbql5` (which provides the `:pivot` clause compiler). Defaults to `false`.
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
index 26aa37443a7..9d7778492b0 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
@@ -64,8 +64,8 @@
[:creator [:maybe CreatorResponse]]
[:created_at DateTimeWithTimeZone]
[:updated_at DateTimeWithTimeZone]
- ;; `:databases` is only included when hydrated (i.e. the GET /:id endpoint).
- ;; The list endpoint omits it — clients should treat a missing array as `[]`.
+ ;; Both the list and GET /:id endpoints hydrate `:databases`; it stays
+ ;; optional so clients treat a missing array as `[]`.
[:databases {:optional true} [:sequential WorkspaceDatabaseResponse]]])
;;; -------------------------------------------- Presentation --------------------------------------------------
@@ -121,30 +121,16 @@
(t2/update! :model/Workspace :id id {:name (:name params)}))
(present-workspace (api/check-404 (ws/get-workspace id))))
-(api.macros/defendpoint :delete "/:id"
- :- [:map
- [:id ms/PositiveInt]
- [:deleted :boolean]
- [:message {:optional true} :string]
- [:orphaned_resources {:optional true}
- [:sequential [:map
- [:workspace_database_id ms/PositiveInt]
- [:database_id ms/PositiveInt]
- [:driver :keyword]
- [:schema :string]
- [:user :string]
- [:reason {:optional true} [:maybe :string]]]]]]
- "Delete a Workspace. Tears down each `:provisioned` database's warehouse isolation
- first (blocking). Refuses with a 409 if any database is still `:provisioning`/
- `:deprovisioning` unless `ignore_pending=true`, in which case those databases are
- left in the warehouse and only their app-DB rows are removed. If the warehouse was
- unreachable for some `:provisioned` databases, the response includes
- `:orphaned_resources` and a `:message` describing the inert schema/user objects
- left behind for manual cleanup."
- [{:keys [id]} :- [:map [:id ms/PositiveInt]]
- {ignore-pending? :ignore-pending} :- [:map [:ignore-pending {:default false} [:maybe ms/BooleanValue]]]]
+(api.macros/defendpoint :delete "/:id" :- :nil
+ "Delete a Workspace. Tears down every database's warehouse isolation first
+ (blocking, any state). Each database is either fully torn down (its row is
+ deleted) or kept; when any teardown fails the workspace is kept too — with the
+ failed rows, so the delete can be retried — and the combined failure is
+ returned as the error. 204 on success, no response body."
+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]
(api/write-check :model/Workspace id)
- (assoc (ws/delete-workspace! id (boolean ignore-pending?)) :id id))
+ (ws/delete-workspace! id)
+ nil)
;;; ------------------------------------------- Config download --------------------------------------------------
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
index e5d6db945b8..faecf90ce24 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
@@ -39,11 +39,12 @@
[metabase.driver.sql :as driver.sql]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.settings.core :as setting]
- [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.workspaces.core :as ws]
[toucan2.core :as t2]))
+(set! *warn-on-reflection* true)
+
(defn engine-namespace-positions
"Return `{:db ?, :schema ?}` — the values that should populate the `:db` and
`:schema` AST slots for a Table row in `database`. `table` is optional; pass
@@ -172,9 +173,9 @@
;;; ------------------------------------- Manager-side helpers ------------------------------------------------
(defn- assert-workspace-exists [workspace-id]
- (or (workspace/get-workspace workspace-id)
- (throw (ex-info "Workspace not found"
- {:status-code 404 :workspace_id workspace-id}))))
+ (when-not (t2/exists? :model/Workspace :id workspace-id)
+ (throw (ex-info "Workspace not found"
+ {:status-code 404 :workspace_id workspace-id}))))
(defn- assert-database-exists [database-id]
(or (t2/select-one :model/Database :id database-id)
@@ -201,82 +202,108 @@
;;; ------------------------------------- Manager-side writes -------------------------------------------------
+(defn- combined-exception
+ "One exception carrying every failure in `throwables`: the first is the cause,
+ the rest are attached as suppressed (the JDK mechanism for multiple failures
+ on one throwable). The message is the messages the databases returned, joined
+ into one line. The ex-data is shaped so the API error middleware returns just
+ that message — without it, an uncaught 500 dumps the full `Throwable->map`
+ cause chain into the response."
+ ^Throwable [throwables]
+ (let [message (str/join "; " (map #(or (ex-message %) (str (class %))) throwables))
+ ^Throwable ex (ex-info message
+ {:status-code 500
+ :message message
+ :errors {:_error message}}
+ (first throwables))]
+ (run! #(.addSuppressed ex ^Throwable %) (rest throwables))
+ ex))
+
+(defn- provision-workspace-databases!
+ "Provision every WorkspaceDatabase of the hydrated workspace `ws` synchronously
+ (blocking), failing fast on the first error."
+ [ws]
+ (doseq [{wsd-id :id} (:databases ws)]
+ (provisioning/provision-single! wsd-id)))
+
+(defn- teardown-workspace-databases!
+ "Tear down every WorkspaceDatabase of `workspace-id` — any state, blocking.
+ Mirrors [[provision-workspace-databases!]], but continues past failures so each
+ row gets its attempt: rows whose teardown succeeds are deleted immediately
+ (progress is persisted per row, so an instance crash midway loses nothing);
+ rows whose teardown fails are kept. Throws the combined failures when any
+ teardown failed."
+ [workspace-id]
+ (let [failures (into []
+ (keep (fn [wsd]
+ (try
+ (provisioning/teardown-workspace-database! wsd provisioning/dispatching-provisioner)
+ nil
+ (catch Throwable t t))))
+ (t2/select :model/WorkspaceDatabase :workspace_id workspace-id))]
+ (when (seq failures)
+ (throw (combined-exception failures)))))
+
(defn create-workspace!
"Create a new Workspace, attach the databases with ids `database_ids` — each must
exist (404) and be eligible for workspaces (400, see
[[workspace-database/database-eligible-for-workspaces?]]) — with all of their
- known schemas as
- `input_schemas`, and provision each database (blocking). Everything runs in a
- single transaction, so a provisioning failure rolls back the workspace and its
- database rows. Returns the created workspace, hydrated."
+ known schemas as `input_schemas`, and provision each database (blocking).
+ Returns the created workspace, hydrated.
+
+ The workspace and its WorkspaceDatabase rows are committed BEFORE provisioning
+ starts — deliberately NOT one big transaction. The rows are the durable record
+ of warehouse resources that may (partially) exist, so they must survive an
+ instance crash mid-provision; a rollback would erase them while the warehouse
+ objects live on. Cleanup after a provisioning failure is therefore explicit:
+ every database is torn down; when the cleanup removes everything, the Workspace
+ row is deleted and the provisioning error is rethrown; when the cleanup itself
+ fails, the workspace is kept — and returned like a successful create — with the
+ failed rows (`:unprovisioned`) so the leak stays visible and the teardown can
+ be retried via delete. Callers detect that case from the databases' statuses."
[{:keys [name creator_id database_ids]}]
(let [databases (mapv (fn [db-id]
(let [database (assert-database-exists db-id)]
(assert-database-eligible-for-workspaces database)
{:database_id db-id
:input_schemas (workspace-database/database-input-schemas database)}))
- database_ids)]
- (t2/with-transaction [_conn]
- (let [ws (workspace/create-workspace! {:name name
- :creator_id creator_id
- :databases databases})]
- (doseq [{wsd-id :id} (:databases ws)]
- (provisioning/provision-single! wsd-id))
- (workspace/get-workspace (:id ws))))))
-
-(defn- orphaned-resources-message
- [workspace-id failures]
- (str (format "Workspace %d was deleted, but warehouse cleanup failed for %d database(s). "
- workspace-id (count failures))
- "These resources were left in place and may need to be removed manually:\n"
- (str/join "\n"
- (for [{:keys [database_id driver schema user reason]} failures]
- (format " - database %d (%s): schema \"%s\", user \"%s\" — not removed because: %s"
- database_id (name driver) schema user reason)))))
-
-(defn- pending-database?
- "True for a WorkspaceDatabase that is mid-provision/deprovision — a state we can't
- safely tear down because warehouse work may still be in flight on another node."
- [wsd]
- (contains? #{:provisioning :deprovisioning} (:status wsd)))
+ database_ids)
+ ws (workspace/create-workspace! {:name name
+ :creator_id creator_id
+ :databases databases})]
+ (try
+ (provision-workspace-databases! ws)
+ (catch Throwable t
+ (let [cleaned-up? (try
+ (teardown-workspace-databases! (:id ws))
+ true
+ ;; Cleanup failed: the workspace and the failed rows are
+ ;; kept — and returned — so the teardown can be retried
+ ;; via delete. Both errors are deliberately swallowed,
+ ;; not logged (raw warehouse errors are sensitive);
+ ;; callers see the failure in the databases' statuses,
+ ;; and a delete retry surfaces the teardown errors.
+ (catch Throwable _ false))]
+ (when cleaned-up?
+ (try
+ (workspace/delete-workspace! (:id ws))
+ (catch Throwable delete-t
+ (.addSuppressed t delete-t)))
+ (throw (combined-exception [t]))))))
+ (workspace/get-workspace (:id ws))))
(defn delete-workspace!
- "Tear down each `:provisioned` database's warehouse isolation (best-effort,
- blocking), then delete the workspace.
-
- `ignore-pending?` (default false) controls databases still `:provisioning` or
- `:deprovisioning`: when false, refuses with a 409 (`:pending_databases` lists
- them) since their warehouse work may be in flight; when true, those databases are
- left untouched in the warehouse and only their app-DB rows are removed.
-
- Returns `{:deleted true}` on clean teardown, or
- `{:deleted true :orphaned_resources [...] :message ..}` when the warehouse was
- unreachable for some `:provisioned` rows and inert schema/user objects were left
- behind. App-DB `TableRemapping` rows are always cleared, so query routing is never
- left dangling."
- ([id] (delete-workspace! id false))
- ([id ignore-pending?]
- (let [ws (assert-workspace-exists id)
- databases (:databases ws)
- pending (filter pending-database? databases)
- active (filter #(= :provisioned (:status %)) databases)]
- (when (and (seq pending) (not ignore-pending?))
- (throw (ex-info "Cannot delete a workspace while some of its databases are still provisioning or deprovisioning"
- {:status-code 409
- :workspace_id id
- :pending_databases (mapv (fn [wsd]
- {:database_id (:database_id wsd)
- :name (:name (:database wsd))
- :status (:status wsd)})
- pending)})))
- (let [results (mapv #(provisioning/force-teardown-for-delete!
- % provisioning/dispatching-provisioner)
- active)
- failures (filterv #(= :failure (:status %)) results)]
- (run! provisioning/ignore-pending-database! pending)
- (workspace/delete-workspace! id)
- (if (seq failures)
- (let [message (orphaned-resources-message id failures)]
- (log/warn message)
- {:deleted true :orphaned_resources failures :message message})
- {:deleted true})))))
+ "Tear down every database's warehouse isolation (any state, blocking), then
+ delete the workspace. There is no partial deletion: each WorkspaceDatabase is
+ either fully torn down (warehouse footprint confirmed gone, row deleted) or
+ kept. Every database gets its teardown attempt even when earlier ones fail; if
+ any of them fail, the workspace is kept alongside the failed rows and one
+ exception combining all the failures is thrown, so the delete can be retried.
+ Progress is persisted per row, so an instance crash midway loses nothing.
+ App-DB `TableRemapping` rows are always cleared, so query routing is never
+ left dangling. Returns nil."
+ [id]
+ (assert-workspace-exists id)
+ (teardown-workspace-databases! id)
+ (workspace/delete-workspace! id)
+ nil)
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
index a8fcb6a0b2d..ce56fcfc9e4 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
@@ -19,8 +19,12 @@
"Wrapper around driver workspace-isolation multimethods for testability.
The default [[dispatching-provisioner]] delegates to the real driver multimethods.
Tests can reify custom implementations that fail on demand, count calls, etc."
+ (details [this driver database workspace]
+ "Compute {:schema ... :database_details ...} for the workspace without touching
+ the warehouse. Called before init! so destroy! can clean up a partial init.")
(init! [this driver database workspace]
- "Create isolated schema + user. Returns {:schema ... :database_details ...}.")
+ "Create isolated schema + user. `workspace` carries the precomputed `:schema`
+ and `:database_details` from [[details]].")
(grant! [this driver database workspace schemas]
"Grant read access on `schemas` to the workspace user/role. `schemas` is a
vector of driver-opaque schema-name strings. 3-slot drivers (SQL Server,
@@ -35,8 +39,13 @@
driver impls acquire connections via the database's `:admin-details` overlay
(when configured). Workspace DDL — `CREATE USER`, `CREATE SCHEMA`, `GRANT` —
typically needs higher-privilege credentials than the regular query user, and
- the admin overlay is how operators provide them."
+ the admin overlay is how operators provide them. `details` computes no DDL but
+ still binds the overlay: some drivers derive connection strings/catalogs from
+ the effective details."
(reify Provisioner
+ (details [_ driver database workspace]
+ (driver.conn/with-admin-connection
+ (driver/workspace-isolation-details driver database workspace)))
(init! [_ driver database workspace]
(driver.conn/with-admin-connection
(driver/init-workspace-isolation! driver database workspace)))
@@ -75,6 +84,13 @@
[workspace-database-id & body]
`(do-with-workspace-database-lock ~workspace-database-id (fn [] ~@body)))
+(defn- wsd-iso-workspace
+ "The synthetic workspace map from which all warehouse identifiers for a
+ WorkspaceDatabase row are derived. Every provision/deprovision/teardown path
+ must build the exact same map — teardown recomputes what provision computed."
+ [workspace-database-id]
+ {:id workspace-database-id :name (str "wsd-" workspace-database-id)})
+
;;; ---------------------------------------- Single-database operations -----------------------------------------------
(defn provision-workspace-database!
@@ -90,21 +106,25 @@
(when-not (= :provisioning (:status wsd))
(throw (ex-info "WorkspaceDatabase must be :provisioning to provision"
{:id workspace-database-id :status (:status wsd)})))
- (let [db (t2/select-one :model/Database :id (:database_id wsd))
- driver (driver.u/database->driver db)
- workspace {:id workspace-database-id :name (str "wsd-" workspace-database-id)}
- init-result (init! provisioner driver db workspace)
- ws-details (merge workspace init-result)
- schemas (vec (:input_schemas wsd))]
+ (let [db (t2/select-one :model/Database :id (:database_id wsd))
+ driver (driver.u/database->driver db)
+ workspace (wsd-iso-workspace workspace-database-id)
+ ws-details (merge workspace (details provisioner driver db workspace))
+ schemas (vec (:input_schemas wsd))]
(try
+ (init! provisioner driver db ws-details)
(grant! provisioner driver db ws-details schemas)
(catch Throwable t
- (destroy! provisioner driver db ws-details)
+ (try
+ (destroy! provisioner driver db ws-details)
+ (catch Throwable destroy-t
+ (log/warnf destroy-t "Failed to clean up after provisioning failure for WorkspaceDatabase %s"
+ workspace-database-id)))
(throw t)))
(t2/update! :model/WorkspaceDatabase
{:id workspace-database-id}
- {:output_namespace (:schema init-result)
- :database_details (:database_details init-result)
+ {:output_namespace (:schema ws-details)
+ :database_details (:database_details ws-details)
:status :provisioned}))))
(catch Throwable t
(t2/update! :model/WorkspaceDatabase
@@ -128,10 +148,9 @@
{:id workspace-database-id :status (:status wsd)})))
(let [db (t2/select-one :model/Database :id (:database_id wsd))
driver (driver.u/database->driver db)
- workspace {:id workspace-database-id
- :name (str "wsd-" workspace-database-id)
- :schema (:output_namespace wsd)
- :database_details (:database_details wsd)}]
+ workspace (assoc (wsd-iso-workspace workspace-database-id)
+ :schema (:output_namespace wsd)
+ :database_details (:database_details wsd))]
(try
(destroy! provisioner driver db workspace)
(finally
@@ -207,73 +226,62 @@
(log/warnf t "Failed to provision WorkspaceDatabase %s" id)))))
triggered)))
-(defn force-teardown-for-delete!
- "Best-effort warehouse teardown for a single WorkspaceDatabase, used only by the
- delete path. Drops the iso schema + user (idempotent), always clears the row's
- app-DB `TableRemapping` rows, then forces the row to `:unprovisioned` regardless
- of the warehouse outcome.
+(defn teardown-workspace-database!
+ "Tear down one WorkspaceDatabase's warehouse isolation and, on success, delete
+ its row. Used by the delete path and by create-workspace cleanup. Works from ANY
+ state: the per-row cluster lock is taken first, so in-flight
+ provisioning/deprovisioning on another node completes before we touch anything
+ (or the lock times out and the teardown is reported as a failure), and the row
+ is re-read under the lock so we see whatever that work persisted. Identifiers
+ are the persisted ones, or
+ recomputed deterministically from the row id via the provisioner's `details`
+ ([[metabase.driver/workspace-isolation-details]]) for a crashed `:provisioning`
+ row that never recorded anything.
- Works from ANY non-`:unprovisioned` state: the iso schema and user names are
- deterministic from the row id (see [[metabase.driver.util/workspace-isolation-namespace-name]]),
- so the destroy target is reconstructable even for a row stuck in `:provisioning`
- that never stored its `:output_namespace`/`:database_details`.
+ There is no partial outcome: the teardown either fully succeeds or the row
+ stays. The row is the durable record that warehouse resources may exist — it
+ must survive instance crashes and disappear only once the warehouse footprint
+ is confirmed gone. On success the WorkspaceDatabase row is DELETED and the
+ app-DB `TableRemapping` rows for the iso namespace are cleared — stale
+ remappings would rewrite queries to a dropped schema and 500 the QP.
- Returns `{:status :success}` when the warehouse objects were dropped, or
- `{:status :failure :workspace_database_id .. :database_id .. :driver .. :schema ..
- :user .. :reason ..}` when the warehouse was unreachable and inert objects were
- left behind for manual cleanup."
+ Throws on failure. When the destroy itself fails, the row bookkeeping runs
+ first: the row is kept, forced `:unprovisioned`, and the remapping cleanup
+ still runs. Failures before the destroy — lock timeout, identifier
+ computation — throw without touching the row. Either way the teardown can be
+ retried. Returns nil."
[wsd provisioner]
(with-workspace-database-lock (:id wsd)
- (let [db (t2/select-one :model/Database :id (:database_id wsd))
- driver (driver.u/database->driver db)
- iso-ws {:id (:id wsd) :name (str "wsd-" (:id wsd))}
- schema (or (not-empty (:output_namespace wsd))
- (driver.u/workspace-isolation-namespace-name iso-ws))
- user (or (get-in wsd [:database_details :user])
- (driver.u/workspace-isolation-user-name iso-ws))
- ;; prefer the stored details verbatim (some drivers stash more than :user
- ;; there); synthesize a minimal map only for a crashed :provisioning row
- ;; that never recorded anything.
- details (if (seq (:database_details wsd))
- (:database_details wsd)
- {:user user})
- workspace (assoc iso-ws :schema schema :database_details details)
- result (try
- (destroy! provisioner driver db workspace)
- {:status :success}
- (catch Throwable t
- (log/warnf t (str "Workspace database %d: warehouse cleanup failed; leaving schema \"%s\" "
- "and user \"%s\" on database %d for manual removal")
- (:id wsd) schema user (:database_id wsd))
- {:status :failure
- :workspace_database_id (:id wsd)
- :database_id (:database_id wsd)
- :driver driver
- :schema schema
- :user user
- :reason (ex-message t)}))]
- ;; App-DB cleanup needs no warehouse connection, so it ALWAYS runs — stale
- ;; remappings would otherwise rewrite queries to a dropped schema and 500 the
- ;; QP. Fresh autocommit connection to survive any surrounding tx rollback
- ;; (mirrors `deprovision-workspace-database!`).
- (binding [t2.connection/*current-connectable* nil]
- (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) schema))
- (t2/update! :model/WorkspaceDatabase {:id (:id wsd)}
- {:output_namespace "" :database_details {} :status :unprovisioned})
- result)))
-
-(defn ignore-pending-database!
- "Delete-path helper for a WorkspaceDatabase the admin chose to delete while it is
- still `:provisioning`/`:deprovisioning`. Does NOT touch the warehouse — the row may
- be in flight on another node — it only clears the row's app-DB `TableRemapping`
- rows and forces it to `:unprovisioned` so it can be cascade-deleted. Any warehouse
- schema/user is intentionally left in place."
- [wsd]
- (with-workspace-database-lock (:id wsd)
- (let [db (t2/select-one :model/Database :id (:database_id wsd))]
- (binding [t2.connection/*current-connectable* nil]
- (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) (:output_namespace wsd)))
- (t2/update! :model/WorkspaceDatabase {:id (:id wsd)} {:status :unprovisioned}))))
+ (when-let [wsd (t2/select-one :model/WorkspaceDatabase :id (:id wsd))]
+ (let [db (t2/select-one :model/Database :id (:database_id wsd))
+ driver (driver.u/database->driver db)
+ iso-ws (wsd-iso-workspace (:id wsd))
+ computed (delay (details provisioner driver db iso-ws))
+ schema (or (not-empty (:output_namespace wsd))
+ (:schema @computed))
+ db-details (if (seq (:database_details wsd))
+ (:database_details wsd)
+ (:database_details @computed))
+ workspace (assoc iso-ws :schema schema :database_details db-details)]
+ (try
+ (destroy! provisioner driver db workspace)
+ (catch Throwable t
+ ;; The rethrow rolls back the cluster lock's wrapping transaction on
+ ;; postgres/mysql app DBs, so the keep-row bookkeeping needs its own
+ ;; autocommit connection to survive it.
+ (binding [t2.connection/*current-connectable* nil]
+ (t2/update! :model/WorkspaceDatabase {:id (:id wsd)}
+ {:output_namespace "" :database_details {} :status :unprovisioned}))
+ (throw t))
+ (finally
+ ;; App-DB cleanup needs no warehouse connection, so it ALWAYS runs — stale
+ ;; remappings would otherwise rewrite queries to a dropped schema and 500 the
+ ;; QP. Fresh autocommit connection to survive any surrounding tx rollback
+ ;; (mirrors `deprovision-workspace-database!`).
+ (binding [t2.connection/*current-connectable* nil]
+ (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) schema))))
+ (t2/delete! :model/WorkspaceDatabase :id (:id wsd))
+ nil))))
(defn deprovision-workspace!
"Flip every `:provisioned` WorkspaceDatabase under `workspace-id` to
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
index ea8ae331315..fa00f51cc26 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
@@ -21,8 +21,9 @@
(defn- stub-provisioner []
(reify provisioning/Provisioner
- (init! [_ _ _ _]
+ (details [_ _ _ _]
{:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})
+ (init! [_ _ _ _] nil)
(grant! [_ _ _ _ _] nil)
(destroy! [_ _ _ _] nil)))
@@ -47,12 +48,11 @@
(is (=? [{:id ws-id}]
(mt/user-http-request :crowberto :get 200 "ee/workspace-manager/"))))
(testing "delete"
- (is (=? {:id ws-id :deleted true}
- (mt/user-http-request :crowberto :delete 200 (str "ee/workspace-manager/" ws-id))))
+ (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id))
(mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id)))))))))
(deftest delete-workspace-pending-databases-test
- (testing "DELETE refuses a workspace with a pending database unless ignore_pending=true"
+ (testing "DELETE tears down a pending database like any other state"
(mt/with-temp [:model/Database {db-id :id} {:engine :postgres
:details {}
:settings {:database-enable-workspaces true}}]
@@ -62,15 +62,31 @@
{:name "Pending" :database_ids [db-id]})
wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id ws-id)]
(t2/update! :model/WorkspaceDatabase {:id wsd-id} {:status :provisioning})
- (testing "refused by default with a 409 listing the pending databases"
- (is (=? {:pending_databases [{:database_id db-id :status "provisioning"}]}
- (mt/user-http-request :crowberto :delete 409 (str "ee/workspace-manager/" ws-id))))
- (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id)))
- (testing "ignore-pending=true deletes anyway"
- (is (=? {:id ws-id :deleted true}
- (mt/user-http-request :crowberto :delete 200
- (str "ee/workspace-manager/" ws-id "?ignore-pending=true"))))
- (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id)))))))))
+ (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id))
+ (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id))))))))
+
+(deftest delete-workspace-teardown-failure-test
+ (testing "DELETE returns a 500 with the database's error and keeps the workspace when a teardown fails"
+ (mt/with-temp [:model/Database {db-id :id} {:engine :postgres
+ :details {}
+ :settings {:database-enable-workspaces true}}]
+ (mt/with-model-cleanup [:model/Workspace]
+ (let [{ws-id :id} (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"
+ {:name "Unreachable" :database_ids [db-id]}))
+ boom (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))]
+ (with-redefs [provisioning/dispatching-provisioner boom]
+ (is (=? {:message "Connection refused"}
+ (mt/user-http-request :crowberto :delete 500 (str "ee/workspace-manager/" ws-id)))))
+ (testing "the workspace survives and a retry deletes it"
+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id))
+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id)))
+ (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id))))))))
(deftest create-workspace-with-database-ids-test
(testing "POST / attaches and provisions the given databases (each must be eligible) with their schemas"
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
index 78883f1f155..a67e18955d1 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
@@ -1,6 +1,7 @@
(ns metabase-enterprise.workspaces.core-test
"Tests for the workspace programmatic API and lifecycle rules."
(:require
+ [clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[metabase-enterprise.workspaces.core :as ws]
[metabase-enterprise.workspaces.provisioning :as provisioning]
@@ -11,6 +12,8 @@
(:import
(clojure.lang ExceptionInfo)))
+(set! *warn-on-reflection* true)
+
(use-fixtures :once (fixtures/initialize :db))
(defn- with-premium-feature [f]
@@ -22,8 +25,9 @@
;;; Stub provisioner: records calls but doesn't hit real drivers.
(defn- stub-provisioner []
(reify provisioning/Provisioner
- (init! [_ _ _ _]
+ (details [_ _ _ _]
{:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})
+ (init! [_ _ _ _] nil)
(grant! [_ _ _ _ _] nil)
(destroy! [_ _ _ _] nil)))
@@ -71,8 +75,9 @@
(ws/create-workspace! {:name "Nope"
:creator_id (mt/user->id :crowberto)
:database_ids [Integer/MAX_VALUE]}))))
- (testing "provisioning failure rolls back the workspace and its database rows"
+ (testing "provisioning failure cleans up: databases torn down, workspace and rows deleted"
(let [failing-provisioner (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
(init! [_ _ _ _] (throw (ex-info "boom" {})))
(grant! [_ _ _ _ _] nil)
(destroy! [_ _ _ _] nil))]
@@ -85,6 +90,29 @@
"the failed create must not leave a Workspace row behind")
(is (not (t2/exists? :model/WorkspaceDatabase :database_id db-id))
"the failed create must not leave WorkspaceDatabase rows behind")))
+ (testing "provisioning failure whose cleanup also fails keeps the workspace and returns it for retry"
+ (let [wedged (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
+ (init! [_ _ _ _] (throw (ex-info "boom" {})))
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (throw (ex-info "warehouse down" {}))))]
+ (with-redefs [provisioning/dispatching-provisioner wedged]
+ (is (=? {:name "Wedged"
+ :databases [{:status :unprovisioned}]}
+ (ws/create-workspace! {:name "Wedged"
+ :creator_id (mt/user->id :crowberto)
+ :database_ids [db-id]}))
+ "the created workspace is returned even though provisioning failed and cleanup could not finish"))
+ (let [ws (t2/select-one :model/Workspace :name "Wedged")]
+ (is (some? ws)
+ "the workspace must be kept while any database row remains")
+ (is (=? [{:status :unprovisioned}]
+ (t2/select :model/WorkspaceDatabase :workspace_id (:id ws)))
+ "the row whose teardown failed is kept, forced :unprovisioned")
+ ;; retry the teardown via delete once the warehouse is back
+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (is (nil? (ws/delete-workspace! (:id ws)))))
+ (is (nil? (ws/get-workspace (:id ws)))))))
(testing "success: the attached database comes back :provisioned"
(with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
(let [ws (ws/create-workspace! {:name "Provisioned"
@@ -122,11 +150,11 @@
(testing "delete workspace with no databases"
(mt/with-model-cleanup [:model/Workspace]
(let [ws (ws/create-workspace! {:name "Empty WS" :creator_id (mt/user->id :crowberto)})]
- (is (= {:deleted true} (ws/delete-workspace! (:id ws))))
+ (is (nil? (ws/delete-workspace! (:id ws))))
(is (nil? (ws/get-workspace (:id ws))))))))
(deftest delete-workspace-with-pending-status-test
- (testing "GHY-3954: a workspace with a database still :provisioning/:deprovisioning"
+ (testing "a workspace database still :provisioning/:deprovisioning is torn down like any other state"
(doseq [pending-status [:provisioning :deprovisioning]]
(testing (str "status " pending-status)
(mt/with-model-cleanup [:model/Workspace]
@@ -141,53 +169,78 @@
(cond-> {:status pending-status}
(= pending-status :provisioning)
(assoc :output_namespace "" :database_details {})))
- (testing "is refused by default, listing the pending databases"
- (is (thrown-with-msg?
- ExceptionInfo #"still provisioning or deprovisioning"
- (ws/delete-workspace! (:id ws))))
- (is (=? {:status-code 409
- :pending_databases [{:database_id (mt/id) :status pending-status}]}
- (try (ws/delete-workspace! (:id ws))
- (catch ExceptionInfo e (ex-data e)))))
- (is (some? (ws/get-workspace (:id ws)))
- "the workspace is left intact when the delete is refused"))
- (testing "is deleted when ignore-pending? is true; the warehouse is left untouched"
- ;; destroy! must NOT be called for a pending row — fail loudly if it is.
- (let [exploding (reify provisioning/Provisioner
- (init! [_ _ _ _] nil)
- (grant! [_ _ _ _ _] nil)
- (destroy! [_ _ _ _] (throw (ex-info "destroy! must not run for a pending row" {}))))]
- (with-redefs [provisioning/dispatching-provisioner exploding]
- (is (= {:deleted true} (ws/delete-workspace! (:id ws) true)))))
+ (let [destroyed? (atom false)
+ recording (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (reset! destroyed? true) nil))]
+ (with-redefs [provisioning/dispatching-provisioner recording]
+ (is (nil? (ws/delete-workspace! (:id ws)))))
+ (is (true? @destroyed?)
+ "pending rows get a real warehouse teardown, not an app-DB-only removal")
(is (nil? (ws/get-workspace (:id ws))))
(is (not (t2/exists? :model/WorkspaceDatabase :id wsd-id))))))))))
-(deftest delete-workspace-warehouse-unreachable-test
- (testing "GHY-3954: delete still succeeds when warehouse teardown fails; the result"
- (testing "names the orphaned schema/user and why they were left behind"
+(deftest delete-workspace-teardown-failure-test
+ (testing "when teardown fails, nothing is deleted: the row and the workspace are kept for retry"
+ (mt/with-model-cleanup [:model/Workspace]
+ (let [ws (ws/create-workspace! {:name "Unreachable WS" :creator_id (mt/user->id :crowberto)})
+ _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (add-database! (:id ws) (mt/id) ["PUBLIC"]))
+ wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))
+ boom (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))]
+ (with-redefs [provisioning/dispatching-provisioner boom]
+ (is (thrown-with-msg? ExceptionInfo #"Connection refused"
+ (ws/delete-workspace! (:id ws)))
+ "the combined teardown failure is thrown, carrying what the database returned"))
+ (is (some? (ws/get-workspace (:id ws)))
+ "the workspace must be kept when any teardown fails")
+ (is (=? {:status :unprovisioned}
+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))
+ "the failed row is kept, forced :unprovisioned")
+ (testing "retrying the delete once the warehouse is reachable finishes the job"
+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (is (nil? (ws/delete-workspace! (:id ws)))))
+ (is (nil? (ws/get-workspace (:id ws))))
+ (is (not (t2/exists? :model/WorkspaceDatabase :id wsd-id))))))))
+
+(deftest delete-workspace-combines-teardown-failures-test
+ (testing "every database gets its teardown attempt and all failures come back as one combined exception"
+ (mt/with-temp [:model/Database {db2-id :id} {:engine :postgres
+ :details {}
+ :settings {:database-enable-workspaces true}}]
(mt/with-model-cleanup [:model/Workspace]
- (let [ws (ws/create-workspace! {:name "Unreachable WS" :creator_id (mt/user->id :crowberto)})
- _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
- (add-database! (:id ws) (mt/id) ["PUBLIC"]))
- wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))
- boom (reify provisioning/Provisioner
- (init! [_ _ _ _] nil)
- (grant! [_ _ _ _ _] nil)
- (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))
- result (with-redefs [provisioning/dispatching-provisioner boom]
- (ws/delete-workspace! (:id ws)))]
- (is (true? (:deleted result)))
- (is (nil? (ws/get-workspace (:id ws)))
- "the workspace is deleted even though warehouse cleanup failed")
- (is (=? [{:status :failure
- :workspace_database_id wsd-id
- :database_id (mt/id)
- :schema "mb_iso_stub"
- :user "stub_user"
- :reason "Connection refused"}]
- (:orphaned_resources result)))
- (is (re-find #"was deleted, but warehouse cleanup failed" (:message result)))
- (is (re-find #"Connection refused" (:message result))))))))
+ (let [ws (ws/create-workspace! {:name "Multi fail" :creator_id (mt/user->id :crowberto)})
+ _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (add-database! (:id ws) (mt/id) ["PUBLIC"])
+ (add-database! (:id ws) db2-id ["public"]))
+ boom (reify provisioning/Provisioner
+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ db _] (throw (ex-info (str "down: " (:id db)) {}))))
+ e (with-redefs [provisioning/dispatching-provisioner boom]
+ (try
+ (ws/delete-workspace! (:id ws))
+ nil
+ (catch Throwable t t)))]
+ (is (some? e))
+ (is (= #{(str "down: " (mt/id)) (str "down: " db2-id)}
+ (set (str/split (ex-message e) #"; ")))
+ "the message joins what each database returned")
+ (is (some? (ex-cause e))
+ "the first failure is the cause")
+ (is (= 1 (count (.getSuppressed ^Throwable e)))
+ "the remaining failures are suppressed")
+ (is (= 2 (t2/count :model/WorkspaceDatabase :workspace_id (:id ws)))
+ "both rows are kept for retry")
+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (is (nil? (ws/delete-workspace! (:id ws))))))))))
;;; -------------------------------------------- Remappings ----------------------------------------------------
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj
index 7a6579be079..feb6407b690 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj
@@ -316,6 +316,19 @@
[]
(subs (str (random-uuid)) 0 8))
+(defn- with-isolation-details
+ "Merge [[driver/workspace-isolation-details]] into `workspace`. Details are pure
+ computation, so tests build the full workspace map before the `try` — the
+ `finally` destroy then always has real identifiers to clean up with, even when
+ init never ran or only partially succeeded."
+ [driver database workspace]
+ (merge workspace (driver/workspace-isolation-details driver database workspace)))
+
+(defn- workspace-user-spec
+ "JDBC spec that connects as the workspace's isolated user."
+ [driver details workspace]
+ (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details workspace))))
+
(deftest ^:synchronized workspace-isolation-perms-test
;; BigQuery isn't a JDBC driver (its workspace isolation goes through GCP IAM
;; rather than SQL ACLs), so it's covered by `workspace-isolation-perms-bigquery-test`
@@ -338,27 +351,16 @@
src (qualify driver in-schema src-name)
workspace {:id (Long/parseLong run-id 16)
:name (str "wsd-permstest-" run-id)}
- ;; Pre-init synthetic ws-details for cleanup. Every driver's destroy impl
- ;; derives its actual identifiers from `workspace :id` (via the `driver.u`
- ;; namespace-/user-name helpers), so this skeleton is enough to drive an
- ;; idempotent destroy even if init never ran or only partially succeeded.
- ;; We swap in the real init-result once we have it, but the atom always
- ;; holds *something* destroy can be called with.
- ws-state (atom (merge workspace
- {:schema (driver.u/workspace-isolation-namespace-name workspace)
- :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]
+ workspace (with-isolation-details driver database workspace)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema)
(jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])
(jdbc/execute! admin-spec [(str "INSERT INTO " src " VALUES (1, 'a')")])
- (let [init-result (driver/init-workspace-isolation! driver database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- user-details (merge details (:database_details ws-with-details))
- user-spec (sql-jdbc.conn/connection-details->spec driver user-details)
- out-schema (:schema ws-with-details)
+ (driver/init-workspace-isolation! driver database workspace)
+ (let [user-spec (workspace-user-spec driver details workspace)
+ out-schema (:schema workspace)
out (qualify driver out-schema out-name)]
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(testing "workspace user can SELECT from a granted input table"
(is (= [{:id 1 :v "a"}]
@@ -418,7 +420,7 @@
;; must not change the workspace user's perms — they still SELECT and
;; still cannot INSERT. Catches both noisy re-grant failures and silent
;; privilege escalation in re-grant code paths.
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(is (= [{:id 1 :v "a"}]
(jdbc/query user-spec [(str "SELECT id, v FROM " src " ORDER BY id")])))
@@ -450,14 +452,14 @@
;; assert that the cleanup actually happened. Drivers' destroy impls are
;; idempotent (`IF EXISTS` everywhere), so the `finally` calling destroy
;; a second time is a no-op.
- (driver/destroy-workspace-isolation! driver database ws-with-details)
+ (driver/destroy-workspace-isolation! driver database workspace)
(verify-jdbc-destroy! driver admin-spec user-spec out-schema)))
(finally
;; Always attempt destroy first — drivers' impls are idempotent (`IF EXISTS`
;; everywhere) so this is safe whether init succeeded fully, partially, or
;; not at all. Catch+log so a destroy failure doesn't shadow the real
;; test failure.
- (try (driver/destroy-workspace-isolation! driver database @ws-state)
+ (try (driver/destroy-workspace-isolation! driver database workspace)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during test cleanup"
driver)))
@@ -512,14 +514,8 @@
:name (str "wsd-A-" ws-a-id)}
ws-b {:id (Long/parseLong ws-b-id 16)
:name (str "wsd-B-" ws-b-id)}
- ;; Pre-init synthetic ws-details for cleanup — same idempotent-destroy
- ;; rationale as in the single-workspace test.
- ws-a-state (atom (merge ws-a
- {:schema (driver.u/workspace-isolation-namespace-name ws-a)
- :database_details {:user (driver.u/workspace-isolation-user-name ws-a)}}))
- ws-b-state (atom (merge ws-b
- {:schema (driver.u/workspace-isolation-namespace-name ws-b)
- :database_details {:user (driver.u/workspace-isolation-user-name ws-b)}}))]
+ ws-a (with-isolation-details driver database ws-a)
+ ws-b (with-isolation-details driver database ws-b)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema-a)
(maybe-create-input-namespace! admin-spec driver database in-schema-b)
@@ -531,23 +527,19 @@
" (id INT, v VARCHAR(8))" (create-table-tail driver))])
(jdbc/execute! admin-spec [(str "INSERT INTO " (qualify driver in-schema-b src-b-name)
" VALUES (1, 'b')")])
- (let [init-a (driver/init-workspace-isolation! driver database ws-a)
- init-b (driver/init-workspace-isolation! driver database ws-b)
- ws-a-full (merge ws-a init-a)
- ws-b-full (merge ws-b init-b)
- _ (reset! ws-a-state ws-a-full)
- _ (reset! ws-b-state ws-b-full)
- user-a-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-a-full)))
- user-b-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-b-full)))
- out-a-schema (:schema ws-a-full)
- out-b-schema (:schema ws-b-full)
+ (driver/init-workspace-isolation! driver database ws-a)
+ (driver/init-workspace-isolation! driver database ws-b)
+ (let [user-a-spec (workspace-user-spec driver details ws-a)
+ user-b-spec (workspace-user-spec driver details ws-b)
+ out-a-schema (:schema ws-a)
+ out-b-schema (:schema ws-b)
b-secret-fq (qualify driver out-b-schema b-secret)
sneaky-fq (qualify driver out-b-schema sneaky-name)]
;; Each workspace is granted access only to its own input namespace —
;; A's grant must not let A read src-b in B's namespace, and vice-versa.
- (driver/grant-workspace-read-access! driver database ws-a-full
+ (driver/grant-workspace-read-access! driver database ws-a
[in-schema-a])
- (driver/grant-workspace-read-access! driver database ws-b-full
+ (driver/grant-workspace-read-access! driver database ws-b
[in-schema-b])
;; B populates its own output schema with a table A should never reach.
(jdbc/execute! user-b-spec [(str "CREATE TABLE " b-secret-fq
@@ -603,11 +595,11 @@
(format "A unexpectedly enumerates B's table %s.%s in catalog. visible=%s"
out-b-schema b-secret visible)))))
(finally
- (try (driver/destroy-workspace-isolation! driver database @ws-a-state)
+ (try (driver/destroy-workspace-isolation! driver database ws-a)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for ws-A on %s during cross-workspace test cleanup"
driver)))
- (try (driver/destroy-workspace-isolation! driver database @ws-b-state)
+ (try (driver/destroy-workspace-isolation! driver database ws-b)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for ws-B on %s during cross-workspace test cleanup"
driver)))
@@ -643,10 +635,8 @@
src (qualify driver in-schema src-name)
workspace {:id (Long/parseLong run-id 16)
:name (str "wsd-collision-" run-id)}
- out-schema (driver.u/workspace-isolation-namespace-name workspace)
- ws-state (atom (merge workspace
- {:schema out-schema
- :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]
+ workspace (with-isolation-details driver database workspace)
+ out-schema (:schema workspace)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema)
(jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])
@@ -654,15 +644,15 @@
;; Pre-create the output namespace at exactly the name init will target,
;; before init runs.
(maybe-create-input-namespace! admin-spec driver database out-schema)
- (let [init-result (driver/init-workspace-isolation! driver database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))
+ (driver/init-workspace-isolation! driver database workspace)
+ (let [user-spec (workspace-user-spec driver details workspace)
out (qualify driver out-schema out-name)]
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(testing "init succeeded against the pre-existing namespace"
- (is (some? init-result)))
+ ;; init returns nil under the new contract; getting here without a
+ ;; throw is the success signal. Sanity-check the details it ran with.
+ (is (some? (:schema workspace))))
(testing "workspace user has full read+write access to its output namespace post-collision"
(jdbc/execute! user-spec [(str "CREATE TABLE " out " (id INT, v VARCHAR(8))" (create-table-tail driver))])
(jdbc/execute! user-spec [(str "INSERT INTO " out " VALUES (1, 'a')")])
@@ -676,7 +666,7 @@
(str "INSERT INTO " src " VALUES (2, 'b')")
:insert-input-after-collision)))
(finally
- (try (driver/destroy-workspace-isolation! driver database @ws-state)
+ (try (driver/destroy-workspace-isolation! driver database workspace)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during collision test cleanup"
driver)))
@@ -711,21 +701,17 @@
src-b (qualify driver in-schema src-b-name)
workspace {:id (Long/parseLong run-id 16)
:name (str "wsd-grantaccum-" run-id)}
- ws-state (atom (merge workspace
- {:schema (driver.u/workspace-isolation-namespace-name workspace)
- :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]
+ workspace (with-isolation-details driver database workspace)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema)
(jdbc/execute! admin-spec [(str "CREATE TABLE " src-a " (id INT, v VARCHAR(8))" (create-table-tail driver))])
(jdbc/execute! admin-spec [(str "INSERT INTO " src-a " VALUES (1, 'a')")])
(jdbc/execute! admin-spec [(str "CREATE TABLE " src-b " (id INT, v VARCHAR(8))" (create-table-tail driver))])
(jdbc/execute! admin-spec [(str "INSERT INTO " src-b " VALUES (1, 'b')")])
- (let [init-result (driver/init-workspace-isolation! driver database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))]
+ (driver/init-workspace-isolation! driver database workspace)
+ (let [user-spec (workspace-user-spec driver details workspace)]
;; First grant: only A.
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(testing "after first grant, A is readable and B is not"
(is (= [{:id 1 :v "a"}]
@@ -741,7 +727,7 @@
:select-b-before-grant)))
;; Second grant: only B. The additive contract means A's grant must
;; still be in effect afterward.
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(testing "after second grant, both A and B are readable (A's grant accumulated)"
(is (= [{:id 1 :v "a"}]
@@ -749,7 +735,7 @@
(is (= [{:id 1 :v "b"}]
(jdbc/query user-spec [(str "SELECT id, v FROM " src-b)])))))
(finally
- (try (driver/destroy-workspace-isolation! driver database @ws-state)
+ (try (driver/destroy-workspace-isolation! driver database workspace)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during grant-accumulation test cleanup"
driver)))
@@ -823,9 +809,7 @@
secret-tbl (str "secret_" run-id)
workspace {:id (Long/parseLong run-id 16)
:name (str "wsd-crossdb-" run-id)}
- ws-state (atom (merge workspace
- {:schema (driver.u/workspace-isolation-namespace-name workspace)
- :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))
+ workspace (with-isolation-details driver database workspace)
second-db-created? (atom false)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema)
@@ -836,18 +820,16 @@
(reset! second-db-created? true)
(doseq [sql (create-table-in-second-db-sqls driver other-db secret-tbl)]
(jdbc/execute! admin-spec [sql]))
- (let [init-result (driver/init-workspace-isolation! driver database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))]
- (driver/grant-workspace-read-access! driver database ws-with-details
+ (driver/init-workspace-isolation! driver database workspace)
+ (let [user-spec (workspace-user-spec driver details workspace)]
+ (driver/grant-workspace-read-access! driver database workspace
[in-schema])
(testing "workspace user denied SELECT against a fully-qualified table in another database"
(expect-sql-denied! user-spec
(str "SELECT id, secret FROM " (second-db-qualified driver other-db secret-tbl))
:select-other-database)))
(finally
- (try (driver/destroy-workspace-isolation! driver database @ws-state)
+ (try (driver/destroy-workspace-isolation! driver database workspace)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during cross-database test cleanup"
driver)))
@@ -924,37 +906,33 @@
src (qualify driver in-schema src-name)
workspace {:id (Long/parseLong run-id 16)
:name (str "wsd-public-" run-id)}
- ws-fragment (driver.u/workspace-isolation-namespace-name workspace)
- ws-state (atom (merge workspace
- {:schema ws-fragment
- :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]
+ workspace (with-isolation-details driver database workspace)
+ ws-fragment (:schema workspace)]
(try
(maybe-create-input-namespace! admin-spec driver database in-schema)
(jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])
- (let [init-result (driver/init-workspace-isolation! driver database workspace)
- ws-with-details (merge workspace init-result)]
- (reset! ws-state ws-with-details)
- (driver/grant-workspace-read-access! driver database ws-with-details
- [in-schema])
- (testing "no leakage to default principal after init+grant"
- ;; Search both the workspace-namespace name and the workspace-user
- ;; name fragment, since per-driver grants can land on either.
- (let [user-fragment (driver.u/workspace-isolation-user-name workspace)
- leaks (atom #{})]
- (doseq [fragment [ws-fragment user-fragment]]
- (when-let [pre-sql (public-grant-pre-query-sql driver)]
- (try (jdbc/query admin-spec [pre-sql]) (catch Throwable _ nil)))
- (let [rows (try (jdbc/query admin-spec [(public-grant-probe-sql driver fragment)])
- (catch Throwable t
- (log/warnf t "public-grant probe failed on %s; skipping" driver)
- nil))]
- (when (seq rows)
- (swap! leaks into (map :resource rows)))))
- (is (empty? @leaks)
- (format "PUBLIC / anonymous principal unexpectedly has grants on workspace resources: %s"
- @leaks)))))
+ (driver/init-workspace-isolation! driver database workspace)
+ (driver/grant-workspace-read-access! driver database workspace
+ [in-schema])
+ (testing "no leakage to default principal after init+grant"
+ ;; Search both the workspace-namespace name and the workspace-user
+ ;; name fragment, since per-driver grants can land on either.
+ (let [user-fragment (get-in workspace [:database_details :user])
+ leaks (atom #{})]
+ (doseq [fragment [ws-fragment user-fragment]]
+ (when-let [pre-sql (public-grant-pre-query-sql driver)]
+ (try (jdbc/query admin-spec [pre-sql]) (catch Throwable _ nil)))
+ (let [rows (try (jdbc/query admin-spec [(public-grant-probe-sql driver fragment)])
+ (catch Throwable t
+ (log/warnf t "public-grant probe failed on %s; skipping" driver)
+ nil))]
+ (when (seq rows)
+ (swap! leaks into (map :resource rows)))))
+ (is (empty? @leaks)
+ (format "PUBLIC / anonymous principal unexpectedly has grants on workspace resources: %s"
+ @leaks))))
(finally
- (try (driver/destroy-workspace-isolation! driver database @ws-state)
+ (try (driver/destroy-workspace-isolation! driver database workspace)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during public-grant test cleanup"
driver)))
@@ -1024,69 +1002,72 @@
b-ns (driver.u/workspace-isolation-namespace-name ws-b)
generated (atom [])
gen-orig driver.u/random-workspace-password
- ws-a-state (atom (merge ws-a
- {:schema (driver.u/workspace-isolation-namespace-name ws-a)
- :database_details {:user (driver.u/workspace-isolation-user-name ws-a)}}))]
+ ;; Details are pure computation, so build the full ws map before the
+ ;; `try` — the `finally` destroy always has real identifiers to work
+ ;; with even if init never ran or only partially succeeded.
+ ws-a (with-isolation-details driver database ws-a)]
(try
- (let [init-a (driver/init-workspace-isolation! driver database ws-a)
- ws-a-full (merge ws-a init-a)]
- (reset! ws-a-state ws-a-full)
- ;; MySQL: the workspace user's JDBC handshake needs access to the
- ;; bound DB (see [[reuse-bound-db-as-input?]]), which comes from a
- ;; read grant.
- (when (reuse-bound-db-as-input? driver)
- (driver/grant-workspace-read-access! driver database ws-a-full
- [(-> database :details :db)]))
- ;; The ticket's failure shape is a caller that CAN create schemas but
- ;; CANNOT create users: postgres/redshift check CREATE at the database
- ;; level, so grant it to user A — execution then reaches the
- ;; password-bearing CREATE USER, which fails with permission denied.
- ;; Elsewhere, pre-create ws-b's namespace so the leading
- ;; `CREATE ... IF NOT EXISTS` can skip instead of failing. That only
- ;; helps engines that check existence before privileges (SQL Server's
- ;; guarded EXEC); MySQL and ClickHouse check privileges regardless,
- ;; so on those the batch dies on the namespace statement and never
- ;; reaches CREATE USER — the leak assertion still covers whatever
- ;; exception surfaces, it just can't prove the echo path there.
- (when (#{:postgres :redshift} driver)
- (jdbc/execute! admin-spec [(format "GRANT CREATE ON DATABASE \"%s\" TO \"%s\""
- (or (:dbname details) (:db details))
- (driver.u/workspace-isolation-user-name ws-a))]))
- (jdbc/execute! admin-spec [(create-input-namespace-sql driver b-ns)])
- (mt/with-temp [:model/Database low-priv-db {:engine driver
- :details (merge details (:database_details ws-a-full))}]
- (let [thrown (with-redefs [driver.u/random-workspace-password
- (fn []
- (let [pw (gen-orig)]
- (swap! generated conj pw)
- pw))]
- (try
- (driver/init-workspace-isolation! driver low-priv-db ws-b)
- nil
- (catch Throwable t t)))]
- (sql-jdbc.conn/invalidate-pool-for-db! low-priv-db)
- (is (some? thrown)
- "init-workspace-isolation! over a low-privilege connection should have thrown")
- (is (seq @generated)
- "the instrumented password generator should have been called")
- (when thrown
- (let [text (throwable-log-text thrown)]
- ;; ws-a's password is also live here — it authenticates the
- ;; low-priv pool, and connection failures can stash details
- ;; maps in ex-data.
- (doseq [pw (cons (get-in ws-a-full [:database_details :password]) @generated)
- :when (some? pw)]
- (is (not (str/includes? text pw))
- (str "workspace password leaked into exception text:\n" text)))
- ;; pgjdbc (and redshift's fork of it) echoes the aborted
- ;; statement in the batch exception message; requiring the
- ;; redaction marker proves this test reached the CREATE USER
- ;; echo rather than failing earlier and passing vacuously.
- ;; Only these two drivers get the database-level CREATE
- ;; grant that lets execution reach CREATE USER.
- (when (#{:postgres :redshift} driver)
- (is (str/includes? text "****")
- (str "expected redacted CREATE USER echo in exception text:\n" text))))))))
+ (driver/init-workspace-isolation! driver database ws-a)
+ ;; MySQL: the workspace user's JDBC handshake needs access to the
+ ;; bound DB (see [[reuse-bound-db-as-input?]]), which comes from a
+ ;; read grant.
+ (when (reuse-bound-db-as-input? driver)
+ (driver/grant-workspace-read-access! driver database ws-a
+ [(-> database :details :db)]))
+ ;; The ticket's failure shape is a caller that CAN create schemas but
+ ;; CANNOT create users: postgres/redshift check CREATE at the database
+ ;; level, so grant it to user A — execution then reaches the
+ ;; password-bearing CREATE USER, which fails with permission denied.
+ ;; Elsewhere, pre-create ws-b's namespace so the leading
+ ;; `CREATE ... IF NOT EXISTS` can skip instead of failing. That only
+ ;; helps engines that check existence before privileges (SQL Server's
+ ;; guarded EXEC); MySQL and ClickHouse check privileges regardless,
+ ;; so on those the batch dies on the namespace statement and never
+ ;; reaches CREATE USER — the leak assertion still covers whatever
+ ;; exception surfaces, it just can't prove the echo path there.
+ (when (#{:postgres :redshift} driver)
+ (jdbc/execute! admin-spec [(format "GRANT CREATE ON DATABASE \"%s\" TO \"%s\""
+ (or (:dbname details) (:db details))
+ (driver.u/workspace-isolation-user-name ws-a))]))
+ (jdbc/execute! admin-spec [(create-input-namespace-sql driver b-ns)])
+ (mt/with-temp [:model/Database low-priv-db {:engine driver
+ :details (merge details (:database_details ws-a))}]
+ ;; The password is generated by `workspace-isolation-details`, so
+ ;; compute ws-b's details inside the instrumented redef.
+ (let [thrown (with-redefs [driver.u/random-workspace-password
+ (fn []
+ (let [pw (gen-orig)]
+ (swap! generated conj pw)
+ pw))]
+ (try
+ (driver/init-workspace-isolation!
+ driver low-priv-db
+ (with-isolation-details driver low-priv-db ws-b))
+ nil
+ (catch Throwable t t)))]
+ (sql-jdbc.conn/invalidate-pool-for-db! low-priv-db)
+ (is (some? thrown)
+ "init-workspace-isolation! over a low-privilege connection should have thrown")
+ (is (seq @generated)
+ "the instrumented password generator should have been called")
+ (when thrown
+ (let [text (throwable-log-text thrown)]
+ ;; ws-a's password is also live here — it authenticates the
+ ;; low-priv pool, and connection failures can stash details
+ ;; maps in ex-data.
+ (doseq [pw (cons (get-in ws-a [:database_details :password]) @generated)
+ :when (some? pw)]
+ (is (not (str/includes? text pw))
+ (str "workspace password leaked into exception text:\n" text)))
+ ;; pgjdbc (and redshift's fork of it) echoes the aborted
+ ;; statement in the batch exception message; requiring the
+ ;; redaction marker proves this test reached the CREATE USER
+ ;; echo rather than failing earlier and passing vacuously.
+ ;; Only these two drivers get the database-level CREATE
+ ;; grant that lets execution reach CREATE USER.
+ (when (#{:postgres :redshift} driver)
+ (is (str/includes? text "****")
+ (str "expected redacted CREATE USER echo in exception text:\n" text)))))))
(finally
;; If init for ws-b unexpectedly succeeded (isolation regression),
;; a user with a live password exists — tear it down too. Also
@@ -1108,7 +1089,43 @@
(or (:dbname details) (:db details))
(driver.u/workspace-isolation-user-name ws-a))])
(catch Throwable _ nil)))
- (try (driver/destroy-workspace-isolation! driver database @ws-a-state)
+ (try (driver/destroy-workspace-isolation! driver database ws-a)
(catch Throwable t
(log/warnf t "destroy-workspace-isolation! failed for %s during password-leak test cleanup"
driver)))))))))
+
+(deftest ^:synchronized reprovision-resets-workspace-password-test
+ ;; A workspace user can survive a failed teardown (the row is kept and later
+ ;; re-provisioned). Identifiers are deterministic but the password is random
+ ;; per provisioning attempt, so init must reset the surviving user's password
+ ;; — engines whose `CREATE USER IF NOT EXISTS` silently no-ops need an
+ ;; explicit ALTER, or the persisted password stops matching the warehouse.
+ (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc)
+ (mt/normal-drivers-with-feature :workspace))
+ (testing "re-running init for the same workspace id resets the user's password"
+ (let [driver driver/*driver*
+ database (mt/db)
+ details (:details database)
+ run-id (random-suffix)
+ workspace {:id (Long/parseLong run-id 16)
+ :name (str "wsd-repw-" run-id)}
+ ;; same deterministic identifiers, two different random passwords
+ first-ws (with-isolation-details driver database workspace)
+ second-ws (with-isolation-details driver database workspace)]
+ (try
+ (driver/init-workspace-isolation! driver database first-ws)
+ (driver/init-workspace-isolation! driver database second-ws)
+ ;; MySQL: the workspace user's JDBC handshake needs access to the
+ ;; bound DB (see [[reuse-bound-db-as-input?]]).
+ (when (reuse-bound-db-as-input? driver)
+ (driver/grant-workspace-read-access! driver database second-ws
+ [(-> database :details :db)]))
+ (testing "the credentials persisted by the second init authenticate"
+ (is (= [{:one 1}]
+ (jdbc/query (workspace-user-spec driver details second-ws)
+ ["SELECT 1 AS one"]))))
+ (finally
+ (try (driver/destroy-workspace-isolation! driver database second-ws)
+ (catch Throwable t
+ (log/warnf t "destroy-workspace-isolation! failed for %s during password-reset test cleanup"
+ driver)))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj
index d0223604eed..4eff09c50d1 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj
@@ -414,7 +414,7 @@
(is (= (:db admin-details) (:db (:details ws-db)))
(str "loader must preserve canonical :db on the workspace Database. "
"If you see the isolation DB here, a driver's "
- "init-workspace-isolation! is putting :db into :database_details, "
+ "workspace-isolation-details is putting :db into :database_details, "
"which the workspace config-loader merges over canonical :details "
"and breaks the connection's bound database for sync."))
(mt/with-db ws-db
@@ -666,24 +666,24 @@
;; `:databases` initializer rewrote the `Database.details` to the workspace
;; user's creds — but `destroy-workspace-isolation!` needs admin privileges
;; (DROP DATABASE / DROP USER on MySQL, etc), so restore admin details first.
- ;; `delete-workspace!` then deprovisions any `:provisioned` databases (calls
+ ;; `delete-workspace!` then tears down every database (any state, calls
;; `destroy-workspace-isolation!`) before deleting the row, safe whether
;; provision succeeded fully or partially.
(ws/clear-instance-workspace!)
(t2/update! :model/Database (:id ws-db) {:details admin-details})
- ;; Cleanup tries [[ws/delete-workspace!]] first. If it throws (e.g. on
- ;; Redshift, [[destroy-workspace-isolation!]] rolls WSD status back to
- ;; `:provisioned` if any cleanup statement fails, and [[delete-workspace!]]
- ;; refuses with 409), fall back to force-clearing the WSD status so
+ ;; Cleanup tries [[ws/delete-workspace!]] first. A failed warehouse
+ ;; teardown throws and keeps the workspace + rows for retry. For test
+ ;; cleanup we don't retry: fall back to force-clearing the rows so
;; `mt/with-temp Database` cleanup can complete. Real fix for the
;; Redshift destroy fragility is tracked separately.
- (let [delete-result (try (ws/delete-workspace! ws-id) ::deleted-ok
- (catch Throwable t
- (log/warn t "delete-workspace! failed; force-clearing WSD")
- ::delete-failed))]
- (when (= ::delete-failed delete-result)
- (doseq [wsd (t2/select :model/WorkspaceDatabase :workspace_id ws-id)]
- (t2/update! :model/WorkspaceDatabase :id (:id wsd) {:status :unprovisioned}))
+ (let [deleted? (try (ws/delete-workspace! ws-id)
+ true
+ (catch Throwable t
+ (log/warnf t "delete-workspace! left workspace %d behind; force-clearing WSD"
+ ws-id)
+ false))]
+ (when-not deleted?
+ (t2/delete! :model/WorkspaceDatabase :workspace_id ws-id)
(t2/delete! :model/Workspace :id ws-id)))))))
(finally
(try (drop-canonical-schema! admin-driver admin-spec admin-details main-schema src-name output-table-name)
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj
index 2eac816eac4..7bde4dd283b 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj
@@ -137,6 +137,7 @@
;; Provisioner that fails on destroy! to simulate partial warehouse teardown
;; (e.g. BQ dataset deleted, SA delete throws).
(let [failing-provisioner (reify provisioning/Provisioner
+ (details [_ _ _ _] (throw (ex-info "not used" {})))
(init! [_ _ _ _] (throw (ex-info "not used" {})))
(grant! [_ _ _ _ _] (throw (ex-info "not used" {})))
(destroy! [_ _ _ _] (throw (ex-info "warehouse teardown blew up" {}))))]
diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
index e2fcba061b3..1d3b22c4653 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
@@ -1,7 +1,5 @@
import type {
CreateWorkspaceRequest,
- DeleteWorkspaceRequest,
- DeleteWorkspaceResponse,
UpdateWorkspaceRequest,
Workspace,
WorkspaceId,
@@ -50,16 +48,12 @@ export const workspaceManagerApi = EnterpriseApi.injectEndpoints({
invalidatesTags: (_, error, { id }) =>
invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),
}),
- deleteWorkspace: builder.mutation<
- DeleteWorkspaceResponse,
- DeleteWorkspaceRequest
- >({
- query: ({ id, ignorePending }) => ({
+ deleteWorkspace: builder.mutation<void, WorkspaceId>({
+ query: (id) => ({
method: "DELETE",
url: `/api/ee/workspace-manager/${id}`,
- params: ignorePending ? { "ignore-pending": true } : undefined,
}),
- invalidatesTags: (_, error, { id }) =>
+ invalidatesTags: (_, error, id) =>
invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),
}),
}),
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
index 6086324e1e4..9ddb43b4fb1 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
@@ -1,6 +1,6 @@
import { t } from "ttag";
-import { useToast } from "metabase/common/hooks/use-toast";
+import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";
import {
Form,
FormErrorMessage,
@@ -20,37 +20,31 @@ import {
useDeleteWorkspaceMutation,
useGetWorkspaceQuery,
} from "metabase-enterprise/api";
-import type { Workspace, WorkspaceDatabase } from "metabase-types/api";
+import type { Workspace, WorkspaceId } from "metabase-types/api";
+
+import { getWorkspaceDatabaseName, isPending } from "../../../utils";
export type DeleteWorkspaceModalProps = {
- workspace: Workspace;
- opened: boolean;
+ workspaceId: WorkspaceId;
onDelete: () => void;
onClose: () => void;
};
-function isPending(workspaceDatabase: WorkspaceDatabase) {
- const { status } = workspaceDatabase;
- return status === "provisioning" || status === "deprovisioning";
-}
-
export function DeleteWorkspaceModal({
- workspace,
- opened,
+ workspaceId,
onDelete,
onClose,
}: DeleteWorkspaceModalProps) {
return (
<Modal
title={t`Delete this workspace?`}
- opened={opened}
+ opened
padding="xl"
onClose={onClose}
>
<FocusTrap.InitialFocus />
- <DeleteWorkspaceForm
- workspace={workspace}
- opened={opened}
+ <DeleteWorkspaceLoader
+ workspaceId={workspaceId}
onDelete={onDelete}
onClose={onClose}
/>
@@ -58,48 +52,56 @@ export function DeleteWorkspaceModal({
);
}
+type DeleteWorkspaceLoaderProps = {
+ workspaceId: WorkspaceId;
+ onDelete: () => void;
+ onClose: () => void;
+};
+
+function DeleteWorkspaceLoader({
+ workspaceId,
+ onDelete,
+ onClose,
+}: DeleteWorkspaceLoaderProps) {
+ // Fetch the workspace on open so the pending-databases warning reflects
+ // fresh statuses, not the possibly stale list-page snapshot.
+ const {
+ data: workspace,
+ isLoading,
+ error,
+ } = useGetWorkspaceQuery(workspaceId);
+
+ if (isLoading || error != null || workspace == null) {
+ return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;
+ }
+
+ return (
+ <DeleteWorkspaceForm
+ workspace={workspace}
+ onDelete={onDelete}
+ onClose={onClose}
+ />
+ );
+}
+
type DeleteWorkspaceFormProps = {
workspace: Workspace;
- opened: boolean;
onDelete: () => void;
onClose: () => void;
};
function DeleteWorkspaceForm({
workspace,
- opened,
onDelete,
onClose,
}: DeleteWorkspaceFormProps) {
const [deleteWorkspace] = useDeleteWorkspaceMutation();
- const [sendToast] = useToast();
- // The list page omits databases, so fetch the hydrated workspace to learn which
- // databases are still provisioning/deprovisioning. Skipped while the modal is closed.
- const { data: hydratedWorkspace, isLoading } = useGetWorkspaceQuery(
- workspace.id,
- { skip: !opened },
- );
-
- const pendingDatabases = (hydratedWorkspace?.databases ?? []).filter(
- isPending,
- );
+ const databases = workspace.databases ?? [];
+ const pendingDatabases = databases.filter(isPending);
const hasPendingDatabases = pendingDatabases.length > 0;
const handleSubmit = async () => {
- const result = await deleteWorkspace({
- id: workspace.id,
- ignorePending: hasPendingDatabases,
- }).unwrap();
- // The workspace is deleted even when warehouse teardown partly fails; warn the
- // admin so the leftover schema/user objects can be removed manually.
- if (result.orphaned_resources?.length) {
- sendToast({
- message: result.message,
- icon: "warning",
- toastColor: "feedback-negative",
- timeout: null,
- });
- }
+ await deleteWorkspace(workspace.id).unwrap();
onDelete();
};
@@ -110,13 +112,12 @@ function DeleteWorkspaceForm({
{hasPendingDatabases ? (
<Stack gap="sm">
<Text>
- {t`Some of this workspace's databases are still being set up or torn down. Deleting now will remove the workspace, but their temporary database users and schemas will be left in place and must be removed manually:`}
+ {t`Some of this workspace's databases are still being set up or torn down. Deleting will wait for that work to finish, then remove the workspace along with its temporary database users and schemas:`}
</Text>
<List>
{pendingDatabases.map((workspaceDatabase) => (
<List.Item key={workspaceDatabase.database_id}>
- {workspaceDatabase.database?.name ??
- t`Database ${workspaceDatabase.database_id}`}
+ {getWorkspaceDatabaseName(workspaceDatabase)}
</List.Item>
))}
</List>
@@ -133,7 +134,6 @@ function DeleteWorkspaceForm({
label={t`Delete workspace`}
variant="filled"
color="feedback-negative"
- disabled={isLoading}
/>
</Group>
</Stack>
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
index a2ebadf8ce2..c75c618a4ed 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
@@ -17,16 +17,11 @@ import {
import { DeleteWorkspaceModal } from "./DeleteWorkspaceModal";
-const ORPHAN_MESSAGE =
- 'Workspace 1 was deleted, but warehouse cleanup failed for 1 database(s). These resources were left in place and may need to be removed manually:\n - database 2 (postgres): schema "mb_iso_1", user "mb_iso_1" — not removed because: Connection refused';
-
function setup({
withError = false,
- withOrphans = false,
databases = [],
}: {
withError?: boolean;
- withOrphans?: boolean;
databases?: WorkspaceDatabase[];
} = {}) {
const workspace = createMockWorkspace({
@@ -38,20 +33,6 @@ function setup({
if (withError) {
setupDeleteWorkspaceEndpointError(workspace.id);
- } else if (withOrphans) {
- setupDeleteWorkspaceEndpoint(workspace.id, {
- message: ORPHAN_MESSAGE,
- orphaned_resources: [
- {
- workspace_database_id: 1,
- database_id: 2,
- driver: "postgres",
- schema: "mb_iso_1",
- user: "mb_iso_1",
- reason: "Connection refused",
- },
- ],
- });
} else {
setupDeleteWorkspaceEndpoint(workspace.id);
}
@@ -62,8 +43,7 @@ function setup({
renderWithProviders(
<>
<DeleteWorkspaceModal
- workspace={workspace}
- opened
+ workspaceId={workspace.id}
onDelete={onDelete}
onClose={onClose}
/>
@@ -98,7 +78,7 @@ describe("DeleteWorkspaceModal", () => {
await waitFor(() => expect(onDelete).toHaveBeenCalled());
});
- it("should list pending databases and delete with ignore-pending when confirmed", async () => {
+ it("should list pending databases and delete when confirmed", async () => {
const { onDelete } = setup({
databases: [
createMockWorkspaceDatabase({
@@ -120,25 +100,12 @@ describe("DeleteWorkspaceModal", () => {
expect(
fetchMock.callHistory.called("path:/api/ee/workspace-manager/1", {
method: "DELETE",
- query: { "ignore-pending": "true" },
}),
).toBe(true);
});
await waitFor(() => expect(onDelete).toHaveBeenCalled());
});
- it("should warn about orphaned warehouse resources but still delete when teardown partly fails", async () => {
- const { onDelete } = setup({ withOrphans: true });
-
- await clickDelete();
-
- expect(
- await screen.findByText(/warehouse cleanup failed/i),
- ).toBeInTheDocument();
- expect(screen.getByText(/Connection refused/i)).toBeInTheDocument();
- await waitFor(() => expect(onDelete).toHaveBeenCalled());
- });
-
it("should show an error message and not call the callback when the request fails", async () => {
const { onDelete } = setup({ withError: true });
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx
index 03d5979788f..1659e893adb 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx
@@ -1,6 +1,7 @@
import { t } from "ttag";
import * as Yup from "yup";
+import { useToast } from "metabase/common/hooks/use-toast";
import {
Form,
FormCheckboxGroup,
@@ -18,6 +19,7 @@ import {
import type { Database, Workspace } from "metabase-types/api";
import { trackWorkspaceCreated } from "../../../analytics";
+import { getProvisioningFailureMessage, isUnprovisioned } from "../../../utils";
type NewWorkspaceModalProps = {
databases: Database[];
@@ -76,6 +78,7 @@ function NewWorkspaceForm({
}: NewWorkspaceFormProps) {
const [createWorkspace] = useCreateWorkspaceMutation();
const [fetchWorkspaces] = useLazyListWorkspacesQuery();
+ const [sendToast] = useToast();
const handleSubmit = async ({
name,
@@ -85,8 +88,19 @@ function NewWorkspaceForm({
name,
database_ids: database_ids.map(Number),
}).unwrap();
- await fetchWorkspaces();
trackWorkspaceCreated({ workspaceId: workspace.id });
+
+ const workspaceDatabases = workspace.databases ?? [];
+ if (workspaceDatabases.some(isUnprovisioned)) {
+ sendToast({
+ message: getProvisioningFailureMessage(),
+ icon: "warning",
+ toastColor: "feedback-negative",
+ timeout: null,
+ });
+ }
+
+ await fetchWorkspaces();
onCreate(workspace);
};
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx
index 0e70c5b7fa3..be957354703 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx
@@ -5,10 +5,12 @@ import {
setupListWorkspacesEndpoint,
} from "__support__/server-mocks";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { UndoListing } from "metabase/common/components/UndoListing";
import type { Workspace } from "metabase-types/api";
import {
createMockDatabase,
createMockWorkspace,
+ createMockWorkspaceDatabase,
} from "metabase-types/api/mocks";
import { NewWorkspaceModal } from "./NewWorkspaceModal";
@@ -31,12 +33,15 @@ function setup({
setupListWorkspacesEndpoint([createdWorkspace]);
renderWithProviders(
- <NewWorkspaceModal
- databases={[DATABASE]}
- opened
- onCreate={onCreate}
- onClose={onClose}
- />,
+ <>
+ <NewWorkspaceModal
+ databases={[DATABASE]}
+ opened
+ onCreate={onCreate}
+ onClose={onClose}
+ />
+ <UndoListing />
+ </>,
);
return { onCreate, onClose, createdWorkspace };
@@ -61,6 +66,32 @@ describe("NewWorkspaceModal", () => {
);
});
+ it("warns when some databases could not be provisioned", async () => {
+ const { onCreate } = setup({
+ createdWorkspace: createMockWorkspace({
+ name: "Brand new workspace",
+ databases: [
+ createMockWorkspaceDatabase({
+ database_id: 10,
+ status: "unprovisioned",
+ database: createMockDatabase({ id: 10, name: "Postgres" }),
+ }),
+ ],
+ }),
+ });
+
+ await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");
+ await userEvent.click(screen.getByRole("checkbox", { name: "Postgres" }));
+ await userEvent.click(
+ screen.getByRole("button", { name: "Create workspace" }),
+ );
+
+ expect(
+ await screen.findByText("Failed to provision the workspace."),
+ ).toBeInTheDocument();
+ await waitFor(() => expect(onCreate).toHaveBeenCalled());
+ });
+
it("requires at least one database", async () => {
const { onCreate } = setup();
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx
index 38e5405a01b..4bb84881e54 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx
@@ -15,6 +15,11 @@ import { getUserName } from "metabase/utils/user";
import type { Workspace, WorkspaceDatabase } from "metabase-types/api";
import { trackWorkspaceConfigDownloaded } from "../../../analytics";
+import {
+ getProvisioningFailureMessage,
+ getWorkspaceDatabaseName,
+ isUnprovisioned,
+} from "../../../utils";
import { DeleteWorkspaceModal } from "../DeleteWorkspaceModal";
import { RenameWorkspaceModal } from "../RenameWorkspaceModal";
@@ -25,6 +30,8 @@ export type WorkspaceItemProps = {
};
export function WorkspaceItem({ workspace }: WorkspaceItemProps) {
+ const databases = workspace.databases ?? [];
+
return (
<Card
role="region"
@@ -40,7 +47,8 @@ export function WorkspaceItem({ workspace }: WorkspaceItemProps) {
{workspace.name}
</Box>
<WorkspaceCreatorInfo workspace={workspace} />
- {workspace.databases?.map((workspaceDatabase) => (
+ {databases.some(isUnprovisioned) && <WorkspaceProvisioningWarning />}
+ {databases.map((workspaceDatabase) => (
<WorkspaceDatabaseItem
key={workspaceDatabase.database_id}
workspaceDatabase={workspaceDatabase}
@@ -74,20 +82,25 @@ type WorkspaceDatabaseItemProps = {
workspaceDatabase: WorkspaceDatabase;
};
+function WorkspaceProvisioningWarning() {
+ return (
+ <Box c="text-secondary" lh="1rem">
+ <Group gap="xs" wrap="nowrap">
+ <FixedSizeIcon name="warning" aria-hidden />
+ {getProvisioningFailureMessage()}
+ </Group>
+ </Box>
+ );
+}
+
function WorkspaceDatabaseItem({
workspaceDatabase,
}: WorkspaceDatabaseItemProps) {
- const { database } = workspaceDatabase;
-
- if (database == null) {
- return null;
- }
-
return (
<Box c="text-secondary" lh="1rem">
<Group gap="xs" wrap="nowrap">
<FixedSizeIcon name="database" aria-hidden />
- {database.name}
+ {getWorkspaceDatabaseName(workspaceDatabase)}
</Group>
</Box>
);
@@ -143,12 +156,13 @@ function WorkspaceMenu({ workspace }: WorkspaceMenuProps) {
onRename={closeRename}
onClose={closeRename}
/>
- <DeleteWorkspaceModal
- workspace={workspace}
- opened={isDeleteOpen}
- onDelete={closeDelete}
- onClose={closeDelete}
- />
+ {isDeleteOpen && (
+ <DeleteWorkspaceModal
+ workspaceId={workspace.id}
+ onDelete={closeDelete}
+ onClose={closeDelete}
+ />
+ )}
</>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
index e1a04b8955f..9aed518284b 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
@@ -47,7 +47,7 @@ describe("WorkspaceItem", () => {
expect(screen.getByText(/^Created /)).toBeInTheDocument();
});
- it("renders only hydrated databases", () => {
+ it("renders databases, falling back to the id when not hydrated", () => {
setup({
workspace: createMockWorkspace({
id: 1,
@@ -66,6 +66,48 @@ describe("WorkspaceItem", () => {
});
expect(screen.getByText("Postgres")).toBeInTheDocument();
+ expect(screen.getByText("Database 20")).toBeInTheDocument();
+ });
+
+ it("warns when a database is not provisioned", () => {
+ setup({
+ workspace: createMockWorkspace({
+ id: 1,
+ name: "My workspace",
+ databases: [
+ createMockWorkspaceDatabase({
+ database_id: 10,
+ status: "unprovisioned",
+ database: createMockDatabase({ id: 10, name: "Postgres" }),
+ }),
+ ],
+ }),
+ });
+
+ expect(
+ screen.getByText("Failed to provision the workspace."),
+ ).toBeInTheDocument();
+ expect(screen.getByText("Postgres")).toBeInTheDocument();
+ });
+
+ it("does not warn when all databases are provisioned", () => {
+ setup({
+ workspace: createMockWorkspace({
+ id: 1,
+ name: "My workspace",
+ databases: [
+ createMockWorkspaceDatabase({
+ database_id: 10,
+ status: "provisioned",
+ database: createMockDatabase({ id: 10, name: "Postgres" }),
+ }),
+ ],
+ }),
+ });
+
+ expect(
+ screen.queryByText("Failed to provision the workspace."),
+ ).not.toBeInTheDocument();
});
it("offers a config download link in the menu", async () => {
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts
new file mode 100644
index 00000000000..b7a6c53d1c5
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts
@@ -0,0 +1,23 @@
+import { t } from "ttag";
+
+import type { WorkspaceDatabase } from "metabase-types/api";
+
+export function getWorkspaceDatabaseName(workspaceDatabase: WorkspaceDatabase) {
+ return (
+ workspaceDatabase.database?.name ??
+ t`Database ${workspaceDatabase.database_id}`
+ );
+}
+
+export function isUnprovisioned(workspaceDatabase: WorkspaceDatabase) {
+ return workspaceDatabase.status === "unprovisioned";
+}
+
+export function isPending(workspaceDatabase: WorkspaceDatabase) {
+ const { status } = workspaceDatabase;
+ return status === "provisioning" || status === "deprovisioning";
+}
+
+export function getProvisioningFailureMessage() {
+ return t`Failed to provision the workspace.`;
+}
diff --git a/frontend/src/metabase-types/api/workspace-manager.ts b/frontend/src/metabase-types/api/workspace-manager.ts
index ae0a890246a..6e7f9d6bd2d 100644
--- a/frontend/src/metabase-types/api/workspace-manager.ts
+++ b/frontend/src/metabase-types/api/workspace-manager.ts
@@ -36,29 +36,3 @@ export type UpdateWorkspaceRequest = {
id: WorkspaceId;
name?: string;
};
-
-export type OrphanedWorkspaceResource = {
- workspace_database_id: number;
- database_id: DatabaseId;
- driver: string;
- schema: string;
- user: string;
- reason?: string | null;
-};
-
-export type DeleteWorkspaceRequest = {
- id: WorkspaceId;
- // When the workspace has databases still provisioning/deprovisioning, the
- // backend refuses unless this is set — then it leaves those databases'
- // warehouse resources in place and removes only the app-DB rows.
- ignorePending?: boolean;
-};
-
-export type DeleteWorkspaceResponse = {
- id: WorkspaceId;
- deleted: boolean;
- // Present only when the warehouse was unreachable during teardown: the workspace
- // is still deleted, but these inert schema/user objects were left behind.
- message?: string;
- orphaned_resources?: OrphanedWorkspaceResource[];
-};
diff --git a/frontend/test/__support__/server-mocks/workspace-manager.ts b/frontend/test/__support__/server-mocks/workspace-manager.ts
index ca4629e907b..86b3c713937 100644
--- a/frontend/test/__support__/server-mocks/workspace-manager.ts
+++ b/frontend/test/__support__/server-mocks/workspace-manager.ts
@@ -1,10 +1,6 @@
import fetchMock from "fetch-mock";
-import type {
- DeleteWorkspaceResponse,
- Workspace,
- WorkspaceId,
-} from "metabase-types/api";
+import type { Workspace, WorkspaceId } from "metabase-types/api";
const BASE_URL = "path:/api/ee/workspace-manager";
@@ -24,15 +20,8 @@ export function setupUpdateWorkspaceEndpoint(workspace: Workspace) {
fetchMock.put(`${BASE_URL}/${workspace.id}`, workspace);
}
-export function setupDeleteWorkspaceEndpoint(
- workspaceId: WorkspaceId,
- response?: Partial<DeleteWorkspaceResponse>,
-) {
- fetchMock.delete(`${BASE_URL}/${workspaceId}`, {
- id: workspaceId,
- deleted: true,
- ...response,
- });
+export function setupDeleteWorkspaceEndpoint(workspaceId: WorkspaceId) {
+ fetchMock.delete(`${BASE_URL}/${workspaceId}`, { status: 204 });
}
export function setupDeleteWorkspaceEndpointError(workspaceId: WorkspaceId) {
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index c1ab9836744..1c1d3b93d47 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -12,7 +12,7 @@
[metabase.driver.bigquery-cloud-sdk.query-processor :as bigquery.qp]
;; Side-effects: registers BigQuery driver multimethods for workspace
;; isolation (`init-workspace-isolation!`, `grant-workspace-read-access!`,
- ;; `check-isolation-permissions`, `destroy-workspace-isolation!`).
+ ;; `destroy-workspace-isolation!`).
[metabase.driver.bigquery-cloud-sdk.workspaces]
[metabase.driver.common :as driver.common]
[metabase.driver.common.table-rows-sample :as table-rows-sample]
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj
index 4f8c156aa39..8078d7d9cb0 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj
@@ -2,7 +2,7 @@
"BigQuery workspace isolation: per-workspace service accounts, IAM bindings,
and dataset ACLs that gate read/write access to isolated datasets. Loaded
for side-effects by `metabase.driver.bigquery-cloud-sdk` to register the
- `driver/{init,grant,check,destroy}-workspace-isolation*` multimethod
+ `driver/{init,grant,destroy}-workspace-isolation*` multimethod
implementations."
(:refer-clojure :exclude [some not-empty])
(:require
@@ -10,10 +10,8 @@
[metabase.driver :as driver]
[metabase.driver.bigquery-cloud-sdk.common :as bigquery.common]
[metabase.driver.connection :as driver.conn]
- [metabase.driver.sql.util :as sql.u]
[metabase.driver.util :as driver.u]
[metabase.util :as u]
- [metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
[metabase.util.performance :refer [some not-empty]])
(:import
@@ -35,8 +33,6 @@
(set! *warn-on-reflection* true)
-(defn- quote-schema [s] (sql.u/quote-name :bigquery-cloud-sdk :schema s))
-
(defn create-dataset!
"Create `dataset-name` in `project-id` via `client`. Idempotent — no-op when
the dataset already exists. `:description` (in `opts`) is optional metadata.
@@ -71,8 +67,6 @@
(into-array BigQuery$DatasetDeleteOption
[(BigQuery$DatasetDeleteOption/deleteContents)])))))
-(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")
-
(defn- sa-propagation-error?
"True for the GCP race where an API call references a service account that was
just created but hasn't yet propagated to the API receiving the call. GCP
@@ -151,6 +145,13 @@
(subs 0 (min 30 (count sa-id)))
u/lower-case-en)))
+(defn- ws-service-account-email
+ "The email that identifies a workspace's service account. The single source of
+ the SA identity — create, delete, and the persisted `:database_details` must
+ all agree on it."
+ [workspace ^String project-id]
+ (format "%s@%s.iam.gserviceaccount.com" (ws-service-account-id workspace) project-id))
+
(defn- with-sa-propagation-retry!
"Run `f` (a no-arg fn), retrying when GCP rejects it because a freshly-created
workspace SA isn't yet visible to the API being called. Used for IAM bindings
@@ -479,8 +480,7 @@
(defn- ws-delete-service-account!
"Delete a service account for a workspace. Idempotent - does nothing if SA doesn't exist."
[^IAMClient iam-client ^String project-id workspace]
- (let [sa-id (ws-service-account-id workspace)
- sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)
+ (let [sa-email (ws-service-account-email workspace project-id)
sa-name (format "projects/%s/serviceAccounts/%s" project-id sa-email)]
(when (ws-service-account-exists? iam-client project-id sa-email)
(log/infof "Deleting service account %s" sa-email)
@@ -540,7 +540,7 @@
side, and `metabase.test.data.bigquery-cloud-sdk` for the cleanup loop."
[^IAMClient iam-client ^String project-id workspace]
(let [sa-id (ws-service-account-id workspace)
- sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)
+ sa-email (ws-service-account-email workspace project-id)
project-name (format "projects/%s" project-id)
description (ws-sa-description (java.time.Instant/now))]
;; Check if already exists first
@@ -589,7 +589,7 @@
client (ws-database-details->client details)
iam-client (ws-database-details->iam-client details)
project-id (bigquery.common/get-project-id details)
- dataset-name (driver.u/workspace-isolation-namespace-name workspace)]
+ dataset-name (:schema workspace)]
(try
(log/infof "Destroying BigQuery workspace isolation: dataset=%s" dataset-name)
;; Delete the dataset if it exists (deleteContents=true removes all tables)
@@ -600,6 +600,15 @@
(finally
(.close iam-client)))))
+(defmethod driver/workspace-isolation-details :bigquery-cloud-sdk
+ [_driver database workspace]
+ (let [details (driver.conn/effective-details database)
+ project-id (bigquery.common/get-project-id details)
+ sa-email (ws-service-account-email workspace project-id)]
+ {:schema (driver.u/workspace-isolation-namespace-name workspace)
+ :database_details {:user sa-email
+ :impersonate-service-account sa-email}}))
+
(defmethod driver/init-workspace-isolation! :bigquery-cloud-sdk
[_driver database workspace]
(let [details (driver.conn/effective-details database)
@@ -607,7 +616,7 @@
iam-client (ws-database-details->iam-client details)
project-id (bigquery.common/get-project-id details)
main-sa-email (.getClientEmail (ws-service-account-credentials details))
- dataset-name (driver.u/workspace-isolation-namespace-name workspace)]
+ dataset-name (:schema workspace)]
(try
;; Create the workspace service account (or get existing)
(let [ws-sa-email (ws-create-service-account! iam-client project-id workspace)
@@ -634,48 +643,6 @@
;; Grant the workspace service account dataEditor role on the isolated dataset
;; dataEditor allows: create/update/delete tables, insert/update/delete data
(ws-grant-dataset-acl! client dataset-id ws-sa-email "roles/bigquery.dataEditor")
- ;; Return workspace connection details for impersonation
- ;; :user is used by grant-read-access-to-tables! to know which SA to grant access to
- ;; :impersonate-service-account is used by the connection swap to use impersonated credentials
- {:schema dataset-name
- :database_details {:user ws-sa-email
- :impersonate-service-account ws-sa-email}})
+ nil)
(finally
(.close iam-client)))))
-
-(defmethod driver/check-isolation-permissions :bigquery-cloud-sdk
- [driver database test-table]
- ;; BigQuery uses GCP IAM APIs instead of SQL, so we can't use transaction rollback.
- ;; We run the actual init/grant/destroy operations and clean up immediately.
- (let [test-workspace {:id perm-check-workspace-id
- :name "_mb_perm_check_"}]
- (driver.conn/with-admin-connection
- (try
- (let [init-result (try
- (driver/init-workspace-isolation! driver database test-workspace)
- (catch Exception e
- (throw (ex-info (tru "Failed to initialize workspace isolation: {0}" (ex-message e))
- {:step :init} e))))
- workspace-with-details (merge test-workspace init-result)]
- (when test-table
- (try
- ;; `grant-workspace-read-access!` takes a vector of schema-name strings.
- ;; Pass the test-table's dataset.
- (driver/grant-workspace-read-access! driver database workspace-with-details
- [(:schema test-table)])
- (catch Exception e
- (throw (ex-info (tru "Failed to grant read access to dataset {0}: {1}"
- (quote-schema (:schema test-table)) (ex-message e))
- {:step :grant :table test-table} e)))))
- (try
- (driver/destroy-workspace-isolation! driver database workspace-with-details)
- (catch Exception e
- (throw (ex-info (tru "Failed to destroy workspace isolation: {0}" (ex-message e))
- {:step :destroy} e)))))
- nil
- (catch Exception e
- (ex-message e))
- (finally
- (try
- (driver/destroy-workspace-isolation! driver database test-workspace)
- (catch Exception _ nil)))))))
diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj
index 521332c2631..d58fc1abf6b 100644
--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj
+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj
@@ -65,41 +65,39 @@
(deftest ^:synchronized workspace-isolation-perms-bigquery-test
(mt/test-driver :bigquery-cloud-sdk
(testing "workspace SA gets read-only access to input dataset, full access to output dataset"
- (let [database (mt/db)
- details (:details database)
+ (let [database (mt/db)
+ details (:details database)
;; The BQ test extension only populates `:project-id` in `details` when
;; `MB_BIGQUERY_CLOUD_SDK_TEST_PROJECT_ID` is set (see test/data/
;; bigquery_cloud_sdk.clj:69-70). When it isn't, fall back to the
;; project embedded in the admin service-account JSON — every
;; Google-issued SA key carries the project it was created in.
- project-id (or (:project-id details)
- (.getProjectId (bq.util/admin-credentials details)))
- admin-creds (bq.util/admin-credentials details)
- admin-client (bq.util/admin-client details)
- iam-client (bq.util/iam-client details)
- run-id (random-suffix)
- in-dataset (str "mb_iso_in_" run-id)
- src-name (str "ws_iso_src_" run-id)
- sneaky-name (str "ws_iso_sneaky_" run-id)
- out-name (str "ws_iso_out_" run-id)
- workspace {:id (Long/parseLong run-id 16)
- :name (str "wsd-permstest-" run-id)}
- ws-state (atom (merge workspace
- {:schema (driver.u/workspace-isolation-namespace-name workspace)}))
- qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
- run-sql (fn [^BigQuery c sql]
- (.query c (QueryJobConfiguration/of sql)
- (into-array BigQuery$JobOption [])))]
+ project-id (or (:project-id details)
+ (.getProjectId (bq.util/admin-credentials details)))
+ admin-creds (bq.util/admin-credentials details)
+ admin-client (bq.util/admin-client details)
+ iam-client (bq.util/iam-client details)
+ run-id (random-suffix)
+ in-dataset (str "mb_iso_in_" run-id)
+ src-name (str "ws_iso_src_" run-id)
+ sneaky-name (str "ws_iso_sneaky_" run-id)
+ out-name (str "ws_iso_out_" run-id)
+ workspace {:id (Long/parseLong run-id 16)
+ :name (str "wsd-permstest-" run-id)}
+ ws-with-details (merge workspace
+ (driver/workspace-isolation-details :bigquery-cloud-sdk database workspace))
+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
+ run-sql (fn [^BigQuery c sql]
+ (.query c (QueryJobConfiguration/of sql)
+ (into-array BigQuery$JobOption [])))]
(try
(bq.util/create-dataset! admin-client project-id in-dataset)
(run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset src-name)))
(run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-dataset src-name)))
- (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
- user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)
- out-dataset (:schema ws-with-details)]
+ (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
+ (let [ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)
+ out-dataset (:schema ws-with-details)]
(driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details
[in-dataset])
(testing "workspace SA can SELECT from a granted input table"
@@ -191,7 +189,7 @@
(driver/destroy-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
(bq.util/verify-destroy! project-id admin-client out-dataset)))
(finally
- (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)
+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
(catch Throwable t
(log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during test cleanup")))
;; Belt-and-suspenders: directly delete the input dataset and workspace SA,
@@ -234,10 +232,10 @@
:name (str "wsd-A-" ws-a-id)}
ws-b {:id (Long/parseLong ws-b-id 16)
:name (str "wsd-B-" ws-b-id)}
- ws-a-state (atom (merge ws-a
- {:schema (driver.u/workspace-isolation-namespace-name ws-a)}))
- ws-b-state (atom (merge ws-b
- {:schema (driver.u/workspace-isolation-namespace-name ws-b)}))
+ ;; Details are pure computation, so both full workspace maps exist
+ ;; before init — the `finally` destroys always have them.
+ ws-a-full (merge ws-a (driver/workspace-isolation-details :bigquery-cloud-sdk database ws-a))
+ ws-b-full (merge ws-b (driver/workspace-isolation-details :bigquery-cloud-sdk database ws-b))
qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
run-sql (fn [^BigQuery c sql]
(.query c (QueryJobConfiguration/of sql)
@@ -249,17 +247,13 @@
(run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-dataset-a src-a-name)))
(run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset-b src-b-name)))
(run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'b')" (qual in-dataset-b src-b-name)))
- (let [init-a (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-a)
- init-b (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-b)
- ws-a-full (merge ws-a init-a)
- ws-b-full (merge ws-b init-b)
- _ (reset! ws-a-state ws-a-full)
- _ (reset! ws-b-state ws-b-full)
- a-sa-email (-> ws-a-full :database_details :impersonate-service-account)
- b-sa-email (-> ws-b-full :database_details :impersonate-service-account)
- a-client (bq.util/impersonated-client admin-creds a-sa-email project-id)
- b-client (bq.util/impersonated-client admin-creds b-sa-email project-id)
- out-b-ds (:schema ws-b-full)]
+ (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-a-full)
+ (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-b-full)
+ (let [a-sa-email (-> ws-a-full :database_details :impersonate-service-account)
+ b-sa-email (-> ws-b-full :database_details :impersonate-service-account)
+ a-client (bq.util/impersonated-client admin-creds a-sa-email project-id)
+ b-client (bq.util/impersonated-client admin-creds b-sa-email project-id)
+ out-b-ds (:schema ws-b-full)]
;; Each workspace is granted access only to its own input dataset —
;; A's grant must not let A read src-b in B's dataset, and vice-versa.
(driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-a-full
@@ -304,7 +298,7 @@
(format "A unexpectedly enumerates B's dataset %s. visible=%s"
out-b-ds (or names #{}))))))
(finally
- (doseq [w [@ws-a-state @ws-b-state]]
+ (doseq [w [ws-a-full ws-b-full]]
(try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database w)
(catch Throwable t
(log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during cross-workspace test cleanup"))))
@@ -324,31 +318,34 @@
;; revoke-then-grant from passing unnoticed.
(mt/test-driver :bigquery-cloud-sdk
(testing "grant-workspace-read-access! is additive across multiple calls"
- (let [database (mt/db)
- details (:details database)
- project-id (or (:project-id details)
- (.getProjectId (bq.util/admin-credentials details)))
- admin-creds (bq.util/admin-credentials details)
- admin-client (bq.util/admin-client details)
- iam-client (bq.util/iam-client details)
- run-id (random-suffix)
+ (let [database (mt/db)
+ details (:details database)
+ project-id (or (:project-id details)
+ (.getProjectId (bq.util/admin-credentials details)))
+ admin-creds (bq.util/admin-credentials details)
+ admin-client (bq.util/admin-client details)
+ iam-client (bq.util/iam-client details)
+ run-id (random-suffix)
;; Two SEPARATE input datasets: BQ's `dataViewer` is dataset-scoped,
;; so a per-table read-denied assertion requires the tables live in
;; different datasets.
- in-a-dataset (str "mb_iso_in_a_" run-id)
- in-b-dataset (str "mb_iso_in_b_" run-id)
- src-a-name (str "ws_iso_src_a_" run-id)
- src-b-name (str "ws_iso_src_b_" run-id)
- workspace {:id (Long/parseLong run-id 16)
- :name (str "wsd-grantaccum-" run-id)}
- ws-state (atom (merge workspace {:schema (driver.u/workspace-isolation-namespace-name workspace)}))
- qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
- run-sql (fn [^BigQuery c sql]
- (.query c (QueryJobConfiguration/of sql)
- (into-array BigQuery$JobOption [])))
- select-id (fn [^BigQuery c sql]
- (mapv (fn [^FieldValueList row] {:id (.getLongValue (.get row "id"))})
- (.iterateAll ^TableResult (run-sql c sql))))]
+ in-a-dataset (str "mb_iso_in_a_" run-id)
+ in-b-dataset (str "mb_iso_in_b_" run-id)
+ src-a-name (str "ws_iso_src_a_" run-id)
+ src-b-name (str "ws_iso_src_b_" run-id)
+ workspace {:id (Long/parseLong run-id 16)
+ :name (str "wsd-grantaccum-" run-id)}
+ ;; Details are pure computation, so the full workspace map exists
+ ;; before init — the `finally` destroy always has it.
+ ws-with-details (merge workspace
+ (driver/workspace-isolation-details :bigquery-cloud-sdk database workspace))
+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
+ run-sql (fn [^BigQuery c sql]
+ (.query c (QueryJobConfiguration/of sql)
+ (into-array BigQuery$JobOption [])))
+ select-id (fn [^BigQuery c sql]
+ (mapv (fn [^FieldValueList row] {:id (.getLongValue (.get row "id"))})
+ (.iterateAll ^TableResult (run-sql c sql))))]
(try
(bigquery.ws/create-dataset! admin-client project-id in-a-dataset)
(bigquery.ws/create-dataset! admin-client project-id in-b-dataset)
@@ -356,11 +353,9 @@
(run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-a-dataset src-a-name)))
(run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-b-dataset src-b-name)))
(run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'b')" (qual in-b-dataset src-b-name)))
- (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
- user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]
+ (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
+ (let [ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]
;; First grant: only dataset A.
(driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details
[in-a-dataset])
@@ -377,7 +372,7 @@
(is (= [{:id 1}] (select-id user-client (format "SELECT id FROM %s" (qual in-a-dataset src-a-name)))))
(is (= [{:id 1}] (select-id user-client (format "SELECT id FROM %s" (qual in-b-dataset src-b-name)))))))
(finally
- (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)
+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
(catch Throwable t
(log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during grant-accumulation test cleanup")))
(try (bigquery.ws/drop-dataset! admin-client project-id in-a-dataset) (catch Throwable _ nil))
@@ -397,25 +392,28 @@
;; in the colliding dataset as the JDBC counterpart.
(mt/test-driver :bigquery-cloud-sdk
(testing "init-workspace-isolation! is robust when its target output dataset already exists"
- (let [database (mt/db)
- details (:details database)
- project-id (or (:project-id details)
- (.getProjectId (bq.util/admin-credentials details)))
- admin-creds (bq.util/admin-credentials details)
- admin-client (bq.util/admin-client details)
- iam-client (bq.util/iam-client details)
- run-id (random-suffix)
- in-dataset (str "mb_iso_in_" run-id)
- src-name (str "ws_iso_src_" run-id)
- out-name (str "ws_iso_out_" run-id)
- workspace {:id (Long/parseLong run-id 16)
- :name (str "wsd-collision-" run-id)}
- out-dataset (driver.u/workspace-isolation-namespace-name workspace)
- ws-state (atom (merge workspace {:schema out-dataset}))
- qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
- run-sql (fn [^BigQuery c sql]
- (.query c (QueryJobConfiguration/of sql)
- (into-array BigQuery$JobOption [])))]
+ (let [database (mt/db)
+ details (:details database)
+ project-id (or (:project-id details)
+ (.getProjectId (bq.util/admin-credentials details)))
+ admin-creds (bq.util/admin-credentials details)
+ admin-client (bq.util/admin-client details)
+ iam-client (bq.util/iam-client details)
+ run-id (random-suffix)
+ in-dataset (str "mb_iso_in_" run-id)
+ src-name (str "ws_iso_src_" run-id)
+ out-name (str "ws_iso_out_" run-id)
+ workspace {:id (Long/parseLong run-id 16)
+ :name (str "wsd-collision-" run-id)}
+ out-dataset (driver.u/workspace-isolation-namespace-name workspace)
+ ;; Details are pure computation, so the full workspace map exists
+ ;; before init — the `finally` destroy always has it.
+ ws-with-details (merge workspace
+ (driver/workspace-isolation-details :bigquery-cloud-sdk database workspace))
+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))
+ run-sql (fn [^BigQuery c sql]
+ (.query c (QueryJobConfiguration/of sql)
+ (into-array BigQuery$JobOption [])))]
(try
(bigquery.ws/create-dataset! admin-client project-id in-dataset)
(run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset src-name)))
@@ -423,15 +421,16 @@
;; Pre-create the output dataset at exactly the name init will target,
;; before init runs.
(bigquery.ws/create-dataset! admin-client project-id out-dataset)
- (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)
- ws-with-details (merge workspace init-result)
- _ (reset! ws-state ws-with-details)
- ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
- user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]
+ (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
+ (let [ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)
+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]
(driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details
[in-dataset])
- (testing "init succeeded against the pre-existing dataset"
- (is (some? init-result)))
+ (testing "isolation details target exactly the pre-existing dataset name"
+ ;; init returns nil, so reaching this point without a throw is the
+ ;; collision-tolerance signal; this pins that the computed details
+ ;; land on the deterministic (colliding) dataset name.
+ (is (= out-dataset (:schema ws-with-details))))
(testing "workspace SA has full read+write access to its output dataset post-collision"
(run-sql user-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual out-dataset out-name)))
(run-sql user-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual out-dataset out-name)))
@@ -451,7 +450,7 @@
(format "INSERT INTO %s (id, v) VALUES (2, 'b')" (qual in-dataset src-name))
:insert-input-after-collision)))
(finally
- (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)
+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)
(catch Throwable t
(log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during collision test cleanup")))
(try (bigquery.ws/drop-dataset! admin-client project-id in-dataset) (catch Throwable _ nil))
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index f9f52d002f3..c56a18aa28b 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -547,42 +547,48 @@
;;; ------------------------------------------ Workspace Isolation ------------------------------------------
(defmethod driver/init-workspace-isolation! :clickhouse
- [_driver database workspace]
- (let [db-name (driver.u/workspace-isolation-namespace-name workspace)
+ [driver database workspace]
+ (let [db-name (:schema workspace)
canonical-db (:db (driver.conn/effective-details database))
- read-user {:user (driver.u/workspace-isolation-user-name workspace)
- :password (driver.u/random-workspace-password)}
+ read-user (:database_details workspace)
quoted-db (quote-schema db-name)
quoted-user (quote-field (:user read-user))
quoted-canonical-db (when-not (str/blank? canonical-db)
(quote-schema canonical-db))]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql (cond-> [(format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)
- (format "CREATE USER IF NOT EXISTS %s IDENTIFIED BY '%s'"
- quoted-user (:password read-user))
- ;; Least-privilege grant on the workspace's own DB (ClickHouse has no
- ;; owner auto-privileges, so grant each verb explicitly):
- ;; SELECT - read its own tables
- ;; INSERT - CTAS populate + incremental insert
- ;; CREATE TABLE - transform target
- ;; DROP TABLE - swap/cleanup
- ;; (these four also satisfy the atomic-swap RENAME TABLE.)
- (format "GRANT SELECT, INSERT, CREATE TABLE, DROP TABLE ON %s.* TO %s"
- quoted-db quoted-user)]
- quoted-canonical-db
- (conj (format "GRANT SHOW DATABASES ON %s.* TO %s"
- quoted-canonical-db quoted-user)))]
- (.addBatch ^Statement stmt ^String sql))
- (try
- (.executeBatch ^Statement stmt)
- (catch Throwable t
- (throw (driver.u/scrub-exceptions t [(:password read-user)]))))))
- {:schema db-name
- :database_details read-user}))
+ ;; No transaction: ClickHouse has no transactional DDL — CREATE/GRANT apply
+ ;; immediately. Failure recovery is compensation via the idempotent destroy.
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [stmt (.createStatement conn)]
+ (doseq [sql (cond-> [(format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)
+ (format "CREATE USER IF NOT EXISTS %s IDENTIFIED BY '%s'"
+ quoted-user (:password read-user))
+ ;; the user may survive a failed teardown; without this it would keep
+ ;; its old password while the new one gets persisted
+ (format "ALTER USER %s IDENTIFIED BY '%s'"
+ quoted-user (:password read-user))
+ ;; Least-privilege grant on the workspace's own DB (ClickHouse has no
+ ;; owner auto-privileges, so grant each verb explicitly):
+ ;; SELECT - read its own tables
+ ;; INSERT - CTAS populate + incremental insert
+ ;; CREATE TABLE - transform target
+ ;; DROP TABLE - swap/cleanup
+ ;; (these four also satisfy the atomic-swap RENAME TABLE.)
+ (format "GRANT SELECT, INSERT, CREATE TABLE, DROP TABLE ON %s.* TO %s"
+ quoted-db quoted-user)]
+ quoted-canonical-db
+ (conj (format "GRANT SHOW DATABASES ON %s.* TO %s"
+ quoted-canonical-db quoted-user)))]
+ (.addBatch ^Statement stmt ^String sql))
+ (try
+ (.executeBatch ^Statement stmt)
+ (catch Throwable t
+ (throw (driver.u/scrub-exceptions t [(:password read-user)])))))))
+ nil))
(defmethod driver/grant-workspace-read-access! :clickhouse
- [_driver database workspace schemas]
+ [driver database workspace schemas]
(let [read-user-name (-> workspace :database_details :user)
quoted-user (quote-field read-user-name)]
(when-not read-user-name
@@ -598,25 +604,29 @@
(format "GRANT SELECT ON %s.* TO %s"
(quote-schema schema)
quoted-user))]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql sqls]
- (.addBatch ^Statement stmt ^String sql))
- (.executeBatch ^Statement stmt))))))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [stmt (.createStatement conn)]
+ (doseq [sql sqls]
+ (.addBatch ^Statement stmt ^String sql))
+ (.executeBatch ^Statement stmt)))))))
(defmethod driver/destroy-workspace-isolation! :clickhouse
- [_driver database workspace]
- (let [db-name (driver.u/workspace-isolation-namespace-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)
+ [driver database workspace]
+ (let [db-name (:schema workspace)
+ username (-> workspace :database_details :user)
quoted-db (quote-schema db-name)
quoted-user (quote-field username)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql [;; DROP DATABASE cascades to all tables within it
- (format "DROP DATABASE IF EXISTS %s" quoted-db)
- (format "DROP USER IF EXISTS %s" quoted-user)]]
- (.addBatch ^Statement stmt ^String sql))
- (.executeBatch ^Statement stmt)))))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [stmt (.createStatement conn)]
+ (doseq [sql [;; DROP DATABASE cascades to all tables within it
+ (format "DROP DATABASE IF EXISTS %s" quoted-db)
+ (format "DROP USER IF EXISTS %s" quoted-user)]]
+ (.addBatch ^Statement stmt ^String sql))
+ (.executeBatch ^Statement stmt))))))
(defmethod driver/llm-sql-dialect-resource :clickhouse [_]
"metabot/prompts/dialects/clickhouse.md")
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index 183384ee1bf..a3cc9b8ff6a 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -1003,9 +1003,8 @@
(defmethod driver/init-workspace-isolation! :redshift
[_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- read-user {:user (driver.u/workspace-isolation-user-name workspace)
- :password (driver.u/random-workspace-password)}
+ (let [schema-name (:schema workspace)
+ read-user (:database_details workspace)
quoted-schema (quote-schema schema-name)
quoted-user (quote-field (:user read-user))]
(jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
@@ -1027,16 +1026,15 @@
(.executeBatch ^Statement stmt)
(catch Throwable t
(throw (driver.u/scrub-exceptions t [(:password read-user)])))))))
- {:schema schema-name
- :database_details read-user}))
+ nil))
(defmethod driver/grant-workspace-read-access! :redshift
- [_driver database workspace schemas]
+ [driver database workspace schemas]
(let [username (-> workspace :database_details :user)
quoted-user (quote-field username)
source-schemas (set schemas)
spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- ;; Pre-flight check (read-only) can run in its own transaction. Redshift's
+ ;; Pre-flight check (read-only, one connection, no transaction). Redshift's
;; GRANT statements error loudly when grant authority is missing, so PG's
;; silent-skip USAGE/SELECT class doesn't reproduce here. But two ALTER
;; DEFAULT PRIVILEGES failure modes do reproduce and need explicit checks:
@@ -1046,10 +1044,13 @@
;; - Foreign default-priv grantors: pre-existing `pg_default_acl` rows whose
;; grantor we can't impersonate at destroy time -> `DROP USER` fails
;; (GHY-3709).
- (jdbc/with-db-transaction [t-conn spec]
- (doseq [s source-schemas]
- (assert-no-public-create-grant! t-conn s)
- (assert-can-alter-default-privileges! t-conn s)))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database nil
+ (fn [^Connection conn]
+ (let [check-conn {:connection conn}]
+ (doseq [s source-schemas]
+ (assert-no-public-create-grant! check-conn s)
+ (assert-can-alter-default-privileges! check-conn s)))))
;; Grants run as auto-commit per statement so privileges are immediately
;; observable to a subsequent describe-database from a different connection.
(doseq [s source-schemas
diff --git a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj b/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
index 10465c3e7f7..927fcb8a10f 100644
--- a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
+++ b/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
@@ -877,9 +877,9 @@
(execute! (str
(format "CREATE USER \"%s\" WITH PASSWORD '%s' CREATEUSER;%n" grantor password)
(format "CREATE SCHEMA \"%s\" AUTHORIZATION \"%s\";%n" schema grantor)))
- (let [init-result (driver/init-workspace-isolation! :redshift (mt/db) workspace)
- iso-user (-> init-result :database_details :user)
- workspace+det (merge workspace init-result)]
+ (let [workspace+det (merge workspace (driver/workspace-isolation-details :redshift (mt/db) workspace))
+ _ (driver/init-workspace-isolation! :redshift (mt/db) workspace+det)
+ iso-user (-> workspace+det :database_details :user)]
(try
;; Seed the foreign-grantor default-priv: connect as the foreign role
;; and issue ALTER DEFAULT PRIVILEGES so the resulting pg_default_acl
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 6a8bea5c524..ceae92dfac9 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -1169,15 +1169,21 @@
[workspace]
(format "MB_ISOLATION_ROLE_%s" (:id workspace)))
+(defmethod driver/workspace-isolation-details :snowflake
+ [driver database workspace]
+ (-> ((get-method driver/workspace-isolation-details :sql-jdbc) driver database workspace)
+ (update :database_details assoc
+ :role (isolation-role-name workspace)
+ :use-password true)))
+
(defmethod driver/init-workspace-isolation! :snowflake
[_driver database workspace]
(let [details (driver.conn/effective-details database)
- schema-name (driver.u/workspace-isolation-namespace-name workspace)
+ schema-name (:schema workspace)
db-name (:db details)
warehouse (:warehouse details)
- role-name (isolation-role-name workspace)
- read-user {:user (driver.u/workspace-isolation-user-name workspace)
- :password (driver.u/random-workspace-password)}
+ role-name (-> workspace :database_details :role)
+ read-user (select-keys (:database_details workspace) [:user :password])
conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
(when-not db-name
(throw (ex-info (tru "Cannot initialize workspace. Snowflake connection details must include a ''db'' (database name). Set it in the database connection and retry.")
@@ -1202,20 +1208,23 @@
(format "GRANT ALL ON FUTURE TABLES IN SCHEMA %s TO ROLE %s" qualified-schema quoted-role)
(format "CREATE USER IF NOT EXISTS %s PASSWORD = '%s' MUST_CHANGE_PASSWORD = FALSE DEFAULT_ROLE = %s"
quoted-user (:password read-user) quoted-role)
+ ;; the user may survive a failed teardown; without this it would keep
+ ;; its old password while the new one gets persisted
+ (format "ALTER USER %s SET PASSWORD = '%s'"
+ quoted-user (:password read-user))
(format "GRANT ROLE %s TO USER %s" quoted-role quoted-user)]]
(jdbc/execute! conn-spec [sql]))
(catch Throwable t
(throw (driver.u/scrub-exceptions t [(:password read-user)])))))
- {:schema schema-name
- :database_details (assoc read-user :role role-name :use-password true)}))
+ nil))
(defmethod driver/destroy-workspace-isolation! :snowflake
[_driver database workspace]
(let [details (driver.conn/effective-details database)
- schema-name (or (:schema workspace) (driver.u/workspace-isolation-namespace-name workspace))
+ schema-name (:schema workspace)
db-name (:db details)
- role-name (isolation-role-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)
+ role-name (-> workspace :database_details :role)
+ username (-> workspace :database_details :user)
conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
(when-not db-name
(throw (ex-info (tru "Cannot destroy workspace. Snowflake connection details must include a ''db'' (database name). Set it in the database connection and retry.")
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 3407faeded0..a14b2116c0e 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -1166,9 +1166,9 @@
(defmethod driver/init-workspace-isolation! :sqlserver
[_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)
- password (driver.u/random-workspace-password)
+ (let [schema-name (:schema workspace)
+ {:keys [user password]} (:database_details workspace)
+ username user
escaped-password (sql.u/escape-sql password :ansi)
escaped-username (sql.u/escape-sql username :ansi)
escaped-schema (sql.u/escape-sql schema-name :ansi)
@@ -1180,6 +1180,9 @@
(doseq [sql [(format (str "IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = '%s') "
"CREATE LOGIN %s WITH PASSWORD = N'%s'")
escaped-username quoted-user escaped-password)
+ ;; the login may survive a failed teardown; without this it would keep
+ ;; its old password while the new one gets persisted
+ (format "ALTER LOGIN %s WITH PASSWORD = N'%s'" quoted-user escaped-password)
(format "IF NOT EXISTS (SELECT name FROM sys.database_principals WHERE name = '%s') CREATE USER %s FOR LOGIN %s"
escaped-username quoted-user quoted-user)
(format "IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '%s') EXEC('CREATE SCHEMA %s')"
@@ -1196,14 +1199,12 @@
(jdbc/execute! conn-spec [sql]))
(catch Throwable t
(throw (driver.u/scrub-exceptions t [password escaped-password]))))
- {:schema schema-name
- :database_details {:user username
- :password password}}))
+ nil))
(defmethod driver/destroy-workspace-isolation! :sqlserver
[_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)
+ (let [schema-name (:schema workspace)
+ username (-> workspace :database_details :user)
escaped-schema (sql.u/escape-sql schema-name :ansi)
escaped-username (sql.u/escape-sql username :ansi)
quoted-schema (quote-schema schema-name)
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index f3a5968c6aa..cf7b82c2f6f 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -2003,20 +2003,46 @@
;;; | Workspace Isolation |
;;; +----------------------------------------------------------------------------------------------------------------+
+(defmulti workspace-isolation-details
+ "Compute the isolation resource descriptors for `workspace` without touching the
+ warehouse. Returns a map with:
+
+ - :schema - The name of the isolated schema/database to create
+ - :database_details - Connection details (user, password, etc.) for the isolated user
+
+ Names are deterministic (workspace id + instance slug), but generated credentials
+ are random per call — callers must compute this once, merge it into the workspace
+ map they pass to [[init-workspace-isolation!]]/[[grant-workspace-read-access!]]/
+ [[destroy-workspace-isolation!]], and persist `:database_details` themselves.
+ Computing details before init is what makes cleanup of a partially-failed init
+ possible: destroy can run with the same map even when init never returned.
+
+ Implementations must be pure computation — no warehouse connections or mutations.
+ Reading the database's connection details (e.g. for a bound catalog) is fine;
+ callers bind [[metabase.driver.connection/with-admin-connection]] so those resolve
+ against the `:admin-details` overlay when configured."
+ {:added "0.64.0" :arglists '([driver database workspace])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
(defmulti init-workspace-isolation!
"Initialize database isolation for a workspace. Creates an isolated schema/database,
user credentials, and grants appropriate permissions for the workspace to operate
within its own namespace.
- Returns a map with:
- - :schema - The name of the isolated schema/database created
- - :database_details - Connection details (user, password, etc.) for the isolated user
+ `workspace` must already carry `:schema` and `:database_details` — the output of
+ [[workspace-isolation-details]] merged over the workspace map. Implementations
+ create resources with exactly those identifiers/credentials and must not generate
+ names or passwords themselves.
Implementations should:
- - Create an isolated schema or database for the workspace
- - Create a user with credentials that can only access that schema
+ - Create the `:schema` isolated schema or database for the workspace
+ - Create the `:database_details` user, with credentials that can only access that schema
- Grant appropriate permissions (CREATE, INSERT, SELECT, etc.) on the isolated schema
+ The return value is unspecified; callers should persist the precomputed details,
+ not anything returned from here.
+
This is an enterprise feature. Drivers must also return true for
(database-supports? driver :workspace database) to indicate support.
@@ -2034,6 +2060,12 @@
This includes dropping schemas/databases, users, roles, and any other
resources created by init-workspace-isolation!.
+ `workspace` carries `:schema` and `:database_details` — either the persisted
+ values from provisioning, or freshly computed via [[workspace-isolation-details]]
+ when nothing was persisted (crashed init). Implementations read identifiers from
+ the map rather than recomputing them, and must not rely on `:password` matching
+ what's on the warehouse (a recomputed password never does).
+
Should be called when deleting a workspace. Implementations should be
idempotent - calling on an already-destroyed workspace should not error.
@@ -2064,33 +2096,6 @@
dispatch-on-initialized-driver
:hierarchy #'hierarchy)
-(defmulti check-isolation-permissions
- "Check if database connection has sufficient permissions for workspace isolation.
-
- Rather than directly checking permissions, this method performs the actual isolation
- operations (init workspace, grant access, destroy resources) in a test workspace
- because:
-
- 1. Some databases don't provide reliable APIs to check permissions a priori.
- 2. Keeping static permission checks in sync with the actual operations is error-prone.
- 3. A database user might have the necessary workspace permissions even if they
- lack the introspection permissions to query permission tables.
-
- Isolation operations run in a transaction that is always rolled back (for databases
- that support transactional DDL), or are manually cleaned up immediately after testing
- (for databases where transactions don't work, like BigQuery).
-
- `test-table` is an optional {:schema ... :name ...} map used to test GRANT SELECT.
- If nil, the grant test is skipped.
-
- Returns nil on success, or an error message string on failure.
-
- Default :sql-jdbc implementation tests CREATE SCHEMA, CREATE USER, GRANT, and DROP.
- Drivers can override for database-specific syntax."
- {:added "0.59.0" :arglists '([driver database test-table])}
- dispatch-on-initialized-driver
- :hierarchy #'hierarchy)
-
(defmulti validate-impersonated-query
"Validates a query for impersonation. Returns the query if it is valid and throws otherwise."
{:added "0.60.0" :arglists '([driver query])}
diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.clj
index 8611eaf6a0c..30af1f799af 100644
--- a/src/metabase/driver/h2.clj
+++ b/src/metabase/driver/h2.clj
@@ -1,7 +1,6 @@
(ns metabase.driver.h2
(:refer-clojure :exclude [some every?])
(:require
- [clojure.java.jdbc :as jdbc]
[clojure.math.combinatorics :as math.combo]
[clojure.string :as str]
[java-time.api :as t]
@@ -749,54 +748,72 @@
(let [[_file options] (connection-string->file+options connection-string)]
(get options "USER")))
-(defmethod driver/init-workspace-isolation! :h2
+(defmethod driver/workspace-isolation-details :h2
[_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)
+ ;; H2 embeds credentials in the :db connection string, so build a new one off the
+ ;; connection's (admin-overlay-resolved) string rather than returning :user/:password.
+ (let [username (driver.u/workspace-isolation-user-name workspace)
password (driver.u/random-workspace-password)
- ;; H2 embeds credentials in the :db connection string, so we need to build a new one
- original-db (:db (driver.conn/effective-details database))
- new-db (replace-credentials original-db username password)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql [(format "CREATE USER IF NOT EXISTS \"%s\" PASSWORD '%s'" username password)
- (format "CREATE SCHEMA IF NOT EXISTS \"%s\" AUTHORIZATION \"%s\"" schema-name username)
- (format "GRANT ALL ON SCHEMA \"%s\" TO \"%s\"" schema-name username)]]
- (.addBatch ^Statement stmt ^String sql))
- (try
- (.executeBatch ^Statement stmt)
- (catch Throwable t
- (throw (driver.u/scrub-exceptions t [password]))))))
- {:schema schema-name
- :database_details {:db new-db}}))
+ original-db (:db (driver.conn/effective-details database))]
+ {:schema (driver.u/workspace-isolation-namespace-name workspace)
+ :database_details {:db (replace-credentials original-db username password)}}))
+
+(defmethod driver/init-workspace-isolation! :h2
+ [driver database workspace]
+ (let [schema-name (:schema workspace)
+ [_file {username "USER" password "PASSWORD"}]
+ (connection-string->file+options (-> workspace :database_details :db))]
+ ;; No transaction: H2 DDL (CREATE USER/SCHEMA, GRANT) commits any open
+ ;; transaction, so a wrapper would be decorative. Failure recovery is
+ ;; compensation via the idempotent destroy, not rollback.
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [sql [(format "CREATE USER IF NOT EXISTS \"%s\" PASSWORD '%s'" username password)
+ ;; the user may survive a failed teardown; without this it would keep
+ ;; its old password while the new one gets persisted
+ (format "ALTER USER \"%s\" SET PASSWORD '%s'" username password)
+ (format "CREATE SCHEMA IF NOT EXISTS \"%s\" AUTHORIZATION \"%s\"" schema-name username)
+ (format "GRANT ALL ON SCHEMA \"%s\" TO \"%s\"" schema-name username)]]
+ (.addBatch ^Statement stmt ^String sql))
+ (try
+ (.executeBatch ^Statement stmt)
+ (catch Throwable t
+ (throw (driver.u/scrub-exceptions t [password])))))))
+ nil))
(defmethod driver/destroy-workspace-isolation! :h2
- [_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- username (driver.u/workspace-isolation-user-name workspace)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql [;; CASCADE drops all objects (tables, etc.) in the schema
- (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name)
- (format "DROP USER IF EXISTS \"%s\"" username)]]
- (.addBatch ^Statement stmt ^String sql))
- (.executeBatch ^Statement stmt)))))
+ [driver database workspace]
+ (let [schema-name (:schema workspace)
+ username (-> workspace :database_details :db get-user-from-connection-string)]
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [sql [;; CASCADE drops all objects (tables, etc.) in the schema
+ (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name)
+ (format "DROP USER IF EXISTS \"%s\"" username)]]
+ (.addBatch ^Statement stmt ^String sql))
+ (.executeBatch ^Statement stmt))))))
(defmethod driver/grant-workspace-read-access! :h2
- [_driver database workspace schemas]
+ [driver database workspace schemas]
(let [username (-> workspace :database_details :db get-user-from-connection-string)
qu (sql.u/quote-name :h2 :field username)
schemas (distinct schemas)]
;; H2 uses GRANT SELECT ON SCHEMA schemaName TO userName.
;; Schema-wide grant covers existing + future tables. Per-table grants
;; are not emitted: workspace input shape is per-namespace, not per-table.
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [schema schemas]
- (.addBatch ^Statement stmt
- ^String (format "GRANT SELECT ON SCHEMA %s TO %s"
- (sql.u/quote-name :h2 :schema schema) qu)))
- (.executeBatch ^Statement stmt)))))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [schema schemas]
+ (.addBatch ^Statement stmt
+ ^String (format "GRANT SELECT ON SCHEMA %s TO %s"
+ (sql.u/quote-name :h2 :schema schema) qu)))
+ (.executeBatch ^Statement stmt))))))
(defmethod driver/llm-sql-dialect-resource :h2 [_]
"metabot/prompts/dialects/h2.md")
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index 47568d58c64..6531240fcc3 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -32,7 +32,7 @@
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x]
- [metabase.util.i18n :refer [deferred-tru tru]]
+ [metabase.util.i18n :refer [deferred-tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.memoize :as memoize]
@@ -1313,61 +1313,59 @@
(seq (jdbc/query conn ["SELECT 1 FROM mysql.user WHERE user = ?" username])))
(defmethod driver/init-workspace-isolation! :mysql
- [_driver database workspace]
+ [driver database workspace]
;; MySQL doesn't have schemas in the PostgreSQL sense - each database is its own namespace.
;; We create a separate database for workspace isolation.
- (let [db-name (driver.u/workspace-isolation-namespace-name workspace)
- user (driver.u/workspace-isolation-user-name workspace)
- password (driver.u/random-workspace-password)
+ (let [db-name (:schema workspace)
+ {:keys [user password]} (:database_details workspace)
escaped-password (sql.u/escape-sql password :ansi)
quoted-db (quote-schema db-name)
quoted-user (quote-field user)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (let [user-sql (if (mysql-user-exists? t-conn user)
- (format "ALTER USER %s@'%%' IDENTIFIED BY '%s'"
- quoted-user escaped-password)
- (format "CREATE USER %s@'%%' IDENTIFIED BY '%s'"
- quoted-user escaped-password))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql [;; Create the isolated database
- (format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)
- user-sql
- ;; Least-privilege grant on the workspace's own DB (vs. ALL PRIVILEGES,
- ;; dropping GRANT OPTION, CREATE VIEW/ROUTINE, TRIGGER, etc.):
- ;; SELECT, INSERT, UPDATE, DELETE - full DML on its own tables
- ;; CREATE - transform target / CTAS
- ;; DROP - swap/cleanup
- ;; ALTER - required (with DROP/CREATE) for RENAME TABLE swaps
- (format "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON %s.* TO %s@'%%'"
- quoted-db quoted-user)]]
- (.addBatch ^Statement stmt ^String sql))
- (try
- (.executeBatch ^Statement stmt)
- (catch Throwable t
- (throw (driver.u/scrub-exceptions t [password escaped-password])))))))
- {:schema db-name
- ;; Intentionally omit `:db` from `:database_details`: when the workspace
- ;; loader merges these over the canonical Database's `:details`, we must
- ;; not overwrite the connection's bound database. MySQL workspace users
- ;; need to read from the canonical input DB (granted via `GRANT SELECT
- ;; ON <input-db>.*`) while output writes go to `db-name` via fully
- ;; qualified `INSERT INTO <db-name>.<table>`. The output DB lives in
- ;; the WSD row's `:output_namespace`, not in connection details.
- :database_details {:user user, :password password}}))
+ ;; No transaction: MySQL DDL (CREATE DATABASE/USER, GRANT) implicitly commits
+ ;; per statement, so a transaction wrapper would be decorative. Failure
+ ;; recovery is compensation via the idempotent destroy, not rollback.
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (let [user-sql (if (mysql-user-exists? {:connection conn} user)
+ (format "ALTER USER %s@'%%' IDENTIFIED BY '%s'"
+ quoted-user escaped-password)
+ (format "CREATE USER %s@'%%' IDENTIFIED BY '%s'"
+ quoted-user escaped-password))]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [sql [;; Create the isolated database
+ (format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)
+ user-sql
+ ;; Least-privilege grant on the workspace's own DB (vs. ALL PRIVILEGES,
+ ;; dropping GRANT OPTION, CREATE VIEW/ROUTINE, TRIGGER, etc.):
+ ;; SELECT, INSERT, UPDATE, DELETE - full DML on its own tables
+ ;; CREATE - transform target / CTAS
+ ;; DROP - swap/cleanup
+ ;; ALTER - required (with DROP/CREATE) for RENAME TABLE swaps
+ (format "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON %s.* TO %s@'%%'"
+ quoted-db quoted-user)]]
+ (.addBatch ^Statement stmt ^String sql))
+ (try
+ (.executeBatch ^Statement stmt)
+ (catch Throwable t
+ (throw (driver.u/scrub-exceptions t [password escaped-password]))))))))
+ nil))
(defmethod driver/destroy-workspace-isolation! :mysql
- [_driver database workspace]
+ [driver database workspace]
(let [db-name (:schema workspace)
username (-> workspace :database_details :user)
quoted-db (quote-schema db-name)
quoted-user (quote-field username)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql (cond-> [(format "DROP DATABASE IF EXISTS %s" quoted-db)]
- (mysql-user-exists? t-conn username)
- (conj (format "DROP USER IF EXISTS %s@'%%'" quoted-user)))]
- (.addBatch ^Statement stmt ^String sql))
- (.executeBatch ^Statement stmt)))))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [sql (cond-> [(format "DROP DATABASE IF EXISTS %s" quoted-db)]
+ (mysql-user-exists? {:connection conn} username)
+ (conj (format "DROP USER IF EXISTS %s@'%%'" quoted-user)))]
+ (.addBatch ^Statement stmt ^String sql))
+ (.executeBatch ^Statement stmt))))))
(defn- grant-workspace-read-access-sqls
"Build SQL statements that grant `username` SELECT on all tables in each database
@@ -1383,7 +1381,7 @@
source-databases)))
(defmethod driver/grant-workspace-read-access! :mysql
- [_driver database workspace schemas]
+ [driver database workspace schemas]
;; Each entry in `schemas` is interpreted as a MySQL database name. The
;; workspace SA gets database-wide SELECT via `GRANT SELECT ON db.*`. The
;; Metabase `Database` row's `:details.db` is the bound database, but MySQL
@@ -1391,64 +1389,13 @@
;; so we don't reject inputs that don't equal `:details.db`.
(let [username (-> workspace :database_details :user)
sqls (grant-workspace-read-access-sqls username schemas)]
- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
- (doseq [sql sqls]
- (.addBatch ^Statement stmt ^String sql))
- (.executeBatch ^Statement stmt)))))
-
-;; MySQL doesn't support transactional DDL, so we need to override check-isolation-permissions
-;; to manually clean up after testing rather than relying on transaction rollback.
-(def ^:private perm-check-workspace-id "-1337")
-
-(defmethod driver/check-isolation-permissions :mysql
- [driver database test-table]
- (let [test-workspace {:id perm-check-workspace-id
- :name "_mb_perm_check_"}]
- (driver.conn/with-admin-connection
- (sql-jdbc.execute/do-with-connection-with-options
- driver
- database
- {:write? true}
- (fn [^Connection _conn]
- (let [result (try
- (let [init-result (try
- (driver/init-workspace-isolation! driver database test-workspace)
- (catch Exception e
- (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE DATABASE/USER): {0}"
- (ex-message e))
- {:step :init} e))))
- workspace-with-details (merge test-workspace init-result)]
- (when test-table
- (try
- ;; `grant-workspace-read-access!` takes a vector of
- ;; schema-name strings. MySQL has no schema layer; each
- ;; entry is interpreted as a database name. `test-table`'s
- ;; `:schema` from the caller IS the MySQL database name.
- (driver/grant-workspace-read-access! driver database workspace-with-details
- [(:schema test-table)])
- (catch Exception e
- (throw (ex-info (tru "Failed to grant read access to database {0}: {1}"
- (quote-schema (:schema test-table)) (ex-message e))
- {:step :grant :table test-table} e)))))
- (try
- (driver/destroy-workspace-isolation! driver database workspace-with-details)
- (catch Exception e
- (throw (ex-info (tru "Failed to destroy workspace isolation (DROP DATABASE/USER): {0}"
- (ex-message e))
- {:step :destroy} e))))
- nil)
- (catch Exception e
- ;; On failure, attempt cleanup
- (try
- (driver/destroy-workspace-isolation! driver database
- (merge test-workspace
- {:schema (driver.u/workspace-isolation-namespace-name test-workspace)
- :database_details {:user (driver.u/workspace-isolation-user-name test-workspace)}}))
- (catch Exception _cleanup-error
- nil))
- (ex-message e)))]
- result))))))
+ (sql-jdbc.execute/do-with-connection-with-options
+ driver database {:write? true}
+ (fn [^Connection conn]
+ (with-open [^Statement stmt (.createStatement conn)]
+ (doseq [sql sqls]
+ (.addBatch ^Statement stmt ^String sql))
+ (.executeBatch ^Statement stmt))))))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Indexes (Index Manager) |
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index dc3de2b78b7..c93fc0ae26f 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -1668,9 +1668,8 @@
(defmethod driver/init-workspace-isolation! :postgres
[_driver database workspace]
- (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)
- read-user {:user (driver.u/workspace-isolation-user-name workspace)
- :password (driver.u/random-workspace-password)}
+ (let [schema-name (:schema workspace)
+ read-user (:database_details workspace)
quoted-schema (quote-schema schema-name)
quoted-user (quote-field (:user read-user))]
(jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
@@ -1695,8 +1694,7 @@
(.executeBatch ^Statement stmt)
(catch Throwable t
(throw (driver.u/scrub-exceptions t [(:password read-user)])))))))
- {:schema schema-name
- :database_details read-user}))
+ nil))
(defmethod driver/destroy-workspace-isolation! :postgres
[_driver database workspace]
@@ -1731,7 +1729,7 @@
source-schemas)))
(defmethod driver/grant-workspace-read-access! :postgres
- [_driver database workspace schemas]
+ [driver database workspace schemas]
(let [username (-> workspace :database_details :user)
source-schemas (set schemas)
;; Pre-flight check: each input schema must not grant CREATE to PUBLIC. See
@@ -1739,12 +1737,16 @@
;; isolation hole this catches. We probe per-schema so only the schemas
;; actually used as inputs need to be locked down — schemas the workspace
;; never touches can keep their default ACLs.
- _ (jdbc/with-db-transaction [check-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
- (doseq [s source-schemas]
- (assert-no-public-create-grant! check-conn s)
- (assert-has-usage-grant-option! check-conn s)
- (assert-has-grant-option! check-conn s)
- (assert-can-alter-default-privileges! check-conn s)))
+ ;; read-only probes — one connection, no transaction needed
+ _ (sql-jdbc.execute/do-with-connection-with-options
+ driver database nil
+ (fn [^Connection conn]
+ (let [check-conn {:connection conn}]
+ (doseq [s source-schemas]
+ (assert-no-public-create-grant! check-conn s)
+ (assert-has-usage-grant-option! check-conn s)
+ (assert-has-grant-option! check-conn s)
+ (assert-can-alter-default-privileges! check-conn s)))))
sqls (grant-workspace-read-access-sqls username schemas)]
(jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]
(with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]
diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.clj
index 5a0ae9500c9..fe038d1b330 100644
--- a/src/metabase/driver/sql_jdbc.clj
+++ b/src/metabase/driver/sql_jdbc.clj
@@ -8,7 +8,6 @@
[medley.core :as m]
[metabase.driver :as driver]
[metabase.driver-api.core :as driver-api]
- [metabase.driver.connection :as driver.conn]
[metabase.driver.sql :as driver.sql]
[metabase.driver.sql-jdbc.actions :as sql-jdbc.actions]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
@@ -20,9 +19,9 @@
[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database]
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.sync :as driver.s]
+ [metabase.driver.util :as driver.u]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.util.honey-sql-2 :as h2x]
- [metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
[metabase.util.performance :refer [mapv select-keys]]
[next.jdbc])
@@ -407,49 +406,8 @@
sql-jdbc.describe-database/all-schemas
(m/find-first #(= % schema))))))
-;;; +----------------------------------------------------------------------------------------------------------------+
-;;; | Workspace Isolation |
-;;; +----------------------------------------------------------------------------------------------------------------+
-
-(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")
-
-(defmethod driver/check-isolation-permissions :sql-jdbc
- [driver database test-table]
- (let [test-workspace {:id perm-check-workspace-id
- :name "_mb_perm_check_"}]
- (driver.conn/with-admin-connection
- (sql-jdbc.execute/do-with-connection-with-options
- driver
- database
- {:write? true}
- (fn [^Connection conn]
- (.setAutoCommit conn false)
- (try
- (let [init-result (try
- (driver/init-workspace-isolation! driver database test-workspace)
- (catch Exception e
- (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE SCHEMA/USER): {0}"
- (ex-message e))
- {:step :init} e))))
- workspace-with-details (merge test-workspace init-result)]
- (when test-table
- (try
- ;; `grant-workspace-read-access!` takes a vector of schema-name
- ;; strings; per-table grants no longer exist.
- (driver/grant-workspace-read-access! driver database workspace-with-details
- [(:schema test-table)])
- (catch Exception e
- (throw (ex-info (tru "Failed to grant read access to schema {0}: {1}"
- (:schema test-table) (ex-message e))
- {:step :grant :table test-table} e)))))
- (try
- (driver/destroy-workspace-isolation! driver database workspace-with-details)
- (catch Exception e
- (throw (ex-info (tru "Failed to destroy workspace isolation (DROP SCHEMA/USER): {0}"
- (ex-message e))
- {:step :destroy} e)))))
- nil
- (catch Exception e
- (ex-message e))
- (finally
- (.rollback conn))))))))
+(defmethod driver/workspace-isolation-details :sql-jdbc
+ [_driver _database workspace]
+ {:schema (driver.u/workspace-isolation-namespace-name workspace)
+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)
+ :password (driver.u/random-workspace-password)}})
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index 225c85cc97c..052d4c3d5d9 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -2639,10 +2639,10 @@
"GRANT %s TO CURRENT_USER; "
"CREATE SCHEMA %s AUTHORIZATION %s;")
qgrantor qgrantor qschema qgrantor)])
- (let [init-result (driver/init-workspace-isolation! :postgres (mt/db) workspace)
- iso-user (-> init-result :database_details :user)
- qiso (sql.u/quote-name :postgres :field iso-user)
- workspace+det (merge workspace init-result)]
+ (let [workspace+det (merge workspace (driver/workspace-isolation-details :postgres (mt/db) workspace))
+ _ (driver/init-workspace-isolation! :postgres (mt/db) workspace+det)
+ iso-user (-> workspace+det :database_details :user)
+ qiso (sql.u/quote-name :postgres :field iso-user)]
(try
;; Seed the foreign-grantor default-priv: set the grantor role and
;; issue ALTER DEFAULT PRIVILEGES so the resulting pg_default_acl