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 { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { TFeatureSet } from "@app/ee/services/license/license-types";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { isInstanceWidePkiQuota } from "@app/services/certificate-common/certificate-quota-scope";
import { TProjectDALFactory } from "@app/services/project/project-dal";
import { TCertificateAuthorityDALFactory } from "./certificate-authority-dal";
export type TCertificateAuthorityQuotaDeps = {
projectDAL: Pick<TProjectDALFactory, "findById">;
licenseService: Pick<TLicenseServiceFactory, "getPlan" | "getInstanceType">;
certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "countCasByOrgId" | "countInternalCasByOrgId">;
};
// A contract granting more internal CAs than total CAs is a License Server misconfiguration; honour
// the larger grant. Guarded on both being numbers because the free tier leaves maxInternalCas null
// while capping maxCas at 1, so treating null as unlimited would lift that cap.
//
// Exported so the quota endpoint reports the same ceiling this module enforces.
export const resolveEffectiveMaxCas = (plan: Pick<TFeatureSet, "maxCas" | "maxInternalCas">) =>
typeof plan.maxCas === "number" && typeof plan.maxInternalCas === "number"
? Math.max(plan.maxCas, plan.maxInternalCas)
: plan.maxCas;
// Enforced where the CA row is written rather than in createCertificateAuthority alone: the still-live
// POST /cert-manager/ca route calls internalCertificateAuthorityService.createCa directly, so a check
// that only sat in the former was bypassable.
//
// Two independent caps, whichever binds first: maxCas covers every type, maxInternalCas covers
// INTERNAL only. A tier sets one and leaves the other null. Both count the root org's tree, or the
// whole instance under a self-hosted license (isInstanceWidePkiQuota).
export const assertCertificateAuthorityQuota = async ({
projectId,
isInternal,
deps
}: {
projectId: string;
isInternal: boolean;
deps: TCertificateAuthorityQuotaDeps;
}): Promise<void> => {
const project = await deps.projectDAL.findById(projectId);
if (!project) throw new NotFoundError({ message: `Project with ID '${projectId}' not found` });
const plan = await deps.licenseService.getPlan(project.orgId);
const maxCas = resolveEffectiveMaxCas(plan);
const isInstanceWide = isInstanceWidePkiQuota(deps.licenseService);
const countOrgId = isInstanceWide ? null : project.orgId;
const scopeNote = isInstanceWide ? " across this instance" : "";
if (typeof maxCas === "number") {
const used = await deps.certificateAuthorityDAL.countCasByOrgId(countOrgId);
if (used >= maxCas) {
throw new BadRequestError({
message: `Failed to create certificate authority due to plan limit reached (${used} of ${maxCas} certificate authorities${scopeNote}). Upgrade plan to add more certificate authorities.`
});
}
}
if (isInternal && typeof plan.maxInternalCas === "number") {
const used = await deps.certificateAuthorityDAL.countInternalCasByOrgId(countOrgId);
if (used >= plan.maxInternalCas) {
throw new BadRequestError({
message: `Failed to create internal certificate authority due to plan limit reached (${used} of ${plan.maxInternalCas} internal certificate authorities${scopeNote}). Upgrade plan to add more internal certificate authorities.`
});
}
}
};