Skip to content

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

chore: migrate sensitive data policy to gRPC (#6022)

ecmadao689057b2e205
diff --git a/frontend/src/components/ColumnTable.vue b/frontend/src/components/ColumnTable.vue
index a7b025c650..a011710352 100644
--- a/frontend/src/components/ColumnTable.vue
+++ b/frontend/src/components/ColumnTable.vue
@@ -73,16 +73,18 @@
import { cloneDeep } from "lodash-es";
import { computed, defineComponent, PropType, reactive } from "vue";
import { useI18n } from "vue-i18n";
-import {
- Column,
- Database,
- SensitiveData,
- SensitiveDataPolicyPayload,
-} from "@/types";
+import { Column, Database } from "@/types";
import { ColumnMetadata, TableMetadata } from "@/types/proto/store/database";
-import { featureToRef, useCurrentUser, usePolicyStore } from "@/store";
+import { featureToRef, useCurrentUser } from "@/store";
import { hasWorkspacePermission } from "@/utils";
import { BBTableColumn } from "@/bbkit/types";
+import { usePolicyV1Store } from "@/store/modules/v1/policy";
+import { getDatabasePathByLegacyDatabase } from "@/store/modules/v1/common";
+import {
+ PolicyType,
+ SensitiveData,
+ SensitiveDataMaskType,
+} from "@/types/proto/v1/org_policy_service";
type LocalState = {
showFeatureModal: boolean;
@@ -276,20 +278,21 @@ export default defineComponent({
schema: props.schema,
table: props.table.name,
column: column.name,
- maskType: "DEFAULT",
+ maskType: SensitiveDataMaskType.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,
+
+ usePolicyV1Store().upsertPolicy({
+ parentPath: getDatabasePathByLegacyDatabase(props.database),
+ policy: {
+ type: PolicyType.SENSITIVE_DATA,
+ sensitiveDataPolicy: {
+ sensitiveData: sensitiveDataList,
+ },
},
+ updateMask: ["payload"],
});
};
diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.ts
index 850bb7416c..1f8984c30a 100644
--- a/frontend/src/store/modules/policy.ts
+++ b/frontend/src/store/modules/policy.ts
@@ -19,7 +19,6 @@ import {
PolicyResourceType,
PolicyType,
PolicyUpsert,
- SensitiveDataPolicyPayload,
} from "@/types/policy";
import { useEnvironmentStore } from "./environment";
import { useCurrentUser } from "./auth";
@@ -77,13 +76,6 @@ 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;
}
diff --git a/frontend/src/store/modules/slowQueryPolicy.ts b/frontend/src/store/modules/slowQueryPolicy.ts
index 56da637c05..ed085bc139 100644
--- a/frontend/src/store/modules/slowQueryPolicy.ts
+++ b/frontend/src/store/modules/slowQueryPolicy.ts
@@ -7,15 +7,7 @@ import {
Policy,
PolicyType,
PolicyResourceType,
- policyTypeToJSON,
} from "@/types/proto/v1/org_policy_service";
-import { policyNamePrefix } from "@/store/modules/v1/common";
-
-const getSlowQueryPolicyName = (parentPath: string): string => {
- return `${parentPath}/${policyNamePrefix}${policyTypeToJSON(
- PolicyType.SLOW_QUERY
- )}`;
-};
export const useSlowQueryPolicyStore = defineStore("slow-query-policy", () => {
const policyMapByName = ref(new Map<string, Policy>());
@@ -24,10 +16,6 @@ export const useSlowQueryPolicyStore = defineStore("slow-query-policy", () => {
return [...policyMapByName.value.values()];
};
- const getPolicy = (name: string) => {
- return policyMapByName.value.get(name);
- };
-
const setPolicyList = (policyList: Policy[]) => {
for (const policy of policyList) {
policyMapByName.value.set(policy.name, policy);
@@ -53,24 +41,16 @@ export const useSlowQueryPolicyStore = defineStore("slow-query-policy", () => {
parentPath: string;
active: boolean;
}) => {
- const policyStore = usePolicyV1Store();
- let policy: Policy;
- if (!getPolicy(getSlowQueryPolicyName(parentPath))) {
- policy = await await policyStore.createPolicy(parentPath, {
+ const policy = await usePolicyV1Store().upsertPolicy({
+ parentPath,
+ policy: {
type: PolicyType.SLOW_QUERY,
slowQueryPolicy: {
active,
},
- });
- } else {
- policy = await policyStore.updatePolicy(["payload"], {
- name: getSlowQueryPolicyName(parentPath),
- type: PolicyType.SLOW_QUERY,
- slowQueryPolicy: {
- active,
- },
- });
- }
+ },
+ updateMask: ["payload"],
+ });
if (policy) {
policyMapByName.value.set(policy.name, policy);
diff --git a/frontend/src/store/modules/sqlReview.ts b/frontend/src/store/modules/sqlReview.ts
index 7c93c43376..65df07cff4 100644
--- a/frontend/src/store/modules/sqlReview.ts
+++ b/frontend/src/store/modules/sqlReview.ts
@@ -87,7 +87,7 @@ interface SQLReviewState {
const getSQLReviewPolicyName = (environmentPath: string): string => {
return `${environmentPath}/${policyNamePrefix}${policyTypeToJSON(
PolicyType.SQL_REVIEW
- )}`;
+ ).toLowerCase()}`;
};
export const useSQLReviewStore = defineStore("sqlReview", {
@@ -214,6 +214,9 @@ export const useSQLReviewStore = defineStore("sqlReview", {
const policy = await policyStore.getOrFetchPolicyByName(
getSQLReviewPolicyName(targetPolicy.environment.name)
);
+ if (!policy) {
+ return;
+ }
const updateMask: string[] = [];
if (rowStatus) {
diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.ts
index e8cc359d67..9a249c32fd 100644
--- a/frontend/src/store/modules/v1/common.ts
+++ b/frontend/src/store/modules/v1/common.ts
@@ -1,9 +1,10 @@
-import { ResourceId, UNKNOWN_ID, Instance } from "@/types";
+import { ResourceId, UNKNOWN_ID, Instance, Database } from "@/types";
export const userNamePrefix = "users/";
export const environmentNamePrefix = "environments/";
export const projectNamePrefix = "projects/";
export const instanceNamePrefix = "instances/";
+export const databaseNamePrefix = "databases/";
export const idpNamePrefix = "idps/";
export const policyNamePrefix = "policies/";
@@ -47,3 +48,7 @@ export const getIdentityProviderResourceId = (name: string): ResourceId => {
export const getInstancePathByLegacyInstance = (instance: Instance): string => {
return `${environmentNamePrefix}${instance.environment.resourceId}/${instanceNamePrefix}${instance.resourceId}`;
};
+
+export const getDatabasePathByLegacyDatabase = (database: Database): string => {
+ return `${environmentNamePrefix}${database.instance.environment.resourceId}/${instanceNamePrefix}${database.instance.resourceId}/${databaseNamePrefix}${database.name}`;
+};
diff --git a/frontend/src/store/modules/v1/policy.ts b/frontend/src/store/modules/v1/policy.ts
index 12ba653bce..ab3c4a05af 100644
--- a/frontend/src/store/modules/v1/policy.ts
+++ b/frontend/src/store/modules/v1/policy.ts
@@ -1,10 +1,16 @@
import { defineStore } from "pinia";
+import { computed, unref, watchEffect } from "vue";
import { policyServiceClient } from "@/grpcweb";
import {
Policy,
PolicyType,
PolicyResourceType,
+ policyTypeToJSON,
} from "@/types/proto/v1/org_policy_service";
+import { MaybeRef, UNKNOWN_ID } from "@/types";
+import { useCurrentUser } from "../auth";
+import { State } from "@/types/proto/v1/common";
+import { policyNamePrefix } from "@/store/modules/v1/common";
interface PolicyState {
policyMapByName: Map<string, Policy>;
@@ -44,7 +50,7 @@ export const usePolicyV1Store = defineStore("policy_v1", {
}: {
resourceType: PolicyResourceType;
policyType: PolicyType;
- showDeleted: boolean;
+ showDeleted?: boolean;
}) {
const { policies } = await policyServiceClient.listPolicies({
parent: getPolicyParentByResourceType(resourceType),
@@ -56,19 +62,61 @@ export const usePolicyV1Store = defineStore("policy_v1", {
}
return policies;
},
+ getPolicies({
+ resourceType,
+ resourceUID,
+ policyType,
+ showDeleted,
+ }: {
+ resourceType: PolicyResourceType;
+ policyType: PolicyType;
+ showDeleted?: boolean;
+ resourceUID?: string;
+ }) {
+ const response: Policy[] = [];
+ for (const [_, policy] of this.policyMapByName) {
+ if (policy.resourceType != resourceType || policy.type != policyType) {
+ continue;
+ }
+ if (!showDeleted && policy.state == State.DELETED) {
+ continue;
+ }
+ if (resourceUID && policy.resourceUid != resourceUID) {
+ continue;
+ }
+ response.push(policy);
+ }
+ return response;
+ },
+ async getOrFetchPolicyByParentAndType({
+ parentPath,
+ policyType,
+ }: {
+ parentPath: string;
+ policyType: PolicyType;
+ }) {
+ const name = `${parentPath}/${policyNamePrefix}${policyTypeToJSON(
+ policyType
+ )}`;
+ return this.getOrFetchPolicyByName(name);
+ },
async getOrFetchPolicyByName(name: string) {
const cachedData = this.getPolicyByName(name);
if (cachedData) {
return cachedData;
}
- const policy = await policyServiceClient.getPolicy({
- name,
- });
- this.policyMapByName.set(policy.name, policy);
- return policy;
+ try {
+ const policy = await policyServiceClient.getPolicy({
+ name: name.toLowerCase(),
+ });
+ this.policyMapByName.set(policy.name, policy);
+ return policy;
+ } catch {
+ return;
+ }
},
getPolicyByName(name: string) {
- return this.policyMapByName.get(name);
+ return this.policyMapByName.get(name.toLowerCase());
},
async createPolicy(parent: string, policy: Partial<Policy>) {
const createdPolicy = await policyServiceClient.createPolicy({
@@ -86,9 +134,80 @@ export const usePolicyV1Store = defineStore("policy_v1", {
this.policyMapByName.set(updatedPolicy.name, updatedPolicy);
return updatedPolicy;
},
+ async upsertPolicy({
+ parentPath,
+ policy,
+ updateMask,
+ }: {
+ parentPath: string;
+ policy: Partial<Policy>;
+ updateMask?: string[];
+ }) {
+ if (!policy.type) {
+ throw new Error("policy type is required");
+ }
+ policy.name = `${parentPath}/${policyNamePrefix}${policyTypeToJSON(
+ policy.type
+ ).toLowerCase()}`;
+ const updatedPolicy = await policyServiceClient.updatePolicy({
+ policy,
+ updateMask,
+ allowMissing: true,
+ });
+ this.policyMapByName.set(updatedPolicy.name, updatedPolicy);
+ return updatedPolicy;
+ },
async deletePolicy(name: string) {
await policyServiceClient.deletePolicy({ name });
this.policyMapByName.delete(name);
},
},
});
+
+export const usePolicyListByResourceTypeAndPolicyType = (
+ params: MaybeRef<{
+ resourceType: PolicyResourceType;
+ policyType: PolicyType;
+ showDeleted: false;
+ }>
+) => {
+ const store = usePolicyV1Store();
+ const currentUser = useCurrentUser();
+ watchEffect(() => {
+ if (currentUser.value.id === UNKNOWN_ID) return;
+ const { resourceType, policyType, showDeleted } = unref(params);
+
+ store.fetchPolicies({ resourceType, policyType, showDeleted });
+ });
+
+ return computed(() => {
+ const { resourceType, policyType, showDeleted } = unref(params);
+ return store.getPolicies({ resourceType, policyType, showDeleted });
+ });
+};
+
+export const usePolicyByParentAndType = (
+ params: MaybeRef<{
+ parentPath: string;
+ policyType: PolicyType;
+ }>
+) => {
+ const store = usePolicyV1Store();
+ const currentUser = useCurrentUser();
+ watchEffect(() => {
+ if (currentUser.value.id === UNKNOWN_ID) return;
+ const { policyType, parentPath } = unref(params);
+ store.getOrFetchPolicyByParentAndType({
+ parentPath,
+ policyType,
+ });
+ });
+
+ return computed(() => {
+ const { parentPath, policyType } = unref(params);
+ const res = store.getPolicyByName(
+ `${parentPath}/${policyNamePrefix}${policyTypeToJSON(policyType)}`
+ );
+ return res;
+ });
+};
diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.ts
index d0e8310bfb..88a4a0f7ed 100644
--- a/frontend/src/types/policy.ts
+++ b/frontend/src/types/policy.ts
@@ -4,9 +4,7 @@ export type PolicyType =
| "bb.policy.pipeline-approval"
| "bb.policy.backup-plan"
| "bb.policy.environment-tier"
- | "bb.policy.sensitive-data"
- | "bb.policy.access-control"
- | "bb.policy.slow-query";
+ | "bb.policy.access-control";
export type PipelineApprovalPolicyValue =
| "MANUAL_APPROVAL_NEVER"
@@ -52,19 +50,6 @@ export type AssigneeGroup = {
value: AssigneeGroupValue;
};
-export type SensitiveDataMaskType = "DEFAULT";
-
-export type SensitiveData = {
- schema: string;
- table: string;
- column: string;
- maskType: SensitiveDataMaskType;
-};
-
-export type SensitiveDataPolicyPayload = {
- sensitiveDataList: SensitiveData[];
-};
-
export type AccessControlRule = {
fullDatabase: boolean;
};
@@ -77,7 +62,6 @@ export type PolicyPayload =
| PipelineApprovalPolicyPayload
| BackupPlanPolicyPayload
| EnvironmentTierPolicyPayload
- | SensitiveDataPolicyPayload
| AccessControlPolicyPayload;
export type PolicyResourceType =
diff --git a/frontend/src/types/schemaEditor/index.ts b/frontend/src/types/schemaEditor/index.ts
index ee4fc29843..121714630b 100644
--- a/frontend/src/types/schemaEditor/index.ts
+++ b/frontend/src/types/schemaEditor/index.ts
@@ -1,6 +1,8 @@
import { Database, DatabaseId } from "..";
import { Schema } from "./atomType";
+export * from "./atomType";
+
export enum SchemaEditorTabType {
TabForDatabase = "database",
TabForTable = "table",
diff --git a/frontend/src/views/SettingWorkspaceSensitiveData.vue b/frontend/src/views/SettingWorkspaceSensitiveData.vue
index 6b8fab615a..4d42a68e7b 100644
--- a/frontend/src/views/SettingWorkspaceSensitiveData.vue
+++ b/frontend/src/views/SettingWorkspaceSensitiveData.vue
@@ -85,17 +85,20 @@ import { NPopconfirm } from "naive-ui";
import { uniq } from "lodash-es";
import { useRouter } from "vue-router";
-import {
- featureToRef,
- useCurrentUser,
- useDatabaseStore,
- usePolicyListByResourceTypeAndPolicyType,
- usePolicyStore,
-} from "@/store";
-import { Database, Policy, SensitiveDataPolicyPayload } from "@/types";
+import { featureToRef, useCurrentUser, useDatabaseStore } from "@/store";
+import { Database } from "@/types";
import { BBGridColumn } from "@/bbkit/types";
import { databaseSlug, hasWorkspacePermission } from "@/utils";
import { BBGrid } from "@/bbkit";
+import {
+ usePolicyListByResourceTypeAndPolicyType,
+ usePolicyV1Store,
+} from "@/store/modules/v1/policy";
+import {
+ PolicyType,
+ Policy,
+ PolicyResourceType,
+} from "@/types/proto/v1/org_policy_service";
type SensitiveColumn = {
database: Database;
@@ -129,14 +132,15 @@ const allowAdmin = computed(() => {
});
const policyList = usePolicyListByResourceTypeAndPolicyType({
- resourceType: "database",
- policyType: "bb.policy.sensitive-data",
+ resourceType: PolicyResourceType.DATABASE,
+ policyType: PolicyType.SENSITIVE_DATA,
+ showDeleted: false,
});
const updateList = async () => {
state.isLoading = true;
const distinctDatabaseIdList = uniq(
- policyList.value.map((policy) => policy.resourceId)
+ policyList.value.map((policy) => policy.resourceUid)
);
// Fetch or get all needed databases
await Promise.all(
@@ -148,13 +152,15 @@ const updateList = async () => {
const sensitiveColumnList: SensitiveColumn[] = [];
for (let i = 0; i < policyList.value.length; i++) {
const policy = policyList.value[i];
- const payload = policy.payload as SensitiveDataPolicyPayload;
+ if (!policy.sensitiveDataPolicy) {
+ continue;
+ }
- const databaseId = policy.resourceId;
+ const databaseId = policy.resourceUid;
const database = await databaseStore.getOrFetchDatabaseById(databaseId);
- for (let j = 0; j < payload.sensitiveDataList.length; j++) {
- const { schema, table, column } = payload.sensitiveDataList[j];
+ for (const sensitiveData of policy.sensitiveDataPolicy.sensitiveData) {
+ const { schema, table, column } = sensitiveData;
sensitiveColumnList.push({ database, policy, schema, table, column });
}
}
@@ -170,27 +176,29 @@ const removeSensitiveColumn = (sensitiveColumn: SensitiveColumn) => {
return;
}
- const { database, table, column } = sensitiveColumn;
+ const { table, column } = sensitiveColumn;
const policy = policyList.value.find(
- (policy) => policy.resourceId === sensitiveColumn.database.id
+ (policy) => policy.resourceUid == sensitiveColumn.database.id
);
if (!policy) return;
+ const sensitiveData = policy.sensitiveDataPolicy?.sensitiveData;
+ if (!sensitiveData) return;
- const payload = policy.payload as SensitiveDataPolicyPayload;
- const index = payload.sensitiveDataList.findIndex(
+ const index = sensitiveData.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);
+ sensitiveData.splice(index, 1);
- usePolicyStore().upsertPolicyByDatabaseAndType({
- databaseId: database.id,
- type: "bb.policy.sensitive-data",
- policyUpsert: {
- payload,
+ usePolicyV1Store().updatePolicy(["payload"], {
+ name: policy.name,
+ type: PolicyType.SENSITIVE_DATA,
+ resourceType: PolicyResourceType.DATABASE,
+ sensitiveDataPolicy: {
+ sensitiveData,
},
});
}
diff --git a/frontend/src/views/TableDetail.vue b/frontend/src/views/TableDetail.vue
index 4241ab1583..dfe03530b8 100644
--- a/frontend/src/views/TableDetail.vue
+++ b/frontend/src/views/TableDetail.vue
@@ -200,17 +200,15 @@ import {
useDBSchemaStore,
usePolicyByDatabaseAndType,
} from "@/store";
-import {
- DEFAULT_PROJECT_ID,
- SensitiveData,
- SensitiveDataPolicyPayload,
- UNKNOWN_ID,
-} from "@/types";
+import { DEFAULT_PROJECT_ID, UNKNOWN_ID } from "@/types";
import { TableMetadata } from "@/types/proto/store/database";
import ColumnTable from "../components/ColumnTable.vue";
import IndexTable from "../components/IndexTable.vue";
import InstanceEngineIcon from "../components/InstanceEngineIcon.vue";
import { SQLEditorButton } from "@/components/DatabaseDetail";
+import { usePolicyByParentAndType } from "@/store/modules/v1/policy";
+import { getDatabasePathByLegacyDatabase } from "@/store/modules/v1/common";
+import { PolicyType, SensitiveData } from "@/types/proto/v1/org_policy_service";
export default defineComponent({
name: "TableDetail",
@@ -290,20 +288,20 @@ export default defineComponent({
}
});
- const sensitiveDataPolicy = usePolicyByDatabaseAndType(
+ const sensitiveDataPolicy = usePolicyByParentAndType(
computed(() => ({
- databaseId: database.value.id,
- type: "bb.policy.sensitive-data",
+ parentPath: getDatabasePathByLegacyDatabase(database.value),
+ policyType: PolicyType.SENSITIVE_DATA,
}))
);
const sensitiveDataList = computed((): SensitiveData[] => {
const policy = sensitiveDataPolicy.value;
- if (!policy) {
+ if (!policy || !policy.sensitiveDataPolicy) {
return [];
}
- const payload = policy.payload as SensitiveDataPolicyPayload;
- return payload.sensitiveDataList;
+
+ return policy.sensitiveDataPolicy.sensitiveData;
});
return {