Skip to content

Commit bb7d5f0

Browse files
authored
run docusaurus locally with SSL if certs are provided (#366)
1 parent e56dc7a commit bb7d5f0

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
HTTPS=true
2+
3+
# Generate via ./scripts/generate-certs, or mkcert
4+
# (see: https://docusaurus.io/docs/cli#enabling-https)
5+
SSL_CRT_FILE=ssl.crt
6+
SSL_KEY_FILE=ssl.key

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
# Misc
1212
.DS_Store
13+
*.crt
14+
*.key
15+
*.pem
16+
.env
1317
.env.local
1418
.env.development.local
1519
.env.test.local

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ npm start
2020
This command starts a local development server and opens up a browser window. Most changes are
2121
reflected live without having to restart the server.
2222

23+
### SSL
24+
25+
By default, `npm start` will attempt to start your local instance with SSL enabled using
26+
certificates referenced in your local dotfile (`.env`).
27+
28+
- copy the provided `.env.example` to `.env` and update the values as needed
29+
- (requires [OpenSSL](https://www.openssl.org/)) Generate your self-signed certs with
30+
`npm run setup:ssl` and follow the instructions.
31+
- Alternatively use [mkcert](https://github.com/FiloSottile/mkcert) to
32+
[generate and add certs to your trust store](https://docusaurus.io/docs/cli#enabling-https)
33+
- run `npm start`!
34+
35+
If the script fails to find your `.env` or the required values within, docusaurus will start
36+
normally without SSL.
37+
38+
If you need to explicitly develop without SSL, simply delete your `.env` file or use the command
39+
`npm start:insecure` instead.
40+
2341
## Build
2442

2543
```bash

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"private": true,
1616
"scripts": {
1717
"docusaurus": "docusaurus",
18-
"start": "docusaurus start",
18+
"start": "./scripts/docusaurus-start.sh",
19+
"start:insecure": "docusaurus start",
20+
"setup:ssl": "./scripts/generate-certs.sh",
1921
"build": "docusaurus build",
2022
"swizzle": "docusaurus swizzle",
2123
"deploy": "docusaurus deploy",

scripts/docusaurus-start.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT_DIR=$(git rev-parse --show-toplevel)
4+
5+
# shellcheck source=.env
6+
set -o allexport
7+
source $ROOT_DIR/.env
8+
set +o allexport
9+
10+
npm run docusaurus start

scripts/generate-certs.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT_DIR=$(git rev-parse --show-toplevel)
4+
5+
# Load .env values into the environment
6+
set -o allexport
7+
. "$ROOT_DIR/.env"
8+
set +o allexport
9+
10+
openssl req -x509 -newkey rsa:4096 -keyout $SSL_KEY_FILE -out $SSL_CRT_FILE -sha256 -days 1826 -nodes \
11+
-subj "/CN=localhost/O=Bitwarden Contributing Docs Local Development" \
12+
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
13+
14+
chmod +rw $SSL_CRT_FILE
15+
chmod +rw $SSL_KEY_FILE
16+
17+
printf "Certificate generated! When prompted, enter your password to update your system's secure store with the Certificate Authority.\n\n"
18+
printf "Alternatively, you can manually add it with:\n"
19+
20+
# Mac OSX
21+
if [[ "$OSTYPE" == "darwin"* ]]; then
22+
printf "\e[30m\e[44m sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain $SSL_CRT_FILE \e[0m\n"
23+
24+
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ssl.crt
25+
# If not Mac OS, assume *nix
26+
else
27+
printf "\e[30m\e[44m sudo cp $SSL_CRT_FILE /usr/local/share/ca-certificates/ && sudo update-ca-certificates \e[0m\n\n"
28+
printf "Important Note! Chromium doesn't use 'ca-certificates' on *nix. Instead it uses nssdb for cert storage, and depending on your configuration, may be in the shared system store at '\$HOME/.pki/nssdb', in Chromium's local snap store (e.g. '\$HOME/snap/chromium/current/.pki/nssdb'), or elsewhere. You will need to install the appropriate binary for your distro to run 'certutil -d sql:\$CHROMIUM_SECURE_STORE -A -t "CP,CP," -n DocsLocalDevelopmentSSL -i ./$SSL_CRT_FILE' from the project root."
29+
30+
sudo cp $SSL_CRT_FILE /usr/local/share/ca-certificates/
31+
sudo update-ca-certificates
32+
fi

0 commit comments

Comments
 (0)