@@ -141,16 +141,11 @@ const internalCertificate = {
141
141
} ) ;
142
142
} )
143
143
. 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 ( ( ) => {
148
147
// 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 ) ;
154
149
} )
155
150
. then ( internalNginx . reload )
156
151
. then ( ( ) => {
@@ -162,15 +157,44 @@ const internalCertificate = {
162
157
} )
163
158
. catch ( ( err ) => {
164
159
// 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 )
169
161
. then ( internalNginx . reload )
170
162
. then ( ( ) => {
171
163
throw err ;
172
164
} ) ;
173
165
} ) ;
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
+ }
174
198
} )
175
199
. then ( ( ) => {
176
200
// At this point, the letsencrypt cert should exist on disk.
@@ -748,6 +772,39 @@ const internalCertificate = {
748
772
} ) ;
749
773
} ,
750
774
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
+
751
808
/**
752
809
* @param {Access } access
753
810
* @param {Object } data
@@ -761,7 +818,9 @@ const internalCertificate = {
761
818
} )
762
819
. then ( ( certificate ) => {
763
820
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 )
765
824
. then ( ( ) => {
766
825
return internalCertificate . getCertificateInfoFromFile ( '/etc/letsencrypt/live/npm-' + certificate . id + '/fullchain.pem' ) ;
767
826
} )
@@ -815,6 +874,29 @@ const internalCertificate = {
815
874
} ) ;
816
875
} ,
817
876
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
+
818
900
/**
819
901
* @param {Object } certificate the certificate row
820
902
* @param {Boolean } [throw_errors]
@@ -824,7 +906,6 @@ const internalCertificate = {
824
906
logger . info ( 'Revoking Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
825
907
826
908
let cmd = certbot_command + ' revoke --non-interactive ' +
827
- '--config "' + le_config + '" ' +
828
909
'--cert-path "/etc/letsencrypt/live/npm-' + certificate . id + '/fullchain.pem" ' +
829
910
'--delete-after-revoke ' +
830
911
( le_staging ? '--staging' : '' ) ;
0 commit comments