Skip to content

Commit d278871

Browse files
committed
Added signing key generation
1 parent 9d02e5e commit d278871

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ I hope to be able to scaffold an app so that identifying the below in unnecessar
7676
|- manifest.json
7777
```
7878

79+
## System Dependencies
80+
81+
If you wish to use the signing key functionality you will need to have `openssl` available on your system.
82+
7983
## Adding to your project
8084

8185
This can be added to your vuejs project by one of the following methods:
@@ -269,9 +273,7 @@ yarn test
269273

270274
## Roadmap
271275

272-
- Add some generator options for other pieces of browser extensions. This includes scaffolding the components/dirs, and registering the build options into the build time hooks.
273276
- A preset
274-
- Key Generation
275277
- Cleanup the dist-zip directory
276278

277279
## Credits

generator/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const { generateKey } = require('../lib/signing-key')
34
const gitignoreSnippet = `
45
# Vue Browser Extension Output
56
*.pem
@@ -117,6 +118,12 @@ module.exports = (api, _options) => {
117118
api.render('./template/content-script', { ...options })
118119
}
119120

121+
if (options.generateSigningKey === true) {
122+
api.render((files) => {
123+
files['key.pem'] = generateKey()
124+
})
125+
}
126+
120127
api.onCreateComplete(() => {
121128
const gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
122129
fs.writeFileSync(api.resolve('./.gitignore'), gitignore + gitignoreSnippet)

index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const path = require('path')
2-
const fs = require('fs')
3-
const { exec } = require('child_process')
42
const logger = require('@vue/cli-shared-utils')
53
const webpack = require('webpack')
64
const CopyWebpackPlugin = require('copy-webpack-plugin')
75
const ExtensionReloader = require('webpack-extension-reloader')
86
const ZipPlugin = require('zip-webpack-plugin')
7+
const { keyExists, hashKey } = require('./lib/signing-key')
98
const defaultOptions = {
109
components: {},
1110
componentOptions: {},
@@ -36,7 +35,7 @@ module.exports = (api, options) => {
3635
const packageJson = require(path.join(appRootPath, 'package.json'))
3736
const isProduction = api.service.mode === 'production'
3837
const keyFile = api.resolve('key.pem')
39-
const hasKeyFile = fs.existsSync(keyFile)
38+
const hasKeyFile = keyExists(keyFile)
4039

4140
api.chainWebpack((webpackConfig) => {
4241
webpackConfig.entryPoints.delete('app')
@@ -114,15 +113,7 @@ module.exports = (api, options) => {
114113

115114
if (hasKeyFile) {
116115
try {
117-
jsonContent.key = await new Promise((resolve, reject) => {
118-
exec(`openssl rsa -in ${keyFile} -pubout -outform DER | openssl base64 -A`, (error, stdout) => {
119-
if (error) {
120-
// node couldn't execute the command
121-
return reject(error)
122-
}
123-
resolve(stdout)
124-
})
125-
})
116+
jsonContent.key = await hashKey(keyFile)
126117
} catch (error) {
127118
logger.error('Unexpected error hashing keyfile:', error)
128119
}

lib/signing-key.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { existsSync } = require('fs')
2+
const { exec, execSync } = require('child_process')
3+
4+
exports.keyExists = (keyFile) => existsSync(keyFile)
5+
6+
exports.generateKey = () => execSync('openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt')
7+
8+
exports.hashKey = (keyFile) =>
9+
new Promise((resolve, reject) => {
10+
exec(`openssl rsa -in ${keyFile} -pubout -outform DER | openssl base64 -A`, (error, stdout) => {
11+
if (error) {
12+
// node couldn't execute the command
13+
return reject(error)
14+
}
15+
resolve(stdout)
16+
})
17+
})

prompts.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,11 @@ module.exports = [
8282
message: 'Make polyfill available without import?',
8383
default: true,
8484
when: (answers) => answers.usePolyfill
85+
},
86+
{
87+
name: 'generateSigningKey',
88+
type: 'confirm',
89+
message: 'Generate a new signing key (danger)?',
90+
default: false
8591
}
8692
]

0 commit comments

Comments
 (0)