Skip to content

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

7.6 KB
Raw Blame
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { TUsageCounterDALFactory } from "@app/services/license-client/usage/usage-counter-dal";
import { TProjectDALFactory } from "@app/services/project/project-dal";

import { buildCertificateQuotaKey, certificateHasWildcard } from "./certificate-quota-key";
import { isInstanceWidePkiQuota, PKI_INSTANCE_QUOTA_SCOPE } from "./certificate-quota-scope";

type TQuotaCounts = { total: number; wildcard: number };

export type TCertificateQuotaDeps = {
  projectDAL: Pick<TProjectDALFactory, "findById">;
  licenseService: Pick<TLicenseServiceFactory, "getPlan" | "getInstanceType">;
  usageCounterDAL: Pick<
    TUsageCounterDALFactory,
    "countActiveCertificateQuotaKeysByOrg" | "isCertificateQuotaKeyActiveInOrg" | "resolveRootOrgId"
  >;
  keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "deleteItem">;
};

// The cache only saves a query, so a keystore failure degrades to the exact count rather than failing
// issuance. That keeps issuance dependent on Postgres alone, which it already needs to write the row.
// orgId is the cache scope: a root org id, or PKI_INSTANCE_QUOTA_SCOPE for an instance-wide count.
const $getQuotaCounts = async (orgId: string, deps: TCertificateQuotaDeps): Promise<TQuotaCounts> => {
  const totalKey = KeyStorePrefixes.PkiCertificateQuotaCount(orgId);
  const wildcardKey = KeyStorePrefixes.PkiWildcardCertificateQuotaCount(orgId);

  try {
    const [cachedTotal, cachedWildcard] = await Promise.all([
      deps.keyStore.getItem(totalKey),
      deps.keyStore.getItem(wildcardKey)
    ]);
    if (cachedTotal !== null && cachedWildcard !== null) {
      const total = Number(cachedTotal);
      const wildcard = Number(cachedWildcard);
      if (Number.isFinite(total) && Number.isFinite(wildcard)) return { total, wildcard };
    }
  } catch (error) {
    logger.error(error, `Failed to read certificate quota cache [orgId=${orgId}]`);
  }

  const counts = await deps.usageCounterDAL.countActiveCertificateQuotaKeysByOrg(
    orgId === PKI_INSTANCE_QUOTA_SCOPE ? undefined : orgId
  );

  try {
    const ttl = KeyStoreTtls.PkiCertificateQuotaCountInSeconds;
    await Promise.all([
      deps.keyStore.setItemWithExpiry(totalKey, ttl, String(counts.total)),
      deps.keyStore.setItemWithExpiry(wildcardKey, ttl, String(counts.wildcard))
    ]);
  } catch (error) {
    logger.error(error, `Failed to write certificate quota cache [orgId=${orgId}]`);
  }

  return counts;
};

// Call once a certificate with a new quota key has actually been written. Drops the cached counts
// rather than incrementing them: incrementByWithExpiry re-sets the TTL on every call, so a steadily
// issuing org would roll the expiry forward indefinitely and never see a revocation or an expiry.
// Never throws, since a stale cache is cheaper than failing a certificate that was already granted.
export const recordNewCertificateQuotaKey = async (
  orgId: string,
  deps: Pick<TCertificateQuotaDeps, "keyStore">,
  isWildcard = false
): Promise<void> => {
  try {
    await deps.keyStore.deleteItem(KeyStorePrefixes.PkiCertificateQuotaCount(orgId));
    if (isWildcard) await deps.keyStore.deleteItem(KeyStorePrefixes.PkiWildcardCertificateQuotaCount(orgId));
  } catch (error) {
    logger.error(error, `Failed to reset certificate quota cache [orgId=${orgId}]`);
  }
};

