Skip to content

Commit 1b611e6

Browse files
committed
Merge commit 'c5aa2b9f771cbd4c78c239ed0791aeb8d9e4d2e4' into features/dns-cloudflare
2 parents 251aac7 + c5aa2b9 commit 1b611e6

File tree

6 files changed

+166
-19
lines changed

6 files changed

+166
-19
lines changed

backend/internal/certificate.js

Lines changed: 96 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,11 @@ const internalCertificate = {
141141
});
142142
})
143143
.then((in_use_result) => {
144-
// 3. Generate the LE config
145-
return internalNginx.generateLetsEncryptRequestConfig(certificate)
146-
.then(internalNginx.reload)
147-
.then(() => {
144+
// Is CloudFlare, no config needed, so skip 3 and 5.
145+
if (data.meta.cloudflare_use) {
146+
return internalNginx.reload().then(() => {
148147
// 4. Request cert
149-
return internalCertificate.requestLetsEncryptSsl(certificate);
150-
})
151-
.then(() => {
152-
// 5. Remove LE config
153-
return internalNginx.deleteLetsEncryptRequestConfig(certificate);
148+
return internalCertificate.requestLetsEncryptCloudFlareDnsSsl(certificate, data.meta.cloudflare_token);
154149
})
155150
.then(internalNginx.reload)
156151
.then(() => {
@@ -162,15 +157,44 @@ const internalCertificate = {
162157
})
163158
.catch((err) => {
164159
// In the event of failure, revert things and throw err back
165-
return internalNginx.deleteLetsEncryptRequestConfig(certificate)
166-
.then(() => {
167-
return internalCertificate.enableInUseHosts(in_use_result);
168-
})
160+
return internalCertificate.enableInUseHosts(in_use_result)
169161
.then(internalNginx.reload)
170162
.then(() => {
171163
throw err;
172164
});
173165
});
166+
} else {
167+
// 3. Generate the LE config
168+
return internalNginx.generateLetsEncryptRequestConfig(certificate)
169+
.then(internalNginx.reload)
170+
.then(() => {
171+
// 4. Request cert
172+
return internalCertificate.requestLetsEncryptSsl(certificate);
173+
})
174+
.then(() => {
175+
// 5. Remove LE config
176+
return internalNginx.deleteLetsEncryptRequestConfig(certificate);
177+
})
178+
.then(internalNginx.reload)
179+
.then(() => {
180+
// 6. Re-instate previously disabled hosts
181+
return internalCertificate.enableInUseHosts(in_use_result);
182+
})
183+
.then(() => {
184+
return certificate;
185+
})
186+
.catch((err) => {
187+
// In the event of failure, revert things and throw err back
188+
return internalNginx.deleteLetsEncryptRequestConfig(certificate)
189+
.then(() => {
190+
return internalCertificate.enableInUseHosts(in_use_result);
191+
})
192+
.then(internalNginx.reload)
193+
.then(() => {
194+
throw err;
195+
});
196+
});
197+
}
174198
})
175199
.then(() => {
176200
// At this point, the letsencrypt cert should exist on disk.
@@ -748,6 +772,39 @@ const internalCertificate = {
748772
});
749773
},
750774

775+
/**
776+
* @param {Object} certificate the certificate row
777+
* @param {String} apiToken the cloudflare api token
778+
* @returns {Promise}
779+
*/
780+
requestLetsEncryptCloudFlareDnsSsl: (certificate, apiToken) => {
781+
logger.info('Requesting Let\'sEncrypt certificates via Cloudflare DNS for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
782+
783+
let tokenLoc = '~/cloudflare-token';
784+
let storeKey = 'echo "dns_cloudflare_api_token = ' + apiToken + '" > ' + tokenLoc;
785+
786+
let cmd =
787+
storeKey + " && " +
788+
certbot_command + ' certonly --non-interactive ' +
789+
'--cert-name "npm-' + certificate.id + '" ' +
790+
'--agree-tos ' +
791+
'--email "' + certificate.meta.letsencrypt_email + '" ' +
792+
'--domains "' + certificate.domain_names.join(',') + '" ' +
793+
'--dns-cloudflare --dns-cloudflare-credentials ' + tokenLoc +
794+
(le_staging ? ' --staging' : '')
795+
+ ' && rm ' + tokenLoc;
796+
797+
if (debug_mode) {
798+
logger.info('Command:', cmd);
799+
}
800+
801+
return utils.exec(cmd).then((result) => {
802+
logger.info(result);
803+
return result;
804+
});
805+
},
806+
807+
751808
/**
752809
* @param {Access} access
753810
* @param {Object} data
@@ -761,7 +818,9 @@ const internalCertificate = {
761818
})
762819
.then((certificate) => {
763820
if (certificate.provider === 'letsencrypt') {
764-
return internalCertificate.renewLetsEncryptSsl(certificate)
821+
let renewMethod = certificate.meta.cloudflare_use ? internalCertificate.renewLetsEncryptCloudFlareSsl : internalCertificate.renewLetsEncryptSsl;
822+
823+
return renewMethod(certificate)
765824
.then(() => {
766825
return internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem');
767826
})
@@ -815,6 +874,29 @@ const internalCertificate = {
815874
});
816875
},
817876

877+
/**
878+
* @param {Object} certificate the certificate row
879+
* @returns {Promise}
880+
*/
881+
renewLetsEncryptCloudFlareSsl: (certificate) => {
882+
logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
883+
884+
let cmd = certbot_command + ' renew --non-interactive ' +
885+
'--cert-name "npm-' + certificate.id + '" ' +
886+
'--disable-hook-validation ' +
887+
(le_staging ? '--staging' : '');
888+
889+
if (debug_mode) {
890+
logger.info('Command:', cmd);
891+
}
892+
893+
return utils.exec(cmd)
894+
.then((result) => {
895+
logger.info(result);
896+
return result;
897+
});
898+
},
899+
818900
/**
819901
* @param {Object} certificate the certificate row
820902
* @param {Boolean} [throw_errors]
@@ -824,7 +906,6 @@ const internalCertificate = {
824906
logger.info('Revoking Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
825907

826908
let cmd = certbot_command + ' revoke --non-interactive ' +
827-
'--config "' + le_config + '" ' +
828909
'--cert-path "/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem" ' +
829910
'--delete-after-revoke ' +
830911
(le_staging ? '--staging' : '');

backend/schema/endpoints/certificates.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
},
4242
"letsencrypt_agree": {
4343
"type": "boolean"
44+
},
45+
"cloudflare_use": {
46+
"type": "boolean"
47+
},
48+
"cloudflare_token": {
49+
"type": "string"
4450
}
4551
}
4652
}

docker/dev/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ ENV S6_FIX_ATTRS_HIDDEN=1
77

88
RUN echo "fs.file-max = 65535" > /etc/sysctl.conf \
99
&& apk update \
10-
&& apk add python2 certbot jq \
10+
&& apk add python2 py-pip certbot jq \
11+
&& pip install certbot-dns-cloudflare \
1112
&& rm -rf /var/cache/apk/*
1213

1314
# Task

frontend/js/app/nginx/certificates/form.ejs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@
2020
<input name="meta[letsencrypt_email]" type="email" class="form-control" placeholder="" value="<%- getLetsencryptEmail() %>" required>
2121
</div>
2222
</div>
23+
24+
<!-- CloudFlare -->
25+
<div class="col-sm-12 col-md-12">
26+
<div class="form-group">
27+
<label class="custom-switch">
28+
<input type="checkbox" class="custom-switch-input" name="meta[cloudflare_use]" value="1">
29+
<span class="custom-switch-indicator"></span>
30+
<span class="custom-switch-description"><%= i18n('ssl', 'use-cloudflare') %></span>
31+
</label>
32+
</div>
33+
</div>
34+
<div class="col-sm-12 col-md-12 cloudflare">
35+
<div class="form-group">
36+
<label class="form-label">CloudFlare DNS API Token <span class="form-required">*</span></label>
37+
<input type="text" name="meta[cloudflare_token]" class="form-control" id="cloudflare_token">
38+
</div>
39+
</div>
40+
2341
<div class="col-sm-12 col-md-12">
2442
<div class="form-group">
2543
<label class="custom-switch">

frontend/js/app/nginx/certificates/form.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,59 @@ module.exports = Mn.View.extend({
2020
save: 'button.save',
2121
other_certificate: '#other_certificate',
2222
other_certificate_key: '#other_certificate_key',
23-
other_intermediate_certificate: '#other_intermediate_certificate'
23+
other_intermediate_certificate: '#other_intermediate_certificate',
24+
cloudflare_switch: 'input[name="meta[cloudflare_use]"]',
25+
cloudflare_token: 'input[name="meta[cloudflare_token]"',
26+
cloudflare: '.cloudflare'
2427
},
2528

2629
events: {
30+
'change @ui.cloudflare_switch': function() {
31+
let checked = this.ui.cloudflare_switch.prop('checked');
32+
if (checked) {
33+
this.ui.cloudflare_token.prop('required', 'required');
34+
this.ui.cloudflare.show();
35+
} else {
36+
this.ui.cloudflare_token.prop('required', false);
37+
this.ui.cloudflare.hide();
38+
}
39+
},
2740
'click @ui.save': function (e) {
2841
e.preventDefault();
2942

3043
if (!this.ui.form[0].checkValidity()) {
3144
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
45+
$(this).removeClass('btn-loading');
3246
return;
3347
}
3448

3549
let view = this;
3650
let data = this.ui.form.serializeJSON();
3751
data.provider = this.model.get('provider');
3852

53+
54+
55+
let domain_err = false;
56+
if (!data.meta.cloudflare_use) {
57+
data.domain_names.split(',').map(function (name) {
58+
if (name.match(/\*/im)) {
59+
domain_err = true;
60+
}
61+
});
62+
}
63+
64+
if (domain_err) {
65+
alert('Cannot request Let\'s Encrypt Certificate for wildcard domains when not using CloudFlare DNS');
66+
return;
67+
}
68+
3969
// Manipulate
4070
if (typeof data.meta !== 'undefined' && typeof data.meta.letsencrypt_agree !== 'undefined') {
4171
data.meta.letsencrypt_agree = !!data.meta.letsencrypt_agree;
4272
}
73+
if (typeof data.meta !== 'undefined' && typeof data.meta.cloudflare_use !== 'undefined') {
74+
data.meta.cloudflare_use = !!data.meta.cloudflare_use;
75+
}
4376

