Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: use database resource selector for masking exception (#14163)
ecmadao07f81fe29dc2
diff --git a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vueindex 19ced986df..ffe222d077 100644--- a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue+++ b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/DatabaseResourceSelector.vue@@ -3,6 +3,7 @@<NTransferv-model:value="selectedValueList"style="height: 512px"+ :disabled="disabled":options="sourceTransferOptions":render-source-list="renderSourceList":render-target-list="renderTargetList"@@ -42,6 +43,7 @@ import {} from "./common";const props = defineProps<{+ disabled?: boolean;projectName: string;includeCloumn: boolean;databaseResources: DatabaseResource[];@@ -57,15 +59,34 @@ const emit = defineEmits<{const databaseStore = useDatabaseV1Store();const dbSchemaStore = useDBSchemaV1Store();const { project } = useProjectByName(props.projectName);++const parseResourceToKey = (resource: DatabaseResource): string => {+ const data = [+ resource.databaseName,+ "schemas",+ resource.schema,+ "tables",+ resource.table,+ "columns",+ resource.column,+ ];++ while (data.length > 0) {+ const item = data.pop();+ if (!item) {+ data.pop();+ continue;+ }+ data.push(item);+ break;+ }++ return data.join("/");+};+const selectedValueList = ref<string[]>(props.databaseResources.map((databaseResource) => {- if (databaseResource.table !== undefined) {- return `${databaseResource.databaseName}/schemas/${databaseResource.schema}/tables/${databaseResource.table}`;- } else if (databaseResource.schema !== undefined) {- return `${databaseResource.databaseName}/schemas/${databaseResource.schema}`;- } else {- return databaseResource.databaseName;- }+ return parseResourceToKey(databaseResource);}));const defaultExpandedKeys = ref<string[]>([]);@@ -158,6 +179,7 @@ const renderSourceList: TransferRenderSourceList = ({ onCheck, pattern }) => {checkable: true,selectable: false,checkOnClick: true,+ disabled: props.disabled,data: sourceTreeOptions.value,blockLine: true,virtualScroll: true,@@ -223,6 +245,7 @@ const renderTargetList: TransferRenderSourceList = () => {checkable: false,selectable: false,defaultExpandAll: true,+ disabled: props.disabled,data: targetTreeOptions.value,blockLine: true,virtualScroll: true,diff --git a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/index.vue b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/index.vueindex 72eb36c5b2..efddbceaa5 100644--- a/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/index.vue+++ b/frontend/src/components/GrantRequestPanel/DatabaseResourceForm/index.vue@@ -1,7 +1,9 @@<template><div class="w-full mb-2"><NRadioGroup+ v-if="allowSelectAll"v-model:value="state.allDatabases"+ :disabled="disabled"class="w-full !flex flex-row justify-start items-center gap-4"><NTooltip trigger="hover">@@ -28,6 +30,7 @@<DatabaseResourceSelectorv-if="project"v-model:database-resources="state.databaseResources"+ :disabled="disabled":project-name="project.name":include-cloumn="includeCloumn"/>@@ -53,12 +56,21 @@ interface LocalState {databaseResources: DatabaseResource[];}-const props = defineProps<{- projectName: string;- requiredFeature: FeatureType;- includeCloumn: boolean;- databaseResources?: DatabaseResource[];-}>();+const props = withDefaults(+ defineProps<{+ disabled?: boolean;+ projectName: string;+ requiredFeature: FeatureType;+ includeCloumn: boolean;+ allowSelectAll?: boolean;+ databaseResources?: DatabaseResource[];+ }>(),+ {+ disabled: false,+ allowSelectAll: true,+ databaseResources: undefined,+ }+);const emit = defineEmits<{(@@ -68,7 +80,8 @@ const emit = defineEmits<{}>();const state = reactive<LocalState>({- allDatabases: (props.databaseResources || []).length === 0,+ allDatabases:+ props.allowSelectAll && (props.databaseResources || []).length === 0,showFeatureModal: false,databaseResources: props.databaseResources || [],});diff --git a/frontend/src/components/SensitiveData/GrantAccessDrawer.vue b/frontend/src/components/SensitiveData/GrantAccessDrawer.vueindex dec4db077b..06a9159ac3 100644--- a/frontend/src/components/SensitiveData/GrantAccessDrawer.vue+++ b/frontend/src/components/SensitiveData/GrantAccessDrawer.vue@@ -1,17 +1,30 @@<template><Drawer :show="true" @close="onDismiss">- <DrawerContent :title="$t('settings.sensitive-data.grant-access')">+ <DrawerContent+ :title="$t('settings.sensitive-data.grant-access')"+ class="w-[64rem] max-w-[100vw] relative"+ ><div class="divide-block-border space-y-8 h-full">- <SensitiveColumnTable- :row-clickable="false"- :show-operation="false"- :row-selectable="false"- :column-list="columnList"- :checked-column-index-list="[]"- />+ <div class="w-full">+ <div class="flex items-center gap-x-1 mb-2">+ <span>{{ $t("common.resources") }}</span>+ <span class="text-red-600">*</span>+ </div>+ <DatabaseResourceForm+ v-model:database-resources="state.databaseResources"+ :project-name="projectName"+ :required-feature="'bb.feature.sensitive-data'"+ :include-cloumn="true"+ :allow-select-all="false"+ :disabled="true"+ />+ </div><div class="w-full">- <p class="mb-2">{{ $t("settings.sensitive-data.action.self") }}</p>+ <div class="flex items-center gap-x-1 mb-2">+ <span>{{ $t("settings.sensitive-data.action.self") }}</span>+ <span class="text-red-600">*</span>+ </div><div class="flex space-x-4"><NCheckboxv-for="action in ACTIONS"@@ -31,9 +44,10 @@</div><div class="w-full">- <p class="mb-2">- {{ $t("settings.sensitive-data.masking-level.self") }}- </p>+ <div class="flex items-center gap-x-1 mb-2">+ <span>{{ $t("settings.sensitive-data.masking-level.self") }}</span>+ <span class="text-red-600">*</span>+ </div><MaskingLevelRadioGroup:level="state.maskingLevel":level-list="MASKING_LEVELS"@@ -85,14 +99,15 @@</template><script lang="ts" setup>-import { groupBy } from "lodash-es";+import { isUndefined } from "lodash-es";import { NButton, NCheckbox, NDatePicker } from "naive-ui";import { computed, reactive } from "vue";import { useI18n } from "vue-i18n";+import DatabaseResourceForm from "@/components/GrantRequestPanel/DatabaseResourceForm/index.vue";import MembersBindingSelect from "@/components/Member/MembersBindingSelect.vue";import { Drawer, DrawerContent } from "@/components/v2";import { usePolicyV1Store, pushNotification } from "@/store";-import type { ComposedProject, DatabaseResource } from "@/types";+import type { DatabaseResource } from "@/types";import { Expr } from "@/types/proto/google/type/expr";import { MaskingLevel } from "@/types/proto/v1/common";import type {@@ -106,9 +121,8 @@ import {maskingExceptionPolicy_MaskingException_ActionToJSON,} from "@/types/proto/v1/org_policy_service";import MaskingLevelRadioGroup from "./components/MaskingLevelRadioGroup.vue";-import SensitiveColumnTable from "./components/SensitiveColumnTable.vue";import type { SensitiveColumn } from "./types";-import { getExpressionsForSensitiveColumn } from "./utils";+import { getExpressionsForDatabaseResource } from "./utils";const props = defineProps<{columnList: SensitiveColumn[];@@ -137,6 +151,12 @@ const state = reactive<LocalState>({maskingLevel: MaskingLevel.PARTIAL,processing: false,supportActions: new Set(ACTIONS),+ databaseResources: props.columnList.map((col) => ({+ databaseName: col.database.name,+ schema: col.maskData.schema,+ table: col.maskData.table,+ column: col.maskData.column,+ })),});const policyStore = usePolicyV1Store();@@ -161,31 +181,25 @@ const submitDisabled = computed(() => {if (state.supportActions.size === 0) {return true;}+ if (+ !isUndefined(state.databaseResources) &&+ state.databaseResources.length === 0+ ) {+ return true;+ }return false;});const onSubmit = async () => {state.processing = true;- const groupByProject = groupBy(- props.columnList,- (item) => item.database.project- );try {- for (const [project, columnList] of Object.entries(groupByProject)) {- if (columnList.length === 0) {- continue;- }- const pendingUpdate = await getPendingUpdatePolicy(- columnList[0].database.projectEntity,- columnList- );- await policyStore.upsertPolicy({- parentPath: project,- policy: pendingUpdate,- updateMask: ["payload"],- });- }+ const pendingUpdate = await getPendingUpdatePolicy(props.projectName);+ await policyStore.upsertPolicy({+ parentPath: props.projectName,+ policy: pendingUpdate,+ updateMask: ["payload"],+ });pushNotification({module: "bytebase",style: "SUCCESS",@@ -198,23 +212,22 @@ const onSubmit = async () => {};const getPendingUpdatePolicy = async (- project: ComposedProject,- columnList: SensitiveColumn[]+ parentPath: string): Promise<Partial<Policy>> => {const maskingExceptions: MaskingExceptionPolicy_MaskingException[] = [];- for (const column of columnList) {- const expressions = getExpressionsForSensitiveColumn(column);- if (state.expirationTimestamp) {- expressions.push(- `request.time < timestamp("${new Date(- state.expirationTimestamp- ).toISOString()}")`- );- }+ const expressions = [];+ if (state.expirationTimestamp) {+ expressions.push(+ `request.time < timestamp("${new Date(+ state.expirationTimestamp+ ).toISOString()}")`+ );+ }- for (const action of state.supportActions.values()) {- for (const member of state.memberList) {+ for (const action of state.supportActions.values()) {+ for (const member of state.memberList) {+ if (!state.databaseResources) {maskingExceptions.push({member,action,@@ -223,12 +236,26 @@ const getPendingUpdatePolicy = async (expression: expressions.join(" && "),}),});+ } else {+ for (const databaseResource of state.databaseResources) {+ const resourceExpressions =+ getExpressionsForDatabaseResource(databaseResource);+ resourceExpressions.push(...expressions);+ maskingExceptions.push({+ member,+ action,+ maskingLevel: state.maskingLevel,+ condition: Expr.fromPartial({+ expression: resourceExpressions.join(" && "),+ }),+ });+ }}}}const policy = await policyStore.getOrFetchPolicyByParentAndType({- parentPath: project.name,+ parentPath,policyType: PolicyType.MASKING_EXCEPTION,});const existed = policy?.maskingExceptionPolicy?.maskingExceptions ?? [];diff --git a/frontend/src/components/SensitiveData/utils.ts b/frontend/src/components/SensitiveData/utils.tsindex 2d7b03806e..094430844a 100644--- a/frontend/src/components/SensitiveData/utils.ts+++ b/frontend/src/components/SensitiveData/utils.ts@@ -1,8 +1,9 @@+import type { DatabaseResource } from "@/types";import type {MaskData,MaskingExceptionPolicy_MaskingException,} from "@/types/proto/v1/org_policy_service";-import { extractInstanceResourceName } from "@/utils";+import { extractDatabaseResourceName } from "@/utils";import type { SensitiveColumn } from "./types";export const getMaskDataIdentifier = (maskData: MaskData): string => {@@ -14,7 +15,12 @@ export const isCurrentColumnException = (sensitiveColumn: SensitiveColumn): boolean => {const expression = exception.condition?.expression ?? "";- const matches = getExpressionsForSensitiveColumn(sensitiveColumn);+ const matches = getExpressionsForDatabaseResource({+ databaseName: sensitiveColumn.database.name,+ schema: sensitiveColumn.maskData.schema,+ table: sensitiveColumn.maskData.table,+ column: sensitiveColumn.maskData.column,+ });for (const match of matches) {if (!expression.includes(match)) {return false;@@ -23,21 +29,24 @@ export const isCurrentColumnException = (return true;};-export const getExpressionsForSensitiveColumn = (- sensitiveColumn: SensitiveColumn+export const getExpressionsForDatabaseResource = (+ databaseResource: DatabaseResource): string[] => {+ const resourceName = extractDatabaseResourceName(+ databaseResource.databaseName+ );const expressions = [- `resource.database_name == "${sensitiveColumn.database.databaseName}"`,- `resource.instance_id == "${extractInstanceResourceName(- sensitiveColumn.database.instance- )}"`,- `resource.table_name == "${sensitiveColumn.maskData.table}"`,- `resource.column_name == "${sensitiveColumn.maskData.column}"`,+ `resource.database_name == "${resourceName.databaseName}"`,+ `resource.instance_id == "${resourceName.instanceName}"`,];- if (sensitiveColumn.maskData.schema) {- expressions.push(- `resource.schema_name == "${sensitiveColumn.maskData.schema}"`- );+ if (databaseResource.schema) {+ expressions.push(`resource.schema_name == "${databaseResource.schema}"`);+ }+ if (databaseResource.table) {+ expressions.push(`resource.table_name == "${databaseResource.table}"`);+ }+ if (databaseResource.column) {+ expressions.push(`resource.column_name == "${databaseResource.column}"`);}return expressions;};diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 4a0627f4bc..b3e9a5b8e4 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -278,6 +278,7 @@"connection": "Connection","permission-denied": "Permission denied","resource": "Resource",+ "resources": "Resources","closed": "Closed","options": "Options","reset": "Reset",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 55efea45f3..2257dbf9ff 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -278,6 +278,7 @@"connection": "Conexión","permission-denied": "Permiso denegado","resource": "Recurso",+ "resources": "Recursos","closed": "Cerrado","options": "Opciones","reset": "Reiniciar",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex e3e00f73ad..a476e0ae7d 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -278,6 +278,7 @@"connection": "接続する","permission-denied": "許可が拒否されました","resource": "リソース",+ "resources": "リソース","closed": "閉まっている","options": "オプション","reset": "リセット",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex f9df32d8c9..05a65219e5 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -278,6 +278,7 @@"connection": "连接","permission-denied": "您没有权限执行此操作","resource": "资源",+ "resources": "资源","closed": "已关闭","options": "配置项","reset": "重置",@@ -3810,4 +3811,4 @@"setup": {"self": "设置"}-}+}\ No newline at end of filediff --git a/frontend/src/utils/v1/database.ts b/frontend/src/utils/v1/database.tsindex 059b66a64e..8c9dac98b1 100644--- a/frontend/src/utils/v1/database.ts+++ b/frontend/src/utils/v1/database.ts@@ -23,7 +23,9 @@ export const databaseV1Url = (db: ComposedDatabase) => {export const extractDatabaseResourceName = (resource: string): {+ // instance full nameinstance: string;+ // database full namedatabase: string;databaseName: string;instanceName: string;