Skip to content

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

feat(frontend): workspace settings for sensitive data (#3471)

Liu Ji2d9aa2fb3a81
diff --git a/api/policy.go b/api/policy.go
index cf681b75c8..9c1da9e06b 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -103,7 +103,7 @@ type Policy struct {
// Related fields
ResourceType PolicyResourceType
- ResourceID int
+ ResourceID int `jsonapi:"attr,resourceId"`
Environment *Environment `jsonapi:"relation,environment"`
// Domain specific fields
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index d03d9d8818..9813d2bd7a 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -209,7 +209,8 @@
"yes": "Yes",
"advanced": "Advanced",
"restart": "Restart",
- "want-help": "Want help"
+ "want-help": "Want help",
+ "operation": "Operation"
},
"error-page": {
"go-back-home": "Go back home"
@@ -326,7 +327,8 @@
"version-control": "Version Control",
"subscription": "Subscription",
"labels": "Labels",
- "debug-log": "Debug Log"
+ "debug-log": "Debug Log",
+ "sensitive-data": "Sensitive Data"
},
"profile": {
"email": "Email",
@@ -397,6 +399,10 @@
"logo-upload-tip": "{extension} up to {size} MiB",
"logo-upload-succeed": "Successfully updated the logo"
}
+ },
+ "sensitive-data": {
+ "description": "The query result of the following columns is displayed as \"******\". \nYou can mark more columns as sensitive data on the table details page.",
+ "remove-sensitive-column-tips": "Expose this column?"
}
},
"activity": {
@@ -880,6 +886,7 @@
"row-count-estimate": "Row count estimate",
"data-size": "Data size",
"index-size": "Index size",
+ "column": "Column",
"columns": "Columns",
"indexes": "Indexes",
"row-count-est": "Row count est.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 1163b68092..cd85fab9d1 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -209,7 +209,8 @@
"yes": "是",
"advanced": "高级",
"restart": "重启",
- "want-help": "加入用户群"
+ "want-help": "加入用户群",
+ "operation": "操作"
},
"error-page": {
"go-back-home": "回到主页"
@@ -326,7 +327,8 @@
"version-control": "版本控制",
"subscription": "订阅",
"labels": "标签",
- "debug-log": "Debug 日志"
+ "debug-log": "Debug 日志",
+ "sensitive-data": "敏感数据"
},
"profile": {
"email": "邮箱",
@@ -397,6 +399,10 @@
"logo-upload-tip": "支持 {extension} 类型文件,不能大于 {size} MiB",
"logo-upload-succeed": "Logo 更新完成"
}
+ },
+ "sensitive-data": {
+ "description": "以下列的查询结果将会被显示为 \"******\"。在表详情页,你可以将多个列标记为敏感数据。",
+ "remove-sensitive-column-tips": "不再对此列数据脱敏?"
}
},
"activity": {
@@ -880,6 +886,7 @@
"row-count-estimate": "估计行数",
"data-size": "数据大小",
"index-size": "索引大小",
+ "column": "列",
"columns": "列",
"indexes": "索引",
"row-count-est": "估计行数",
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 6a64dce1fa..604a831d67 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -352,6 +352,14 @@ const routes: Array<RouteRecordRaw> = [
import("../views/SettingWorkspaceIMIntegration.vue"),
props: true,
},
+ {
+ path: "sensitive-data",
+ name: "setting.workspace.sensitive-data",
+ meta: { title: () => t("settings.sidebar.sensitive-data") },
+ component: () =>
+ import("../views/SettingWorkspaceSensitiveData.vue"),
+ props: true,
+ },
{
path: "version-control",
name: "setting.workspace.version-control",
diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.ts
index e05ff5b016..c55b16e901 100644
--- a/frontend/src/store/modules/policy.ts
+++ b/frontend/src/store/modules/policy.ts
@@ -12,6 +12,7 @@ import {
import {
PipelineApprovalPolicyPayload,
Policy,
+ PolicyResourceType,
PolicyType,
PolicyUpsert,
SensitiveDataPolicyPayload,
@@ -92,6 +93,7 @@ export const usePolicyStore = defineStore("policy", {
state: (): PolicyState => ({
policyMapByEnvironmentId: new Map(),
policyMapByDatabaseId: new Map(),
+ policyMapByResourceType: new Map(),
}),
actions: {
getPolicyByEnvironmentIdAndType(
@@ -272,6 +274,48 @@ export const usePolicyStore = defineStore("policy", {
}
}
},
+
+ getPolicyListByResourceTypeAndPolicyType(
+ resourceType: PolicyResourceType,
+ policyType: PolicyType
+ ) {
+ const map = this.policyMapByResourceType.get(resourceType);
+ if (!map) return [];
+ return map.get(policyType) ?? [];
+ },
+ setPolicyListByResourceTypeAndPolicyType(
+ resourceType: PolicyResourceType,
+ policyType: PolicyType,
+ policyList: Policy[]
+ ) {
+ const map = this.policyMapByResourceType.get(resourceType);
+ if (map) {
+ map.set(policyType, policyList);
+ } else {
+ this.policyMapByResourceType.set(
+ resourceType,
+ new Map([[policyType, policyList]])
+ );
+ }
+ },
+ async fetchPolicyListByResourceTypeAndPolicyType(
+ resourceType: PolicyResourceType,
+ policyType: PolicyType
+ ) {
+ const url = `/api/policy?resourceType=${resourceType}&type=${policyType}`;
+ const data: { data: ResourceObject[]; included: ResourceObject[] } = (
+ await axios.get(url)
+ ).data;
+
+ const policyList = data.data.map((d) => convert(d, data.included));
+ this.setPolicyListByResourceTypeAndPolicyType(
+ resourceType,
+ policyType,
+ policyList
+ );
+
+ return policyList;
+ },
},
});
@@ -312,3 +356,25 @@ export const usePolicyByDatabaseAndType = (
)
);
};
+
+export const usePolicyListByResourceTypeAndPolicyType = (
+ params: Ref<{ resourceType: PolicyResourceType; policyType: PolicyType }>
+) => {
+ const store = usePolicyStore();
+ const currentUser = useCurrentUser();
+ watchEffect(() => {
+ if (currentUser.value.id === UNKNOWN_ID) return;
+
+ store.fetchPolicyListByResourceTypeAndPolicyType(
+ params.value.resourceType,
+ params.value.policyType
+ );
+ });
+
+ return computed(() =>
+ store.getPolicyListByResourceTypeAndPolicyType(
+ params.value.resourceType,
+ params.value.policyType
+ )
+ );
+};
diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.ts
index f17b064682..b9dc4f39b1 100644
--- a/frontend/src/types/policy.ts
+++ b/frontend/src/types/policy.ts
@@ -88,6 +88,14 @@ export type PolicyPayload =
| EnvironmentTierPolicyPayload
| SensitiveDataPolicyPayload;
+export type PolicyResourceType =
+ | ""
+ | "workspace"
+ | "environment"
+ | "project"
+ | "instance"
+ | "database";
+
export type Policy = {
id: PolicyId;
@@ -99,6 +107,8 @@ export type Policy = {
rowStatus: RowStatus;
// Related fields
+ resourceType: PolicyResourceType;
+ resourceId: number;
environment: Environment;
// Domain specific fields
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 8bc8bd88b0..63465f3e5c 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -10,6 +10,7 @@ import {
DBExtension,
Sheet,
OnboardingGuideType,
+ PolicyResourceType,
} from ".";
import { Activity } from "./activity";
import { ServerInfo } from "./actuator";
@@ -108,6 +109,7 @@ export interface TaskState {}
export interface PolicyState {
policyMapByEnvironmentId: Map<EnvironmentId, Map<PolicyType, Policy>>;
policyMapByDatabaseId: Map<DatabaseId, Map<PolicyType, Policy>>;
+ policyMapByResourceType: Map<PolicyResourceType, Map<PolicyType, Policy[]>>;
}
export interface ProjectState {
diff --git a/frontend/src/views/SettingSidebar.vue b/frontend/src/views/SettingSidebar.vue
index 89025dba80..ef18c87f33 100644
--- a/frontend/src/views/SettingSidebar.vue
+++ b/frontend/src/views/SettingSidebar.vue
@@ -67,6 +67,13 @@
class="outline-item group w-full flex items-center pl-11 pr-2 py-2"
>{{ $t("settings.sidebar.labels") }}</router-link
>
+ <router-link
+ v-if="showSensitiveDataItem"
+ to="/setting/sensitive-data"
+ class="outline-item group w-full flex items-center truncate pl-11 pr-2 py-2"
+ >
+ {{ $t("settings.sidebar.sensitive-data") }}
+ </router-link>
<router-link
v-if="showIMIntegrationItem"
to="/setting/im-integration"
@@ -137,6 +144,13 @@ const showProjectItem = computed((): boolean => {
);
});
+const showSensitiveDataItem = computed((): boolean => {
+ return hasWorkspacePermission(
+ "bb.permission.workspace.manage-database",
+ currentUser.value.role
+ );
+});
+
const showIMIntegrationItem = computed((): boolean => {
return hasWorkspacePermission(
"bb.permission.workspace.manage-im-integration",
diff --git a/frontend/src/views/SettingWorkspaceSensitiveData.vue b/frontend/src/views/SettingWorkspaceSensitiveData.vue
new file mode 100644
index 0000000000..516e92c36a
--- /dev/null
+++ b/frontend/src/views/SettingWorkspaceSensitiveData.vue
@@ -0,0 +1,214 @@
+<template>
+ <div class="w-full mt-4 space-y-4">
+ <div class="textinfolabel">
+ {{ $t("settings.sensitive-data.description") }}
+ </div>
+
+ <BBTable
+ :column-list="COLUMN_LIST"
+ :data-source="state.sensitiveColumnList"
+ :show-header="true"
+ :left-bordered="true"
+ :right-bordered="true"
+ :row-clickable="false"
+ >
+ <template #body="{ rowData: item }: { rowData: SensitiveColumn }">
+ <BBTableCell :left-padding="4" class="w-[15%]">
+ {{ item.column }}
+ </BBTableCell>
+ <BBTableCell class="w-[15%]">
+ {{ item.table }}
+ </BBTableCell>
+ <BBTableCell class="w-[15%]">
+ <div class="flex items-center space-x-2">
+ <span>{{ item.database.name }}</span>
+ </div>
+ </BBTableCell>
+ <BBTableCell class="w-[15%]">
+ <div class="flex flex-row items-center space-x-1">
+ <InstanceEngineIcon :instance="item.database.instance" />
+ <span class="flex-1 whitespace-pre-wrap">
+ {{ instanceName(item.database.instance) }}
+ </span>
+ </div>
+ </BBTableCell>
+ <BBTableCell class="w-[10%]">
+ <div class="flex items-center">
+ {{ environmentName(item.database.instance.environment) }}
+ <ProtectedEnvironmentIcon
+ class="ml-1"
+ :environment="item.database.instance.environment"
+ />
+ </div>
+ </BBTableCell>
+ <BBTableCell class="w-[15%]">
+ {{ projectName(item.database.project) }}
+ </BBTableCell>
+ <BBTableCell>
+ {{ humanizeTs(item.policy.updatedTs) }}
+ </BBTableCell>
+ <BBTableCell>
+ <div class="flex items-center justify-center">
+ <NPopconfirm @positive-click="removeSensitiveColumn(item)">
+ <template #trigger>
+ <button
+ :disabled="!allowAdmin"
+ class="w-5 h-5 p-0.5 bg-white hover:bg-control-bg-hover rounded cursor-pointer disabled:cursor-not-allowed disabled:hover:bg-white disabled:text-gray-400"
+ >
+ <heroicons-outline:trash />
+ </button>
+ </template>
+
+ <div class="whitespace-nowrap">
+ {{ $t("settings.sensitive-data.remove-sensitive-column-tips") }}
+ </div>
+ </NPopconfirm>
+ </div>
+ </BBTableCell>
+ </template>
+ </BBTable>
+ </div>
+
+ <FeatureModal
+ v-if="state.showFeatureModal"
+ feature="bb.feature.sensitive-data"
+ @cancel="state.showFeatureModal = false"
+ />
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, watchEffect } from "vue";
+import { useI18n } from "vue-i18n";
+import { NPopconfirm } from "naive-ui";
+
+import {
+ featureToRef,
+ useCurrentUser,
+ useDatabaseStore,
+ usePolicyListByResourceTypeAndPolicyType,
+ usePolicyStore,
+} from "@/store";
+import { Database, Policy, SensitiveDataPolicyPayload } from "@/types";
+import { BBTableColumn } from "@/bbkit/types";
+import { hasWorkspacePermission } from "@/utils";
+
+type SensitiveColumn = {
+ database: Database;
+ policy: Policy;
+ table: string;
+ column: string;
+};
+interface LocalState {
+ showFeatureModal: boolean;
+ isLoading: boolean;
+ sensitiveColumnList: SensitiveColumn[];
+}
+
+const { t } = useI18n();
+const state = reactive<LocalState>({
+ showFeatureModal: false,
+ isLoading: false,
+ sensitiveColumnList: [],
+});
+const databaseStore = useDatabaseStore();
+const hasSensitiveDataFeature = featureToRef("bb.feature.sensitive-data");
+
+const currentUser = useCurrentUser();
+const allowAdmin = computed(() => {
+ return hasWorkspacePermission(
+ "bb.permission.workspace.manage-database",
+ currentUser.value.role
+ );
+});
+
+const policyList = usePolicyListByResourceTypeAndPolicyType(
+ computed(() => ({
+ resourceType: "database",
+ policyType: "bb.policy.sensitive-data",
+ }))
+);
+
+const updateList = async () => {
+ state.isLoading = true;
+ const sensitiveColumnList: SensitiveColumn[] = [];
+ for (let i = 0; i < policyList.value.length; i++) {
+ const policy = policyList.value[i];
+ const payload = policy.payload as SensitiveDataPolicyPayload;
+
+ const databaseId = policy.resourceId;
+ const database = await databaseStore.getOrFetchDatabaseById(databaseId);
+
+ for (let j = 0; j < payload.sensitiveDataList.length; j++) {
+ const { table, column } = payload.sensitiveDataList[j];
+ sensitiveColumnList.push({ database, policy, table, column });
+ }
+ }
+ state.sensitiveColumnList = sensitiveColumnList;
+ state.isLoading = false;
+};
+
+watchEffect(updateList);
+
+const removeSensitiveColumn = (sensitiveColumn: SensitiveColumn) => {
+ if (!hasSensitiveDataFeature.value) {
+ state.showFeatureModal = true;
+ return;
+ }
+
+ const { database, table, column } = sensitiveColumn;
+ const policy = policyList.value.find(
+ (policy) => policy.resourceId === sensitiveColumn.database.id
+ );
+ if (!policy) return;
+
+ const payload = policy.payload as SensitiveDataPolicyPayload;
+ const index = payload.sensitiveDataList.findIndex(
+ (sensitiveData) =>
+ sensitiveData.table === table && sensitiveData.column === column
+ );
+ if (index >= 0) {
+ // mutate the list and the item directly
+ // so we don't need to re-fetch the whole list.
+ payload.sensitiveDataList.splice(index, 1);
+
+ usePolicyStore().upsertPolicyByDatabaseAndType({
+ databaseId: database.id,
+ type: "bb.policy.sensitive-data",
+ policyUpsert: {
+ payload,
+ },
+ });
+ }
+ updateList();
+};
+
+const COLUMN_LIST = computed((): BBTableColumn[] => [
+ {
+ title: t("database.column"),
+ },
+ {
+ title: t("common.table"),
+ },
+ {
+ title: t("common.database"),
+ },
+ {
+ title: t("common.instance"),
+ },
+ {
+ title: t("common.environment"),
+ },
+ {
+ title: t("common.project"),
+ },
+ {
+ title: t("common.updated-at"),
+ nowrap: true,
+ },
+ {
+ title: t("common.operation"),
+ center: true,
+ nowrap: true,
+ },
+]);
+</script>