Skip to content

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

add remaining TTL

Matheus Nogueiraad23670aee66
diff --git a/backend/src/ee/routes/v1/insights-router.ts b/backend/src/ee/routes/v1/insights-router.ts
index aa21313ce3..a9a1103ba0 100644
--- a/backend/src/ee/routes/v1/insights-router.ts
+++ b/backend/src/ee/routes/v1/insights-router.ts
@@ -195,14 +195,22 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {
}
},
onRequest: verifyAuth([AuthMode.JWT]),
- handler: async (req) => {
+ handler: async (req, reply) => {
const { projectId, offset, limit } = req.query;
- const result = await server.services.insights.getSecretsDuplication({ projectId, offset, limit }, req.permission);
+ const { result, remainingTTL } = await server.services.insights.getSecretsDuplication(
+ { projectId, offset, limit },
+ req.permission
+ );
await server.services.auditLog.createAuditLog({
projectId,
event: { type: EventType.VIEW_INSIGHTS_SECRETS_DUPLICATION, metadata: { projectId } },
...req.auditLogInfo
});
+
+ if (remainingTTL >= 0) {
+ void reply.header("X-Cache-TTL", remainingTTL);
+ }
+
return result;
}
});
diff --git a/backend/src/ee/services/insights/insights-service.ts b/backend/src/ee/services/insights/insights-service.ts
index 9d23701331..907139142d 100644
--- a/backend/src/ee/services/insights/insights-service.ts
+++ b/backend/src/ee/services/insights/insights-service.ts
@@ -9,7 +9,7 @@ import { TPermissionServiceFactory } from "@app/ee/services/permission/permissio
import { ProjectPermissionInsightsActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";
import { TSecretRotationV2DALFactory } from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-dal";
import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
-import { withCache } from "@app/lib/cache/with-cache";
+import { getCacheTtl, withCache } from "@app/lib/cache/with-cache";
import { BadRequestError } from "@app/lib/errors";
import { OrgServiceActor } from "@app/lib/types";
import { ActorType } from "@app/services/auth/auth-type";
@@ -47,7 +47,7 @@ type TInsightsServiceFactoryDep = {
projectDAL: Pick<TProjectDALFactory, "findById">;
userDAL: Pick<TUserDALFactory, "find">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
- keyStore: Pick<TKeyStoreFactory, "setItemWithExpiry" | "getItem">;
+ keyStore: Pick<TKeyStoreFactory, "setItemWithExpiry" | "getItem" | "ttl">;
};
export type TInsightsServiceFactory = ReturnType<typeof insightsServiceFactory>;
@@ -512,14 +512,19 @@ export const insightsServiceFactory = ({
if (!project.secretBlindIndexEnabled) {
return {
- secretBlindIndexEnabled: false as const,
- groups: [] as {
- secrets: { key: string; environment: string; environmentSlug: string; secretPath: string }[];
- }[]
+ result: {
+ secretBlindIndexEnabled: false as const,
+ groups: [] as {
+ secrets: { key: string; environment: string; environmentSlug: string; secretPath: string }[];
+ }[]
+ },
+ remainingTTL: -1
};
}
- return withCache({
+ const remainingTTL = await getCacheTtl(keyStore, cacheKey);
+
+ const result = await withCache({
keyStore,
key: cacheKey,
ttlSeconds: KeyStoreTtls.InsightsDuplicationCacheInSeconds,
@@ -563,6 +568,8 @@ export const insightsServiceFactory = ({
return { secretBlindIndexEnabled: true as const, groups };
}
});
+
+ return { result, remainingTTL };
};
return {
diff --git a/backend/src/ee/services/pam-insights/pam-insights-service.ts b/backend/src/ee/services/pam-insights/pam-insights-service.ts
index 648496fa54..93034104bd 100644
--- a/backend/src/ee/services/pam-insights/pam-insights-service.ts
+++ b/backend/src/ee/services/pam-insights/pam-insights-service.ts
@@ -38,7 +38,7 @@ type TPamInsightsServiceFactoryDep = {
| "findRotationCandidatesByProject"
>;
pamResourceRotationRulesDAL: Pick<TPamResourceRotationRulesDALFactory, "findByResourceIds">;
- keyStore: Pick<TKeyStoreFactory, "setItemWithExpiry" | "getItem">;
+ keyStore: Pick<TKeyStoreFactory, "setItemWithExpiry" | "getItem" | "ttl">;
};
export type TPamInsightsServiceFactory = ReturnType<typeof pamInsightsServiceFactory>;
diff --git a/backend/src/lib/cache/with-cache.test.ts b/backend/src/lib/cache/with-cache.test.ts
index 6a261912a3..e93e4384b5 100644
--- a/backend/src/lib/cache/with-cache.test.ts
+++ b/backend/src/lib/cache/with-cache.test.ts
@@ -10,12 +10,14 @@ describe("withCache", () => {
let mockKeyStore: {
getItem: ReturnType<typeof vi.fn>;
setItemWithExpiry: ReturnType<typeof vi.fn>;
+ ttl: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
mockKeyStore = {
getItem: vi.fn(),
- setItemWithExpiry: vi.fn().mockResolvedValue("OK")
+ setItemWithExpiry: vi.fn().mockResolvedValue("OK"),
+ ttl: vi.fn().mockResolvedValue(-2)
};
});
@@ -362,12 +364,14 @@ describe("withCacheFingerprint", () => {
let mockKeyStore: {
getItem: ReturnType<typeof vi.fn>;
setItemWithExpiry: ReturnType<typeof vi.fn>;
+ ttl: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
mockKeyStore = {
getItem: vi.fn(),
- setItemWithExpiry: vi.fn().mockResolvedValue("OK")
+ setItemWithExpiry: vi.fn().mockResolvedValue("OK"),
+ ttl: vi.fn().mockResolvedValue(-2)
};
});
diff --git a/backend/src/lib/cache/with-cache.ts b/backend/src/lib/cache/with-cache.ts
index 6aab904929..62845d8f1e 100644
--- a/backend/src/lib/cache/with-cache.ts
+++ b/backend/src/lib/cache/with-cache.ts
@@ -2,7 +2,7 @@ import { TKeyStoreFactory } from "@app/keystore/keystore";
import { logger } from "@app/lib/logger";
import { permissionCacheFingerprintDurationHistogram, permissionCacheLookupCounter } from "@app/lib/telemetry/metrics";
-type TCacheKeyStore = Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry">;
+type TCacheKeyStore = Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "ttl">;
/** Read a raw string from Redis, returning null on miss or error. */
const cacheGet = async (keyStore: TCacheKeyStore, key: string, errMsg: string): Promise<string | null> => {
@@ -77,6 +77,21 @@ export const withCache = async <T>({ keyStore, key, ttlSeconds, fetcher, reviver
return result;
};
+/**
+ * Returns the remaining TTL (in seconds) for a cache key, or -1 if the key does not exist / has no expiry.
+ */
+export const getCacheTtl = async (keyStore: TCacheKeyStore, key: string): Promise<number> => {
+ try {
+ const remaining = await keyStore.ttl(key);
+ // Redis returns -2 if key doesn't exist, -1 if no expiry is set
+ if (remaining < 0) return -1;
+ return remaining;
+ } catch (err) {
+ logger.warn({ key, err }, `getCacheTtl: failed to read TTL [key=${key}]`);
+ return -1;
+ }
+};
+
type TWithCacheFingerprintOpts<T> = {
keyStore: TCacheKeyStore;
dataKey: string;
diff --git a/backend/src/services/announcement/announcement-service.ts b/backend/src/services/announcement/announcement-service.ts
index 3ce62b2de1..faa30e8373 100644
--- a/backend/src/services/announcement/announcement-service.ts
+++ b/backend/src/services/announcement/announcement-service.ts
@@ -44,7 +44,7 @@ const sanitizeLink = (link: string | null | undefined): string | null => {
type TAnnouncementServiceFactoryDep = {
userDAL: Pick<TUserDALFactory, "findById" | "updateById">;
- keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry">;
+ keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "ttl">;
};
export type TAnnouncementServiceFactory = ReturnType<typeof announcementServiceFactory>;
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index 59533eb4bb..341234b857 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -195,7 +195,7 @@ type TProjectServiceFactoryDep = {
licenseService: Pick<TLicenseServiceFactory, "getPlan" | "invalidateGetPlan">;
smtpService: Pick<TSmtpService, "sendMail">;
orgDAL: Pick<TOrgDALFactory, "findOne" | "findEffectiveOrgMembership">;
- keyStore: Pick<TKeyStoreFactory, "deleteItem" | "acquireLock" | "getItem" | "setItemWithExpiry">;
+ keyStore: Pick<TKeyStoreFactory, "deleteItem" | "acquireLock" | "getItem" | "setItemWithExpiry" | "ttl">;
roleDAL: Pick<TRoleDALFactory, "find" | "insertMany" | "delete">;
kmsService: Pick<
TKmsServiceFactory,
diff --git a/backend/src/services/super-admin/super-admin-service.ts b/backend/src/services/super-admin/super-admin-service.ts
index cb73986549..e38239cdc3 100644
--- a/backend/src/services/super-admin/super-admin-service.ts
+++ b/backend/src/services/super-admin/super-admin-service.ts
@@ -86,7 +86,7 @@ type TSuperAdminServiceFactoryDep = {
kmsService: Pick<TKmsServiceFactory, "encryptWithRootKey" | "decryptWithRootKey" | "updateEncryptionStrategy">;
kmsRootConfigDAL: TKmsRootConfigDALFactory;
orgService: Pick<TOrgServiceFactory, "createOrganization">;
- keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "deleteItem" | "deleteItems">;
+ keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "deleteItem" | "deleteItems" | "ttl">;
licenseService: Pick<TLicenseServiceFactory, "onPremFeatures" | "updateSubscriptionOrgMemberCount">;
microsoftTeamsService: Pick<TMicrosoftTeamsServiceFactory, "initializeTeamsBot">;
invalidateCacheQueue: TInvalidateCacheQueueFactory;
diff --git a/frontend/src/hooks/api/secretInsights/queries.tsx b/frontend/src/hooks/api/secretInsights/queries.tsx
index 3d397eac94..c6f3a4e6a4 100644
--- a/frontend/src/hooks/api/secretInsights/queries.tsx
+++ b/frontend/src/hooks/api/secretInsights/queries.tsx
@@ -184,11 +184,12 @@ export const useGetSecretsDuplication = (
return useQuery({
queryKey: secretInsightsKeys.secretsDuplication(params),
queryFn: async () => {
- const { data } = await apiRequest.get<TGetSecretsDuplicationResponse>(
+ const res = await apiRequest.get<TGetSecretsDuplicationResponse>(
"/api/v1/insights/secrets/secrets-duplication",
{ params }
);
- return data;
+ const remainingTtl = Number(res.headers["x-cache-ttl"] ?? -1);
+ return { ...res.data, remainingTtl };
},
staleTime: INSIGHTS_STALE_TIME,
...options
diff --git a/frontend/src/hooks/api/secretInsights/types.ts b/frontend/src/hooks/api/secretInsights/types.ts
index f567959349..eca15db325 100644
--- a/frontend/src/hooks/api/secretInsights/types.ts
+++ b/frontend/src/hooks/api/secretInsights/types.ts
@@ -141,6 +141,7 @@ export type TDuplicatedSecretGroup = {
export type TGetSecretsDuplicationResponse = {
secretBlindIndexEnabled: boolean;
groups: TDuplicatedSecretGroup[];
+ remainingTtl: number;
};
export type TGetSecretBlindIndexStatusDTO = {
diff --git a/frontend/src/pages/secret-manager/InsightsPage/components/DuplicatedSecretsCard.tsx b/frontend/src/pages/secret-manager/InsightsPage/components/DuplicatedSecretsCard.tsx
index bced561aac..d619fcc9b4 100644
--- a/frontend/src/pages/secret-manager/InsightsPage/components/DuplicatedSecretsCard.tsx
+++ b/frontend/src/pages/secret-manager/InsightsPage/components/DuplicatedSecretsCard.tsx
@@ -132,6 +132,11 @@ export const DuplicatedSecretsCard = () => {
);
};
+ const remainingMinutes =
+ data?.remainingTtl != null && data.remainingTtl >= 0
+ ? Math.max(1, Math.ceil(data.remainingTtl / 60))
+ : null;
+
const isMigrationRunning = migrationTriggered && migrationStatus !== "failed";
const isMigrationFailed = migrationStatus === "failed" && !enableBlindIndex.isPending;
const showEnableButton = !secretBlindIndexEnabled && !isMigrationRunning && !isMigrationFailed;
@@ -139,9 +144,16 @@ export const DuplicatedSecretsCard = () => {
return (
<Card>
<CardHeader>
- <CardTitle>Duplicated Secrets</CardTitle>
+ <CardTitle>
+ Duplicated Secrets
+ {remainingMinutes != null && (
+ <Badge variant="neutral" className="ml-2 font-normal">
+ Updates in {remainingMinutes} {remainingMinutes === 1 ? "minute" : "minutes"}
+ </Badge>
+ )}
+ </CardTitle>
<CardDescription>
- Detect secrets that share the same value across environments and paths
+ <span>Detect secrets that share the same value across environments and paths</span>
</CardDescription>
</CardHeader>
<CardContent>