Skip to content

Commit be273aa

Browse files
authored
Feature: Allow multiple Subject Alternative Name (SAN) extensions (#52)
* Feature: Adding multiple domains using subject alternative name (SAN) * Update README with Docker and SAN instructions * Update readme * Readme polish * Tweaks * Formatting fixes * Adjust pathForDomain * Create a stable hash for directory path of SAN listed domains * Merge SAN with validation features * Tweak for latest typescript
1 parent d343e2d commit be273aa

9 files changed

+2719
-43
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,98 @@ The `certutil` tooling is installed in OS-specific ways:
9797
so devcert will simply fallback to the wizard approach for Firefox outlined
9898
above)
9999

100+
## Multiple domains (SAN)
101+
If you are developing a multi-tenant app or have many apps locally, you can generate a security
102+
certificate using `devcert` to also use the [Subject Alternative Name](https://en.wikipedia.org/wiki/Subject_Alternative_Name)
103+
extension, just pass an array of domains instead.
104+
105+
```js
106+
let ssl = await devcert.certificateFor([
107+
'localhost',
108+
'local.api.example.com',
109+
'local.example.com',
110+
'local.auth.example.com'
111+
]);
112+
https.createServer(ssl, app).listen(3000);
113+
```
114+
115+
## Docker and local development
116+
If you are developing with Docker, one option is to install `devcert` into a base folder in your home directory and
117+
generate certificates for all of your local Docker projects. See comments and caveats in [this issue](https://github.com/davewasmer/devcert/issues/17).
118+
119+
While not elegant, you only really need to do this as often as you add new domains locally, which is probably not very often.
120+
121+
The general script would look something like:
122+
123+
```js
124+
// example: make a directory in home directory such as ~/devcert-util
125+
// ~/devcert-util/generate.js
126+
const fs = require('fs');
127+
const devcert = require('devcert');
128+
129+
// or if its just one domain - devcert.certificateFor('local.example.com')
130+
devcert.certificateFor([
131+
'localhost',
132+
'local.api.example.com',
133+
'local.example.com',
134+
'local.auth.example.com'
135+
])
136+
.then(({key, cert}) => {
137+
fs.writeFileSync('./certs/tls.key', key);
138+
fs.writeFileSync('./certs/tls.cert', cert);
139+
})
140+
.catch(console.error);
141+
```
142+
143+
An easy way to use the files generated from above script is to copy the `~/devcert-util/certs` folder into your Docker projects:
144+
```
145+
# local-docker-project-root/
146+
🗀 certs/
147+
🗎 tls.key
148+
🗎 tls.cert
149+
```
150+
151+
And add this line to your `.gitignore`:
152+
```
153+
certs/
154+
```
155+
156+
These two files can now easily be used by any project, be it Node.js or something else.
157+
158+
In Node, within Docker, simply load the copied certificate files into your https server:
159+
```js
160+
const fs = require('fs');
161+
const Express = require('express');
162+
const app = new Express();
163+
https
164+
.createServer({
165+
key: fs.readFileSync('./certs/tls.key'),
166+
cert: fs.readFileSync('./certs/tls.cert')
167+
}, app)
168+
.listen(3000);
169+
```
170+
171+
Also works with webpack dev server or similar technologies:
172+
```js
173+
// webpack.config.js
174+
const fs = require('fs');
175+
176+
module.exports = {
177+
//...
178+
devServer: {
179+
contentBase: join(__dirname, 'dist'),
180+
host: '0.0.0.0',
181+
public: 'local.api.example.com',
182+
port: 3000,
183+
publicPath: '/',
184+
https: {
185+
key: fs.readFileSync('./certs/tls.key'),
186+
cert: fs.readFileSync('./certs/tls.cert')
187+
}
188+
}
189+
};
190+
```
191+
100192
## How it works
101193

102194
When you ask for a development certificate, devcert will first check to see

openssl-configurations/domain-certificate-signing-requests.conf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ subjectAltName = @subject_alt_names
2121
subjectKeyIdentifier = hash
2222

2323
[ subject_alt_names ]
24-
DNS.1 = <%= domain %>
25-
DNS.2 = *.<%= domain %>
24+
<%= subjectAltNames %>

openssl-configurations/domain-certificates.conf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ extendedKeyUsage = serverAuth
3535
subjectAltName = @subject_alt_names
3636

3737
[ subject_alt_names ]
38-
DNS.1 = <%= domain %>
39-
DNS.2 = *.<%= domain %>
38+
<%= subjectAltNames %>

0 commit comments

Comments
 (0)