Skip to content

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

Address veria comment

Carlos Monastyrski5df9e9ccff04
diff --git a/backend/src/services/app-connection/ssh/ssh-connection-fns.ts b/backend/src/services/app-connection/ssh/ssh-connection-fns.ts
index 84986a22b5..45fce9bdbe 100644
--- a/backend/src/services/app-connection/ssh/ssh-connection-fns.ts
+++ b/backend/src/services/app-connection/ssh/ssh-connection-fns.ts
@@ -355,6 +355,65 @@ export const executeSshCommandViaGateway = async (
return response.result;
};
+const MAX_SSH_COMMAND_OUTPUT_BYTES = 16 * 1024 * 1024;
+
+const execOverSshClient = (client: Client, command: string, timeoutMs: number): Promise<SshExecResult> =>
+ new Promise((resolve, reject) => {
+ let settled = false;
+ let timer: NodeJS.Timeout;
+ const finish = (fn: () => void) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ fn();
+ };
+
+ timer = setTimeout(
+ () => finish(() => reject(new Error(`Command did not finish within ${Math.round(timeoutMs / 1000)}s`))),
+ timeoutMs
+ );
+
+ client.exec(command, (err, stream) => {
+ if (err) {
+ finish(() => reject(err));
+ return;
+ }
+
+ let stdout = "";
+ let stderr = "";
+ let exitCode = 0;
+ const append = (current: string, chunk: Buffer) =>
+ current.length >= MAX_SSH_COMMAND_OUTPUT_BYTES ? current : current + chunk.toString("utf8");
+
+ stream.on("data", (chunk: Buffer) => {
+ stdout = append(stdout, chunk);
+ });
+ stream.stderr.on("data", (chunk: Buffer) => {
+ stderr = append(stderr, chunk);
+ });
+ stream.on("exit", (code: number | null) => {
+ exitCode = typeof code === "number" ? code : 1;
+ });
+ stream.on("close", () => finish(() => resolve({ stdout, stderr, exitCode })));
+ });
+ });
+
+export const executeSshCommand = async (
+ config: TSshConnectionConfig,
+ gatewayServices: TSshGatewayServices,
+ args: { command: string; timeoutMs: number },
+ options?: TSshConnectionOptions
+): Promise<SshExecResult> => {
+ if (!options?.expectedHostKeys) return executeSshCommandViaGateway(config, gatewayServices, args);
+
+ return withSshConnection(
+ config,
+ gatewayServices,
+ (client) => execOverSshClient(client, args.command, args.timeoutMs),
+ options
+ );
+};
+
export const validateSshConnectionCredentials = async (
config: TSshConnectionConfig,
_gatewayService: Pick<TGatewayServiceFactory, "fnGetGatewayClientTlsByGatewayId">,
diff --git a/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts b/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts
index 7acf65ee5f..cd7cdfa759 100644
--- a/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts
@@ -15,7 +15,11 @@ import {
resolveLdapBackedHostCredentials
} from "@app/services/app-connection/ldap/ldap-directory-fns";
import { SshConnectionMethod } from "@app/services/app-connection/ssh/ssh-connection-enums";
-import { executeSshCommandViaGateway, withSshConnection } from "@app/services/app-connection/ssh/ssh-connection-fns";
+import {
+ executeSshCommand,
+ TSshConnectionOptions,
+ withSshConnection
+} from "@app/services/app-connection/ssh/ssh-connection-fns";
import { TSshConnectionConfig } from "@app/services/app-connection/ssh/ssh-connection-types";
import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certificate-sync-dal";
import { TSyncMetadata } from "@app/services/certificate-sync/certificate-sync-schemas";
@@ -311,12 +315,18 @@ const executeLinuxServerHostCommand = (
kind: HostCommandKind,
plan: { command: string; context: THostCommandContext },
sshConfig: TSshConnectionConfig,
- gatewayServices: Pick<TLinuxServerPkiSyncFactoryDeps, "gatewayV2Service" | "gatewayPoolService">
+ gatewayServices: Pick<TLinuxServerPkiSyncFactoryDeps, "gatewayV2Service" | "gatewayPoolService">,
+ pinOptions?: TSshConnectionOptions
) =>
- executeSshCommandViaGateway(sshConfig, gatewayServices, {
- command: renderHostCommandContext(plan.command, plan.context, toPosixShellLiteral),
- timeoutMs: HOST_COMMAND_TIMEOUT_MS[kind]
- });
+ executeSshCommand(
+ sshConfig,
+ gatewayServices,
+ {
+ command: renderHostCommandContext(plan.command, plan.context, toPosixShellLiteral),
+ timeoutMs: HOST_COMMAND_TIMEOUT_MS[kind]
+ },
+ pinOptions
+ );
const runLinuxServerHealthCheckCommand = async ({
pkiSync,
@@ -351,7 +361,13 @@ const runLinuxServerHealthCheckCommand = async ({
secretsToRedact: [plan.context.pkcs12Password],
execute: async () => {
if (sshConfig instanceof Error) throw sshConfig;
- return executeLinuxServerHostCommand(HostCommandKind.HealthCheck, plan, sshConfig, gatewayServices);
+ return executeLinuxServerHostCommand(
+ HostCommandKind.HealthCheck,
+ plan,
+ sshConfig,
+ gatewayServices,
+ sshPinOptions(pkiSync)
+ );
}
});
};
@@ -360,17 +376,19 @@ const runLinuxServerPostSyncCommand = ({
syncId,
plan,
sshConfig,
- gatewayServices
+ gatewayServices,
+ pinOptions
}: {
syncId: string;
plan: TPostSyncCommandPlan;
sshConfig: TSshConnectionConfig;
gatewayServices: Pick<TLinuxServerPkiSyncFactoryDeps, "gatewayV2Service" | "gatewayPoolService">;
+ pinOptions?: TSshConnectionOptions;
}) =>
runPostSyncCommand({
syncId,
secretsToRedact: [plan.context.pkcs12Password],
- execute: () => executeLinuxServerHostCommand(HostCommandKind.PostSync, plan, sshConfig, gatewayServices)
+ execute: () => executeLinuxServerHostCommand(HostCommandKind.PostSync, plan, sshConfig, gatewayServices, pinOptions)
});
export const linuxServerPkiSyncFactory = ({
@@ -546,7 +564,8 @@ export const linuxServerPkiSyncFactory = ({
syncId: pkiSync.id,
plan: postSyncCommandPlan,
sshConfig,
- gatewayServices: { gatewayV2Service, gatewayPoolService }
+ gatewayServices: { gatewayV2Service, gatewayPoolService },
+ pinOptions: sshPinOptions(pkiSync)
})
: undefined;