Skip to content

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

31.8 KB
Raw Blame
/* eslint-disable no-await-in-loop */
import RE2 from "re2";

import { TCertificateSyncs } from "@app/db/schemas";
import { logger } from "@app/lib/logger";
import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certificate-sync-dal";
import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";
import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";

import { PkiSyncError } from "../pki-sync-errors";
import { TCertificateMap, TPkiSyncSyncResult, TPkiSyncWithCredentials } from "../pki-sync-types";
import { getGcpAccessToken } from "./gcp-certificate-manager-pki-sync-auth-fns";
import {
  createGcpCertificateManagerClient,
  executeWithConcurrencyLimit,
  getGcpErrorStatus,
  getGcpHttpStatus,
  isCertificateInUseError,
  mapGcpError
} from "./gcp-certificate-manager-pki-sync-client";
import {
  GCP_CERTIFICATE_ID_LABEL_KEY,
  GCP_MANAGED_BY_LABEL_KEY,
  GCP_MANAGED_BY_LABEL_VALUE,
  GCP_MAX_CERTIFICATES_PER_MAP_ENTRY,
  GCP_PRIMARY_MATCHER,
  gcpCertificateMapEntryPermission,
  gcpCertificateMapPermission,
  gcpCertificatePermission
} from "./gcp-certificate-manager-pki-sync-constants";
import {
  GcpCertificateManagerAction,
  GcpCertificateManagerScope,
  GcpErrorStatus
} from "./gcp-certificate-manager-pki-sync-enums";
import { assertKeyAlgorithmSupported } from "./gcp-certificate-manager-pki-sync-key-fns";
import {
  buildGcpCertificateResourceName,
  toGcpCertificateId,
  toGcpCertificateMapEntryId
} from "./gcp-certificate-manager-pki-sync-name-fns";
import {
  TGcpCertificate,
  TGcpCertificateManagerPkiSyncConfig,
  TGcpCertificateManagerPkiSyncOptions,
  TGcpCertificateMapEntry
} from "./gcp-certificate-manager-pki-sync-types";
import { buildGcpTooManyCertificatesMessage } from "./gcp-certificate-manager-pki-sync-update-fns";

type TGcpCertificateManagerPkiSyncFactoryDeps = {
  certificateSyncDAL: Pick<
    TCertificateSyncDALFactory,
    | "removeCertificates"
    | "addCertificates"
    | "findByPkiSyncAndCertificate"
    | "updateById"
    | "findByPkiSyncId"
    | "updateSyncStatus"
  >;
  certificateDAL: Pick<TCertificateDALFactory, "findById">;
};

type TPlannedCertificate = { skipReason: string } | { target: TUploadTarget };

type TUploadTarget = {
  key: string;
  certificateId: string;
  resourceName: string;
  pemCertificate: string;
  pemPrivateKey: string;
  infisicalCertificateId?: string;
  renewedFromCertificateId?: string;
  renewedFromResourceName?: string;
  shouldPatch: boolean;
};

const getConfig = (pkiSync: TPkiSyncWithCredentials) => {
  const config = pkiSync.destinationConfig as TGcpCertificateManagerPkiSyncConfig;

  if (!config?.gcpProjectId || !config?.location) {
    throw new PkiSyncError({
      shouldRetry: false,
      message: "GCP Certificate Manager sync is missing its project or location configuration."
    });
  }

  return config;
};

const DASH_REGEX = new RE2("-", "g");

const ENTRY_RESOURCE_NAME_REGEX = new RE2("/certificateMaps/([^/]+)/certificateMapEntries/([^/]+)$");

const buildLabels = (userLabels: TGcpCertificateManagerPkiSyncOptions["labels"], infisicalCertificateId?: string) => ({
  ...Object.fromEntries((userLabels ?? []).map(({ key, value }) => [key, value])),
  [GCP_MANAGED_BY_LABEL_KEY]: GCP_MANAGED_BY_LABEL_VALUE,
  ...(infisicalCertificateId ? { [GCP_CERTIFICATE_ID_LABEL_KEY]: infisicalCertificateId.replace(DASH_REGEX, "") } : {})
});

