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
feat(embed-onboarding): Block all schemas that does not contain selected (#70717)
Nicolò Pretto64712e1f3f21
diff --git a/e2e/test/scenarios/embedding/embedding-hub/embedding-hub.cy.spec.ts b/e2e/test/scenarios/embedding/embedding-hub/embedding-hub.cy.spec.tsindex 7d167214c1c..32cd6c1caac 100644--- a/e2e/test/scenarios/embedding/embedding-hub/embedding-hub.cy.spec.ts+++ b/e2e/test/scenarios/embedding/embedding-hub/embedding-hub.cy.spec.ts@@ -1,4 +1,4 @@-import { SAMPLE_DB_TABLES } from "e2e/support/cypress_data";+import { SAMPLE_DB_TABLES, WRITABLE_DB_ID } from "e2e/support/cypress_data";import { ALL_EXTERNAL_USERS_GROUP_ID } from "e2e/support/cypress_sample_instance_data";const { H } = cy;@@ -962,23 +962,30 @@ describe("scenarios - embedding hub", () => {);});- cy.log("permissions graph should have sandboxed view-data");cy.request("GET",`/api/permissions/graph/group/${ALL_EXTERNAL_USERS_GROUP_ID}`,).should((response) => {const graph = response.body;- // [1] is for sample database, public schema.const permissions = graph.groups[ALL_EXTERNAL_USERS_GROUP_ID!][1];expect(permissions).to.exist;- const viewData = permissions["view-data"];- expect(viewData).to.exist;- expect(viewData["PUBLIC"][STATIC_ORDERS_ID]).to.equal("sandboxed");- expect(viewData["PUBLIC"][STATIC_PEOPLE_ID]).to.equal("sandboxed");+ // deep.equal ensures only the selected tables have permissions —+ // non-selected tables should be blocked (omitted from the response)+ expect(permissions["view-data"]).to.deep.equal({+ PUBLIC: {+ [STATIC_ORDERS_ID]: "sandboxed",+ [STATIC_PEOPLE_ID]: "sandboxed",+ },+ });- expect(permissions["create-queries"]).to.equal("query-builder");+ expect(permissions["create-queries"]).to.deep.equal({+ PUBLIC: {+ [STATIC_ORDERS_ID]: "query-builder",+ [STATIC_PEOPLE_ID]: "query-builder",+ },+ });});H.main()@@ -1099,6 +1106,122 @@ describe("scenarios - embedding hub", () => {.should("exist");});+ it(+ "should block schemas without selected tables in RLS setup",+ { tags: ["@external"] },+ () => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");++ cy.log(+ 'reset "multi_schema" fixture: creates Domestic and Wild schemas, each with tables',+ );+ H.resetTestTable({ type: "postgres", table: "multi_schema" });+ H.resyncDatabase({ dbId: WRITABLE_DB_ID });++ cy.intercept("PUT", "/api/permissions/graph").as(+ "updatePermissionsGraph",+ );++ cy.visit("/admin/embedding/setup-guide/permissions");++ cy.log("enable tenants and create shared collection");+ H.main()+ .findByRole("button", {+ name: "Enable tenants and create shared collection",+ })+ .click();++ cy.log("wait for tenants to be enabled");+ H.main()+ .findByRole("listitem", {+ name: "Enable multi-tenant user strategy",+ timeout: 10_000,+ })+ .icon("check")+ .should("exist");++ cy.log("select RLS strategy");+ H.main()+ .findByRole("radio", { name: /Row and column level security/ })+ .scrollIntoView()+ .click();++ H.main()+ .findByRole("button", {+ name: "Use row and column level security",+ })+ .scrollIntoView()+ .click();++ cy.log(+ "pick a table from the Domestic schema of the Postgres database",+ );++ H.main().findByText("Pick a table").click();+ H.miniPicker().findByText("Writable Postgres12").click();+ H.miniPicker().findByText("Domestic").click();+ H.miniPicker().findByText("Animals").click();++ cy.log("pick Score column as the tenant filter field");+ H.main().findByPlaceholderText("Pick a column").click();+ H.popover().findByText("Score").click();++ cy.log("create sandbox");+ H.main().findByRole("button", { name: "Next" }).click();++ cy.log("wait for sandbox creation to complete");+ cy.wait("@updatePermissionsGraph");++ cy.log("no error toast should appear");+ H.undoToast().should("not.exist");++ cy.log("verify schemas without selected tables are blocked");+ cy.request(+ "GET",+ `/api/permissions/graph/group/${ALL_EXTERNAL_USERS_GROUP_ID}`,+ ).then((response) => {+ const graph = response.body;++ const permissions =+ graph.groups[ALL_EXTERNAL_USERS_GROUP_ID!][WRITABLE_DB_ID];+ expect(permissions).to.exist;++ const viewData = permissions["view-data"];+ expect(viewData).to.exist;++ // Domestic schema should have granular per-table permissions+ expect(viewData["Domestic"]).to.be.an("object");+ const domesticTableIds = Object.keys(viewData["Domestic"]);+ expect(domesticTableIds.length).to.be.at.least(1);++ // At least one table should be sandboxed (the Animals table we selected)+ const domesticValues = Object.values(+ viewData["Domestic"],+ ) as string[];+ expect(domesticValues).to.include("sandboxed");++ // Wild schema should be blocked (it has no selected tables)+ expect(viewData["Wild"]).to.equal("blocked");++ // create-queries should allow query-builder for Domestic,+ // and be "no" for Wild (cascaded from blocked view-data)+ const createQueries = permissions["create-queries"];+ expect(createQueries["Domestic"]).to.equal("query-builder");+ expect(createQueries["Wild"]).to.equal("no");+ });++ H.main()+ .findByRole("listitem", {+ name: "Select data to make available",+ timeout: 10_000,+ })+ .icon("check")+ .should("exist");+ },+ );+it("locks the production embed step when JWT is not enabled", () => {cy.visit("/admin/embedding/setup-guide");diff --git a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/hooks/use-update-all-tenant-users-group-permissions.ts b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/hooks/use-update-all-tenant-users-group-permissions.tsindex 8e943abb957..6cea56c36e7 100644--- a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/hooks/use-update-all-tenant-users-group-permissions.ts+++ b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/hooks/use-update-all-tenant-users-group-permissions.ts@@ -1,12 +1,15 @@import { useCallback, useMemo } from "react";-import { Api, useListPermissionsGroupsQuery } from "metabase/api";+import { Api, databaseApi, useListPermissionsGroupsQuery } from "metabase/api";+import { tableApi } from "metabase/api/table";import { listTag } from "metabase/api/tags";import { useDispatch } from "metabase/lib/redux";import { PLUGIN_TENANTS } from "metabase/plugins";import { PermissionsApi } from "metabase/services";+import type { DatabaseId, TableId } from "metabase-types/api";import {+ type AllSchemaTables,type UpdateTenantDataAccessOptions,buildPermissionsGraph,buildSandboxPolicies,@@ -62,12 +65,50 @@ export function useUpdateAllTenantUsersGroupPermissions(): UseUpdateAllTenantUsereturn;}+ // Fetch all tables for each database with sandboxed tables so we can+ // block non-selected tables and schemas+ const uniqueDbIds = [+ ...new Set(sandboxedTables.map((t) => t.databaseId)),+ ];+ const allSchemaTables: AllSchemaTables = {};+ await Promise.all(+ uniqueDbIds.map(async (dbId) => {+ const { data: tables } = await dispatch(+ tableApi.endpoints.listTables.initiate({ dbId }),+ );+ if (tables) {+ const bySchema: Record<string, TableId[]> = {};+ for (const table of tables) {+ const schema = table.schema ?? "";+ if (!bySchema[schema]) {+ bySchema[schema] = [];+ }+ bySchema[schema].push(table.id);+ }+ allSchemaTables[dbId] = bySchema;+ }+ }),+ );++ // Fetch all database IDs so we can block databases without sandboxed tables+ const allDatabaseIds = await dispatch(+ databaseApi.endpoints.listDatabases.initiate(),+ )+ .unwrap()+ .then((res) => res.data.map((db) => db.id))+ .catch(() => [] as DatabaseId[]);+// get the revision number of the graphconst graph = await PermissionsApi.graphForGroup({groupId: allTenantUsersGroupId,});- const groups = buildPermissionsGraph(allTenantUsersGroupId, options);+ const groups = buildPermissionsGraph(+ allTenantUsersGroupId,+ options,+ allSchemaTables,+ allDatabaseIds,+ );const sandboxes = buildSandboxPolicies(allTenantUsersGroupId,sandboxedTables,diff --git a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.ts b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.tsindex 9087f006820..a9aab47e382 100644--- a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.ts+++ b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.ts@@ -35,15 +35,20 @@ type SandboxedTableConfig = {type DatabasePermissionGraph = Record<string,- string | Record<string, Record<TableId, string>>+ string | Record<string, string | Record<TableId, string>>>;+/** All tables grouped by schema for each database */+export type AllSchemaTables = Record<DatabaseId, Record<string, TableId[]>>;+/*** Builds the permission graph structure for the API call.*/export function buildPermissionsGraph(groupId: GroupId,options: UpdateTenantDataAccessOptions,+ allSchemaTables?: AllSchemaTables,+ allDatabaseIds?: DatabaseId[],): Record<GroupId, Record<DatabaseId, DatabasePermissionGraph>> {const { impersonatedDatabaseIds = [], sandboxedTables = [] } = options;@@ -73,14 +78,63 @@ export function buildPermissionsGraph(const viewData = dbPerms["view-data"] as Record<string,- Record<TableId, string>+ string | Record<TableId, string>>;if (!viewData[schema]) {viewData[schema] = {};}- viewData[schema][tableId as TableId] = "sandboxed";+ (viewData[schema] as Record<TableId, string>)[tableId as TableId] =+ "sandboxed";+ }++ // Block non-selected tables and schemas within databases that have sandboxed tables+ if (allSchemaTables) {+ for (const [dbIdStr, schemas] of Object.entries(allSchemaTables)) {+ const databaseId = Number(dbIdStr) as DatabaseId;++ if (!groupGraph[databaseId]) {+ continue;+ }++ const dbPerms = groupGraph[databaseId];+ if (!dbPerms["view-data"] || typeof dbPerms["view-data"] === "string") {+ continue;+ }++ const viewData = dbPerms["view-data"] as Record<+ string,+ string | Record<TableId, string>+ >;++ for (const [schema, tableIds] of Object.entries(schemas)) {+ if (!viewData[schema]) {+ // Schema has no sandboxed tables — block the entire schema+ viewData[schema] = "blocked";+ } else if (typeof viewData[schema] === "object") {+ // Schema has sandboxed tables — block all non-selected tables+ const schemaPerms = viewData[schema] as Record<TableId, string>;+ for (const tableId of tableIds) {+ if (!schemaPerms[tableId]) {+ schemaPerms[tableId] = "blocked";+ }+ }+ }+ }+ }+ }++ // Block entire databases that have no sandboxed tables and are not impersonated+ if (allDatabaseIds) {+ for (const databaseId of allDatabaseIds) {+ if (!groupGraph[databaseId]) {+ groupGraph[databaseId] = {+ "view-data": "blocked",+ "create-queries": "no",+ };+ }+ }}return { [groupId]: groupGraph };diff --git a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.unit.spec.ts b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.unit.spec.tsindex 84aa0c940eb..8307118b065 100644--- a/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.unit.spec.ts+++ b/frontend/src/metabase/embedding/embedding-hub/components/SetupPermissionsAndTenantsPage/utils/permission-utils.unit.spec.ts@@ -42,4 +42,114 @@ describe("buildPermissionsGraph", () => {},});});++ it("blocks schemas without selected tables when allSchemaTables is provided", () => {+ const result = buildPermissionsGraph(+ groupId,+ {+ sandboxedTables: [+ {+ tableId: 100,+ databaseId: 1,+ schemaName: "tenant_data",+ filterFieldId: 5,+ },+ ],+ },+ {+ 1: {+ tenant_data: [100, 101],+ internal: [200, 201],+ analytics: [300],+ },+ },+ );++ expect(result).toEqual({+ [groupId]: {+ 1: {+ "view-data": {+ tenant_data: { 100: "sandboxed", 101: "blocked" },+ internal: "blocked",+ analytics: "blocked",+ },+ "create-queries": "query-builder",+ },+ },+ });+ });++ it("blocks non-selected tables within schemas that have sandboxed tables", () => {+ const result = buildPermissionsGraph(+ groupId,+ {+ sandboxedTables: [+ {+ tableId: 100,+ databaseId: 1,+ schemaName: "public",+ filterFieldId: 5,+ },+ ],+ },+ { 1: { public: [100, 101, 102] } },+ );++ expect(result).toEqual({+ [groupId]: {+ 1: {+ "view-data": {+ public: { 100: "sandboxed", 101: "blocked", 102: "blocked" },+ },+ "create-queries": "query-builder",+ },+ },+ });+ });++ it("handles multiple databases with allSchemaTables", () => {+ const result = buildPermissionsGraph(+ groupId,+ {+ sandboxedTables: [+ {+ tableId: 100,+ databaseId: 1,+ schemaName: "public",+ filterFieldId: 5,+ },+ {+ tableId: 200,+ databaseId: 2,+ schemaName: "data",+ filterFieldId: 6,+ },+ ],+ },+ {+ 1: { public: [100, 101], private: [150] },+ 2: { data: [200, 201], logs: [300], archive: [400] },+ },+ );++ expect(result).toEqual({+ [groupId]: {+ 1: {+ "view-data": {+ public: { 100: "sandboxed", 101: "blocked" },+ private: "blocked",+ },+ "create-queries": "query-builder",+ },+ 2: {+ "view-data": {+ data: { 200: "sandboxed", 201: "blocked" },+ logs: "blocked",+ archive: "blocked",+ },+ "create-queries": "query-builder",+ },+ },+ });+ });});