Skip to content

Commit

Permalink
fix: ssl:make timing bug (#180)
Browse files Browse the repository at this point in the history
createCA() is async and thus doesn't finish till *after* it returns.
createCert() *needs* the CA file to exist before it runs.

If both files are missing, this command died every time because
`createCert()` would be called before the CA file was created.

It would then kill the node process before *either* file was created.

Now we use async/await to order them appropriately and we add a test to
ensure the files get created.
  • Loading branch information
danielbeardsley authored and hayes committed Mar 9, 2022
1 parent 0f8e0ce commit bec0e9e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cli/__tests__/__snapshots__/ssl.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test ssl command ssl:make creates some files in .localhost_ssl 1`] = `
Array [
"ca.crt",
"ca.key",
"cert.crt",
"cert.key",
]
`;
33 changes: 33 additions & 0 deletions cli/__tests__/ssl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-env jest */
const ssl = require('../commands/ssl');
const mock = require('mock-fs');
const fs = require('fs');
const os = require('os');
const path = require('path');

beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
});

describe('Test ssl command', () => {
beforeEach(() => {
// Start with an empty file system
mock({});
});
afterEach(() => {
mock.restore();
});
test('ssl:make creates some files in .localhost_ssl', (done) => {
ssl({make: true})
.then(() => {
assertSnapshot(path.join(os.homedir(), '.localhost_ssl'));
})
.then(done);
});
});

function assertSnapshot(dir) {
const result = fs.readdirSync(dir);
mock.restore();
expect(result).toMatchSnapshot();
}
4 changes: 2 additions & 2 deletions cli/commands/ssl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {checkSSL, validate} = require('../../src/server/ssl');

module.exports = (args) => {
module.exports = async (args) => {
if (args.check) {
validate();
}
if (args.make) {
checkSSL();
await checkSSL();
}
};
6 changes: 3 additions & 3 deletions src/server/ssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getSSL() {
return {key, cert, ca};
}

function checkSSL() {
async function checkSSL() {
makeCertFolder();

const writeCert = (cert, key) => {
Expand All @@ -72,13 +72,13 @@ function checkSSL() {
console.log(
'No Certificate Authority (CA) found. Creating one now to issue new SSL certificates'
);
createCA()
await createCA()
.then(({cert, key}) => writeCA(cert, key))
.catch((err) => console.error(err));
}
if (!checkCert()) {
console.log('No SSL Certificate found. Creating one now');
createCert()
await createCert()
.then(({cert, key}) => writeCert(cert, key))
.catch((err) => console.error(err));
}
Expand Down

0 comments on commit bec0e9e

Please sign in to comment.