Skip to content

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

Lock config-managed workspaces against runtime edits (#74929)

Timothy S. Dean680c6ec1a0e2
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj
index 7c82a1d97de..d714ff8fba0 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj
@@ -102,6 +102,7 @@
[metabase-enterprise.advanced-config.file.settings]
[metabase-enterprise.advanced-config.file.users]
[metabase-enterprise.advanced-config.file.workspace :as advanced-config.file.workspace]
+ [metabase-enterprise.workspaces.core :as ws]
[metabase.lib.core :as lib]
[metabase.premium-features.core :as premium-features]
[metabase.util :as u]
@@ -287,6 +288,15 @@
(defn boot-initialize!
"Boot-time entry point: read the config file from disk and run [[initialize!]]
- with `{{env VAR}}` template expansion enabled. No-op when no file is present."
+ with `{{env VAR}}` template expansion enabled. No-op when no file is present.
+ When the config contains a `:workspace` section, locks the workspace against
+ runtime mutation (see
+ [[metabase-enterprise.workspaces.core/workspace-locked-by-config?]])."
[]
- (initialize! (config-from-disk) {:expand-templates? true}))
+ (let [parsed (config-from-disk)
+ result (initialize! parsed {:expand-templates? true})]
+ ;; Only the boot path locks. Runtime config uploads (POST /api/ee/advanced-config)
+ ;; call `initialize!` directly, bypassing this wrapper, so they never lock.
+ (when (get-in parsed [:config :workspace])
+ (ws/mark-locked-by-config!))
+ result))
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj
index 55e1be3fe0c..75b05213af0 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj
@@ -7,6 +7,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.lib.schema.id :as lib.schema.id]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.malli.schema :as ms]
[metabase.workspaces.core :as ws.oss]))
@@ -25,7 +26,8 @@
(def ^:private WorkspaceInstance
[:map
[:name ms/NonBlankString]
- [:databases [:map-of ::lib.schema.id/database WorkspaceInstanceDatabase]]])
+ [:databases [:map-of ::lib.schema.id/database WorkspaceInstanceDatabase]]
+ [:can_write :boolean]])
(def ^:private TableRemapping
[:map
@@ -52,7 +54,20 @@
(defn- present-workspace-instance [workspace]
{:name (:name workspace)
- :databases (update-vals (:databases workspace) present-workspace-instance-database)})
+ :databases (update-vals (:databases workspace) present-workspace-instance-database)
+ :can_write (not (ws/workspace-locked-by-config?))})
+
+;;; ------------------------------------------------ Guards ----------------------------------------------------
+
+(defn- check-workspace-writable!
+ "Throw a 400 if this instance's workspace is locked by deployment config (a
+ `config.yml` `:workspace` section or the `MB_INSTANCE_WORKSPACE` env var).
+ Call after `check-superuser`, so a non-admin still gets a 403 rather than this."
+ []
+ (when (ws/workspace-locked-by-config?)
+ (throw (ex-info (tru "This instance''s workspace is set by deployment config (`config.yml` or the `MB_INSTANCE_WORKSPACE` env var) and can''t be changed at runtime. Remove that config and restart this instance to enable workspace edits.")
+ {:status-code 400
+ :workspace-locked-by-config true}))))
;;; ---------------------------------------------- Endpoints ---------------------------------------------------
@@ -64,7 +79,11 @@
section of `config.yml`, or at runtime by `POST /current`. `:data` is `null`
when no workspace was loaded — i.e. this is a manager-only instance, or no
`config.yml` was present at boot and `POST /current` hasn't been called.
- The envelope avoids an empty JSON body for the `nil` case."
+ The envelope avoids an empty JSON body for the `nil` case.
+
+ The workspace's `can_write` flag is `false` when it was set by deployment
+ config (`config.yml` or `MB_INSTANCE_WORKSPACE`); `POST`/`DELETE` are rejected
+ in that case."
[]
(api/check-superuser)
{:data (some-> (ws/instance-workspace) present-workspace-instance)})
@@ -73,11 +92,15 @@
"Install a workspace config on this instance at runtime. Accepts the same shape
`GET /current` returns and persists it via the `instance-workspace` setting so
it survives restarts. Use this on a running instance to enter workspace mode
- without restarting from `config.yml`."
+ without restarting from `config.yml`.
+
+ Returns 400 if the workspace was set by deployment config (`config.yml` or
+ `MB_INSTANCE_WORKSPACE`) and so is read-only."
[_route-params
_query-params
body :- ::ws.oss/workspace-instance-config]
(api/check-superuser)
+ (check-workspace-writable!)
(ws/set-instance-workspace! body)
(present-workspace-instance (ws/instance-workspace)))
@@ -85,9 +108,13 @@
"Clear the workspace config on this instance. After this returns, the instance
is no longer in workspace mode and `GET /current` returns `nil`. Also drops
every `TableRemapping` row, since stale mappings from the prior workspace
- would otherwise keep rewriting queries on the now-unmanaged databases."
+ would otherwise keep rewriting queries on the now-unmanaged databases.
+
+ Returns 400 if the workspace was set by deployment config (`config.yml` or
+ `MB_INSTANCE_WORKSPACE`) and so is read-only."
[]
(api/check-superuser)
+ (check-workspace-writable!)
(ws/clear-instance-workspace!)
(ws/clear-all-remappings!)
nil)
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
index fb75ad84d6f..46563a967ef 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
@@ -38,6 +38,7 @@
[metabase.driver.sql :as driver.sql]
[metabase.driver.util :as driver.u]
[metabase.premium-features.core :refer [defenterprise]]
+ [metabase.settings.core :as setting]
[metabase.util.malli :as mu]
[metabase.workspaces.core :as ws]
[toucan2.core :as t2]))
@@ -102,6 +103,30 @@
(ws.settings/instance-workspace! nil)
nil)
+;;; ------------------------------- Instance-workspace lock -------------------------------
+
+(defonce ^:private locked-by-config?* (atom false))
+
+(defn workspace-locked-by-config?
+ "True iff this instance's workspace was set by out-of-band deployment config —
+ a `:workspace` section in `config.yml` at boot, or the `MB_INSTANCE_WORKSPACE`
+ env var — and so must not be mutated through the workspace-instance API. The
+ lock is per-boot and not persisted: removing the config and restarting clears it.
+
+ Read at the HTTP boundary only. Do NOT consult it from `set-instance-workspace!`
+ or `clear-instance-workspace!` — internal callers and test fixtures rely on
+ those setters staying unconditional."
+ []
+ (or @locked-by-config?*
+ ;; Checked inline rather than captured at boot like the atom: nothing reads
+ ;; this env var at startup, and the lookup is cheap.
+ (some? (setting/env-var-value :instance-workspace))))
+
+(defn mark-locked-by-config!
+ "Called by the boot loader only; runtime code paths never flip the lock."
+ []
+ (reset! locked-by-config?* true))
+
(defn clear-all-remappings!
"Delete every `:model/TableRemapping` row across all databases."
[]
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj
index 50c5bb7c589..247b4997f14 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj
@@ -72,10 +72,35 @@
(is (= "Uploaded Workspace" (:name stored)))
(is (= {:schema "ws_uploaded"} (get-in stored [:databases (:id db) :output])))))))
(finally
- ;; `clear-instance-workspace!` is gated on `:workspaces` (see GHY-3685); this
- ;; cleanup must be allowed to run regardless of what features the test enabled.
- (mt/with-premium-features #{:workspaces}
- (ws/clear-instance-workspace!))
+ (ws/clear-instance-workspace!)
+ (when-let [db-id (t2/select-one-pk :model/Database :name db-name :engine "postgres")]
+ (t2/delete! :model/Database db-id)))))))
+
+(deftest runtime-upload-via-api-does-not-set-the-lock-test
+ (testing "POST /api/ee/advanced-config with a :workspace section does NOT lock the workspace (only the boot loader does)"
+ (let [db-name (str "advanced-config-ws-" (random-uuid))
+ payload {:version 1
+ :config {:databases [{:name db-name
+ :engine "postgres"
+ :details {}}]
+ :workspace {:name "Uploaded Workspace"
+ :databases {(keyword db-name) {:input_schemas ["public"]
+ :output {:schema "ws_uploaded"}}}}}}
+ lock-atom @#'ws/locked-by-config?*
+ prior @lock-atom]
+ (try
+ (reset! lock-atom false)
+ (mt/with-premium-features #{:config-text-file :workspaces}
+ (mt/with-temporary-setting-values [config-from-file-sync-databases false]
+ (with-redefs [metabase.driver.util/can-connect-with-details? (constantly true)]
+ (mt/user-http-request :crowberto :post 204 "ee/advanced-config/"
+ (first (multipart (yaml-bytes payload)))
+ (second (multipart (yaml-bytes payload)))))))
+ (is (false? (ws/workspace-locked-by-config?))
+ "a runtime upload must not flip the boot lock")
+ (finally
+ (reset! lock-atom prior)
+ (ws/clear-instance-workspace!)
(when-let [db-id (t2/select-one-pk :model/Database :name db-name :engine "postgres")]
(t2/delete! :model/Database db-id)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj
index 76fb734c341..9e37f4ff24b 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj
@@ -16,16 +16,20 @@
(use-fixtures :each
(fn [thunk]
- ;; Default to `:workspaces` enabled for tests in this ns — most exercise
- ;; `apply-workspace-section!` which calls `set-instance-workspace!` (gated on
- ;; `:workspaces`, see GHY-3685). Tests that need the feature *absent* override
- ;; with their own `mt/with-premium-features` block.
+ ;; Several tests drive a `:workspace` section through `initialize!`, which
+ ;; requires the `:workspaces` feature; default it on here. Tests that assert
+ ;; the feature's *absence* override with their own `mt/with-premium-features`.
+ ;; Also save/restore the boot-lock atom so the boot-lock tests below can't
+ ;; leak a flipped lock into the rest of the suite.
(mt/with-premium-features #{:workspaces}
(binding [advanced-config.file/*supported-versions* {:min 1, :max 1}]
- (try
- (thunk)
- (finally
- (ws/clear-instance-workspace!)))))))
+ (let [lock-atom @#'ws/locked-by-config?*
+ prior @lock-atom]
+ (try
+ (thunk)
+ (finally
+ (ws/clear-instance-workspace!)
+ (reset! lock-atom prior))))))))
(defn- load-fixture-by-driver [driver]
(-> (str "metabase_enterprise/workspaces/resources/workspace_config_" (name driver) ".yml")
@@ -296,3 +300,52 @@
(advanced-config.file.workspace/apply-workspace-section! section)
(is (= (:output expectations) (ws/db-workspace-namespace db-id))
(str driver " setting output matches fixture"))))))))))
+
+;;; ----------------------------------------- boot lock wiring ----------------------------------------
+;;;
+;;; `boot-initialize!` is the sole boot entry point. When it parses a
+;;; `config.yml` containing a `:workspace` section, it flips the in-memory
+;;; `workspace-locked-by-config?*` atom. Runtime callers of `initialize!` (the
+;;; `POST /api/ee/advanced-config` path) and direct callers of
+;;; `apply-workspace-section!` do NOT flip the lock — the boot wrapper is the
+;;; only thing that does.
+
+(defn- lock-atom [] @#'ws/locked-by-config?*)
+
+(deftest boot-initialize!-sets-the-lock-when-config-has-workspace-test
+ (testing "boot-initialize!, given a parsed config.yml with a :workspace section, flips the lock"
+ (mt/with-empty-h2-app-db!
+ (mt/with-premium-features #{:config-text-file :workspaces}
+ (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]
+ (reset! (lock-atom) false)
+ (with-redefs [advanced-config.file/config-from-disk
+ (constantly {:version 1
+ :config {:workspace (workspace-section "ws-test-db")}})]
+ (advanced-config.file/boot-initialize!))
+ (is (true? (ws/workspace-locked-by-config?))))))))
+
+(deftest boot-initialize!-does-not-set-the-lock-when-config-has-no-workspace-test
+ (testing "boot-initialize! with no :workspace section in the config leaves the lock false"
+ (mt/with-empty-h2-app-db!
+ (reset! (lock-atom) false)
+ (with-redefs [advanced-config.file/config-from-disk
+ (constantly {:version 1
+ :config {:settings {}}})]
+ (advanced-config.file/boot-initialize!))
+ (is (false? (ws/workspace-locked-by-config?))))))
+
+(deftest boot-initialize!-no-config-file-does-not-set-the-lock-test
+ (testing "boot-initialize! when no config.yml is present (config-from-disk returns nil) leaves the lock false"
+ (mt/with-empty-h2-app-db!
+ (reset! (lock-atom) false)
+ (with-redefs [advanced-config.file/config-from-disk (constantly nil)]
+ (advanced-config.file/boot-initialize!))
+ (is (false? (ws/workspace-locked-by-config?))))))
+
+(deftest apply-workspace-section!-direct-call-does-not-set-the-lock-test
+ (testing "calling apply-workspace-section! directly (the runtime / test path) does NOT flip the lock"
+ (mt/with-empty-h2-app-db!
+ (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]
+ (reset! (lock-atom) false)
+ (advanced-config.file.workspace/apply-workspace-section! (workspace-section "ws-test-db"))
+ (is (false? (ws/workspace-locked-by-config?)))))))
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj
index 7575b90d4f6..c77466279a7 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj
@@ -5,6 +5,7 @@
(:require
[clojure.test :refer [deftest is testing use-fixtures]]
[metabase-enterprise.workspaces.core :as ws]
+ [metabase-enterprise.workspaces.test-util :as ws.tu]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[toucan2.core :as t2]))
@@ -38,6 +39,8 @@
(let [result (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current")
data (:data result)]
(is (= "Current Test" (:name data)))
+ (testing "can_write is true when the workspace is not locked by config"
+ (is (true? (:can_write data))))
(testing "databases is a map keyed by database id with input_schemas + output"
(is (= 1 (count (:databases data))))
(let [databases (:databases data)
@@ -47,6 +50,23 @@
(is (= ["PUBLIC"] (:input_schemas db)))
(is (= {:schema "ws_alice"} (:output db)))))))))
+(deftest current-with-workspace-locked-returns-can-write-false-test
+ (testing "GET /ee/workspace-instance/current returns can_write false when locked by config"
+ (ws.tu/with-workspace-locked-by-config
+ (fn []
+ (with-redefs [ws/instance-workspace (constantly {:name "Locked Test" :databases {}})]
+ (let [data (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]
+ (is (= "Locked Test" (:name data)))
+ (is (false? (:can_write data)))))))))
+
+(deftest current-with-workspace-locked-by-env-var-returns-can-write-false-test
+ (testing "GET /ee/workspace-instance/current returns can_write false when MB_INSTANCE_WORKSPACE is set"
+ (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]
+ (with-redefs [ws/instance-workspace (constantly {:name "Env Locked Test" :databases {}})]
+ (let [data (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]
+ (is (= "Env Locked Test" (:name data)))
+ (is (false? (:can_write data))))))))
+
(deftest post-current-superuser-only-test
(testing "POST /ee/workspace-instance/current requires data analyst or superuser"
(is (= "You don't have permissions to do that."
@@ -60,11 +80,56 @@
:output {:schema "ws_api_install"}}}}
posted (mt/user-http-request :crowberto :post 200 "ee/workspace-instance/current" payload)]
(is (= "Installed via API" (:name posted)))
+ (testing "the POST response reports can_write true while unlocked"
+ (is (true? (:can_write posted))))
(testing "GET reflects the workspace just installed"
(let [fetched (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]
(is (= "Installed via API" (:name fetched)))
(is (= 1 (count (:databases fetched)))))))))
+(deftest post-current-rejects-when-locked-test
+ (testing "POST /ee/workspace-instance/current returns 400 when locked by config"
+ (ws.tu/with-workspace-locked-by-config
+ (fn []
+ (let [resp (mt/user-http-request :crowberto :post 400 "ee/workspace-instance/current"
+ {:name "x" :databases {}})]
+ (is (re-find #"config\.yml" (:message resp))))))))
+
+(deftest post-current-rejects-when-env-var-set-test
+ (testing "POST /ee/workspace-instance/current returns 400 when MB_INSTANCE_WORKSPACE is set"
+ (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]
+ (let [resp (mt/user-http-request :crowberto :post 400 "ee/workspace-instance/current"
+ {:name "x" :databases {}})]
+ (is (re-find #"config\.yml" (:message resp)))))))
+
+(deftest delete-current-rejects-when-locked-test
+ (testing "DELETE /ee/workspace-instance/current returns 400 when locked by config"
+ (ws.tu/with-workspace-locked-by-config
+ (fn []
+ (let [resp (mt/user-http-request :crowberto :delete 400 "ee/workspace-instance/current")]
+ (is (re-find #"config\.yml" (:message resp))))))))
+
+(deftest delete-current-rejects-when-env-var-set-test
+ (testing "DELETE /ee/workspace-instance/current returns 400 when MB_INSTANCE_WORKSPACE is set"
+ (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]
+ (let [resp (mt/user-http-request :crowberto :delete 400 "ee/workspace-instance/current")]
+ (is (re-find #"config\.yml" (:message resp)))))))
+
+(deftest post-current-rejects-non-superuser-before-lock-test
+ (testing "POST /ee/workspace-instance/current returns 403 (not 400) for a non-superuser even when locked"
+ (ws.tu/with-workspace-locked-by-config
+ (fn []
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :post 403 "ee/workspace-instance/current"
+ {:name "x" :databases {}})))))))
+
+(deftest delete-current-rejects-non-superuser-before-lock-test
+ (testing "DELETE /ee/workspace-instance/current returns 403 (not 400) for a non-superuser even when locked"
+ (ws.tu/with-workspace-locked-by-config
+ (fn []
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :delete 403 "ee/workspace-instance/current")))))))
+
(deftest delete-current-superuser-only-test
(testing "DELETE /ee/workspace-instance/current requires data analyst or superuser"
(is (= "You don't have permissions to do that."
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
index 2d9f21cd0cd..c553703b4f4 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
@@ -155,3 +155,64 @@
:to_table_name "orders"})
(let [remappings (ws/list-remappings)]
(is (some #(= "orders" (:from_table_name %)) remappings)))))
+
+;;; ----------------------------------------- workspace-instance lock ------------------------------------------
+
+(defn- lock-atom
+ "Reach through the private var to the lock atom."
+ []
+ @#'ws/locked-by-config?*)
+
+(deftest workspace-locked-by-config?-baseline-test
+ (testing "default state (no atom flip, no env-var) is unlocked"
+ (let [a (lock-atom)
+ prior @a]
+ (try
+ (reset! a false)
+ (is (false? (ws/workspace-locked-by-config?)))
+ (finally (reset! a prior))))))
+
+(deftest workspace-locked-by-config?-atom-flipped-test
+ (testing "after mark-locked-by-config!, the predicate returns true"
+ (workspaces.tu/with-workspace-locked-by-config
+ (fn [] (is (true? (ws/workspace-locked-by-config?)))))))
+
+(deftest mark-locked-by-config!-flips-the-atom-test
+ (testing "mark-locked-by-config! flips the atom to true"
+ (let [a (lock-atom)
+ prior @a]
+ (try
+ (reset! a false)
+ (is (false? (ws/workspace-locked-by-config?)))
+ (ws/mark-locked-by-config!)
+ (is (true? @a))
+ (is (true? (ws/workspace-locked-by-config?)))
+ (finally (reset! a prior))))))
+
+(deftest env-var-presence-locks-test
+ (testing "MB_INSTANCE_WORKSPACE set => predicate true regardless of atom"
+ (let [a (lock-atom)
+ prior @a]
+ (try
+ (reset! a false)
+ (mt/with-temp-env-var-value! [mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]
+ (is (true? (ws/workspace-locked-by-config?))))
+ (testing "outside the binding, atom alone determines the lock"
+ (is (false? (ws/workspace-locked-by-config?))))
+ (finally (reset! a prior))))))
+
+(deftest env-var-empty-string-does-not-lock-test
+ (testing "MB_INSTANCE_WORKSPACE set to empty string does NOT lock (env-var-value treats blank as unset)"
+ (let [a (lock-atom)
+ prior @a]
+ (try
+ (reset! a false)
+ (mt/with-temp-env-var-value! [mb-instance-workspace ""]
+ (is (false? (ws/workspace-locked-by-config?))))
+ (finally (reset! a prior))))))
+
+(deftest env-var-and-atom-both-lock-test
+ (testing "both sources set => still true; OR semantics"
+ (mt/with-temp-env-var-value! [mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]
+ (workspaces.tu/with-workspace-locked-by-config
+ (fn [] (is (true? (ws/workspace-locked-by-config?))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj
index 6a2ea057866..b126ab64c5f 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj
@@ -2,19 +2,15 @@
(:require
[clojure.test :refer [deftest is testing use-fixtures]]
[metabase-enterprise.workspaces.core :as ws]
- [metabase.test :as mt]
[metabase.test.fixtures :as fixtures]))
(use-fixtures :once (fixtures/initialize :db))
-;; All tests in this ns exercise set/clear-instance-workspace!, which are gated on :workspaces
-;; (see GHY-3685). Enable the feature for every test in this ns.
(use-fixtures :each (fn [thunk]
- (mt/with-premium-features #{:workspaces}
- (try
- (thunk)
- (finally
- (ws/clear-instance-workspace!))))))
+ (try
+ (thunk)
+ (finally
+ (ws/clear-instance-workspace!)))))
(deftest workspace-mode?-false-when-setting-empty-test
(testing "workspace-mode? is false when no :workspace section has been loaded"
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj
index e6a5fcaad5d..7245f8d03f6 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj
@@ -1,6 +1,7 @@
(ns metabase-enterprise.workspaces.test-util
"Shared test helpers for the workspaces module."
(:require
+ [metabase-enterprise.workspaces.core :as ws]
[metabase.driver :as driver]))
(defn driver-loadable?
@@ -13,3 +14,14 @@
[engine]
(try (driver/the-initialized-driver engine) true
(catch Throwable _ false)))
+
+(defn with-workspace-locked-by-config
+ "Flip the workspace-instance lock for the duration of `thunk`, restoring the
+ prior value on exit. Reaches into the private atom via `#'` so the ws/core
+ surface stays clean of test-only setters."
+ [thunk]
+ (let [a @#'ws/locked-by-config?*
+ prior @a]
+ (reset! a true)
+ (try (thunk)
+ (finally (reset! a prior)))))
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx
index e47a970bafa..13994c4d518 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx
@@ -93,8 +93,7 @@ function WorkspaceInstancePageBody({
remappings={remappings}
/>
))}
- {/* TODO: restore `workspace.can_write && ` gate once the backend exposes it */}
- <DeleteSection />
+ {workspace.can_write && <DeleteSection />}
</Stack>
</PageContainer>
);
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx
index 349755884e8..4f1e0970c48 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx
@@ -86,4 +86,24 @@ describe("WorkspaceInstancePage", () => {
await screen.findByTestId("workspace-instance-delete-section"),
).toBeInTheDocument();
});
+
+ it("hides the delete section when can_write is false", async () => {
+ setup({
+ workspace: createMockWorkspaceInstance({
+ name: "Dev workspace",
+ databases: {
+ [POSTGRES.id]: createMockWorkspaceInstanceDatabase({
+ input_schemas: ["public"],
+ output: { schema: "ws_dev" },
+ }),
+ },
+ can_write: false,
+ }),
+ });
+
+ expect(await screen.findByText("Dev workspace")).toBeInTheDocument();
+ expect(
+ screen.queryByTestId("workspace-instance-delete-section"),
+ ).not.toBeInTheDocument();
+ });
});
diff --git a/frontend/src/metabase-types/api/mocks/workspace-instance.ts b/frontend/src/metabase-types/api/mocks/workspace-instance.ts
index 07d2cd1f04b..f17fb42d9d0 100644
--- a/frontend/src/metabase-types/api/mocks/workspace-instance.ts
+++ b/frontend/src/metabase-types/api/mocks/workspace-instance.ts
@@ -20,6 +20,9 @@ export function createMockWorkspaceInstance(
return {
name: "Test workspace",
databases: {},
+ // Default to a non-writable workspace so tests that don't care about
+ // lock state behave like a deployment-managed instance. Opt into the
+ // editable case with createMockWorkspaceInstance({ can_write: true }).
can_write: false,
...opts,
};