Skip to content

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

feat: api corrected based on other schema

=385c5f47a5c0
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 bb3e69dff9..c729d0bb78 100644
--- a/backend/src/services/app-connection/ssh/ssh-connection-fns.ts
+++ b/backend/src/services/app-connection/ssh/ssh-connection-fns.ts
@@ -2,14 +2,14 @@ import { Client, ConnectConfig } from "ssh2";
import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";
import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";
-import { BadRequestError } from "@app/lib/errors";
+import { BadRequestError, InternalServerError } from "@app/lib/errors";
import { GatewayProxyProtocol } from "@app/lib/gateway";
import { withGatewayV2Proxy } from "@app/lib/gateway-v2/gateway-v2";
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
import { SshConnectionMethod } from "./ssh-connection-enums";
-import { TSshConnection, TSshConnectionConfig } from "./ssh-connection-types";
+import { TSshConnectionConfig } from "./ssh-connection-types";
const SSH_TIMEOUT = 15_000;
@@ -22,13 +22,23 @@ export const getSshConnectionListItem = () => {
};
export const getSshConnectionClient = async (
- credentials: TSshConnection["credentials"],
+ credentials: TSshConnectionConfig,
targetHost: string,
targetPort: number
): Promise<Client> => {
return new Promise((resolve, reject) => {
const client = new Client();
+ const connectConfig: ConnectConfig = {
+ host: targetHost,
+ port: targetPort,
+ readyTimeout: SSH_TIMEOUT,
+ tryKeyboard: false,
+ algorithms: {
+ serverHostKey: ["rsa-sha2-512", "rsa-sha2-256", "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp521"]
+ }
+ };
+
client.on("error", (err: Error) => {
client.destroy();
reject(new Error(`SSH Error: ${err.message}`));
@@ -43,20 +53,26 @@ export const getSshConnectionClient = async (
reject(new Error("SSH Connection Timeout"));
});
- const connectConfig: ConnectConfig = {
- host: targetHost,
- port: targetPort,
- username: credentials.username,
- readyTimeout: SSH_TIMEOUT
- };
-
- // Handle auth method using discriminated union
- if (credentials.authMethod === SshConnectionMethod.Password) {
- connectConfig.password = credentials.password;
- } else {
- connectConfig.privateKey = credentials.privateKey;
- if (credentials.passphrase) {
- connectConfig.passphrase = credentials.passphrase;
+ switch (credentials.method) {
+ case SshConnectionMethod.Password: {
+ const { credentials: inputCredentials } = credentials;
+ connectConfig.username = inputCredentials.username;
+ connectConfig.password = inputCredentials.password;
+ break;
+ }
+ case SshConnectionMethod.SshKey: {
+ const { credentials: inputCredentials } = credentials;
+ connectConfig.username = inputCredentials.username;
+ connectConfig.privateKey = inputCredentials.privateKey;
+ if (inputCredentials.passphrase) {
+ connectConfig.passphrase = inputCredentials.passphrase;
+ }
+ break;
+ }
+ default: {
+ throw new InternalServerError({
+ message: `Unhandled connection method: ${(credentials as { method: string }).method}`
+ });
}
}
@@ -109,7 +125,7 @@ export const validateSshConnectionCredentials = async (
) => {
try {
await executeWithPotentialGateway(config, gatewayV2Service, async (targetHost, targetPort) => {
- const client = await getSshConnectionClient(config.credentials, targetHost, targetPort);
+ const client = await getSshConnectionClient(config, targetHost, targetPort);
client.destroy(); // Clean up after successful connection
});
diff --git a/backend/src/services/app-connection/ssh/ssh-connection-schemas.ts b/backend/src/services/app-connection/ssh/ssh-connection-schemas.ts
index fe6b09e12c..a8a6deb2bf 100644
--- a/backend/src/services/app-connection/ssh/ssh-connection-schemas.ts
+++ b/backend/src/services/app-connection/ssh/ssh-connection-schemas.ts
@@ -11,44 +11,81 @@ import {
import { APP_CONNECTION_NAME_MAP } from "../app-connection-maps";
import { SshConnectionMethod } from "./ssh-connection-enums";
-// Base credentials schema with common fields
-const SshConnectionBaseCredentialsSchema = z.object({
+// Password method credentials schema
+export const SshConnectionPasswordCredentialsSchema = z.object({
host: z.string().trim().min(1, "Host required").describe(AppConnections.CREDENTIALS.SSH.host),
port: z.coerce.number().int().min(1).max(65535).describe(AppConnections.CREDENTIALS.SSH.port),
- username: z.string().trim().min(1, "Username required").describe(AppConnections.CREDENTIALS.SSH.username)
-});
-
-// Auth method specific schemas
-const SshPasswordAuthSchema = z.object({
- authMethod: z.literal(SshConnectionMethod.Password).describe(AppConnections.CREDENTIALS.SSH.authMethod),
+ username: z.string().trim().min(1, "Username required").describe(AppConnections.CREDENTIALS.SSH.username),
password: z.string().trim().min(1, "Password required").describe(AppConnections.CREDENTIALS.SSH.password)
});
-const SshKeyAuthSchema = z.object({
- authMethod: z.literal(SshConnectionMethod.SshKey).describe(AppConnections.CREDENTIALS.SSH.authMethod),
+// SSH Key method credentials schema
+export const SshConnectionSshKeyCredentialsSchema = z.object({
+ host: z.string().trim().min(1, "Host required").describe(AppConnections.CREDENTIALS.SSH.host),
+ port: z.coerce.number().int().min(1).max(65535).describe(AppConnections.CREDENTIALS.SSH.port),
+ username: z.string().trim().min(1, "Username required").describe(AppConnections.CREDENTIALS.SSH.username),
privateKey: z.string().trim().min(1, "Private key required").describe(AppConnections.CREDENTIALS.SSH.privateKey),
passphrase: z.string().trim().optional().describe(AppConnections.CREDENTIALS.SSH.passphrase)
});
-// Full credentials schema combining base + auth method discriminated union
-export const SshConnectionCredentialsSchema = SshConnectionBaseCredentialsSchema.and(
- z.discriminatedUnion("authMethod", [SshPasswordAuthSchema, SshKeyAuthSchema])
+// Validation schema for credentials - top-level method discrimination
+export const ValidateSshConnectionCredentialsSchema = z.discriminatedUnion("method", [
+ z.object({
+ method: z.literal(SshConnectionMethod.Password).describe(AppConnections.CREATE(AppConnection.SSH).method),
+ credentials: SshConnectionPasswordCredentialsSchema.describe(AppConnections.CREATE(AppConnection.SSH).credentials)
+ }),
+ z.object({
+ method: z.literal(SshConnectionMethod.SshKey).describe(AppConnections.CREATE(AppConnection.SSH).method),
+ credentials: SshConnectionSshKeyCredentialsSchema.describe(AppConnections.CREATE(AppConnection.SSH).credentials)
+ })
+]);
+
+// Create connection schema
+export const CreateSshConnectionSchema = ValidateSshConnectionCredentialsSchema.and(
+ GenericCreateAppConnectionFieldsSchema(AppConnection.SSH, {
+ supportsGateways: true
+ })
);
+// Update connection schema
+export const UpdateSshConnectionSchema = z
+ .object({
+ credentials: z
+ .union([SshConnectionPasswordCredentialsSchema, SshConnectionSshKeyCredentialsSchema])
+ .optional()
+ .describe(AppConnections.UPDATE(AppConnection.SSH).credentials)
+ })
+ .and(
+ GenericUpdateAppConnectionFieldsSchema(AppConnection.SSH, {
+ supportsGateways: true
+ })
+ );
+
// Base connection schema
const BaseSshConnectionSchema = BaseAppConnectionSchema.extend({
app: z.literal(AppConnection.SSH)
});
// Full connection schema
-export const SshConnectionSchema = BaseSshConnectionSchema.extend({
- method: z.literal(SshConnectionMethod.Password).or(z.literal(SshConnectionMethod.SshKey)),
- credentials: SshConnectionCredentialsSchema
-});
+export const SshConnectionSchema = z.intersection(
+ BaseSshConnectionSchema,
+ z.discriminatedUnion("method", [
+ z.object({
+ method: z.literal(SshConnectionMethod.Password),
+ credentials: SshConnectionPasswordCredentialsSchema
+ }),
+ z.object({
+ method: z.literal(SshConnectionMethod.SshKey),
+ credentials: SshConnectionSshKeyCredentialsSchema
+ })
+ ])
+);
-// Sanitized credentials schema (excludes password/privateKey/passphrase)
-const SanitizedSshConnectionCredentialsSchema = SshConnectionBaseCredentialsSchema.extend({
- authMethod: z.nativeEnum(SshConnectionMethod).describe(AppConnections.CREDENTIALS.SSH.authMethod)
+// Sanitized credentials schema (excludes sensitive fields)
+const SanitizedSshConnectionCredentialsSchema = z.object({
+ host: z.string(),
+ port: z.number(),
+ username: z.string()
});
// Sanitized schema (excludes sensitive fields) - must be discriminated union for router compatibility
@@ -63,35 +100,6 @@ export const SanitizedSshConnectionSchema = z.discriminatedUnion("method", [
}).describe(JSON.stringify({ title: `${APP_CONNECTION_NAME_MAP[AppConnection.SSH]} (SSH Key)` }))
]);
-// Validation schema for credentials
-export const ValidateSshConnectionCredentialsSchema = z.object({
- method: z
- .literal(SshConnectionMethod.Password)
- .or(z.literal(SshConnectionMethod.SshKey))
- .describe(AppConnections.CREATE(AppConnection.SSH).method),
- credentials: SshConnectionCredentialsSchema.describe(AppConnections.CREATE(AppConnection.SSH).credentials)
-});
-
-// Create connection schema
-export const CreateSshConnectionSchema = ValidateSshConnectionCredentialsSchema.and(
- GenericCreateAppConnectionFieldsSchema(AppConnection.SSH, {
- supportsGateways: true
- })
-);
-
-// Update connection schema
-export const UpdateSshConnectionSchema = z
- .object({
- credentials: SshConnectionCredentialsSchema.optional().describe(
- AppConnections.UPDATE(AppConnection.SSH).credentials
- )
- })
- .and(
- GenericUpdateAppConnectionFieldsSchema(AppConnection.SSH, {
- supportsGateways: true
- })
- );
-
// List item schema
export const SshConnectionListItemSchema = z
.object({