Skip to content

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

chore: fix database related permission checks (#13725)

boojack841b24e2f01b
diff --git a/frontend/src/components/QuickActionPanel.vue b/frontend/src/components/QuickActionPanel.vue
index fea7e57f74..fcbaa06dd7 100644
--- a/frontend/src/components/QuickActionPanel.vue
+++ b/frontend/src/components/QuickActionPanel.vue
@@ -117,7 +117,6 @@ import { PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL } from "@/router/dashboard/proje
import { PROJECT_V1_ROUTE_DASHBOARD } from "@/router/dashboard/workspaceRoutes";
import {
useCommandStore,
- useCurrentUserIamPolicy,
useSubscriptionV1Store,
useProjectV1Store,
useInstanceResourceList,
@@ -191,19 +190,6 @@ const project = computed(() => {
);
});
-// Only show alter schema and change data if the user has permission to alter schema of at least one project.
-const shouldShowAlterDatabaseEntries = computed(() => {
- const currentUserIamPolicy = useCurrentUserIamPolicy();
- return projectStore.projectList
- .map((project) => {
- return (
- currentUserIamPolicy.allowToChangeDatabaseOfProject(project.name) &&
- hasProjectPermissionV2(project, "bb.issues.create")
- );
- })
- .includes(true);
-});
-
watch(route, () => {
state.quickActionType = undefined;
});
@@ -265,7 +251,10 @@ const availableQuickActionList = computed((): QuickAction[] => {
{
type: "quickaction.bb.database.create",
title: t("quick-action.new-db"),
- hide: !shouldShowAlterDatabaseEntries.value,
+ hide: !(
+ hasProjectPermissionV2(project.value, "bb.issues.create") &&
+ hasProjectPermissionV2(project.value, "bb.plans.create")
+ ),
action: createDatabase,
icon: h(DatabaseIcon),
},
diff --git a/frontend/src/components/v2/Model/DatabaseV1Table/DatabaseOperations.vue b/frontend/src/components/v2/Model/DatabaseV1Table/DatabaseOperations.vue
index 377ff47d7a..1f8ba2f260 100644
--- a/frontend/src/components/v2/Model/DatabaseV1Table/DatabaseOperations.vue
+++ b/frontend/src/components/v2/Model/DatabaseV1Table/DatabaseOperations.vue
@@ -103,7 +103,6 @@ import TransferOutDatabaseForm from "@/components/TransferOutDatabaseForm";
import { Drawer } from "@/components/v2";
import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
import {
- useCurrentUserIamPolicy,
useProjectV1Store,
useDatabaseV1Store,
useGracefulRequest,
@@ -111,7 +110,7 @@ import {
pushNotification,
useAppFeature,
} from "@/store";
-import type { ComposedDatabase, Permission } from "@/types";
+import type { ComposedDatabase } from "@/types";
import { DEFAULT_PROJECT_NAME } from "@/types";
import {
Database,
@@ -165,7 +164,6 @@ const router = useRouter();
const databaseStore = useDatabaseV1Store();
const projectStore = useProjectV1Store();
const dbSchemaStore = useDBSchemaV1Store();
-const currentUserIamPolicy = useCurrentUserIamPolicy();
const disableSchemaEditor = useAppFeature(
"bb.feature.issue.disable-schema-editor"
);
@@ -204,24 +202,6 @@ const selectedProjectName = computed(() => {
return projectStore.getProjectByName(project).name;
});
-const canEditDatabase = (
- db: ComposedDatabase,
- requiredProjectPermission: Permission
-): boolean => {
- if (isArchivedDatabaseV1(db)) {
- return false;
- }
- if (currentUserIamPolicy.allowToChangeDatabaseOfProject(db.project)) {
- return true;
- }
-
- if (hasProjectPermissionV2(db.projectEntity, requiredProjectPermission)) {
- return true;
- }
-
- return false;
-};
-
const databaseSupportAlterSchema = computed(() => {
return props.databases.every((db) => {
return instanceV1HasAlterSchema(db.instanceResource);
@@ -241,8 +221,10 @@ const allowChangeData = computed(() => {
});
const allowTransferProject = computed(() => {
- return props.databases.every((db) =>
- canEditDatabase(db, "bb.projects.update")
+ return props.databases.every(
+ (db) =>
+ !isArchivedDatabaseV1(db) &&
+ hasProjectPermissionV2(db.projectEntity, "bb.projects.update")
);
});
diff --git a/frontend/src/store/modules/v1/projectIamPolicy.ts b/frontend/src/store/modules/v1/projectIamPolicy.ts
index acc21e662c..d6fa8279b4 100644
--- a/frontend/src/store/modules/v1/projectIamPolicy.ts
+++ b/frontend/src/store/modules/v1/projectIamPolicy.ts
@@ -1,17 +1,21 @@
import { isUndefined, uniq } from "lodash-es";
import { defineStore } from "pinia";
-import { computed, ref, unref, watch, watchEffect } from "vue";
+import { computed, ref, unref, watch } from "vue";
import { projectServiceClient } from "@/grpcweb";
-import type { ComposedDatabase, ComposedProject, MaybeRef } from "@/types";
-import { PresetRoleType } from "@/types";
+import type {
+ ComposedDatabase,
+ ComposedProject,
+ MaybeRef,
+ Permission,
+} from "@/types";
import type { Expr } from "@/types/proto/google/api/expr/v1alpha1/syntax";
+import type { User } from "@/types/proto/v1/auth_service";
import { IamPolicy } from "@/types/proto/v1/iam_policy";
-import { hasProjectPermissionV2, hasWorkspacePermissionV2 } from "@/utils";
import { getUserEmailListInBinding } from "@/utils";
import { convertFromExpr } from "@/utils/issue/cel";
import { useCurrentUserV1 } from "../auth";
+import { useRoleStore } from "../role";
import { usePermissionStore } from "./permission";
-import { useProjectV1Store } from "./project";
export const useProjectIamPolicyStore = defineStore(
"project-iam-policy",
@@ -132,151 +136,87 @@ export const useProjectIamPolicy = (project: MaybeRef<string>) => {
return { policy, ready };
};
-export const useCurrentUserIamPolicy = () => {
- const iamPolicyStore = useProjectIamPolicyStore();
- const projectStore = useProjectV1Store();
- const currentUser = useCurrentUserV1();
-
- watchEffect(() => {
- // Fetch all project iam policies.
- Promise.all(
- projectStore.projectList.map((project) =>
- iamPolicyStore.getOrFetchProjectIamPolicy(project.name)
- )
- );
- });
-
- // hasWorkspaceSuperPrivilege checks whether the current user has the super privilege to access all databases. AKA. Owners and DBAs
- const hasWorkspaceSuperPrivilege = computed(() =>
- hasWorkspacePermissionV2("bb.projects.list")
- );
-
- const allowToChangeDatabaseOfProject = (projectName: string) => {
- if (hasWorkspaceSuperPrivilege.value) {
- return true;
+const checkProjectIAMPolicyWithExpr = (
+ user: User,
+ project: ComposedProject,
+ permission: Permission,
+ bindingExprCheck: (expr?: Expr) => boolean
+): boolean => {
+ const roleStore = useRoleStore();
+ // Check if the user has the permission.
+ for (const binding of project.iamPolicy.bindings) {
+ // If the user is not in the binding, then skip.
+ const userEmailList = getUserEmailListInBinding({
+ binding,
+ ignoreGroup: false,
+ });
+ if (!userEmailList.includes(user.email)) {
+ continue;
}
-
- const project = projectStore.getProjectByName(projectName);
- if (!project) {
- return false;
+ // If the role does not have the permission, then skip.
+ const permissions =
+ roleStore.getRoleByName(binding.role)?.permissions || [];
+ if (!permissions.includes(permission)) {
+ continue;
}
-
- return hasProjectPermissionV2(project, "bb.databases.update");
- };
-
- const checkProjectIAMPolicy = (
- project: ComposedProject,
- role: string,
- bindingExprCheck: (expr: Expr) => boolean
- ): boolean => {
- const roleList = usePermissionStore().currentRoleListInProjectV1(project);
- if (roleList.includes(PresetRoleType.PROJECT_OWNER)) {
+ // If binding expr check passes, then return true.
+ if (bindingExprCheck(binding.parsedExpr?.expr)) {
return true;
}
- if (!roleList.includes(role)) {
- return false;
- }
-
- // Check if the user has the permission to query the database.
- for (const binding of project.iamPolicy.bindings) {
- if (binding.role !== role) {
- continue;
- }
+ }
- const userEmailList = getUserEmailListInBinding({
- binding,
- ignoreGroup: false,
- });
- if (!userEmailList.includes(currentUser.value.email)) {
- continue;
- }
+ return false;
+};
- if (!binding.parsedExpr?.expr) {
- return true;
- }
- if (bindingExprCheck(binding.parsedExpr?.expr)) {
+export const checkQuerierPermission = (
+ database: ComposedDatabase,
+ schema?: string,
+ table?: string
+) => {
+ return checkProjectIAMPolicyWithExpr(
+ useCurrentUserV1().value,
+ database.projectEntity,
+ "bb.databases.query",
+ (expr?: Expr): boolean => {
+ // If no condition is set, then return true.
+ if (!expr) {
return true;
}
- }
-
- return false;
- };
- const allowToQueryDatabaseV1 = (
- database: ComposedDatabase,
- schema?: string,
- table?: string
- ) => {
- if (hasWorkspaceSuperPrivilege.value) {
- return true;
- }
-
- return checkProjectIAMPolicy(
- database.projectEntity,
- PresetRoleType.PROJECT_QUERIER,
- (expr: Expr): boolean => {
- const conditionExpr = convertFromExpr(expr);
- if (
- conditionExpr.databaseResources &&
- conditionExpr.databaseResources.length > 0
- ) {
- for (const databaseResource of conditionExpr.databaseResources) {
- if (databaseResource.databaseName === database.name) {
- if (isUndefined(schema) && isUndefined(table)) {
+ const conditionExpr = convertFromExpr(expr);
+ // Check if the condition is expired.
+ if (
+ conditionExpr.expiredTime &&
+ new Date(conditionExpr.expiredTime).getTime() < Date.now()
+ ) {
+ return false;
+ }
+ // Check if the condition is valid for the database.
+ if (
+ conditionExpr.databaseResources &&
+ conditionExpr.databaseResources.length > 0
+ ) {
+ 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 {
- if (
- isUndefined(databaseResource.schema) ||
- (isUndefined(databaseResource.schema) &&
- isUndefined(databaseResource.table)) ||
- (databaseResource.schema === schema &&
- databaseResource.table === table)
- ) {
- return true;
- }
}
}
}
- return false;
- } else {
- return true;
}
+ return false;
+ } else {
+ return true;
}
- );
- };
-
- const allowToExportDatabaseV1 = (database: ComposedDatabase) => {
- if (hasWorkspaceSuperPrivilege.value) {
- return true;
}
-
- return checkProjectIAMPolicy(
- database.projectEntity,
- PresetRoleType.PROJECT_EXPORTER,
- (expr: Expr): boolean => {
- const conditionExpr = convertFromExpr(expr);
- if (
- conditionExpr.databaseResources &&
- conditionExpr.databaseResources.length > 0
- ) {
- const hasDatabaseField =
- conditionExpr.databaseResources.find(
- (item) => item.databaseName === database.name
- ) !== undefined;
- if (hasDatabaseField) {
- return true;
- }
- return false;
- } else {
- return true;
- }
- }
- );
- };
-
- return {
- allowToChangeDatabaseOfProject,
- allowToQueryDatabaseV1,
- allowToExportDatabaseV1,
- };
+ );
};
diff --git a/frontend/src/utils/v1/database.ts b/frontend/src/utils/v1/database.ts
index 9269d50040..5ada4a5668 100644
--- a/frontend/src/utils/v1/database.ts
+++ b/frontend/src/utils/v1/database.ts
@@ -1,7 +1,7 @@
import { orderBy } from "lodash-es";
import {
+ checkQuerierPermission,
hasFeature,
- useCurrentUserIamPolicy,
useSubscriptionV1Store,
} from "@/store";
import {
@@ -88,7 +88,11 @@ export const isDatabaseV1Alterable = (database: ComposedDatabase): boolean => {
};
// isDatabaseV1Queryable checks if database allowed to query in SQL Editor.
-export const isDatabaseV1Queryable = (database: ComposedDatabase): boolean => {
+export const isDatabaseV1Queryable = (
+ database: ComposedDatabase,
+ schema?: string,
+ table?: string
+): boolean => {
if (!hasFeature("bb.feature.access-control")) {
// The current plan doesn't have access control feature.
// Fallback to true.
@@ -99,8 +103,7 @@ export const isDatabaseV1Queryable = (database: ComposedDatabase): boolean => {
return true;
}
- const currentUserIamPolicy = useCurrentUserIamPolicy();
- if (currentUserIamPolicy.allowToQueryDatabaseV1(database)) {
+ if (checkQuerierPermission(database, schema, table)) {
return true;
}
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index 38d49f443a..cc5f03430d 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -74,7 +74,6 @@
</dl>
</div>
<div
- v-if="allowToChangeDatabase"
class="flex flex-row justify-start items-center flex-wrap shrink gap-x-2 gap-y-2"
data-label="bb-database-detail-action-buttons-container"
>
@@ -118,21 +117,21 @@
/>
</NTabPane>
<NTabPane
- v-if="allowToChangeDatabase && allowListChangeHistories"
+ v-if="allowListChangeHistories"
name="change-history"
:tab="$t('change-history.self')"
>
<DatabaseChangeHistoryPanel class="mt-2" :database="database" />
</NTabPane>
<NTabPane
- v-if="allowToChangeDatabase && allowListSlowQueries"
+ v-if="allowListSlowQueries"
name="slow-query"
:tab="$t('slow-query.slow-queries')"
>
<DatabaseSlowQueryPanel class="mt-2" :database="database" />
</NTabPane>
<NTabPane
- v-if="allowToChangeDatabase"
+ v-if="allowUpdateDatabase"
name="setting"
:tab="$t('common.settings')"
>
@@ -217,7 +216,6 @@ import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
import {
useAnomalyV1Store,
useAppFeature,
- useCurrentUserIamPolicy,
useDatabaseV1Store,
useEnvironmentV1Store,
} from "@/store";
@@ -272,9 +270,9 @@ const state = reactive<LocalState>({
selectedTab: "overview",
});
const route = useRoute();
-const currentUserIamPolicy = useCurrentUserIamPolicy();
const anomalyList = ref<Anomaly[]>([]);
const {
+ allowUpdateDatabase,
allowTransferDatabase,
allowChangeData,
allowAlterSchema,
@@ -320,12 +318,6 @@ const database = computed(() => {
});
const project = computed(() => database.value.projectEntity);
-const allowToChangeDatabase = computed(() => {
- return currentUserIamPolicy.allowToChangeDatabaseOfProject(
- project.value.name
- );
-});
-
const hasSchemaDiagramFeature = computed((): boolean => {
return instanceV1HasAlterSchema(database.value.instanceResource);
});
diff --git a/frontend/src/views/sql-editor/EditorCommon/BatchQueryDatabasesSelector.vue b/frontend/src/views/sql-editor/EditorCommon/BatchQueryDatabasesSelector.vue
index c6513f4962..d857d56951 100644
--- a/frontend/src/views/sql-editor/EditorCommon/BatchQueryDatabasesSelector.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/BatchQueryDatabasesSelector.vue
@@ -119,11 +119,11 @@ import {
hasFeature,
useAppFeature,
useConnectionOfCurrentSQLEditorTab,
- useCurrentUserIamPolicy,
useDatabaseV1Store,
useSQLEditorTabStore,
} from "@/store/modules";
import { isValidDatabaseName, type ComposedDatabase } from "@/types";
+import { isDatabaseV1Queryable } from "@/utils";
interface LocalState {
keyword: string;
@@ -133,7 +133,6 @@ interface LocalState {
const { t } = useI18n();
const databaseStore = useDatabaseV1Store();
const tabStore = useSQLEditorTabStore();
-const currentUserIamPolicy = useCurrentUserIamPolicy();
const state = reactive<LocalState>({
keyword: "",
showFeatureModal: false,
@@ -157,7 +156,7 @@ const databases = computed(() => {
// Don't show the currently selected database.
.filter((db) => db.name !== selectedDatabase.value.name)
// Only show databases that the user has permission to query.
- .filter((db) => currentUserIamPolicy.allowToQueryDatabaseV1(db))
+ .filter((db) => isDatabaseV1Queryable(db))
// Only show databases with same engine.
.filter(
(db) =>