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
Exclude audit db when coalescing default perms (#76608)
Ben Grabow2833fa63b274
diff --git a/e2e/test/scenarios/permissions/sandboxing/helpers/e2e-sandboxing-helpers.ts b/e2e/test/scenarios/permissions/sandboxing/helpers/e2e-sandboxing-helpers.tsindex be64dd0f697..8d3e8f05247 100644--- a/e2e/test/scenarios/permissions/sandboxing/helpers/e2e-sandboxing-helpers.ts+++ b/e2e/test/scenarios/permissions/sandboxing/helpers/e2e-sandboxing-helpers.ts@@ -350,14 +350,12 @@ export const configureSandboxPolicy = (cy.log("Modify the sandboxing policy for the 'data' group");H.modifyPermission("data", 0, "Row and column security");- if (databaseId === 1) {- H.modal().within(() => {- cy.findByText(- /Change access to this database to .*Row and column security.*?/,- );- cy.button("Change").click();- });- }+ H.modal().within(() => {+ cy.findByText(+ /Change access to this database to .*Row and column security.*?/,+ );+ cy.button("Change").click();+ });H.modal().findByText(/Configure row and column security for this table/);diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.tsindex 2e3bd579e05..0200970ce86 100644--- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts+++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts@@ -1,6 +1,6 @@const { H } = cy;-import { WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { USER_GROUPS, WRITABLE_DB_ID } from "e2e/support/cypress_data";import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";import { ORDERS_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";import type {@@ -1355,6 +1355,20 @@ describe("issue 13347", () => {},});+ // The normal user belongs to the data group, which would otherwise get+ // query-builder access to this database by default. Revoke create-queries so+ // the user cannot build new questions on it - the condition this issue is+ // about. (Avoid view-data "blocked", which needs the advanced-permissions+ // token feature this spec doesn't have.)+ cy.updatePermissionsGraph({+ [USER_GROUPS.DATA_GROUP]: {+ [WRITABLE_DB_ID]: {+ "view-data": "unrestricted",+ "create-queries": "no",+ },+ },+ });+cy.signInAsNormalUser();cy.visit("/");diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.cljindex 6b4d965fde4..8b8fef24e90 100644--- a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj+++ b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj@@ -100,7 +100,7 @@(deftest analytics-permissions-test(mt/with-premium-features #{:audit-app}(mt/with-temp [:model/PermissionsGroup {group-id :id} {}- :model/Database {database-id :id} {}+ :model/Database {database-id :id} {:is_audit true}:model/Table view-table {:db_id database-id :name "v_users"}:model/Collection collection {:namespace "analytics"}](with-redefs [audit/audit-db-id database-iddiff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.cljindex 999c60984ec..89f1fb177f4 100644--- a/src/metabase/permissions/models/data_permissions.clj+++ b/src/metabase/permissions/models/data_permissions.clj@@ -1068,7 +1068,10 @@:from [[(t2/table-name :model/DataPermissions)]]:where [:and[:in :group_id group-ids]- [:in :perm_type ["perms/create-queries" "perms/download-results"]]]}))+ [:in :perm_type ["perms/create-queries" "perms/download-results"]]+ [:not-in :db_id {:select [:id]+ :from [(t2/table-name :model/Database)]+ :where [:= :is_audit true]}]]}));; Group by (group_id, perm_type) → set of valuesperms-by-grp (when all-perms(reduce (fn [acc {:keys [group_id perm_type perm_value]}]diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.cljindex 7d5e4f31ee1..86faaa7047a 100644--- a/test/metabase/permissions/models/data_permissions_test.clj+++ b/test/metabase/permissions/models/data_permissions_test.clj@@ -821,6 +821,44 @@:perm_type :perms/download-results))"blocked view-data must force :no downloads"))))))+(deftest set-default-database-permissions!-ignores-audit-db-test+ (testing "A database added after the audit DB does not inherit the audit DB's restrictive create-queries level.+ The audit DB always has create-queries :no; coalescing it as the group's lowest existing level would+ wrongly drag every later database down to :no, leaving All Users unable to query newly added DBs."+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}+ :model/Database {audit-db-id :id} {:is_audit true}+ :model/Database {new-db-id :id} {}]+ ;; Leave the group with only the audit DB's perms, so the audit DB is its sole source of an existing level.+ (t2/delete! :model/DataPermissions :group_id group-id)+ (let [group (t2/select-one :model/PermissionsGroup :id group-id)]+ (data-perms/set-default-database-permissions! {:id audit-db-id :is_audit true} [group])+ (is (= :no (t2/select-one-fn :perm_value :model/DataPermissions+ :group_id group-id :db_id audit-db-id :perm_type :perms/create-queries))+ "sanity: the audit DB grants :no create-queries")+ (data-perms/set-default-database-permissions! {:id new-db-id} [group])+ (is (= :query-builder-and-native+ (t2/select-one-fn :perm_value :model/DataPermissions+ :group_id group-id :db_id new-db-id :perm_type :perms/create-queries))+ "the new DB gets the default create-queries level, unpolluted by the audit DB's :no")))))++(deftest set-default-database-permissions!-coalesces-customer-db-but-not-audit-db-test+ (testing "A new database coalesces the group's lowest level across real customer databases while still ignoring+ the audit DB. A customer DB set to :query-builder must lower the new DB to :query-builder; the audit+ DB's :no must not lower it further."+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}+ :model/Database {audit-db-id :id} {:is_audit true}+ :model/Database {cust-db-id :id} {}+ :model/Database {new-db-id :id} {}]+ (t2/delete! :model/DataPermissions :group_id group-id)+ (let [group (t2/select-one :model/PermissionsGroup :id group-id)]+ (data-perms/set-default-database-permissions! {:id audit-db-id :is_audit true} [group])+ (data-perms/set-database-permission! group-id cust-db-id :perms/create-queries :query-builder)+ (data-perms/set-default-database-permissions! {:id new-db-id} [group])+ (is (= :query-builder+ (t2/select-one-fn :perm_value :model/DataPermissions+ :group_id group-id :db_id new-db-id :perm_type :perms/create-queries))+ "new DB coalesces the customer DB's :query-builder, not the audit DB's :no")))))+(deftest set-default-table-permissions!-going-granular-test(testing "When a group has DB-level perms but new table needs :blocked, it expands to per-table rows"(mt/with-temp [:model/Database {db-id :id} {}