-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: audit reports #7099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: audit reports #7099
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
81323a7
feat: secret reporting
varonix0 2f9ab9a
Merge branch 'main' into daniel/secret-reporting
varonix0 092244b
Update 20260629120000_create-audit-reports.ts
varonix0 2bac7c7
fix: ui nits
varonix0 81a7650
fix: outdated migration schemas
varonix0 b6605bc
Update pam-sessions.ts
varonix0 83762e8
fix: structural changes
varonix0 406715e
requested changes
varonix0 32a759d
docs
varonix0 47602af
requested changes
varonix0 9d104c2
requested changes
varonix0 9fe6535
lint
varonix0 1621687
requested changes
varonix0 2eac54c
requested changes
varonix0 583b159
lint
varonix0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
backend/src/db/migrations/20260629120000_create-audit-reports.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Knex } from "knex"; | ||
|
|
||
| import { TableName } from "@app/db/schemas"; | ||
| import { createOnUpdateTrigger, dropOnUpdateTrigger } from "@app/db/utils"; | ||
| import { AuditReportStatus } from "@app/ee/services/audit-report/audit-report-types"; | ||
|
|
||
| export async function up(knex: Knex): Promise<void> { | ||
| if (!(await knex.schema.hasTable(TableName.AuditReport))) { | ||
| await knex.schema.createTable(TableName.AuditReport, (t) => { | ||
| t.uuid("id", { primaryKey: true }).defaultTo(knex.fn.uuid()); | ||
| t.string("projectId").notNullable(); | ||
| t.foreign("projectId").references("id").inTable(TableName.Project).onDelete("CASCADE"); | ||
|
|
||
| t.uuid("requestedByUserId"); | ||
| t.foreign("requestedByUserId").references("id").inTable(TableName.Users).onDelete("SET NULL"); | ||
|
|
||
| t.string("status").notNullable().defaultTo(AuditReportStatus.Pending); | ||
| t.jsonb("reportConfigs").notNullable(); | ||
| t.specificType("emailRecipients", "text[]").notNullable(); | ||
| t.jsonb("resultSummary"); | ||
| t.text("errorMessage"); | ||
| t.timestamps(true, true, true); | ||
|
|
||
| t.index(["projectId"]); | ||
| t.index(["requestedByUserId"]); | ||
| }); | ||
|
|
||
| await createOnUpdateTrigger(knex, TableName.AuditReport); | ||
| } | ||
| } | ||
|
|
||
| export async function down(knex: Knex): Promise<void> { | ||
| await dropOnUpdateTrigger(knex, TableName.AuditReport); | ||
| await knex.schema.dropTableIfExists(TableName.AuditReport); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Code generated by automation script, DO NOT EDIT. | ||
| // Automated by pulling database and generating zod schema | ||
| // To update. Just run npm run generate:schema | ||
| // Written by akhilmhdh. | ||
|
|
||
| import { z } from "zod"; | ||
|
|
||
| import { TImmutableDBKeys } from "./models"; | ||
|
|
||
| export const AuditReportsSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| projectId: z.string(), | ||
| requestedByUserId: z.string().uuid().nullable().optional(), | ||
| status: z.string().default("pending"), | ||
| reportConfigs: z.unknown(), | ||
| emailRecipients: z.string().array(), | ||
| resultSummary: z.unknown().nullable().optional(), | ||
| errorMessage: z.string().nullable().optional(), | ||
| createdAt: z.date(), | ||
| updatedAt: z.date() | ||
| }); | ||
|
|
||
| export type TAuditReports = z.infer<typeof AuditReportsSchema>; | ||
| export type TAuditReportsInsert = Omit<z.input<typeof AuditReportsSchema>, TImmutableDBKeys>; | ||
| export type TAuditReportsUpdate = Partial<Omit<z.input<typeof AuditReportsSchema>, TImmutableDBKeys>>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Code generated by automation script, DO NOT EDIT. | ||
| // Automated by pulling database and generating zod schema | ||
| // To update. Just run npm run generate:schema | ||
| // Written by akhilmhdh. | ||
|
|
||
| import { z } from "zod"; | ||
|
|
||
| import { TImmutableDBKeys } from "./models"; | ||
|
|
||
| export const ProjectFolderGrantsSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| sourceProjectId: z.string(), | ||
| sourceFolderId: z.string().uuid(), | ||
| targetProjectId: z.string(), | ||
| createdAt: z.date(), | ||
| updatedAt: z.date() | ||
| }); | ||
|
|
||
| export type TProjectFolderGrants = z.infer<typeof ProjectFolderGrantsSchema>; | ||
| export type TProjectFolderGrantsInsert = Omit<z.input<typeof ProjectFolderGrantsSchema>, TImmutableDBKeys>; | ||
| export type TProjectFolderGrantsUpdate = Partial<Omit<z.input<typeof ProjectFolderGrantsSchema>, TImmutableDBKeys>>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { EventType } from "@app/ee/services/audit-log/audit-log-types"; | ||
| import { | ||
| AuditReportResultEntrySchema, | ||
| AuditReportStatus, | ||
| AuditReportType | ||
| } from "@app/ee/services/audit-report/audit-report-types"; | ||
| import { readLimit, writeLimit } from "@app/server/config/rateLimiter"; | ||
| import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; | ||
| import { AuthMode } from "@app/services/auth/auth-type"; | ||
|
|
||
| const AuditReportConfigSchema = z.object({ | ||
| type: z.nativeEnum(AuditReportType), | ||
| inputs: z.record(z.unknown()) | ||
| }); | ||
|
|
||
| const AuditReportSchema = z.object({ | ||
| id: z.string().uuid(), | ||
| projectId: z.string(), | ||
| requestedByUserId: z.string().uuid().nullable(), | ||
| status: z.nativeEnum(AuditReportStatus), | ||
| reportConfigs: z.array(AuditReportConfigSchema), | ||
| emailRecipients: z.string().array(), | ||
| resultSummary: z.array(AuditReportResultEntrySchema).nullable(), | ||
| errorMessage: z.string().nullable(), | ||
| createdAt: z.date(), | ||
| updatedAt: z.date() | ||
| }); | ||
|
|
||
| export const registerAuditReportRouter = async (server: FastifyZodProvider) => { | ||
| server.route({ | ||
| method: "POST", | ||
| url: "/", | ||
| config: { rateLimit: writeLimit }, | ||
| schema: { | ||
| operationId: "requestAuditReport", | ||
| description: "Request generation of one or more audit reports, delivered by email as CSV attachments", | ||
| security: [{ bearerAuth: [] }], | ||
| body: z.object({ | ||
| projectId: z.string().trim(), | ||
| reports: z | ||
| .array( | ||
| z.object({ | ||
| type: z.nativeEnum(AuditReportType), | ||
| inputs: z.record(z.unknown()).optional() | ||
| }) | ||
| ) | ||
| .min(1) | ||
| .describe("The report types to generate, each with optional type-specific inputs."), | ||
| emailRecipients: z | ||
| .array(z.string().email()) | ||
| .optional() | ||
| .describe("Email addresses to deliver the reports to. Defaults to the requesting user's email.") | ||
| }), | ||
| response: { | ||
| 200: AuditReportSchema | ||
| } | ||
| }, | ||
| onRequest: verifyAuth([AuthMode.JWT]), | ||
|
varonix0 marked this conversation as resolved.
Outdated
|
||
| handler: async (req) => { | ||
| const report = await server.services.auditReport.requestReport(req.body, req.permission); | ||
| await server.services.auditLog.createAuditLog({ | ||
| projectId: report.projectId, | ||
| event: { | ||
| type: EventType.CREATE_AUDIT_REPORT, | ||
| metadata: { | ||
| auditReportId: report.id, | ||
| projectId: report.projectId, | ||
| reportTypes: report.reportConfigs.map((config) => config.type), | ||
| recipientCount: report.emailRecipients.length | ||
| } | ||
| }, | ||
| ...req.auditLogInfo | ||
| }); | ||
| return report; | ||
| } | ||
| }); | ||
|
|
||
| server.route({ | ||
| method: "GET", | ||
| url: "/", | ||
| config: { rateLimit: readLimit }, | ||
| schema: { | ||
| operationId: "listAuditReports", | ||
| description: "List audit reports for a project", | ||
| security: [{ bearerAuth: [] }], | ||
| querystring: z.object({ | ||
| projectId: z.string().trim(), | ||
| offset: z.coerce.number().min(0).default(0), | ||
| limit: z.coerce.number().min(1).max(100).default(10) | ||
| }), | ||
| response: { | ||
| 200: z.object({ reports: z.array(AuditReportSchema), totalCount: z.number() }) | ||
| } | ||
| }, | ||
| onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]), | ||
| handler: async (req) => { | ||
| const { projectId, offset, limit } = req.query; | ||
| const { reports, totalCount } = await server.services.auditReport.listReports( | ||
| { projectId, offset, limit }, | ||
| req.permission | ||
| ); | ||
| await server.services.auditLog.createAuditLog({ | ||
| projectId, | ||
| event: { type: EventType.GET_AUDIT_REPORTS, metadata: { projectId } }, | ||
| ...req.auditLogInfo | ||
| }); | ||
| return { reports, totalCount }; | ||
| } | ||
| }); | ||
|
|
||
| server.route({ | ||
| method: "GET", | ||
| url: "/:auditReportId", | ||
| config: { rateLimit: readLimit }, | ||
| schema: { | ||
| operationId: "getAuditReport", | ||
| description: "Get a single audit report by ID", | ||
| security: [{ bearerAuth: [] }], | ||
| params: z.object({ auditReportId: z.string().uuid() }), | ||
| querystring: z.object({ projectId: z.string().trim() }), | ||
|
varonix0 marked this conversation as resolved.
Outdated
|
||
| response: { | ||
| 200: AuditReportSchema | ||
| } | ||
| }, | ||
| onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]), | ||
| handler: async (req) => { | ||
| const report = await server.services.auditReport.getReportById( | ||
| { projectId: req.query.projectId, auditReportId: req.params.auditReportId }, | ||
| req.permission | ||
| ); | ||
| await server.services.auditLog.createAuditLog({ | ||
| projectId: report.projectId, | ||
| event: { | ||
| type: EventType.GET_AUDIT_REPORT, | ||
| metadata: { auditReportId: report.id, projectId: report.projectId } | ||
| }, | ||
| ...req.auditLogInfo | ||
| }); | ||
| return report; | ||
| } | ||
| }); | ||
|
|
||
| server.route({ | ||
| method: "DELETE", | ||
| url: "/:auditReportId", | ||
| config: { rateLimit: writeLimit }, | ||
| schema: { | ||
| operationId: "deleteAuditReport", | ||
| description: "Delete an audit report", | ||
| security: [{ bearerAuth: [] }], | ||
| params: z.object({ auditReportId: z.string().uuid() }), | ||
| querystring: z.object({ projectId: z.string().trim() }), | ||
| response: { | ||
| 200: AuditReportSchema | ||
| } | ||
| }, | ||
| onRequest: verifyAuth([AuthMode.JWT]), | ||
|
varonix0 marked this conversation as resolved.
Outdated
|
||
| handler: async (req) => { | ||
| const report = await server.services.auditReport.deleteReport( | ||
| { projectId: req.query.projectId, auditReportId: req.params.auditReportId }, | ||
| req.permission | ||
| ); | ||
| await server.services.auditLog.createAuditLog({ | ||
| projectId: report.projectId, | ||
| event: { | ||
| type: EventType.DELETE_AUDIT_REPORT, | ||
| metadata: { auditReportId: report.id, projectId: report.projectId } | ||
| }, | ||
| ...req.auditLogInfo | ||
| }); | ||
| return report; | ||
| } | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.