Skip to content

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

chore: support set algorithms for the semantic type (#14817)

ecmadao15e99e1bafaf
diff --git a/frontend/src/components/SensitiveData/MaskingAlgorithmsView.vue b/frontend/src/components/SensitiveData/MaskingAlgorithmsView.vue
index 58969fc207..ed5c2b29a6 100644
--- a/frontend/src/components/SensitiveData/MaskingAlgorithmsView.vue
+++ b/frontend/src/components/SensitiveData/MaskingAlgorithmsView.vue
@@ -51,6 +51,7 @@
:show="state.showCreateDrawer"
:algorithm="state.pendingEditData"
:readonly="!hasPermission || !hasSensitiveDataFeature"
+ @apply="onAlgorithmUpsert"
@dismiss="onDrawerDismiss"
/>
<DataExampleModal
@@ -122,6 +123,47 @@ const onDrawerDismiss = () => {
state.showCreateDrawer = false;
};
+const algorithmList = computed((): Algorithm[] => {
+ return (
+ settingStore.getSettingByName("bb.workspace.masking-algorithm")?.value
+ ?.maskingAlgorithmSettingValue?.algorithms ?? []
+ );
+});
+
+const onAlgorithmUpsert = async (maskingAlgorithm: Algorithm) => {
+ state.processing = true;
+
+ const index = algorithmList.value.findIndex(
+ (item) => item.id === maskingAlgorithm.id
+ );
+ const newList = [...algorithmList.value];
+ if (index < 0) {
+ newList.push({ ...maskingAlgorithm });
+ } else {
+ newList[index] = { ...maskingAlgorithm };
+ }
+
+ try {
+ await settingStore.upsertSetting({
+ name: "bb.workspace.masking-algorithm",
+ value: {
+ maskingAlgorithmSettingValue: {
+ algorithms: newList,
+ },
+ },
+ });
+
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("common.updated"),
+ });
+ onDrawerDismiss();
+ } finally {
+ state.processing = false;
+ }
+};
+
const onEdit = (data: Algorithm) => {
state.pendingEditData = data;
state.showCreateDrawer = true;
diff --git a/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue b/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue
index 242ca35256..36782f915f 100644
--- a/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue
+++ b/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue
@@ -1,6 +1,8 @@
<template>
<Drawer :show="show" @close="$emit('dismiss')">
- <DrawerContent :title="$t('common.add')">
+ <DrawerContent
+ :title="algorithm.title ? $t('common.edit') : $t('common.add')"
+ >
<div
class="w-[40rem] max-w-[calc(100vw-5rem)] space-y-6 divide-y divide-block-border"
>
@@ -206,22 +208,14 @@
v-model:value="state.innerOuterMask.type"
:disabled="state.processing || readonly"
>
- <NRadio
- :value="
- Algorithm_InnerOuterMask_MaskType.INNER
- "
- >
+ <NRadio :value="Algorithm_InnerOuterMask_MaskType.INNER">
{{
$t(
"settings.sensitive-data.algorithms.inner-outer-mask.inner-mask"
)
}}
</NRadio>
- <NRadio
- :value="
- Algorithm_InnerOuterMask_MaskType.OUTER
- "
- >
+ <NRadio :value="Algorithm_InnerOuterMask_MaskType.OUTER">
{{
$t(
"settings.sensitive-data.algorithms.inner-outer-mask.outer-mask"
@@ -307,7 +301,7 @@
v-if="!readonly"
:disabled="isSubmitDisabled"
type="primary"
- @click.prevent="onUpsert"
+ @click.prevent="() => $emit('apply', maskingAlgorithm)"
>
{{ $t("common.confirm") }}
</NButton>
@@ -340,7 +334,6 @@ import {
RadioGrid,
MiniActionButton,
} from "@/components/v2";
-import { pushNotification, useSettingV1Store } from "@/store";
import {
Algorithm,
Algorithm_FullMask as FullMask,
@@ -375,14 +368,15 @@ const props = defineProps<{
algorithm: Algorithm;
}>();
-const emit = defineEmits<{
+defineEmits<{
(event: "dismiss"): void;
+ (event: "apply", algorithm: Algorithm): void;
}>();
const defaultRangeMask = computed(() =>
RangeMask.fromPartial({
slices: [
- Algorithm_RangeMask_Slice.fromPartial({
+ Algorithm_RangeMask_Slice.fromPartial({
start: 0,
end: 1,
substitution: "*",
@@ -412,7 +406,6 @@ const state = reactive<LocalState>({
});
const { t } = useI18n();
-const settingStore = useSettingV1Store();
const maskingTypeList = computed((): MaskingTypeOption[] => [
{
@@ -433,13 +426,6 @@ const maskingTypeList = computed((): MaskingTypeOption[] => [
},
]);
-const algorithmList = computed((): Algorithm[] => {
- return (
- settingStore.getSettingByName("bb.workspace.masking-algorithm")?.value
- ?.maskingAlgorithmSettingValue?.algorithms ?? []
- );
-});
-
watch(
() => props.algorithm,
(algorithm) => {
@@ -562,40 +548,6 @@ const isSubmitDisabled = computed(() => {
return !!errorMessage.value;
});
-const onUpsert = async () => {
- state.processing = true;
-
- const index = algorithmList.value.findIndex(
- (item) => item.id === maskingAlgorithm.value.id
- );
- const newList = [...algorithmList.value];
- if (index < 0) {
- newList.push(maskingAlgorithm.value);
- } else {
- newList[index] = maskingAlgorithm.value;
- }
-
- try {
- await settingStore.upsertSetting({
- name: "bb.workspace.masking-algorithm",
- value: {
- maskingAlgorithmSettingValue: {
- algorithms: newList,
- },
- },
- });
-
- pushNotification({
- module: "bytebase",
- style: "SUCCESS",
- title: t("common.updated"),
- });
- emit("dismiss");
- } finally {
- state.processing = false;
- }
-};
-
const onMaskingTypeChange = (maskingType: MaskingType) => {
if (state.processing || props.readonly) {
return;
diff --git a/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue b/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue
index c81dac7370..5b38d60495 100644
--- a/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue
+++ b/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue
@@ -1,153 +1,37 @@
<template>
- <BBGrid
- :column-list="columnList"
- :data-source="semanticItemList"
- :row-clickable="rowClickable"
- class="border compact"
- @click-row="(item: SemanticItem) => $emit('select', item.item.id)"
- >
- <template #item="{ item, row }: SemanticItemRow">
- <div class="bb-grid-cell">
- <h3 v-if="item.mode === 'NORMAL'" class="break-normal">
- {{ item.item.title }}
- </h3>
- <NInput
- v-else
- :value="item.item.title"
- size="small"
- type="text"
- :placeholder="
- $t('settings.sensitive-data.semantic-types.table.semantic-type')
- "
- @input="
- (val: string) => onInput(row, (data) => (data.item.title = val))
- "
- />
- </div>
- <div class="bb-grid-cell">
- <h3 v-if="item.mode === 'NORMAL'">
- {{ item.item.description }}
- </h3>
- <NInput
- v-else
- :value="item.item.description"
- size="small"
- type="text"
- :placeholder="
- $t('settings.sensitive-data.semantic-types.table.description')
- "
- @input="
- (val: string) =>
- onInput(row, (data) => (data.item.description = val))
- "
- />
- </div>
- <div class="bb-grid-cell">
- <h3 v-if="item.mode === 'NORMAL'">
- {{
- getAlgorithmById(item.item.fullMaskAlgorithmId)?.label ??
- $t("settings.sensitive-data.algorithms.default")
- }}
- </h3>
- <NSelect
- v-else
- :value="item.item.fullMaskAlgorithmId"
- :options="algorithmList"
- :consistent-menu-width="false"
- :placeholder="$t('settings.sensitive-data.algorithms.default')"
- :fallback-option="
- (_: string) => ({
- label: $t('settings.sensitive-data.algorithms.default'),
- value: '',
- })
- "
- clearable
- size="small"
- style="min-width: 7rem; width: auto; overflow-x: hidden"
- @update:value="
- onInput(row, (data) => (data.item.fullMaskAlgorithmId = $event))
- "
- />
- </div>
- <div class="bb-grid-cell">
- <h3 v-if="item.mode === 'NORMAL'">
- {{
- getAlgorithmById(item.item.partialMaskAlgorithmId)?.label ??
- $t("settings.sensitive-data.algorithms.default")
- }}
- </h3>
- <NSelect
- v-else
- :value="item.item.partialMaskAlgorithmId"
- :options="algorithmList"
- :consistent-menu-width="false"
- :placeholder="$t('settings.sensitive-data.algorithms.default')"
- :fallback-option="
- (_: string) => ({
- label: $t('settings.sensitive-data.algorithms.default'),
- value: '',
- })
- "
- clearable
- size="small"
- style="min-width: 7rem; width: auto; overflow-x: hidden"
- @update:value="
- onInput(row, (data) => (data.item.partialMaskAlgorithmId = $event))
- "
- />
- </div>
- <div v-if="!props.readonly" class="bb-grid-cell justify-end">
- <NPopconfirm
- v-if="item.mode === 'EDIT'"
- @positive-click="$emit('remove', row)"
- >
- <template #trigger>
- <MiniActionButton tag="div" @click.stop="">
- <TrashIcon class="w-4 h-4" />
- </MiniActionButton>
- </template>
- <div class="whitespace-nowrap">
- {{ $t("settings.sensitive-data.semantic-types.table.delete") }}
- </div>
- </NPopconfirm>
-
- <MiniActionButton
- v-if="item.mode !== 'NORMAL'"
- @click="$emit('cancel', row)"
- >
- <Undo2Icon class="w-4 h-4" />
- </MiniActionButton>
- <MiniActionButton
- v-if="item.mode !== 'NORMAL'"
- type="primary"
- :disabled="isConfirmDisabled(item)"
- @click.stop="$emit('confirm', row)"
- >
- <CheckIcon class="w-4 h-4" />
- </MiniActionButton>
-
- <MiniActionButton
- v-if="item.mode === 'NORMAL'"
- @click.stop="item.mode = 'EDIT'"
- >
- <PencilIcon class="w-4 h-4" />
- </MiniActionButton>
- </div>
- </template>
- </BBGrid>
+ <NDataTable
+ :data="semanticItemList"
+ :columns="columnList"
+ :striped="true"
+ :bordered="true"
+ :row-props="rowProps"
+ :row-key="(item: SemanticItem) => item.item.id"
+ />
+
+ <MaskingAlgorithmsCreateDrawer
+ :show="state.showAlgorithmDrawer"
+ :algorithm="state.pendingEditAlgorithm"
+ :readonly="readonly"
+ @apply="onAlgorithmUpsert"
+ @dismiss="onDrawerDismiss"
+ />
</template>
-<script lang="ts" setup>
+<script lang="tsx" setup>
import { CheckIcon, PencilIcon, TrashIcon, Undo2Icon } from "lucide-vue-next";
-import { NPopconfirm, NSelect, NInput } from "naive-ui";
-import type { SelectOption } from "naive-ui";
-import { computed } from "vue";
+import { NPopconfirm, NSelect, NInput, NDataTable } from "naive-ui";
+import type { SelectOption, DataTableColumn } from "naive-ui";
+import { v4 as uuidv4 } from "uuid";
+import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
-import { BBGrid } from "@/bbkit";
-import type { BBGridColumn, BBGridRow } from "@/bbkit/types";
import { MiniActionButton } from "@/components/v2";
import { useSettingV1Store } from "@/store";
-import type { SemanticTypeSetting_SemanticType } from "@/types/proto/v1/setting_service";
+import {
+ Algorithm,
+ type SemanticTypeSetting_SemanticType,
+} from "@/types/proto/v1/setting_service";
+import { isDev } from "@/utils";
+import MaskingAlgorithmsCreateDrawer from "./MaskingAlgorithmsCreateDrawer.vue";
type SemanticItemMode = "NORMAL" | "CREATE" | "EDIT";
@@ -157,7 +41,12 @@ export interface SemanticItem {
item: SemanticTypeSetting_SemanticType;
}
-type SemanticItemRow = BBGridRow<SemanticItem>;
+interface LocalState {
+ showAlgorithmDrawer: boolean;
+ pendingEditAlgorithm: Algorithm;
+ pendingEditSemanticIndex?: number;
+ processing: boolean;
+}
const props = defineProps<{
readonly: boolean;
@@ -165,49 +54,264 @@ const props = defineProps<{
rowClickable: boolean;
}>();
-defineEmits<{
+const emit = defineEmits<{
(event: "select", id: string): void;
(event: "remove", index: number): void;
(event: "cancel", index: number): void;
(event: "confirm", index: number): void;
}>();
+const state = reactive<LocalState>({
+ showAlgorithmDrawer: false,
+ pendingEditAlgorithm: Algorithm.fromPartial({
+ id: uuidv4(),
+ }),
+ processing: false,
+});
+
const { t } = useI18n();
const settingStore = useSettingV1Store();
+const onDrawerDismiss = () => {
+ state.pendingEditSemanticIndex = undefined;
+ state.showAlgorithmDrawer = false;
+};
+
+const onAlgorithmUpsert = async (maskingAlgorithm: Algorithm) => {
+ if (state.pendingEditSemanticIndex === undefined) {
+ return;
+ }
+ onInput(
+ state.pendingEditSemanticIndex,
+ (data) => (data.item.algorithms = maskingAlgorithm)
+ );
+ emit("confirm", state.pendingEditSemanticIndex);
+ onDrawerDismiss();
+};
+
const columnList = computed(() => {
- const columns: BBGridColumn[] = [
+ const columns: DataTableColumn<SemanticItem>[] = [
{
+ key: "title",
title: t("settings.sensitive-data.semantic-types.table.semantic-type"),
- width: "minmax(min-content, auto)",
+ render: (item, row) => {
+ if (item.mode === "NORMAL") {
+ return <h3 class="break-normal">{item.item.title}</h3>;
+ }
+ return (
+ <NInput
+ value={item.item.title}
+ size="small"
+ type="text"
+ placeholder={t(
+ "settings.sensitive-data.semantic-types.table.semantic-type"
+ )}
+ onInput={(val: string) =>
+ onInput(row, (data) => (data.item.title = val))
+ }
+ />
+ );
+ },
},
{
+ key: "description",
title: t("settings.sensitive-data.semantic-types.table.description"),
width: "minmax(min-content, auto)",
+ render: (item, row) => {
+ if (item.mode === "NORMAL") {
+ return <h3>{item.item.description}</h3>;
+ }
+ return (
+ <NInput
+ value={item.item.description}
+ size="small"
+ type="text"
+ placeholder={t(
+ "settings.sensitive-data.semantic-types.table.description"
+ )}
+ onInput={(val: string) =>
+ onInput(row, (data) => (data.item.description = val))
+ }
+ />
+ );
+ },
},
- {
- title: t(
- "settings.sensitive-data.semantic-types.table.full-masking-algorithm"
- ),
- width: "minmax(min-content, auto)",
- },
- {
+ ];
+ if (isDev()) {
+ columns.push({
+ key: "algorithm",
title: t(
- "settings.sensitive-data.semantic-types.table.partial-masking-algorithm"
+ "settings.sensitive-data.semantic-types.table.masking-algorithm"
),
width: "minmax(min-content, auto)",
- },
- ];
+ render: (item, row) => {
+ return (
+ <div class="flex items-center space-x-1">
+ <h3>
+ {item.item.algorithms?.title ??
+ t("settings.sensitive-data.algorithms.default")}
+ </h3>
+ {!props.readonly && (
+ <MiniActionButton
+ onClick={() => {
+ state.pendingEditAlgorithm =
+ item.item.algorithms ??
+ Algorithm.fromPartial({
+ id: uuidv4(),
+ });
+ state.pendingEditSemanticIndex = row;
+ state.showAlgorithmDrawer = true;
+ }}
+ >
+ <PencilIcon class="w-4 h-4" />
+ </MiniActionButton>
+ )}
+ </div>
+ );
+ },
+ });
+ } else {
+ columns.push(
+ {
+ key: "full-masking-algorithm",
+ title: t(
+ "settings.sensitive-data.semantic-types.table.full-masking-algorithm"
+ ),
+ width: "minmax(min-content, auto)",
+ render: (item, row) => {
+ if (item.mode === "NORMAL") {
+ return (
+ <h3>
+ {getAlgorithmById(item.item.fullMaskAlgorithmId)?.label ??
+ t("settings.sensitive-data.algorithms.default")}
+ </h3>
+ );
+ }
+
+ return (
+ <NSelect
+ value={item.item.fullMaskAlgorithmId}
+ options={algorithmList.value}
+ consistentMenuWidth={false}
+ placeholder={t("settings.sensitive-data.algorithms.default")}
+ fallbackOption={(_: string) => ({
+ label: t("settings.sensitive-data.algorithms.default"),
+ value: "",
+ })}
+ clearable
+ size="small"
+ style="min-width: 7rem; width: auto; overflow-x: hidden"
+ onUpdateValue={(val) =>
+ onInput(row, (data) => (data.item.fullMaskAlgorithmId = val))
+ }
+ />
+ );
+ },
+ },
+ {
+ key: "partial-masking-algorithm",
+ title: t(
+ "settings.sensitive-data.semantic-types.table.partial-masking-algorithm"
+ ),
+ width: "minmax(min-content, auto)",
+ render: (item, row) => {
+ if (item.mode === "NORMAL") {
+ return (
+ <h3>
+ {getAlgorithmById(item.item.partialMaskAlgorithmId)?.label ??
+ t("settings.sensitive-data.algorithms.default")}
+ </h3>
+ );
+ }
+
+ return (
+ <NSelect
+ value={item.item.partialMaskAlgorithmId}
+ options={algorithmList.value}
+ consistentMenuWidth={false}
+ placeholder={t("settings.sensitive-data.algorithms.default")}
+ fallbackOption={(_: string) => ({
+ label: t("settings.sensitive-data.algorithms.default"),
+ value: "",
+ })}
+ clearable
+ size="small"
+ style="min-width: 7rem; width: auto; overflow-x: hidden"
+ onUpdateValue={(val) =>
+ onInput(row, (data) => (data.item.partialMaskAlgorithmId = val))
+ }
+ />
+ );
+ },
+ }
+ );
+ }
if (!props.readonly) {
// operation.
columns.push({
+ key: "operation",
title: "",
width: "minmax(min-content, auto)",
+ render: (item, row) => {
+ return (
+ <div>
+ {item.mode === "EDIT" && (
+ <NPopconfirm onPositiveClick={() => emit("remove", row)}>
+ {{
+ trigger: () => {
+ return (
+ <MiniActionButton>
+ <TrashIcon class="w-4 h-4" />
+ </MiniActionButton>
+ );
+ },
+ default: () => (
+ <div class="whitespace-nowrap">
+ {t("settings.sensitive-data.semantic-types.table.delete")}
+ </div>
+ ),
+ }}
+ </NPopconfirm>
+ )}
+ {item.mode !== "NORMAL" && (
+ <MiniActionButton onClick={() => emit("cancel", row)}>
+ <Undo2Icon class="w-4 h-4" />
+ </MiniActionButton>
+ )}
+ {item.mode !== "NORMAL" && (
+ <MiniActionButton
+ type={"primary"}
+ disabled={isConfirmDisabled(item)}
+ onClick={() => emit("confirm", row)}
+ >
+ <CheckIcon class="w-4 h-4" />
+ </MiniActionButton>
+ )}
+ {item.mode === "NORMAL" && (
+ <MiniActionButton onClick={() => (item.mode = "EDIT")}>
+ <PencilIcon class="w-4 h-4" />
+ </MiniActionButton>
+ )}
+ </div>
+ );
+ },
});
}
return columns;
});
+const rowProps = (item: SemanticItem) => {
+ return {
+ style: props.rowClickable ? "cursor: pointer;" : "",
+ onClick: () => {
+ if (!props.rowClickable) {
+ return;
+ }
+ emit("select", item.item.id);
+ },
+ };
+};
+
const onInput = (index: number, callback: (item: SemanticItem) => void) => {
const item = props.semanticItemList[index];
if (!item) {
diff --git a/frontend/src/components/v2/Button/MiniActionButton.vue b/frontend/src/components/v2/Button/MiniActionButton.vue
index 9e87948fb2..c384f9d5b6 100644
--- a/frontend/src/components/v2/Button/MiniActionButton.vue
+++ b/frontend/src/components/v2/Button/MiniActionButton.vue
@@ -1,5 +1,13 @@
<template>
- <NButton quaternary size="tiny" style="--n-padding: 4px" :disabled="disabled">
+ <NButton
+ quaternary
+ size="tiny"
+ :type="type"
+ style="--n-padding: 4px"
+ :disabled="disabled"
+ v-bind="$attrs"
+ @click="$emit('click', $event)"
+ >
<template #icon>
<slot />
</template>
@@ -14,5 +22,17 @@ import { NButton } from "naive-ui";
defineProps<{
disabled?: boolean;
+ type?:
+ | "default"
+ | "tertiary"
+ | "primary"
+ | "success"
+ | "info"
+ | "warning"
+ | "error";
+}>();
+
+defineEmits<{
+ (event: "click", e: MouseEvent): void;
}>();
</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index a1b671c7db..f0e3998ae9 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -831,6 +831,7 @@
"table": {
"semantic-type": "Semantic Type",
"description": "Description",
+ "masking-algorithm": "Masking Algorithm",
"full-masking-algorithm": "Full Masking Algorithm",
"partial-masking-algorithm": "Partial Masking Algorithm",
"delete": "Delete this semantic type?"
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index a6bcb6f706..7db1679d81 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -831,6 +831,7 @@
"table": {
"semantic-type": "Tipo Semántico",
"description": "Descripción",
+ "masking-algorithm": "Algoritmo de enmascaramiento",
"full-masking-algorithm": "Algoritmo De Enmascaramiento Completo",
"partial-masking-algorithm": "Algoritmo De Enmascaramiento Parcial",
"delete": "¿Eliminar este tipo semántico?"
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 11aaf77ff4..eaf07034b5 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -831,6 +831,7 @@
"table": {
"semantic-type": "セマンティックタイプ",
"description": "説明する",
+ "masking-algorithm": "マスキングアルゴリズム",
"full-masking-algorithm": "完全マスクのアルゴリズム",
"partial-masking-algorithm": "部分マスクのアルゴリズム",
"delete": "このセマンティック タイプを削除しますか?"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index d4c83cd0f8..c41c105bb9 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -831,6 +831,7 @@
"table": {
"semantic-type": "语义类型",
"description": "描述",
+ "masking-algorithm": "脱敏算法",
"full-masking-algorithm": "全脱敏算法",
"partial-masking-algorithm": "半脱敏算法",
"delete": "删除这个语义类型?"