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
refac: change method name to be similar to the route
adilson07bcfbb22f18
diff --git a/backend/e2e-test/insights-static-secret-usage.spec.ts b/backend/e2e-test/insights-static-secret-usage.spec.tsnew file mode 100644index 0000000000..f51d932248--- /dev/null+++ b/backend/e2e-test/insights-static-secret-usage.spec.ts@@ -0,0 +1,286 @@+import { randomUUID } from "node:crypto";++import { ForbiddenError } from "@casl/ability";+import { Knex } from "knex";+import { afterAll, beforeAll, describe, expect, test } from "vitest";++import {+ ProjectType,+ ProjectVersion,+ TableName,+ TOrganizationsInsert,+ TProjectEnvironmentsInsert,+ TProjectsInsert,+ TSecretFoldersInsert,+ TSecretsV2Insert,+ TUsersInsert+} from "@app/db/schemas";+import { insightsDALFactory } from "@app/ee/services/insights/insights-dal";+import { insightsServiceFactory } from "@app/ee/services/insights/insights-service";+import { TOrgInsightsDTO } from "@app/ee/services/insights/insights-types";+import { ActorType, AuthMethod } from "@app/services/auth/auth-type";++import { buildOrgInsightsGateStubs, passThroughKeyStore, projectScopedInsightsDepStubs } from "./testUtils/insights";++declare const testDb: Knex;++const ORG_ID = randomUUID();+const OTHER_ORG_ID = randomUUID();+const suffix = randomUUID().slice(0, 8);++const USER_ID = randomUUID();++const WEEKS_IN_WINDOW = 12;+const DAY_MS = 24 * 60 * 60 * 1000;++// The UTC week rule is restated here rather than imported from insights-fns, so the expected+// buckets are derived independently of the implementation that produces them. It is the same+// three lines either way; sharing them would make every assertion below self-referential.+const mondayOfUtcWeek = (date: Date) => {+ const monday = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));+ monday.setUTCDate(monday.getUTCDate() - ((monday.getUTCDay() + 6) % 7));++ return monday;+};++const shiftUtcDays = (date: Date, days: number) => {+ const shifted = new Date(date);+ shifted.setUTCDate(shifted.getUTCDate() + days);++ return shifted;+};++const toUtcDateString = (date: Date) => date.toISOString().slice(0, 10);++// weeksAgo(0) is the Monday of the week in progress, weeksAgo(11) the oldest week still in window.+const CURRENT_WEEK_START = mondayOfUtcWeek(new Date());+const weeksAgo = (count: number) => shiftUtcDays(CURRENT_WEEK_START, -count * 7);+const WINDOW_START = weeksAgo(WEEKS_IN_WINDOW - 1);++const EXPECTED_WEEK_STARTS = Array.from({ length: WEEKS_IN_WINDOW }, (_, index) =>+ toUtcDateString(weeksAgo(WEEKS_IN_WINDOW - 1 - index))+);++// One project per rule the org-wide query filters on. Only `counted` may contribute.+const projects = {+ counted: { projectId: randomUUID(), envId: randomUUID(), folderId: randomUUID(), name: `usage-${suffix}` },+ secondCounted: {+ projectId: randomUUID(),+ envId: randomUUID(),+ folderId: randomUUID(),+ name: `usage-second-${suffix}`+ },+ otherProductType: { projectId: randomUUID(), envId: randomUUID(), folderId: randomUUID(), name: `pam-${suffix}` },+ deletedProject: { projectId: randomUUID(), envId: randomUUID(), folderId: randomUUID(), name: `del-${suffix}` },+ deletedEnvironment: {+ projectId: randomUUID(),+ envId: randomUUID(),+ folderId: randomUUID(),+ name: `delenv-${suffix}`+ },+ oldVersion: { projectId: randomUUID(), envId: randomUUID(), folderId: randomUUID(), name: `v1-${suffix}` },+ otherOrg: { projectId: randomUUID(), envId: randomUUID(), folderId: randomUUID(), name: `other-${suffix}` }+};++const projectList = Object.values(projects);++const { permissionService, licenseService, setCanReadInsights, setPlanHasInsights } = buildOrgInsightsGateStubs();++const insightsService = insightsServiceFactory({+ ...projectScopedInsightsDepStubs,+ permissionService,+ licenseService,+ orgDAL: { countAllOrgMembers: async () => 0 },+ identityOrgMembershipDAL: { countAllOrgIdentities: async () => 0 },+ dynamicSecretLeaseDAL: { countLeasesForOrg: async () => 0 },+ insightsDAL: insightsDALFactory(testDb),+ keyStore: passThroughKeyStore+});++const actor = (orgId: string): TOrgInsightsDTO => ({+ actor: ActorType.USER,+ actorId: USER_ID,+ actorAuthMethod: AuthMethod.EMAIL,+ actorOrgId: orgId,+ orgId+});++// createdAt is what the weekly bucketing reads, and the generated insert type omits it because the+// column is defaulted. Backdating has to happen at INSERT: secrets_v2 carries an on-update trigger,+// so a later UPDATE would only move updatedAt anyway, and createdAt is not writable through the DAL.+const secret = (folderId: string, createdAt: Date, overrides: Partial<TSecretsV2Insert> = {}) =>+ ({+ id: randomUUID(),+ key: `SECRET_${randomUUID().slice(0, 8)}`,+ folderId,+ createdAt,+ ...overrides+ }) as TSecretsV2Insert & { id: string };++// Half a week past the Monday, so a fixture cannot land in a neighbouring bucket through rounding.+const midWeek = (weekStart: Date) => new Date(weekStart.getTime() + 3 * DAY_MS + 12 * 60 * 60 * 1000);++describe("insights static secrets usage", () => {+ beforeAll(async () => {+ const orgRows: (TOrganizationsInsert & { id: string })[] = [+ { id: ORG_ID, name: `usage-${suffix}`, slug: `usage-${suffix}` },+ { id: OTHER_ORG_ID, name: `usage-other-${suffix}`, slug: `usage-other-${suffix}` }+ ];+ await testDb(TableName.Organization).insert(orgRows);++ // Owner of the personal-override secret that must be excluded from the counts.+ const userRows: (TUsersInsert & { id: string })[] = [+ { id: USER_ID, username: `usage-user-${suffix}`, isAccepted: true, isGhost: false }+ ];+ await testDb(TableName.Users).insert(userRows);++ const projectRows: (TProjectsInsert & { id: string })[] = projectList.map((p) => ({+ id: p.projectId,+ name: p.name,+ slug: p.name,+ orgId: p === projects.otherOrg ? OTHER_ORG_ID : ORG_ID,+ type: p === projects.otherProductType ? ProjectType.PAM : ProjectType.SecretManager,+ // Only v3 secret-manager projects are counted (the column defaults to v1).+ version: p === projects.oldVersion ? ProjectVersion.V1 : ProjectVersion.V3,+ ...(p === projects.deletedProject ? { deleteAfter: new Date() } : {})+ }));+ await testDb(TableName.Project).insert(projectRows);++ const environmentRows: (TProjectEnvironmentsInsert & { id: string })[] = projectList.map((p, index) => ({+ id: p.envId,+ name: "Development",+ slug: "dev",+ position: index + 1,+ projectId: p.projectId,+ ...(p === projects.deletedEnvironment ? { deleteAfter: new Date() } : {})+ }));+ await testDb(TableName.Environment).insert(environmentRows);++ const folderRows: (TSecretFoldersInsert & { id: string })[] = projectList.map((p) => ({+ id: p.folderId,+ name: "root",+ envId: p.envId,+ parentId: null+ }));+ await testDb(TableName.SecretFolder).insert(folderRows);++ const secretRows: (TSecretsV2Insert & { id: string })[] = [+ // Week in progress: 2. One sits exactly on the Monday boundary, which belongs to the current+ // week rather than the one before it (the history query is half-open at currentWeekStart).+ secret(projects.counted.folderId, CURRENT_WEEK_START),+ secret(projects.counted.folderId, new Date()),+ // Last week: 3, spread over two projects so the aggregate is genuinely org-wide.+ secret(projects.counted.folderId, midWeek(weeksAgo(1))),+ secret(projects.counted.folderId, midWeek(weeksAgo(1))),+ secret(projects.secondCounted.folderId, midWeek(weeksAgo(1))),+ // Oldest week still in window: 1, exactly on the inclusive lower bound.+ secret(projects.counted.folderId, WINDOW_START),+ // One millisecond before the window opens: dropped entirely.+ secret(projects.counted.folderId, new Date(WINDOW_START.getTime() - 1)),+ // Excluded by scope. All dated to last week, so a leak shows up as 4 instead of 3.+ secret(projects.counted.folderId, midWeek(weeksAgo(1)), { userId: USER_ID, type: "personal" }),+ secret(projects.otherProductType.folderId, midWeek(weeksAgo(1))),+ secret(projects.deletedProject.folderId, midWeek(weeksAgo(1))),+ secret(projects.deletedEnvironment.folderId, midWeek(weeksAgo(1))),+ secret(projects.oldVersion.folderId, midWeek(weeksAgo(1))),+ secret(projects.otherOrg.folderId, midWeek(weeksAgo(1)))+ ];+ await testDb(TableName.SecretV2).insert(secretRows);+ });++ afterAll(async () => {+ const orgIds = [ORG_ID, OTHER_ORG_ID];+ await testDb(TableName.SecretV2)+ .whereIn(+ "folderId",+ projectList.map((p) => p.folderId)+ )+ .delete();+ await testDb(TableName.SecretFolder)+ .whereIn(+ "id",+ projectList.map((p) => p.folderId)+ )+ .delete();+ await testDb(TableName.Environment)+ .whereIn(+ "id",+ projectList.map((p) => p.envId)+ )+ .delete();+ await testDb(TableName.Project).whereIn("orgId", orgIds).delete();+ await testDb(TableName.Users).where("id", USER_ID).delete();+ await testDb(TableName.Organization).whereIn("id", orgIds).delete();+ });++ test("returns twelve UTC calendar weeks, oldest first, with only the last one partial", async () => {+ const { weeks } = await insightsService.getStaticSecretsUsage(actor(ORG_ID));++ expect(weeks).toHaveLength(WEEKS_IN_WINDOW);+ expect(weeks.map((week) => week.weekStart)).toEqual(EXPECTED_WEEK_STARTS);+ expect(weeks.map((week) => week.isPartial)).toEqual([...Array(WEEKS_IN_WINDOW - 1).fill(false), true]);+ });++ test("counts creations into the week they happened in and zero-fills the rest", async () => {+ const { weeks } = await insightsService.getStaticSecretsUsage(actor(ORG_ID));+ const countFor = (weekStart: Date) => weeks.find((week) => week.weekStart === toUtcDateString(weekStart));++ // The Monday-boundary secret and the one created now.+ expect(countFor(CURRENT_WEEK_START)).toMatchObject({ totalSecrets: 2, isPartial: true });+ // Two in the counted project and one in the second, across the same org.+ expect(countFor(weeksAgo(1))).toMatchObject({ totalSecrets: 3, isPartial: false });+ // The secret sitting exactly on the inclusive lower bound of the window.+ expect(countFor(WINDOW_START)).toMatchObject({ totalSecrets: 1, isPartial: false });++ // Every other week saw nothing and is reported as zero rather than skipped.+ const populated = new Set([CURRENT_WEEK_START, weeksAgo(1), WINDOW_START].map(toUtcDateString));+ expect(weeks.filter((week) => !populated.has(week.weekStart)).map((week) => week.totalSecrets)).toEqual(+ Array(WEEKS_IN_WINDOW - 3).fill(0)+ );++ // The secret created one millisecond before the window is the only one unaccounted for.+ expect(weeks.reduce((sum, week) => sum + week.totalSecrets, 0)).toBe(6);+ });++ test("a newly created secret lands in the week in progress", async () => {+ const before = await insightsService.getStaticSecretsUsage(actor(ORG_ID));+ const beforeCurrent = before.weeks[before.weeks.length - 1].totalSecrets;++ const added = secret(projects.counted.folderId, new Date());+ await testDb(TableName.SecretV2).insert(added);++ try {+ const after = await insightsService.getStaticSecretsUsage(actor(ORG_ID));+ expect(after.weeks[after.weeks.length - 1].totalSecrets).toBe(beforeCurrent + 1);+ // Only the week in progress moves; the closed weeks are untouched.+ expect(after.weeks.slice(0, -1)).toEqual(before.weeks.slice(0, -1));+ } finally {+ await testDb(TableName.SecretV2).where("id", added.id).delete();+ }+ });++ test("an organization that has created nothing reports twelve zeroed weeks", async () => {+ const { weeks } = await insightsService.getStaticSecretsUsage(actor(randomUUID()));++ expect(weeks.map((week) => week.weekStart)).toEqual(EXPECTED_WEEK_STARTS);+ expect(weeks.every((week) => week.totalSecrets === 0)).toBe(true);+ });++ test("throws when the actor cannot read secrets management insights", async () => {+ setCanReadInsights(false);+ try {+ await expect(insightsService.getStaticSecretsUsage(actor(ORG_ID))).rejects.toBeInstanceOf(ForbiddenError);+ } finally {+ setCanReadInsights(true);+ }+ });++ test("throws when the plan does not include secret access insights", async () => {+ setPlanHasInsights(false);+ try {+ await expect(insightsService.getStaticSecretsUsage(actor(ORG_ID))).rejects.toThrow(/Upgrade your plan/);+ } finally {+ setPlanHasInsights(true);+ }+ });+});diff --git a/backend/e2e-test/routes/v1/insights.spec.ts b/backend/e2e-test/routes/v1/insights.spec.tsindex 7fb4d65a6c..14bfd8cc80 100644--- a/backend/e2e-test/routes/v1/insights.spec.ts+++ b/backend/e2e-test/routes/v1/insights.spec.ts@@ -1,15 +1,24 @@import { describe, expect, test } from "vitest";-const URL = "/api/v1/insights/secrets/summary";+const PROJECTS_URL = "/api/v1/insights/secrets/projects";++// Every org-scoped insight, by the URL the router registers it at. The aggregation behind each one is+// covered against a real database (or a faked ClickHouse) in the e2e-test/insights-*.spec.ts files.+// This file only asserts the wiring, because the e2e instance runs unlicensed and the plan gate+// therefore rejects every request before any aggregate is computed.+const ORG_SCOPED_URLS = {+ summary: "/api/v1/insights/secrets/summary",+ projects: PROJECTS_URL,+ accessVolume: "/api/v1/insights/secrets/access-volume",+ authMethods: "/api/v1/insights/secrets/usage/auth-methods",+ staticSecrets: "/api/v1/insights/secrets/usage/static-secrets"+};-// The counting itself is covered against a real database in e2e-test/insights-usage.spec.ts.-// This file only asserts the route wiring, because the e2e instance runs unlicensed and the plan gate-// therefore rejects every request before any count is taken.describe("Insights V1 Router (org-scoped)", async () => {- test("GET secrets summary is registered and refuses on plan restriction", async () => {+ test.each(Object.entries(ORG_SCOPED_URLS))("GET %s is registered and refuses on plan restriction", async (_, url) => {const res = await testServer.inject({method: "GET",- url: URL,+ url,headers: { authorization: `Bearer ${jwtAuthToken}` }});@@ -19,27 +28,30 @@ describe("Insights V1 Router (org-scoped)", async () => {expect(res.json().message).toContain("Upgrade your plan");});- const PROJECTS_URL = "/api/v1/insights/secrets/projects";+ test.each(Object.entries(ORG_SCOPED_URLS))("GET %s requires authentication", async (_, url) => {+ const res = await testServer.inject({ method: "GET", url });- test("GET secrets projects is registered and refuses on plan restriction", async () => {+ expect(res.statusCode).toBe(401);+ });++ test("GET secrets projects rejects an out-of-bounds limit", async () => {const res = await testServer.inject({method: "GET",- url: PROJECTS_URL,+ url: `${PROJECTS_URL}?limit=500`,headers: { authorization: `Bearer ${jwtAuthToken}` }});- expect(res.statusCode).toBe(400);- expect(res.json().message).toContain("Upgrade your plan");+ // Schema validation failures map to 422 (ValidationError in error-handler.ts)+ expect(res.statusCode).toBe(422);});- test("GET secrets projects rejects an out-of-bounds limit", async () => {+ test("GET secrets projects rejects a negative offset", async () => {const res = await testServer.inject({method: "GET",- url: `${PROJECTS_URL}?limit=500`,+ url: `${PROJECTS_URL}?offset=-1`,headers: { authorization: `Bearer ${jwtAuthToken}` }});- // Schema validation failures map to 422 (ValidationError in error-handler.ts)expect(res.statusCode).toBe(422);});});diff --git a/backend/src/ee/routes/v1/insights-router.ts b/backend/src/ee/routes/v1/insights-router.tsindex 40016d3db2..b9b243a925 100644--- a/backend/src/ee/routes/v1/insights-router.ts+++ b/backend/src/ee/routes/v1/insights-router.ts@@ -55,10 +55,7 @@ const AuditReportSchema = z.object({});export const registerInsightsRouter = async (server: FastifyZodProvider) => {- // Org-scoped insights. These aggregate across every secret management project in the organization,- // so they carry no :projectId and are gated on the org-level Secrets Management Insights subject.- // Everything below this block is project-scoped, lives under /:projectId, and is gated on the- // project-level Insights subject instead.+ // Org scoped insights don't have a :projectId, so they are gated on the org-level Secrets Management Insights subject.server.route({method: "GET",@@ -121,7 +118,7 @@ export const registerInsightsRouter = async (server: FastifyZodProvider) => {}},handler: async (req) => {- const projectWarnings = await server.services.insights.getSecretsProjectWarnings({+ const projectWarnings = await server.services.insights.getSecretsProjects({actor: req.permission.type,actorId: req.permission.id,actorAuthMethod: req.permission.authMethod,diff --git a/backend/src/ee/services/insights/insights-service.ts b/backend/src/ee/services/insights/insights-service.tsindex e96104e962..77d71a8ec6 100644--- a/backend/src/ee/services/insights/insights-service.ts+++ b/backend/src/ee/services/insights/insights-service.ts@@ -603,7 +603,7 @@ export const insightsServiceFactory = ({return { activeLeases, users, identities };};- const getSecretsProjectWarnings = async (dto: TGetSecretsProjectWarningsDTO): Promise<TSecretsProjectWarnings> => {+ const getSecretsProjects = async (dto: TGetSecretsProjectWarningsDTO): Promise<TSecretsProjectWarnings> => {await assertOrgInsightsRead(dto);const cacheKey = KeyStorePrefixes.InsightsCache(dto.orgId, `project-warnings:${dto.offset}:${dto.limit}`);@@ -766,7 +766,7 @@ export const insightsServiceFactory = ({getSecretsDuplication,getCounts,getSecretsUsageInsights,- getSecretsProjectWarnings,+ getSecretsProjects,getStaticSecretsUsage};};