Skip to content

Commit

Permalink
feat(core): private key not found error in ssh open
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 29, 2024
1 parent 5077189 commit 09fdf57
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
25 changes: 12 additions & 13 deletions packages/core/lib/actions/ssh/open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
},
Expand Down
14 changes: 11 additions & 3 deletions packages/core/test/actions/ssh/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 09fdf57

Please sign in to comment.