Skip to content

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

4.7 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 { BadRequestError, InternalServerError } from "@app/lib/errors";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { GatewayProxyProtocol } from "@app/lib/gateway-v2/types";
import { callWinRmEndpoint, WinRmRpcEndpoint } from "@app/lib/gateway-v2/winrm-rpc";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";

import { WinRMConnectionMethod } from "./winrm-connection-enums";
import { TWinRMConnectionConfig } from "./winrm-connection-types";

export type TWinRMGatewayDeps = {
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
  gatewayPoolService?: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">;
};

export type TWinRMCredentials = {
  host: string;
  port: number;
  username: string;
  password: string;
  sslEnabled?: boolean;
  sslRejectUnauthorized?: boolean;
  sslCertificate?: string;
};

export const getWinRMConnectionListItem = () => ({
  name: "Windows (WinRM)" as const,
  app: AppConnection.WinRM as const,
  methods: Object.values(WinRMConnectionMethod) as [WinRMConnectionMethod.UsernamePassword]
});

/**
 * Runs a single WinRM operation on the gateway that sits inside the customer
 * network. Node cannot perform WinRM message sealing, so delivery always runs on
 * the gateway (Go/masterzen) and a gateway is always required for this connection.
 * The target host/port travel in the signed routing extension; only the transport
 * flags and operation payload go in the request body.
 */
export const executeWinRMGatewayOperation = async <T>(
  args: {
    gatewayId?: string | null;
    gatewayPoolId?: string | null;
    credentials: TWinRMCredentials;
    endpoint: WinRmRpcEndpoint;
    params?: Record<string, unknown>;
  },
  deps: TWinRMGatewayDeps
): Promise<T> => {
  const { gatewayV2Service, gatewayPoolService } = deps;

  if (args.gatewayPoolId && !gatewayPoolService) {
    throw new InternalServerError({ message: "Pool-backed connections require gatewayPoolService at the call site" });
  }

  const gatewayId =
    args.gatewayPoolId && gatewayPoolService
      ? await gatewayPoolService.resolveEffectiveGatewayId({
          gatewayId: args.gatewayId,
          gatewayPoolId: args.gatewayPoolId
        })
      : args.gatewayId;

  if (!gatewayId) {
    throw new BadRequestError({
      message: "Windows (WinRM) connections require a gateway to reach the host."
    });
  }

  await blockLocalAndPrivateIpAddresses(`winrm://${args.credentials.host}:${args.credentials.port}`, true);

  const connectionDetails = await gatewayV2Service.getPlatformConnectionDetailsByGatewayId({
    gatewayId,
    targetHost: args.credentials.host,
    targetPort: args.credentials.port
  });
  if (!connectionDetails) {
    throw new BadRequestError({ message: "Unable to connect to gateway, no platform connection details found" });
  }

  const response = await withGatewayV2Proxy(
    async (port) =>
      callWinRmEndpoint<T>({
        port,
        endpoint: args.endpoint,
        body: {
          username: args.credentials.username,
          password: args.credentials.password,
          params: {
            useHttps: args.credentials.sslEnabled ?? false,
            insecure: args.credentials.sslEnabled ? args.credentials.sslRejectUnauthorized === false : false,
            ...(args.credentials.sslEnabled && args.credentials.sslCertificate
              ? { caCertificate: args.credentials.sslCertificate }
              : {}),
            ...args.params
          }
        }
      }),
    {
      protocol: GatewayProxyProtocol.WinRm,
      ...connectionDetails
    }
  );

  if (!response.ok) {
    throw new BadRequestError({ message: `WinRM gateway operation failed: ${response.errorMessage ?? ""}` });
  }
  return response.result;
};

export const validateWinRMConnectionCredentials = async (
  config: TWinRMConnectionConfig,
  gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">
) => {
  try {
    await executeWinRMGatewayOperation<{ ok: boolean }>(
      {
        gatewayId: config.gatewayId,
        gatewayPoolId: config.gatewayPoolId,
        credentials: config.credentials,
        endpoint: WinRmRpcEndpoint.Test
      },
      { gatewayV2Service }
    );

    return config.credentials;
  } catch (error) {
    throw new BadRequestError({
      message: `Unable to validate connection: ${
        (error as Error)?.message?.replaceAll(config.credentials.password, "********************") ??
        "verify credentials"
      }`
    });
  }
};