Skip to content

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

change to PUT

Thiago Araujo da Silvacfe0b85d8a84
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index e9c63e891d..4c4416c75c 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -1015,7 +1015,9 @@ Enforcement is `isAuditLogEventEnabled` in the settings service, called from `bu
Suppressed events are dropped silently and do not count on the dropped counter. Management is
always on: the helper returns true for it before looking at any row, `toSettings` reports it as
enabled, and the update methods reject any request that names it, so the change that turns a
-class off is itself always recorded.
+class off is itself always recorded. An update is a full replacement: `PUT` must name every class in
+`CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES` exactly once, and the service deletes the scope's rows and
+inserts the new set, so there is no merge with what was stored before.
`PERMISSION_DENIED` is recorded by the `onError` hook in
`src/server/plugins/audit-log-permission-denied.ts` for every CASL `ForbiddenError` and
diff --git a/backend/src/ee/services/audit-log/audit-log-event-classes.ts b/backend/src/ee/services/audit-log/audit-log-event-classes.ts
index c160632070..1e70ae7b85 100644
--- a/backend/src/ee/services/audit-log/audit-log-event-classes.ts
+++ b/backend/src/ee/services/audit-log/audit-log-event-classes.ts
@@ -14,6 +14,14 @@ export const AUDIT_LOG_EVENT_CLASS_DEFAULTS: Record<AuditLogEventClass, boolean>
[AuditLogEventClass.Authorization]: false
};
+export const CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES = [
+ AuditLogEventClass.DataAccess,
+ AuditLogEventClass.Authentication,
+ AuditLogEventClass.Authorization
+] as const;
+
+export type TConfigurableAuditLogEventClass = (typeof CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES)[number];
+
export const AUTHENTICATION_EVENT_TYPES: readonly EventType[] = [
EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH,
EventType.LOGIN_IDENTITY_UNIVERSAL_AUTH_FAILED,
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.ts
index 5968474198..a140074771 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
@@ -64,9 +64,6 @@ const createHarness = ({ rows = [] as TRow[] } = {}) => {
const auditLogSettingsDAL = {
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)
};
@@ -146,27 +143,60 @@ describe("updateOrgSettings", () => {
expect(auditLogSettingsDAL.transaction).not.toHaveBeenCalled();
});
- test("answers from the rows it wrote and the scope's untouched rows, without re-reading", async () => {
+ test("refuses a request that leaves a class out", async () => {
+ const { service, auditLogSettingsDAL } = createHarness();
+
+ await expect(
+ service.updateOrgSettings({
+ actor: orgActor,
+ eventClasses: [{ eventClass: AuditLogEventClass.DataAccess, isEnabled: false }]
+ })
+ ).rejects.toThrow("Every event class except management must be included. Missing: authentication, authorization");
+ expect(auditLogSettingsDAL.transaction).not.toHaveBeenCalled();
+ });
+
+ test("refuses a request that names a class twice", async () => {
+ const { service, auditLogSettingsDAL } = createHarness();
+
+ await expect(
+ service.updateOrgSettings({
+ actor: orgActor,
+ eventClasses: [
+ { eventClass: AuditLogEventClass.DataAccess, isEnabled: false },
+ { eventClass: AuditLogEventClass.DataAccess, isEnabled: true },
+ { eventClass: AuditLogEventClass.Authentication, isEnabled: true },
+ { eventClass: AuditLogEventClass.Authorization, isEnabled: false }
+ ]
+ })
+ ).rejects.toThrow("Event class 'data-access' appears more than once");
+ expect(auditLogSettingsDAL.transaction).not.toHaveBeenCalled();
+ });
+
+ test("replaces the scope's rows and answers from the request, 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 }]
+ eventClasses: [
+ { eventClass: AuditLogEventClass.Authorization, isEnabled: true },
+ { eventClass: AuditLogEventClass.DataAccess, isEnabled: false },
+ { eventClass: AuditLogEventClass.Authentication, isEnabled: true }
+ ]
});
expect(auditLogSettingsDAL.findByOrgIds).not.toHaveBeenCalled();
- expect(auditLogSettingsDAL.delete).toHaveBeenCalledWith(
- { orgId: "org-1", projectId: null, $in: { eventClass: ["data-access"] } },
- expect.anything()
- );
+ expect(auditLogSettingsDAL.delete).toHaveBeenCalledWith({ orgId: "org-1", projectId: null }, expect.anything());
expect(auditLogSettingsDAL.insertMany).toHaveBeenCalledWith(
- [{ orgId: "org-1", projectId: null, eventClass: "data-access", isEnabled: false }],
+ [
+ { orgId: "org-1", projectId: null, eventClass: "data-access", isEnabled: false },
+ { orgId: "org-1", projectId: null, eventClass: "authentication", isEnabled: true },
+ { orgId: "org-1", projectId: null, eventClass: "authorization", isEnabled: true }
+ ],
expect.anything()
);
expect(keyStore.deleteItem).toHaveBeenCalledTimes(1);
@@ -175,8 +205,8 @@ describe("updateOrgSettings", () => {
eventClasses: [
{ eventClass: AuditLogEventClass.Management, isEnabled: true },
{ eventClass: AuditLogEventClass.DataAccess, isEnabled: false },
- { eventClass: AuditLogEventClass.Authentication, isEnabled: false },
- { eventClass: AuditLogEventClass.Authorization, isEnabled: false }
+ { eventClass: AuditLogEventClass.Authentication, isEnabled: true },
+ { eventClass: AuditLogEventClass.Authorization, isEnabled: true }
]
});
});
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.ts
index 11ba6bac27..c689e9afd1 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
@@ -10,7 +10,13 @@ import { TProjectDALFactory } from "@app/services/project/project-dal";
import { OrgPermissionActions, OrgPermissionSubjects } from "../permission/org-permission";
import { TPermissionServiceFactory } from "../permission/permission-service-types";
import { ProjectPermissionActions, ProjectPermissionSub } from "../permission/project-permission";
-import { AUDIT_LOG_EVENT_CLASS_DEFAULTS, AuditLogEventClass, getAuditLogEventClass } from "./audit-log-event-classes";
+import {
+ AUDIT_LOG_EVENT_CLASS_DEFAULTS,
+ AuditLogEventClass,
+ CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES,
+ getAuditLogEventClass,
+ TConfigurableAuditLogEventClass
+} from "./audit-log-event-classes";
import { TAuditLogSettingsDALFactory } from "./audit-log-settings-dal";
import {
TAuditLogEventClassOverrides,
@@ -64,11 +70,30 @@ const toSettings = (overrides: TAuditLogEventClassOverrides): TAuditLogEventClas
: (overrides[eventClass] ?? AUDIT_LOG_EVENT_CLASS_DEFAULTS[eventClass])
}));
-const toChanges = (eventClasses: TAuditLogEventClassSetting[]): [AuditLogEventClass, boolean][] => {
+// An update replaces the scope's settings, so the request must name every configurable class once.
+const toFullOverrides = (
+ eventClasses: TAuditLogEventClassSetting[]
+): Record<TConfigurableAuditLogEventClass, boolean> => {
if (eventClasses.some((el) => el.eventClass === AuditLogEventClass.Management)) {
throw new BadRequestError({ message: "Management events are always recorded and cannot be changed" });
}
- return [...new Map(eventClasses.map((el) => [el.eventClass, el.isEnabled])).entries()];
+ const seen = new Set<AuditLogEventClass>();
+ eventClasses.forEach((el) => {
+ if (seen.has(el.eventClass)) {
+ throw new BadRequestError({ message: `Event class '${el.eventClass}' appears more than once` });
+ }
+ seen.add(el.eventClass);
+ });
+ const missing = CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES.filter((eventClass) => !seen.has(eventClass));
+ if (missing.length) {
+ throw new BadRequestError({
+ message: `Every event class except management must be included. Missing: ${missing.join(", ")}`
+ });
+ }
+ return Object.fromEntries(eventClasses.map((el) => [el.eventClass, el.isEnabled])) as Record<
+ TConfigurableAuditLogEventClass,
+ boolean
+ >;
};
export const auditLogSettingsServiceFactory = ({
@@ -128,25 +153,20 @@ 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 })),
+ overrides: Record<TConfigurableAuditLogEventClass, boolean>
+ ) => {
+ await auditLogSettingsDAL.transaction(async (tx) => {
+ await auditLogSettingsDAL.delete(scope, tx);
+ await auditLogSettingsDAL.insertMany(
+ CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES.map((eventClass) => ({
+ ...scope,
+ eventClass,
+ isEnabled: overrides[eventClass]
+ })),
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) => {
@@ -183,10 +203,8 @@ export const auditLogSettingsServiceFactory = ({
const org = await orgDAL.findById(actor.orgId);
if (!org) throw new NotFoundError({ message: `Organization with ID '${actor.orgId}' not found` });
- const changed = toChanges(eventClasses);
- if (!changed.length) return getOrgSettings({ actor });
-
- const overrides = await writeScopeSettings({ orgId: actor.orgId, projectId: null }, changed);
+ const overrides = toFullOverrides(eventClasses);
+ await writeScopeSettings({ orgId: actor.orgId, projectId: null }, overrides);
return {
eventClasses: toSettings(overrides),
shouldUseNewPrivilegeSystem: Boolean(org.shouldUseNewPrivilegeSystem)
@@ -243,10 +261,8 @@ export const auditLogSettingsServiceFactory = ({
const org = await orgDAL.findById(project.orgId);
if (!org) throw new NotFoundError({ message: `Organization with ID '${project.orgId}' not found` });
- const changed = toChanges(eventClasses);
- if (!changed.length) return getProjectSettings(dto);
-
- const overrides = await writeScopeSettings({ orgId: project.orgId, projectId: dto.projectId }, changed);
+ const overrides = toFullOverrides(eventClasses);
+ await writeScopeSettings({ orgId: project.orgId, projectId: dto.projectId }, overrides);
return {
eventClasses: toSettings(overrides),
shouldUseNewPrivilegeSystem: Boolean(org.shouldUseNewPrivilegeSystem)
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 6a0626aaa6..b08a63a8a3 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -2087,14 +2087,14 @@ export const AUDIT_LOG_SETTINGS = {
"Whether the organization is on the new privilege system. Permission denials (the authorization class) are only recorded on the new privilege system.",
UPDATE_ORG: {
eventClasses:
- "The event classes to update. Management events are always recorded, so a request that includes the management class is rejected.",
+ "Every event class except management, each exactly once. The request replaces the current settings. Management events are always recorded, so a request that includes the management class is rejected.",
isEnabled:
"Whether to record events of this class for the organization. This only covers organization-level events; each project has its own setting."
},
UPDATE_PROJECT: {
projectId: "The ID of the project to update the audit log settings for.",
eventClasses:
- "The event classes to update. Management events are always recorded, so a request that includes the management class is rejected.",
+ "Every event class except management, each exactly once. The request replaces the current settings. Management events are always recorded, so a request that includes the management class is rejected.",
isEnabled:
"Whether to record events of this class for the project. A project that has never saved a setting uses the default."
},
diff --git a/docs/documentation/platform/audit-logs.mdx b/docs/documentation/platform/audit-logs.mdx
index d0fc9dfc29..165bd7a06a 100644
--- a/docs/documentation/platform/audit-logs.mdx
+++ b/docs/documentation/platform/audit-logs.mdx
@@ -268,13 +268,19 @@ By default, Infisical records every class except `authorization`. The `managemen
Only project admins can change these toggles. A project that has never saved a setting uses the defaults.
</Tab>
<Tab title="API">
- Update the organization's classes with [the organization audit log settings endpoint](/api-reference/endpoints/audit-logs/update-organization-audit-log-settings), or one project's classes with [the project audit log settings endpoint](/api-reference/endpoints/audit-logs/update-project-audit-log-settings).
+ Update the organization's classes with [the organization audit log settings endpoint](/api-reference/endpoints/audit-logs/update-organization-audit-log-settings), or one project's classes with [the project audit log settings endpoint](/api-reference/endpoints/audit-logs/update-project-audit-log-settings). The request replaces the current settings, so include every class except `management`.
```bash
curl -X PUT "https://app.infisical.com/api/v1/organization/audit-log-settings" \
-H "Authorization: Bearer <your-access-token>" \
-H "Content-Type: application/json" \
- -d '{"eventClasses": [{"eventClass": "data-access", "isEnabled": false}]}'
+ -d '{
+ "eventClasses": [
+ {"eventClass": "data-access", "isEnabled": false},
+ {"eventClass": "authentication", "isEnabled": true},
+ {"eventClass": "authorization", "isEnabled": false}
+ ]
+ }'
```
</Tab>
diff --git a/frontend/src/hooks/api/auditLogSettings/constants.tsx b/frontend/src/hooks/api/auditLogSettings/constants.tsx
index a35f954c17..ba14a14d8b 100644
--- a/frontend/src/hooks/api/auditLogSettings/constants.tsx
+++ b/frontend/src/hooks/api/auditLogSettings/constants.tsx
@@ -33,3 +33,10 @@ export const AUDIT_LOG_EVENT_CLASSES = [
AuditLogEventClass.Authentication,
AuditLogEventClass.Authorization
] as const;
+
+// Every class an update must name; the request replaces the scope's settings.
+export const CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES = [
+ AuditLogEventClass.DataAccess,
+ AuditLogEventClass.Authentication,
+ AuditLogEventClass.Authorization
+] as const;
diff --git a/frontend/src/pages/organization/AuditLogsPage/components/AuditLogSettingsTab/AuditLogSettingsTab.tsx b/frontend/src/pages/organization/AuditLogsPage/components/AuditLogSettingsTab/AuditLogSettingsTab.tsx
index 22baa7cdb1..519f1ada76 100644
--- a/frontend/src/pages/organization/AuditLogsPage/components/AuditLogSettingsTab/AuditLogSettingsTab.tsx
+++ b/frontend/src/pages/organization/AuditLogsPage/components/AuditLogSettingsTab/AuditLogSettingsTab.tsx
@@ -24,7 +24,8 @@ import { useScopeVariant } from "@app/hooks";
import { useGetOrgAuditLogSettings, useUpdateOrgAuditLogSettings } from "@app/hooks/api";
import {
AUDIT_LOG_EVENT_CLASS_DEFAULTS,
- AUDIT_LOG_EVENT_CLASSES
+ AUDIT_LOG_EVENT_CLASSES,
+ CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES
} from "@app/hooks/api/auditLogSettings/constants";
import { AuditLogEventClass } from "@app/hooks/api/auditLogSettings/types";
@@ -53,7 +54,7 @@ export const AuditLogSettingsTab = withPermission(
control,
handleSubmit,
reset,
- formState: { isDirty, dirtyFields }
+ formState: { isDirty }
} = useForm<TForm>({
values: Object.fromEntries(
AUDIT_LOG_EVENT_CLASSES.map((eventClass) => [eventClass, isEnabled(eventClass)])
@@ -64,10 +65,10 @@ export const AuditLogSettingsTab = withPermission(
settings?.shouldUseNewPrivilegeSystem ?? currentOrg.shouldUseNewPrivilegeSystem;
const onSubmit = async (form: TForm) => {
- const eventClasses = AUDIT_LOG_EVENT_CLASSES.filter(
- (eventClass) => eventClass !== AuditLogEventClass.Management && dirtyFields[eventClass]
- ).map((eventClass) => ({ eventClass, isEnabled: form[eventClass] }));
- if (!eventClasses.length) return;
+ const eventClasses = CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES.map((eventClass) => ({
+ eventClass,
+ isEnabled: form[eventClass]
+ }));
await updateSettings({ eventClasses });
createNotification({ text: "Audit log settings saved", type: "success" });
diff --git a/frontend/src/pages/project/SettingsPage/components/AuditLogEventClassesSection/AuditLogEventClassesSection.tsx b/frontend/src/pages/project/SettingsPage/components/AuditLogEventClassesSection/AuditLogEventClassesSection.tsx
index a229a35b69..389e67a93b 100644
--- a/frontend/src/pages/project/SettingsPage/components/AuditLogEventClassesSection/AuditLogEventClassesSection.tsx
+++ b/frontend/src/pages/project/SettingsPage/components/AuditLogEventClassesSection/AuditLogEventClassesSection.tsx
@@ -17,7 +17,8 @@ import { useOrganization, useProject, useProjectPermission } from "@app/context"
import { useGetProjectAuditLogSettings, useUpdateProjectAuditLogSettings } from "@app/hooks/api";
import {
AUDIT_LOG_EVENT_CLASS_DEFAULTS,
- AUDIT_LOG_EVENT_CLASSES
+ AUDIT_LOG_EVENT_CLASSES,
+ CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES
} from "@app/hooks/api/auditLogSettings/constants";
import { AuditLogEventClass } from "@app/hooks/api/auditLogSettings/types";
import { ProjectMembershipRole } from "@app/hooks/api/roles/types";
@@ -44,7 +45,7 @@ export const AuditLogEventClassesSection = () => {
control,
handleSubmit,
reset,
- formState: { isDirty, dirtyFields }
+ formState: { isDirty }
} = useForm<TForm>({
values: Object.fromEntries(
AUDIT_LOG_EVENT_CLASSES.map((eventClass) => [eventClass, isEnabled(eventClass)])
@@ -55,10 +56,10 @@ export const AuditLogEventClassesSection = () => {
settings?.shouldUseNewPrivilegeSystem ?? currentOrg.shouldUseNewPrivilegeSystem;
const onSubmit = async (form: TForm) => {
- const eventClasses = AUDIT_LOG_EVENT_CLASSES.filter(
- (eventClass) => eventClass !== AuditLogEventClass.Management && dirtyFields[eventClass]
- ).map((eventClass) => ({ eventClass, isEnabled: form[eventClass] }));
- if (!eventClasses.length) return;
+ const eventClasses = CONFIGURABLE_AUDIT_LOG_EVENT_CLASSES.map((eventClass) => ({
+ eventClass,
+ isEnabled: form[eventClass]
+ }));
await updateSettings({ projectId: currentProject.id, eventClasses });
createNotification({ text: "Audit log settings saved", type: "success" });