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: migrate slow query policy to gRPC (#6008)
ecmadao99c680942497
diff --git a/frontend/src/components/SlowQuery/Panel/LogFilter.vue b/frontend/src/components/SlowQuery/Panel/LogFilter.vueindex 81e733caf0..1b0bcb1bee 100644--- a/frontend/src/components/SlowQuery/Panel/LogFilter.vue+++ b/frontend/src/components/SlowQuery/Panel/LogFilter.vue@@ -102,7 +102,6 @@ import {type ProjectId,UNKNOWN_ID,Instance,- SlowQueryPolicyPayload,} from "@/types";import {useCurrentUser,@@ -209,13 +208,12 @@ const instanceFilter = (instance: Instance) => {return false;}const policy = policyList.value.find(- (policy) => policy.resourceId === instance.id+ (policy) => policy.resourceUid == instance.id);if (!policy) {return false;}- const payload = policy.payload as SlowQueryPolicyPayload;- return payload.active;+ return policy.slowQueryPolicy?.active;};const isDateDisabled = (date: number) => {diff --git a/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue b/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vueindex 3b2c48c8ee..21017a90ea 100644--- a/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue+++ b/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue@@ -44,7 +44,7 @@ import { computed, shallowRef, watch } from "vue";import { NButton } from "naive-ui";import { useI18n } from "vue-i18n";-import type { ComposedSlowQueryLog, SlowQueryPolicyPayload } from "@/types";+import type { ComposedSlowQueryLog } from "@/types";import {pushNotification,useInstanceStore,@@ -113,18 +113,13 @@ const syncNow = async () => {try {const instanceStore = useInstanceStore();await instanceStore.fetchInstanceList(["NORMAL"]);- const policyList =- await useSlowQueryPolicyStore().fetchPolicyListByResourceTypeAndPolicyType(- "instance",- "bb.policy.slow-query"- );+ const policyList = await useSlowQueryPolicyStore().fetchPolicyList();const requestList = policyList.filter((policy) => {- const payload = policy.payload as SlowQueryPolicyPayload;- return payload.active;+ return policy.slowQueryPolicy?.active;}).map((policy) => {- const instanceId = policy.resourceId;+ const instanceId = policy.resourceUid;const instance = instanceStore.getInstanceById(instanceId);return slowQueryStore.syncSlowQueriesByInstance(instance);});diff --git a/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue b/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vueindex a8b0a6ed2d..85a5c1abff 100644--- a/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue+++ b/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue@@ -58,17 +58,14 @@ import {Environment,EnvironmentId,Instance,- SlowQueryPolicyPayload,UNKNOWN_ID,} from "@/types";import { EnvironmentTabFilter, SearchBox } from "@/components/v2";import { SlowQueryPolicyTable } from "./components";-import {- extractSlowQueryPolicyPayload,- instanceSupportSlowQuery,-} from "@/utils";+import { instanceSupportSlowQuery } from "@/utils";import { useGracefulRequest } from "@/store/modules/utils";import LearnMoreLink from "@/components/LearnMoreLink.vue";+import { getInstancePathByLegacyInstance } from "@/store/modules/v1/common";type LocalState = {ready: boolean;@@ -89,25 +86,21 @@ const state = reactive<LocalState>({});const { t } = useI18n();-const policyStore = useSlowQueryPolicyStore();+const slowQueryPolicyStore = useSlowQueryPolicyStore();const slowQueryStore = useSlowQueryStore();const instanceStore = useInstanceStore();const environmentList = useEnvironmentList(["NORMAL"]);const policyList = computed(() => {- return policyStore.getPolicyListByResourceTypeAndPolicyType(- "instance",- "bb.policy.slow-query"- );+ return slowQueryPolicyStore.getPolicyList();});const composedSlowQueryPolicyList = computed(() => {const list = state.instanceList.map<ComposedSlowQueryPolicy>((instance) => {- const policy = policyList.value.find((p) => p.resourceId === instance.id);- const payload = extractSlowQueryPolicyPayload(policy);+ const policy = policyList.value.find((p) => p.resourceUid == instance.id);return {instance,- active: payload.active,+ active: policy?.slowQueryPolicy?.active ?? false,};});@@ -147,10 +140,7 @@ const prepare = async () => {state.instanceList = list.filter(instanceSupportSlowQuery);};const preparePolicyList = async () => {- await policyStore.fetchPolicyListByResourceTypeAndPolicyType(- "instance",- "bb.policy.slow-query"- );+ await slowQueryPolicyStore.fetchPolicyList();};await Promise.all([prepareInstanceList(), preparePolicyList()]);} finally {@@ -162,18 +152,14 @@ const changeEnvironment = (id: EnvironmentId | undefined) => {state.filter.environment = environmentList.value.find((env) => env.id === id);};-const patchInstanceSlowQueryPolicy = (instance: Instance, active: boolean) => {- const payload: SlowQueryPolicyPayload = {+const patchInstanceSlowQueryPolicy = async (+ instance: Instance,+ active: boolean+) => {+ return slowQueryPolicyStore.upsertPolicy({+ parentPath: getInstancePathByLegacyInstance(instance),active,- };- return policyStore.upsertPolicyByResourceTypeAndPolicyType(- "instance",- instance.id,- "bb.policy.slow-query",- {- payload,- }- );+ });};const toggleActive = async (instance: Instance, active: boolean) => {diff --git a/frontend/src/store/modules/slowQueryPolicy.ts b/frontend/src/store/modules/slowQueryPolicy.tsindex ad040e78b2..56da637c05 100644--- a/frontend/src/store/modules/slowQueryPolicy.ts+++ b/frontend/src/store/modules/slowQueryPolicy.ts@@ -1,174 +1,88 @@import { defineStore } from "pinia";import { computed, ref, watchEffect } from "vue";-import axios from "axios";-import { IdType, ResourceObject, unknown, UNKNOWN_ID } from "@/types";+import { UNKNOWN_ID } from "@/types";+import { useCurrentUser } from "./auth";+import { usePolicyV1Store } from "./v1/policy";import {- PipelineApprovalPolicyPayload,Policy,- PolicyResourceType,PolicyType,- PolicyUpsert,- SensitiveDataPolicyPayload,-} from "@/types/policy";-import { useCurrentUser } from "./auth";--function convert(- resourceType: PolicyResourceType,- policy: ResourceObject,- includedList: ResourceObject[]-): Policy {- const result: Policy = {- ...(policy.attributes as Omit<Policy, "id" | "environment" | "payload">),- id: parseInt(policy.id),- resourceType,- environment: unknown("ENVIRONMENT"),- payload: JSON.parse((policy.attributes.payload as string) || "{}"),- };-- if (result.type === "bb.policy.pipeline-approval") {- const payload = result.payload as PipelineApprovalPolicyPayload;- if (!payload.assigneeGroupList) {- // Assign an empty array as fallback- 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 = [];- }- }+ PolicyResourceType,+ policyTypeToJSON,+} from "@/types/proto/v1/org_policy_service";+import { policyNamePrefix } from "@/store/modules/v1/common";- return result;-}+const getSlowQueryPolicyName = (parentPath: string): string => {+ return `${parentPath}/${policyNamePrefix}${policyTypeToJSON(+ PolicyType.SLOW_QUERY+ )}`;+};export const useSlowQueryPolicyStore = defineStore("slow-query-policy", () => {- const policyMapByResourceType = ref(- new Map<PolicyResourceType, Map<PolicyType, Policy[]>>()- );+ const policyMapByName = ref(new Map<string, Policy>());- const getPolicyListByResourceTypeAndPolicyType = (- resourceType: PolicyResourceType,- policyType: PolicyType- ) => {- return (- policyMapByResourceType.value.get(resourceType)?.get(policyType) ?? []- );+ const getPolicyList = () => {+ return [...policyMapByName.value.values()];};- const setPolicyListByResourceTypeAndPolicyType = (- resourceType: PolicyResourceType,- policyType: PolicyType,- policyList: Policy[]- ) => {- const map = policyMapByResourceType.value.get(resourceType);- if (map) {- map.set(policyType, policyList);- } else {- policyMapByResourceType.value.set(- resourceType,- new Map([[policyType, policyList]])- );- }- };- const updatePolicy = (policy: Policy) => {- const { resourceType, type } = policy;- const policyList = getPolicyListByResourceTypeAndPolicyType(- resourceType,- type- );- const index = policyList.findIndex(- (p) => p.resourceId === policy.resourceId- );- if (index >= 0) {- policyList[index] = policy;- } else {- policyList.push(policy);- }- setPolicyListByResourceTypeAndPolicyType(resourceType, type, policyList);++ const getPolicy = (name: string) => {+ return policyMapByName.value.get(name);};- const removePolicy = (- resourceType: PolicyResourceType,- resourceId: IdType,- policyType: PolicyType- ) => {- const policyList = getPolicyListByResourceTypeAndPolicyType(- resourceType,- policyType- );- const index = policyList.findIndex((p) => p.resourceId === resourceId);- if (index >= 0) {- policyList.splice(index, 1);- setPolicyListByResourceTypeAndPolicyType(- resourceType,- policyType,- policyList- );++ const setPolicyList = (policyList: Policy[]) => {+ for (const policy of policyList) {+ policyMapByName.value.set(policy.name, policy);}};- const fetchPolicyListByResourceTypeAndPolicyType = async (- 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(resourceType, d, data.included)- );- setPolicyListByResourceTypeAndPolicyType(- resourceType,- policyType,- policyList- );+ const fetchPolicyList = async () => {+ const policyStore = usePolicyV1Store();+ const policyList = await policyStore.fetchPolicies({+ resourceType: PolicyResourceType.INSTANCE,+ policyType: PolicyType.SLOW_QUERY,+ showDeleted: true,+ });++ setPolicyList(policyList);return policyList;};- const upsertPolicyByResourceTypeAndPolicyType = async (- resourceType: PolicyResourceType,- resourceId: IdType,- policyType: PolicyType,- policyUpsert: PolicyUpsert- ) => {- const data = (- await axios.patch(- `/api/policy/${resourceType}/${resourceId}?type=${policyType}`,- {- data: {- type: "policyUpsert",- attributes: {- rowStatus: policyUpsert.rowStatus,- payload: policyUpsert.payload- ? JSON.stringify(policyUpsert.payload)- : undefined,- },- },- }- )- ).data;- const policy = convert(resourceType, data.data, data.included);- updatePolicy(policy);+ const upsertPolicy = async ({+ parentPath,+ active,+ }: {+ parentPath: string;+ active: boolean;+ }) => {+ const policyStore = usePolicyV1Store();+ let policy: Policy;+ if (!getPolicy(getSlowQueryPolicyName(parentPath))) {+ policy = await await policyStore.createPolicy(parentPath, {+ type: PolicyType.SLOW_QUERY,+ slowQueryPolicy: {+ active,+ },+ });+ } else {+ policy = await policyStore.updatePolicy(["payload"], {+ name: getSlowQueryPolicyName(parentPath),+ type: PolicyType.SLOW_QUERY,+ slowQueryPolicy: {+ active,+ },+ });+ }++ if (policy) {+ policyMapByName.value.set(policy.name, policy);+ }return policy;};- const deletePolicyByResourceTypeAndPolicyType = async (- resourceType: PolicyResourceType,- resourceId: IdType,- policyType: PolicyType- ) => {- await axios.delete(- `/api/policy/${resourceType}/${resourceId}?type=${policyType}`- );- removePolicy(resourceType, resourceId, policyType);- };return {- getPolicyListByResourceTypeAndPolicyType,- fetchPolicyListByResourceTypeAndPolicyType,- upsertPolicyByResourceTypeAndPolicyType,- deletePolicyByResourceTypeAndPolicyType,+ getPolicyList,+ fetchPolicyList,+ upsertPolicy,};});@@ -176,24 +90,18 @@ export const useSlowQueryPolicyList = () => {const store = useSlowQueryPolicyStore();const currentUser = useCurrentUser();const ready = ref(false);+watchEffect(() => {if (currentUser.value.id === UNKNOWN_ID) return;-ready.value = false;- store- .fetchPolicyListByResourceTypeAndPolicyType(- "instance",- "bb.policy.slow-query"- )- .finally(() => {- ready.value = true;- });+ store.fetchPolicyList().finally(() => {+ ready.value = true;+ });});+const list = computed(() => {- return store.getPolicyListByResourceTypeAndPolicyType(- "instance",- "bb.policy.slow-query"- );+ return store.getPolicyList();});+return { list, ready };};diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.tsindex f0922d28d7..e8cc359d67 100644--- a/frontend/src/store/modules/v1/common.ts+++ b/frontend/src/store/modules/v1/common.ts@@ -1,4 +1,4 @@-import { ResourceId, UNKNOWN_ID } from "@/types";+import { ResourceId, UNKNOWN_ID, Instance } from "@/types";export const userNamePrefix = "users/";export const environmentNamePrefix = "environments/";@@ -43,3 +43,7 @@ export const getIdentityProviderResourceId = (name: string): ResourceId => {const tokens = getNameParentTokens(name, [idpNamePrefix]);return tokens[0];};++export const getInstancePathByLegacyInstance = (instance: Instance): string => {+ return `${environmentNamePrefix}${instance.environment.resourceId}/${instanceNamePrefix}${instance.resourceId}`;+};diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.tsindex 31b2d3b2d9..d0e8310bfb 100644--- a/frontend/src/types/policy.ts+++ b/frontend/src/types/policy.ts@@ -1,17 +1,8 @@-import {- RowStatus,- Environment,- IssueType,- PolicyId,- RuleType,- RuleLevel,- SubsetOf,-} from ".";+import { RowStatus, Environment, IssueType, PolicyId, SubsetOf } from ".";export type PolicyType =| "bb.policy.pipeline-approval"| "bb.policy.backup-plan"- | "bb.policy.sql-review"| "bb.policy.environment-tier"| "bb.policy.sensitive-data"| "bb.policy.access-control"@@ -46,17 +37,6 @@ export type BackupPlanPolicyPayload = {export const DefaultSchedulePolicy: BackupPlanPolicySchedule = "UNSET";-// SQLReviewPolicyPayload is the payload for SQL review policy in the backend.-export type SQLReviewPolicyPayload = {- name: string;- ruleList: {- type: RuleType;- level: RuleLevel;- payload: string;- comment: string;- }[];-};-export type AssigneeGroupValue = "WORKSPACE_OWNER_OR_DBA" | "PROJECT_OWNER";export const DefaultAssigneeGroup: AssigneeGroupValue =@@ -93,18 +73,12 @@ export type AccessControlPolicyPayload = {disallowRuleList: AccessControlRule[];};-export type SlowQueryPolicyPayload = {- active: boolean;-};-export type PolicyPayload =| PipelineApprovalPolicyPayload| BackupPlanPolicyPayload- | SQLReviewPolicyPayload| EnvironmentTierPolicyPayload| SensitiveDataPolicyPayload- | AccessControlPolicyPayload- | SlowQueryPolicyPayload;+ | AccessControlPolicyPayload;export type PolicyResourceType =| ""diff --git a/frontend/src/utils/slowQuery.ts b/frontend/src/utils/slowQuery.tsindex f186de5e5f..468672cf64 100644--- a/frontend/src/utils/slowQuery.ts+++ b/frontend/src/utils/slowQuery.ts@@ -1,17 +1,6 @@-import type {- EngineType,- Instance,- Policy,- SlowQueryPolicyPayload,-} from "@/types";+import type { EngineType, Instance } from "@/types";import { semverCompare } from "./util";-export const extractSlowQueryPolicyPayload = (- policy: Policy | undefined-): SlowQueryPolicyPayload => {- if (!policy) return { active: false };- return policy.payload as SlowQueryPolicyPayload;-};export const InstanceListSupportSlowQuery: [EngineType, string][] = [["MYSQL", "5.7"],["POSTGRES", "0"],