Skip to content

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

chore(frontend): implement export audit logs (#11878)

boojackfcb3403b1f66
diff --git a/backend/api/v1/acl_iam.go b/backend/api/v1/acl_iam.go
index f788a9bf5e..30005d1afd 100644
--- a/backend/api/v1/acl_iam.go
+++ b/backend/api/v1/acl_iam.go
@@ -147,6 +147,7 @@ func isSkippedMethod(fullMethod string) bool {
// handled in the method because checking is complex.
case
v1pb.AuditLogService_SearchAuditLogs_FullMethodName,
+ v1pb.AuditLogService_ExportAuditLogs_FullMethodName,
v1pb.InstanceService_SearchInstances_FullMethodName,
v1pb.DatabaseService_ListSlowQueries_FullMethodName,
v1pb.DatabaseService_ListDatabases_FullMethodName,
diff --git a/backend/api/v1/audit_log_service.go b/backend/api/v1/audit_log_service.go
index c5fdd9adb2..8afffa97dc 100644
--- a/backend/api/v1/audit_log_service.go
+++ b/backend/api/v1/audit_log_service.go
@@ -103,7 +103,7 @@ func (s *AuditLogService) ExportAuditLogs(ctx context.Context, request *v1pb.Exp
ColumnNames: []string{"time", "user", "method", "severity", "resource", "request", "response", "status"},
}
for _, auditLog := range searchAuditLogsResult.AuditLogs {
- result.Rows = append(result.Rows, &v1pb.QueryRow{Values: []*v1pb.RowValue{
+ queryRow := &v1pb.QueryRow{Values: []*v1pb.RowValue{
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.CreateTime.AsTime().Format(time.RFC3339)}},
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.User}},
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Method}},
@@ -111,8 +111,13 @@ func (s *AuditLogService) ExportAuditLogs(ctx context.Context, request *v1pb.Exp
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Resource}},
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Request}},
{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Response}},
- {Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Status.String()}},
- }})
+ }}
+ if auditLog.Status != nil {
+ queryRow.Values = append(queryRow.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: auditLog.Status.String()}})
+ } else {
+ queryRow.Values = append(queryRow.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_NullValue{}})
+ }
+ result.Rows = append(result.Rows, queryRow)
}
var content []byte
diff --git a/frontend/src/store/modules/v1/auditLog.ts b/frontend/src/store/modules/v1/auditLog.ts
index 74023a1d26..8c159b822a 100644
--- a/frontend/src/store/modules/v1/auditLog.ts
+++ b/frontend/src/store/modules/v1/auditLog.ts
@@ -5,6 +5,7 @@ import { reactive } from "vue";
import { auditLogServiceClient } from "@/grpcweb";
import type { SearchAuditLogsParams } from "@/types";
import type { AuditLog } from "@/types/proto/v1/audit_log_service";
+import type { ExportFormat } from "@/types/proto/v1/common";
import { userNamePrefix } from "./common";
dayjs.extend(utc);
@@ -38,7 +39,20 @@ export const useAuditLogStore = defineStore("audit_log", () => {
return resp;
};
+ const exportAuditLogs = async (
+ search: SearchAuditLogsParams,
+ format: ExportFormat
+ ) => {
+ const { content } = await auditLogServiceClient.exportAuditLogs({
+ filter: buildFilter(search),
+ orderBy: search.order ? `create_time ${search.order}` : undefined,
+ format,
+ });
+ return content;
+ };
+
return {
fetchAuditLogs,
+ exportAuditLogs,
};
});
diff --git a/frontend/src/views/SettingWorkspaceAuditLog.vue b/frontend/src/views/SettingWorkspaceAuditLog.vue
index edde560018..2deccb4eb1 100644
--- a/frontend/src/views/SettingWorkspaceAuditLog.vue
+++ b/frontend/src/views/SettingWorkspaceAuditLog.vue
@@ -1,12 +1,27 @@
<template>
<div class="w-full space-y-4">
<FeatureAttention feature="bb.feature.audit-log" />
- <div class="flex justify-end items-center space-x-2">
- <div class="w-72">
- <UserSelect
- v-model:user="state.userUid"
- :multiple="false"
- :include-all="true"
+ <div class="flex justify-between items-center space-x-2">
+ <div>
+ <div class="w-72">
+ <UserSelect
+ v-model:user="state.userUid"
+ :multiple="false"
+ :include-all="true"
+ />
+ </div>
+ </div>
+ <div>
+ <DataExportButton
+ size="medium"
+ :file-type="'raw'"
+ :support-formats="[
+ ExportFormat.CSV,
+ ExportFormat.JSON,
+ ExportFormat.XLSX,
+ ]"
+ :disabled="!hasAuditLogFeature"
+ @export="handleExport"
/>
</div>
</div>
@@ -27,18 +42,23 @@
</template>
<script lang="ts" setup>
+import type { BinaryLike } from "node:crypto";
import { reactive, computed } from "vue";
import AuditLogDataTable from "@/components/AuditLog/AuditLogDataTable.vue";
+import type { ExportOption } from "@/components/DataExportButton.vue";
+import DataExportButton from "@/components/DataExportButton.vue";
import PagedAuditLogTable from "@/components/PagedAuditLogTable.vue";
-import { featureToRef, useUserStore } from "@/store";
+import { featureToRef, useAuditLogStore, useUserStore } from "@/store";
import type { SearchAuditLogsParams } from "@/types";
import { UNKNOWN_ID } from "@/types";
+import { ExportFormat } from "@/types/proto/v1/common";
const state = reactive({
userUid: String(UNKNOWN_ID),
});
const userStore = useUserStore();
+const auditLogStore = useAuditLogStore();
const hasAuditLogFeature = featureToRef("bb.feature.audit-log");
@@ -53,4 +73,15 @@ const searchAuditLogs = computed((): SearchAuditLogsParams => {
order: "desc",
};
});
+
+const handleExport = async (
+ options: ExportOption,
+ callback: (content: BinaryLike | Blob, options: ExportOption) => void
+) => {
+ const content = await auditLogStore.exportAuditLogs(
+ searchAuditLogs.value,
+ options.format
+ );
+ callback(content, options);
+};
</script>
diff --git a/frontend/src/views/project/ProjectAuditLogDashboard.vue b/frontend/src/views/project/ProjectAuditLogDashboard.vue
index bfe593a7a3..ed58758e06 100644
--- a/frontend/src/views/project/ProjectAuditLogDashboard.vue
+++ b/frontend/src/views/project/ProjectAuditLogDashboard.vue
@@ -1,34 +1,76 @@
<template>
- <PagedAuditLogTable
- v-if="hasAuditLogFeature"
- :search-audit-logs="searchAuditLogs"
- session-key="bb.page-audit-log-table.settings-audit-log-v1-table"
- :page-size="10"
- >
- <template #table="{ list }">
- <AuditLogDataTable :audit-log-list="list" :show-project="false" />
+ <div class="w-full space-y-4">
+ <FeatureAttention feature="bb.feature.audit-log" />
+ <div class="flex justify-between items-center space-x-2">
+ <div>
+ <div class="w-72">
+ <UserSelect
+ v-model:user="state.userUid"
+ :multiple="false"
+ :include-all="true"
+ />
+ </div>
+ </div>
+ <div>
+ <DataExportButton
+ size="medium"
+ :file-type="'raw'"
+ :support-formats="[
+ ExportFormat.CSV,
+ ExportFormat.JSON,
+ ExportFormat.XLSX,
+ ]"
+ :disabled="!hasAuditLogFeature"
+ @export="handleExport"
+ />
+ </div>
+ </div>
+
+ <PagedAuditLogTable
+ v-if="hasAuditLogFeature"
+ :search-audit-logs="searchAuditLogs"
+ session-key="bb.page-audit-log-table.settings-audit-log-v1-table"
+ :page-size="10"
+ >
+ <template #table="{ list }">
+ <AuditLogDataTable :audit-log-list="list" :show-project="false" />
+ </template>
+ </PagedAuditLogTable>
+ <template v-else>
+ <AuditLogDataTable :audit-log-list="[]" :show-project="false" />
</template>
- </PagedAuditLogTable>
- <template v-else>
- <AuditLogDataTable :audit-log-list="[]" :show-project="false" />
- </template>
+ </div>
</template>
<script setup lang="ts">
-import { computed } from "vue";
+import type { BinaryLike } from "node:crypto";
+import { computed, reactive } from "vue";
import AuditLogDataTable from "@/components/AuditLog/AuditLogDataTable.vue";
+import type { ExportOption } from "@/components/DataExportButton.vue";
import PagedAuditLogTable from "@/components/PagedAuditLogTable.vue";
-import { featureToRef, useProjectV1Store } from "@/store";
+import {
+ featureToRef,
+ useAuditLogStore,
+ useProjectV1Store,
+ useUserStore,
+} from "@/store";
import { projectNamePrefix } from "@/store/modules/v1/common";
-import type { SearchAuditLogsParams } from "@/types";
+import { UNKNOWN_ID, type SearchAuditLogsParams } from "@/types";
+import { ExportFormat } from "@/types/proto/v1/common";
const props = defineProps<{
projectId: string;
}>();
+const state = reactive({
+ userUid: String(UNKNOWN_ID),
+});
+
const hasAuditLogFeature = featureToRef("bb.feature.audit-log");
+const userStore = useUserStore();
const projectV1Store = useProjectV1Store();
+const auditLogStore = useAuditLogStore();
const project = computed(() => {
return projectV1Store.getProjectByName(
@@ -36,10 +78,27 @@ const project = computed(() => {
);
});
+const selectedUserEmail = computed((): string => {
+ const selected = userStore.getUserById(state.userUid);
+ return selected?.email ?? "";
+});
+
const searchAuditLogs = computed((): SearchAuditLogsParams => {
return {
parent: project.value.name,
+ creatorEmail: selectedUserEmail.value,
order: "desc",
};
});
+
+const handleExport = async (
+ options: ExportOption,
+ callback: (content: BinaryLike | Blob, options: ExportOption) => void
+) => {
+ const content = await auditLogStore.exportAuditLogs(
+ searchAuditLogs.value,
+ options.format
+ );
+ callback(content, options);
+};
</script>