api: Audit-Log verdrahtet (auditedProcedure an kritische Mutationen)
withAudit war als tRPC-Wrapper definiert, aber an keine Procedure gehaengt (audit_event blieb leer). Jetzt eine echte tRPC-v11-Middleware-Procedure (auditedProcedure), die nach erfolgreicher Mutation best-effort einen audit_event schreibt (Fehler beim Audit kippen die Mutation nicht). Verdrahtet an die folgenreichen Zustandsuebergaenge: - blocker.resolve - project.archive, project.setPhase, project.addPhaseGate Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
6e7f639013
commit
c98d27e70c
3 files changed
+30
-25
No files matched your search
@@ -2,7 +2,7 @@ import { z } from 'zod';
|
|||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
import { eq, isNull, desc } from 'drizzle-orm';
|
import { eq, isNull, desc } from 'drizzle-orm';
|
||||||
import { blocker, project, user } from '@cockpit/db';
|
import { blocker, project, user } from '@cockpit/db';
|
||||||
import { router, protectedProcedure } from '../trpc.js';
|
import { router, protectedProcedure, auditedProcedure } from '../trpc.js';
|
||||||
|
|
||||||
export const blockerRouter = router({
|
export const blockerRouter = router({
|
||||||
listOpen: protectedProcedure.query(async ({ ctx }) => {
|
listOpen: protectedProcedure.query(async ({ ctx }) => {
|
||||||
@@ -52,7 +52,7 @@ export const blockerRouter = router({
|
|||||||
return created;
|
return created;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
resolve: protectedProcedure
|
resolve: auditedProcedure('blocker.resolve')
|
||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
blockerId: z.string().uuid(),
|
blockerId: z.string().uuid(),
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
type Project,
|
type Project,
|
||||||
} from '@cockpit/db';
|
} from '@cockpit/db';
|
||||||
import { ProjectSchemas } from '@cockpit/shared';
|
import { ProjectSchemas } from '@cockpit/shared';
|
||||||
import { router, protectedProcedure } from '../trpc.js';
|
import { router, protectedProcedure, auditedProcedure } from '../trpc.js';
|
||||||
|
|
||||||
const PHASE_ORDER = ['definition', 'planung', 'umsetzung', 'abschluss'] as const;
|
const PHASE_ORDER = ['definition', 'planung', 'umsetzung', 'abschluss'] as const;
|
||||||
function nextPhase(p: (typeof PHASE_ORDER)[number]): (typeof PHASE_ORDER)[number] {
|
function nextPhase(p: (typeof PHASE_ORDER)[number]): (typeof PHASE_ORDER)[number] {
|
||||||
@@ -116,7 +116,7 @@ export const projectRouter = router({
|
|||||||
return result;
|
return result;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
archive: protectedProcedure
|
archive: auditedProcedure('project.archive')
|
||||||
.input(ProjectSchemas.archiveProjectInput)
|
.input(ProjectSchemas.archiveProjectInput)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const p = (
|
const p = (
|
||||||
@@ -145,7 +145,7 @@ export const projectRouter = router({
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
// Phase direkt setzen (Leitfaden Kap. 2).
|
// Phase direkt setzen (Leitfaden Kap. 2).
|
||||||
setPhase: protectedProcedure
|
setPhase: auditedProcedure('project.setPhase')
|
||||||
.input(ProjectSchemas.setPhaseInput)
|
.input(ProjectSchemas.setPhaseInput)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const [updated] = await ctx.db
|
const [updated] = await ctx.db
|
||||||
@@ -158,7 +158,7 @@ export const projectRouter = router({
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
// Gate-Review am Phasenende (Leitfaden Kap. 5), optional Folgephase freigeben.
|
// Gate-Review am Phasenende (Leitfaden Kap. 5), optional Folgephase freigeben.
|
||||||
addPhaseGate: protectedProcedure
|
addPhaseGate: auditedProcedure('project.addPhaseGate')
|
||||||
.input(ProjectSchemas.addPhaseGateInput)
|
.input(ProjectSchemas.addPhaseGateInput)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const [gate] = await ctx.db
|
const [gate] = await ctx.db
|
||||||
|
|||||||
+24
-19
@@ -3,8 +3,9 @@
|
|||||||
import './types.js';
|
import './types.js';
|
||||||
import { initTRPC, TRPCError } from '@trpc/server';
|
import { initTRPC, TRPCError } from '@trpc/server';
|
||||||
import type { FastifyRequest, FastifyReply } from 'fastify';
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
||||||
import type { User } from '@cockpit/db';
|
import { auditEvent, type User } from '@cockpit/db';
|
||||||
import { getDb } from './db.js';
|
import { getDb } from './db.js';
|
||||||
|
import { logger } from './lib/logger.js';
|
||||||
|
|
||||||
export interface Context {
|
export interface Context {
|
||||||
user: User | null;
|
user: User | null;
|
||||||
@@ -45,25 +46,29 @@ export const protectedProcedure = t.procedure.use(async ({ ctx, next }) => {
|
|||||||
return next({ ctx: { ...ctx, user: ctx.user } });
|
return next({ ctx: { ...ctx, user: ctx.user } });
|
||||||
});
|
});
|
||||||
|
|
||||||
// Audit-Wrapper: schreibt nach erfolgreicher Mutation einen audit_event.
|
/**
|
||||||
// Genutzt als zusätzliche Middleware auf Mutations.
|
* Auditierte Procedure: wie `protectedProcedure`, schreibt aber nach erfolgreicher
|
||||||
export function withAudit<TInput, TResult extends Record<string, unknown> | unknown>(
|
* Ausführung einen `audit_event`. Nutzung:
|
||||||
action: string,
|
* auditedProcedure('blocker.resolve').input(schema).mutation(...)
|
||||||
) {
|
* Das Audit ist best-effort — ein Fehler beim Schreiben darf die eigentliche
|
||||||
return async function audit(opts: {
|
* Mutation nicht scheitern lassen.
|
||||||
ctx: Context;
|
*/
|
||||||
input: TInput;
|
export function auditedProcedure(action: string) {
|
||||||
next: () => Promise<{ ok: true; data: TResult } | TResult>;
|
return protectedProcedure.use(async ({ ctx, next, getRawInput }) => {
|
||||||
}) {
|
const result = await next();
|
||||||
const result = await opts.next();
|
if (result.ok) {
|
||||||
if (!opts.ctx.user) return result;
|
try {
|
||||||
const { auditEvent } = await import('@cockpit/db');
|
const rawInput = await getRawInput();
|
||||||
await opts.ctx.db.insert(auditEvent).values({
|
await ctx.db.insert(auditEvent).values({
|
||||||
actorUserId: opts.ctx.user.id,
|
actorUserId: ctx.user.id,
|
||||||
action,
|
action,
|
||||||
entityKind: action.split('.')[0] ?? 'unknown',
|
entityKind: action.split('.')[0] ?? 'unknown',
|
||||||
payload: { input: opts.input as unknown },
|
payload: { input: (rawInput ?? null) as unknown },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn({ err, action }, 'audit_event konnte nicht geschrieben werden');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
});
|
});
|
||||||
return result;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user