// Nothing locks between this check and the insert, so the caps are commercial limits with bounded
// overshoot rather than hard boundaries.
export const assertCertificateQuota = async ({
  orgId,
  commonName,
  altNames,
  deps,
  isApprovedRequest = false
}: {
  orgId: string;
  commonName?: string | null;
  altNames?: string | null;
  deps: TCertificateQuotaDeps;
  isApprovedRequest?: boolean;
  // quotaOrgId is the scope the cache is keyed on: the tree root, or PKI_INSTANCE_QUOTA_SCOPE when the
  // caps count the whole instance. Callers must pass it back to recordNewCertificateQuotaKey rather
  // than their own orgId.
}): Promise<{ quotaKey: string; isNewQuotaKey: boolean; quotaOrgId: string; isWildcard: boolean }> => {
  const quotaKey = buildCertificateQuotaKey({ commonName, altNames });
  const isWildcard = certificateHasWildcard({ commonName, altNames });
  const plan = await deps.licenseService.getPlan(orgId);
  const unlimited = { quotaKey, isNewQuotaKey: false, quotaOrgId: orgId, isWildcard };

  const { maxCertificates, maxWildcardCertificates } = plan;
  const hasWildcardCap = isWildcard && typeof maxWildcardCertificates === "number";
  if (typeof maxCertificates !== "number" && !hasWildcardCap) return unlimited;

  // Correctness, not an optimization: reissuing names already held in the counted scope cannot raise
  // either count, and this is what lets an org sitting at its cap keep renewing. Remove it and its
  // certs expire.
  const isInstanceWide = isInstanceWidePkiQuota(deps.licenseService);
  const isAlreadyActive = await deps.usageCounterDAL.isCertificateQuotaKeyActiveInOrg(
    isInstanceWide ? undefined : orgId,
    quotaKey
  );
  const quotaOrgId = isInstanceWide ? PKI_INSTANCE_QUOTA_SCOPE : await deps.usageCounterDAL.resolveRootOrgId(orgId);
  if (isAlreadyActive) return { quotaKey, isNewQuotaKey: false, quotaOrgId, isWildcard };
  const scopeNote = isInstanceWide ? " across this instance" : "";
  const holder = isInstanceWide ? "this instance" : "your organization";

  // 0 is how a plan says it has no wildcard support at all.
  if (isWildcard && maxWildcardCertificates === 0) {
    throw new BadRequestError({
      message: `Failed to issue certificate with a wildcard name due to plan restriction. Upgrade plan to issue wildcard certificates.`
    });
  }

  const counts = await $getQuotaCounts(quotaOrgId, deps);

  const refuse = (used: number, cap: number, subject: string) => {
    if (isApprovedRequest) {
      throw new BadRequestError({
        message: `This certificate request was approved, but ${holder} has since reached its plan limit of ${cap} ${subject} (${used} of ${cap} in use). Upgrade plan or revoke an unused certificate, then submit the request again.`
      });
    }
    throw new BadRequestError({
      message: `Failed to issue certificate due to plan limit reached (${used} of ${cap} ${subject}${scopeNote}). Upgrade plan to issue more certificates.`
    });
  };

  // Before the total, so the refusal names the limit the caller actually hit.
  if (hasWildcardCap && counts.wildcard >= maxWildcardCertificates) {
    refuse(counts.wildcard, maxWildcardCertificates, "active wildcard certificates");
  }

  if (typeof maxCertificates === "number" && counts.total >= maxCertificates) {
    refuse(counts.total, maxCertificates, "active certificates");
  }

  return { quotaKey, isNewQuotaKey: true, quotaOrgId, isWildcard };
};

/** Same check for callers holding a projectId. Returns the orgId so usage can be recorded after the write. */
export const assertCertificateQuotaForProject = async ({
  projectId,
  commonName,
  altNames,
  deps,
  isApprovedRequest = false
}: {
  projectId: string;
  commonName?: string | null;
  altNames?: string | null;
  deps: TCertificateQuotaDeps;
  isApprovedRequest?: boolean;
}): Promise<{ quotaKey: string; isNewQuotaKey: boolean; quotaOrgId: string; isWildcard: boolean }> => {
  const project = await deps.projectDAL.findById(projectId);
  if (!project) throw new NotFoundError({ message: `Project with ID '${projectId}' not found` });

  return assertCertificateQuota({ orgId: project.orgId, commonName, altNames, deps, isApprovedRequest });
};