Skip to content

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

feat(frontend): setting table columns' sensitive data mask policy (#3466)

Liu Ji2a41e30e182e
diff --git a/frontend/src/bbkit/BBTable/BBTable.vue b/frontend/src/bbkit/BBTable/BBTable.vue
index 4b41de0855..690bcf7d21 100644
--- a/frontend/src/bbkit/BBTable/BBTable.vue
+++ b/frontend/src/bbkit/BBTable/BBTable.vue
@@ -18,6 +18,7 @@
headerClass,
index == 0 ? 'pl-4' : 'pl-2',
column.center && 'text-center pr-2',
+ column.nowrap && 'whitespace-nowrap',
]"
>
{{ column.title }}
diff --git a/frontend/src/bbkit/types.ts b/frontend/src/bbkit/types.ts
index 6f5a1a3209..8895488ba4 100644
--- a/frontend/src/bbkit/types.ts
+++ b/frontend/src/bbkit/types.ts
@@ -16,6 +16,7 @@ export type BBButtonConfirmStyle =
export type BBTableColumn = {
title: string;
center?: boolean;
+ nowrap?: boolean;
};
export type BBTableSectionDataSource<T> = {
diff --git a/frontend/src/components/ColumnTable.vue b/frontend/src/components/ColumnTable.vue
index 5670c1fef4..5cf9686375 100644
--- a/frontend/src/components/ColumnTable.vue
+++ b/frontend/src/components/ColumnTable.vue
@@ -5,8 +5,25 @@
:show-header="true"
:left-bordered="true"
:right-bordered="true"
+ v-bind="$attrs"
>
<template #body="{ rowData: column }">
+ <BBTableCell :left-padding="4" class="w-[1%] text-center">
+ <!-- width: 1% means as narrow as possible -->
+ <input
+ type="checkbox"
+ class="h-4 w-4 text-accent rounded disabled:cursor-not-allowed border-control-border focus:ring-accent"
+ :disabled="!allowAdmin"
+ :checked="isSensitiveColumn(column)"
+ @input="
+ toggleSensitiveColumn(
+ column,
+ ($event.target as HTMLInputElement).checked,
+ $event
+ )
+ "
+ />
+ </BBTableCell>
<BBTableCell :left-padding="4" class="w-16">
{{ column.name }}
</BBTableCell>
@@ -40,17 +57,44 @@
</BBTableCell>
</template>
</BBTable>
+
+ <FeatureModal
+ v-if="state.showFeatureModal"
+ feature="bb.feature.sensitive-data"
+ @cancel="state.showFeatureModal = false"
+ />
</template>
<script lang="ts">
-import { computed, defineComponent, PropType } from "vue";
-import { Column, EngineType } from "../types";
+import { computed, defineComponent, PropType, reactive } from "vue";
+import {
+ Column,
+ Database,
+ EngineType,
+ SensitiveData,
+ SensitiveDataPolicyPayload,
+ Table,
+} from "../types";
import { useI18n } from "vue-i18n";
+import { cloneDeep } from "lodash-es";
+import { featureToRef, useCurrentUser, usePolicyStore } from "@/store";
+import { hasWorkspacePermission } from "@/utils";
+
+type LocalState = {
+ showFeatureModal: boolean;
+};
export default defineComponent({
name: "ColumnTable",
- components: {},
props: {
+ database: {
+ required: true,
+ type: Object as PropType<Database>,
+ },
+ table: {
+ required: true,
+ type: Object as PropType<Table>,
+ },
columnList: {
required: true,
type: Object as PropType<Column[]>,
@@ -59,11 +103,33 @@ export default defineComponent({
required: true,
type: String as PropType<EngineType>,
},
+ sensitiveDataList: {
+ required: true,
+ type: Array as PropType<SensitiveData[]>,
+ },
},
setup(props) {
const { t } = useI18n();
+ const state = reactive<LocalState>({
+ showFeatureModal: false,
+ });
+
+ const hasSensitiveDataFeature = featureToRef("bb.feature.sensitive-data");
+
+ const currentUser = useCurrentUser();
+ const allowAdmin = computed(() => {
+ return hasWorkspacePermission(
+ "bb.permission.workspace.manage-database",
+ currentUser.value.role
+ );
+ });
const NORMAL_COLUMN_LIST = computed(() => [
+ {
+ title: t("database.sensitive"),
+ center: true,
+ nowrap: true,
+ },
{
title: t("common.name"),
},
@@ -87,6 +153,10 @@ export default defineComponent({
},
]);
const POSTGRES_COLUMN_LIST = computed(() => [
+ {
+ title: t("database.sensitive"),
+ center: true,
+ },
{
title: t("common.name"),
},
@@ -107,6 +177,10 @@ export default defineComponent({
},
]);
const CLICKHOUSE_SNOWFLAKE_COLUMN_LIST = computed(() => [
+ {
+ title: t("database.sensitive"),
+ center: true,
+ },
{
title: t("common.name"),
},
@@ -136,8 +210,63 @@ export default defineComponent({
}
});
+ const isSensitiveColumn = (column: Column) => {
+ return (
+ props.sensitiveDataList.findIndex((sensitiveData) => {
+ return (
+ sensitiveData.table === props.table.name &&
+ sensitiveData.column === column.name
+ );
+ }) >= 0
+ );
+ };
+
+ const toggleSensitiveColumn = (column: Column, on: boolean, e: Event) => {
+ if (!hasSensitiveDataFeature.value) {
+ state.showFeatureModal = true;
+
+ // Revert UI states
+ e.preventDefault();
+ e.stopPropagation();
+ (e.target as HTMLInputElement).checked = !on;
+ return;
+ }
+
+ const index = props.sensitiveDataList.findIndex((sensitiveData) => {
+ return (
+ sensitiveData.table === props.table.name &&
+ sensitiveData.column === column.name
+ );
+ });
+ const sensitiveDataList = cloneDeep(props.sensitiveDataList);
+ if (on && index < 0) {
+ // Turn on sensitive
+ sensitiveDataList.push({
+ table: props.table.name,
+ column: column.name,
+ maskType: "DEFAULT",
+ });
+ } else if (!on && index >= 0) {
+ sensitiveDataList.splice(index, 1);
+ }
+ const payload: SensitiveDataPolicyPayload = {
+ sensitiveDataList,
+ };
+ usePolicyStore().upsertPolicyByDatabaseAndType({
+ databaseId: props.database.id,
+ type: "bb.policy.sensitive-data",
+ policyUpsert: {
+ payload,
+ },
+ });
+ };
+
return {
+ state,
columnNameList,
+ allowAdmin,
+ isSensitiveColumn,
+ toggleSensitiveColumn,
};
},
});
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 3be5ee0a15..d03d9d8818 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -936,7 +936,8 @@
"more-version": "more...",
"character-sets-diff-found": "There are differences between character sets.",
"preview-issue": "Preview issue"
- }
+ },
+ "sensitive": "Sensitive"
},
"repository": {
"branch-observe-file-change": "The branch where Bytebase observes the file change.",
diff --git a/frontend/src/locales/subscription/en-US.json b/frontend/src/locales/subscription/en-US.json
index 11c868f7b9..cdf1bbf5e5 100644
--- a/frontend/src/locales/subscription/en-US.json
+++ b/frontend/src/locales/subscription/en-US.json
@@ -107,7 +107,8 @@
"sync-members-from-vcs": "Sync project membership from the linked GitLab project",
"dba-workflow": "DBA workflow",
"dba-workflow-tooltip": "Only allow DBA and Owner to create or transfer database",
- "environment-tier": "Environment tier"
+ "environment-tier": "Environment tier",
+ "sensitive-data": "Sensitive data masking"
}
},
"branding": {
diff --git a/frontend/src/locales/subscription/zh-CN.json b/frontend/src/locales/subscription/zh-CN.json
index 464bf714d5..240a512455 100644
--- a/frontend/src/locales/subscription/zh-CN.json
+++ b/frontend/src/locales/subscription/zh-CN.json
@@ -107,7 +107,8 @@
"sync-members-from-vcs": "从 GitLab 同步成员信息",
"dba-workflow": "DBA 工作流",
"dba-workflow-tooltip": "仅允许 DBA 和所有者创建或者转移数据库",
- "environment-tier": "环境分级"
+ "environment-tier": "环境分级",
+ "sensitive-data": "数据脱敏"
}
},
"branding": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 3f8f198d97..1163b68092 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -936,7 +936,8 @@
"more-version": "更多...",
"character-sets-diff-found": "Schema 之间的字符集不同",
"preview-issue": "预览工单"
- }
+ },
+ "sensitive": "敏感"
},
"repository": {
"branch-observe-file-change": "Bytebase 跟踪文件变更的分支。",
diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.ts
index 76d7d426a4..e05ff5b016 100644
--- a/frontend/src/store/modules/policy.ts
+++ b/frontend/src/store/modules/policy.ts
@@ -1,7 +1,7 @@
import { defineStore } from "pinia";
import axios from "axios";
import {
- Environment,
+ DatabaseId,
EnvironmentId,
PolicyState,
ResourceIdentifier,
@@ -14,32 +14,44 @@ import {
Policy,
PolicyType,
PolicyUpsert,
+ SensitiveDataPolicyPayload,
} from "@/types/policy";
import { getPrincipalFromIncludedList } from "./principal";
import { useEnvironmentStore } from "./environment";
import { computed, Ref, watchEffect } from "vue";
import { useCurrentUser } from "./auth";
-function convert(
+function convertEnvironment(
policy: ResourceObject,
includedList: ResourceObject[]
-): Policy {
- const environmentId = (
- policy.relationships!.environment.data as ResourceIdentifier
- ).id;
- let environment: Environment = unknown("ENVIRONMENT") as Environment;
- environment.id = parseInt(environmentId);
-
- const environmentStore = useEnvironmentStore();
- for (const item of includedList || []) {
- if (
- item.type == "environment" &&
- (policy.relationships!.environment.data as ResourceIdentifier).id ==
- item.id
- ) {
- environment = environmentStore.convert(item, includedList);
+) {
+ // The `environment` relationship cannot retire now.
+ // But for database-level policies it will be null.
+ // In order not to break the typings, we will fallback to <<Unknown Environment>>
+ // for database-level policies here.
+ let environment = unknown("ENVIRONMENT");
+ const data = policy.relationships?.environment?.data as
+ | ResourceIdentifier
+ | undefined;
+ if (data) {
+ const environmentId = data.id;
+ environment.id = parseInt(environmentId);
+
+ const environmentStore = useEnvironmentStore();
+ for (const item of includedList || []) {
+ if (item.type == "environment" && data.id == item.id) {
+ environment = environmentStore.convert(item, includedList);
+ }
}
}
+ return environment;
+}
+
+function convert(
+ policy: ResourceObject,
+ includedList: ResourceObject[]
+): Policy {
+ const environment = convertEnvironment(policy, includedList);
const result = {
...(policy.attributes as Omit<
@@ -65,6 +77,13 @@ function convert(
payload.assigneeGroupList = [];
}
}
+ if (result.type === "bb.policy.sensitive-data") {
+ const payload = result.payload as SensitiveDataPolicyPayload;
+ if (!payload.sensitiveDataList) {
+ // The array might be null, fill it with empty array to fallback.
+ payload.sensitiveDataList = [];
+ }
+ }
return result;
}
@@ -72,6 +91,7 @@ function convert(
export const usePolicyStore = defineStore("policy", {
state: (): PolicyState => ({
policyMapByEnvironmentId: new Map(),
+ policyMapByDatabaseId: new Map(),
}),
actions: {
getPolicyByEnvironmentIdAndType(
@@ -165,6 +185,93 @@ export const usePolicyStore = defineStore("policy", {
`/api/policy/environment/${environmentId}?type=${type}`
);
},
+
+ getPolicyByDatabaseIdAndType(
+ databaseId: DatabaseId,
+ type: PolicyType
+ ): Policy | undefined {
+ const map = this.policyMapByDatabaseId.get(databaseId);
+ if (map) {
+ return map.get(type);
+ }
+ return undefined;
+ },
+ setPolicyByDatabaseId({
+ databaseId,
+ policy,
+ }: {
+ databaseId: DatabaseId;
+ policy: Policy;
+ }) {
+ const map = this.policyMapByDatabaseId.get(databaseId);
+ if (map) {
+ map.set(policy.type, policy);
+ } else {
+ this.policyMapByDatabaseId.set(
+ databaseId,
+ new Map([[policy.type, policy]])
+ );
+ }
+ },
+ async fetchPolicyByDatabaseAndType({
+ databaseId,
+ type,
+ }: {
+ databaseId: DatabaseId;
+ type: PolicyType;
+ }): Promise<Policy> {
+ const data = (
+ await axios.get(`/api/policy/database/${databaseId}?type=${type}`)
+ ).data;
+ const policy = convert(data.data, data.included);
+ this.setPolicyByDatabaseId({ databaseId, policy });
+
+ return policy;
+ },
+ async upsertPolicyByDatabaseAndType({
+ databaseId,
+ type,
+ policyUpsert,
+ }: {
+ databaseId: DatabaseId;
+ type: PolicyType;
+ policyUpsert: PolicyUpsert;
+ }): Promise<Policy> {
+ const data = (
+ await axios.patch(`/api/policy/database/${databaseId}?type=${type}`, {
+ data: {
+ type: "policyUpsert",
+ attributes: {
+ rowStatus: policyUpsert.rowStatus,
+ payload: policyUpsert.payload
+ ? JSON.stringify(policyUpsert.payload)
+ : undefined,
+ },
+ },
+ })
+ ).data;
+ const policy = convert(data.data, data.included);
+
+ this.setPolicyByDatabaseId({ databaseId, policy });
+
+ return policy;
+ },
+ async deletePolicyByDatabaseAndType({
+ databaseId,
+ type,
+ }: {
+ databaseId: DatabaseId;
+ type: PolicyType;
+ }) {
+ await axios.delete(`/api/policy/database/${databaseId}?type=${type}`);
+ // Remove it from local store.
+ const map = this.policyMapByDatabaseId.get(databaseId);
+ if (map) {
+ if (map.has(type)) {
+ map.delete(type);
+ }
+ }
+ },
},
});
@@ -186,3 +293,22 @@ export const usePolicyByEnvironmentAndType = (
)
);
};
+
+export const usePolicyByDatabaseAndType = (
+ params: Ref<{ databaseId: DatabaseId; type: PolicyType }>
+) => {
+ const store = usePolicyStore();
+ const currentUser = useCurrentUser();
+ watchEffect(() => {
+ if (currentUser.value.id === UNKNOWN_ID) return;
+
+ store.fetchPolicyByDatabaseAndType(params.value);
+ });
+
+ return computed(() =>
+ store.getPolicyByDatabaseIdAndType(
+ params.value.databaseId,
+ params.value.type
+ )
+ );
+};
diff --git a/frontend/src/types/plan.ts b/frontend/src/types/plan.ts
index 46c928ba1c..961db4ea24 100644
--- a/frontend/src/types/plan.ts
+++ b/frontend/src/types/plan.ts
@@ -26,7 +26,8 @@ export type FeatureType =
// Policy Control
| "bb.feature.approval-policy"
| "bb.feature.backup-policy"
- | "bb.feature.environment-tier-policy";
+ | "bb.feature.environment-tier-policy"
+ | "bb.feature.sensitive-data";
export enum PlanType {
FREE = 0,
@@ -102,6 +103,7 @@ export const FEATURE_MATRIX: Map<FeatureType, boolean[]> = new Map([
["bb.feature.approval-policy", [false, true, true]],
["bb.feature.backup-policy", [false, true, true]],
["bb.feature.environment-tier-policy", [false, false, true]],
+ ["bb.feature.sensitive-data", [false, false, true]],
]);
export const FEATURE_SECTIONS: { type: string; featureList: string[] }[] =
diff --git a/frontend/src/types/plan.yaml b/frontend/src/types/plan.yaml
index 17f3b6a3f8..74d1bd32bc 100644
--- a/frontend/src/types/plan.yaml
+++ b/frontend/src/types/plan.yaml
@@ -29,6 +29,7 @@ categoryList:
- sync-members-from-vcs
- dba-workflow
- environment-tier
+ - sensitive-data
- type: branding
featureList:
- branding-logo
@@ -170,6 +171,7 @@ planList:
- type: dba-workflow
tooltip: dba-workflow-tooltip
- type: environment-tier
+ - type: sensitive-data
- type: branding-logo
mainFeatureList:
- sla
diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.ts
index 0e407d05b9..f17b064682 100644
--- a/frontend/src/types/policy.ts
+++ b/frontend/src/types/policy.ts
@@ -13,7 +13,8 @@ export type PolicyType =
| "bb.policy.pipeline-approval"
| "bb.policy.backup-plan"
| "bb.policy.sql-review"
- | "bb.policy.environment-tier";
+ | "bb.policy.environment-tier"
+ | "bb.policy.sensitive-data";
export type PipelineApprovalPolicyValue =
| "MANUAL_APPROVAL_NEVER"
@@ -68,11 +69,24 @@ export type AssigneeGroup = {
value: AssigneeGroupValue;
};
+export type SensitiveDataMaskType = "DEFAULT";
+
+export type SensitiveData = {
+ table: string;
+ column: string;
+ maskType: SensitiveDataMaskType;
+};
+
+export type SensitiveDataPolicyPayload = {
+ sensitiveDataList: SensitiveData[];
+};
+
export type PolicyPayload =
| PipelineApprovalPolicyPayload
| BackupPlanPolicyPayload
| SQLReviewPolicyPayload
- | EnvironmentTierPolicyPayload;
+ | EnvironmentTierPolicyPayload
+ | SensitiveDataPolicyPayload;
export type Policy = {
id: PolicyId;
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index a491eeaf51..8bc8bd88b0 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -107,6 +107,7 @@ export interface TaskState {}
export interface PolicyState {
policyMapByEnvironmentId: Map<EnvironmentId, Map<PolicyType, Policy>>;
+ policyMapByDatabaseId: Map<DatabaseId, Map<PolicyType, Policy>>;
}
export interface ProjectState {
diff --git a/frontend/src/utils/role.ts b/frontend/src/utils/role.ts
index 4328d28f2f..4ead966148 100644
--- a/frontend/src/utils/role.ts
+++ b/frontend/src/utils/role.ts
@@ -5,6 +5,7 @@ export type WorkspacePermissionType =
| "bb.permission.workspace.debug"
| "bb.permission.workspace.manage-environment"
| "bb.permission.workspace.manage-instance"
+ | "bb.permission.workspace.manage-database"
// Change issue assignee, change issue status, view all issues
| "bb.permission.workspace.manage-issue"
| "bb.permission.workspace.manage-label"
@@ -26,6 +27,7 @@ export const WORKSPACE_PERMISSION_MATRIX: Map<
["bb.permission.workspace.debug", [false, true, true]],
["bb.permission.workspace.manage-environment", [false, true, true]],
["bb.permission.workspace.manage-instance", [false, true, true]],
+ ["bb.permission.workspace.manage-database", [false, false, true]],
["bb.permission.workspace.manage-issue", [false, true, true]],
["bb.permission.workspace.manage-label", [false, true, true]],
["bb.permission.workspace.manage-project", [false, true, true]],
diff --git a/frontend/src/views/TableDetail.vue b/frontend/src/views/TableDetail.vue
index ab9356d524..8b5ba95a1c 100644
--- a/frontend/src/views/TableDetail.vue
+++ b/frontend/src/views/TableDetail.vue
@@ -195,8 +195,11 @@
{{ $t("database.columns") }}
</div>
<ColumnTable
+ :database="database"
+ :table="table"
:column-list="table.columnList"
:engine="database.instance.engine"
+ :sensitive-data-list="sensitiveDataList"
/>
</div>
@@ -221,8 +224,8 @@ import {
idFromSlug,
isGhostTable,
} from "../utils";
-import { useTableStore } from "@/store";
-import { Table } from "@/types";
+import { usePolicyByDatabaseAndType, useTableStore } from "@/store";
+import { SensitiveData, SensitiveDataPolicyPayload, Table } from "@/types";
export default defineComponent({
name: "TableDetail",
@@ -259,12 +262,29 @@ export default defineComponent({
window.open(url);
};
+ const sensitiveDataPolicy = usePolicyByDatabaseAndType(
+ computed(() => ({
+ databaseId: database.value.id,
+ type: "bb.policy.sensitive-data",
+ }))
+ );
+
+ const sensitiveDataList = computed((): SensitiveData[] => {
+ const policy = sensitiveDataPolicy.value;
+ if (!policy) {
+ return [];
+ }
+ const payload = policy.payload as SensitiveDataPolicyPayload;
+ return payload.sensitiveDataList;
+ });
+
return {
table,
database,
gotoSQLEditor,
bytesToString,
isGhostTable,
+ sensitiveDataList,
};
},
});