Skip to content

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

update tests for review

Andreyd7b2be1935c6
diff --git a/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts b/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
index eb42335b4f..0405aa44f3 100644
--- a/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
+++ b/backend/src/ee/services/pam-account-heartbeat/pam-account-heartbeat-service.test.ts
@@ -6,6 +6,7 @@ import {
ResourcePermissionSub
} from "@app/ee/services/permission/resource-permission";
import { crypto } from "@app/lib/crypto/cryptography";
+import { NotFoundError } from "@app/lib/errors";
import { ActorType, AuthMethod } from "@app/services/auth/auth-type";
import { PAM_ROTATION_FACTORY_MAP, TPamRotationHandler } from "../pam-account-rotation/pam-rotation-handlers";
@@ -58,6 +59,7 @@ const buildService = (
) => {
const updateById = vi.fn(async () => undefined);
const testCredential = opts.testCredential ?? vi.fn(async () => true);
+ const assertGatewayBindingInOrg = vi.fn<(input: unknown) => Promise<void>>(async () => undefined);
const handler = { validateTarget: vi.fn(), applyPasswordChange: vi.fn(), testCredential } as TPamRotationHandler;
const rotationHandlers = {
[PamAccountType.Postgres]: handler,
@@ -83,7 +85,7 @@ const buildService = (
gatewayV2Service: { getPlatformConnectionDetailsByGatewayId: vi.fn() },
gatewayPoolService: {
resolveEffectiveGatewayId: vi.fn(async () => "gw-1"),
- assertGatewayBindingInOrg: vi.fn(async () => undefined)
+ assertGatewayBindingInOrg
},
kmsService: { createCipherPairWithDataKey: vi.fn(async () => identityCipher) },
permissionService: { getProjectPermission: vi.fn(), getResourcePermission: vi.fn() },
@@ -94,7 +96,7 @@ const buildService = (
const service = pamAccountHeartbeatServiceFactory(
deps as unknown as Parameters<typeof pamAccountHeartbeatServiceFactory>[0]
);
- return { service, updateById, testCredential, permissionService: deps.permissionService };
+ return { service, updateById, testCredential, assertGatewayBindingInOrg, permissionService: deps.permissionService };
};
describe("heartbeat: Windows", () => {
@@ -277,3 +279,65 @@ describe("heartbeat: manual check permissions", () => {
expect(testCredential).not.toHaveBeenCalled();
});
});
+
+describe("heartbeat: gateway binding check", () => {
+ beforeAll(async () => {
+ await crypto.initialize({} as never, {} as never, {} as never);
+ });
+
+ test.each([
+ [
+ "the account's own gateway",
+ { gatewayId: "gw-own", templateGatewayId: null },
+ { gatewayId: "gw-own", gatewayPoolId: null }
+ ],
+ [
+ "the account's own pool",
+ { gatewayPoolId: "pool-own", templateGatewayId: null },
+ { gatewayId: null, gatewayPoolId: "pool-own" }
+ ],
+ [
+ "a binding inherited from the template",
+ { templateGatewayId: "gw-template", templateGatewayPoolId: "pool-template" },
+ { gatewayId: "gw-template", gatewayPoolId: "pool-template" }
+ ]
+ ])("checks %s against the project's org", async (_label, overrides, binding) => {
+ const { service, assertGatewayBindingInOrg } = buildService(buildWindowsAccount(overrides));
+
+ await service.checkScheduledAccount("acc-win");
+
+ expect(assertGatewayBindingInOrg).toHaveBeenCalledWith({ ...binding, orgId: "org-1" });
+ });
+
+ test.each([
+ ["Windows", buildWindowsAccount()],
+ [
+ "Postgres",
+ buildWindowsAccount({
+ id: "acc-pg",
+ accountType: PamAccountType.Postgres,
+ encryptedCredentials: blobOf({ username: "app", password: "pw" }),
+ encryptedConnectionDetails: blobOf({
+ host: "db.internal",
+ port: 5432,
+ database: "app",
+ sslEnabled: false,
+ sslRejectUnauthorized: false
+ })
+ })
+ ]
+ ])("a %s account whose binding fails the check is never probed", async (_label, account) => {
+ gatewayBuiltTestConnection.mockReset();
+ const { service, testCredential, assertGatewayBindingInOrg } = buildService(account);
+ assertGatewayBindingInOrg.mockRejectedValueOnce(new NotFoundError({ message: "Gateway with ID 'gw-1' not found" }));
+
+ const result = await service.checkScheduledAccount(account.id);
+
+ expect(result).toMatchObject({
+ status: PamHeartbeatStatus.CannotCheck,
+ message: "Gateway with ID 'gw-1' not found"
+ });
+ expect(testCredential).not.toHaveBeenCalled();
+ expect(gatewayBuiltTestConnection).not.toHaveBeenCalled();
+ });
+});
diff --git a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts
index 25e12e3b4d..b7a8c3f690 100644
--- a/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts
+++ b/backend/src/ee/services/pam-account-rotation/pam-account-rotation-service.test.ts
@@ -1,6 +1,7 @@
import { beforeAll, describe, expect, test, vi } from "vitest";
import { crypto } from "@app/lib/crypto/cryptography";
+import { NotFoundError } from "@app/lib/errors";
import { PamAccountType } from "../pam/pam-enums";
import { pamAccountRotationServiceFactory, ROTATION_STATUS } from "./pam-account-rotation-service";
@@ -36,10 +37,10 @@ const buildAccount = () => ({
encryptedConnectionDetails: blobOf(connectionDetails),
encryptedPendingCredentials: blobOf({ username: "app", password: PENDING_PASSWORD }),
templateSettings: { rotation: { enabled: true, intervalSeconds: 3600 as number | null } },
- templateGatewayId: null,
- templateGatewayPoolId: null,
- gatewayId: null,
- gatewayPoolId: null
+ templateGatewayId: null as string | null,
+ templateGatewayPoolId: null as string | null,
+ gatewayId: null as string | null,
+ gatewayPoolId: null as string | null
});
// Builds the service with a fake handler whose testCredential verdict is driven per-password by `passwordWorks`.
@@ -55,6 +56,7 @@ const buildService = (
const applyPasswordChange = vi.fn<(input: unknown) => Promise<void>>(async () => undefined);
const testCredential = vi.fn(async ({ auth }: { auth: { password: string } }) => passwordWorks(auth.password));
+ const assertGatewayBindingInOrg = vi.fn<(input: unknown) => Promise<void>>(async () => undefined);
const handler: TPamRotationHandler = { validateTarget: vi.fn(), applyPasswordChange, testCredential };
const rotationHandlers: typeof PAM_ROTATION_FACTORY_MAP = {
[PamAccountType.Postgres]: handler,
@@ -85,7 +87,7 @@ const buildService = (
kmsService: { createCipherPairWithDataKey: vi.fn(async () => identityCipher) },
keyStore: { acquireLock: vi.fn(async () => ({ release: vi.fn(async () => undefined) })) },
gatewayV2Service: { getPlatformConnectionDetailsByGatewayId: vi.fn() },
- gatewayPoolService: { resolveEffectiveGatewayId: vi.fn(), assertGatewayBindingInOrg: vi.fn(async () => undefined) },
+ gatewayPoolService: { resolveEffectiveGatewayId: vi.fn(), assertGatewayBindingInOrg },
projectDAL: { findById: vi.fn(async () => ({ id: "proj-1", orgId: "org-1" })) },
pamAccountDependencyDAL: { findByAccountId: vi.fn(async () => []), updateById: vi.fn(async () => undefined) },
rotationHandlers
@@ -94,7 +96,7 @@ const buildService = (
const service = pamAccountRotationServiceFactory(
deps as unknown as Parameters<typeof pamAccountRotationServiceFactory>[0]
);
- return { service, updateById, applyPasswordChange, testCredential };
+ return { service, updateById, applyPasswordChange, testCredential, assertGatewayBindingInOrg };
};
describe("rotateScheduledAccount recovery probe", () => {
@@ -492,3 +494,43 @@ describe("sqlRotationHandler.validateTarget", () => {
expect(() => validate({ accountType: PamAccountType.MsSQL, authMethod: "ntlm" })).toThrow(/SQL Server/);
});
});
+
+describe("rotation gateway binding check", () => {
+ beforeAll(async () => {
+ await crypto.initialize({} as never, {} as never, {} as never);
+ });
+
+ test.each([
+ ["the account's own gateway", { gatewayId: "gw-own" }, { gatewayId: "gw-own", gatewayPoolId: null }],
+ ["the account's own pool", { gatewayPoolId: "pool-own" }, { gatewayId: null, gatewayPoolId: "pool-own" }],
+ [
+ "a binding inherited from the template",
+ { templateGatewayId: "gw-template", templateGatewayPoolId: "pool-template" },
+ { gatewayId: "gw-template", gatewayPoolId: "pool-template" }
+ ]
+ ])("checks %s against the project's org", async (_label, overrides, binding) => {
+ const { service, assertGatewayBindingInOrg } = buildService((password) => password === PENDING_PASSWORD, {
+ account: { ...buildAccount(), ...overrides }
+ });
+
+ await service.rotateScheduledAccount("acc-1");
+
+ expect(assertGatewayBindingInOrg).toHaveBeenCalledWith({ ...binding, orgId: "org-1" });
+ });
+
+ test("a binding that fails the check stops the rotation before anything connects", async () => {
+ const { service, assertGatewayBindingInOrg, applyPasswordChange, testCredential } = buildService(() => true);
+ assertGatewayBindingInOrg.mockRejectedValueOnce(
+ new NotFoundError({ message: "Gateway pool with ID 'pool-own' not found" })
+ );
+
+ const result = await service.rotateScheduledAccount("acc-1");
+
+ expect(result).toMatchObject({
+ rotationStatus: ROTATION_STATUS.Failed,
+ message: "Rotation failed: Gateway pool with ID 'pool-own' not found"
+ });
+ expect(testCredential).not.toHaveBeenCalled();
+ expect(applyPasswordChange).not.toHaveBeenCalled();
+ });
+});