From 213f2ed29b65cc07028a13e9d190b609857ec22e Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Fri, 1 Mar 2024 19:24:29 +0530 Subject: [PATCH 1/2] fix(server): auditlog won't push if retention period is zero --- .../ee/services/audit-log/audit-log-queue.ts | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) 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 6f2c93221..e248bda2b 100644 --- a/backend/src/ee/services/audit-log/audit-log-queue.ts +++ b/backend/src/ee/services/audit-log/audit-log-queue.ts @@ -22,23 +22,13 @@ export const auditLogQueueServiceFactory = ({ licenseService }: TAuditLogQueueServiceFactoryDep) => { const pushToLog = async (data: TCreateAuditLogDTO) => { - await queueService.queue(QueueName.AuditLog, QueueJobs.AuditLog, data, { - removeOnFail: { - count: 5 - }, - removeOnComplete: true - }); - }; - - queueService.start(QueueName.AuditLog, async (job) => { - const { actor, event, ipAddress, projectId, userAgent, userAgentType } = job.data; - let { orgId } = job.data; + let { orgId } = data; const MS_IN_DAY = 24 * 60 * 60 * 1000; if (!orgId) { // it will never be undefined for both org and project id // TODO(akhilmhdh): use caching here in dal to avoid db calls - const project = await projectDAL.findById(projectId as string); + const project = await projectDAL.findById(data.projectId as string); orgId = project.orgId; } @@ -46,6 +36,26 @@ export const auditLogQueueServiceFactory = ({ const ttl = plan.auditLogsRetentionDays * MS_IN_DAY; // skip inserting if audit log retention is 0 meaning its not supported if (ttl === 0) return; + + await queueService.queue( + QueueName.AuditLog, + QueueJobs.AuditLog, + { ...data, orgId }, + { + removeOnFail: { + count: 3 + }, + removeOnComplete: true + } + ); + }; + + queueService.start(QueueName.AuditLog, async (job) => { + const { actor, event, ipAddress, projectId, userAgent, userAgentType, orgId } = job.data; + const MS_IN_DAY = 24 * 60 * 60 * 1000; + + const plan = await licenseService.getPlan(orgId as string); + const ttl = plan.auditLogsRetentionDays * MS_IN_DAY; await auditLogDAL.create({ actor: actor.type, actorMetadata: actor.metadata, From c8b7c37aee4359388feb1d669d0a88c596effa38 Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Fri, 1 Mar 2024 20:10:27 +0530 Subject: [PATCH 2/2] fix(server): identity login audit log fixed --- backend/src/server/routes/v1/identity-ua.ts | 3 ++- backend/src/services/identity-ua/identity-ua-service.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/src/server/routes/v1/identity-ua.ts b/backend/src/server/routes/v1/identity-ua.ts index 11b8e0e8d..4499a88e7 100644 --- a/backend/src/server/routes/v1/identity-ua.ts +++ b/backend/src/server/routes/v1/identity-ua.ts @@ -39,11 +39,12 @@ export const registerIdentityUaRouter = async (server: FastifyZodProvider) => { } }, handler: async (req) => { - const { identityUa, accessToken, identityAccessToken, validClientSecretInfo } = + const { identityUa, accessToken, identityAccessToken, validClientSecretInfo, identityMembershipOrg } = await server.services.identityUa.login(req.body.clientId, req.body.clientSecret, req.realIp); await server.services.auditLog.createAuditLog({ ...req.auditLogInfo, + orgId: identityMembershipOrg?.orgId, event: { type: EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH, metadata: { diff --git a/backend/src/services/identity-ua/identity-ua-service.ts b/backend/src/services/identity-ua/identity-ua-service.ts index 3b73da6d4..d375a8fa5 100644 --- a/backend/src/services/identity-ua/identity-ua-service.ts +++ b/backend/src/services/identity-ua/identity-ua-service.ts @@ -54,6 +54,8 @@ export const identityUaServiceFactory = ({ const identityUa = await identityUaDAL.findOne({ clientId }); if (!identityUa) throw new UnauthorizedError(); + const identityMembershipOrg = await identityOrgMembershipDAL.findOne({ identityId: identityUa.identityId }); + checkIPAgainstBlocklist({ ipAddress: ip, trustedIps: identityUa.clientSecretTrustedIps as TIp[] @@ -131,7 +133,7 @@ export const identityUaServiceFactory = ({ } ); - return { accessToken, identityUa, validClientSecretInfo, identityAccessToken }; + return { accessToken, identityUa, validClientSecretInfo, identityAccessToken, identityMembershipOrg }; }; const attachUa = async ({