Skip to content

Commit 99d7162

Browse files
committed
fix(ssl): cleanup after failure, don't allow re-running
closes #302 - checks if ssl has already been setup, if it has then skips - better cleanup/handling of well-known config block
1 parent c83f83d commit 99d7162

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

extensions/nginx/index.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ class NginxExtension extends cli.Extension {
2727
setupNginx(argv, ctx, task) {
2828
if (!this.isSupported()) {
2929
this.ui.log('Nginx is not installed. Skipping Nginx setup.', 'yellow');
30-
return task && task.skip();
30+
return task.skip();
3131
}
3232

3333
let parsedUrl = url.parse(ctx.instance.config.get('url'));
3434

3535
if (parsedUrl.port) {
3636
this.ui.log('Your url contains a port. Skipping Nginx setup.', 'yellow');
37-
return task && task.skip();
37+
return task.skip();
3838
}
3939

4040
if (parsedUrl.pathname !== '/') {
4141
this.ui.log('The Nginx service does not support subdirectory configurations yet. Skipping Nginx setup.', 'yellow');
42-
return task && task.skip();
42+
return task.skip();
4343
}
4444

4545
if (fs.existsSync(`/etc/nginx/sites-available/${parsedUrl.hostname}.conf`)) {
4646
this.ui.log('Nginx configuration already found for this url. Skipping Nginx setup.', 'yellow');
47-
return task && task.skip();
47+
return task.skip();
4848
}
4949

5050
return Promise.fromNode((cb) => NginxConfFile.createFromSource('', cb)).then((conf) => {
@@ -87,9 +87,14 @@ class NginxExtension extends cli.Extension {
8787
}
8888

8989
setupSSL(argv, ctx, task) {
90+
if (ctx.instance.cliConfig.get('extension.ssl', false)) {
91+
this.ui.log('SSL has already been set up, skipping', 'yellow');
92+
return task.skip();
93+
}
94+
9095
if (!argv.prompt && !argv.sslemail) {
9196
this.ui.log('SSL email must be provided via the --sslemail option, skipping SSL setup', 'yellow');
92-
return task && task.skip();
97+
return task.skip();
9398
}
9499

95100
let parsedUrl = url.parse(ctx.instance.config.get('url'));
@@ -102,7 +107,7 @@ class NginxExtension extends cli.Extension {
102107
this.ui.log('Nginx config file does not exist, skipping SSL setup', 'yellow');
103108
}
104109

105-
return task && task.skip();
110+
return task.skip();
106111
}
107112

108113
let rootPath = path.resolve(ctx.instance.dir, 'system', 'nginx-root');
@@ -151,8 +156,12 @@ class NginxExtension extends cli.Extension {
151156

152157
ctx.ssl.conf = conf;
153158
ctx.ssl.http = conf.nginx.server;
154-
ctx.ssl.http._add('location', '~ /.well-known');
155-
ctx.ssl.http.location[1]._add('allow', 'all');
159+
160+
// Don't add well-known block if it already exists
161+
if (ctx.ssl.http.location.length === 1) {
162+
ctx.ssl.http._add('location', '~ /.well-known');
163+
ctx.ssl.http.location[1]._add('allow', 'all');
164+
}
156165
});
157166
});
158167
}
@@ -161,7 +170,17 @@ class NginxExtension extends cli.Extension {
161170
task: () => this.restartNginx()
162171
}, {
163172
title: 'Getting SSL Certificate',
164-
task: () => letsencrypt(ctx.instance, argv.sslemail, argv.sslstaging)
173+
task: () => {
174+
return letsencrypt(ctx.instance, argv.sslemail, argv.sslstaging).catch((error) => {
175+
if (!(error instanceof cli.errors.ProcessError)) {
176+
return Promise.reject(error);
177+
}
178+
179+
// Ensure ~/.well-known location gets cleaned up
180+
ctx.ssl.http._remove('location', 1);
181+
return Promise.reject(error);
182+
});
183+
}
165184
}, {
166185
title: 'Generating Encryption Key (may take a few minutes)',
167186
task: (ctx) => {

0 commit comments

Comments
 (0)