Skip to content

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

1.9 KB
Raw Blame
import { TGatewayPoolServiceFactory } from "@app/ee/services/gateway-pool/gateway-pool-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
import { logger } from "@app/lib/logger";
import { OrgServiceActor } from "@app/lib/types";
import { KvVersion } from "@app/services/external-migration/external-migration-types";

import { AppConnection } from "../app-connection-enums";
import { listHCVaultMounts } from "./hc-vault-connection-fns";
import { THCVaultConnection } from "./hc-vault-connection-types";

type TGetAppConnectionFunc = (
  app: AppConnection,
  connectionId: string,
  actor: OrgServiceActor
) => Promise<THCVaultConnection>;

export const hcVaultConnectionService = (
  getAppConnection: TGetAppConnectionFunc,
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
  gatewayPoolService: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">
) => {
  const listMounts = async (connectionId: string, actor: OrgServiceActor) => {
    const appConnection = await getAppConnection(AppConnection.HCVault, connectionId, actor);
    const effectiveGatewayId = await gatewayPoolService.resolveEffectiveGatewayId({
      gatewayId: appConnection.gatewayId,
      gatewayPoolId: appConnection.gatewayPoolId
    });
    const resolvedConnection = { ...appConnection, gatewayId: effectiveGatewayId, gatewayPoolId: null };

    try {
      const mounts = await listHCVaultMounts(resolvedConnection, gatewayV2Service);
      // Filter for KV mounts (v1 and v2) and extract just the paths
      return mounts
        .filter((mount) => mount.type === "kv" && (mount.version === KvVersion.V2 || mount.version === KvVersion.V1))
        .map((mount) => mount.path);
    } catch (error) {
      logger.error(error, "Failed to establish connection with Hashicorp Vault");
      return [];
    }
  };

  return {
    listMounts
  };
};