Skip to content

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

2.6 KB
Raw Blame
import crypto from "node:crypto";

import { Knex } from "knex";

import { logger } from "@app/lib/logger";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
import { TProjectDALFactory } from "@app/services/project/project-dal";

export const CERT_MANAGER_APPLICATION_RESOURCE_TYPE = "cert-manager.application";

export enum CertificateApplicationAlertEvent {
  Expiry = "cert-manager.application.certificate.expiry",
  Issuance = "cert-manager.application.certificate.issuance",
  Renewal = "cert-manager.application.certificate.renewal",
  Revocation = "cert-manager.application.certificate.revocation"
}

export const CERT_MANAGER_RESOURCE_TYPE = "cert-manager";

export enum CertificateManagerAlertEvent {
  Expiry = "cert-manager.certificate.expiry",
  Issuance = "cert-manager.certificate.issuance",
  Renewal = "cert-manager.certificate.renewal",
  Revocation = "cert-manager.certificate.revocation"
}

export const getIssuanceAlertEvent = (isRenewal?: boolean) =>
  isRenewal ? CertificateApplicationAlertEvent.Renewal : CertificateApplicationAlertEvent.Issuance;

export type TCertificateAlertEventInput = {
  certificateId: string;
  projectId: string;
  orgId?: string;
  applicationId?: string | null;
  eventType: CertificateApplicationAlertEvent;
};

type TCertificateAlertEventEmitterDep = {
  eventEmitter: TEventEmitter;
  projectDAL: Pick<TProjectDALFactory, "findById">;
};

export type TCertificateAlertEventEmitter = ReturnType<typeof certificateAlertEventEmitterFactory>;

export const certificateAlertEventEmitterFactory = ({ eventEmitter, projectDAL }: TCertificateAlertEventEmitterDep) => {
  const $emit = async (
    { certificateId, projectId, orgId, applicationId, eventType }: TCertificateAlertEventInput,
    tx?: Knex
  ) => {
    const resolvedOrgId = orgId ?? (await projectDAL.findById(projectId, tx))?.orgId;
    if (!resolvedOrgId) return;

    const event = {
      id: crypto.randomUUID(),
      type: eventType,
      data: {
        orgId: resolvedOrgId,
        projectId,
        resourceType: CERT_MANAGER_APPLICATION_RESOURCE_TYPE,
        resourceId: applicationId ?? null,
        targetIds: [certificateId]
      }
    };

    await eventEmitter.emit([event], tx);
  };

  const emit = (input: TCertificateAlertEventInput, tx: Knex) => $emit(input, tx);

  const notify = async (input: TCertificateAlertEventInput) => {
    try {
      await $emit(input);
    } catch (error) {
      logger.warn(
        error,
        `Failed to emit certificate alert event [certificateId=${input.certificateId}] [eventType=${input.eventType}]`
      );
    }
  };

  return { emit, notify };
};