This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
ssh.js
100 lines (81 loc) · 2.88 KB
/
ssh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
import keypair from 'keypair';
import { pki, ssh } from 'node-forge';
import uuid from 'uuid';
import _ from 'lodash';
import * as Config from './config';
import { exists } from './fs';
import Log from './logger';
import keysList from '../commands/keys/list';
import keyCreate from '../commands/keys/add';
/**
* Generate an SSH key pair and save them at ~/.reaction/keys/
* @param {String} email The email found in the public key
* @return {Object} returns a results object: { publicKey: String, privateKey: String, title: String }
*/
export function generateKeyPair({ email }) {
if (!email) {
Log.error('An email is required to generate a keypair');
process.exit(1);
}
const pair = keypair();
const pub = pki.publicKeyFromPem(pair.public);
const priv = pki.privateKeyFromPem(pair.private);
const publicKey = ssh.publicKeyToOpenSSH(pub, email);
const privateKey = ssh.privateKeyToOpenSSH(priv);
const userHome = os.homedir();
const title = uuid.v1();
const publicKeyFile = path.resolve(`${userHome}/.reaction/keys/${title}.pub`);
const privateKeyFile = path.resolve(`${userHome}/.reaction/keys/${title}`);
fs.ensureFileSync(publicKeyFile);
fs.ensureFileSync(privateKeyFile);
fs.writeFileSync(publicKeyFile, publicKey);
fs.writeFileSync(privateKeyFile, privateKey);
fs.chmodSync(publicKeyFile, '400');
fs.chmodSync(privateKeyFile, '400');
Log.debug(`Generated new SSH key pair: ${title}`);
return {
publicKey,
privateKey,
title
};
}
/**
* Check if the user has a registered SSH key in ~/.reaction/keys/
* If so, set the GIT_SSH_COMMAND environment variable to use the private key's identity
* https://superuser.com/a/912281
* @return {String|null} returns the path to the chosen private key or null if none found
*/
export function setGitSSHKeyEnv() {
const homeDir = os.homedir();
const keys = Config.get('global', 'launchdock.keys', []);
const key = _.filter(keys, (k) => exists(path.resolve(`${homeDir}/.reaction/keys/${k.title}`)))[0];
if (!!key) {
const keyPath = path.resolve(`${homeDir}/.reaction/keys/${key.title}`);
process.env.GIT_SSH_COMMAND = `ssh -i ${keyPath}`;
Log.debug(`Setting SSH key identity to: ${keyPath}`);
return keyPath;
}
return null;
}
/**
* Check if the user has a registered SSH key in ~/.reaction/keys/
* @return {undefined} returns nothing
*/
export async function ensureSSHKeysExist() {
const keys = await keysList();
const homeDir = os.homedir();
let hasKey = false;
keys.forEach((k) => {
if (exists(`${homeDir}/.reaction/keys/${k.title}`)) {
hasKey = true;
}
});
if (keys.length === 0 || !hasKey) {
const email = Config.get('global', 'launchdock.email');
const keyPair = generateKeyPair({ email });
await keyCreate({ publicKey: keyPair.publicKey, title: keyPair.title });
}
}