Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(audit-log): Include userId in in-app agent api-key audit logs (#14920)

Ben Bachem3f631e42440e
diff --git a/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts b/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts
new file mode 100644
index 0000000000..647ea1d066
--- /dev/null
+++ b/web/src/__tests__/server/audit-log-in-app-agent.servertest.ts
@@ -0,0 +1,89 @@
+import { randomUUID } from "crypto";
+import { auditLog } from "@/src/features/audit-logs/auditLog";
+import { prisma, AuditLogRecordType } from "../../../../packages/shared/src/db";
+import { createAndAddApiKeysToDb } from "../../../../packages/shared/src/server/auth/apiKeys";
+import { createOrgProjectAndApiKey } from "../../../../packages/shared/src/server/test-utils/org-factory";
+
+describe("in-app agent audit logging", () => {
+ it("stores the creator on in-app-agent MCP keys", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const user = await prisma.user.create({
+ data: {
+ email: `${randomUUID()}@test.com`,
+ name: "In-app Agent User",
+ },
+ });
+
+ const mcpApiKey = await createAndAddApiKeysToDb({
+ prisma,
+ entityId: projectId,
+ scope: "PROJECT",
+ note: "In-app agent MCP session",
+ isInAppAgentKey: true,
+ createdByUserId: user.id,
+ });
+
+ const persistedApiKey = await prisma.apiKey.findUniqueOrThrow({
+ where: { id: mcpApiKey.id },
+ select: {
+ createdByUserId: true,
+ isInAppAgentKey: true,
+ },
+ });
+
+ expect(persistedApiKey).toEqual({
+ createdByUserId: user.id,
+ isInAppAgentKey: true,
+ });
+ });
+
+ it("stores the creator user id on audit logs written by in-app agent API keys", async () => {
+ const { orgId, projectId } = await createOrgProjectAndApiKey();
+ const user = await prisma.user.create({
+ data: {
+ email: `${randomUUID()}@test.com`,
+ name: "Audit User",
+ },
+ });
+
+ const mcpApiKey = await createAndAddApiKeysToDb({
+ prisma,
+ entityId: projectId,
+ scope: "PROJECT",
+ note: "In-app agent MCP session",
+ isInAppAgentKey: true,
+ createdByUserId: user.id,
+ });
+
+ const resourceId = randomUUID();
+ await auditLog({
+ action: "create",
+ resourceType: "job",
+ resourceId,
+ orgId,
+ projectId,
+ apiKeyId: mcpApiKey.id,
+ });
+
+ const persistedAuditLog = await prisma.auditLog.findFirstOrThrow({
+ where: {
+ apiKeyId: mcpApiKey.id,
+ resourceId,
+ },
+ select: {
+ type: true,
+ apiKeyId: true,
+ userId: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ expect(persistedAuditLog).toEqual({
+ type: AuditLogRecordType.API_KEY,
+ apiKeyId: mcpApiKey.id,
+ userId: user.id,
+ });
+ });
+});
diff --git a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
index 5b070fcb72..62e714e253 100644
--- a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
+++ b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
@@ -379,7 +379,7 @@ describe("MCP public API tools", () => {
).resolves.toBe(assignmentAuditLogCount + 1);
const auditLogCreateSpy = vi
- .spyOn(prisma.auditLog, "create")
+ .spyOn(prisma, "$transaction")
.mockRejectedValueOnce(new Error("audit failed"));
try {
diff --git a/web/src/features/audit-logs/auditLog.ts b/web/src/features/audit-logs/auditLog.ts
index 26e24e3d70..64a13aa895 100644
--- a/web/src/features/audit-logs/auditLog.ts
+++ b/web/src/features/audit-logs/auditLog.ts
@@ -83,40 +83,72 @@ type AuditLog = {
);
export async function auditLog(log: AuditLog, prisma?: typeof _prisma) {
- const meta =
- "session" in log
- ? {
- userId: log.session.user.id,
- orgId: log.session.orgId,
- userOrgRole: log.session.orgRole,
- projectId: log.session.projectId,
- userProjectRole: log.session.projectRole,
- type: AuditLogRecordType.USER,
- }
- : "userId" in log
- ? {
- userId: log.userId,
- orgId: log.orgId,
- userOrgRole: log.orgRole,
- projectId: log.projectId,
- userProjectRole: log.projectRole,
- type: AuditLogRecordType.USER,
- }
- : {
- apiKeyId: log.apiKeyId,
- orgId: log.orgId,
- projectId: log.projectId,
- type: AuditLogRecordType.API_KEY,
- };
+ const db = prisma ?? _prisma;
+ const shared = {
+ resourceType: log.resourceType,
+ resourceId: log.resourceId,
+ action: log.action,
+ before: log.before ? JSON.stringify(log.before) : undefined,
+ after: log.after ? JSON.stringify(log.after) : undefined,
+ };
- await (prisma ?? _prisma).auditLog.create({
- data: {
- ...meta,
- resourceType: log.resourceType,
- resourceId: log.resourceId,
- action: log.action,
- before: log.before ? JSON.stringify(log.before) : undefined,
- after: log.after ? JSON.stringify(log.after) : undefined,
- },
- });
+ if ("apiKeyId" in log) {
+ await db.$transaction(async (tx) => {
+ const apiKey = await tx.apiKey.findUnique({
+ where: { id: log.apiKeyId },
+ select: {
+ isInAppAgentKey: true,
+ createdByUserId: true,
+ },
+ });
+
+ await tx.auditLog.create({
+ data: {
+ apiKeyId: log.apiKeyId,
+ userId:
+ apiKey?.isInAppAgentKey === true
+ ? (apiKey.createdByUserId ?? undefined)
+ : undefined,
+ orgId: log.orgId,
+ projectId: log.projectId,
+ type: AuditLogRecordType.API_KEY,
+ ...shared,
+ },
+ });
+ });
+
+ return;
+ }
+
+ if ("session" in log) {
+ await db.auditLog.create({
+ data: {
+ userId: log.session.user.id,
+ orgId: log.session.orgId,
+ userOrgRole: log.session.orgRole,
+ projectId: log.session.projectId,
+ userProjectRole: log.session.projectRole,
+ type: AuditLogRecordType.USER,
+ ...shared,
+ },
+ });
+
+ return;
+ }
+
+ if ("userId" in log) {
+ await db.auditLog.create({
+ data: {
+ userId: log.userId,
+ orgId: log.orgId,
+ userOrgRole: log.orgRole,
+ projectId: log.projectId,
+ userProjectRole: log.projectRole,
+ type: AuditLogRecordType.USER,
+ ...shared,
+ },
+ });
+
+ return;
+ }
}