generate.js 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SSHGenerateCommand = void 0;
  4. const tslib_1 = require("tslib");
  5. const cli_framework_1 = require("@ionic/cli-framework");
  6. const utils_fs_1 = require("@ionic/utils-fs");
  7. const utils_terminal_1 = require("@ionic/utils-terminal");
  8. const path = tslib_1.__importStar(require("path"));
  9. const color_1 = require("../../lib/color");
  10. const base_1 = require("./base");
  11. const SSH_KEY_TYPES = ['ecdsa', 'ed25519', 'rsa'];
  12. class SSHGenerateCommand extends base_1.SSHBaseCommand {
  13. async getMetadata() {
  14. return {
  15. name: 'generate',
  16. type: 'global',
  17. summary: 'Generates a private and public SSH key pair',
  18. inputs: [
  19. {
  20. name: 'key-path',
  21. summary: 'Destination of private key file',
  22. },
  23. ],
  24. options: [
  25. {
  26. name: 'type',
  27. summary: `The type of key to generate: ${SSH_KEY_TYPES.map(v => (0, color_1.input)(v)).join(', ')}`,
  28. default: 'rsa',
  29. aliases: ['t'],
  30. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  31. },
  32. {
  33. name: 'bits',
  34. summary: 'Number of bits in the key',
  35. aliases: ['b'],
  36. default: '2048',
  37. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  38. },
  39. {
  40. name: 'annotation',
  41. summary: 'Annotation (comment) in public key. Your Ionic email address will be used',
  42. aliases: ['C'],
  43. groups: ["advanced" /* MetadataGroup.ADVANCED */],
  44. },
  45. ],
  46. groups: ["deprecated" /* MetadataGroup.DEPRECATED */],
  47. };
  48. }
  49. async preRun(inputs, options) {
  50. await this.checkForOpenSSH();
  51. await this.env.session.getUserToken();
  52. if (!options['annotation']) {
  53. options['annotation'] = this.env.config.get('user.email');
  54. }
  55. (0, cli_framework_1.validate)(String(options['type']), 'type', [(0, cli_framework_1.contains)(SSH_KEY_TYPES, { caseSensitive: false })]);
  56. }
  57. async run(inputs, options) {
  58. const { getGeneratedPrivateKeyPath } = await Promise.resolve().then(() => tslib_1.__importStar(require('../../lib/ssh')));
  59. const { bits, annotation } = options;
  60. const keyPath = inputs[0] ? (0, utils_terminal_1.expandPath)(String(inputs[0])) : await getGeneratedPrivateKeyPath(this.env.config.get('user.id'));
  61. const keyPathDir = path.dirname(keyPath);
  62. const pubkeyPath = `${keyPath}.pub`;
  63. if (!(await (0, utils_fs_1.pathExists)(keyPathDir))) {
  64. await (0, utils_fs_1.mkdirp)(keyPathDir, 0o700);
  65. this.env.log.msg(`Created ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPathDir))} directory for you.`);
  66. }
  67. if (await (0, utils_fs_1.pathExists)(keyPath)) {
  68. const confirm = await this.env.prompt({
  69. type: 'confirm',
  70. name: 'confirm',
  71. message: `Key ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))} exists. Overwrite?`,
  72. });
  73. if (confirm) {
  74. await (0, utils_fs_1.unlink)(keyPath);
  75. }
  76. else {
  77. this.env.log.msg(`Not overwriting ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))}.`);
  78. return;
  79. }
  80. }
  81. this.env.log.info('Enter a passphrase for your private key.\n' +
  82. `You will be prompted to provide a ${(0, color_1.strong)('passphrase')}, which is used to protect your private key should you lose it. (If someone has your private key, they can impersonate you!) Passphrases are recommended, but not required.\n`);
  83. await this.env.shell.run('ssh-keygen', ['-q', '-t', String(options['type']), '-b', String(bits), '-C', String(annotation), '-f', keyPath], { stdio: 'inherit', showCommand: false, showError: false });
  84. this.env.log.nl();
  85. this.env.log.rawmsg(`Private Key (${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))}): Keep this safe!\n` +
  86. `Public Key (${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(pubkeyPath))}): Give this to all your friends!\n\n`);
  87. this.env.log.ok('A new pair of SSH keys has been generated!');
  88. this.env.log.nl();
  89. this.env.log.msg(`${(0, color_1.strong)('Next steps:')}\n` +
  90. ` * Add your public key to Ionic: ${(0, color_1.input)('ionic ssh add ' + (0, utils_terminal_1.prettyPath)(pubkeyPath))}\n` +
  91. ` * Use your private key for secure communication with Ionic: ${(0, color_1.input)('ionic ssh use ' + (0, utils_terminal_1.prettyPath)(keyPath))}`);
  92. }
  93. }
  94. exports.SSHGenerateCommand = SSHGenerateCommand;