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