Skip to content

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

feat: update cel transformer for export data approval flow (#6242)

boojackc6f4f01f2b21
diff --git a/frontend/src/components/Issue/IssueStatusTransitionButtonGroup.vue b/frontend/src/components/Issue/IssueStatusTransitionButtonGroup.vue
index 91e31fed65..50b06c8a65 100644
--- a/frontend/src/components/Issue/IssueStatusTransitionButtonGroup.vue
+++ b/frontend/src/components/Issue/IssueStatusTransitionButtonGroup.vue
@@ -581,7 +581,8 @@ const allowCreate = computed(() => {
const createContext = newIssue.createContext as GrantRequestContext;
if (createContext.role === "EXPORTER") {
return (
- createContext.databases.length === 1 && createContext.statement !== ""
+ createContext.databaseResources.length > 0 &&
+ createContext.statement !== ""
);
}
}
diff --git a/frontend/src/components/Issue/form/GrantRequestExporterForm.vue b/frontend/src/components/Issue/form/GrantRequestExporterForm.vue
index c9b3b584e4..b5fb24016f 100644
--- a/frontend/src/components/Issue/form/GrantRequestExporterForm.vue
+++ b/frontend/src/components/Issue/form/GrantRequestExporterForm.vue
@@ -124,11 +124,7 @@ import {
UNKNOWN_ID,
dialectOfEngine,
} from "@/types";
-import {
- getDatabaseIdByName,
- memberListInProjectV1,
- parseConditionExpressionString,
-} from "@/utils";
+import { memberListInProjectV1 } from "@/utils";
import {
convertUserToPrincipal,
useDatabaseStore,
@@ -136,12 +132,15 @@ import {
} from "@/store";
import MonacoEditor from "@/components/MonacoEditor";
import RequiredStar from "@/components/RequiredStar.vue";
+import { DatabaseResource } from "./SelectDatabaseResourceForm/common";
+import { converFromCEL } from "@/utils/issue/cel";
interface LocalState {
// For creating
projectId?: string;
environmentId?: string;
databaseId?: DatabaseId;
+ selectedDatabaseResourceList: DatabaseResource[];
maxRowCount: number;
exportFormat: "CSV" | "JSON";
statement: string;
@@ -150,6 +149,7 @@ interface LocalState {
const { create, issue } = useIssueLogic();
const databaseStore = useDatabaseStore();
const state = reactive<LocalState>({
+ selectedDatabaseResourceList: [],
maxRowCount: 1000,
exportFormat: "CSV",
statement: "",
@@ -234,6 +234,7 @@ const handleStatementChange = (value: string) => {
watch(
() => [
state.databaseId,
+ state.selectedDatabaseResourceList,
state.maxRowCount,
state.exportFormat,
state.statement,
@@ -243,9 +244,13 @@ watch(
const context = (issue.value as IssueCreate)
.createContext as GrantRequestContext;
if (state.databaseId) {
- context.databases = [state.databaseId as string];
+ context.databaseResources = [
+ {
+ databaseId: state.databaseId,
+ },
+ ];
} else {
- context.databases = [];
+ context.databaseResources = [];
}
context.maxRowCount = state.maxRowCount;
context.exportFormat = state.exportFormat;
@@ -267,24 +272,20 @@ watch(
throw "Only support EXPORTER role";
}
- const conditionExpression = parseConditionExpressionString(
+ const conditionExpression = await converFromCEL(
payload.condition.expression
);
- if (conditionExpression.statement !== undefined) {
- state.statement = conditionExpression.statement;
- }
if (
- conditionExpression.databases !== undefined &&
- conditionExpression.databases.length > 0
+ conditionExpression.databaseResources !== undefined &&
+ conditionExpression.databaseResources.length > 0
) {
- const databaseIdList = [];
- for (const databaseName of conditionExpression.databases) {
- const id = await getDatabaseIdByName(databaseName);
- if (id && id !== UNKNOWN_ID) {
- databaseIdList.push(id);
- }
+ const resource = head(conditionExpression.databaseResources);
+ if (resource) {
+ state.databaseId = resource.databaseId;
}
- state.databaseId = head(databaseIdList);
+ }
+ if (conditionExpression.statement !== undefined) {
+ state.statement = conditionExpression.statement;
}
if (conditionExpression.rowLimit !== undefined) {
state.maxRowCount = conditionExpression.rowLimit;
diff --git a/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue b/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
index b6503338c3..ae6f89e5d7 100644
--- a/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
+++ b/frontend/src/components/Issue/form/GrantRequestQuerierForm.vue
@@ -136,13 +136,12 @@
</template>
<script lang="ts" setup>
-import { head, uniq } from "lodash-es";
+import { head } from "lodash-es";
import { NRadioGroup, NRadio, NInputNumber } from "naive-ui";
import { computed, onMounted, reactive, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useIssueLogic } from "../logic";
import {
- DatabaseId,
GrantRequestContext,
GrantRequestPayload,
Issue,
@@ -150,13 +149,13 @@ import {
PresetRoleType,
UNKNOWN_ID,
} from "@/types";
-import { getDatabaseIdByName, memberListInProjectV1 } from "@/utils";
+import { memberListInProjectV1 } from "@/utils";
import {
convertUserToPrincipal,
useDatabaseStore,
useProjectV1Store,
} from "@/store";
-import { converFromCEL, stringifyDatabaseResources } from "@/utils/issue/cel";
+import { converFromCEL } from "@/utils/issue/cel";
import { DatabaseResource } from "./SelectDatabaseResourceForm/common";
import RequiredStar from "@/components/RequiredStar.vue";
import SelectDatabaseResourceForm from "./SelectDatabaseResourceForm/index.vue";
@@ -167,7 +166,6 @@ interface LocalState {
// For creating
projectId?: string;
allDatabases: boolean;
- selectedDatabaseIdList: DatabaseId[];
selectedDatabaseResourceList: DatabaseResource[];
expireDays: number;
customDays: number;
@@ -182,7 +180,6 @@ const state = reactive<LocalState>({
showSelectDatabasePanel: false,
projectId: undefined,
allDatabases: true,
- selectedDatabaseIdList: [],
selectedDatabaseResourceList: [],
expireDays: 7,
customDays: 7,
@@ -248,14 +245,15 @@ const handleProjectSelect = async (projectId: string) => {
const ownerPrincipal = convertUserToPrincipal(projectOwner.user);
(issue.value as IssueCreate).assigneeId = ownerPrincipal.id;
}
- state.selectedDatabaseIdList = state.selectedDatabaseIdList.filter((id) => {
- const database = databaseStore.getDatabaseById(id);
- return String(database.project.id) === projectId;
- });
+ state.selectedDatabaseResourceList =
+ state.selectedDatabaseResourceList.filter((resource) => {
+ const database = databaseStore.getDatabaseById(resource.databaseId);
+ return String(database.project.id) === projectId;
+ });
};
const handleManuallySelectClick = () => {
- if (state.selectedDatabaseIdList.length === 0) {
+ if (state.selectedDatabaseResourceList.length === 0) {
state.showSelectDatabasePanel = true;
}
};
@@ -266,7 +264,6 @@ const handleSelectedDatabaseResourceChanged = (
state.selectedDatabaseResourceList = databaseResourceList;
state.showSelectDatabasePanel = false;
state.allDatabases = false;
- stringifyDatabaseResources(databaseResourceList);
if (create.value) {
(
@@ -310,19 +307,6 @@ watch(
conditionExpression.expiredTime
).toLocaleString();
}
- if (
- conditionExpression.databases !== undefined &&
- conditionExpression.databases.length > 0
- ) {
- const databaseIdList = [];
- for (const databaseName of conditionExpression.databases) {
- const id = await getDatabaseIdByName(databaseName);
- if (id && id !== UNKNOWN_ID) {
- databaseIdList.push(id);
- }
- }
- state.selectedDatabaseIdList = uniq(databaseIdList);
- }
if (
conditionExpression.databaseResources !== undefined &&
conditionExpression.databaseResources.length > 0
diff --git a/frontend/src/components/Issue/form/SelectDatabaseResourceForm/index.vue b/frontend/src/components/Issue/form/SelectDatabaseResourceForm/index.vue
index f80cfb87f7..71b428c3cd 100644
--- a/frontend/src/components/Issue/form/SelectDatabaseResourceForm/index.vue
+++ b/frontend/src/components/Issue/form/SelectDatabaseResourceForm/index.vue
@@ -45,9 +45,11 @@ import {
import Label from "./Label.vue";
import { watch } from "vue";
import { uniq } from "lodash-es";
+import { IdType } from "@/types";
const props = defineProps<{
projectId: string;
+ databaseId?: IdType;
selectedDatabaseResourceList: DatabaseResource[];
}>();
@@ -95,7 +97,10 @@ onMounted(async () => {
});
const databaseList = computed(() => {
- return databaseStore.getDatabaseListByProjectId(props.projectId);
+ const list = databaseStore.getDatabaseListByProjectId(props.projectId);
+ return props.databaseId
+ ? list.filter((item) => item.id === props.databaseId)
+ : list;
});
const allowConfirm = computed(() => {
diff --git a/frontend/src/components/Issue/logic/GrantRequestModeProvider.ts b/frontend/src/components/Issue/logic/GrantRequestModeProvider.ts
index 8b86fbdaee..b006f9192e 100644
--- a/frontend/src/components/Issue/logic/GrantRequestModeProvider.ts
+++ b/frontend/src/components/Issue/logic/GrantRequestModeProvider.ts
@@ -1,7 +1,7 @@
import { cloneDeep } from "lodash-es";
import { defineComponent, onMounted } from "vue";
import { GrantRequestContext, IssueCreate } from "@/types";
-import { useCurrentUserV1, useDatabaseStore, useProjectV1Store } from "@/store";
+import { useCurrentUserV1, useProjectV1Store } from "@/store";
import { provideIssueLogic, useCommonLogic, useIssueLogic } from "./index";
import { stringifyDatabaseResources } from "@/utils/issue/cel";
@@ -9,7 +9,6 @@ export default defineComponent({
name: "GrantRequestModeProvider",
setup() {
const { issue, createIssue } = useIssueLogic();
- const databaseStore = useDatabaseStore();
onMounted(() => {
useProjectV1Store().fetchProjectList();
@@ -21,7 +20,6 @@ export default defineComponent({
const context: GrantRequestContext = {
...{
- databases: [],
databaseResources: [],
expireDays: 7,
maxRowCount: 1000,
@@ -46,20 +44,18 @@ export default defineComponent({
);
} else if (context.role === "EXPORTER") {
if (
- !Array.isArray(context.databases) ||
- context.databases.length === 0
+ !Array.isArray(context.databaseResources) ||
+ context.databaseResources.length === 0
) {
throw "Exporter must have at least one database";
}
- const databaseId = context.databases[0];
- const database = await databaseStore.getOrFetchDatabaseById(databaseId);
- const databaseNames = [];
- databaseNames.push(
- `instances/${database.instance.resourceId}/databases/${database.name}`
- );
- expression.push(
- `resource.database in ${JSON.stringify(databaseNames)}`
- );
+ if (
+ Array.isArray(context.databaseResources) &&
+ context.databaseResources.length > 0
+ ) {
+ const cel = stringifyDatabaseResources(context.databaseResources);
+ expression.push(cel);
+ }
expression.push(`request.statement == "${btoa(context.statement)}"`);
expression.push(`request.row_limit == ${context.maxRowCount}`);
expression.push(`request.export_format == "${context.exportFormat}"`);
diff --git a/frontend/src/plugins/cel/types/factor.ts b/frontend/src/plugins/cel/types/factor.ts
index bdffc40866..0503418efb 100644
--- a/frontend/src/plugins/cel/types/factor.ts
+++ b/frontend/src/plugins/cel/types/factor.ts
@@ -1,4 +1,12 @@
-export const NumberFactorList = ["affected_rows", "level", "source"] as const;
+export const NumberFactorList = [
+ // Risk related factors
+ "affected_rows",
+ "level",
+ "source",
+
+ // Grant request issue related factors
+ "request.row_limit",
+] as const;
export type NumberFactor = typeof NumberFactorList[number];
export const StringFactorList = [
@@ -13,6 +21,8 @@ export const StringFactorList = [
"resource.database",
"resource.schema",
"resource.table",
+ "request.statement",
+ "request.export_format",
] as const;
export type StringFactor = typeof StringFactorList[number];
diff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.ts
index b306d37e93..3d0111a63b 100644
--- a/frontend/src/types/issue.ts
+++ b/frontend/src/types/issue.ts
@@ -92,7 +92,6 @@ export type EmptyContext = {};
export interface GrantRequestContext {
role: "EXPORTER" | "QUERIER";
// Conditions in CEL expression.
- databases: string[];
databaseResources: DatabaseResource[];
expireDays: number;
maxRowCount: number;
diff --git a/frontend/src/utils/issue/cel.ts b/frontend/src/utils/issue/cel.ts
index e4a6a8a885..e24168d7b0 100644
--- a/frontend/src/utils/issue/cel.ts
+++ b/frontend/src/utils/issue/cel.ts
@@ -198,17 +198,30 @@ export const converFromCEL = async (
}
} else if (expr.operator === "_==_") {
const [left, right] = expr.args;
- if (typeof left === "string" && typeof right === "string") {
- if (left === "resource.database") {
- const databaseId = await getDatabaseIdByName(right);
- const databaseResource: DatabaseResource = {
- databaseId: databaseId,
- };
- conditionExpression.databaseResources!.push(databaseResource);
- } else if (left === "resource.schema") {
- const databaseResource = last(conditionExpression.databaseResources);
- if (databaseResource) {
- databaseResource.schema = right;
+ if (typeof left === "string") {
+ if (typeof right === "string") {
+ if (left === "resource.database") {
+ const databaseId = await getDatabaseIdByName(right);
+ const databaseResource: DatabaseResource = {
+ databaseId: databaseId,
+ };
+ conditionExpression.databaseResources!.push(databaseResource);
+ } else if (left === "resource.schema") {
+ const databaseResource = last(
+ conditionExpression.databaseResources
+ );
+ if (databaseResource) {
+ databaseResource.schema = right;
+ }
+ } else if (left === "request.statement") {
+ const statement = atob(right);
+ conditionExpression.statement = statement;
+ } else if (left === "request.export_format") {
+ conditionExpression.exportFormat = right;
+ }
+ } else if (typeof right === "number") {
+ if (left === "request.row_limit") {
+ conditionExpression.rowLimit = right;
}
}
}