feat(audit): capture IP address, make TRUST_PROXY configurable

This commit is contained in:
SnapOtter
2026-06-13 16:25:58 +08:00
parent f6d5479334
commit 5d2f520d78
9 changed files with 38 additions and 32 deletions
+2 -2
View File
@@ -86,7 +86,7 @@ export async function apiKeyRoutes(app: FastifyInstance): Promise<void> {
return reply.status(409).send({ error: "Failed to create API key" });
}
await auditLog(request.log, "API_KEY_CREATED", { userId: user.id, keyId: id, keyName: name });
await auditLog(request.log, "API_KEY_CREATED", { userId: user.id, keyId: id, keyName: name }, request.ip);
// Return the raw key ONCE — it cannot be retrieved again
return reply.status(201).send({
@@ -155,7 +155,7 @@ export async function apiKeyRoutes(app: FastifyInstance): Promise<void> {
await db.delete(schema.apiKeys).where(eq(schema.apiKeys.id, id));
await auditLog(request.log, "API_KEY_DELETED", { userId: user.id, keyId: id });
await auditLog(request.log, "API_KEY_DELETED", { userId: user.id, keyId: id }, request.ip);
return reply.send({ ok: true });
},
+3 -3
View File
@@ -116,7 +116,7 @@ export async function rolesRoutes(app: FastifyInstance): Promise<void> {
createdBy: user.id,
});
await auditLog(request.log, "ROLE_CREATED", { adminId: user.id, roleId: id, roleName: name });
await auditLog(request.log, "ROLE_CREATED", { adminId: user.id, roleId: id, roleName: name }, request.ip);
return reply.status(201).send({
id,
@@ -185,7 +185,7 @@ export async function rolesRoutes(app: FastifyInstance): Promise<void> {
}
await tx.update(schema.roles).set(updates).where(eq(schema.roles.id, id));
});
await auditLog(request.log, "ROLE_UPDATED", { adminId: user.id, roleId: id });
await auditLog(request.log, "ROLE_UPDATED", { adminId: user.id, roleId: id }, request.ip);
return reply.send({ ok: true });
},
@@ -220,7 +220,7 @@ export async function rolesRoutes(app: FastifyInstance): Promise<void> {
adminId: user.id,
roleId: id,
roleName: role.name,
});
}, request.ip);
return reply.send({ ok: true });
},
+1 -1
View File
@@ -95,7 +95,7 @@ export async function settingsRoutes(app: FastifyInstance): Promise<void> {
adminId: admin.id,
username: admin.username,
keys: entries.map((e) => e.key),
});
}, request.ip);
}
return reply.send({ ok: true, updatedCount: entries.length });
+3 -3
View File
@@ -263,7 +263,7 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
userId,
count: created.length,
files: created.map((f) => f.originalName),
});
}, request.ip);
return reply.status(201).send({ files: created });
},
@@ -495,7 +495,7 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
.map((f) => f.id);
if (validIds.length === 0) {
await auditLog(request.log, "FILE_DELETED", { userId: user.id, count: 0, ids });
await auditLog(request.log, "FILE_DELETED", { userId: user.id, count: 0, ids }, request.ip);
return reply.send({ deleted: 0 });
}
@@ -546,7 +546,7 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
userId: user.id,
count: chainRows.length,
ids,
});
}, request.ip);
return reply.send({ deleted: chainRows.length });
});