Skip to content

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

feat: add trackingStartedAt and unusedAccessFrom to unused secret access response

Dimitris Staratzisb846ff0248fb
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 052dc19035..5eb2daf989 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -1113,7 +1113,10 @@ and the events cannot say which folders a recursive read returned. Instead it ke
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".
+ sheet shows the plain fact instead: a read, or "No reads". The card response carries `trackingStartedAt` (the
+ later of the migration that created the table and the project's creation) and `unusedAccessFrom` (a window
+ after it), so the UI can say tracking is in progress instead of showing an empty list as "no unused access".
+ The dev `tsx watch` sometimes misses edits to this module; restart `infisical-dev-api` when the API looks stale.
### 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 1d69b9017e..ccb6e6df1a 100644
--- a/backend/src/ee/routes/v1/insights-router.ts
+++ b/backend/src/ee/routes/v1/insights-router.ts
@@ -492,6 +492,8 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
response: {
200: z.object({
totalCount: z.number(),
+ trackingStartedAt: z.date(),
+ unusedAccessFrom: z.date(),
unusedAccess: z
.object({
secretKey: z.string(),
@@ -510,7 +512,7 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
onRequest: verifyAuth([AuthMode.JWT, AuthMode.OAUTH]),
handler: async (req, reply) => {
const { projectId } = req.params;
- const { unusedAccess, totalCount, remainingTTL } =
+ const { unusedAccess, totalCount, trackingStartedAt, unusedAccessFrom, remainingTTL } =
await server.services.secretReadActivity.listUnusedSecretAccess({ projectId, ...req.query }, req.permission);
await server.services.auditLog.createAuditLog({
projectId,
@@ -522,7 +524,7 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
void reply.header("X-Cache-TTL", remainingTTL);
}
- return { unusedAccess, totalCount };
+ return { unusedAccess, totalCount, trackingStartedAt, unusedAccessFrom };
}
});
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 b32d743c8f..e5c4170a1d 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
@@ -21,6 +21,7 @@ import { OrderByDirection, OrgServiceActor } from "@app/lib/types";
import { ActorType } from "@app/services/auth/auth-type";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { KmsDataKey } from "@app/services/kms/kms-types";
+import { TProjectDALFactory } from "@app/services/project/project-dal";
import { TProjectEnvDALFactory } from "@app/services/project-env/project-env-dal";
import { TSecretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
import { buildFolderIdMap, buildFolderPath } from "@app/services/secret-folder/secret-folder-fns";
@@ -70,6 +71,7 @@ type TSecretReadActivityServiceFactoryDep = {
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
secretReadActivityDAL: TSecretReadActivityDALFactory;
folderDAL: Pick<TSecretFolderDALFactory, "findByProjectId">;
+ projectDAL: Pick<TProjectDALFactory, "findById">;
projectEnvDAL: Pick<TProjectEnvDALFactory, "find">;
secretImportDAL: Pick<TSecretImportDALFactory, "find" | "findByIds">;
secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "find" | "findSecretReferencesByProjectId">;
@@ -96,6 +98,7 @@ export const secretReadActivityServiceFactory = ({
licenseService,
secretReadActivityDAL,
folderDAL,
+ projectDAL,
projectEnvDAL,
secretImportDAL,
secretV2BridgeDAL,
@@ -659,7 +662,16 @@ export const secretReadActivityServiceFactory = ({
reviveDates(parsed, "lastReadAt");
}
});
- const remainingTTL = await getCacheTtl(keyStore, cacheKey);
+ const [remainingTTL, instanceTrackingStartedAt, project] = await Promise.all([
+ getCacheTtl(keyStore, cacheKey),
+ $getTrackingStartedAt(),
+ projectDAL.findById(projectId)
+ ]);
+ // Reads are known from the later of when tracking began and when the project was created, and nothing can be
+ // called unused until a whole window has passed since then.
+ const projectTrackingStartedAt = new Date(
+ Math.max(instanceTrackingStartedAt?.getTime() ?? 0, project?.createdAt.getTime() ?? 0)
+ );
// Unsorted, the list keeps its ranking. The sort is stable, so ties keep it too.
const sorted = orderBy
@@ -672,6 +684,8 @@ export const secretReadActivityServiceFactory = ({
return {
unusedAccess: sorted.slice(offset, offset + limit),
totalCount: sorted.length,
+ trackingStartedAt: projectTrackingStartedAt,
+ unusedAccessFrom: new Date(projectTrackingStartedAt.getTime() + SECRET_READ_ACTIVITY_WINDOW_DAYS * DAY_IN_MS),
remainingTTL
};
};
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 7798af59d1..fd26671e56 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -3487,6 +3487,7 @@ export const registerRoutes = async (
licenseService,
secretReadActivityDAL: secretReadActivityDALFactory(db),
folderDAL,
+ projectDAL,
projectEnvDAL,
secretImportDAL,
secretV2BridgeDAL,
diff --git a/frontend/src/hooks/api/secretInsights/types.ts b/frontend/src/hooks/api/secretInsights/types.ts
index 1dfa77e1a4..758bba0e8c 100644
--- a/frontend/src/hooks/api/secretInsights/types.ts
+++ b/frontend/src/hooks/api/secretInsights/types.ts
@@ -83,6 +83,8 @@ export type TUnusedSecretAccess = {
export type TGetUnusedSecretAccessResponse = {
unusedAccess: TUnusedSecretAccess[];
totalCount: number;
+ trackingStartedAt: string;
+ unusedAccessFrom: string;
remainingTtl: number;
};
diff --git a/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx b/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
index 5d04097c46..b129e52d4c 100644
--- a/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
+++ b/frontend/src/pages/secret-manager/InsightsPage/components/UnusedSecretAccessCard.tsx
@@ -1,4 +1,5 @@
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";
@@ -66,6 +67,7 @@ export const UnusedSecretAccessCard = () => {
data?.remainingTtl != null && data.remainingTtl >= 0
? formatRemainingTtl(data.remainingTtl)
: null;
+ const isStillTracking = Boolean(data && new Date(data.unusedAccessFrom) > new Date());
const environmentNames = new Map(currentProject.environments.map((env) => [env.slug, env.name]));
const getSortDirection = (column: UnusedSecretAccessOrderBy): TableSortDirection => {
@@ -117,7 +119,19 @@ export const UnusedSecretAccessCard = () => {
</EmptyHeader>
</Empty>
)}
- {data && totalCount === 0 && (
+ {data && isStillTracking && (
+ <Empty className="border">
+ <EmptyHeader>
+ <EmptyTitle>Tracking reads</EmptyTitle>
+ <EmptyDescription>
+ Reads have been tracked since {format(new Date(data.trackingStartedAt), "PP")}.
+ Access that goes unread for 30 days shows here from{" "}
+ {format(new Date(data.unusedAccessFrom), "PP")}.
+ </EmptyDescription>
+ </EmptyHeader>
+ </Empty>
+ )}
+ {data && !isStillTracking && totalCount === 0 && (
<Empty className="border">
<EmptyHeader>
<EmptyTitle>No unused access</EmptyTitle>
@@ -128,7 +142,7 @@ export const UnusedSecretAccessCard = () => {
</EmptyHeader>
</Empty>
)}
- {data && totalCount > 0 && (
+ {data && !isStillTracking && totalCount > 0 && (
<>
<Table>
<TableHeader>