Skip to content

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

9.7 KB
Raw Blame
import { useQuery } from "@tanstack/react-query";

import { apiRequest } from "@app/config/request";

import { TCertificateTemplate } from "../certificateTemplates/types";
import { CaRenewalStatus, CaSigningConfigType, CaType } from "./enums";
import {
  TAdcsTemplate,
  TAzureAdCsTemplate,
  TCaQuota,
  TInternalCertificateAuthority,
  TUnifiedCertificateAuthority
} from "./types";

export const caKeys = {
  getCaById: (caId: string) => [{ caId }, "ca"],
  getCaByNameAndProjectId: (caName: string) => [{ caName }, "ca"],
  listCasByTypeAndProjectId: (type: CaType) => [{ type }, "cas"],
  listCasByProjectId: () => ["cas"] as const,
  listExternalCasByProjectId: () => ["external-cas"] as const,
  getCaCerts: (caId: string) => [{ caId }, "ca-cert"],
  getCaCrls: (caId: string) => [{ caId }, "ca-crls"],
  getCaCert: (caId: string) => [{ caId }, "ca-certificate"],
  getCaCsr: (caId: string) => [{ caId }, "ca-csr"],
  getCaCrl: (caId: string) => [{ caId }, "ca-crl"],
  getCaCertTemplates: (caId: string) => [{ caId }, "ca-cert-templates"],
  getCaEstConfig: (caId: string) => [{ caId }, "ca-est-config"],
  getAzureAdcsTemplates: (caId: string) => [{ caId }, "azure-adcs-templates"],
  getAdcsTemplates: (caId: string) => [{ caId }, "adcs-templates"],
  getCaSigningConfig: (caId: string) => [{ caId }, "ca-signing-config"],
  getCaAutoRenewal: (caId: string) => [{ caId }, "ca-auto-renewal"],
  getCaQuota: () => ["ca-quota"] as const
};

export const useGetCa = ({
  caId,
  type,
  options
}: {
  caId: string;
  type: CaType;
  options?: { enabled?: boolean };
}) => {
  return useQuery({
    queryKey: caKeys.getCaById(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<TUnifiedCertificateAuthority>(
        `/api/v1/cert-manager/ca/${type}/${caId}`
      );
      return data;
    },
    enabled: options?.enabled !== undefined ? options.enabled : Boolean(caId && type)
  });
};

export const useListCasByTypeAndProjectId = (type: CaType) => {
  return useQuery({
    queryKey: caKeys.listCasByTypeAndProjectId(type),
    queryFn: async () => {
      const { data } = await apiRequest.get<TUnifiedCertificateAuthority[]>(
        `/api/v1/cert-manager/ca/${type}`
      );

      return data;
    }
  });
};

export const useListCasByProjectId = () => {
  return useQuery({
    queryKey: caKeys.listCasByProjectId(),
    queryFn: async () => {
      const { data } = await apiRequest.get<{
        certificateAuthorities: TUnifiedCertificateAuthority[];
      }>("/api/v1/cert-manager/ca");

      return data.certificateAuthorities;
    }
  });
};

export const useListExternalCasByProjectId = () => {
  return useQuery({
    queryKey: caKeys.listExternalCasByProjectId(),
    queryFn: async () => {
      const [
        acmeResponse,
        azureAdCsResponse,
        adcsResponse,
        awsPcaResponse,
        digicertResponse,
        awsAcmPublicCaResponse,
        venafiTppResponse,
        godaddyResponse
      ] = await Promise.allSettled([
        apiRequest.get<TUnifiedCertificateAuthority[]>(`/api/v1/cert-manager/ca/${CaType.ACME}`),
        apiRequest.get<TUnifiedCertificateAuthority[]>(
          `/api/v1/cert-manager/ca/${CaType.AZURE_AD_CS}`
        ),
        apiRequest.get<TUnifiedCertificateAuthority[]>(`/api/v1/cert-manager/ca/${CaType.ADCS}`),
        apiRequest.get<TUnifiedCertificateAuthority[]>(`/api/v1/cert-manager/ca/${CaType.AWS_PCA}`),
        apiRequest.get<TUnifiedCertificateAuthority[]>(
          `/api/v1/cert-manager/ca/${CaType.DIGICERT}`
        ),
        apiRequest.get<TUnifiedCertificateAuthority[]>(
          `/api/v1/cert-manager/ca/${CaType.AWS_ACM_PUBLIC_CA}`
        ),
        apiRequest.get<TUnifiedCertificateAuthority[]>(
          `/api/v1/cert-manager/ca/${CaType.VENAFI_TPP}`
        ),
        apiRequest.get<TUnifiedCertificateAuthority[]>(`/api/v1/cert-manager/ca/${CaType.GODADDY}`)
      ]);

      const allCas: TUnifiedCertificateAuthority[] = [];

      if (acmeResponse.status === "fulfilled") {
        allCas.push(...acmeResponse.value.data);
      }

      if (azureAdCsResponse.status === "fulfilled") {
        allCas.push(...azureAdCsResponse.value.data);
      }

      if (adcsResponse.status === "fulfilled") {
        allCas.push(...adcsResponse.value.data);
      }

      if (awsPcaResponse.status === "fulfilled") {
        allCas.push(...awsPcaResponse.value.data);
      }

      if (digicertResponse.status === "fulfilled") {
        allCas.push(...digicertResponse.value.data);
      }

      if (awsAcmPublicCaResponse.status === "fulfilled") {
        allCas.push(...awsAcmPublicCaResponse.value.data);
      }

      if (venafiTppResponse.status === "fulfilled") {
        allCas.push(...venafiTppResponse.value.data);
      }

      if (godaddyResponse.status === "fulfilled") {
        allCas.push(...godaddyResponse.value.data);
      }

      return allCas;
    }
  });
};

