Skip to content

Commit 2dfe847

Browse files
committed
feat(ssl): only generate dhparam and ssl-params once per server
closes #487 - put dhparam.pem and ssl-param.conf in nginx snippets directory
1 parent 430ed8a commit 2dfe847

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

extensions/nginx/index.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class NginxExtension extends cli.Extension {
8686
}
8787

8888
const rootPath = path.resolve(ctx.instance.dir, 'system', 'nginx-root');
89-
const dhparamFile = path.join(ctx.instance.dir, 'system', 'files', 'dhparam.pem');
89+
const dhparamFile = '/etc/nginx/snippets/dhparam.pem';
90+
const sslParamsFile = '/etc/nginx/snippets/ssl-params.conf';
91+
const sslParamsConf = template(fs.readFileSync(path.join(__dirname, 'templates', 'ssl-params.conf'), 'utf8'));
9092

9193
return this.ui.listr([{
9294
title: 'Checking DNS resolution',
@@ -148,21 +150,22 @@ class NginxExtension extends cli.Extension {
148150
}
149151
}, {
150152
title: 'Generating Encryption Key (may take a few minutes)',
151-
skip: (ctx) => ctx.dnsfail,
153+
skip: (ctx) => ctx.dnsfail || fs.existsSync(dhparamFile),
152154
task: () => {
153-
return execa.shell(`openssl dhparam -out ${dhparamFile} 2048`)
155+
return this.ui.sudo(`openssl dhparam -out ${dhparamFile} 2048 > /dev/null`)
154156
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
155157
}
156158
}, {
157159
title: 'Generating SSL security headers',
158-
skip: (ctx) => ctx.dnsfail,
159-
task: (ctx) => {
160-
const sslParamsConf = template(fs.readFileSync(path.join(__dirname, 'templates', 'ssl-params.conf'), 'utf8'));
161-
return ctx.instance.template(
162-
sslParamsConf({dhparam: dhparamFile}),
163-
'ssl security parameters',
164-
'ssl-params.conf'
165-
);
160+
skip: (ctx) => ctx.dnsfail || fs.existsSync(sslParamsFile),
161+
task: () => {
162+
const tmpfile = path.join(os.tmpdir(), 'ssl-params.conf');
163+
164+
return fs.writeFile(tmpfile, sslParamsConf({dhparam: dhparamFile}), {encoding: 'utf8'}).then(() => {
165+
return this.ui.sudo(`mv ${tmpfile} ${sslParamsFile}`).catch(
166+
(error) => Promise.reject(new cli.errors.ProcessError(error))
167+
);
168+
});
166169
}
167170
}, {
168171
title: 'Generating SSL configuration',
@@ -175,7 +178,7 @@ class NginxExtension extends cli.Extension {
175178
webroot: rootPath,
176179
fullchain: path.join(acmeFolder, 'fullchain.cer'),
177180
privkey: path.join(acmeFolder, `${parsedUrl.hostname}.key`),
178-
sslparams: path.join(ctx.instance.dir, 'system', 'files', 'ssl-params.conf'),
181+
sslparams: sslParamsFile,
179182
location: parsedUrl.pathname !== '/' ? `^~ ${parsedUrl.pathname}` : '/',
180183
port: ctx.instance.config.get('server.port')
181184
});

extensions/nginx/templates/ssl-params.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ssl_stapling on; # Requires nginx >= 1.3.7
88
ssl_stapling_verify on; # Requires nginx => 1.3.7
99
resolver 8.8.8.8 8.8.4.4 valid=300s;
1010
resolver_timeout 5s;
11-
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
11+
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains; preload';
1212
add_header X-Frame-Options SAMEORIGIN;
1313
add_header X-Content-Type-Options nosniff;
1414

0 commit comments

Comments
 (0)