Skip to content

Commit

Permalink
feat: Create self-signed ssl cert from Packer CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Mar 21, 2021
1 parent 7e88beb commit f006d6d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [1.3.8](https://github.com/hayes0724/shopify-packer/compare/1.3.7...1.3.8) (2021-03-21)


### :sparkles: Features

* Create self-signed ssl cert from Packer CLI ([98701e6](https://github.com/hayes0724/shopify-packer/commit/98701e637889794b688a7ee4a1b9193e55d10379))



## [1.3.7](https://github.com/hayes0724/shopify-packer/compare/1.3.6...1.3.7) (2021-03-20)


Expand Down
10 changes: 10 additions & 0 deletions cli/commands/ssl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {makeCert} = require('../../src/server/ssl');

module.exports = (args) => {
if (args.check) {

}
if (args.make) {
makeCert(args)
}
};
8 changes: 8 additions & 0 deletions cli/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ switch (process.argv[2]) {
args.download = true;
require('./commands/theme')(args);
break;
case 'ssl:make':
args.make = true
require('./commands/ssl')(args);
break;
case 'ssl:check':
args.check = true
require('./commands/ssl')(args);
break;
case '--version':
console.log(chalk.green(`Shopify Packer - v${version}`));
break;
Expand Down
10 changes: 10 additions & 0 deletions docs/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ List of all API commands for Packer:
- [theme:create](#theme:create)
- [theme:remove](#theme:remove)
- [theme:download](#theme:download)
- [ssl:make](#ssl:make)
- [help](#help)

### init
Expand Down Expand Up @@ -125,6 +126,15 @@ packer theme:download
| --- | --- |
| ``--env`` | Targets a custom environment. Setting --env=production would use the production settings in packer.config.json |

### ssl:make
Create a self-signed ssl cert for local development
```bash
packer ssl:make
```
| Flag | Description |
| --- | --- |
| ``--name`` | Name for SSL cert, defaults to localhost |


### help
Display all commands and flags
Expand Down
6 changes: 4 additions & 2 deletions docs/docs/ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ This is the fastest method but will often have to be repeated

### Create self signed cert
Create a trusted, self-signed SSL certificate on your device so the
assets, served via https, are never blocked. This takes longer and the
steps very depending on what OS you are using.
assets, served via https, are never blocked.

Packer will look for the certificate with extension `.cer` or `.crt`
in ``~/.localhost_ssl`` it will be in the users home directory

The certificate common name must match the ip you are using with Packer.
When your IP changes you will need to make a new cert

Packer can create an SSL cert for you using the `packer ssl:make` command,
see [commands](commands.md) for usage
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hayes0724/shopify-packer",
"version": "1.3.7",
"version": "1.3.8",
"bin": {
"packer": "cli/index.js"
},
Expand Down Expand Up @@ -85,6 +85,7 @@
"prettier": "^2.1.2",
"read-yaml": "^1.1.0",
"sass-loader": "^10.1.0",
"selfsigned": "^1.10.8",
"style-loader": "^2.0.0",
"stylelint": "^13.7.2",
"stylelint-config-prettier": "^8.0.1",
Expand Down
38 changes: 37 additions & 1 deletion src/server/ssl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
const path = require('path');
const os = require('os');
const {existsSync, readFileSync, readdirSync} = require('fs');
const {existsSync, readFileSync, readdirSync, writeFileSync} = require('fs');

const chalk = require('chalk');
const ora = require('ora');
const selfsigned = require('selfsigned');

const spinner = ora(chalk.magenta('Creating SSL certificate...'));

function makeCert(args) {
const name = args.name ? args.name : 'localhost';
spinner.start();
const attrs = [{name: 'commonName', value: name}];
const pems = selfsigned.generate(attrs, {days: 365});
try {
const certFile = path.resolve(
os.homedir(),
'.localhost_ssl/',
`${name}.crt`
);
const keyFile = path.resolve(
os.homedir(),
'.localhost_ssl/',
`${name}.key`
);
writeFileSync(certFile, pems.cert);
writeFileSync(keyFile, pems.private);
console.log('\n');
console.log(chalk.green(`SSL Cert: ${certFile}`));
console.log(chalk.green(`SSL Key: ${keyFile}`));
console.log('\n');
spinner.succeed('SSL certificate created successfully!');
} catch (err) {
console.error(err);
spinner.fail();
}
}

function sslKeyCert() {
const key = readFileSync(getSSLKeyPath());
Expand Down Expand Up @@ -32,6 +67,7 @@ function findKey() {
}

module.exports = {
makeCert,
sslKeyCert,
getSSLCertPath,
getSSLKeyPath,
Expand Down

0 comments on commit f006d6d

Please sign in to comment.