Skip to content

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

test(insights): search by value through the service in e2e

Calvinb9b303186913
diff --git a/backend/e2e-test/routes/v1/insights.spec.ts b/backend/e2e-test/routes/v1/insights.spec.ts
index 14bfd8cc80..44da570723 100644
--- a/backend/e2e-test/routes/v1/insights.spec.ts
+++ b/backend/e2e-test/routes/v1/insights.spec.ts
@@ -34,6 +34,31 @@ describe("Insights V1 Router (org-scoped)", async () => {
expect(res.statusCode).toBe(401);
});
+ // Value search sits behind the same plan gate. What the index finds is covered in
+ // routes/v3/secrets-management.spec.ts, through the service.
+ test("POST secrets search-by-value is registered and refuses on plan restriction", async () => {
+ const res = await testServer.inject({
+ method: "POST",
+ url: "/api/v1/insights/secrets/search-by-value",
+ headers: { authorization: `Bearer ${jwtAuthToken}` },
+ body: { secretValue: "any-value" }
+ });
+
+ expect(res.statusCode).toBe(400);
+ expect(res.json().message).toContain("Upgrade your plan");
+ });
+
+ test("POST secrets search-by-value rejects an empty value", async () => {
+ const res = await testServer.inject({
+ method: "POST",
+ url: "/api/v1/insights/secrets/search-by-value",
+ headers: { authorization: `Bearer ${jwtAuthToken}` },
+ body: { secretValue: "" }
+ });
+
+ expect(res.statusCode).toBe(422);
+ });
+
test("GET secrets projects rejects an out-of-bounds limit", async () => {
const res = await testServer.inject({
method: "GET",
diff --git a/backend/e2e-test/routes/v3/secrets-management.spec.ts b/backend/e2e-test/routes/v3/secrets-management.spec.ts
index 9f555a0326..cfff104517 100644
--- a/backend/e2e-test/routes/v3/secrets-management.spec.ts
+++ b/backend/e2e-test/routes/v3/secrets-management.spec.ts
@@ -1,9 +1,34 @@
+import { createMongoAbility } from "@casl/ability";
import { createFakeWebhookServer, TFakeWebhookServer } from "e2e-test/fakes/webhook-destination";
import { createIsolatedOrgAndProject, createProject } from "e2e-test/testUtils/fixtures";
import { createFolder } from "e2e-test/testUtils/folders";
+import { projectScopedInsightsDepStubs, usageInsightsDepStubs } from "e2e-test/testUtils/insights";
import { createSecretV2, deleteSecretV2, updateSecretV2 } from "e2e-test/testUtils/secrets";
import { createWebhook, waitForWebhookRun } from "e2e-test/testUtils/webhooks";
+import { isHsmActiveAndEnabled } from "@app/ee/services/hsm/hsm-fns";
+import { insightsServiceFactory, TInsightsServiceFactoryDep } from "@app/ee/services/insights/insights-service";
+import { TFeatureSet } from "@app/ee/services/license/license-types";
+import {
+ OrgPermissionSecretsManagementInsightsActions,
+ OrgPermissionSet,
+ OrgPermissionSubjects
+} from "@app/ee/services/permission/org-permission";
+import { getConfig, initEnvConfig } from "@app/lib/config/env";
+import { initLogger, logger } from "@app/lib/logger";
+import { ActorType, AuthMethod } from "@app/services/auth/auth-type";
+import { internalKmsDALFactory } from "@app/services/kms/internal-kms-dal";
+import { internalKmsKeyVersionDALFactory } from "@app/services/kms/internal-kms-key-version-dal";
+import { kmsKekHistoryDALFactory } from "@app/services/kms/kms-kek-history-dal";
+import { kmskeyDALFactory } from "@app/services/kms/kms-key-dal";
+import { kmsLegacyEncryptionKeyDALFactory } from "@app/services/kms/kms-legacy-encryption-key-dal";
+import { kmsRootConfigDALFactory } from "@app/services/kms/kms-root-config-dal";
+import { kmsServiceFactory } from "@app/services/kms/kms-service";
+import { orgDALFactory } from "@app/services/org/org-dal";
+import { projectDALFactory } from "@app/services/project/project-dal";
+import { secretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
+import { secretV2BridgeDALFactory } from "@app/services/secret-v2-bridge/secret-v2-bridge-dal";
+
const ENV = "dev";
const PROD_ENV = "prod";
@@ -15,19 +40,70 @@ type TFoundSecret = {
secretPath: string;
};
-const searchByValue = async (
- secretValue: string,
- authToken: string,
- scope?: { scope: "project"; projectId: string }
-) => {
- const res = await testServer.inject({
- method: "POST",
- url: "/api/v4/secrets/search-by-value",
- headers: { authorization: `Bearer ${authToken}` },
- body: { secretValue, ...scope }
+// The e2e instance runs unlicensed, and value search sits behind the insights plan, so the route
+// refuses every request here (insights.spec.ts asserts that). These tests are about what the index
+// written by the secret routes finds, so they search through the service on the same database and
+// KMS, with the plan and permission let through.
+const buildValueSearch = async () => {
+ // Spec files run in a separate module graph from the test server, so the env config and the
+ // cryptography module must be initialized here as well (same process env, same result).
+ initLogger();
+ await initEnvConfig(testHsmService, testKmsRootConfigDAL, testSuperAdminDAL, logger);
+
+ const kmsRootConfigDAL = kmsRootConfigDALFactory(testDb);
+ const kmsService = kmsServiceFactory({
+ kmsRootConfigDAL,
+ kmsLegacyEncryptionKeyDAL: kmsLegacyEncryptionKeyDALFactory(testDb),
+ kmsKekHistoryDAL: kmsKekHistoryDALFactory(testDb),
+ kmsDAL: kmskeyDALFactory(testDb),
+ internalKmsDAL: internalKmsDALFactory(testDb),
+ internalKmsKeyVersionDAL: internalKmsKeyVersionDALFactory(testDb),
+ orgDAL: orgDALFactory(testDb),
+ projectDAL: projectDALFactory(testDb),
+ hsmService: testHsmService,
+ // startService never touches the key store; only the factory's dependency type requires it.
+ keyStore: {
+ getItem: async () => null,
+ setItemWithExpiry: async () => "OK" as const,
+ deleteItem: async () => 0
+ },
+ envConfig: getConfig()
});
- expect(res.statusCode).toBe(200);
- return res.json().secrets as TFoundSecret[];
+ await kmsService.startService(await isHsmActiveAndEnabled({ hsmService: testHsmService, kmsRootConfigDAL }));
+
+ const insightsService = insightsServiceFactory({
+ ...projectScopedInsightsDepStubs,
+ ...usageInsightsDepStubs,
+ orgDAL: orgDALFactory(testDb),
+ secretV2BridgeDAL: secretV2BridgeDALFactory({ db: testDb, keyStore: testKeyStore }),
+ folderDAL: secretFolderDALFactory(testDb),
+ kmsService,
+ permissionService: {
+ getOrgPermission: async () => ({
+ permission: createMongoAbility<OrgPermissionSet>([
+ {
+ action: OrgPermissionSecretsManagementInsightsActions.SearchAllSecretValues,
+ subject: OrgPermissionSubjects.SecretsManagementInsights
+ }
+ ]),
+ memberships: [],
+ hasRole: () => false
+ })
+ } as unknown as TInsightsServiceFactoryDep["permissionService"],
+ licenseService: {
+ getPlan: async () => ({ secretAccessInsights: true }) as unknown as TFeatureSet
+ } as TInsightsServiceFactoryDep["licenseService"]
+ } as unknown as TInsightsServiceFactoryDep);
+
+ return async (secretValue: string, orgId: string): Promise<TFoundSecret[]> =>
+ insightsService.searchOrgSecretsByValue({
+ secretValue,
+ orgId,
+ actor: ActorType.USER,
+ actorId: "value-search-e2e",
+ actorAuthMethod: AuthMethod.EMAIL,
+ actorOrgId: orgId
+ });
};
const at = (secrets: TFoundSecret[]) => secrets.map((s) => `${s.environment.slug}${s.secretPath}${s.key}`).sort();
@@ -101,6 +177,12 @@ describe("Secrets management", () => {
});
describe("Finding a secret by its value", () => {
+ let searchByValue: Awaited<ReturnType<typeof buildValueSearch>>;
+
+ beforeAll(async () => {
+ searchByValue = await buildValueSearch();
+ });
+
test("a rotated value stops being found and the new one starts", async () => {
await createSecretV2({
workspaceId: projectId,
@@ -110,7 +192,7 @@ describe("Secrets management", () => {
value: "the-old-value",
authToken
});
- expect(await searchByValue("the-old-value", authToken)).toHaveLength(1);
+ expect(await searchByValue("the-old-value", orgId)).toHaveLength(1);
await updateSecretV2({
workspaceId: projectId,
@@ -121,8 +203,8 @@ describe("Secrets management", () => {
authToken
});
- expect(await searchByValue("the-old-value", authToken)).toEqual([]);
- expect(await searchByValue("the-new-value", authToken)).toHaveLength(1);
+ expect(await searchByValue("the-old-value", orgId)).toEqual([]);
+ expect(await searchByValue("the-new-value", orgId)).toHaveLength(1);
});
// A rename carries no value, so the secret must still be findable by the value it still holds.
@@ -145,7 +227,7 @@ describe("Secrets management", () => {
authToken
});
- const found = await searchByValue("survives-a-rename", authToken);
+ const found = await searchByValue("survives-a-rename", orgId);
expect(found).toHaveLength(1);
expect(found[0].key).toBe("AFTER_RENAME");
});
@@ -159,7 +241,7 @@ describe("Secrets management", () => {
value: "here-then-gone",
authToken
});
- expect(await searchByValue("here-then-gone", authToken)).toHaveLength(1);
+ expect(await searchByValue("here-then-gone", orgId)).toHaveLength(1);
await deleteSecretV2({
workspaceId: projectId,
@@ -169,13 +251,13 @@ describe("Secrets management", () => {
authToken
});
- expect(await searchByValue("here-then-gone", authToken)).toEqual([]);
+ expect(await searchByValue("here-then-gone", orgId)).toEqual([]);
});
// This is the test the org-scoped index exists for. The project-scoped digest cannot match across
// projects, so a search that found only the first project would still be broken for the case the
// feature was built for: a value is believed compromised, so where else is it?
- test("a value held across projects, environments and folders is found in each, and project scope narrows it", async () => {
+ test("a value held across projects, environments and folders is found in each", async () => {
const shared = `across-projects-${Date.now()}`;
const secondProjectId = await createProject(authToken, "secrets-management-e2e-second");
@@ -191,7 +273,7 @@ describe("Secrets management", () => {
await createSecretV2({ ...copy, value: shared, authToken });
}
- const found = await searchByValue(shared, authToken);
+ const found = await searchByValue(shared, orgId);
expect(at(found)).toEqual([
`${ENV}/IN_FIRST_ROOT`,
`${ENV}/IN_SECOND_PROJECT`,
@@ -199,9 +281,6 @@ describe("Secrets management", () => {
`${PROD_ENV}/IN_FIRST_PROD`
]);
expect(new Set(found.map((f) => f.projectId))).toEqual(new Set([projectId, secondProjectId]));
-
- const inSecondOnly = await searchByValue(shared, authToken, { scope: "project", projectId: secondProjectId });
- expect(inSecondOnly.map((f) => f.key)).toEqual(["IN_SECOND_PROJECT"]);
});
// The org digest is keyed by the org data key, so one org's values must never match another's.
@@ -218,8 +297,8 @@ describe("Secrets management", () => {
authToken: other.authToken
});
- expect(await searchByValue(value, other.authToken)).toHaveLength(1);
- expect(await searchByValue(value, authToken)).toEqual([]);
+ expect(await searchByValue(value, other.orgId)).toHaveLength(1);
+ expect(await searchByValue(value, orgId)).toEqual([]);
expect(other.orgId).not.toBe(orgId);
} finally {
await other.cleanup();