Skip to content

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

make discovery a single gateway protocol

xb3ccdafbddc0
diff --git a/backend/src/ee/services/pam-discovery/pam-discovery-fns.ts b/backend/src/ee/services/pam-discovery/pam-discovery-fns.ts
index 26050f296a..387694f8fa 100644
--- a/backend/src/ee/services/pam-discovery/pam-discovery-fns.ts
+++ b/backend/src/ee/services/pam-discovery/pam-discovery-fns.ts
@@ -1,9 +1,7 @@
-import net from "node:net";
-
import { BadRequestError } from "@app/lib/errors";
import { GatewayProxyProtocol } from "@app/lib/gateway";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
-import { callSshExec, SshExecCredentials } from "@app/lib/gateway-v2/ssh-rpc";
+import { callPortSweep, callSshExec, SshExecCredentials } from "@app/lib/gateway-v2/ssh-rpc";
import { verifyHostInputValidity } from "../dynamic-secret/dynamic-secret-fns";
import { TGatewayV2ServiceFactory } from "../gateway-v2/gateway-v2-service";
@@ -38,7 +36,7 @@ export const sshExecWithGateway = async (
return response.result.stdout;
},
{
- protocol: GatewayProxyProtocol.SshExec,
+ protocol: GatewayProxyProtocol.Discovery,
relayHost: platform.relayHost,
gateway: platform.gateway,
relay: platform.relay
@@ -88,37 +86,18 @@ export const sweepReachableTargets = async (
});
if (!platform) throw new BadRequestError({ message: "Unable to connect to gateway" });
- const request = `${JSON.stringify({ targets: targets.map((t) => `${t.host}:${t.port}`), timeoutMs: dialTimeoutMs })}\n`;
+ const hostPorts = targets.map((t) => `${t.host}:${t.port}`);
return withGatewayV2Proxy(
(proxyPort) =>
- new Promise<Set<string>>((resolve, reject) => {
- const socket = net.connect({ host: "127.0.0.1", port: proxyPort });
- let buffer = "";
- const timer = setTimeout(() => {
- socket.destroy();
- reject(new Error("Port sweep timed out"));
- }, SWEEP_RESPONSE_TIMEOUT_MS);
- const finish = (err: Error | null) => {
- clearTimeout(timer);
- socket.destroy();
- if (err) return reject(err);
- try {
- const parsed = JSON.parse(buffer) as { open?: string[] };
- return resolve(new Set(parsed.open ?? []));
- } catch {
- return reject(new Error("Invalid port sweep response"));
- }
- };
- socket.on("connect", () => socket.write(request));
- socket.on("data", (chunk) => {
- buffer += chunk.toString("utf-8");
- });
- socket.on("end", () => finish(null));
- socket.on("error", (err) => finish(err));
+ callPortSweep({
+ port: proxyPort,
+ targets: hostPorts,
+ dialTimeoutMs,
+ responseTimeoutMs: SWEEP_RESPONSE_TIMEOUT_MS
}),
{
- protocol: GatewayProxyProtocol.PortSweep,
+ protocol: GatewayProxyProtocol.Discovery,
relayHost: platform.relayHost,
gateway: platform.gateway,
relay: platform.relay,
diff --git a/backend/src/lib/gateway-v2/gateway-v2.ts b/backend/src/lib/gateway-v2/gateway-v2.ts
index 5b245e386f..1c69a7d241 100644
--- a/backend/src/lib/gateway-v2/gateway-v2.ts
+++ b/backend/src/lib/gateway-v2/gateway-v2.ts
@@ -98,8 +98,7 @@ export const createGatewayConnection = async (
[GatewayProxyProtocol.PamSessionCancellation]: ["infisical-pam-session-cancellation"],
[GatewayProxyProtocol.Pkcs11]: ["infisical-pkcs11"],
[GatewayProxyProtocol.Adcs]: ["infisical-adcs"],
- [GatewayProxyProtocol.PortSweep]: ["infisical-port-sweep"],
- [GatewayProxyProtocol.SshExec]: ["infisical-ssh-exec"]
+ [GatewayProxyProtocol.Discovery]: ["infisical-discovery"]
};
const tlsOptions: tls.ConnectionOptions = {
diff --git a/backend/src/lib/gateway-v2/ssh-rpc.ts b/backend/src/lib/gateway-v2/ssh-rpc.ts
index 4d5f6f27c8..85d9cbcb5e 100644
--- a/backend/src/lib/gateway-v2/ssh-rpc.ts
+++ b/backend/src/lib/gateway-v2/ssh-rpc.ts
@@ -18,7 +18,7 @@ export type SshExecResponse = SshExecSuccess | SshExecFailure;
export const SSH_EXEC_RPC_TIMEOUT_MS = 120_000;
-const MAX_SSH_EXEC_RESPONSE_BYTES = 16 * 1024 * 1024;
+const MAX_RPC_RESPONSE_BYTES = 16 * 1024 * 1024;
// calls the gateway's ssh-exec handler over the local relay proxy; the gateway performs the ssh login (any auth
// method, including certificates the node client can't present) and returns the command output
@@ -48,8 +48,8 @@ export const callSshExec = (args: {
let received = 0;
res.on("data", (chunk: Buffer) => {
received += chunk.length;
- if (received > MAX_SSH_EXEC_RESPONSE_BYTES) {
- res.destroy(new Error(`SSH exec response exceeded ${MAX_SSH_EXEC_RESPONSE_BYTES} bytes`));
+ if (received > MAX_RPC_RESPONSE_BYTES) {
+ res.destroy(new Error(`SSH exec response exceeded ${MAX_RPC_RESPONSE_BYTES} bytes`));
return;
}
chunks.push(chunk);
@@ -97,3 +97,70 @@ export const callSshExec = (args: {
req.end();
});
};
+
+// calls the gateway's port-sweep handler over the local relay proxy; the gateway TCP-probes every target
+// in-network and returns the reachable "host:port" set, so a scan can filter a whole CIDR to live hosts
+export const callPortSweep = (args: {
+ port: number;
+ targets: string[];
+ dialTimeoutMs: number;
+ responseTimeoutMs: number;
+}): Promise<Set<string>> => {
+ const payload = JSON.stringify({ targets: args.targets, timeoutMs: args.dialTimeoutMs });
+ return new Promise<Set<string>>((resolve, reject) => {
+ const req = http.request(
+ {
+ host: "127.0.0.1",
+ port: args.port,
+ path: "/v1/sweep",
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "Content-Length": Buffer.byteLength(payload).toString(),
+ Connection: "close"
+ },
+ timeout: args.responseTimeoutMs
+ },
+ (res) => {
+ const chunks: Buffer[] = [];
+ let received = 0;
+ res.on("data", (chunk: Buffer) => {
+ received += chunk.length;
+ if (received > MAX_RPC_RESPONSE_BYTES) {
+ res.destroy(new Error(`Port sweep response exceeded ${MAX_RPC_RESPONSE_BYTES} bytes`));
+ return;
+ }
+ chunks.push(chunk);
+ });
+ res.on("end", () => {
+ const status = res.statusCode ?? 0;
+ const text = Buffer.concat(chunks).toString("utf8");
+ if (status < 200 || status >= 300) {
+ const errEnv = (() => {
+ try {
+ return (JSON.parse(text) as { error?: { message?: string } }).error;
+ } catch {
+ return undefined;
+ }
+ })();
+ reject(new Error(errEnv?.message ?? `Port sweep returned HTTP ${status}`));
+ return;
+ }
+ try {
+ const parsed = JSON.parse(text) as { open?: string[] };
+ resolve(new Set(parsed.open ?? []));
+ } catch {
+ reject(new Error("Invalid port sweep response"));
+ }
+ });
+ res.on("error", reject);
+ }
+ );
+ req.on("timeout", () => {
+ req.destroy(new Error(`Port sweep timed out after ${args.responseTimeoutMs}ms`));
+ });
+ req.on("error", reject);
+ req.write(payload);
+ req.end();
+ });
+};
diff --git a/backend/src/lib/gateway/types.ts b/backend/src/lib/gateway/types.ts
index af807b5821..c492157d15 100644
--- a/backend/src/lib/gateway/types.ts
+++ b/backend/src/lib/gateway/types.ts
@@ -14,8 +14,7 @@ export enum GatewayProxyProtocol {
PamSessionCancellation = "pam-session-cancellation",
Pkcs11 = "pkcs11",
Adcs = "adcs",
- PortSweep = "port-sweep",
- SshExec = "ssh-exec"
+ Discovery = "discovery"
}
export enum GatewayHttpProxyActions {