The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Add `writable-connection` feature flag (#70287)
Alexander Polyankin420c4e27e011
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex bd37fbb390b..d74ed69bf68 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2531,7 +2531,12 @@transformstransforms-baseutil- enterprise/transforms-python}}}+ enterprise/transforms-python}}++ enterprise/writable-connection+ {:team "Querying"+ :api #{}+ :uses #{premium-features}}};; namespaces matching these patterns (with `re-find`) are excluded from the module linter. Since regex literals;; aren't allowed in EDN just used the `(str <regex>)` version i.e. two slashes instead of one.diff --git a/enterprise/backend/src/metabase_enterprise/writable_connection/core.clj b/enterprise/backend/src/metabase_enterprise/writable_connection/core.cljnew file mode 100644index 00000000000..6996d8cce0f--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/writable_connection/core.clj@@ -0,0 +1,11 @@+(ns metabase-enterprise.writable-connection.core+ (:require+ [metabase.premium-features.core :refer [defenterprise]]))++(defenterprise database-write-data-details+ "Returns the `:write-data-details` for a database when the `:writable-connection` feature is available.++ `database` is a [[lib/metadata]] database instance. To pass a Toucan2 database instance, use [[driver.u/ensure-lib-database]]."+ :feature :writable-connection+ [database]+ (:write-data-details database))diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.cljindex ac6a91e6b83..2f069f3c172 100644--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj@@ -58,7 +58,8 @@:database-routing:tenants:cloud-custom-smtp- :workspaces}+ :workspaces+ :writable-connection}(is (= {:advanced_permissions true:attached_dwh true:audit_app true@@ -111,5 +112,6 @@:etl_connections false:etl_connections_pg false:dependencies false- :workspaces true}+ :workspaces true+ :writable_connection true}(:token-features (mt/user-http-request :crowberto :get 200 "session/properties"))))))diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.cljindex 994460f7041..a51ba0cfd13 100644--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj@@ -7,6 +7,7 @@[metabase-enterprise.impersonation.driver :as impersonation.driver][metabase-enterprise.impersonation.util-test :as impersonation.util-test][metabase-enterprise.test :as met]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver.connection :as driver.conn][metabase.driver.settings :as driver.settings]@@ -306,50 +307,51 @@;; impersonation role, and the original superuser (for write) who can SET ROLE to anything.;; If the write path mistakenly used the read pool, the SET ROLE would fail.(deftest conn-impersonation-with-write-connection-test-2- (testing "Write connection is actually used: read-only user cannot SET ROLE, but write user can"- (mt/test-driver :postgres- (mt/with-premium-features #{:advanced-permissions}- (let [details (:details (mt/db))- password (:password details)- readonly-user (u/lower-case-en (mt/random-name))- spec (sql-jdbc.conn/db->pooled-connection-spec (mt/id))- venues-table (sql.tx/qualify-and-quote :postgres "test-data" "venues")- role-name (u/lower-case-en (mt/random-name))]- (try- (driver/execute-raw-queries! :postgres spec- [[(format "CREATE ROLE %s WITH LOGIN PASSWORD '%s'" readonly-user password)]- [(format "GRANT USAGE ON SCHEMA public TO %s" readonly-user)]- [(format "GRANT SELECT ON ALL TABLES IN SCHEMA public TO %s" readonly-user)]])- ;; The impersonation role is granted to (:user details) — the superuser — not readonly-user.- (tx/with-temp-roles! :postgres- details- {role-name {venues-table {}}}- (:user details)- nil- (let [readonly-details (assoc details :user readonly-user)- write-details {:user (:user details)- :password password}]- (mt/with-temp [:model/Database database {:engine :postgres- :details readonly-details- :write_data_details write-details}]- (mt/with-db database- (sync/sync-database! database {:scan :schema})- (impersonation.util-test/with-impersonations!- {:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]- :attributes {"impersonation_attr" role-name}}- (testing "Negative: read-only user fails because it cannot assume the impersonation role"- (is (thrown?- java.lang.Exception- (mt/run-mbql-query venues {:aggregation [[:count]]}))))- (testing "Positive: write user succeeds because it can assume the impersonation role"- (driver.conn/with-write-connection- (is (= [[100]]- (mt/formatted-rows [int]- (mt/run-mbql-query venues {:aggregation [[:count]]})))))))))))- (finally+ (when config/ee-available?+ (testing "Write connection is actually used: read-only user cannot SET ROLE, but write user can"+ (mt/test-driver :postgres+ (mt/with-premium-features #{:advanced-permissions :writable-connection}+ (let [details (:details (mt/db))+ password (:password details)+ readonly-user (u/lower-case-en (mt/random-name))+ spec (sql-jdbc.conn/db->pooled-connection-spec (mt/id))+ venues-table (sql.tx/qualify-and-quote :postgres "test-data" "venues")+ role-name (u/lower-case-en (mt/random-name))]+ (try(driver/execute-raw-queries! :postgres spec- [[(format "DROP OWNED BY %s" readonly-user)]- [(format "DROP ROLE IF EXISTS %s" readonly-user)]]))))))))+ [[(format "CREATE ROLE %s WITH LOGIN PASSWORD '%s'" readonly-user password)]+ [(format "GRANT USAGE ON SCHEMA public TO %s" readonly-user)]+ [(format "GRANT SELECT ON ALL TABLES IN SCHEMA public TO %s" readonly-user)]])+ ;; The impersonation role is granted to (:user details) — the superuser — not readonly-user.+ (tx/with-temp-roles! :postgres+ details+ {role-name {venues-table {}}}+ (:user details)+ nil+ (let [readonly-details (assoc details :user readonly-user)+ write-details {:user (:user details)+ :password password}]+ (mt/with-temp [:model/Database database {:engine :postgres+ :details readonly-details+ :write_data_details write-details}]+ (mt/with-db database+ (sync/sync-database! database {:scan :schema})+ (impersonation.util-test/with-impersonations!+ {:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]+ :attributes {"impersonation_attr" role-name}}+ (testing "Negative: read-only user fails because it cannot assume the impersonation role"+ (is (thrown?+ java.lang.Exception+ (mt/run-mbql-query venues {:aggregation [[:count]]}))))+ (testing "Positive: write user succeeds because it can assume the impersonation role"+ (driver.conn/with-write-connection+ (is (= [[100]]+ (mt/formatted-rows [int]+ (mt/run-mbql-query venues {:aggregation [[:count]]})))))))))))+ (finally+ (driver/execute-raw-queries! :postgres spec+ [[(format "DROP OWNED BY %s" readonly-user)]+ [(format "DROP ROLE IF EXISTS %s" readonly-user)]])))))))))(deftest conn-impersonation-columns-test(mt/test-drivers (mt/normal-drivers-with-feature :test/column-impersonation)diff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/index.js b/enterprise/frontend/src/metabase-enterprise/advanced_permissions/index.jsindex bc7218fd797..cab3d9bbd54 100644--- a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/index.js+++ b/enterprise/frontend/src/metabase-enterprise/advanced_permissions/index.js@@ -21,13 +21,11 @@ import {import { hasPremiumFeature } from "metabase-enterprise/settings";import { ImpersonationModal } from "./components/ImpersonationModal";-import { WritableConnectionInfoSection } from "./components/WritableConnectionInfoSection";import {shouldRestrictNativeQueryPermissions,upgradeViewPermissionsIfNeeded,} from "./graph";import { advancedPermissionsSlice, getImpersonatedPostAction } from "./reducer";-import { getWritableConnectionInfoRoutes } from "./routes";import { getImpersonations } from "./selectors";const IMPERSONATED_PERMISSION_OPTION = {@@ -159,11 +157,6 @@ export function initializePlugin() {upgradeViewPermissionsIfNeeded;PLUGIN_DATA_PERMISSIONS.shouldRestrictNativeQueryPermissions =shouldRestrictNativeQueryPermissions;-- PLUGIN_ADVANCED_PERMISSIONS.getWritableConnectionInfoRoutes =- getWritableConnectionInfoRoutes;- PLUGIN_ADVANCED_PERMISSIONS.WritableConnectionInfoSection =- WritableConnectionInfoSection;}}diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.ts b/enterprise/frontend/src/metabase-enterprise/plugins.tsindex 767f723effe..7bef4d9779c 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.ts+++ b/enterprise/frontend/src/metabase-enterprise/plugins.ts@@ -50,6 +50,7 @@ import { initializePlugin as initializeTransformsPython } from "./transforms-pytimport { initializePlugin as initializeUploadManagement } from "./upload_management";import { initializePlugin as initializeUserProvisioning } from "./user_provisioning";import { initializePlugin as initializeWhitelabel } from "./whitelabel";+import { initializePlugin as initializeWritableConnection } from "./writable_connection";/*** Initialize all enterprise plugins that use hasPremiumFeature.@@ -97,4 +98,5 @@ export function initializePlugins() {initializeSupport();initializeTenants?.();initializeWorkspaces();+ initializeWritableConnection();}diff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsx b/enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsxsimilarity index 98%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsxrename to enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsxindex 5523d927df9..09d027aa1d7 100644--- a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsx+++ b/enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/WritableConnectionInfoSection.tsx@@ -13,7 +13,7 @@ import {import { useUpdateDatabaseMutation } from "metabase/api";import { useConfirmation } from "metabase/common/hooks/use-confirmation";import * as Urls from "metabase/lib/urls";-import type { WritableConnectionInfoSectionProps } from "metabase/plugins/oss/permissions";+import type { WritableConnectionInfoSectionProps } from "metabase/plugins/oss/writable-connection";import { Alert, Button, Group, Icon } from "metabase/ui";export function WritableConnectionInfoSection({diff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/WritableConnectionInfoSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/WritableConnectionInfoSection.unit.spec.tsxsimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/WritableConnectionInfoSection.unit.spec.tsxrename to enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/WritableConnectionInfoSection.unit.spec.tsxdiff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/index.ts b/enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/index.tssimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/components/WritableConnectionInfoSection/index.tsrename to enterprise/frontend/src/metabase-enterprise/writable_connection/components/WritableConnectionInfoSection/index.tsdiff --git a/enterprise/frontend/src/metabase-enterprise/writable_connection/index.ts b/enterprise/frontend/src/metabase-enterprise/writable_connection/index.tsnew file mode 100644index 00000000000..163e0186eb7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/writable_connection/index.ts@@ -0,0 +1,14 @@+import { PLUGIN_WRITABLE_CONNECTION } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { WritableConnectionInfoSection } from "./components/WritableConnectionInfoSection";+import { getWritableConnectionInfoRoutes } from "./routes";++export function initializePlugin() {+ if (hasPremiumFeature("writable_connection")) {+ PLUGIN_WRITABLE_CONNECTION.getWritableConnectionInfoRoutes =+ getWritableConnectionInfoRoutes;+ PLUGIN_WRITABLE_CONNECTION.WritableConnectionInfoSection =+ WritableConnectionInfoSection;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/pages/WritableConnectionInfoPage/WritableConnectionInfoPage.tsx b/enterprise/frontend/src/metabase-enterprise/writable_connection/pages/WritableConnectionInfoPage/WritableConnectionInfoPage.tsxsimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/pages/WritableConnectionInfoPage/WritableConnectionInfoPage.tsxrename to enterprise/frontend/src/metabase-enterprise/writable_connection/pages/WritableConnectionInfoPage/WritableConnectionInfoPage.tsxdiff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/pages/WritableConnectionInfoPage/index.ts b/enterprise/frontend/src/metabase-enterprise/writable_connection/pages/WritableConnectionInfoPage/index.tssimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/pages/WritableConnectionInfoPage/index.tsrename to enterprise/frontend/src/metabase-enterprise/writable_connection/pages/WritableConnectionInfoPage/index.tsdiff --git a/enterprise/frontend/src/metabase-enterprise/advanced_permissions/routes.tsx b/enterprise/frontend/src/metabase-enterprise/writable_connection/routes.tsxsimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/advanced_permissions/routes.tsxrename to enterprise/frontend/src/metabase-enterprise/writable_connection/routes.tsxdiff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.tsindex 0b56952765e..b9acf024364 100644--- a/frontend/src/metabase-types/api/mocks/settings.ts+++ b/frontend/src/metabase-types/api/mocks/settings.ts@@ -145,6 +145,7 @@ export const createMockTokenFeatures = ("support-users": false,tenants: false,workspaces: false,+ writable_connection: false,...opts,});diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 3a791ff644e..fa8d9d4593b 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -363,6 +363,7 @@ export const tokenFeatures = ["support-users","tenants","workspaces",+ "writable_connection",] as const;export type TokenFeature = (typeof tokenFeatures)[number];diff --git a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsxindex ec7f2c060a9..9f17dd155ce 100644--- a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx+++ b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx@@ -16,11 +16,11 @@ import CS from "metabase/css/core/index.css";import { usePageTitle } from "metabase/hooks/use-page-title";import { connect, useSelector } from "metabase/lib/redux";import {- PLUGIN_ADVANCED_PERMISSIONS,PLUGIN_DATABASE_REPLICATION,PLUGIN_DB_ROUTING,PLUGIN_TABLE_EDITING,PLUGIN_WORKSPACES,+ PLUGIN_WRITABLE_CONNECTION,} from "metabase/plugins";import { getUserIsAdmin } from "metabase/selectors/user";import { Box, Divider, Flex } from "metabase/ui";@@ -104,7 +104,7 @@ function DatabaseEditAppInner({><DatabaseConnectionInfoSection database={database} />- <PLUGIN_ADVANCED_PERMISSIONS.WritableConnectionInfoSection+ <PLUGIN_WRITABLE_CONNECTION.WritableConnectionInfoSectiondatabase={database}/>diff --git a/frontend/src/metabase/admin/routes.jsx b/frontend/src/metabase/admin/routes.jsxindex 9dda2dc7daa..3aa7e58d1bb 100644--- a/frontend/src/metabase/admin/routes.jsx+++ b/frontend/src/metabase/admin/routes.jsx@@ -42,12 +42,12 @@ import { DataModelV1 } from "metabase/metadata/pages/DataModelV1";import {PLUGIN_ADMIN_TOOLS,PLUGIN_ADMIN_USER_MENU_ROUTES,- PLUGIN_ADVANCED_PERMISSIONS,PLUGIN_CACHING,PLUGIN_DB_ROUTING,PLUGIN_DEPENDENCIES,PLUGIN_SUPPORT,PLUGIN_TENANTS,+ PLUGIN_WRITABLE_CONNECTION,} from "metabase/plugins";import { getTokenFeature } from "metabase/setup";@@ -84,7 +84,7 @@ export const getRoutes = (store, CanAccessSettings, IsAdmin) => {<Route path="create" component={DatabasePage} /></Route><Route path=":databaseId/edit" component={DatabasePage} />- {PLUGIN_ADVANCED_PERMISSIONS.getWritableConnectionInfoRoutes(IsAdmin)}+ {PLUGIN_WRITABLE_CONNECTION.getWritableConnectionInfoRoutes(IsAdmin)}<Route path=":databaseId" component={DatabaseEditApp}>{PLUGIN_DB_ROUTING.getDestinationDatabaseRoutes(IsAdmin)}</Route>diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.tsindex fb95eceec47..9900b2ba7e2 100644--- a/frontend/src/metabase/plugins/index.ts+++ b/frontend/src/metabase/plugins/index.ts@@ -142,6 +142,10 @@ export {} from "./oss/transforms";export { PLUGIN_UPLOAD_MANAGEMENT } from "./oss/upload-management";export { PLUGIN_WHITELABEL } from "./oss/whitelabel";+export {+ PLUGIN_WRITABLE_CONNECTION,+ type WritableConnectionInfoSectionProps,+} from "./oss/writable-connection";export { PLUGIN_SUPPORT } from "./oss/support";export { PLUGIN_TENANTS } from "./oss/tenants";@@ -187,6 +191,7 @@ import { reinitialize as reinitializeTenants } from "./oss/tenants";import { reinitialize as reinitializeTransforms } from "./oss/transforms";import { reinitialize as reinitializeUploadManagement } from "./oss/upload-management";import { reinitialize as reinitializeWhitelabel } from "./oss/whitelabel";+import { reinitialize as reinitializeWritableConnection } from "./oss/writable-connection";/*** Mostly for test purposes, reinitialize all plugins.@@ -227,4 +232,5 @@ export function reinitialize() {reinitializeTransforms();reinitializeUploadManagement();reinitializeWhitelabel();+ reinitializeWritableConnection();}diff --git a/frontend/src/metabase/plugins/oss/permissions.ts b/frontend/src/metabase/plugins/oss/permissions.tsindex 52d258d18ab..91abbcc24bf 100644--- a/frontend/src/metabase/plugins/oss/permissions.ts+++ b/frontend/src/metabase/plugins/oss/permissions.ts@@ -1,4 +1,4 @@-import type { ComponentType, ReactNode } from "react";+import type { ReactNode } from "react";import {type DataPermission,@@ -11,7 +11,6 @@ import {import { getUserIsAdmin } from "metabase/selectors/user";import type Database from "metabase-lib/v1/metadata/Database";import type {- Database as DatabaseType,Dataset,Group,GroupPermissions,@@ -20,7 +19,6 @@ import type {} from "metabase-types/api";import type { State } from "metabase-types/store";-import { PluginPlaceholder } from "../components/PluginPlaceholder";import type { PluginGroupManagersType } from "../types";const getDefaultAdminPermissionsDatabaseRoutes = () => [];@@ -114,10 +112,6 @@ const getDefaultAdminUserMenuRoutes = (): (() => React.ReactNode)[] => [];export const PLUGIN_ADMIN_USER_MENU_ITEMS = getDefaultAdminUserMenuItems();export const PLUGIN_ADMIN_USER_MENU_ROUTES = getDefaultAdminUserMenuRoutes();-export type WritableConnectionInfoSectionProps = {- database: DatabaseType;-};-const getDefaultAdvancedPermissions = () => ({addDatabasePermissionOptions: (permissions: any[], _database: Database) =>permissions,@@ -133,9 +127,6 @@ const getDefaultAdvancedPermissions = () => ({isRestrictivePermission: (_value: string) => false,shouldShowViewDataColumn: false,defaultViewDataPermission: DataPermissionValue.UNRESTRICTED,- getWritableConnectionInfoRoutes: (_IsAdmin: ComponentType): ReactNode => null,- WritableConnectionInfoSection:- PluginPlaceholder<WritableConnectionInfoSectionProps>,});export const PLUGIN_ADVANCED_PERMISSIONS = getDefaultAdvancedPermissions();diff --git a/frontend/src/metabase/plugins/oss/writable-connection.ts b/frontend/src/metabase/plugins/oss/writable-connection.tsnew file mode 100644index 00000000000..fc510df4020--- /dev/null+++ b/frontend/src/metabase/plugins/oss/writable-connection.ts@@ -0,0 +1,24 @@+import type { ComponentType, ReactNode } from "react";++import type { Database } from "metabase-types/api";++import { PluginPlaceholder } from "../components/PluginPlaceholder";++export type WritableConnectionInfoSectionProps = {+ database: Database;+};++const getDefaultWritableConnection = () => ({+ getWritableConnectionInfoRoutes: (_IsAdmin: ComponentType): ReactNode => null,+ WritableConnectionInfoSection:+ PluginPlaceholder<WritableConnectionInfoSectionProps>,+});++export const PLUGIN_WRITABLE_CONNECTION = getDefaultWritableConnection();++/**+ * @internal Do not call directly. Use the main reinitialize function from metabase/plugins instead.+ */+export function reinitialize() {+ Object.assign(PLUGIN_WRITABLE_CONNECTION, getDefaultWritableConnection());+}diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.cljindex 770c6469fa9..71f344e670f 100644--- a/src/metabase/analytics/stats.clj+++ b/src/metabase/analytics/stats.clj@@ -952,7 +952,10 @@:enabled (premium-features/enable-support-users?)}{:name :workspaces:available (premium-features/enable-workspaces?)- :enabled (premium-features/enable-workspaces?)}])+ :enabled (premium-features/enable-workspaces?)}+ {:name :writable-connection+ :available (premium-features/enable-writable-connection?)+ :enabled (premium-features/enable-writable-connection?)}])(defn- snowplow-features[]diff --git a/src/metabase/driver/connection.clj b/src/metabase/driver/connection.cljindex 022dccfef2e..73332d436ce 100644--- a/src/metabase/driver/connection.clj+++ b/src/metabase/driver/connection.clj@@ -12,6 +12,7 @@[metabase.analytics.prometheus :as prometheus][metabase.driver.connection.workspaces :as driver.w][metabase.driver.util :as driver.u]+ [metabase.premium-features.core :refer [defenterprise]][metabase.util :as u][metabase.util.log :as log][metabase.util.malli.registry :as mr]))@@ -91,6 +92,13 @@`(binding [*suppress-resolution-telemetry* true]~@body))+(defenterprise database-write-data-details+ "Returns the `:write-data-details` for a database, or `nil` if the writable-connection feature is not available.+ OSS implementation always returns `nil`."+ metabase-enterprise.writable-connection.core+ [_database]+ nil)+(defn effective-details"Returns the connection details map appropriate for the current context.@@ -103,7 +111,7 @@[database](when-let [database (some-> database driver.u/ensure-lib-database)](let [write? (= *connection-type* :write-data)- write-details (when write? (:write-data-details database))+ write-details (when write? (database-write-data-details database))base (merge (:details database) write-details)];; Track when write-data-details are genuinely used (not fallback, not workspace-swapped).;; Default resolutions are not tracked here — see :metabase-db-connection/write-op for@@ -127,7 +135,7 @@(let [database (driver.u/ensure-lib-database database)](case connection-type:default (:details database)- :write-data (:write-data-details database))))+ :write-data (database-write-data-details database))))(defn write-connection-requested?"True if currently executing within a [[with-write-connection]] scope."@@ -144,7 +152,7 @@pools) when the requested type would resolve identically to `:default`."[database](if (and (= *connection-type* :write-data)- (some? (:write-data-details (driver.u/ensure-lib-database database))))+ (some? (database-write-data-details (driver.u/ensure-lib-database database)))):write-data:default))diff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.cljindex 2aafef16477..9656cfa8a38 100644--- a/src/metabase/premium_features/core.clj+++ b/src/metabase/premium_features/core.clj@@ -85,6 +85,7 @@enable-upload-management?enable-whitelabeling?enable-workspaces?+ enable-writable-connection?has-attached-dwh?hide-embed-branding?is-hosted?diff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.cljindex 8672972595f..aa7a9423c65 100644--- a/src/metabase/premium_features/settings.clj+++ b/src/metabase/premium_features/settings.clj@@ -325,6 +325,10 @@"Should we allow users to use workspaces?":workspaces)+(define-premium-feature enable-writable-connection?+ "Should we allow admins to configure separate write connection credentials?"+ :writable-connection)+(defn- -token-features []{:advanced_permissions (enable-advanced-permissions?):ai_sql_fixer (enable-ai-sql-fixer?)@@ -378,7 +382,8 @@:transforms-python (enable-python-transforms?):upload_management (enable-upload-management?):whitelabel (enable-whitelabeling?)- :workspaces (enable-workspaces?)})+ :workspaces (enable-workspaces?)+ :writable_connection (enable-writable-connection?)})(defsetting token-features"Features registered for this instance's token"diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.cljindex 212f616498a..b4a89f63721 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -293,7 +293,7 @@(t2/update! :model/Database (:id database) {:provider_name provider})(catch Throwable provider-e(log/warnf provider-e "Error during provider detection for database {:id %d}" (:id database)))))))- (when (:write_data_details database)+ (when (driver.conn/database-write-data-details (driver.u/ensure-lib-database database))(let [write-details (driver.conn/without-resolution-telemetry(driver.conn/with-write-connection(driver.conn/effective-details database)))]diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 289eb5c3872..5ea75bd1ddb 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -1004,7 +1004,7 @@[:settings {:optional true} [:maybe ms/Map]]]];; TODO - ensure that custom schedules and let-user-control-scheduling go in lockstep(when (some? write_data_details)- (premium-features/assert-has-feature :advanced-permissions (tru "Advanced Permissions")))+ (premium-features/assert-has-feature :writable-connection (tru "Writable Connection")))(when (:write-data-connection details)(throw (ex-info (tru "write-data-connection must not be set in details"){:status-code 400})))diff --git a/test/metabase/driver/connection_test.clj b/test/metabase/driver/connection_test.cljindex 7ae1a2cc61d..c24c00ff064 100644--- a/test/metabase/driver/connection_test.clj+++ b/test/metabase/driver/connection_test.clj@@ -21,53 +21,70 @@(driver.conn/effective-details database))))))(deftest effective-details-write-with-write-details-test- (let [details {:host "read-host" :port 5432}- write-data-details {:host "write-host" :port 5432}]- (testing "effective-details returns write_data_details when :write-data and details exist"- (testing "with kebab-case key (MLv2 metadata style)"- (let [database {:lib/type :metadata/database- :details details- :write-data-details write-data-details}]- (driver.conn/with-write-connection- (is (=? write-data-details- (driver.conn/effective-details database))))))- (testing "with snake_case key (Toucan2 instance style)"- (let [database (t2/instance :model/Database- {:id 1- :details details- :write_data_details write-data-details})]- (driver.conn/with-write-connection- (is (=? write-data-details- (driver.conn/effective-details database)))))))))+ (mt/when-ee-evailable+ (mt/with-premium-features #{:writable-connection}+ (let [details {:host "read-host" :port 5432}+ write-data-details {:host "write-host" :port 5432}]+ (testing "effective-details returns write_data_details when :write-data and details exist"+ (testing "with kebab-case key (MLv2 metadata style)"+ (let [database {:lib/type :metadata/database+ :details details+ :write-data-details write-data-details}]+ (driver.conn/with-write-connection+ (is (=? write-data-details+ (driver.conn/effective-details database))))))+ (testing "with snake_case key (Toucan2 instance style)"+ (let [database (t2/instance :model/Database+ {:id 1+ :details details+ :write_data_details write-data-details})]+ (driver.conn/with-write-connection+ (is (=? write-data-details+ (driver.conn/effective-details database)))))))))))(deftest effective-details-write-fallback-test- (let [details {:host "read-host" :port 5432}]- (testing "effective-details falls back to :details when :write-data but no write details"- (let [database {:lib/type :metadata/database- :details details- :write-data-details nil}]- (driver.conn/with-write-connection- (is (=? details- (driver.conn/effective-details database)))))- (testing "also when :write_data_details key is missing entirely"+ (mt/with-premium-features #{:writable-connection}+ (let [details {:host "read-host" :port 5432}]+ (testing "effective-details falls back to :details when :write-data but no write details"(let [database {:lib/type :metadata/database- :details details}]+ :details details+ :write-data-details nil}](driver.conn/with-write-connection(is (=? details- (driver.conn/effective-details database)))))))))+ (driver.conn/effective-details database)))))+ (testing "also when :write_data_details key is missing entirely"+ (let [database {:lib/type :metadata/database+ :details details}]+ (driver.conn/with-write-connection+ (is (=? details+ (driver.conn/effective-details database))))))))))++(deftest effective-details-write-without-feature-test+ (testing "without :writable-connection feature, with-write-connection falls back to main connection details"+ (mt/with-premium-features #{}+ (let [details {:host "read-host" :port 5432}+ write-data-details {:host "write-host" :port 5432}+ database {:lib/type :metadata/database+ :details details+ :write-data-details write-data-details}]+ (driver.conn/with-write-connection+ (is (=? details+ (driver.conn/effective-details database))))))))(deftest effective-details-nil-test(testing "effective-details returns nil when database is nil"(is (nil? (driver.conn/effective-details nil)))- (driver.conn/with-write-connection- (is (nil? (driver.conn/effective-details nil))))))+ (mt/with-premium-features #{:writable-connection}+ (driver.conn/with-write-connection+ (is (nil? (driver.conn/effective-details nil)))))))(deftest write-connection-requested?-test(testing "write-connection-requested? returns false by default"(is (false? (driver.conn/write-connection-requested?))))(testing "write-connection-requested? returns true inside with-write-connection"- (driver.conn/with-write-connection- (is (true? (driver.conn/write-connection-requested?))))))+ (mt/with-premium-features #{:writable-connection}+ (driver.conn/with-write-connection+ (is (true? (driver.conn/write-connection-requested?)))))))(deftest effective-details-with-workspace-swap-test(testing "effective-details applies workspace swap in :default connection type"@@ -78,76 +95,80 @@(is (=? {:host "read-host" :user "ws-user" :password "ws-pass" :port 5432}(driver.conn/effective-details database))))))- (testing "effective-details applies workspace swap AFTER write-data merge"- (let [database {:lib/type :metadata/database- :id 1- :details {:host "host" :user "admin" :port 5432}- :write-data-details {:user "writer" :password "write-pass" :write true}}]- (driver.w/with-swapped-connection-details 1 {:user "ws-user" :password "ws-pass"}- (driver.conn/with-write-connection- (is (=? {:host "host" :user "ws-user" :password "ws-pass" :port 5432 :write true}- (driver.conn/effective-details database)))))))+ (mt/when-ee-evailable+ (mt/with-premium-features #{:writable-connection}+ (testing "effective-details applies workspace swap AFTER write-data merge"+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "host" :user "admin" :port 5432}+ :write-data-details {:user "writer" :password "write-pass" :write true}}]+ (driver.w/with-swapped-connection-details 1 {:user "ws-user" :password "ws-pass"}+ (driver.conn/with-write-connection+ (is (=? {:host "host" :user "ws-user" :password "ws-pass" :port 5432 :write true}+ (driver.conn/effective-details database)))))))- (testing "without workspace swap, effective-details is unchanged (regression)"- (let [database {:lib/type :metadata/database- :id 1- :details {:host "host" :user "admin"}- :write-data-details {:user "writer"}}]- (is (=? {:host "host" :user "admin"}- (driver.conn/effective-details database)))- (driver.conn/with-write-connection- (is (=? {:host "host" :user "writer"}- (driver.conn/effective-details database)))))))+ (testing "without workspace swap, effective-details is unchanged (regression)"+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "host" :user "admin"}+ :write-data-details {:user "writer"}}]+ (is (=? {:host "host" :user "admin"}+ (driver.conn/effective-details database)))+ (driver.conn/with-write-connection+ (is (=? {:host "host" :user "writer"}+ (driver.conn/effective-details database)))))))))(deftest type-resolved-metric-test- (testing "type-resolved counter increments when write-data-details are genuinely used"- (mt/with-prometheus-system! [_ system]- (let [database {:lib/type :metadata/database- :id 1- :details {:host "read-host" :port 5432}- :write-data-details {:host "write-host"}}]- (driver.conn/with-write-connection- (driver.conn/effective-details database))- (is (prometheus-test/approx= 1 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"}))))))- (testing "type-resolved counter does NOT increment on fallback (no write-data-details)"- (mt/with-prometheus-system! [_ system]- (let [database {:lib/type :metadata/database- :id 1- :details {:host "read-host" :port 5432}}]- (driver.conn/with-write-connection- (driver.conn/effective-details database))- (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"}))))))- (testing "type-resolved counter does NOT increment for default connection type"- (mt/with-prometheus-system! [_ system]- (let [database {:lib/type :metadata/database- :id 1- :details {:host "read-host" :port 5432}- :write-data-details {:host "write-host"}}]- (driver.conn/effective-details database)- (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"}))))))- (testing "type-resolved counter does NOT increment when workspace swap is active"- (mt/with-prometheus-system! [_ system]- (let [database {:lib/type :metadata/database- :id 1- :details {:host "read-host" :port 5432}- :write-data-details {:host "write-host"}}]- (driver.w/with-swapped-connection-details 1 {:user "ws-user"}- (driver.conn/with-write-connection- (driver.conn/effective-details database)))- (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"}))))))- (testing "type-resolved counter does NOT increment inside without-resolution-telemetry"- (mt/with-prometheus-system! [_ system]- (let [database {:lib/type :metadata/database- :id 1- :details {:host "read-host" :port 5432}- :write-data-details {:host "write-host"}}]- (driver.conn/without-resolution-telemetry- (driver.conn/with-write-connection- (is (=? {:host "write-host" :port 5432}- (driver.conn/effective-details database)))))- (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"})))))))+ (mt/when-ee-evailable+ (mt/with-premium-features #{:writable-connection}+ (testing "type-resolved counter increments when write-data-details are genuinely used"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :write-data-details {:host "write-host"}}]+ (driver.conn/with-write-connection+ (driver.conn/effective-details database))+ (is (prometheus-test/approx= 1 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "write-data"}))))))+ (testing "type-resolved counter does NOT increment on fallback (no write-data-details)"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}}]+ (driver.conn/with-write-connection+ (driver.conn/effective-details database))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "write-data"}))))))+ (testing "type-resolved counter does NOT increment for default connection type"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :write-data-details {:host "write-host"}}]+ (driver.conn/effective-details database)+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "write-data"}))))))+ (testing "type-resolved counter does NOT increment when workspace swap is active"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :write-data-details {:host "write-host"}}]+ (driver.w/with-swapped-connection-details 1 {:user "ws-user"}+ (driver.conn/with-write-connection+ (driver.conn/effective-details database)))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "write-data"}))))))+ (testing "type-resolved counter does NOT increment inside without-resolution-telemetry"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :write-data-details {:host "write-host"}}]+ (driver.conn/without-resolution-telemetry+ (driver.conn/with-write-connection+ (is (=? {:host "write-host" :port 5432}+ (driver.conn/effective-details database)))))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "write-data"})))))))))diff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.cljindex 838706fc1b5..353eb9a32b1 100644--- a/test/metabase/driver/sql_jdbc/connection_test.clj+++ b/test/metabase/driver/sql_jdbc/connection_test.clj@@ -98,51 +98,53 @@(deftest connection-type-pool-separation-test(mt/test-driver :h2- (testing "Different connection types get separate pools for the same database"- (let [read-details {:db "mem:read_pool_test"}- write-details {:db "mem:write_pool_test"}- spec (mdb/spec :h2 read-details)]- ;; Create an in-memory H2 db we can use for the test- (sql-jdbc.execute/do-with-connection-with-options- :h2- spec- {:write? true}- (fn [conn]- (next.jdbc/execute! conn ["CREATE TABLE IF NOT EXISTS test_tbl (id int)"])- ;; Use snake_case for column name since deftransforms uses snake_case keys- (mt/with-temp [:model/Database database {:engine :h2- :details read-details- :write_data_details write-details}]- (let [db-id (u/the-id database)- default-cache-key [db-id :default]- write-cache-key [db-id :write-data]]+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (testing "Different connection types get separate pools for the same database"+ (let [read-details {:db "mem:read_pool_test"}+ write-details {:db "mem:write_pool_test"}+ spec (mdb/spec :h2 read-details)]+ ;; Create an in-memory H2 db we can use for the test+ (sql-jdbc.execute/do-with-connection-with-options+ :h2+ spec+ {:write? true}+ (fn [conn]+ (next.jdbc/execute! conn ["CREATE TABLE IF NOT EXISTS test_tbl (id int)"])+ ;; Use snake_case for column name since deftransforms uses snake_case keys+ (mt/with-temp [:model/Database database {:engine :h2+ :details read-details+ :write_data_details write-details}]+ (let [db-id (u/the-id database)+ default-cache-key [db-id :default]+ write-cache-key [db-id :write-data]];; Ensure pools are cleared- (sql-jdbc.conn/invalidate-pool-for-db! database)-- (testing "initially no pools exist"- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)))- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))-- (testing "getting a default connection creates only the default pool"- (sql-jdbc.conn/db->pooled-connection-spec database)- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))-- (testing "getting a write connection creates a separate write pool"- (driver.conn/with-write-connection- (sql-jdbc.conn/db->pooled-connection-spec database))- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))-- (testing "the two pools are different objects"- (let [default-pool (get @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)- write-pool (get @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)]- (is (some? default-pool))- (is (some? write-pool))- (is (not (identical? default-pool write-pool)))))+ (sql-jdbc.conn/invalidate-pool-for-db! database)++ (testing "initially no pools exist"+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))++ (testing "getting a default connection creates only the default pool"+ (sql-jdbc.conn/db->pooled-connection-spec database)+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))++ (testing "getting a write connection creates a separate write pool"+ (driver.conn/with-write-connection+ (sql-jdbc.conn/db->pooled-connection-spec database))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))++ (testing "the two pools are different objects"+ (let [default-pool (get @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)+ write-pool (get @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)]+ (is (some? default-pool))+ (is (some? write-pool))+ (is (not (identical? default-pool write-pool)))));; Cleanup- (sql-jdbc.conn/invalidate-pool-for-db! database)))))))))+ (sql-jdbc.conn/invalidate-pool-for-db! database)))))))))))(deftest write-connection-reuses-default-pool-when-unconfigured-test(mt/test-driver :h2@@ -168,66 +170,70 @@(deftest write-pool-uses-write-details-test(mt/test-driver :h2- (testing "Write connection pool uses :write-data-details when available"- (let [read-details {:db "mem:read_details_db"}- write-details {:db "mem:write_details_db"}]- ;; Use snake_case for column name since deftransforms uses snake_case keys- (mt/with-temp [:model/Database database {:engine :h2- :details read-details- :write_data_details write-details}]- (let [db-id (u/the-id database)]- ;; Ensure pools are cleared- (sql-jdbc.conn/invalidate-pool-for-db! database)-- (testing "jdbc-spec-hash differs between default and write connection types"- (let [default-hash (#'sql-jdbc.conn/jdbc-spec-hash database)- write-hash (driver.conn/with-write-connection- (#'sql-jdbc.conn/jdbc-spec-hash database))]- (is (integer? default-hash))- (is (integer? write-hash))- (is (not= default-hash write-hash)- "Hash should differ because effective-details returns different details")))-- (testing "hash cache uses composite keys"- ;; Get both pools- (sql-jdbc.conn/db->pooled-connection-spec database)- (driver.conn/with-write-connection- (sql-jdbc.conn/db->pooled-connection-spec database))-- (let [default-cached-hash (get @@#'sql-jdbc.conn/pool-cache-key->jdbc-spec-hash [db-id :default])- write-cached-hash (get @@#'sql-jdbc.conn/pool-cache-key->jdbc-spec-hash [db-id :write-data])]- (is (some? default-cached-hash))- (is (some? write-cached-hash))- (is (not= default-cached-hash write-cached-hash))))-- ;; Cleanup- (sql-jdbc.conn/invalidate-pool-for-db! database)))))))+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (testing "Write connection pool uses :write-data-details when available"+ (let [read-details {:db "mem:read_details_db"}+ write-details {:db "mem:write_details_db"}]+ ;; Use snake_case for column name since deftransforms uses snake_case keys+ (mt/with-temp [:model/Database database {:engine :h2+ :details read-details+ :write_data_details write-details}]+ (let [db-id (u/the-id database)]+ ;; Ensure pools are cleared+ (sql-jdbc.conn/invalidate-pool-for-db! database)++ (testing "jdbc-spec-hash differs between default and write connection types"+ (let [default-hash (#'sql-jdbc.conn/jdbc-spec-hash database)+ write-hash (driver.conn/with-write-connection+ (#'sql-jdbc.conn/jdbc-spec-hash database))]+ (is (integer? default-hash))+ (is (integer? write-hash))+ (is (not= default-hash write-hash)+ "Hash should differ because effective-details returns different details")))++ (testing "hash cache uses composite keys"+ ;; Get both pools+ (sql-jdbc.conn/db->pooled-connection-spec database)+ (driver.conn/with-write-connection+ (sql-jdbc.conn/db->pooled-connection-spec database))++ (let [default-cached-hash (get @@#'sql-jdbc.conn/pool-cache-key->jdbc-spec-hash [db-id :default])+ write-cached-hash (get @@#'sql-jdbc.conn/pool-cache-key->jdbc-spec-hash [db-id :write-data])]+ (is (some? default-cached-hash))+ (is (some? write-cached-hash))+ (is (not= default-cached-hash write-cached-hash))))++ ;; Cleanup+ (sql-jdbc.conn/invalidate-pool-for-db! database)))))))))(deftest invalidate-pool-clears-both-connection-types-test(mt/test-driver :h2- (testing "invalidate-pool-for-db! clears both default and write pools"- (let [read-details {:db "mem:invalidate_test"}- write-details {:db "mem:invalidate_write_test"}]- ;; Use snake_case for column name since deftransforms uses snake_case keys- (mt/with-temp [:model/Database database {:engine :h2- :details read-details- :write_data_details write-details}]- (let [db-id (u/the-id database)- default-cache-key [db-id :default]- write-cache-key [db-id :write-data]]- ;; Create both pools- (sql-jdbc.conn/db->pooled-connection-spec database)- (driver.conn/with-write-connection- (sql-jdbc.conn/db->pooled-connection-spec database))-- (testing "both pools exist before invalidation"- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))-- (testing "invalidate-pool-for-db! removes both pools"- (sql-jdbc.conn/invalidate-pool-for-db! database)- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)))- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))))))+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (testing "invalidate-pool-for-db! clears both default and write pools"+ (let [read-details {:db "mem:invalidate_test"}+ write-details {:db "mem:invalidate_write_test"}]+ ;; Use snake_case for column name since deftransforms uses snake_case keys+ (mt/with-temp [:model/Database database {:engine :h2+ :details read-details+ :write_data_details write-details}]+ (let [db-id (u/the-id database)+ default-cache-key [db-id :default]+ write-cache-key [db-id :write-data]]+ ;; Create both pools+ (sql-jdbc.conn/db->pooled-connection-spec database)+ (driver.conn/with-write-connection+ (sql-jdbc.conn/db->pooled-connection-spec database))++ (testing "both pools exist before invalidation"+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))++ (testing "invalidate-pool-for-db! removes both pools"+ (sql-jdbc.conn/invalidate-pool-for-db! database)+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))))))))(deftest ^:parallel c3p0-datasource-name-test(mt/test-drivers (mt/driver-select {:+parent :sql-jdbc})diff --git a/test/metabase/driver/sql_jdbc/execute_test.clj b/test/metabase/driver/sql_jdbc/execute_test.cljindex a930f894a04..e8315d1d594 100644--- a/test/metabase/driver/sql_jdbc/execute_test.clj+++ b/test/metabase/driver/sql_jdbc/execute_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer :all][malli.error :as me]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver.connection :as driver.conn][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]@@ -200,14 +201,16 @@(fn [_conn] nil))(is (pos? (mt/metric-value system :metabase-db-connection/write-op{:connection-type "default"}))))))- (testing "write-op counter tracks write-data connection acquisitions"- (mt/with-prometheus-system! [_ system]- (mt/test-drivers (descendants driver/hierarchy :sql-jdbc)- (let [db (mt/db)]- (mt/with-temp-vals-in-db :model/Database (:id db) {:write_data_details (:details db)}- (driver.conn/with-write-connection- (sql-jdbc.execute/do-with-connection-with-options- driver/*driver* (mt/id) nil- (fn [_conn] nil)))- (is (pos? (mt/metric-value system :metabase-db-connection/write-op- {:connection-type "write-data"})))))))))+ (when config/ee-available?+ (testing "write-op counter tracks write-data connection acquisitions"+ (mt/with-premium-features #{:writable-connection}+ (mt/with-prometheus-system! [_ system]+ (mt/test-drivers (descendants driver/hierarchy :sql-jdbc)+ (let [db (mt/db)]+ (mt/with-temp-vals-in-db :model/Database (:id db) {:write_data_details (:details db)}+ (driver.conn/with-write-connection+ (sql-jdbc.execute/do-with-connection-with-options+ driver/*driver* (mt/id) nil+ (fn [_conn] nil)))+ (is (pos? (mt/metric-value system :metabase-db-connection/write-op+ {:connection-type "write-data"})))))))))))diff --git a/test/metabase/transforms/execute_test.clj b/test/metabase/transforms/execute_test.cljindex 7026ab93ed0..98d8ee5e94c 100644--- a/test/metabase/transforms/execute_test.clj+++ b/test/metabase/transforms/execute_test.clj@@ -2,6 +2,7 @@(:require[clojure.string :as str][clojure.test :refer :all]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.lib.core :as lib]@@ -432,106 +433,110 @@;; TODO(Timothy, 02-16-26):;; Like no-create-schema-permissions-test, this would be improved by being driver agnostic(deftest write-connection-used-for-transforms-test- (mt/test-driver :postgres- (mt/dataset transforms-dataset/transforms-test- (let [details (:details (mt/db))- password (:password details)- readonly-user (u/lower-case-en (mt/random-name))- spec (sql-jdbc.conn/db->pooled-connection-spec (mt/id))- schema (t2/select-one-fn :schema :model/Table (mt/id :transforms_products))- make-widget-query- (fn [mp]- (let [transforms-products (lib.metadata/table mp (mt/id :transforms_products))- products-category (lib.metadata/field mp (mt/id :transforms_products :category))- products-id (lib.metadata/field mp (mt/id :transforms_products :id))]- (-> (lib/query mp transforms-products)- (lib/filter (lib/= products-category "Widget"))- (lib/order-by products-id :asc))))]- (try- (driver/execute-raw-queries! driver/*driver* spec- [[(format "CREATE ROLE %s WITH LOGIN PASSWORD '%s';" readonly-user password)]- [(format "GRANT USAGE ON SCHEMA %s TO %s;" schema readonly-user)]- [(format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s;" schema readonly-user)]- ;; Grant SELECT on future tables created by the write user,- ;; so the readonly user can query transform output tables.- [(format "ALTER DEFAULT PRIVILEGES FOR ROLE %s IN SCHEMA %s GRANT SELECT ON TABLES TO %s;"- (:user details) schema readonly-user)]- ;; PG < 15 grants CREATE on `public` to PUBLIC by default.- ;; Revoke it so the readonly user truly cannot create tables. :thinking_face:- [(format "REVOKE CREATE ON SCHEMA %s FROM PUBLIC;" schema)]])- (let [readonly-details (assoc details :user readonly-user)- write-details {:user (:user details)- :password (:password details)}- run-transform!- (fn [db-opts target-name]- (mt/with-temp [:model/Database db (merge {:engine :postgres} db-opts)]- (mt/with-db db- (sync/sync-database! db {:scan :schema})- (let [query (make-widget-query (mt/metadata-provider))]- (with-transform-cleanup! [target-table {:type "table"- :schema schema- :name target-name}]- (mt/with-temp [:model/Transform transform {:name target-name- :source {:type :query- :query query}- :target target-table}]- (transforms.execute/execute! transform {:run-method :manual})- (let [_ (transforms.tu/wait-for-table (:name target-table) 10000)- mp (mt/metadata-provider)- table-result (lib.metadata/table mp (mt/id (keyword (:name target-table))))]- (->> (lib/query mp table-result)- (qp/process-query)- (mt/formatted-rows [int str str 2.0 str])- (sort-by first <)))))))))]- (testing "Negative: read-only user without write_data_details fails to run transform"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"permission denied"- (run-transform! {:details readonly-details} "write_conn_neg"))))- (testing "Positive: read-only user with write_data_details succeeds"- (is (= [[1 "Widget A" "Widget" 19.99 "2024-01-01T10:00:00Z"]- [7 "Widget B" "Widget" 24.99 "2024-01-07T10:00:00Z"]- [9 "Widget C" "Widget" 14.99 "2024-01-09T10:00:00Z"]- [10 "Widget D" "Widget" 34.99 "2024-01-10T10:00:00Z"]- [15 "Widget E" "Widget" 44.99 "2024-01-15T10:00:00Z"]]- (run-transform! {:details readonly-details- :write_data_details write-details}- "write_conn_pos")))))- (finally- (driver/execute-raw-queries! driver/*driver* spec- [;; [(format "GRANT CREATE ON SCHEMA %s TO PUBLIC;" schema)]- [(format "DROP OWNED BY %s;" readonly-user)]- [(format "DROP ROLE IF EXISTS %s;" readonly-user)]])))))))+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (mt/test-driver :postgres+ (mt/dataset transforms-dataset/transforms-test+ (let [details (:details (mt/db))+ password (:password details)+ readonly-user (u/lower-case-en (mt/random-name))+ spec (sql-jdbc.conn/db->pooled-connection-spec (mt/id))+ schema (t2/select-one-fn :schema :model/Table (mt/id :transforms_products))+ make-widget-query+ (fn [mp]+ (let [transforms-products (lib.metadata/table mp (mt/id :transforms_products))+ products-category (lib.metadata/field mp (mt/id :transforms_products :category))+ products-id (lib.metadata/field mp (mt/id :transforms_products :id))]+ (-> (lib/query mp transforms-products)+ (lib/filter (lib/= products-category "Widget"))+ (lib/order-by products-id :asc))))]+ (try+ (driver/execute-raw-queries! driver/*driver* spec+ [[(format "CREATE ROLE %s WITH LOGIN PASSWORD '%s';" readonly-user password)]+ [(format "GRANT USAGE ON SCHEMA %s TO %s;" schema readonly-user)]+ [(format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s;" schema readonly-user)]+ ;; Grant SELECT on future tables created by the write user,+ ;; so the readonly user can query transform output tables.+ [(format "ALTER DEFAULT PRIVILEGES FOR ROLE %s IN SCHEMA %s GRANT SELECT ON TABLES TO %s;"+ (:user details) schema readonly-user)]+ ;; PG < 15 grants CREATE on `public` to PUBLIC by default.+ ;; Revoke it so the readonly user truly cannot create tables. :thinking_face:+ [(format "REVOKE CREATE ON SCHEMA %s FROM PUBLIC;" schema)]])+ (let [readonly-details (assoc details :user readonly-user)+ write-details {:user (:user details)+ :password (:password details)}+ run-transform!+ (fn [db-opts target-name]+ (mt/with-temp [:model/Database db (merge {:engine :postgres} db-opts)]+ (mt/with-db db+ (sync/sync-database! db {:scan :schema})+ (let [query (make-widget-query (mt/metadata-provider))]+ (with-transform-cleanup! [target-table {:type "table"+ :schema schema+ :name target-name}]+ (mt/with-temp [:model/Transform transform {:name target-name+ :source {:type :query+ :query query}+ :target target-table}]+ (transforms.execute/execute! transform {:run-method :manual})+ (let [_ (transforms.tu/wait-for-table (:name target-table) 10000)+ mp (mt/metadata-provider)+ table-result (lib.metadata/table mp (mt/id (keyword (:name target-table))))]+ (->> (lib/query mp table-result)+ (qp/process-query)+ (mt/formatted-rows [int str str 2.0 str])+ (sort-by first <)))))))))]+ (testing "Negative: read-only user without write_data_details fails to run transform"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"permission denied"+ (run-transform! {:details readonly-details} "write_conn_neg"))))+ (testing "Positive: read-only user with write_data_details succeeds"+ (is (= [[1 "Widget A" "Widget" 19.99 "2024-01-01T10:00:00Z"]+ [7 "Widget B" "Widget" 24.99 "2024-01-07T10:00:00Z"]+ [9 "Widget C" "Widget" 14.99 "2024-01-09T10:00:00Z"]+ [10 "Widget D" "Widget" 34.99 "2024-01-10T10:00:00Z"]+ [15 "Widget E" "Widget" 44.99 "2024-01-15T10:00:00Z"]]+ (run-transform! {:details readonly-details+ :write_data_details write-details}+ "write_conn_pos")))))+ (finally+ (driver/execute-raw-queries! driver/*driver* spec+ [;; [(format "GRANT CREATE ON SCHEMA %s TO PUBLIC;" schema)]+ [(format "DROP OWNED BY %s;" readonly-user)]+ [(format "DROP ROLE IF EXISTS %s;" readonly-user)]])))))))))(deftest transform-creates-write-pool-test(mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc, :+features [:transforms/table]})- (mt/dataset transforms-dataset/transforms-test- (let [db-id (mt/id)- write-cache-key [db-id :write-data]- schema (t2/select-one-fn :schema :model/Table (mt/id :transforms_products))- old-write-details (:write_data_details (mt/db))]- (when-not old-write-details- (t2/update! :model/Database db-id {:write_data_details (:details (mt/db))}))- (try- (sql-jdbc.conn/invalidate-pool-for-db! (mt/db))- (testing "write pool does not exist before transform execution"- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))- (with-transform-cleanup! [target-table {:type "table"- :schema schema- :name "pool_test"}]- (let [mp (mt/metadata-provider)- transforms-products (lib.metadata/table mp (mt/id :transforms_products))- products-category (lib.metadata/field mp (mt/id :transforms_products :category))- query (-> (lib/query mp transforms-products)- (lib/filter (lib/= products-category "Widget")))]- (mt/with-temp [:model/Transform transform {:name "pool-test"- :source {:type :query- :query query}- :target target-table}]- (transforms.execute/execute! transform {:run-method :manual})- (transforms.tu/wait-for-table (:name target-table) 10000)- (testing "write pool is created during transform execution"- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))- (finally- (t2/update! :model/Database db-id {:write_data_details old-write-details})- (sql-jdbc.conn/invalidate-pool-for-db! (mt/db))))))))+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (mt/dataset transforms-dataset/transforms-test+ (let [db-id (mt/id)+ write-cache-key [db-id :write-data]+ schema (t2/select-one-fn :schema :model/Table (mt/id :transforms_products))+ old-write-details (:write_data_details (mt/db))]+ (when-not old-write-details+ (t2/update! :model/Database db-id {:write_data_details (:details (mt/db))}))+ (try+ (sql-jdbc.conn/invalidate-pool-for-db! (mt/db))+ (testing "write pool does not exist before transform execution"+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))+ (with-transform-cleanup! [target-table {:type "table"+ :schema schema+ :name "pool_test"}]+ (let [mp (mt/metadata-provider)+ transforms-products (lib.metadata/table mp (mt/id :transforms_products))+ products-category (lib.metadata/field mp (mt/id :transforms_products :category))+ query (-> (lib/query mp transforms-products)+ (lib/filter (lib/= products-category "Widget")))]+ (mt/with-temp [:model/Transform transform {:name "pool-test"+ :source {:type :query+ :query query}+ :target target-table}]+ (transforms.execute/execute! transform {:run-method :manual})+ (transforms.tu/wait-for-table (:name target-table) 10000)+ (testing "write pool is created during transform execution"+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))+ (finally+ (t2/update! :model/Database db-id {:write_data_details old-write-details})+ (sql-jdbc.conn/invalidate-pool-for-db! (mt/db))))))))))diff --git a/test/metabase/warehouses/models/database_test.clj b/test/metabase/warehouses/models/database_test.cljindex 38e3a983682..f84c7f0fa3a 100644--- a/test/metabase/warehouses/models/database_test.clj+++ b/test/metabase/warehouses/models/database_test.clj@@ -5,6 +5,7 @@[java-time.api :as t][mb.hawk.assert-exprs.approximately-equal :as =?][metabase.api.common :as api]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver.settings :as driver.settings][metabase.driver.util :as driver.u]@@ -133,39 +134,41 @@(deftest health-check-write-connection-test(mt/test-drivers (mt/normal-drivers)- (with-redefs [quick-task/submit-task! (fn [task] (task))]- (binding [driver.settings/*allow-testing-h2-connections* true]- (testing "database with write_data_details checks both connections"- (mt/with-prometheus-system! [_ system]- (mt/with-temporary-setting-values [db-connection-timeout-ms 30000]- (database/health-check-database! (assoc (mt/db) :write_data_details (:details (mt/db))))- (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "default"}))- "default connection healthy")- (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "write-data"}))- "write-data connection healthy"))))-- (testing "database without write_data_details only checks default connection"- (mt/with-prometheus-system! [_ system]- (mt/with-temporary-setting-values [db-connection-timeout-ms 30000]- (database/health-check-database! (mt/db))- (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "default"}))- "default connection healthy")- (is (== 0 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "write-data"}))- "no write-data connection checked"))))-- (testing "write connection failure does not prevent default check"- (let [call-count (atom 0)]- (with-redefs [driver/can-connect? (fn [& _args]- (if (< (swap! call-count inc) 2)- true- (throw (Exception. "write connection failed"))))]+ (when config/ee-available?+ (mt/with-premium-features #{:writable-connection}+ (with-redefs [quick-task/submit-task! (fn [task] (task))]+ (binding [driver.settings/*allow-testing-h2-connections* true]+ (testing "database with write_data_details checks both connections"(mt/with-prometheus-system! [_ system](mt/with-temporary-setting-values [db-connection-timeout-ms 30000](database/health-check-database! (assoc (mt/db) :write_data_details (:details (mt/db))))(is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "default"}))"default connection healthy")- (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy false :reason "exception" :connection-type "write-data"}))- "write-data connection failed"))))))))))+ (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "write-data"}))+ "write-data connection healthy"))))++ (testing "database without write_data_details only checks default connection"+ (mt/with-prometheus-system! [_ system]+ (mt/with-temporary-setting-values [db-connection-timeout-ms 30000]+ (database/health-check-database! (mt/db))+ (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "default"}))+ "default connection healthy")+ (is (== 0 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "write-data"}))+ "no write-data connection checked"))))++ (testing "write connection failure does not prevent default check"+ (let [call-count (atom 0)]+ (with-redefs [driver/can-connect? (fn [& _args]+ (if (< (swap! call-count inc) 2)+ true+ (throw (Exception. "write connection failed"))))]+ (mt/with-prometheus-system! [_ system]+ (mt/with-temporary-setting-values [db-connection-timeout-ms 30000]+ (database/health-check-database! (assoc (mt/db) :write_data_details (:details (mt/db))))+ (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy true :connection-type "default"}))+ "default connection healthy")+ (is (== 1 (mt/metric-value system :metabase-database/status {:driver driver/*driver* :healthy false :reason "exception" :connection-type "write-data"}))+ "write-data connection failed"))))))))))))(deftest can-read-database-setting-test(let [encode-decode (comp json/decode json/encode)diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.cljindex 5fe7ee7aed2..e67bbf9ba7c 100644--- a/test/metabase/warehouses_rest/api_test.clj+++ b/test/metabase/warehouses_rest/api_test.clj@@ -7,6 +7,7 @@[medley.core :as m][metabase.analytics.snowplow-test :as snowplow-test][metabase.audit-app.core :as audit]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver.settings :as driver.settings][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]@@ -2426,13 +2427,15 @@(is (= {:status "error":message "Failed to connect to Database"}(mt/user-http-request :crowberto :get 200 (str "database/" id "/healthcheck")))))))- (testing "connection-type passed and configured"- (mt/with-temp [:model/Database {id :id} {:details {:host "primary"}- :write_data_details {:host "write"}}]- (with-redefs [driver/available? (constantly true)- driver/can-connect? (constantly true)]- (is (= {:status "ok"}- (mt/user-http-request :crowberto :get 200 (str "database/" id "/healthcheck?connection-type=write-data")))))))+ (when config/ee-available?+ (testing "connection-type passed and configured"+ (mt/with-premium-features #{:writable-connection}+ (mt/with-temp [:model/Database {id :id} {:details {:host "primary"}+ :write_data_details {:host "write"}}]+ (with-redefs [driver/available? (constantly true)+ driver/can-connect? (constantly true)]+ (is (= {:status "ok"}+ (mt/user-http-request :crowberto :get 200 (str "database/" id "/healthcheck?connection-type=write-data")))))))))(testing "connection-type passed but not configured returns 400"(mt/with-temp [:model/Database {id :id} {:details {:host "primary"}}](with-redefs [driver/available? (constantly true)]@@ -2716,7 +2719,7 @@(deftest update-database-write-data-details-test(testing "PUT /api/database/:id with write_data_details"(testing "Superusers can set write_data_details"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}](with-redefs [driver/can-connect? (constantly true)]@@ -2730,7 +2733,7 @@(is (= {:host "write-host" :password "write-pass" :write-data-connection true}(:write_data_details db)))))))))(testing "Superusers can clear write_data_details by setting it to nil"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}:write_data_details {:host "write-host"}}]@@ -2740,7 +2743,7 @@(let [db (t2/select-one :model/Database :id db-id)](is (nil? (:write_data_details db))))))))(testing "Sensitive fields are preserved when protected-password is sent"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}:write_data_details {:host "write-host"@@ -2753,7 +2756,7 @@(let [db (t2/select-one :model/Database :id db-id)](is (= "new-write-host" (get-in db [:write_data_details :host])))(is (= "original-pass" (get-in db [:write_data_details :password]))))))))- (testing "Returns 402 without :advanced-permissions feature"+ (testing "Returns 402 without :writable-connection feature"(with-redefs [premium-features/has-feature? (constantly false)](mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}]@@ -2761,23 +2764,24 @@{:write_data_details {:host "write-host"}}))))))(deftest put-validates-write-data-details-connection-test- (testing "PUT /api/database/:id returns 400 when write connection test fails"- (mt/with-premium-features #{:advanced-permissions}- (mt/with-temp [:model/Database {db-id :id} {:engine :h2- :details {:host "localhost"}}]- (with-redefs [driver/can-connect? (fn [_engine details]- (if (:write-data-connection details)- (throw (Exception. "Write connection failed"))- true))]- (let [response (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)- {:write_data_details {:host "totally-bogus-host"- :write-data-connection true}})]- (is (= "Write connection failed" (:message response)))))))))+ (when config/ee-available?+ (testing "PUT /api/database/:id returns 400 when write connection test fails"+ (mt/with-premium-features #{:writable-connection}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (with-redefs [driver/can-connect? (fn [_engine details]+ (if (:write-data-connection details)+ (throw (Exception. "Write connection failed"))+ true))]+ (let [response (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:write_data_details {:host "totally-bogus-host"+ :write-data-connection true}})]+ (is (= "Write connection failed" (:message response))))))))))(deftest write-data-details-guardrails-test(testing "PUT /api/database/:id write_data_details guardrails"(testing "write-data-connection must not be truthy in details"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}](is (= "write-data-connection must not be set in details"@@ -2785,14 +2789,14 @@{:details {:host "localhost":write-data-connection true}}))))))(testing "write-data-connection must be truthy in write_data_details"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}](is (= "write-data-connection must be set in write_data_details"(mt/user-http-request :crowberto :put 400 (format "database/%d" db-id){:write_data_details {:host "write-host"}}))))))(testing "Destination-database must be false in write_data_details"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}](is (= "destination-database must be false in write_data_details"@@ -2801,7 +2805,7 @@:write-data-connection true:destination-database true}}))))))(testing "Fields hidden for write connections must not be in write_data_details"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}}](is (str/includes?@@ -2811,7 +2815,7 @@:auto_run_queries true}})"write_data_details must not contain fields hidden for write connections")))))(testing "Cannot set write_data_details on a destination database"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {router-id :id} {:engine :h2:details {:host "localhost"}}:model/Database {dest-id :id} {:engine :h2@@ -2822,7 +2826,7 @@{:write_data_details {:host "write-host":write-data-connection true}}))))))(testing "Cannot set write_data_details on a router database"- (mt/with-premium-features #{:advanced-permissions}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {router-id :id} {:engine :h2:details {:host "localhost"}}:model/Database {_dest-id :id} {:engine :h2