refactor(server): throw error when authentication fails

This commit is contained in:
Mauricio Siu
2024-09-20 00:15:25 -06:00
parent e90b98e629
commit ee5516bb91
2 changed files with 54 additions and 34 deletions

View File

@@ -24,41 +24,43 @@ export const execAsyncRemote = async (
conn conn
.once("ready", () => { .once("ready", () => {
console.log("Client :: ready"); console.log("Client :: ready");
conn conn.exec(command, (err, stream) => {
.exec(command, (err, stream) => { if (err) throw err;
if (err) throw err; stream
stream .on("close", (code: number, signal: string) => {
.on("close", (code: number, signal: string) => { console.log(
console.log( `Stream :: close :: code: ${code}, signal: ${signal}`,
`Stream :: close :: code: ${code}, signal: ${signal}`, );
conn.end();
if (code === 0) {
resolve({ stdout, stderr });
} else {
reject(
new Error(
`Command exited with code ${code}. Stderr: ${stderr}, command: ${command}`,
),
); );
conn.end(); }
if (code === 0) { })
resolve({ stdout, stderr }); .on("data", (data: string) => {
} else { stdout += data.toString();
reject( })
new Error( .stderr.on("data", (data) => {
`Command exited with code ${code}. Stderr: ${stderr}, command: ${command}`, stderr += data.toString();
), });
); });
} })
}) .on("error", (err) => {
.on("data", (data: string) => { conn.end();
stdout += data.toString(); if (err.level === "client-authentication") {
}) reject(
.stderr.on("data", (data) => { new Error(
stderr += data.toString(); `Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
}); ),
}) );
.on("keyboard-interactive", () => { } else {
console.log("Warning: Keyboard interactive, closing connection"); reject(new Error(`SSH connection error: ${err.message}`));
conn.end(); }
reject(
new Error(
"Password requested. Invalid SSH key or authentication issue.",
),
);
});
}) })
.connect({ .connect({
host: server.ipAddress, host: server.ipAddress,

View File

@@ -107,6 +107,24 @@ const connectToServer = async (serverId: string, logPath: string) => {
}); });
}); });
}) })
.on("error", (err) => {
client.end();
if (err.level === "client-authentication") {
writeStream.write(
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
);
reject(
new Error(
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
),
);
} else {
writeStream.write(
`SSH connection error: ${err.message} ${err.level}`,
);
reject(new Error(`SSH connection error: ${err.message}`));
}
})
.connect({ .connect({
host: server.ipAddress, host: server.ipAddress,
port: server.port, port: server.port,