const isInfisicalManaged = (labels?: Record<string, string>) =>
  labels?.[GCP_MANAGED_BY_LABEL_KEY] === GCP_MANAGED_BY_LABEL_VALUE;

// GCP expects the leaf certificate first, followed by any intermediates. The root is already stripped
// upstream when includeRootCa is disabled.
const buildPemChain = (cert: string, certificateChain?: string) => {
  const leaf = cert.trim();
  const chain = certificateChain?.trim();
  return chain ? `${leaf}\n${chain}` : leaf;
};

const GCP_MAP_ENTRY_OPERATION: Record<GcpCertificateManagerAction, string> = {
  [GcpCertificateManagerAction.Get]: "certificate map entry lookup",
  [GcpCertificateManagerAction.List]: "certificate map entry list",
  [GcpCertificateManagerAction.Create]: "certificate map entry creation",
  [GcpCertificateManagerAction.Update]: "certificate map entry update",
  [GcpCertificateManagerAction.Delete]: "certificate map entry deletion"
};

export const gcpCertificateManagerPkiSyncFactory = ({
  certificateSyncDAL,
  certificateDAL
}: TGcpCertificateManagerPkiSyncFactoryDeps) => {
  const sameCertificateSet = (current: string[] | undefined, desired: string[]) => {
    if ((current?.length ?? 0) !== desired.length) return false;
    const have = new Set(current ?? []);
    return desired.every((name) => have.has(name));
  };

  const $reconcileCertificateMapEntry = async ({
    client,
    pkiSync,
    gcpProjectId,
    certificateMapBinding,
    certificateResourceNames,
    retainResourceNames,
    userLabels
  }: {
    client: ReturnType<typeof createGcpCertificateManagerClient>;
    pkiSync: TPkiSyncWithCredentials;
    gcpProjectId: string;
    certificateMapBinding: NonNullable<TGcpCertificateManagerPkiSyncConfig["certificateMapBinding"]>;
    certificateResourceNames: string[];
    retainResourceNames: string[];
    userLabels: TGcpCertificateManagerPkiSyncOptions["labels"];
  }) => {
    const { certificateMap, hostname } = certificateMapBinding;
    const entryId = toGcpCertificateMapEntryId(pkiSync.id);

    let existingEntry: TGcpCertificateMapEntry | undefined;
    try {
      existingEntry = await client.getCertificateMapEntry(certificateMap, entryId);
    } catch (error) {
      throw mapGcpError(error, {
        operation: "certificate map entry lookup",
        gcpProjectId,
        resource: `certificate map entry "${entryId}" in map "${certificateMap}"`,
        permission: gcpCertificateMapEntryPermission(GcpCertificateManagerAction.Get)
      });
    }

    const desiredResourceNames = Array.from(
      new Set([
        ...certificateResourceNames,
        ...retainResourceNames.filter((name) => existingEntry?.certificates?.includes(name))
      ])
    );

    if (existingEntry && !isInfisicalManaged(existingEntry.labels)) {
      throw new PkiSyncError({
        shouldRetry: false,
        message: `Certificate map entry "${entryId}" already exists in map "${certificateMap}" and is not managed by Infisical. Remove or rename it before enabling certificate map binding.`
      });
    }

    const willBecomePrimary = !hostname && existingEntry?.matcher !== GCP_PRIMARY_MATCHER;

    if (willBecomePrimary) {
      let entries: Map<string, TGcpCertificateMapEntry>;
      try {
        entries = await client.listCertificateMapEntries(certificateMap);
      } catch (error) {
        throw mapGcpError(error, {
          operation: "certificate map entry list",
          gcpProjectId,
          resource: `certificate map "${certificateMap}"`,
          permission: gcpCertificateMapEntryPermission(GcpCertificateManagerAction.List)
        });
      }

      const conflictingPrimary = Array.from(entries.values()).find((entry) => entry.matcher === GCP_PRIMARY_MATCHER);
      if (conflictingPrimary) {
        throw new PkiSyncError({
          shouldRetry: false,
          message: `Certificate map "${certificateMap}" already has a primary entry ("${conflictingPrimary.name
            .split("/")
            .pop()}"). Set a hostname on this sync instead.`
        });
      }
    }

    let attempted = existingEntry ? GcpCertificateManagerAction.Update : GcpCertificateManagerAction.Create;

    try {
      if (!existingEntry) {
        await client.createCertificateMapEntry({
          certificateMap,
          entryId,
          certificateResourceNames: desiredResourceNames,
          hostname,
          labels: buildLabels(userLabels)
        });
        return;
      }

      // hostname and matcher are immutable on a certificate map entry, so a change to either
      // means deleting and recreating rather than patching
      const desiredMatcher = hostname ? undefined : GCP_PRIMARY_MATCHER;
      if (existingEntry.hostname !== hostname || existingEntry.matcher !== desiredMatcher) {
        attempted = GcpCertificateManagerAction.Delete;
        await client.deleteCertificateMapEntry({ certificateMap, entryId });
        attempted = GcpCertificateManagerAction.Create;
        await client.createCertificateMapEntry({
          certificateMap,
          entryId,
          certificateResourceNames: desiredResourceNames,
          hostname,
          labels: buildLabels(userLabels)
        });
        return;
      }

      if (sameCertificateSet(existingEntry.certificates, desiredResourceNames)) return;

      await client.updateCertificateMapEntryCertificates({
        certificateMap,
        entryId,
        certificateResourceNames: desiredResourceNames,
        labels: buildLabels(userLabels)
      });
    } catch (error) {
      if (error instanceof PkiSyncError) throw error;
      throw mapGcpError(error, {
        operation: GCP_MAP_ENTRY_OPERATION[attempted],
        gcpProjectId,
        resource: `certificate map entry "${entryId}"`,
        permission: gcpCertificateMapEntryPermission(attempted)
      });
    }
  };

  // A certificate cannot be deleted while a certificate map entry references it, so our entry is
  // removed first when it still points at the certificate being deleted.
  const $detachAndDeleteCertificate = async ({
    client,
    pkiSync,
    gcpProjectId,
    certificateMapBinding,
    certificateId,
    resourceName
  }: {
    client: ReturnType<typeof createGcpCertificateManagerClient>;
    pkiSync: TPkiSyncWithCredentials;
    gcpProjectId: string;
    certificateMapBinding?: TGcpCertificateManagerPkiSyncConfig["certificateMapBinding"];
    certificateId: string;
    resourceName: string;
  }) => {
    if (certificateMapBinding) {
      const entryId = toGcpCertificateMapEntryId(pkiSync.id);
      let attempted = GcpCertificateManagerAction.Get;

      try {
        const entry = await client.getCertificateMapEntry(certificateMapBinding.certificateMap, entryId);

        if (entry && isInfisicalManaged(entry.labels) && entry.certificates?.includes(resourceName)) {
          const remaining = entry.certificates.filter((name) => name !== resourceName);

          if (remaining.length) {
            attempted = GcpCertificateManagerAction.Update;
            await client.updateCertificateMapEntryCertificates({
              certificateMap: certificateMapBinding.certificateMap,
              entryId,
              certificateResourceNames: remaining,
              labels: entry.labels ?? {}
            });
          } else {
            attempted = GcpCertificateManagerAction.Delete;
            await client.deleteCertificateMapEntry({
              certificateMap: certificateMapBinding.certificateMap,
              entryId
            });
          }
        }
      } catch (error) {
        if (error instanceof PkiSyncError) throw error;
        throw mapGcpError(error, {
          operation: GCP_MAP_ENTRY_OPERATION[attempted],
          gcpProjectId,
          resource: `certificate map entry "${entryId}" in map "${certificateMapBinding.certificateMap}"`,
          permission: gcpCertificateMapEntryPermission(attempted)
        });
      }
    }

    try {
      await client.deleteCertificate(certificateId);
    } catch (error) {
      if (error instanceof PkiSyncError) throw error;

      if (getGcpErrorStatus(error) === GcpErrorStatus.NotFound || getGcpHttpStatus(error) === 404) return;

      if (isCertificateInUseError(error)) {
        throw new PkiSyncError({
          shouldRetry: false,
          message: `Certificate "${certificateId}" is still referenced by a certificate map entry in GCP project "${gcpProjectId}". Detach it from the certificate map before removing it from this sync.`
        });
      }

      throw mapGcpError(error, {
        operation: "certificate deletion",
        gcpProjectId,
        resource: `certificate "${certificateId}"`,
        permission: gcpCertificatePermission(GcpCertificateManagerAction.Delete)
      });
    }
  };

  const $reapOrphanedMapEntries = async ({
    client,
    pkiSync,
    certificates,
    currentCertificateMap
  }: {
    client: ReturnType<typeof createGcpCertificateManagerClient>;
    pkiSync: TPkiSyncWithCredentials;
    certificates: Map<string, TGcpCertificate>;
    currentCertificateMap?: string;
  }) => {
    const entryId = toGcpCertificateMapEntryId(pkiSync.id);
    const orphans = new Map<string, string>();

    certificates.forEach((certificate) => {
      if (!isInfisicalManaged(certificate.labels)) return;
      (certificate.usedBy ?? []).forEach(({ name }) => {
        const match = ENTRY_RESOURCE_NAME_REGEX.exec(name);
        if (!match) return;
        const [, certificateMap, usedEntryId] = match;
        if (usedEntryId !== entryId) return;
        if (certificateMap === currentCertificateMap) return;
        orphans.set(name, certificateMap);
      });
    });

    const failedReaps: string[] = [];

    for (const [name, certificateMap] of orphans) {
      try {
        await client.deleteCertificateMapEntry({ certificateMap, entryId });
        logger.info({ syncId: pkiSync.id, entry: name }, "Removed orphaned GCP certificate map entry");
      } catch (error) {
        failedReaps.push(`"${entryId}" in map "${certificateMap}"`);
        logger.warn({ syncId: pkiSync.id, entry: name, error }, "Failed to remove orphaned GCP certificate map entry");
      }
    }

    return failedReaps;
  };

  const $planCertificate = async ({
    certName,
    entry,
    syncRecordsByCertId,
    existingCertificates,
    gcpProjectId,
    location,
    preserveItemOnRenewal
  }: {
    certName: string;
    entry: TCertificateMap[string];
    syncRecordsByCertId: Map<string, TCertificateSyncs>;
    existingCertificates: Map<string, TGcpCertificate>;
    gcpProjectId: string;
    location: string;
    preserveItemOnRenewal: boolean;
  }): Promise<TPlannedCertificate> => {
    const { cert, privateKey, certificateChain, certificateId } = entry;

    const certificate = typeof certificateId === "string" ? await certificateDAL.findById(certificateId) : undefined;
    if (certificate?.renewedByCertificateId) {
      return { skipReason: "Certificate has been renewed and replaced by a newer certificate" };
    }
    const { renewedFromCertificateId } = certificate ?? {};

    if (!cert || !cert.includes("-----BEGIN CERTIFICATE-----")) {
      return { skipReason: "Certificate is missing or not in valid PEM format" };
    }

    if (!privateKey || !privateKey.includes("-----BEGIN")) {
      return { skipReason: "Private key is missing or not in valid PEM format" };
    }

    try {
      assertKeyAlgorithmSupported(cert);
    } catch (error) {
      return { skipReason: error instanceof Error ? error.message : "Unsupported key algorithm" };
    }

    const syncRecordLookupId = renewedFromCertificateId || certificateId;
    const existingSyncRecord =
      (typeof syncRecordLookupId === "string" ? syncRecordsByCertId.get(syncRecordLookupId) : undefined) ??
      (preserveItemOnRenewal && typeof certificateId === "string" ? syncRecordsByCertId.get(certificateId) : undefined);
    const reusableResourceName =
      preserveItemOnRenewal &&
      existingSyncRecord?.externalIdentifier &&
      existingCertificates.has(existingSyncRecord.externalIdentifier)
        ? existingSyncRecord.externalIdentifier
        : undefined;

    const gcpCertificateId = reusableResourceName
      ? (reusableResourceName.split("/").pop() as string)
      : toGcpCertificateId(certName);
    const resourceName = buildGcpCertificateResourceName({ gcpProjectId, location, certificateId: gcpCertificateId });
    const existing = existingCertificates.get(resourceName);

    if (existing && !isInfisicalManaged(existing.labels)) {
      return {
        skipReason: `A certificate named "${gcpCertificateId}" already exists in GCP and is not managed by Infisical. Rename the certificate name schema or remove the existing certificate.`
      };
    }

    return {
      target: {
        key: certName,
        certificateId: gcpCertificateId,
        resourceName,
        pemCertificate: buildPemChain(cert, certificateChain),
        pemPrivateKey: privateKey.trim(),
        infisicalCertificateId: certificateId,
        renewedFromCertificateId: renewedFromCertificateId ?? undefined,
        renewedFromResourceName: renewedFromCertificateId
          ? (existingSyncRecord?.externalIdentifier ?? undefined)
          : undefined,
        shouldPatch: Boolean(existing)
      }
    };
  };

  const $planUploads = async ({
    certificateMap,
    syncRecordsByCertId,
    existingCertificates,
    gcpProjectId,
    location,
    preserveItemOnRenewal
  }: {
    certificateMap: TCertificateMap;
    syncRecordsByCertId: Map<string, TCertificateSyncs>;
    existingCertificates: Map<string, TGcpCertificate>;
    gcpProjectId: string;
    location: string;
    preserveItemOnRenewal: boolean;
  }) => {
    const skippedCertificates: Array<{ name: string; reason: string }> = [];
    const uploadTargets: TUploadTarget[] = [];
    const activeResourceNames = new Set<string>();
    const trackedCertificateIds = new Set<string>();
    const retainResourceNames = new Set<string>();

    for (const [certName, entry] of Object.entries(certificateMap)) {
      if (entry.certificateId) trackedCertificateIds.add(entry.certificateId);

      const planned = await $planCertificate({
        certName,
        entry,
        syncRecordsByCertId,
        existingCertificates,
        gcpProjectId,
        location,
        preserveItemOnRenewal
      });

      if ("skipReason" in planned) {
        skippedCertificates.push({ name: certName, reason: planned.skipReason });
        const record = entry.certificateId ? syncRecordsByCertId.get(entry.certificateId) : undefined;
        if (record?.externalIdentifier) retainResourceNames.add(record.externalIdentifier);
      } else {
        activeResourceNames.add(planned.target.resourceName);
        uploadTargets.push(planned.target);
      }
    }

    return { uploadTargets, skippedCertificates, activeResourceNames, trackedCertificateIds, retainResourceNames };
  };

  const $servingResourceName = (target?: TUploadTarget) => {
    if (!target) return undefined;
    return target.shouldPatch ? target.resourceName : target.renewedFromResourceName;
  };

  const $uploadCertificate = async ({
    client,
    pkiSync,
    target,
    gcpProjectId,
    scope,
    userLabels
  }: {
    client: ReturnType<typeof createGcpCertificateManagerClient>;
    pkiSync: TPkiSyncWithCredentials;
    target: TUploadTarget;
    gcpProjectId: string;
    scope: GcpCertificateManagerScope;
    userLabels: TGcpCertificateManagerPkiSyncOptions["labels"];
  }) => {
    try {
      await client.upsertCertificate({
        certificateId: target.certificateId,
        pemCertificate: target.pemCertificate,
        pemPrivateKey: target.pemPrivateKey,
        labels: buildLabels(userLabels, target.infisicalCertificateId),
        scope,
        shouldPatch: target.shouldPatch
      });
    } catch (error) {
      if (error instanceof PkiSyncError) throw error;
      throw mapGcpError(error, {
        operation: target.shouldPatch ? "certificate update" : "certificate upload",
        gcpProjectId,
        resource: `certificate "${target.certificateId}"`,
        permission: target.shouldPatch
          ? gcpCertificatePermission(GcpCertificateManagerAction.Update)
          : gcpCertificatePermission(GcpCertificateManagerAction.Create)
      });
    }

    if (!target.infisicalCertificateId) return target;

    const existingCertSync = await certificateSyncDAL.findByPkiSyncAndCertificate(
      pkiSync.id,
      target.infisicalCertificateId
    );

    if (existingCertSync) {
      await certificateSyncDAL.updateById(existingCertSync.id, {
        externalIdentifier: target.resourceName,
        syncStatus: CertificateSyncStatus.Succeeded,
        lastSyncedAt: new Date()
      });
    } else {
      await certificateSyncDAL.addCertificates(pkiSync.id, [
        { certificateId: target.infisicalCertificateId, externalIdentifier: target.resourceName }
      ]);
    }

    if (target.renewedFromCertificateId && target.renewedFromResourceName === target.resourceName) {
      await certificateSyncDAL.removeCertificates(pkiSync.id, [target.renewedFromCertificateId]);
    }

    return target;
  };

  const $removeStaleCertificates = async ({
    client,
    pkiSync,
    gcpProjectId,
    certificateMapBinding,
    existingSyncRecords,
    existingCertificates,
    activeResourceNames,
    uploadedCertificateIds,
    trackedCertificateIds
  }: {
    client: ReturnType<typeof createGcpCertificateManagerClient>;
    pkiSync: TPkiSyncWithCredentials;
    gcpProjectId: string;
    certificateMapBinding?: TGcpCertificateManagerPkiSyncConfig["certificateMapBinding"];
    existingSyncRecords: TCertificateSyncs[];
    existingCertificates: Map<string, TGcpCertificate>;
    activeResourceNames: Set<string>;
    uploadedCertificateIds: Set<string>;
    trackedCertificateIds: Set<string>;
  }) => {
    const candidates = existingSyncRecords.flatMap((record) => {
      const { externalIdentifier, certificateId } = record;
      if (!externalIdentifier || !certificateId) return [];
      if (activeResourceNames.has(externalIdentifier) || !existingCertificates.has(externalIdentifier)) return [];
      if (trackedCertificateIds.has(certificateId) && !uploadedCertificateIds.has(certificateId)) return [];

      return [
        {
          certificateId: externalIdentifier.split("/").pop() as string,
          resourceName: externalIdentifier,
          infisicalCertificateId: certificateId,
          keepSyncRecord: uploadedCertificateIds.has(certificateId)
        }
      ];
    });

    let removed = 0;
    const failedRemovals: Array<{ name: string; error: string }> = [];

    for (const candidate of candidates) {
      try {
        await $detachAndDeleteCertificate({
          client,
          pkiSync,
          gcpProjectId,
          certificateMapBinding,
          certificateId: candidate.certificateId,
          resourceName: candidate.resourceName
        });
        if (!candidate.keepSyncRecord) {
          await certificateSyncDAL.removeCertificates(pkiSync.id, [candidate.infisicalCertificateId]);
        }
        removed += 1;
      } catch (error) {
        const message = error instanceof Error ? error.message : "Unknown error";
        failedRemovals.push({ name: candidate.certificateId, error: message });
        await certificateSyncDAL.updateSyncStatus(
          pkiSync.id,
          candidate.infisicalCertificateId,
          CertificateSyncStatus.Failed,
          message
        );
        logger.warn(
          { syncId: pkiSync.id, certificateId: candidate.certificateId, error },
          "Failed to remove certificate from GCP Certificate Manager"
        );
      }
    }

    return { removed, failedRemovals };
  };

  const syncCertificates = async (
    pkiSync: TPkiSyncWithCredentials,
    certificateMap: TCertificateMap
  ): Promise<TPkiSyncSyncResult> => {
    const { gcpProjectId, location, scope, certificateMapBinding } = getConfig(pkiSync);
    const syncOptions = pkiSync.syncOptions as TGcpCertificateManagerPkiSyncOptions | undefined;
    const canRemoveCertificates = syncOptions?.canRemoveCertificates ?? true;
    const userLabels = syncOptions?.labels;

    const accessToken = await getGcpAccessToken(pkiSync);
    const client = createGcpCertificateManagerClient({ accessToken, gcpProjectId, location, syncId: pkiSync.id });

    if (certificateMapBinding) {
      try {
        await client.assertCertificateMapExists(certificateMapBinding.certificateMap);
      } catch (error) {
        throw mapGcpError(error, {
          operation: "certificate map lookup",
          gcpProjectId,
          resource: `certificate map "${certificateMapBinding.certificateMap}"`,
          permission: gcpCertificateMapPermission(GcpCertificateManagerAction.Get)
        });
      }
    }

    let existingCertificates: Map<string, TGcpCertificate>;
    try {
      existingCertificates = await client.listCertificates();
    } catch (error) {
      throw mapGcpError(error, {
        operation: "certificate list",
        gcpProjectId,
        permission: gcpCertificatePermission(GcpCertificateManagerAction.List)
      });
    }

    const existingSyncRecords = await certificateSyncDAL.findByPkiSyncId(pkiSync.id);
    const syncRecordsByCertId = new Map<string, TCertificateSyncs>();
    existingSyncRecords.forEach((record) => {
      if (record.certificateId) syncRecordsByCertId.set(record.certificateId, record);
    });

    const { uploadTargets, skippedCertificates, activeResourceNames, trackedCertificateIds, retainResourceNames } =
      await $planUploads({
        certificateMap,
        syncRecordsByCertId,
        existingCertificates,
        gcpProjectId,
        location,
        preserveItemOnRenewal: syncOptions?.preserveItemOnRenewal ?? true
      });

    if (certificateMapBinding && uploadTargets.length > GCP_MAX_CERTIFICATES_PER_MAP_ENTRY) {
      throw new PkiSyncError({
        shouldRetry: false,
        message: buildGcpTooManyCertificatesMessage(certificateMapBinding.certificateMap, uploadTargets.length)
      });
    }

    const uploadResults = await executeWithConcurrencyLimit(
      uploadTargets,
      (target) => $uploadCertificate({ client, pkiSync, target, gcpProjectId, scope, userLabels }),
      { operation: "upload-certificates", syncId: pkiSync.id }
    );

    const successfulUploads = uploadResults.filter(
      (result): result is PromiseFulfilledResult<TUploadTarget> => result.status === "fulfilled"
    );

    const failedUploadTargets = uploadResults
      .map((result, index) => ({ result, target: uploadTargets[index] }))
      .filter(({ result }) => result.status === "rejected");

    const failedUploads = failedUploadTargets.map(({ result, target }) => ({
      name: target?.key ?? "unknown",
      error:
        (result as PromiseRejectedResult).reason instanceof Error
          ? ((result as PromiseRejectedResult).reason as Error).message
          : "Unknown error"
    }));

    let failedMapEntry: string | undefined;
    if (certificateMapBinding && successfulUploads.length) {
      try {
        await $reconcileCertificateMapEntry({
          client,
          pkiSync,
          gcpProjectId,
          certificateMapBinding,
          certificateResourceNames: successfulUploads.map(({ value }) => value.resourceName),
          retainResourceNames: [
            ...retainResourceNames,
            ...failedUploadTargets.flatMap(({ target }) => $servingResourceName(target) ?? [])
          ],
          userLabels
        });
      } catch (error) {
        failedMapEntry = error instanceof Error ? error.message : "Unknown error";
        logger.error(
          { syncId: pkiSync.id, certificateMap: certificateMapBinding.certificateMap, error },
          "Certificates synced to GCP Certificate Manager but the certificate map entry could not be reconciled"
        );
      }
    }

    let failedReaps: string[] = [];
    const mapEntryOutOfSync =
      Boolean(certificateMapBinding) &&
      (Boolean(failedMapEntry) || (uploadTargets.length > 0 && !successfulUploads.length));

    if (mapEntryOutOfSync) {
      logger.warn(
        { syncId: pkiSync.id, certificateMap: certificateMapBinding?.certificateMap },
        "Skipping GCP certificate map entry cleanup because the entry could not be brought up to date"
      );
    } else {
      failedReaps = await $reapOrphanedMapEntries({
        client,
        pkiSync,
        certificates: existingCertificates,
        currentCertificateMap: certificateMapBinding?.certificateMap
      });
    }

    const { removed, failedRemovals } =
      canRemoveCertificates && !mapEntryOutOfSync
        ? await $removeStaleCertificates({
            client,
            pkiSync,
            gcpProjectId,
            certificateMapBinding,
            existingSyncRecords,
            existingCertificates,
            activeResourceNames,
            trackedCertificateIds,
            uploadedCertificateIds: new Set(
              successfulUploads
                .map(({ value }) => value.infisicalCertificateId)
                .filter((id): id is string => Boolean(id))
            )
          })
        : { removed: 0, failedRemovals: [] };

    if (failedUploads.length) {
      logger.error(
        { syncId: pkiSync.id, failedUploads },
        "Some certificates failed to sync to GCP Certificate Manager"
      );
    }

    const details: TPkiSyncSyncResult["details"] = {};
    if (skippedCertificates.length) details.skippedCertificates = skippedCertificates;
    if (failedUploads.length) details.failedUploads = failedUploads;
    if (failedRemovals.length) details.failedRemovals = failedRemovals;
    const buildPartialFailureMessage = () => {
      if (failedMapEntry) {
        return `${successfulUploads.length} certificate(s) reached GCP Certificate Manager, but the certificate map entry could not be updated: ${failedMapEntry}`;
      }

      if (failedReaps.length) {
        return `${successfulUploads.length} certificate(s) reached GCP Certificate Manager, but ${failedReaps.length} certificate map entry from a previous binding could not be removed (${failedReaps.join(
          ", "
        )}). Those entries keep serving the certificate they already point at.`;
      }

      return undefined;
    };

    const partialFailureMessage = buildPartialFailureMessage();

    return {
      uploaded: successfulUploads.length,
      removed,
      failedRemovals: failedRemovals.length,
      skipped: skippedCertificates.length,
      details: Object.keys(details).length ? details : undefined,
      partialFailureMessage
    };
  };

  const removeCertificates = async (
    pkiSync: TPkiSyncWithCredentials,
    certificateNames: string[],
    deps?: { certificateSyncDAL?: TCertificateSyncDALFactory; certificateMap?: TCertificateMap }
  ) => {
    const { gcpProjectId, location, certificateMapBinding } = getConfig(pkiSync);

    const accessToken = await getGcpAccessToken(pkiSync);
    const client = createGcpCertificateManagerClient({ accessToken, gcpProjectId, location, syncId: pkiSync.id });

    const existingSyncRecords = await certificateSyncDAL.findByPkiSyncId(pkiSync.id);
    const targets = certificateNames.flatMap((certName) => {
      const infisicalCertificateId = deps?.certificateMap?.[certName]?.certificateId;
      if (!infisicalCertificateId) return [];

      const record = existingSyncRecords.find((row) => row.certificateId === infisicalCertificateId);
      if (!record?.externalIdentifier) return [];

      return [
        {
          certificateId: record.externalIdentifier.split("/").pop() as string,
          resourceName: record.externalIdentifier,
          infisicalCertificateId
        }
      ];
    });

    if (!targets.length) {
      return { removed: 0, failed: 0, skipped: certificateNames.length };
    }

    const failures: string[] = [];
    let removed = 0;

    for (const target of targets) {
      try {
        await $detachAndDeleteCertificate({
          client,
          pkiSync,
          gcpProjectId,
          certificateMapBinding,
          certificateId: target.certificateId,
          resourceName: target.resourceName
        });
        await certificateSyncDAL.removeCertificates(pkiSync.id, [target.infisicalCertificateId]);
        removed += 1;
      } catch (error) {
        const message = error instanceof Error ? error.message : "Unknown error";
        failures.push(message);
        await certificateSyncDAL.updateSyncStatus(
          pkiSync.id,
          target.infisicalCertificateId,
          CertificateSyncStatus.Failed,
          message
        );
      }
    }

    if (failures.length) {
      throw new PkiSyncError({
        shouldRetry: false,
        message: `Failed to remove ${failures.length} certificate(s) from GCP Certificate Manager: ${failures.join("; ")}`,
        context: { totalCertificates: targets.length, failedCount: failures.length }
      });
    }

    return { removed, failed: 0, skipped: certificateNames.length - targets.length };
  };

  return {
    syncCertificates,
    removeCertificates
  };
};