-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenssl.js
More file actions
256 lines (227 loc) · 6.58 KB
/
openssl.js
File metadata and controls
256 lines (227 loc) · 6.58 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const fs = require('fs')
const path = require('path')
const ora = require('ora')
const execa = require('execa')
const chalk = require('chalk')
const keychain = require('./keychain')
const cosmiconfig = require('cosmiconfig')
const { IniSyncLoader } = require('./ini-parser')
const explorer = cosmiconfig('openssl', {
searchPlaces: [
'openssl.config.js',
'openssl.ini',
'.openssl',
'.openssl.yaml',
'webssl.config.js',
'webssl.ini',
'.webssl',
'.webssl.yaml',
'package.json',
],
loaders: {
'.ini': {
sync: IniSyncLoader,
},
},
})
class OpenSSL {
constructor(customCnf) {
this.cwd = process.cwd()
let config = {}
let pathToConfig = this.cwd
if (customCnf.config && fs.existsSync(customCnf.config)) {
pathToConfig = customCnf.config
}
if (customCnf.config !== false) {
const explored = explorer.searchSync(pathToConfig)
config = explored ? explored.config : {}
}
const defaults = {
bits: 2048,
filename: 'openssl',
addToKeychain: false,
removeOld: false,
keychain: 'login',
}
this.cnf = { ...defaults, ...customCnf, ...config }
this.args = []
this.outDir = this.cnf.outDir.startsWith('/')
? this.cnf.outDir
: path.resolve(this.cwd, this.cnf.outDir)
this.cnfPath = path.resolve(this.outDir, 'conf.ini')
const filename = this.cnf.filename || this.cnf.openssl.commonName
this.keyPath = path.resolve(this.outDir, `${filename}.key`)
this.certPath = path.resolve(this.outDir, `${filename}.crt`)
console.log('Options:')
console.log(this.cnf)
console.log() // line-break
console.log('outDir: ', this.outDir)
console.log('sslDir: ', this.sslDir)
console.log() // line-break
}
async generate() {
this.spinner = ora()
await this.setArgs()
await this.writeConfig()
this.spinner.start('Generating OpenSSL certificate...')
try {
this.instance = await execa.shell('openssl req ' + this.args.join(' '))
} catch (error) {
this.spinner.fail()
console.log(this.instance)
console.log(error)
} finally {
this.spinner.succeed()
console.log() // empty
console.log(chalk.dim(this.instance.cmd))
}
if (this.cnf.addToKeychain) {
this.addToKeychain()
}
}
async addToKeychain() {
this.spinner.indent = 2
console.log() // line-break
console.log('Adding certificate to macOS Keychain...')
// 1. ask for sudo
// this.spinner.start('Ask for sudo...')
// const sudo = await keychain.askForSudo()
// if (!sudo) return
// 2. get user keychain
this.spinner.start('Get user keychain...')
const keychainPath = await keychain.getKeychain('user', this.cnf.keychain)
if (keychainPath) {
this.spinner.succeed()
} else {
this.spinner.fail()
console.log('No keychain found for your user!')
process.exit(1)
}
// 3. find cert with same name
this.spinner.start('Find existing cert...')
const alreadyInstalled = await keychain.find(
this.cnf.openssl.commonName,
keychainPath
)
if (alreadyInstalled) {
this.spinner.fail()
if (this.cnf.removeOld) {
this.spinner.start('Remove existing cert...')
const successful = await keychain.remove(
this.cnf.openssl.commonName,
keychainPath
)
if (!successful) {
this.spinner.fail()
process.exit(1)
}
this.spinner.succeed()
} else {
console.log(
chalk.red(
'Certificate is already installed. Please remove it manually!'
)
)
process.exit(0)
}
} else {
this.spinner.succeed()
}
// 4. add certificate to keychain
this.spinner.start('Add cert to keychain...')
const successful = await keychain.add(this.certPath, keychainPath)
if (!successful) {
this.spinner.fail()
process.exit(1)
} else {
this.spinner.succeed()
}
console.log('Done!')
this.spinner.indent = 0
}
addArg(arg, val = null) {
this.spinner.start(`Pushing arg: ${arg} -> ${val}`)
if (val) this.args.push(`-${arg}`, val)
else this.args.push(`-${arg}`)
this.spinner.succeed()
return true
}
async setArgs() {
console.log('Setting arguments...')
this.spinner.indent = 2
await this.addArg('x509')
await this.addArg('newkey', `rsa:${this.cnf.bits}`)
await this.addArg('nodes')
await this.addArg('subj', this.formatSubj())
await this.addArg('config', `"${this.cnfPath}"`)
await this.addArg('days', 365)
await this.addArg('keyout', `"${this.keyPath}"`)
await this.addArg('out', `"${this.certPath}"`)
this.spinner.indent = 0
console.log('Done!\n')
}
formatSubj() {
const subjNames = [
'countryName',
'stateOrProvinceName',
'localityName',
'organizationName',
'commonName',
]
return (
'"/' +
Object.keys(this.cnf.openssl)
.filter(k => subjNames.includes(k))
.map(v => `${v}=${this.cnf.openssl[v]}`)
.join('/') +
'/"'
)
}
async writeConfig() {
this.spinner.start('Writing conf to file...')
let i = 2
const dnsNames = this.cnf.dns.map(val => {
const tmpl = `
DNS.${i} = ${val}
DNS.${i + 1} = www.${val}
`.trim()
i = i + 2
return tmpl
})
// prettier-ignore
const cnf = `
# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# This should only be used in development environments
# due to security reasons, so use this at your own risk!
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
[req]
default_bits = ${this.cnf.bits}
distinguished_name = req_distinguished_name
x509_extensions = req_ext
req_extensions = req_ext
[req_distinguished_name]
countryName = ${this.cnf.openssl.countryName}
stateOrProvinceName = ${this.cnf.openssl.stateOrProvinceName}
localityName = ${this.cnf.openssl.localityName}
organizationName = ${this.cnf.openssl.organizationName}
organizationalUnit = ${this.cnf.openssl.organizationalUnit}
commonName = ${this.cnf.openssl.commonName}
[req_ext]
subjectAltName = @alt_names
[alt_names]
IP.1 = 127.0.0.1
IP.2 = ::1
DNS.1 = localhost
${dnsNames.join("\n")}
`.trim()
try {
fs.writeFileSync(this.cnfPath, cnf)
this.spinner.succeed()
} catch (error) {
this.spinner.fail()
throw new Error(error)
}
return true
}
}
module.exports = OpenSSL