UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SSHKeyClient = exports.validatePrivateKey = exports.parsePublicKey = exports.parsePublicKeyFile = exports.getGeneratedPrivateKeyPath = exports.ERROR_SSH_INVALID_PRIVKEY = exports.ERROR_SSH_INVALID_PUBKEY = exports.ERROR_SSH_MISSING_PRIVKEY = void 0;
4const utils_fs_1 = require("@ionic/utils-fs");
5const os = require("os");
6const path = require("path");
7const guards_1 = require("../guards");
8const http_1 = require("./http");
9exports.ERROR_SSH_MISSING_PRIVKEY = 'SSH_MISSING_PRIVKEY';
10exports.ERROR_SSH_INVALID_PUBKEY = 'SSH_INVALID_PUBKEY';
11exports.ERROR_SSH_INVALID_PRIVKEY = 'SSH_INVALID_PRIVKEY';
12async function getGeneratedPrivateKeyPath(userId = 0) {
13 return path.resolve(os.homedir(), '.ssh', 'ionic', String(userId));
14}
15exports.getGeneratedPrivateKeyPath = getGeneratedPrivateKeyPath;
16async function parsePublicKeyFile(pubkeyPath) {
17 return parsePublicKey((await utils_fs_1.readFile(pubkeyPath, { encoding: 'utf8' })).trim());
18}
19exports.parsePublicKeyFile = parsePublicKeyFile;
20/**
21 * @return [full pubkey, algorithm, public numbers, annotation]
22 */
23function parsePublicKey(pubkey) {
24 const r = /^(ssh-[A-z0-9]+)\s([A-z0-9+\/=]+)\s?(.+)?$/.exec(pubkey);
25 if (!r) {
26 throw exports.ERROR_SSH_INVALID_PUBKEY;
27 }
28 if (!r[3]) {
29 r[3] = '';
30 }
31 r[1] = r[1].trim();
32 r[2] = r[2].trim();
33 r[3] = r[3].trim();
34 return [pubkey, r[1], r[2], r[3]];
35}
36exports.parsePublicKey = parsePublicKey;
37async function validatePrivateKey(keyPath) {
38 try {
39 await utils_fs_1.stat(keyPath);
40 }
41 catch (e) {
42 if (e.code === 'ENOENT') {
43 throw exports.ERROR_SSH_MISSING_PRIVKEY;
44 }
45 throw e;
46 }
47 const f = await utils_fs_1.readFile(keyPath, { encoding: 'utf8' });
48 const lines = f.split('\n');
49 if (!lines[0].match(/^\-{5}BEGIN [A-Z]+ PRIVATE KEY\-{5}$/)) {
50 throw exports.ERROR_SSH_INVALID_PRIVKEY;
51 }
52}
53exports.validatePrivateKey = validatePrivateKey;
54class SSHKeyClient extends http_1.ResourceClient {
55 constructor({ client, token, user }) {
56 super();
57 this.client = client;
58 this.token = token;
59 this.user = user;
60 }
61 async create({ pubkey }) {
62 const { req } = await this.client.make('POST', `/users/${this.user.id}/sshkeys`);
63 this.applyAuthentication(req, this.token);
64 req.send({ pubkey });
65 const res = await this.client.do(req);
66 if (!guards_1.isSSHKeyResponse(res)) {
67 throw http_1.createFatalAPIFormat(req, res);
68 }
69 return res.data;
70 }
71 async load(id) {
72 const { req } = await this.client.make('GET', `/users/${this.user.id}/sshkeys/${id}`);
73 this.applyAuthentication(req, this.token);
74 const res = await this.client.do(req);
75 if (!guards_1.isSSHKeyResponse(res)) {
76 throw http_1.createFatalAPIFormat(req, res);
77 }
78 return res.data;
79 }
80 async delete(id) {
81 const { req } = await this.client.make('DELETE', `/users/${this.user.id}/sshkeys/${id}`);
82 this.applyAuthentication(req, this.token);
83 await this.client.do(req);
84 }
85 paginate(args = {}) {
86 return this.client.paginate({
87 reqgen: async () => {
88 const { req } = await this.client.make('GET', `/users/${this.user.id}/sshkeys`);
89 this.applyAuthentication(req, this.token);
90 return { req };
91 },
92 guard: guards_1.isSSHKeyListResponse,
93 });
94 }
95}
96exports.SSHKeyClient = SSHKeyClient;