Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: filter unavailable tables in sql editor panel (#6946)

boojack42495f679ce7
diff --git a/frontend/src/store/modules/v1/projectIamPolicy.ts b/frontend/src/store/modules/v1/projectIamPolicy.ts
index 6a47789188..194fc0dc5e 100644
--- a/frontend/src/store/modules/v1/projectIamPolicy.ts
+++ b/frontend/src/store/modules/v1/projectIamPolicy.ts
@@ -13,6 +13,7 @@ import {
isOwnerOfProjectV1,
} from "@/utils";
import { convertFromExpr } from "@/utils/issue/cel";
+import { isUndefined } from "lodash-es";
export const useProjectIamPolicyStore = defineStore(
"project-iam-policy",
@@ -169,7 +170,11 @@ export const useCurrentUserIamPolicy = () => {
return isProjectOwnerOrDeveloper(projectName);
};
- const allowToQueryDatabaseV1 = (database: ComposedDatabase) => {
+ const allowToQueryDatabaseV1 = (
+ database: ComposedDatabase,
+ schema?: string,
+ table?: string
+ ) => {
if (hasWorkspaceSuperPrivilege) {
return true;
}
@@ -192,16 +197,23 @@ export const useCurrentUserIamPolicy = () => {
) {
if (binding.parsedExpr?.expr) {
const conditionExpr = convertFromExpr(binding.parsedExpr.expr);
- if (
- conditionExpr.databaseResources &&
- conditionExpr.databaseResources.length > 0
- ) {
- const hasDatabaseField =
- conditionExpr.databaseResources.find(
- (item) => item.databaseName === database.name
- ) !== undefined;
- if (hasDatabaseField) {
- return true;
+ if (conditionExpr.databaseResources) {
+ for (const databaseResource of conditionExpr.databaseResources) {
+ if (databaseResource.databaseName === database.name) {
+ if (isUndefined(schema) && isUndefined(table)) {
+ return true;
+ } else {
+ if (
+ isUndefined(databaseResource.schema) ||
+ (isUndefined(databaseResource.schema) &&
+ isUndefined(databaseResource.table)) ||
+ (databaseResource.schema === schema &&
+ databaseResource.table === table)
+ ) {
+ return true;
+ }
+ }
+ }
}
} else {
return true;
diff --git a/frontend/src/utils/v1/database.ts b/frontend/src/utils/v1/database.ts
index ac54ed3d9e..4d3c945f80 100644
--- a/frontend/src/utils/v1/database.ts
+++ b/frontend/src/utils/v1/database.ts
@@ -159,6 +159,58 @@ export const isDatabaseV1Queryable = (
return false;
};
+// isTableQueryable checks if table allowed to query in SQL Editor.
+export const isTableQueryable = (
+ database: ComposedDatabase,
+ schema: string,
+ table: string,
+ user: User
+): boolean => {
+ if (!hasFeature("bb.feature.access-control")) {
+ // The current plan doesn't have access control feature.
+ // Fallback to true.
+ return true;
+ } else {
+ const policy = usePolicyV1Store().getPolicyByName("policies/WORKSPACE_IAM");
+ if (policy) {
+ const bindings = policy.workspaceIamPolicy?.bindings;
+ if (bindings) {
+ const querierBinding = bindings.find(
+ (binding) => binding.role === "roles/QUERIER"
+ );
+ if (querierBinding) {
+ const simpleExpr = resolveCELExpr(
+ querierBinding.parsedExpr?.expr || Expr.fromPartial({})
+ );
+ const envNameList = extractEnvironmentNameListFromExpr(simpleExpr);
+ if (envNameList.includes(database.instanceEntity.environment)) {
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ if (
+ hasWorkspacePermissionV1(
+ "bb.permission.workspace.manage-access-control",
+ user.userRole
+ )
+ ) {
+ // The current user has the super privilege to access all databases.
+ // AKA. Owners and DBAs
+ return true;
+ }
+
+ const currentUserIamPolicy = useCurrentUserIamPolicy();
+ if (currentUserIamPolicy.allowToQueryDatabaseV1(database, schema, table)) {
+ return true;
+ }
+
+ // denied otherwise
+ return false;
+};
+
type DatabaseV1FilterFields =
| "name"
| "project"
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
index 2ed23f553c..c3cfb7662b 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
@@ -34,7 +34,7 @@
</div>
<div class="flex-1 p-1 overflow-y-auto flex flex-col gap-y-2">
- <template v-for="(schema, i) in databaseMetadata.schemas" :key="i">
+ <template v-for="(schema, i) in availableSchemas" :key="i">
<div v-for="(table, j) in schema.tables" :key="j" class="text-sm">
<div
class="flex items-center h-6 px-1 text-gray-600 whitespace-pre-wrap break-words rounded-sm"
@@ -62,11 +62,16 @@ import type {
TableMetadata,
} from "@/types/proto/store/database";
import type { ComposedDatabase } from "@/types";
-import { databaseV1Slug, instanceV1HasAlterSchema } from "@/utils";
+import {
+ databaseV1Slug,
+ instanceV1HasAlterSchema,
+ isTableQueryable,
+} from "@/utils";
import ExternalLinkButton from "./ExternalLinkButton.vue";
import AlterSchemaButton from "./AlterSchemaButton.vue";
import SchemaDiagramButton from "./SchemaDiagramButton.vue";
import { Engine } from "@/types/proto/v1/common";
+import { useCurrentUserV1 } from "@/store";
const props = defineProps<{
database: ComposedDatabase;
@@ -83,10 +88,33 @@ const emit = defineEmits<{
): void;
}>();
+const currentUser = useCurrentUserV1();
+
const engine = computed(() => props.database.instanceEntity.engine);
const rowClickable = computed(() => engine.value !== Engine.MONGODB);
+const availableSchemas = computed(() => {
+ const schemas = props.databaseMetadata.schemas
+ .map((schema) => {
+ return {
+ ...schema,
+ tables: schema.tables.filter((table) => {
+ return isTableQueryable(
+ props.database,
+ schema.name,
+ table.name,
+ currentUser.value
+ );
+ }),
+ };
+ })
+ .filter((schema) => {
+ return schema.tables.length !== 0;
+ });
+ return schemas;
+});
+
const handleClickHeader = () => {
if (!props.headerClickable) return;
emit("click-header");