add.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SSHAddCommand = 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 fs = tslib_1.__importStar(require("fs"));
  9. const os = tslib_1.__importStar(require("os"));
  10. const path = tslib_1.__importStar(require("path"));
  11. const guards_1 = require("../../guards");
  12. const color_1 = require("../../lib/color");
  13. const errors_1 = require("../../lib/errors");
  14. const executor_1 = require("../../lib/executor");
  15. const base_1 = require("./base");
  16. class SSHAddCommand extends base_1.SSHBaseCommand {
  17. async getMetadata() {
  18. return {
  19. name: 'add',
  20. type: 'global',
  21. summary: 'Add an SSH public key to Ionic',
  22. inputs: [
  23. {
  24. name: 'pubkey-path',
  25. summary: 'Location of public key file to add to Ionic',
  26. validators: [cli_framework_1.validators.required],
  27. },
  28. ],
  29. options: [
  30. {
  31. name: 'use',
  32. summary: 'Use the newly added key as your default SSH key for Ionic',
  33. type: Boolean,
  34. },
  35. ],
  36. groups: ["deprecated" /* MetadataGroup.DEPRECATED */],
  37. };
  38. }
  39. async preRun(inputs, options) {
  40. if (!inputs[0]) {
  41. const defaultPubkeyPath = path.resolve(os.homedir(), '.ssh', 'id_rsa.pub');
  42. const defaultPubkeyExists = await (0, utils_fs_1.pathAccessible)(defaultPubkeyPath, fs.constants.R_OK);
  43. const pubkeyPath = await this.env.prompt({
  44. type: 'input',
  45. name: 'pubkeyPath',
  46. message: 'Enter the location to your public key file to upload to Ionic:',
  47. default: defaultPubkeyExists ? (0, utils_terminal_1.prettyPath)(defaultPubkeyPath) : undefined,
  48. });
  49. inputs[0] = pubkeyPath;
  50. }
  51. }
  52. async run(inputs, options, runinfo) {
  53. const { ERROR_SSH_INVALID_PUBKEY, SSHKeyClient, parsePublicKeyFile } = await Promise.resolve().then(() => tslib_1.__importStar(require('../../lib/ssh')));
  54. const pubkeyPath = (0, utils_terminal_1.expandPath)(inputs[0]);
  55. const pubkeyName = (0, utils_terminal_1.prettyPath)(pubkeyPath);
  56. let pubkey;
  57. try {
  58. [pubkey] = await parsePublicKeyFile(pubkeyPath);
  59. }
  60. catch (e) {
  61. if (e.code === 'ENOENT') {
  62. throw new errors_1.FatalException(`${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(pubkeyPath))} does not appear to exist. Please specify a valid SSH public key.\n` +
  63. `If you are having issues, try using ${(0, color_1.input)('ionic ssh setup')}.`);
  64. }
  65. else if (e === ERROR_SSH_INVALID_PUBKEY) {
  66. throw new errors_1.FatalException(`${(0, color_1.strong)(pubkeyName)} does not appear to be a valid SSH public key. (Not in ${(0, color_1.strong)('authorized_keys')} file format.)\n` +
  67. `If you are having issues, try using ${(0, color_1.input)('ionic ssh setup')}.`);
  68. }
  69. throw e;
  70. }
  71. const user = this.env.session.getUser();
  72. const token = await this.env.session.getUserToken();
  73. const sshkeyClient = new SSHKeyClient({ client: this.env.client, token, user });
  74. try {
  75. const key = await sshkeyClient.create({ pubkey });
  76. this.env.log.ok(`Your public key (${(0, color_1.strong)(key.fingerprint)}) has been added to Ionic!`);
  77. }
  78. catch (e) {
  79. if ((0, guards_1.isSuperAgentError)(e) && e.response.status === 409) {
  80. this.env.log.msg('Pubkey already added to Ionic.');
  81. }
  82. else {
  83. throw e;
  84. }
  85. }
  86. if (pubkeyPath.endsWith('.pub')) {
  87. let confirm = options['use'];
  88. if (!confirm) {
  89. confirm = await this.env.prompt({
  90. type: 'confirm',
  91. name: 'confirm',
  92. message: 'Would you like to use this key as your default for Ionic?',
  93. });
  94. }
  95. if (confirm) {
  96. const keyPath = pubkeyPath.substring(0, pubkeyPath.length - 4); // corresponding private key, theoretically
  97. const keyExists = await (0, utils_fs_1.pathExists)(keyPath);
  98. if (keyExists) {
  99. await (0, executor_1.runCommand)(runinfo, ['ssh', 'use', (0, utils_terminal_1.prettyPath)(keyPath)]);
  100. }
  101. else {
  102. this.env.log.error(`SSH key does not exist: ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))}.\n` +
  103. `Please use ${(0, color_1.input)('ionic ssh use')} manually to use the corresponding private key.`);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. exports.SSHAddCommand = SSHAddCommand;