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,8 +24,7 @@ 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) => {
@@ -49,16 +48,19 @@ export const execAsyncRemote = async (
.stderr.on("data", (data) => { .stderr.on("data", (data) => {
stderr += data.toString(); stderr += data.toString();
}); });
});
}) })
.on("keyboard-interactive", () => { .on("error", (err) => {
console.log("Warning: Keyboard interactive, closing connection");
conn.end(); conn.end();
if (err.level === "client-authentication") {
reject( reject(
new Error( new Error(
"Password requested. Invalid SSH key or authentication issue.", `Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
), ),
); );
}); } else {
reject(new Error(`SSH connection error: ${err.message}`));
}
}) })
.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,