alwaysai
Version:
The alwaysAI command-line interface (CLI)
68 lines • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const child_process_1 = require("child_process");
const util_1 = require("util");
const ora = require("ora");
const constants_1 = require("../constants");
const alwayscli_1 = require("@alwaysai/alwayscli");
async function writePrivateKeyFileComponent() {
const spinner = ora(`Write private key file "${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}"`).start();
if (!fs_1.existsSync(constants_1.PRIVATE_KEY_FILE_PATH)) {
// ssh-keygen creates the private key file and the corresponding .pub file
try {
await util_1.promisify(child_process_1.execFile)('ssh-keygen', [
'-q',
'-b',
'2048',
'-t',
'rsa',
'-N',
'',
'-C',
'Generated by alwaysAI CLI',
'-f',
constants_1.PRIVATE_KEY_FILE_PATH,
]);
spinner.succeed();
}
catch (exception) {
spinner.fail();
throw exception;
}
}
else {
const stats = await util_1.promisify(fs_1.stat)(constants_1.PRIVATE_KEY_FILE_PATH);
if (!stats.isFile()) {
spinner.fail();
throw new alwayscli_1.TerseError(`"${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}" exists but is not a file. Please remove it and try again.`);
}
if ((stats.mode & 0o700) < 0o400) {
spinner.fail();
throw new alwayscli_1.TerseError(`"${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}" exists but is not user-readable. Please remove it or fix its permissions`);
}
if ((stats.mode & 0o077) !== 0) {
spinner.fail();
throw new alwayscli_1.TerseError(`"${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}" exists but is group- or world-readable. Please remove it or fix its permissions.`);
}
const keyMaterial = await util_1.promisify(fs_1.readFile)(constants_1.PRIVATE_KEY_FILE_PATH, {
encoding: 'utf8',
});
if (!keyMaterial.startsWith('-----BEGIN OPENSSH PRIVATE KEY-----')) {
spinner.fail();
throw new alwayscli_1.TerseError(`"${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}" exists but is not an OpenSSH private key. Please remove it and try again.`);
}
// Make sure here that the .pub half of the key is in place
if (!fs_1.existsSync(constants_1.PUBLIC_KEY_FILE_PATH)) {
const { stdout } = await util_1.promisify(child_process_1.execFile)('ssh-keygen', [
'-y',
'-f',
constants_1.PRIVATE_KEY_FILE_PATH,
]);
await util_1.promisify(fs_1.writeFile)(constants_1.PUBLIC_KEY_FILE_PATH, stdout);
}
spinner.succeed(`Found private key file "${constants_1.PRIVATE_KEY_FILE_PRETTY_PATH}"`);
}
}
exports.writePrivateKeyFileComponent = writePrivateKeyFileComponent;
//# sourceMappingURL=write-private-key-file-component.js.map