diff --git a/packages/core/lib/actions/ssh/open/index.js b/packages/core/lib/actions/ssh/open/index.js index e0b2f9c98..2b02de99e 100644 --- a/packages/core/lib/actions/ssh/open/index.js +++ b/packages/core/lib/actions/ssh/open/index.js @@ -25,10 +25,15 @@ export default { log("DEBUG", `Read Private Key from: ${config.private_key_path}`); const location = await utils.tilde.normalize(config.private_key_path); try { - ({ data: config.private_key } = await fs.readFile(location, "ascii")); + config.private_key = await fs + .readFile(location, "ascii") + .then(({ data }) => data); } catch (error) { if (error.code === "ENOENT") { - throw Error(`Private key doesnt exists: ${JSON.stringify(location)}`); + throw utils.error( + "NIKITA_SSH_OPEN_PRIVATE_KEY_NOT_FOUND", + `private key doesnt exists: ${JSON.stringify(location)}`, + ); } throw error; } @@ -40,12 +45,10 @@ export default { `Read Private Key: ${JSON.stringify(config.private_key_path)}`, ); const conn = await connect(config); - log("Connection is established"); - return { - ssh: conn, - }; + log("SSH connection is established"); + return { ssh: conn }; } catch { - log("WARN", "Connection failed"); + log("WARN", "SSH connection failed"); // Continue to bootstrap root access } // Enable root access @@ -62,14 +65,10 @@ export default { on_action: function ({ config }) { config.private_key ??= config.privateKey; // Define host from ip - if (config.ip && !config.host) { - config.host = config.ip; - } + config.host ??= config.ip; // Default root properties config.root ??= {}; - if (config.root.ip && !config.root.host) { - config.root.host = config.root.ip; - } + config.root.host ??= config.root.ip; config.root.host ??= config.host; config.root.port ??= config.port; }, diff --git a/packages/core/test/actions/ssh/open.js b/packages/core/test/actions/ssh/open.js index 79d3ccb6d..b888c93c0 100644 --- a/packages/core/test/actions/ssh/open.js +++ b/packages/core/test/actions/ssh/open.js @@ -6,14 +6,22 @@ import mochaThey from "mocha-they"; const they = mochaThey(test.config.filter(({ ssh }) => !!ssh)); describe("actions.ssh.open", function () { - describe("schema", function () { + describe("validation", function () { if (!test.tags.api) return; - they("config.host", function ({ ssh }) { + they("schema config.host", function ({ ssh }) { return nikita.ssh - .open({ ...ssh, host: "_invalid_", debug: undefined }) + .open({ ...ssh, host: "_invalid_" }) .should.be.rejectedWith({ code: "NIKITA_SCHEMA_VALIDATION_CONFIG" }); }); + + they("private key not found", function ({ ssh }) { + return nikita.ssh + .open({ ...ssh, private_key_path: "_invalid_" }) + .should.be.rejectedWith({ + code: "NIKITA_SSH_OPEN_PRIVATE_KEY_NOT_FOUND", + }); + }); }); describe("usage", function () {