Skip to content

Commit b89ef70

Browse files
committed
Fixes #17: graphical sudo prompt is now shown in post-install script
1 parent c8c9402 commit b89ef70

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [7.0.3] - 2021-03-17
9+
10+
### Fixed
11+
12+
- No longer fails on install on system accounts that don’t have passwordless sudo set up. Instead, shows a graphical password box to get the person’s sudo password. (Fixes https://source.small-tech.org/site.js/lib/auto-encrypt-localhost/-/issues/17. See https://github.com/npm/cli/issues/2887 for the original npm bug.
13+
814
## [7.0.2] - 2021-03-06
915

1016
### Fixed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ At runtime, you can reach your server via the local loopback addresses (localhos
1414
npm i @small-tech/auto-encrypt-localhost
1515
```
1616

17+
Note that during installation, Auto Encrypt Localhost will create your local certificate authority and install it in the system root store and generate locally-trusted certificates. These actions require elevated privileges (`sudo`). Since [npm does not handle sudo prompts correctly in lifecycle scripts](https://github.com/npm/cli/issues/2887), you will see a graphical sudo prompt pop up to ask you for your adminstrator password. Once you’ve provided it, installation will proceed as normal.
18+
19+
20+
1721
## Usage
1822

1923
### Instructions

bin/post-install.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { version, binaryName } from '../lib/mkcert.js'
2929

3030
import fs from 'fs-extra'
3131

32+
import sudoPrompt from 'sudo-prompt'
3233

3334
async function secureGet (url) {
3435
return new Promise((resolve, reject) => {
@@ -123,7 +124,25 @@ mkcertProcessOptions.env.CAROOT = settingsPath
123124

124125
// Create the local certificate authority.
125126
process.stdout.write(` ╰─ Creating local certificate authority (local CA) using mkcert… `)
126-
childProcess.execFileSync(mkcertBinary, ['-install'], mkcertProcessOptions)
127+
128+
// We are using the sudo-prompt package here, instead of childProcess.execFileSync() because
129+
// this script is meant to run as an npm script and it appears that npm scripts fail to show
130+
// the system sudo prompt (and instead hang).
131+
//
132+
// See: https://github.com/npm/cli/issues/2887
133+
//
134+
// To workaround this issue, we use sudo-prompt here to display a graphical sudo prompt
135+
// that works well with npm scripts.
136+
137+
await (() => {
138+
return new Promise((resolve, reject) => {
139+
sudoPrompt.exec(`${mkcertBinary} -install`, {name: 'Auto Encrypt Localhost'}, function(error, stdout, stderr) {
140+
if (error) reject(error)
141+
resolve()
142+
})
143+
})
144+
})()
145+
127146
process.stdout.write('done.\n')
128147

129148
// Create the local certificate.
@@ -145,7 +164,9 @@ const certificateDetails = [
145164
'localhost'
146165
].concat(localIPv4Addresses)
147166

167+
// We can use a regular execFileSync call here as the sudo permissions will not have timed out yet.
148168
childProcess.execFileSync(mkcertBinary, certificateDetails, mkcertProcessOptions)
169+
149170
process.stdout.write('done.\n')
150171

151172
// This should never happen as an error in the above, if there is one,

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@small-tech/auto-encrypt-localhost",
3-
"version": "7.0.2",
3+
"version": "7.0.3",
44
"description": "Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.",
55
"keywords": [
66
"mkcert",
@@ -22,8 +22,7 @@
2222
"bin"
2323
],
2424
"scripts": {
25-
"postinstall": "node bin/post-install.js",
26-
"start": "node index.js",
25+
"postinstall": "bin/post-install.js",
2726
"test": "QUIET=true esm-tape-runner 'test/**/*.js' | tap-monkey",
2827
"coverage": "QUIET=true c8 esm-tape-runner 'test/**/*.js' | tap-monkey",
2928
"test-debug": "esm-tape-runner 'test/**/*.js' | tap-monkey",
@@ -53,6 +52,7 @@
5352
"encodeurl": "^1.0.2",
5453
"fs-extra": "^8.1.0",
5554
"server-destroy": "^1.0.1",
55+
"sudo-prompt": "^9.2.1",
5656
"syswide-cas": "^5.3.0"
5757
},
5858
"devDependencies": {

0 commit comments

Comments
 (0)