Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Add try catch to audit logs Add audit log try catch

Maidul Islam2cad28dd8b50
diff --git a/backend/src/ee/services/audit-log/audit-log-queue.ts b/backend/src/ee/services/audit-log/audit-log-queue.ts
index a55ab1d617..73e2b931e9 100644
--- a/backend/src/ee/services/audit-log/audit-log-queue.ts
+++ b/backend/src/ee/services/audit-log/audit-log-queue.ts
@@ -79,69 +79,77 @@ export const auditLogQueueServiceFactory = async ({
};
queueService.start(QueueName.AuditLog, async (job) => {
- const { actor, event, ipAddress, projectId, userAgent, userAgentType } = job.data;
- let { orgId } = job.data;
- let project: TProjects | undefined;
-
- if (!orgId) {
- // it will never be undefined for both org and project id
- // TODO(akhilmhdh): use caching here in dal to avoid db calls
- project = await projectDAL.findById(projectId as string);
- orgId = project.orgId;
- }
+ try {
+ const { actor, event, ipAddress, projectId, userAgent, userAgentType } = job.data;
+ let { orgId } = job.data;
+ let project: TProjects | undefined;
+
+ if (!orgId) {
+ // it will never be undefined for both org and project id
+ // TODO(akhilmhdh): use caching here in dal to avoid db calls
+ project = await projectDAL.findById(projectId as string);
+ orgId = project.orgId;
+ }
- const plan = await licenseService.getPlan(orgId);
-
- // skip inserting if audit log retention is 0 meaning its not supported
- if (plan.auditLogsRetentionDays !== 0) {
- // For project actions, set TTL to project-level audit log retention config
- // This condition ensures that the plan's audit log retention days cannot be bypassed
- const ttlInDays =
- project?.auditLogsRetentionDays && project.auditLogsRetentionDays < plan.auditLogsRetentionDays
- ? project.auditLogsRetentionDays
- : plan.auditLogsRetentionDays;
-
- const ttl = ttlInDays * MS_IN_DAY;
-
- const createdAt = new Date(job.timestamp);
- const eventMetadata = normalizeJsonPayload(event.metadata);
- const actorMetadata = normalizeJsonPayload(actor.metadata);
-
- // UUIDv7 embeds job.timestamp so the id's time matches createdAt
- // UUIDv4 for Postgres (existing schema expectation)
- const id = isClickHouseBatchEnabled ? uuidv7({ msecs: createdAt.getTime() }) : randomUUID();
- const auditLog = {
- id,
- actor: actor.type,
- actorMetadata,
- ipAddress: normalizeEmptyValue(ipAddress, isClickHouseBatchEnabled),
- eventType: event.type,
- eventMetadata,
- userAgent: normalizeEmptyValue(userAgent, isClickHouseBatchEnabled),
- userAgentType: normalizeEmptyValue(userAgentType, isClickHouseBatchEnabled),
- projectId: normalizeEmptyValue(projectId, isClickHouseBatchEnabled),
- orgId,
- expiresAt: new Date(createdAt.getTime() + ttl),
- createdAt,
- updatedAt: createdAt,
- // project name is only used for non-ClickHouse insertion
- ...(!isClickHouseBatchEnabled ? { projectName: project?.name } : {})
- };
-
- // Push to Redis stream for ClickHouse batch processing
- if (isClickHouseBatchEnabled) {
- try {
- await keyStore.streamAdd(AUDIT_LOG_CLICKHOUSE_STREAM_KEY, "*", {
- data: JSON.stringify(auditLog)
- });
- } catch (error) {
- logger.error(error, "Failed to push audit log to Redis stream for ClickHouse batch");
+ const plan = await licenseService.getPlan(orgId);
+
+ // skip inserting if audit log retention is 0 meaning its not supported
+ if (plan.auditLogsRetentionDays !== 0) {
+ // For project actions, set TTL to project-level audit log retention config
+ // This condition ensures that the plan's audit log retention days cannot be bypassed
+ const ttlInDays =
+ project?.auditLogsRetentionDays && project.auditLogsRetentionDays < plan.auditLogsRetentionDays
+ ? project.auditLogsRetentionDays
+ : plan.auditLogsRetentionDays;
+
+ const ttl = ttlInDays * MS_IN_DAY;
+
+ const createdAt = new Date(job.timestamp);
+ const eventMetadata = normalizeJsonPayload(event.metadata);
+ const actorMetadata = normalizeJsonPayload(actor.metadata);
+
+ // UUIDv7 embeds job.timestamp so the id's time matches createdAt
+ // UUIDv4 for Postgres (existing schema expectation)
+ const id = isClickHouseBatchEnabled ? uuidv7({ msecs: createdAt.getTime() }) : randomUUID();
+ const auditLog = {
+ id,
+ actor: actor.type,
+ actorMetadata,
+ ipAddress: normalizeEmptyValue(ipAddress, isClickHouseBatchEnabled),
+ eventType: event.type,
+ eventMetadata,
+ userAgent: normalizeEmptyValue(userAgent, isClickHouseBatchEnabled),
+ userAgentType: normalizeEmptyValue(userAgentType, isClickHouseBatchEnabled),
+ projectId: normalizeEmptyValue(projectId, isClickHouseBatchEnabled),
+ orgId,
+ expiresAt: new Date(createdAt.getTime() + ttl),
+ createdAt,
+ updatedAt: createdAt,
+ // project name is only used for non-ClickHouse insertion
+ ...(!isClickHouseBatchEnabled ? { projectName: project?.name } : {})
+ };
+
+ // Push to Redis stream for ClickHouse batch processing
+ if (isClickHouseBatchEnabled) {
+ try {
+ await keyStore.streamAdd(AUDIT_LOG_CLICKHOUSE_STREAM_KEY, "*", {
+ data: JSON.stringify(auditLog)
+ });
+ } catch (error) {
+ logger.error(error, "Failed to push audit log to Redis stream for ClickHouse batch");
+ }
+ } else {
+ await auditLogDAL.create(auditLog);
}
- } else {
- await auditLogDAL.create(auditLog);
- }
- await auditLogStreamService.streamLog(orgId, auditLog);
+ await auditLogStreamService.streamLog(orgId, auditLog);
+ }
+ } catch (error) {
+ logger.error(
+ error,
+ `audit-log-queue: Failed to process audit log job [jobId=${job.id}] [event=${job.data.event?.type}] [orgId=${job.data.orgId}] [projectId=${job.data.projectId}]`
+ );
+ throw error;
}
});