Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix put
Thiago Araujo da Silva938463ad9c88
diff --git a/backend/src/ee/services/audit-log/audit-log-settings-service.test.ts b/backend/src/ee/services/audit-log/audit-log-settings-service.test.tsindex 5b011a79e6..71d0b6bb76 100644--- a/backend/src/ee/services/audit-log/audit-log-settings-service.test.ts+++ b/backend/src/ee/services/audit-log/audit-log-settings-service.test.ts@@ -1,5 +1,7 @@+import { createMongoAbility } from "@casl/ability";import { beforeEach, describe, expect, test, vi } from "vitest";+import { OrgPermissionActions, OrgPermissionSubjects } from "../permission/org-permission";import { AuditLogEventClass } from "./audit-log-event-classes";import { auditLogSettingsServiceFactory, isAuditLogEventEnabled } from "./audit-log-settings-service";import { TEffectiveAuditLogSettings } from "./audit-log-settings-types";@@ -46,12 +48,27 @@ describe("isAuditLogEventEnabled", () => {type TRow = { orgId: string; projectId: string | null; eventClass: string; isEnabled: boolean };+const orgActor = {+ type: "user",+ id: "user-1",+ authMethod: null,+ orgId: "org-1",+ rootOrgId: "org-1",+ parentOrgId: "org-1"+} as never;+const createHarness = ({ rows = [] as TRow[] } = {}) => {const orgDAL = {findById: vi.fn(async (id: string) => ({ id, shouldUseNewPrivilegeSystem: true }))};const auditLogSettingsDAL = {- findByOrgIds: vi.fn(async (orgIds: string[]) => rows.filter((row) => orgIds.includes(row.orgId)))+ findByOrgIds: vi.fn(async (orgIds: string[]) => rows.filter((row) => orgIds.includes(row.orgId))),+ transaction: vi.fn(async (cb: (tx: unknown) => Promise<unknown>) => cb({})),+ find: vi.fn(async (filter: { orgId: string; projectId: string | null }) =>+ rows.filter((row) => row.orgId === filter.orgId && row.projectId === filter.projectId)+ ),+ delete: vi.fn(async () => []),+ insertMany: vi.fn(async (data: TRow[]) => data)};const keyStore = {getItem: vi.fn(async () => null),@@ -59,11 +76,17 @@ const createHarness = ({ rows = [] as TRow[] } = {}) => {deleteItem: vi.fn(async () => 1)};+ const permissionService = {+ getOrgPermission: vi.fn(async () => ({+ permission: createMongoAbility([{ action: OrgPermissionActions.Edit, subject: OrgPermissionSubjects.Settings }])+ }))+ };+const service = auditLogSettingsServiceFactory({auditLogSettingsDAL: auditLogSettingsDAL as never,orgDAL: orgDAL as never,projectDAL: {} as never,- permissionService: {} as never,+ permissionService: permissionService as never,keyStore: keyStore as never});@@ -102,3 +125,44 @@ describe("getEffectiveSettings", () => {expect(keyStore.setItemWithExpiry).toHaveBeenCalledTimes(1);});});++describe("updateOrgSettings", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ test("answers from the rows it wrote and the scope's untouched rows, without re-reading", async () => {+ const { service, auditLogSettingsDAL, keyStore } = createHarness({+ rows: [+ { orgId: "org-1", projectId: null, eventClass: "authentication", isEnabled: false },+ { orgId: "org-1", projectId: null, eventClass: "data-access", isEnabled: true },+ { orgId: "org-1", projectId: "p1", eventClass: "data-access", isEnabled: false }+ ]+ });++ const result = await service.updateOrgSettings({+ actor: orgActor,+ eventClasses: [{ eventClass: AuditLogEventClass.DataAccess, isEnabled: false }]+ });++ expect(auditLogSettingsDAL.findByOrgIds).not.toHaveBeenCalled();+ expect(auditLogSettingsDAL.delete).toHaveBeenCalledWith(+ { orgId: "org-1", projectId: null, $in: { eventClass: ["data-access"] } },+ expect.anything()+ );+ expect(auditLogSettingsDAL.insertMany).toHaveBeenCalledWith(+ [{ orgId: "org-1", projectId: null, eventClass: "data-access", isEnabled: false }],+ expect.anything()+ );+ expect(keyStore.deleteItem).toHaveBeenCalledTimes(1);+ expect(result).toEqual({+ shouldUseNewPrivilegeSystem: true,+ eventClasses: [+ { eventClass: AuditLogEventClass.Management, isEnabled: true },+ { eventClass: AuditLogEventClass.DataAccess, isEnabled: false },+ { eventClass: AuditLogEventClass.Authentication, isEnabled: false },+ { eventClass: AuditLogEventClass.Authorization, isEnabled: false }+ ]+ });+ });+});diff --git a/backend/src/ee/services/audit-log/audit-log-settings-service.ts b/backend/src/ee/services/audit-log/audit-log-settings-service.tsindex 6cae520471..63e0156833 100644--- a/backend/src/ee/services/audit-log/audit-log-settings-service.ts+++ b/backend/src/ee/services/audit-log/audit-log-settings-service.ts@@ -117,6 +117,29 @@ export const auditLogSettingsServiceFactory = ({}};+ const writeScopeSettings = async (+ scope: { orgId: string; projectId: string | null },+ changed: [AuditLogEventClass, boolean][]+ ): Promise<TAuditLogEventClassOverrides> => {+ const changedClasses = changed.map(([eventClass]) => eventClass);+ const rows = await auditLogSettingsDAL.transaction(async (tx) => {+ const existing = await auditLogSettingsDAL.find(scope, { tx });+ await auditLogSettingsDAL.delete({ ...scope, $in: { eventClass: changedClasses } }, tx);+ const inserted = await auditLogSettingsDAL.insertMany(+ changed.map(([eventClass, isEnabled]) => ({ ...scope, eventClass, isEnabled })),+ tx+ );+ return [...existing.filter((row) => !changedClasses.includes(row.eventClass as AuditLogEventClass)), ...inserted];+ });+ await invalidateCache(scope.orgId);++ const overrides: TAuditLogEventClassOverrides = {};+ rows.forEach((row) => {+ if (isKnownEventClass(row.eventClass)) overrides[row.eventClass] = row.isEnabled;+ });+ return overrides;+ };+const getOrgSettings = async ({ actor }: TGetOrgAuditLogSettingsDTO) => {const { permission } = await permissionService.getOrgPermission({scope: OrganizationActionScope.Any,@@ -148,23 +171,17 @@ export const auditLogSettingsServiceFactory = ({});ForbiddenError.from(permission).throwUnlessCan(OrgPermissionActions.Edit, OrgPermissionSubjects.Settings);- const changed = [...new Map(eventClasses.map((el) => [el.eventClass, el.isEnabled])).entries()];+ const org = await orgDAL.findById(actor.orgId);+ if (!org) throw new NotFoundError({ message: `Organization with ID '${actor.orgId}' not found` });- if (changed.length) {- await auditLogSettingsDAL.transaction(async (tx) => {- await auditLogSettingsDAL.delete(- { orgId: actor.orgId, projectId: null, $in: { eventClass: changed.map(([eventClass]) => eventClass) } },- tx- );- await auditLogSettingsDAL.insertMany(- changed.map(([eventClass, isEnabled]) => ({ orgId: actor.orgId, projectId: null, eventClass, isEnabled })),- tx- );- });- await invalidateCache(actor.orgId);- }+ const changed = [...new Map(eventClasses.map((el) => [el.eventClass, el.isEnabled])).entries()];+ if (!changed.length) return getOrgSettings({ actor });- return getOrgSettings({ actor });+ const overrides = await writeScopeSettings({ orgId: actor.orgId, projectId: null }, changed);+ return {+ eventClasses: toSettings(overrides),+ shouldUseNewPrivilegeSystem: Boolean(org.shouldUseNewPrivilegeSystem)+ };};const getProjectSettings = async ({@@ -214,28 +231,17 @@ export const auditLogSettingsServiceFactory = ({const project = await projectDAL.findById(dto.projectId);if (!project) throw new NotFoundError({ message: `Project with ID '${dto.projectId}' not found` });- const changed = [...new Map(eventClasses.map((el) => [el.eventClass, el.isEnabled])).entries()];+ const org = await orgDAL.findById(project.orgId);+ if (!org) throw new NotFoundError({ message: `Organization with ID '${project.orgId}' not found` });- if (changed.length) {- await auditLogSettingsDAL.transaction(async (tx) => {- await auditLogSettingsDAL.delete(- { projectId: dto.projectId, $in: { eventClass: changed.map(([eventClass]) => eventClass) } },- tx- );- await auditLogSettingsDAL.insertMany(- changed.map(([eventClass, isEnabled]) => ({- orgId: project.orgId,- projectId: dto.projectId,- eventClass,- isEnabled- })),- tx- );- });- await invalidateCache(project.orgId);- }+ const changed = [...new Map(eventClasses.map((el) => [el.eventClass, el.isEnabled])).entries()];+ if (!changed.length) return getProjectSettings(dto);- return getProjectSettings(dto);+ const overrides = await writeScopeSettings({ orgId: project.orgId, projectId: dto.projectId }, changed);+ return {+ eventClasses: toSettings(overrides),+ shouldUseNewPrivilegeSystem: Boolean(org.shouldUseNewPrivilegeSystem)+ };};return {