4477
if (typeof data.domain_names === 'string' && data.domain_names) {
4578
data.domain_names = data.domain_names.split(',');
@@ -81,6 +114,7 @@ module.exports = Mn.View.extend({
81114
}
82115

83116
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
117+
this.ui.save.addClass('btn-loading');
84118

85119
// compile file data
86120
let form_data = new FormData();
@@ -119,6 +153,7 @@ module.exports = Mn.View.extend({
119153
.catch(err => {
120154
alert(err.message);
121155
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
156+
this.ui.save.removeClass('btn-loading');
122157
});
123158
}
124159
},
@@ -130,6 +165,10 @@ module.exports = Mn.View.extend({
130165

131166
getLetsencryptAgree: function () {
132167
return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
168+
},
169+
170+
getCloudflareUse: function () {
171+
return typeof this.meta.cloudflare_use !== 'undefined' ? this.meta.cloudflare_use : false;
133172
}
134173
},
135174

@@ -144,8 +183,9 @@ module.exports = Mn.View.extend({
144183
text: input
145184
};
146185
},
147-
createFilter: /^(?:[^.*]+\.?)+[^.]$/
186+
createFilter: /^(?:[^.]+\.?)+[^.]$/
148187
});
188+
this.ui.cloudflare.hide();
149189
},
150190

151191
initialize: function (options) {

frontend/js/i18n/messages.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
"letsencrypt-email": "Email Address for Let's Encrypt",
102102
"letsencrypt-agree": "I Agree to the <a href=\"{url}\" target=\"_blank\">Let's Encrypt Terms of Service</a>",
103103
"delete-ssl": "The SSL certificates attached will NOT be removed, they will need to be removed manually.",
104-
"hosts-warning": "These domains must be already configured to point to this installation"
104+
"hosts-warning": "These domains must be already configured to point to this installation",
105+
"use-cloudflare": "Use CloudFlare DNS verification"
105106
},
106107
"proxy-hosts": {
107108
"title": "Proxy Hosts",

0 commit comments

Comments
 (0)