Skip to content

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

refactor: simplify secret read status logic and remove unused properties

Dimitris Staratzis5548031629e7
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 19ff97e6d3..052dc19035 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -1108,11 +1108,12 @@ and the events cannot say which folders a recursive read returned. Instead it ke
referencing it, or the source of a replication import (imports and references check the reader's own access
to the source, so revoking it would break them). The card computes the whole project once and caches it for
5 minutes, grouping principals by identical rules so each rule set is checked once per secret.
-- **Unread is only "No reads" after a full window.** A principal who has not read a secret is `tracking`, not
- `no-reads`, until both the secret (`trackedSince`) and their access have existed for the whole window. Access
- age is `findAccessChangedAt`: the latest membership creation, role or privilege (folder grants included)
- creation or edit. Which secrets a change opened up is not recorded, so any change restarts the window, and a
- custom role's rules being edited is not seen at all.
+- **The card only judges a full window.** It lists a principal who has not read a secret only once both the
+ secret (`trackedSince`) and their access have existed for the whole window, and leaves them out before that;
+ there is no separate status for it. Access age is `findAccessChangedAt`: the latest membership creation, role
+ or privilege (folder grants included) creation or edit. Which secrets a change opened up is not recorded, so
+ any change restarts the window, and a custom role's rules being edited is not seen at all. The per-secret
+ sheet shows the plain fact instead: a read, or "No reads".
### Audit Log Event Classes and Settings
diff --git a/backend/src/ee/routes/v1/insights-router.ts b/backend/src/ee/routes/v1/insights-router.ts
index bf0546ccee..1d69b9017e 100644
--- a/backend/src/ee/routes/v1/insights-router.ts
+++ b/backend/src/ee/routes/v1/insights-router.ts
@@ -501,8 +501,7 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
principalId: z.string(),
principalName: z.string(),
readStatus: z.nativeEnum(SecretReadStatus),
- lastReadAt: z.date().nullable(),
- trackedSince: z.date()
+ lastReadAt: z.date().nullable()
})
.array()
})
diff --git a/backend/src/ee/routes/v1/secret-router.ts b/backend/src/ee/routes/v1/secret-router.ts
index 0b904bf386..7fe8c4ab4d 100644
--- a/backend/src/ee/routes/v1/secret-router.ts
+++ b/backend/src/ee/routes/v1/secret-router.ts
@@ -109,7 +109,6 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {
200: z.object({
readActivity: z.object({
trackedSince: z.date(),
- windowStart: z.date(),
reads: z
.object({
actorType: z.enum([ActorType.USER, ActorType.IDENTITY]),
@@ -117,12 +116,6 @@ export const registerSecretRouter = async (server: FastifyZodProvider) => {
status: z.nativeEnum(SecretReadStatus),
lastReadAt: z.date()
})
- .array(),
- accessChanges: z
- .object({
- actorId: z.string(),
- accessChangedAt: z.date()
- })
.array()
})
})
diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts
index cbaff9bc14..7b69aed7b4 100644
--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts
+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts
@@ -5,7 +5,7 @@ import { ActorType } from "@app/services/auth/auth-type";
import {
classifySecretReads,
computeSecretReach,
- getUnreadStatus,
+ coversWholeWindow,
getUtcMonthStart,
indexSecretReads
} from "./secret-read-activity-fns";
@@ -114,26 +114,19 @@ describe("classifySecretReads", () => {
});
});
-describe("getUnreadStatus", () => {
+describe("coversWholeWindow", () => {
const windowStart = new Date("2026-09-15T00:00:00.000Z");
- test("flags no reads once the secret and the access have both existed for the whole window", () => {
- expect(getUnreadStatus(windowStart, new Date("2026-08-01T00:00:00.000Z"), windowStart)).toEqual({
- trackedSince: windowStart,
- readStatus: SecretReadStatus.NoReads
- });
+ test("is true once the secret and the access have both existed for the whole window", () => {
+ expect(coversWholeWindow(windowStart, new Date("2026-08-01T00:00:00.000Z"), windowStart)).toBe(true);
+ expect(coversWholeWindow(windowStart, undefined, windowStart)).toBe(true);
});
- test("keeps tracking a secret newer than the window", () => {
- const createdAt = new Date("2026-10-01T00:00:00.000Z");
- expect(getUnreadStatus(createdAt, undefined, windowStart).readStatus).toBe(SecretReadStatus.Tracking);
+ test("is false for a secret newer than the window", () => {
+ expect(coversWholeWindow(new Date("2026-10-01T00:00:00.000Z"), undefined, windowStart)).toBe(false);
});
- test("keeps tracking, from the change, a reader whose access changed inside the window", () => {
- const accessChangedAt = new Date("2026-10-10T00:00:00.000Z");
- expect(getUnreadStatus(windowStart, accessChangedAt, windowStart)).toEqual({
- trackedSince: accessChangedAt,
- readStatus: SecretReadStatus.Tracking
- });
+ test("is false for a reader whose access changed inside the window", () => {
+ expect(coversWholeWindow(windowStart, new Date("2026-10-10T00:00:00.000Z"), windowStart)).toBe(false);
});
});
diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts
index 3ef13307fc..d6b80dbb48 100644
--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts
+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts
@@ -53,14 +53,9 @@ export const computeSecretReach = (secret: TReachSecret, graph: TSecretReachGrap
export const indexSecretReads = (reads: TSecretLastRead[]) => groupBy(reads, (read) => read.objectId);
// An unread secret only counts as unused once both it and the reader's current access have existed for the
-// whole window.
-export const getUnreadStatus = (trackedSince: Date, accessChangedAt: Date | undefined, windowStart: Date) => {
- const judgedSince = accessChangedAt && accessChangedAt > trackedSince ? accessChangedAt : trackedSince;
- return {
- trackedSince: judgedSince,
- readStatus: judgedSince > windowStart ? SecretReadStatus.Tracking : SecretReadStatus.NoReads
- };
-};
+// whole window. Before that, the reader has not had the whole window to read it.
+export const coversWholeWindow = (trackedSince: Date, accessChangedAt: Date | undefined, windowStart: Date) =>
+ trackedSince <= windowStart && (!accessChangedAt || accessChangedAt <= windowStart);
// Reads before trackedSince are ignored: the secret or its folder did not exist yet.
export const classifySecretReads = (
diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
index c7643289b8..b32d743c8f 100644
--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts
@@ -33,7 +33,7 @@ import { TSecretReadActivityDALFactory } from "./secret-read-activity-dal";
import {
classifySecretReads,
computeSecretReach,
- getUnreadStatus,
+ coversWholeWindow,
getUtcMonthStart,
indexSecretReads,
normalizeSecretPath,
@@ -560,11 +560,11 @@ export const secretReadActivityServiceFactory = ({
) => {
const { permission } = await checkInsightsPermission(permissionService, licenseService, projectId, actor);
- const windowStart = $getWindowStart();
- const [[activity], accessChangedAtByActorId] = await Promise.all([
- $getReadActivity({ projectId, windowStart, secret: { environment, secretPath, secretName } }),
- secretReadActivityDAL.findAccessChangedAt(projectId)
- ]);
+ const [activity] = await $getReadActivity({
+ projectId,
+ windowStart: $getWindowStart(),
+ secret: { environment, secretPath, secretName }
+ });
if (!activity) {
throw new NotFoundError({
message: `Secret with name '${secretName}' not found at path '${secretPath}' in environment '${environment}'`
@@ -577,16 +577,11 @@ export const secretReadActivityServiceFactory = ({
secretTags: activity.secret.tags?.map((tag) => tag.slug)
});
- return {
- trackedSince: activity.trackedSince,
- windowStart,
- reads: [...activity.reads.values()],
- accessChanges: [...accessChangedAtByActorId].map(([actorId, accessChangedAt]) => ({ actorId, accessChangedAt }))
- };
+ return { trackedSince: activity.trackedSince, reads: [...activity.reads.values()] };
};
- // Everyone who can read a secret's value but has not read it, ranked by the secrets with the most of them. Those
- // whose secret or access is newer than the window come last, marked as still being tracked.
+ // Everyone who can read a secret's value but has not read it in the whole window, ranked by the secrets with the
+ // most of them.
const $computeUnusedSecretAccess = async (projectId: string, orgId: string) => {
const windowStart = $getWindowStart();
const [activities, { userPermissions, identityPermissions }, accessChangedAtByActorId] = await Promise.all([
@@ -625,7 +620,12 @@ export const secretReadActivityServiceFactory = ({
)
.flat()
.flatMap((principal) => {
- if (reads.has(`${principal.actorType}:${principal.id}`)) return [];
+ if (
+ reads.has(`${principal.actorType}:${principal.id}`) ||
+ !coversWholeWindow(trackedSince, accessChangedAtByActorId.get(principal.id), windowStart)
+ ) {
+ return [];
+ }
return {
secretKey: secret.key,
environment,
@@ -633,19 +633,13 @@ export const secretReadActivityServiceFactory = ({
principalType: principal.actorType,
principalId: principal.id,
principalName: principal.name,
- lastReadAt: null,
- ...getUnreadStatus(trackedSince, accessChangedAtByActorId.get(principal.id), windowStart)
+ readStatus: SecretReadStatus.NoReads,
+ lastReadAt: null
};
})
);
}
- const countNoReads = (rows: TUnusedSecretAccess[]) =>
- rows.filter((row) => row.readStatus === SecretReadStatus.NoReads).length;
- const ranked = rowsBySecret.sort((a, b) => countNoReads(b) - countNoReads(a)).flat();
- return [
- ...ranked.filter((row) => row.readStatus === SecretReadStatus.NoReads),
- ...ranked.filter((row) => row.readStatus === SecretReadStatus.Tracking)
- ];
+ return rowsBySecret.sort((a, b) => b.length - a.length).flat();
};
// The whole project is computed once and cached, so paging through it is cheap.
@@ -662,7 +656,7 @@ export const secretReadActivityServiceFactory = ({
ttlSeconds: UNUSED_SECRET_ACCESS_CACHE_TTL_SECONDS,
fetcher: () => $computeUnusedSecretAccess(projectId, actor.orgId),
reviver: (parsed) => {
- reviveDates(parsed, "lastReadAt", "trackedSince");
+ reviveDates(parsed, "lastReadAt");
}
});
const remainingTTL = await getCacheTtl(keyStore, cacheKey);
@@ -671,11 +665,7 @@ export const secretReadActivityServiceFactory = ({
const sorted = orderBy
? [...unusedAccess].sort((a, b) => {
const [first, second] = orderDirection === OrderByDirection.DESC ? [b, a] : [a, b];
- const firstValue = first[orderBy];
- const secondValue = second[orderBy];
- return firstValue instanceof Date && secondValue instanceof Date
- ? firstValue.getTime() - secondValue.getTime()
- : String(firstValue).localeCompare(String(secondValue));
+ return first[orderBy].localeCompare(second[orderBy]);
})
: unusedAccess;
diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts
index e48ae14e14..0b8fa66c9f 100644
--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts
+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts
@@ -6,12 +6,9 @@ import { ActorType } from "@app/services/auth/auth-type";
// on write and folds away on read.
export type TSecretLastRead = Omit<TSecretReadActivity, "readMonth">;
-// Tracking: not read yet, but the secret or the reader's access to it is newer than the window, so it is too
-// early to call the access unused.
export enum SecretReadStatus {
Read = "read",
- NoReads = "no-reads",
- Tracking = "tracking"
+ NoReads = "no-reads"
}
// A bulk read that returned whole folders is recorded against them. Anything narrower, and every single read,
@@ -110,7 +107,6 @@ export type TUnusedSecretAccess = {
principalName: string;
readStatus: SecretReadStatus;
lastReadAt: Date | null;
- trackedSince: Date;
};
// Each value is the row field it sorts by.
@@ -119,8 +115,7 @@ export enum UnusedSecretAccessOrderBy {
Environment = "environment",
SecretPath = "secretPath",
Principal = "principalName",
- Status = "readStatus",
- TrackedSince = "trackedSince"
+ Status = "readStatus"
}
export type TListUnusedSecretAccessDTO = {
diff --git a/frontend/src/components/secrets/SecretReadStatusBadge.tsx b/frontend/src/components/secrets/SecretReadStatusBadge.tsx
index 91720b4c38..5d38d02489 100644
--- a/frontend/src/components/secrets/SecretReadStatusBadge.tsx
+++ b/frontend/src/components/secrets/SecretReadStatusBadge.tsx
@@ -1,14 +1,11 @@
-import { addDays, differenceInCalendarDays, format } from "date-fns";
+import { differenceInCalendarDays } from "date-fns";
-import { Badge, Tooltip, TooltipContent, TooltipTrigger } from "@app/components/v3";
+import { Badge } from "@app/components/v3";
import { SecretReadStatus } from "@app/hooks/api/secrets/types";
-const SECRET_READ_WINDOW_DAYS = 30;
-
type Props = {
status: SecretReadStatus;
lastReadAt?: string | null;
- trackedSince?: string;
};
// Only a reader's first read of the day is recorded, so the time of day would understate later reads.
@@ -19,23 +16,9 @@ const formatReadDay = (lastReadAt: string) => {
return `${days} days ago`;
};
-export const SecretReadStatusBadge = ({ status, lastReadAt, trackedSince }: Props) => {
+export const SecretReadStatusBadge = ({ status, lastReadAt }: Props) => {
if (status === SecretReadStatus.Read && lastReadAt) {
return <span>{formatReadDay(lastReadAt)}</span>;
}
- if (status === SecretReadStatus.Tracking && trackedSince) {
- return (
- <Tooltip>
- <TooltipTrigger asChild>
- <Badge variant="neutral">Tracking</Badge>
- </TooltipTrigger>
- <TooltipContent>
- Not read since {format(new Date(trackedSince), "PP")}, when this secret or access was
- created or last changed. Shown as unused if still unread on{" "}
- {format(addDays(new Date(trackedSince), SECRET_READ_WINDOW_DAYS), "PP")}.
- </TooltipContent>
- </Tooltip>
- );
- }
return <Badge variant="danger">No reads</Badge>;
};
diff --git a/frontend/src/hooks/api/secretInsights/types.ts b/frontend/src/hooks/api/secretInsights/types.ts
index 8ce9dc8b6d..1dfa77e1a4 100644
--- a/frontend/src/hooks/api/secretInsights/types.ts
+++ b/frontend/src/hooks/api/secretInsights/types.ts
@@ -58,8 +58,7 @@ export enum UnusedSecretAccessOrderBy {
Environment = "environment",
SecretPath = "secretPath",
Principal = "principalName",
- Status = "readStatus",
- TrackedSince = "trackedSince"
+ Status = "readStatus"
}
export type TGetUnusedSecretAccessDTO = {
@@ -79,7 +78,6 @@ export type TUnusedSecretAccess = {
principalName: string;
readStatus: SecretReadStatus;
lastReadAt: string | null;
- trackedSince: string;
};
export type TGetUnusedSecretAccessResponse = {
diff --git a/frontend/src/hooks/api/secrets/types.ts b/frontend/src/hooks/api/secrets/types.ts
index a8b18c525a..c9561cc3c4 100644
--- a/frontend/src/hooks/api/secrets/types.ts
+++ b/frontend/src/hooks/api/secrets/types.ts
@@ -164,23 +164,17 @@ export type TGetSecretReadActivityDTO = Omit<TGetSecretAccessListDTO, "includeAl
export enum SecretReadStatus {
Read = "read",
- NoReads = "no-reads",
- Tracking = "tracking"
+ NoReads = "no-reads"
}
export type TSecretReadActivity = {
trackedSince: string;
- windowStart: string;
reads: {
actorType: "user" | "identity";
actorId: string;
status: SecretReadStatus;
lastReadAt: string;
}[];
- accessChanges: {
- actorId: string;
- accessChangedAt: string;
- }[];
};
export type TCreateSecretsV3DTO = {
diff --git a/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx b/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
index 23fbf858a7..5d04097c46 100644
--- a/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
+++ b/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
@@ -1,5 +1,4 @@
import axios from "axios";
-import { format } from "date-fns";
import { ChevronDownIcon, FolderIcon, HardDriveIcon, KeyIcon, UserIcon } from "lucide-react";
import { SecretReadStatusBadge } from "@app/components/secrets/SecretReadStatusBadge";
@@ -85,8 +84,7 @@ export const UnusedSecretAccessCard = () => {
{ column: UnusedSecretAccessOrderBy.Environment, label: "Environment" },
{ column: UnusedSecretAccessOrderBy.SecretPath, label: "Path" },
{ column: UnusedSecretAccessOrderBy.Principal, label: "Principal" },
- { column: UnusedSecretAccessOrderBy.Status, label: "Status" },
- { column: UnusedSecretAccessOrderBy.TrackedSince, label: "Tracked Since" }
+ { column: UnusedSecretAccessOrderBy.Status, label: "Status" }
];
return (
@@ -124,7 +122,8 @@ export const UnusedSecretAccessCard = () => {
<EmptyHeader>
<EmptyTitle>No unused access</EmptyTitle>
<EmptyDescription>
- Everyone who can read this project&apos;s secrets has read them in the last 30 days.
+ Access shows here once someone has been able to read a secret for 30 days without
+ reading it.
</EmptyDescription>
</EmptyHeader>
</Empty>
@@ -186,10 +185,8 @@ export const UnusedSecretAccessCard = () => {
<SecretReadStatusBadge
status={row.readStatus}
lastReadAt={row.lastReadAt}
- trackedSince={row.trackedSince}
/>
</TableCell>
- <TableCell>{format(new Date(row.trackedSince), "PP")}</TableCell>
</TableRow>
);
})}
diff --git a/frontend/src/pages/secret-manager/OverviewPage/components/SecretTableRow/SecretAccessInsights.tsx b/frontend/src/pages/secret-manager/OverviewPage/components/SecretTableRow/SecretAccessInsights.tsx
index 9bdd7336c0..5426d98d13 100644
--- a/frontend/src/pages/secret-manager/OverviewPage/components/SecretTableRow/SecretAccessInsights.tsx
+++ b/frontend/src/pages/secret-manager/OverviewPage/components/SecretTableRow/SecretAccessInsights.tsx
@@ -484,13 +484,6 @@ export function SecretAccessInsights({ secretKey, environment, secretPath }: Pro
() => new Map(readActivity?.reads.map((read) => [`${read.actorType}:${read.actorId}`, read])),
[readActivity]
);
- const accessChangedAt = useMemo(
- () =>
- new Map(
- readActivity?.accessChanges.map((change) => [change.actorId, change.accessChangedAt])
- ),
- [readActivity]
- );
let readActivityDescription = "Loading read activity...";
if (readActivityError) {
@@ -508,23 +501,10 @@ export function SecretAccessInsights({ secretKey, environment, secretPath }: Pro
if (!readActivity) return null;
const read = lastReads.get(`${row.type}:${row.entry.id}`);
- if (read) return <SecretReadStatusBadge status={read.status} lastReadAt={read.lastReadAt} />;
-
- // Mirrors the Insights card: unread access is only called unused once both the secret and the access
- // have existed for the whole window.
- const changedAt = accessChangedAt.get(row.entry.id);
- const trackedSince =
- changedAt && new Date(changedAt) > new Date(readActivity.trackedSince)
- ? changedAt
- : readActivity.trackedSince;
return (
<SecretReadStatusBadge
- status={
- new Date(trackedSince) > new Date(readActivity.windowStart)
- ? SecretReadStatus.Tracking
- : SecretReadStatus.NoReads
- }
- trackedSince={trackedSince}
+ status={read?.status ?? SecretReadStatus.NoReads}
+ lastReadAt={read?.lastReadAt}
/>
);
};