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
import knex, { Knex } from "knex";
import { TDbClient } from "@app/db";
import { CertificateSource } from "@app/ee/services/pki-discovery/pki-discovery-types";
import { certManagerCertificateAlertDALFactory } from "./cert-manager-certificate-alert-dal";
const PROJECT_ID = "3f2d7a4e-1b6c-4d8e-9a0f-5c7b2e1d4a6b";
const APPLICATION_ID = "7b0a6b54-3c1e-4f3a-9d5e-2f1b8c4d6e90";
const buildDAL = (firstQueryRows: unknown[] = []) => {
const builder = knex({ client: "pg" });
const queries: { sql: string; bindings: readonly unknown[] }[] = [];
const reader = ((table: string) => {
const query = builder(table);
return Object.assign(query, {
then: (resolve: (rows: unknown[]) => void) => {
const { sql, bindings } = query.toSQL();
queries.push({ sql, bindings });
resolve(queries.length === 1 ? firstQueryRows : []);
}
});
}) as unknown as Knex;
const db = Object.assign(reader, { replicaNode: () => reader }) as unknown as TDbClient;
return { dal: certManagerCertificateAlertDALFactory(db), queries };
};
describe("cert manager certificate alert DAL", () => {
test("findExpiringCertificates only scans live certificates of the alert's application inside the window", async () => {
const { dal, queries } = buildDAL();
const asOf = new Date("2026-01-01T00:00:00Z");
await dal.findExpiringCertificates({
projectId: PROJECT_ID,
applicationId: APPLICATION_ID,
alertBeforeInterval: "30 days",
leadInterval: "1 hour",
asOf,
alreadyAlerted: { alertId: "alert-1", channelIds: ["channel-1"], since: asOf }
});
const [{ sql, bindings }] = queries;
expect(sql).toContain('"certificates"."projectId" = ?');
expect(sql).toContain('"certificates"."applicationId" = ?');
expect(sql).toContain('not "certificates"."status" = ?');
expect(sql).toContain('"certificates"."renewedByCertificateId" is null');
expect(sql).toContain('"certificates"."notAfter" > ?::timestamptz');
expect(sql).toContain('"certificates"."notAfter" <= ?::timestamptz + ?::interval + ?::interval');
expect(sql).toContain('order by "certificates"."notAfter" asc');
expect(bindings).toEqual(
expect.arrayContaining([PROJECT_ID, APPLICATION_ID, "revoked", asOf, "30 days", "1 hour"])
);
});
test("findExpiringCertificates skips certificates already delivered on every channel before the cap", async () => {
const { dal, queries } = buildDAL();
const since = new Date("2026-01-01T00:00:00Z");
await dal.findExpiringCertificates({
projectId: PROJECT_ID,
applicationId: APPLICATION_ID,
alertBeforeInterval: "30 days",
leadInterval: "1 day",
asOf: new Date("2026-01-02T00:00:00Z"),
alreadyAlerted: { alertId: "alert-1", channelIds: ["channel-1", "channel-2"], since }
});
expect(queries).toHaveLength(2);
const [notFullyNotified, leastRecentlyNotified] = queries;
expect(notFullyNotified.sql).toContain('"certificates".id::text not in ((select "deliveredTgt"."targetId"');
expect(notFullyNotified.sql).toContain('having count(distinct "deliveredTgt"."channelId") >= ?');
expect(notFullyNotified.sql).not.toContain('"deliveredTgt"."targetId" = "certificates".id::text');
expect(notFullyNotified.sql).toMatch(/order by "certificates"\."notAfter" asc limit \?$/);
expect(notFullyNotified.bindings).toEqual(
expect.arrayContaining(["alert-1", "success", "channel-1", "channel-2", 2, 1000])
);
expect(leastRecentlyNotified.sql).toContain('count(distinct "tgt"."channelId")');
expect(leastRecentlyNotified.sql).toContain('"hist"."triggeredAt" >= ?');
expect(leastRecentlyNotified.bindings).toEqual(expect.arrayContaining([since]));
expect(leastRecentlyNotified.sql).toContain('max("deliveredHist"."triggeredAt") as "lastDeliveredAt"');
expect(leastRecentlyNotified.sql).toContain('group by "deliveredTgt"."targetId"');
expect(leastRecentlyNotified.sql).toContain(
'order by "lastDelivered"."lastDeliveredAt" asc, "certificates"."notAfter" asc limit ?'
);
expect(leastRecentlyNotified.sql).toContain('having count(distinct "deliveredTgt"."channelId") >= ?');
});
test("findExpiringCertificates stops after the never-notified query when it fills the cap", async () => {
const { dal, queries } = buildDAL(Array.from({ length: 1000 }, (_, index) => ({ id: `cert-${index}` })));
const certificates = await dal.findExpiringCertificates({
projectId: PROJECT_ID,
alertBeforeInterval: "30 days",
leadInterval: "1 day",
asOf: new Date("2026-01-02T00:00:00Z"),
alreadyAlerted: { alertId: "alert-1", channelIds: ["channel-1"], since: new Date("2026-01-01T00:00:00Z") }
});
expect(certificates).toHaveLength(1000);
expect(queries).toHaveLength(1);
});
test("findCertificatesByIds keeps event targets inside the alert's project and application", async () => {
const { dal, queries } = buildDAL();
await dal.findCertificatesByIds({
projectId: PROJECT_ID,
applicationId: APPLICATION_ID,
certificateIds: ["cert-1", "cert-2"]
});
const [{ sql, bindings }] = queries;
expect(sql).toContain('"certificates"."projectId" = ?');
expect(sql).toContain('"certificates"."applicationId" = ?');
expect(sql).toContain('"certificates"."id" in (?, ?)');
expect(bindings).toEqual(expect.arrayContaining([PROJECT_ID, APPLICATION_ID, "cert-1", "cert-2"]));
});
test("findApplicationById skips applications in a project pending deletion", async () => {
const { dal, queries } = buildDAL();
await dal.findApplicationById(APPLICATION_ID);
expect(queries[0].sql).toContain('"projects"."deleteAfter" is null');
});
test("findApplicationNamesByIds scopes the lookup to the caller's org", async () => {
const { dal, queries } = buildDAL();
await dal.findApplicationNamesByIds([APPLICATION_ID], "org-1");
expect(queries[0].sql).toContain('"projects"."orgId" = ?');
expect(queries[0].bindings).toEqual(expect.arrayContaining([APPLICATION_ID, "org-1"]));
});
test("a project-wide scan filters by the application and profile lists instead of pinning one application", async () => {
const { dal, queries } = buildDAL();
await dal.findExpiringCertificates({
projectId: PROJECT_ID,
applicationIds: ["app-1", "app-2"],
profileIds: ["prof-1"],
alertBeforeInterval: "30 days",
leadInterval: "1 day",
asOf: new Date("2026-01-01T00:00:00Z"),
alreadyAlerted: { alertId: "alert-1", channelIds: ["channel-1"], since: new Date("2026-01-01T00:00:00Z") }
});
const [{ sql, bindings }] = queries;
expect(sql).toContain('"certificates"."projectId" = ?');
expect(sql).not.toContain('"certificates"."applicationId" = ?');
expect(sql).toContain('"certificates"."applicationId" in (?, ?)');
expect(sql).toContain('"certificates"."profileId" in (?)');
expect(bindings).toEqual(expect.arrayContaining(["app-1", "app-2", "prof-1"]));
});
test("findCertificatesByIds narrows a project-wide lookup by the profile list", async () => {
const { dal, queries } = buildDAL();
await dal.findCertificatesByIds({ projectId: PROJECT_ID, profileIds: ["prof-1"], certificateIds: ["cert-1"] });
expect(queries[0].sql).toContain('"certificates"."profileId" in (?)');
expect(queries[0].sql).toContain('"certificates"."id" in (?)');
});
test("the source filter matches the listed sources, and issued also matches certificates with no source", async () => {
const { dal, queries } = buildDAL();
await dal.findCertificatesByIds({
projectId: PROJECT_ID,
sources: [CertificateSource.Imported],
certificateIds: ["c"]
});
await dal.findCertificatesByIds({
projectId: PROJECT_ID,
sources: [CertificateSource.Issued, CertificateSource.Discovered],
certificateIds: ["c"]
});
expect(queries[0].sql).toContain('("certificates"."source" in (?))');
expect(queries[0].sql).not.toContain('"certificates"."source" is null');
expect(queries[0].bindings).toEqual(expect.arrayContaining(["imported"]));
expect(queries[1].sql).toContain('("certificates"."source" in (?, ?) or "certificates"."source" is null)');
});
test("application and profile id checks are scoped to the project", async () => {
const { dal, queries } = buildDAL();
await dal.findProjectApplicationIds(PROJECT_ID, ["app-1"]);
await dal.findProjectProfileIds(PROJECT_ID, ["prof-1"]);
expect(queries[0].sql).toContain('from "pki_applications" where "projectId" = ?');
expect(queries[1].sql).toContain('from "pki_certificate_profiles" where "projectId" = ?');
expect(queries.map((query) => query.bindings)).toEqual([
expect.arrayContaining([PROJECT_ID, "app-1"]),
expect.arrayContaining([PROJECT_ID, "prof-1"])
]);
});
});