Skip to content

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

18.7 KB
Raw Blame
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { ActorType } from "@app/services/auth/auth-type";
import { CertificateIssuanceOperation } from "@app/services/certificate-common/certificate-constants";
import { CertificateRequestStatus } from "@app/services/certificate-request/certificate-request-types";
import { CertStatus } from "@app/services/certificate/certificate-types";

import { EnrollmentType } from "../certificate-profile/certificate-profile-types";
import { certificateEstV3ServiceFactory, TCertificateEstV3ServiceFactory } from "./certificate-est-v3-service";

// Mock the x509 module
vi.mock("@peculiar/x509", () => ({
  Pkcs10CertificateRequest: vi.fn(),
  GeneralNames: vi.fn(),
  KeyUsagesExtension: vi.fn(),
  ExtendedKeyUsageExtension: vi.fn(),
  X509Certificate: vi.fn().mockImplementation(() => ({
    rawData: new ArrayBuffer(0)
  })),
  KeyUsageFlags: {
    digitalSignature: 1,
    nonRepudiation: 2,
    keyEncipherment: 4,
    dataEncipherment: 8,
    keyAgreement: 16,
    keyCertSign: 32,
    cRLSign: 64,
    encipherOnly: 128,
    decipherOnly: 256
  },
  ExtendedKeyUsage: {
    clientAuth: "1.3.6.1.5.5.7.3.2",
    serverAuth: "1.3.6.1.5.5.7.3.1",
    codeSigning: "1.3.6.1.5.5.7.3.3",
    emailProtection: "1.3.6.1.5.5.7.3.4",
    ocspSigning: "1.3.6.1.5.5.7.3.9",
    timeStamping: "1.3.6.1.5.5.7.3.8"
  }
}));

vi.mock("@app/lib/certificates/extract-certificate", () => ({
  extractX509CertFromChain: vi.fn(() => ["mock-cert"])
}));

vi.mock("@app/services/certificate/certificate-fns", () => ({
  isCertChainValid: vi.fn(() => Promise.resolve(true))
}));

vi.mock("@app/services/certificate-authority/certificate-authority-fns", () => ({
  getCaCertChain: vi.fn(() =>
    Promise.resolve({
      caCert: "mock-ca-cert",
      caCertChain: "mock-ca-chain"
    })
  ),
  getCaCertChains: vi.fn(() =>
    Promise.resolve([
      {
        certificate: "mock-cert",
        certificateChain: "mock-chain"
      }
    ])
  ),
  assertCaInProfileProject: vi.fn()
}));

vi.mock("@app/services/certificate-common/certificate-csr-utils", () => ({
  extractCertificateRequestFromCSR: vi.fn().mockReturnValue({ commonName: "different.example.com" })
}));

vi.mock("@app/services/project/project-fns", () => ({
  getProjectKmsCertificateKeyId: vi.fn(() => Promise.resolve("mock-kms-id"))
}));

vi.mock("../../ee/services/certificate-est/certificate-est-fns", () => ({
  convertRawCertsToPkcs7: vi.fn(() => "mocked-pkcs7-response")
}));

