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
test(e2e): fake the license so specs can turn plan features on
Calvin18c029a4bb32
diff --git a/backend/e2e-test/fakes/license-fns.ts b/backend/e2e-test/fakes/license-fns.tsnew file mode 100644index 0000000000..48b8921a3f--- /dev/null+++ b/backend/e2e-test/fakes/license-fns.ts@@ -0,0 +1,39 @@+import { TFeatureSet } from "@app/ee/services/license/license-types";++import { getDefaultOnPremFeatures as getMockDefaults } from "../../src/ee/services/license/__mocks__/license-fns";++// The e2e instance runs on-prem with no license key, so getPlan answers with the feature set the+// license service took from getDefaultOnPremFeatures when it booted, by reference. Handing out one+// shared object means a spec can change the plan the running server sees without a restart.+// Wired up by test.alias in vitest.e2e.config.mts. Nothing under src/ references this file.+//+// The relative path to the unit-test mock is deliberate: the alias keys on the specifier+// "./license-fns", so this path is not itself aliased and does not resolve back to this file.+export { getLicenseKeyConfig } from "../../src/ee/services/license/__mocks__/license-fns";++// Shared through globalThis for the same reason as the AWS fakes: the alias makes this module+// reachable by more than one specifier, so it can be instantiated twice, and a spec would then+// configure a copy the server never reads.+const globalScope = globalThis as typeof globalThis & {+ infisicalFakeLicenseFeatures?: TFeatureSet;+};++globalScope.infisicalFakeLicenseFeatures ??= getMockDefaults() as TFeatureSet;++const features = globalScope.infisicalFakeLicenseFeatures;++export const getDefaultOnPremFeatures = () => features;++export const fakeLicense = {+ // Mutates in place rather than replacing the object, since the license service holds a reference+ // to it from boot.+ reset: () => {+ Object.keys(features).forEach((key) => delete (features as Record<string, unknown>)[key]);+ Object.assign(features, getMockDefaults());+ },++ // Turns plan features on (or off) for the whole instance until the next reset.+ setFeatures: (overrides: Partial<TFeatureSet>) => {+ Object.assign(features, overrides);+ }+};diff --git a/backend/e2e-test/routes/v1/insights.spec.ts b/backend/e2e-test/routes/v1/insights.spec.tsindex 44da570723..f9a7ac27a2 100644--- a/backend/e2e-test/routes/v1/insights.spec.ts+++ b/backend/e2e-test/routes/v1/insights.spec.ts@@ -35,7 +35,7 @@ describe("Insights V1 Router (org-scoped)", async () => {});// Value search sits behind the same plan gate. What the index finds is covered in- // routes/v3/secrets-management.spec.ts, through the service.+ // routes/v3/secrets-management.spec.ts, which turns the plan on with fakeLicense.test("POST secrets search-by-value is registered and refuses on plan restriction", async () => {const res = await testServer.inject({method: "POST",diff --git a/backend/e2e-test/routes/v3/secrets-management.spec.ts b/backend/e2e-test/routes/v3/secrets-management.spec.tsindex cfff104517..b63b9f5fe5 100644--- a/backend/e2e-test/routes/v3/secrets-management.spec.ts+++ b/backend/e2e-test/routes/v3/secrets-management.spec.ts@@ -1,34 +1,10 @@-import { createMongoAbility } from "@casl/ability";+import { fakeLicense } from "e2e-test/fakes/license-fns";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";@@ -40,70 +16,15 @@ type TFoundSecret = {secretPath: string;};-// 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()+const searchByValue = async (secretValue: string, authToken: string) => {+ const res = await testServer.inject({+ method: "POST",+ url: "/api/v1/insights/secrets/search-by-value",+ headers: { authorization: `Bearer ${authToken}` },+ body: { secretValue }});- 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- });+ expect(res.statusCode).toBe(200);+ return res.json().secrets as TFoundSecret[];};const at = (secrets: TFoundSecret[]) => secrets.map((s) => `${s.environment.slug}${s.secretPath}${s.key}`).sort();@@ -177,10 +98,13 @@ describe("Secrets management", () => {});describe("Finding a secret by its value", () => {- let searchByValue: Awaited<ReturnType<typeof buildValueSearch>>;+ // Value search sits behind the insights plan, and the e2e instance runs unlicensed.+ beforeAll(() => {+ fakeLicense.setFeatures({ secretAccessInsights: true });+ });- beforeAll(async () => {- searchByValue = await buildValueSearch();+ afterAll(() => {+ fakeLicense.reset();});test("a rotated value stops being found and the new one starts", async () => {@@ -192,7 +116,7 @@ describe("Secrets management", () => {value: "the-old-value",authToken});- expect(await searchByValue("the-old-value", orgId)).toHaveLength(1);+ expect(await searchByValue("the-old-value", authToken)).toHaveLength(1);await updateSecretV2({workspaceId: projectId,@@ -203,8 +127,8 @@ describe("Secrets management", () => {authToken});- expect(await searchByValue("the-old-value", orgId)).toEqual([]);- expect(await searchByValue("the-new-value", orgId)).toHaveLength(1);+ expect(await searchByValue("the-old-value", authToken)).toEqual([]);+ expect(await searchByValue("the-new-value", authToken)).toHaveLength(1);});// A rename carries no value, so the secret must still be findable by the value it still holds.@@ -227,7 +151,7 @@ describe("Secrets management", () => {authToken});- const found = await searchByValue("survives-a-rename", orgId);+ const found = await searchByValue("survives-a-rename", authToken);expect(found).toHaveLength(1);expect(found[0].key).toBe("AFTER_RENAME");});@@ -241,7 +165,7 @@ describe("Secrets management", () => {value: "here-then-gone",authToken});- expect(await searchByValue("here-then-gone", orgId)).toHaveLength(1);+ expect(await searchByValue("here-then-gone", authToken)).toHaveLength(1);await deleteSecretV2({workspaceId: projectId,@@ -251,7 +175,7 @@ describe("Secrets management", () => {authToken});- expect(await searchByValue("here-then-gone", orgId)).toEqual([]);+ expect(await searchByValue("here-then-gone", authToken)).toEqual([]);});// This is the test the org-scoped index exists for. The project-scoped digest cannot match across@@ -273,7 +197,7 @@ describe("Secrets management", () => {await createSecretV2({ ...copy, value: shared, authToken });}- const found = await searchByValue(shared, orgId);+ const found = await searchByValue(shared, authToken);expect(at(found)).toEqual([`${ENV}/IN_FIRST_ROOT`,`${ENV}/IN_SECOND_PROJECT`,@@ -297,8 +221,8 @@ describe("Secrets management", () => {authToken: other.authToken});- expect(await searchByValue(value, other.orgId)).toHaveLength(1);- expect(await searchByValue(value, orgId)).toEqual([]);+ expect(await searchByValue(value, other.authToken)).toHaveLength(1);+ expect(await searchByValue(value, authToken)).toEqual([]);expect(other.orgId).not.toBe(orgId);} finally {await other.cleanup();diff --git a/backend/e2e-test/setup/reset-fakes.ts b/backend/e2e-test/setup/reset-fakes.tsindex d05c5951f2..e46c3abb20 100644--- a/backend/e2e-test/setup/reset-fakes.ts+++ b/backend/e2e-test/setup/reset-fakes.ts@@ -1,6 +1,7 @@import { fakeAwsConnection } from "e2e-test/fakes/aws-connection-fns";import { fakeIamUserSecret } from "e2e-test/fakes/aws-iam-user-secret-rotation-fns";import { fakeParameterStore } from "e2e-test/fakes/aws-parameter-store-sync-fns";+import { fakeLicense } from "e2e-test/fakes/license-fns";import { fakeS3Bucket } from "e2e-test/fakes/s3";// The fakes are module singletons shared with the server booted in this same process, and the@@ -18,4 +19,5 @@ beforeAll(() => {fakeAwsConnection.reset();fakeS3Bucket.reset();fakeIamUserSecret.reset();+ fakeLicense.reset();});diff --git a/backend/vitest.e2e.config.mts b/backend/vitest.e2e.config.mtsindex aaa3ad1a9b..616137d226 100644--- a/backend/vitest.e2e.config.mts+++ b/backend/vitest.e2e.config.mts@@ -52,7 +52,8 @@ export default defineConfig({// AWS Parameter Store, the AWS IAM user secret rotation provider and the AWS app connection// are replaced by fakes for the whole e2e run, so the secret sync and secret rotation specs// can assert what Infisical hands a destination or a rotation provider without reaching AWS.- // Nothing under src/ knows the fakes exist (see e2e-test/fakes/).+ // Nothing under src/ knows the fakes exist (see e2e-test/fakes/). The license functions are+ // replaced the same way, so a spec can turn plan features on with fakeLicense.setFeatures.//// Entries match the *import specifier*, which is what keeps this surgical rather than// sweeping: "./aws-parameter-store-sync-fns" is imported only by its own barrel,@@ -67,7 +68,7 @@ export default defineConfig({alias: [{find: "./license-fns",- replacement: path.resolve(__dirname, "./src/ee/services/license/__mocks__/license-fns")+ replacement: path.resolve(__dirname, "./e2e-test/fakes/license-fns")},{find: "./aws-parameter-store-sync-fns",