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: support change risk source (#15235)
ecmadao7ff42bb50bc9
diff --git a/backend/api/v1/risk_service.go b/backend/api/v1/risk_service.goindex a4f1934dcc..ef45b73bb4 100644--- a/backend/api/v1/risk_service.go+++ b/backend/api/v1/risk_service.go@@ -33,7 +33,7 @@ func NewRiskService(store *store.Store, licenseService enterprise.LicenseServicefunc convertToRisk(risk *store.RiskMessage) (*v1pb.Risk, error) {return &v1pb.Risk{Name: fmt.Sprintf("%s%v", common.RiskPrefix, risk.ID),- Source: convertToSource(risk.Source),+ Source: convertToV1Source(risk.Source),Title: risk.Name,Level: risk.Level,Condition: risk.Expression,@@ -69,7 +69,7 @@ func (s *RiskService) CreateRisk(ctx context.Context, request *v1pb.CreateRiskRe}risk, err := s.store.CreateRisk(ctx, &store.RiskMessage{- Source: convertSource(request.Risk.Source),+ Source: convertToSource(request.Risk.Source),Level: request.Risk.Level,Name: request.Risk.Title,Active: request.Risk.Active,@@ -115,6 +115,11 @@ func (s *RiskService) UpdateRisk(ctx context.Context, request *v1pb.UpdateRiskRereturn nil, status.Errorf(codes.InvalidArgument, "failed to validate risk expression, error: %v", err)}patch.Expression = request.Risk.Condition+ case "source":+ source := convertToSource(request.Risk.Source)+ if risk.Source != source {+ patch.Source = &source+ }}}@@ -147,7 +152,7 @@ func (s *RiskService) DeleteRisk(ctx context.Context, request *v1pb.DeleteRiskRereturn &emptypb.Empty{}, nil}-func convertToSource(source store.RiskSource) v1pb.Risk_Source {+func convertToV1Source(source store.RiskSource) v1pb.Risk_Source {switch source {case store.RiskSourceDatabaseCreate:return v1pb.Risk_CREATE_DATABASE@@ -165,7 +170,7 @@ func convertToSource(source store.RiskSource) v1pb.Risk_Source {return v1pb.Risk_SOURCE_UNSPECIFIED}-func convertSource(source v1pb.Risk_Source) store.RiskSource {+func convertToSource(source v1pb.Risk_Source) store.RiskSource {switch source {case v1pb.Risk_CREATE_DATABASE:return store.RiskSourceDatabaseCreatediff --git a/backend/store/risk.go b/backend/store/risk.goindex 42bab241ca..55590060c9 100644--- a/backend/store/risk.go+++ b/backend/store/risk.go@@ -51,6 +51,7 @@ type UpdateRiskMessage struct {Active *boolLevel *int32Expression *expr.Expr+ Source *RiskSource}// GetRisk gets a risk.@@ -222,6 +223,9 @@ func (s *Store) UpdateRisk(ctx context.Context, patch *UpdateRiskMessage, id intif v := patch.Level; v != nil {set, args = append(set, fmt.Sprintf("level = $%d", len(args)+1)), append(args, *v)}+ if v := patch.Source; v != nil {+ set, args = append(set, fmt.Sprintf("source = $%d", len(args)+1)), append(args, *v)+ }if v := patch.Expression; v != nil {expressionBytes, err := protojson.Marshal(patch.Expression)if err != nil {diff --git a/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskForm.vue b/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskForm.vueindex fd025b241c..da8443ad90 100644--- a/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskForm.vue+++ b/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskForm.vue@@ -31,7 +31,8 @@</label><RiskSourceSelectv-model:value="state.risk.source"- :disabled="mode === 'EDIT' || !allowAdmin"+ :disabled="sourceList.length <= 1 || !allowAdmin"+ :sources="sourceList"@update:value="$emit('update')"/></div>@@ -90,12 +91,13 @@</template><script lang="ts" setup>-import { cloneDeep, head } from "lodash-es";+import { cloneDeep, head, uniq, flatten } from "lodash-es";import { NButton, NInput } from "naive-ui";import { computed, ref, watch } from "vue";import ExprEditor from "@/components/ExprEditor";import LearnMoreLink from "@/components/LearnMoreLink.vue";import type { ConditionGroupExpr, SimpleExpr } from "@/plugins/cel";+import { ExprType } from "@/plugins/cel";import {resolveCELExpr,buildCELExpr,@@ -103,8 +105,9 @@ import {validateSimpleExpr,emptySimpleExpr,} from "@/plugins/cel";+import { useSupportedSourceList } from "@/types";import { Expr } from "@/types/proto/google/type/expr";-import { Risk } from "@/types/proto/v1/risk_service";+import { Risk, Risk_Source } from "@/types/proto/v1/risk_service";import {batchConvertCELStringToParsedExpr,batchConvertParsedExprToCELString,@@ -114,6 +117,7 @@ import {getFactorList,getFactorOptionsMap,factorSupportDropdown,+ RiskSourceFactorMap,} from "../../common/utils";import { useRiskCenterContext } from "../context";import RiskLevelSelect from "./RiskLevelSelect.vue";@@ -137,6 +141,7 @@ const emit = defineEmits<{const context = useRiskCenterContext();const { allowAdmin } = context;+const SupportedSourceList = useSupportedSourceList();const state = ref<LocalState>({risk: Risk.fromPartial({}),@@ -144,6 +149,37 @@ const state = ref<LocalState>({});const mode = computed(() => context.dialog.value?.mode ?? "CREATE");+const extractFactorList = (expr: SimpleExpr): string[] => {+ switch (expr.type) {+ case ExprType.Condition:+ return expr.args.length > 1 ? [expr.args[0]] : [];+ case ExprType.ConditionGroup:+ return uniq(flatten(expr.args.map(extractFactorList)));+ case ExprType.RawString:+ return [];+ }+};++const sourceList = computed(() => {+ if (mode.value !== "EDIT") {+ return SupportedSourceList.value;+ }++ const selectedFactor = extractFactorList(state.value.expr);+ const sourceList: Risk_Source[] = [];++ for (const [source, factorList] of RiskSourceFactorMap.entries()) {+ if (!SupportedSourceList.value.includes(source)) {+ continue;+ }+ if (selectedFactor.every((v) => factorList.includes(v))) {+ sourceList.push(source);+ }+ }++ return sourceList;+});+const resolveLocalState = async () => {const risk = cloneDeep(context.dialog.value!.risk);diff --git a/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskSourceSelect.vue b/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskSourceSelect.vueindex 688be5095a..4c207b1d35 100644--- a/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskSourceSelect.vue+++ b/frontend/src/components/CustomApproval/Settings/components/RiskCenter/RiskDialog/RiskSourceSelect.vue@@ -14,7 +14,6 @@import type { SelectOption } from "naive-ui";import { NSelect, type SelectProps } from "naive-ui";import { computed } from "vue";-import { useSupportedSourceList } from "@/types";import type { Risk_Source } from "@/types/proto/v1/risk_service";import { sourceText } from "../../common";import { useRiskCenterContext } from "../context";@@ -22,9 +21,10 @@ import { useRiskCenterContext } from "../context";export interface RiskSourceSelectProps extends /* @vue-ignore */ SelectProps {value: Risk_Source;disabled?: boolean;+ sources: Risk_Source[];}-defineProps<RiskSourceSelectProps>();+const props = defineProps<RiskSourceSelectProps>();defineEmits<{(event: "update:value", source: Risk_Source | undefined): void;@@ -32,10 +32,9 @@ defineEmits<{const context = useRiskCenterContext();const { allowAdmin } = context;-const SupportedSourceList = useSupportedSourceList();const options = computed(() => {- return SupportedSourceList.value.map<SelectOption>((source) => ({+ return props.sources.map<SelectOption>((source) => ({label: sourceText(source),value: source,}));diff --git a/frontend/src/components/CustomApproval/Settings/components/common/utils.ts b/frontend/src/components/CustomApproval/Settings/components/common/utils.tsindex b5b81c7e20..34c6342380 100644--- a/frontend/src/components/CustomApproval/Settings/components/common/utils.ts+++ b/frontend/src/components/CustomApproval/Settings/components/common/utils.ts@@ -78,88 +78,92 @@ const StringFactorList = ["table_name",] as const;-const FactorList = {- DDL: uniq(- without(- [...NumberFactorList, ...StringFactorList],- "level",- "source",- "expiration_days",- "export_rows"- )- ),- DML: uniq(- without(- [...NumberFactorList, ...StringFactorList],- "level",- "source",- "expiration_days",- "export_rows"- )- ),- CreateDatabase: without(- [...StringFactorList],- "sql_type",- "table_name",- "expiration_days",- "export_rows"- ),- DataExport: uniq(- without(- [...StringFactorList, ...NumberFactorList],- "level",- "affected_rows",- "table_rows",- "source",- "sql_type",- "table_name",- "expiration_days",- "export_rows"- )- ),- RequestQuery: uniq(- without(- [...StringFactorList, ...NumberFactorList],- "level",- "source",- "affected_rows",- "table_rows",- "sql_type",- "table_name",- "export_rows"- )- ),- RequestExport: uniq(- without(- [...StringFactorList, ...NumberFactorList],- "level",- "source",- "affected_rows",- "table_rows",- "sql_type",- "table_name"- )- ),-};+export const RiskSourceFactorMap: Map<Risk_Source, string[]> = new Map([+ [+ Risk_Source.DDL,+ uniq(+ without(+ [...NumberFactorList, ...StringFactorList],+ "level",+ "source",+ "expiration_days",+ "export_rows"+ )+ ),+ ],+ [+ Risk_Source.DML,+ uniq(+ without(+ [...NumberFactorList, ...StringFactorList],+ "level",+ "source",+ "expiration_days",+ "export_rows"+ )+ ),+ ],+ [+ Risk_Source.CREATE_DATABASE,+ uniq(+ without(+ [...StringFactorList],+ "sql_type",+ "table_name",+ "expiration_days",+ "export_rows"+ )+ ),+ ],+ [+ Risk_Source.DATA_EXPORT,+ uniq(+ without(+ [...StringFactorList, ...NumberFactorList],+ "level",+ "affected_rows",+ "table_rows",+ "source",+ "sql_type",+ "table_name",+ "expiration_days",+ "export_rows"+ )+ ),+ ],+ [+ Risk_Source.REQUEST_QUERY,+ uniq(+ without(+ [...StringFactorList, ...NumberFactorList],+ "level",+ "source",+ "affected_rows",+ "table_rows",+ "sql_type",+ "table_name",+ "export_rows"+ )+ ),+ ],+ [+ Risk_Source.REQUEST_EXPORT,+ uniq(+ without(+ [...StringFactorList, ...NumberFactorList],+ "level",+ "source",+ "affected_rows",+ "table_rows",+ "sql_type",+ "table_name"+ )+ ),+ ],+]);export const getFactorList = (source: Risk_Source) => {- switch (source) {- case Risk_Source.DDL:- return [...FactorList.DDL];- case Risk_Source.DML:- return [...FactorList.DML];- case Risk_Source.CREATE_DATABASE:- return [...FactorList.CreateDatabase];- case Risk_Source.DATA_EXPORT:- return [...FactorList.DataExport];- case Risk_Source.REQUEST_QUERY:- return [...FactorList.RequestQuery];- case Risk_Source.REQUEST_EXPORT:- return [...FactorList.RequestExport];- default:- // unsupported namespace- return [];- }+ return RiskSourceFactorMap.get(source) ?? [];};const getEnvironmentIdOptions = () => {diff --git a/frontend/src/store/modules/risk.ts b/frontend/src/store/modules/risk.tsindex 08ac70f7fa..7113060e09 100644--- a/frontend/src/store/modules/risk.ts+++ b/frontend/src/store/modules/risk.ts@@ -20,7 +20,7 @@ export const useRiskStore = defineStore("risk", () => {// updateconst updated = await riskServiceClient.updateRisk({risk,- updateMask: ["title", "level", "active", "condition"],+ updateMask: ["title", "level", "active", "condition", "source"],});Object.assign(existedRisk, updated);} else {