describe("CertificateEstV3Service", () => {
  let service: TCertificateEstV3ServiceFactory;

  const mockCertificateV3Service = {
    signCertificateFromProfile: vi.fn()
  };

  const mockCertificateAuthorityDAL = {
    findById: vi.fn(),
    findByIdWithAssociatedCa: vi.fn()
  };

  const mockCertificateAuthorityCertDAL = {
    find: vi.fn(),
    findById: vi.fn()
  };

  const mockCertificateDAL = {
    findOne: vi.fn(),
    transaction: vi.fn().mockImplementation(async (cb: (tx: unknown) => unknown) => cb({}))
  };

  const mockProjectDAL = {
    findOne: vi.fn(),
    updateById: vi.fn(),
    transaction: vi.fn()
  };

  const mockKmsService = {
    decryptWithKmsKey: vi.fn().mockResolvedValue(vi.fn(() => Promise.resolve(Buffer.from("mock-decrypted")))),
    generateKmsKey: vi.fn()
  };

  const mockLicenseService = {
    getPlan: vi.fn()
  };

  const mockCertificateProfileDAL = {
    findByIdWithConfigs: vi.fn()
  };

  const mockEstEnrollmentConfigDAL = {
    findById: vi.fn()
  };

  const mockCertificatePolicyDAL = {
    findById: vi.fn()
  };

  const mockProfile = {
    id: "profile-123",
    projectId: "project-123",
    caId: "ca-123",
    certificatePolicyId: "policy-123",
    slug: "test-profile",
    enrollmentType: EnrollmentType.EST,
    issuerType: "ca" as const,
    estConfigId: "est-config-123",
    createdAt: new Date(),
    updatedAt: new Date()
  };

  const mockEstConfig = {
    id: "est-config-123",
    disableBootstrapCaValidation: true,
    encryptedCaChain: null,
    hashedPassphrase: "hashed",
    createdAt: new Date(),
    updatedAt: new Date()
  };

  const mockProject = {
    id: "project-123",
    orgId: "org-123"
  };

  const mockPlan = {
    pkiEst: true
  };

  const mockPolicy = {
    id: "policy-123",
    validity: { max: "90d" }
  };

  const mockAuditLogService = { createCollapsedAuditLog: vi.fn() };
  const mockPkiApplicationDAL = { findById: vi.fn() };

  beforeEach(async () => {
    service = certificateEstV3ServiceFactory({
      certificateV3Service: mockCertificateV3Service,
      certificateAuthorityDAL: mockCertificateAuthorityDAL,
      certificateAuthorityCertDAL: mockCertificateAuthorityCertDAL,
      certificateDAL: mockCertificateDAL,
      projectDAL: mockProjectDAL,
      kmsService: mockKmsService,
      licenseService: mockLicenseService,
      certificateProfileDAL: mockCertificateProfileDAL,
      estEnrollmentConfigDAL: mockEstEnrollmentConfigDAL,
      certificatePolicyDAL: mockCertificatePolicyDAL,
      pkiApplicationDAL: mockPkiApplicationDAL as never,
      auditLogService: mockAuditLogService
    });

    mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue(mockProfile);
    mockEstEnrollmentConfigDAL.findById.mockResolvedValue(mockEstConfig);
    mockProjectDAL.findOne.mockResolvedValue(mockProject);
    mockLicenseService.getPlan.mockResolvedValue(mockPlan);
    mockCertificatePolicyDAL.findById.mockResolvedValue(mockPolicy);
    mockCertificateDAL.findOne.mockResolvedValue(null);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  describe("simpleEnrollByProfile", () => {
    it("should successfully enroll and return PKCS7 response", async () => {
      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.ISSUED,
        certificate: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
        certificateId: "cert-123"
      });

      const result = await service.simpleEnrollByProfile({
        csr: "mock-csr",
        profileId: "profile-123",
        sslClientCert: ""
      });

      expect(result).toBe("mocked-pkcs7-response");
      expect(mockCertificateV3Service.signCertificateFromProfile).toHaveBeenCalledWith(
        expect.objectContaining({
          profileId: "profile-123",
          csr: "mock-csr",
          enrollmentType: EnrollmentType.EST,
          validity: { ttl: "90d" }
        })
      );
    });

    it("should throw error when approval is required", async () => {
      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.PENDING_APPROVAL,
        certificateRequestId: "req-123",
        message: "Requires approval"
      });

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(BadRequestError);

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(/requires approval/i);
    });

    it("should throw error when profile not found", async () => {
      mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue(null);

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "nonexistent",
          sslClientCert: ""
        })
      ).rejects.toThrow(NotFoundError);
    });

    it("should throw error when profile is not configured for EST", async () => {
      mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue({
        ...mockProfile,
        enrollmentType: EnrollmentType.API
      });

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(BadRequestError);
    });

    it("should throw error when EST config not found", async () => {
      mockEstEnrollmentConfigDAL.findById.mockResolvedValue(null);

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(NotFoundError);
    });

    it("should throw error when PKI EST is not available in plan", async () => {
      mockLicenseService.getPlan.mockResolvedValue({ pkiEst: false });

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(BadRequestError);
    });

    it("should throw error when certificate is not returned", async () => {
      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.ISSUED,
        certificate: null,
        certificateId: "cert-123"
      });

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: ""
        })
      ).rejects.toThrow(BadRequestError);
    });

    it("records an issuance failure when the profile's default TTL exceeds the policy maximum", async () => {
      mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue({ ...mockProfile, defaults: { ttlDays: 365 } });

      await expect(
        service.simpleEnrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: "",
          auditLogInfo: { ipAddress: "127.0.0.1", actor: { type: ActorType.PLATFORM, metadata: {} } }
        })
      ).rejects.toThrow("exceeds the policy's maximum validity");

      expect(mockCertificateV3Service.signCertificateFromProfile).not.toHaveBeenCalled();
      expect(mockAuditLogService.createCollapsedAuditLog).toHaveBeenCalledWith(
        expect.objectContaining({
          actor: { type: ActorType.EST_ACCOUNT, metadata: { profileId: "profile-123" } },
          event: expect.objectContaining({
            type: EventType.CERTIFICATE_ISSUANCE_FAILED,
            metadata: expect.objectContaining({
              operation: CertificateIssuanceOperation.SIGN,
              enrollmentType: EnrollmentType.EST,
              certificateProfileId: "profile-123"
            })
          })
        })
      );
    });

    it("should use flow default TTL when profile has no defaultTtlDays", async () => {
      mockCertificatePolicyDAL.findById.mockResolvedValue({
        id: "policy-123",
        validity: { max: "30d" }
      });

      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.ISSUED,
        certificate: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
        certificateId: "cert-123"
      });

      await service.simpleEnrollByProfile({
        csr: "mock-csr",
        profileId: "profile-123",
        sslClientCert: ""
      });

      expect(mockCertificateV3Service.signCertificateFromProfile).toHaveBeenCalledWith(
        expect.objectContaining({
          validity: { ttl: "90d" }
        })
      );
    });

    it("should use default 90d TTL when policy has no validity", async () => {
      mockCertificatePolicyDAL.findById.mockResolvedValue({
        id: "policy-123",
        validity: null
      });

      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.ISSUED,
        certificate: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
        certificateId: "cert-123"
      });

      await service.simpleEnrollByProfile({
        csr: "mock-csr",
        profileId: "profile-123",
        sslClientCert: ""
      });

      expect(mockCertificateV3Service.signCertificateFromProfile).toHaveBeenCalledWith(
        expect.objectContaining({
          validity: { ttl: "90d" }
        })
      );
    });
  });

  describe("simpleReenrollByProfile", () => {
    beforeEach(async () => {
      const { Pkcs10CertificateRequest, X509Certificate, GeneralNames } = await import("@peculiar/x509");

      (Pkcs10CertificateRequest as any).mockImplementation(() => ({
        subject: "CN=test.example.com",
        extensions: []
      }));

      (X509Certificate as any).mockImplementation(() => ({
        subject: "CN=test.example.com",
        extensions: [],
        rawData: new ArrayBuffer(0)
      }));

      (GeneralNames as any).mockImplementation(() => ({
        items: []
      }));
    });

    it("should successfully re-enroll and return PKCS7 response", async () => {
      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.ISSUED,
        certificate: "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----",
        certificateId: "cert-123"
      });

      const result = await service.simpleReenrollByProfile({
        csr: "mock-csr",
        profileId: "profile-123",
        sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
      });

      expect(result).toBe("mocked-pkcs7-response");
    });

    it("should throw error when subjects do not match", async () => {
      const { Pkcs10CertificateRequest, X509Certificate } = await import("@peculiar/x509");

      (Pkcs10CertificateRequest as any).mockImplementation(() => ({
        subject: "CN=different.example.com",
        extensions: []
      }));

      (X509Certificate as any).mockImplementation(() => ({
        subject: "CN=test.example.com",
        extensions: [],
        rawData: new ArrayBuffer(0)
      }));

      await expect(
        service.simpleReenrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
        })
      ).rejects.toThrow(BadRequestError);
    });

    it("records a renew failure when the CSR subject doesn't match the client certificate", async () => {
      const { Pkcs10CertificateRequest } = await import("@peculiar/x509");
      (Pkcs10CertificateRequest as any).mockImplementation(() => ({
        subject: "CN=different.example.com",
        extensions: []
      }));
      mockCertificateDAL.findOne.mockResolvedValue({ id: "cert-old", status: CertStatus.ACTIVE });

      await expect(
        service.simpleReenrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----"),
          auditLogInfo: { ipAddress: "127.0.0.1", actor: { type: ActorType.PLATFORM, metadata: {} } }
        })
      ).rejects.toThrow("Subject mismatch");

      expect(mockAuditLogService.createCollapsedAuditLog).toHaveBeenCalledWith(
        expect.objectContaining({
          projectId: "project-123",
          actor: { type: ActorType.EST_ACCOUNT, metadata: { profileId: "profile-123" } },
          event: expect.objectContaining({
            type: EventType.CERTIFICATE_ISSUANCE_FAILED,
            metadata: expect.objectContaining({
              operation: CertificateIssuanceOperation.RENEW,
              enrollmentType: EnrollmentType.EST,
              certificateProfileId: "profile-123",
              originalCertificateId: "cert-old",
              error: "Subject mismatch"
            })
          })
        })
      );
    });

    it("should throw error when approval is required for re-enrollment", async () => {
      mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({
        status: CertificateRequestStatus.PENDING_APPROVAL,
        certificateRequestId: "req-123",
        message: "Requires approval"
      });

      await expect(
        service.simpleReenrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
        })
      ).rejects.toThrow(/requires approval/i);
    });

    it("should reject re-enrollment when the client certificate is revoked", async () => {
      mockCertificateDAL.findOne.mockResolvedValue({ status: CertStatus.REVOKED });

      await expect(
        service.simpleReenrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
        })
      ).rejects.toThrow(UnauthorizedError);

      expect(mockCertificateV3Service.signCertificateFromProfile).not.toHaveBeenCalled();
    });

    it("should reject re-enrollment when the client certificate was deleted", async () => {
      mockCertificateDAL.findOne.mockResolvedValue({
        status: CertStatus.ACTIVE,
        deletedAt: new Date(),
        commonName: "device.example.com",
        serialNumber: "01"
      });

      await expect(
        service.simpleReenrollByProfile({
          csr: "mock-csr",
          profileId: "profile-123",
          sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
        })
      ).rejects.toThrow("was deleted and can no longer be re-enrolled");

      expect(mockCertificateV3Service.signCertificateFromProfile).not.toHaveBeenCalled();
    });
  });

  describe("getCaCertsByProfile", () => {
    it("should return CA certificates in PKCS7 format", async () => {
      mockCertificateAuthorityDAL.findByIdWithAssociatedCa.mockResolvedValue({
        id: "ca-123",
        internalCa: {
          id: "internal-ca-123",
          activeCaCertId: "ca-cert-123"
        }
      });

      const result = await service.getCaCertsByProfile({
        profileId: "profile-123"
      });

      expect(result).toBe("mocked-pkcs7-response");
    });

    it("should throw error when profile not found", async () => {
      mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue(null);

      await expect(
        service.getCaCertsByProfile({
          profileId: "nonexistent"
        })
      ).rejects.toThrow(NotFoundError);
    });

    it("should throw error when profile is not configured for EST", async () => {
      mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue({
        ...mockProfile,
        enrollmentType: EnrollmentType.API
      });

      await expect(
        service.getCaCertsByProfile({
          profileId: "profile-123"
        })
      ).rejects.toThrow(BadRequestError);
    });
  });
});