Skip to content

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

chore(sql-editor): update isDatabaseV1Queryable logic (#14324)

Liu Jie59530b89e80
diff --git a/frontend/src/store/modules/v1/projectIamPolicy.ts b/frontend/src/store/modules/v1/projectIamPolicy.ts
index 3f88377a8f..1ce7aa25ba 100644
--- a/frontend/src/store/modules/v1/projectIamPolicy.ts
+++ b/frontend/src/store/modules/v1/projectIamPolicy.ts
@@ -2,11 +2,12 @@ import { isUndefined, uniq } from "lodash-es";
import { defineStore } from "pinia";
import { computed, ref, unref, watch } from "vue";
import { projectServiceClient } from "@/grpcweb";
-import type {
- ComposedDatabase,
- ComposedProject,
- MaybeRef,
- Permission,
+import {
+ QueryPermissionQueryAny,
+ type ComposedDatabase,
+ type ComposedProject,
+ type MaybeRef,
+ type QueryPermission,
} from "@/types";
import type { Expr } from "@/types/proto/google/api/expr/v1alpha1/syntax";
import type { User } from "@/types/proto/v1/auth_service";
@@ -139,7 +140,7 @@ export const useProjectIamPolicy = (project: MaybeRef<string>) => {
const checkProjectIAMPolicyWithExpr = (
user: User,
project: ComposedProject,
- permission: Permission,
+ requiredPermissions: QueryPermission[],
bindingExprCheck: (expr?: Expr) => boolean
): boolean => {
const roleStore = useRoleStore();
@@ -156,12 +157,14 @@ const checkProjectIAMPolicyWithExpr = (
// If the role does not have the permission, then skip.
const permissions =
roleStore.getRoleByName(binding.role)?.permissions || [];
- if (!permissions.includes(permission)) {
- continue;
- }
- // If binding expr check passes, then return true.
- if (bindingExprCheck(binding.parsedExpr)) {
- return true;
+
+ for (const permission of permissions) {
+ if (requiredPermissions.includes(permission as QueryPermission)) {
+ // If binding expr check passes, then return true.
+ if (bindingExprCheck(binding.parsedExpr)) {
+ return true;
+ }
+ }
}
}
@@ -170,13 +173,14 @@ const checkProjectIAMPolicyWithExpr = (
export const checkQuerierPermission = (
database: ComposedDatabase,
+ permissions: QueryPermission[] = QueryPermissionQueryAny,
schema?: string,
table?: string
) => {
return checkProjectIAMPolicyWithExpr(
useCurrentUserV1().value,
database.projectEntity,
- "bb.databases.query",
+ permissions,
(expr?: Expr): boolean => {
// If no condition is set, then return true.
if (!expr) {
diff --git a/frontend/src/types/iam/permission.ts b/frontend/src/types/iam/permission.ts
index e24747de39..498984e080 100644
--- a/frontend/src/types/iam/permission.ts
+++ b/frontend/src/types/iam/permission.ts
@@ -1,3 +1,5 @@
+import type { PickLiteral } from "../utils";
+
export type Permission =
| "bb.auditLogs.export"
| "bb.auditLogs.search"
@@ -30,6 +32,10 @@ export type Permission =
| "bb.databases.list"
| "bb.databases.query"
| "bb.databases.execute"
+ | "bb.databases.queryInfo"
+ | "bb.databases.queryExplain"
+ | "bb.databases.queryDML"
+ | "bb.databases.queryDDL"
| "bb.databases.sync"
| "bb.databases.update"
| "bb.issueComments.list"
@@ -112,3 +118,25 @@ export type Permission =
| "bb.settings.set"
| "bb.worksheets.manage"
| "bb.worksheets.get";
+
+export type QueryPermission = PickLiteral<
+ Permission,
+ | "bb.databases.query"
+ | "bb.databases.queryInfo"
+ | "bb.databases.queryExplain"
+ | "bb.databases.queryDDL"
+ | "bb.databases.queryDML"
+ | "bb.databases.execute"
+>;
+
+export const QueryPermissionQueryOnly: QueryPermission[] = [
+ "bb.databases.query",
+];
+export const QueryPermissionQueryAny: QueryPermission[] = [
+ "bb.databases.query",
+ "bb.databases.queryInfo",
+ "bb.databases.queryExplain",
+ "bb.databases.queryDDL",
+ "bb.databases.queryDML",
+ "bb.databases.execute",
+];
diff --git a/frontend/src/types/utils.ts b/frontend/src/types/utils.ts
index 527e46a71a..0c8b5366cf 100644
--- a/frontend/src/types/utils.ts
+++ b/frontend/src/types/utils.ts
@@ -6,3 +6,5 @@ export type ValidatedMessage = {
type: "warning" | "error";
message: string;
};
+
+export type PickLiteral<A, B extends A> = B;
diff --git a/frontend/src/utils/v1/database.ts b/frontend/src/utils/v1/database.ts
index 8c9dac98b1..f7a5d8a199 100644
--- a/frontend/src/utils/v1/database.ts
+++ b/frontend/src/utils/v1/database.ts
@@ -5,8 +5,8 @@ import {
databaseNamePrefix,
instanceNamePrefix,
} from "@/store/modules/v1/common";
-import { UNKNOWN_ID } from "@/types";
-import type { ComposedDatabase } from "@/types";
+import { QueryPermissionQueryAny, UNKNOWN_ID } from "@/types";
+import type { ComposedDatabase, QueryPermission } from "@/types";
import { State } from "@/types/proto/v1/common";
import {
hasPermissionToCreateChangeDatabaseIssue,
@@ -88,6 +88,7 @@ export const isDatabaseV1Alterable = (database: ComposedDatabase): boolean => {
// isDatabaseV1Queryable checks if database allowed to query in SQL Editor.
export const isDatabaseV1Queryable = (
database: ComposedDatabase,
+ permissions: QueryPermission[] = QueryPermissionQueryAny,
schema?: string,
table?: string
): boolean => {
@@ -97,11 +98,11 @@ export const isDatabaseV1Queryable = (
return true;
}
- if (hasWorkspacePermissionV2("bb.databases.query")) {
+ if (permissions.some((permission) => hasWorkspacePermissionV2(permission))) {
return true;
}
- if (checkQuerierPermission(database, schema, table)) {
+ if (checkQuerierPermission(database, permissions, schema, table)) {
return true;
}