export const useGetInternalCaById = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaById(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<TInternalCertificateAuthority>(
        `/api/v1/cert-manager/ca/internal/${caId}`
      );
      return data;
    },
    enabled: Boolean(caId)
  });
};

export const useGetCaCerts = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaCerts(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<
        {
          certificate: string;
          certificateChain: string;
          serialNumber: string;
          version: number;
        }[]
      >(`/api/v1/cert-manager/ca/internal/${caId}/ca-certificates`);
      return data;
    },
    enabled: Boolean(caId)
  });
};

export const useGetCaCert = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaCert(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<{
        certificate: string;
        certificateChain: string;
        serialNumber: string;
      }>(`/api/v1/cert-manager/ca/internal/${caId}/certificate`);
      return data;
    },
    enabled: Boolean(caId)
  });
};

export const useGetCaCsr = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaCsr(caId),
    queryFn: async () => {
      const {
        data: { csr }
      } = await apiRequest.get<{
        csr: string;
      }>(`/api/v1/cert-manager/ca/internal/${caId}/csr`);
      return csr;
    },
    enabled: Boolean(caId)
  });
};

export const useGetCaCrls = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaCrls(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<
        {
          id: string;
          crl: string;
        }[]
      >(`/api/v1/cert-manager/ca/internal/${caId}/crls`);
      return data;
    },
    enabled: Boolean(caId)
  });
};

// TODO: DEPRECATE
export const useGetCaCertTemplates = (caId: string) => {
  return useQuery({
    queryKey: caKeys.getCaCertTemplates(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<{
        certificateTemplates: TCertificateTemplate[];
      }>(`/api/v1/pki/ca/${caId}/certificate-templates`);
      return data;
    },
    enabled: Boolean(caId)
  });
};

export type TCaSigningConfig = {
  id: string;
  caId: string;
  type: CaSigningConfigType;
  parentCaId: string | null;
  appConnectionId: string | null;
  destinationConfig: Record<string, unknown> | null;
  lastExternalCertificateId: string | null;
  createdAt: string;
  updatedAt: string;
};

export type TCaAutoRenewalConfig = {
  autoRenewalEnabled: boolean;
  autoRenewalDaysBeforeExpiry: number | null;
  lastRenewalStatus: CaRenewalStatus | null;
  lastRenewalMessage: string | null;
  lastRenewalAt: string | null;
};

export const useGetCaSigningConfig = (caId: string, options?: { enabled?: boolean }) => {
  return useQuery({
    queryKey: caKeys.getCaSigningConfig(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<TCaSigningConfig | null>(
        `/api/v1/cert-manager/ca/internal/${caId}/signing-config`
      );
      return data;
    },
    enabled: options?.enabled !== undefined ? options.enabled : Boolean(caId)
  });
};

export const useGetCaAutoRenewal = (caId: string, options?: { enabled?: boolean }) => {
  return useQuery({
    queryKey: caKeys.getCaAutoRenewal(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<TCaAutoRenewalConfig>(
        `/api/v1/cert-manager/ca/internal/${caId}/auto-renewal`
      );
      return data;
    },
    enabled: options?.enabled !== undefined ? options.enabled : Boolean(caId),
    refetchInterval: (query) => {
      const { data } = query.state;
      if (data?.lastRenewalStatus === CaRenewalStatus.PENDING) return 10_000;
      return false;
    }
  });
};

export const useGetAzureAdcsTemplates = ({
  caId,
  isAzureAdcsCa
}: {
  caId: string;
  isAzureAdcsCa: boolean;
}) => {
  return useQuery({
    queryKey: caKeys.getAzureAdcsTemplates(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<{
        templates: TAzureAdCsTemplate[];
      }>(`/api/v1/cert-manager/ca/azure-ad-cs/${caId}/templates`);
      return data;
    },
    enabled: Boolean(caId && isAzureAdcsCa)
  });
};

export const useGetAdcsTemplates = ({ caId, isAdcsCa }: { caId: string; isAdcsCa: boolean }) => {
  return useQuery({
    queryKey: caKeys.getAdcsTemplates(caId),
    queryFn: async () => {
      const { data } = await apiRequest.get<{
        templates: TAdcsTemplate[];
      }>(`/api/v1/cert-manager/ca/adcs/${caId}/templates`);
      return data;
    },
    enabled: Boolean(caId && isAdcsCa)
  });
};

export const useGetCaQuota = (options?: { enabled?: boolean }) =>
  useQuery({
    queryKey: caKeys.getCaQuota(),
    queryFn: async () => {
      const { data } = await apiRequest.get<TCaQuota>("/api/v1/cert-manager/ca/quota");
      return data;
    },
    enabled: options?.enabled ?? true
  });