@@ -77,7 +77,7 @@ const internalCertificate = {
77
77
. where ( 'id' , certificate . id )
78
78
. andWhere ( 'provider' , 'letsencrypt' )
79
79
. patch ( {
80
- expires_on : certificateModel . raw ( 'FROM_UNIXTIME(' + cert_info . dates . to + ') ')
80
+ expires_on : moment ( cert_info . dates . to , 'X' ) . format ( 'YYYY-MM-DD HH:mm:ss ')
81
81
} ) ;
82
82
} )
83
83
. catch ( ( err ) => {
@@ -141,36 +141,60 @@ 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 ) ;
154
- } )
155
- . then ( internalNginx . reload )
156
- . then ( ( ) => {
157
- // 6. Re-instate previously disabled hosts
158
- return internalCertificate . enableInUseHosts ( in_use_result ) ;
159
- } )
160
- . then ( ( ) => {
161
- return certificate ;
148
+ return internalCertificate . requestLetsEncryptCloudFlareDnsSsl ( certificate , data . meta . cloudflare_token ) ;
162
149
} )
163
- . catch ( ( err ) => {
164
- // 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
- } )
169
- . then ( internalNginx . reload )
170
- . then ( ( ) => {
171
- throw err ;
172
- } ) ;
173
- } ) ;
150
+ . then ( internalNginx . reload )
151
+ . then ( ( ) => {
152
+ // 6. Re-instate previously disabled hosts
153
+ return internalCertificate . enableInUseHosts ( in_use_result ) ;
154
+ } )
155
+ . then ( ( ) => {
156
+ return certificate ;
157
+ } )
158
+ . catch ( ( err ) => {
159
+ // In the event of failure, revert things and throw err back
160
+ return internalCertificate . enableInUseHosts ( in_use_result )
161
+ . then ( internalNginx . reload )
162
+ . then ( ( ) => {
163
+ throw err ;
164
+ } ) ;
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.
@@ -180,7 +204,7 @@ const internalCertificate = {
180
204
return certificateModel
181
205
. query ( )
182
206
. patchAndFetchById ( certificate . id , {
183
- expires_on : certificateModel . raw ( 'FROM_UNIXTIME(' + cert_info . dates . to + ') ')
207
+ expires_on : moment ( cert_info . dates . to , 'X' ) . format ( 'YYYY-MM-DD HH:mm:ss ')
184
208
} )
185
209
. then ( ( saved_row ) => {
186
210
// Add cert data for audit log
@@ -558,7 +582,7 @@ const internalCertificate = {
558
582
// TODO: This uses a mysql only raw function that won't translate to postgres
559
583
return internalCertificate . update ( access , {
560
584
id : data . id ,
561
- expires_on : certificateModel . raw ( 'FROM_UNIXTIME(' + validations . certificate . dates . to + ') ') ,
585
+ expires_on : moment ( validations . certificate . dates . to , 'X' ) . format ( 'YYYY-MM-DD HH:mm:ss ') ,
562
586
domain_names : [ validations . certificate . cn ] ,
563
587
meta : _ . clone ( row . meta ) // Prevent the update method from changing this value that we'll use later
564
588
} )
@@ -733,7 +757,6 @@ const internalCertificate = {
733
757
'--agree-tos ' +
734
758
'--email "' + certificate . meta . letsencrypt_email + '" ' +
735
759
'--preferred-challenges "dns,http" ' +
736
- '--webroot ' +
737
760
'--domains "' + certificate . domain_names . join ( ',' ) + '" ' +
738
761
( le_staging ? '--staging' : '' ) ;
739
762
@@ -748,6 +771,39 @@ const internalCertificate = {
748
771
} ) ;
749
772
} ,
750
773
774
+ /**
775
+ * @param {Object } certificate the certificate row
776
+ * @param {String } apiToken the cloudflare api token
777
+ * @returns {Promise }
778
+ */
779
+ requestLetsEncryptCloudFlareDnsSsl : ( certificate , apiToken ) => {
780
+ logger . info ( 'Requesting Let\'sEncrypt certificates via Cloudflare DNS for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
781
+
782
+ let tokenLoc = '~/cloudflare-token' ;
783
+ let storeKey = 'echo "dns_cloudflare_api_token = ' + apiToken + '" > ' + tokenLoc ;
784
+
785
+ let cmd =
786
+ storeKey + ' && ' +
787
+ certbot_command + ' certonly --non-interactive ' +
788
+ '--cert-name "npm-' + certificate . id + '" ' +
789
+ '--agree-tos ' +
790
+ '--email "' + certificate . meta . letsencrypt_email + '" ' +
791
+ '--domains "' + certificate . domain_names . join ( ',' ) + '" ' +
792
+ '--dns-cloudflare --dns-cloudflare-credentials ' + tokenLoc +
793
+ ( le_staging ? ' --staging' : '' )
794
+ + ' && rm ' + tokenLoc ;
795
+
796
+ if ( debug_mode ) {
797
+ logger . info ( 'Command:' , cmd ) ;
798
+ }
799
+
800
+ return utils . exec ( cmd ) . then ( ( result ) => {
801
+ logger . info ( result ) ;
802
+ return result ;
803
+ } ) ;
804
+ } ,
805
+
806
+
751
807
/**
752
808
* @param {Access } access
753
809
* @param {Object } data
@@ -761,15 +817,17 @@ const internalCertificate = {
761
817
} )
762
818
. then ( ( certificate ) => {
763
819
if ( certificate . provider === 'letsencrypt' ) {
764
- return internalCertificate . renewLetsEncryptSsl ( certificate )
820
+ let renewMethod = certificate . meta . cloudflare_use ? internalCertificate . renewLetsEncryptCloudFlareSsl : internalCertificate . renewLetsEncryptSsl ;
821
+
822
+ return renewMethod ( certificate )
765
823
. then ( ( ) => {
766
824
return internalCertificate . getCertificateInfoFromFile ( '/etc/letsencrypt/live/npm-' + certificate . id + '/fullchain.pem' ) ;
767
825
} )
768
826
. then ( ( cert_info ) => {
769
827
return certificateModel
770
828
. query ( )
771
829
. patchAndFetchById ( certificate . id , {
772
- expires_on : certificateModel . raw ( 'FROM_UNIXTIME(' + cert_info . dates . to + ') ')
830
+ expires_on : moment ( cert_info . dates . to , 'X' ) . format ( 'YYYY-MM-DD HH:mm:ss ')
773
831
} ) ;
774
832
} )
775
833
. then ( ( updated_certificate ) => {
@@ -815,6 +873,29 @@ const internalCertificate = {
815
873
} ) ;
816
874
} ,
817
875
876
+ /**
877
+ * @param {Object } certificate the certificate row
878
+ * @returns {Promise }
879
+ */
880
+ renewLetsEncryptCloudFlareSsl : ( certificate ) => {
881
+ logger . info ( 'Renewing Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
882
+
883
+ let cmd = certbot_command + ' renew --non-interactive ' +
884
+ '--cert-name "npm-' + certificate . id + '" ' +
885
+ '--disable-hook-validation ' +
886
+ ( le_staging ? '--staging' : '' ) ;
887
+
888
+ if ( debug_mode ) {
889
+ logger . info ( 'Command:' , cmd ) ;
890
+ }
891
+
892
+ return utils . exec ( cmd )
893
+ . then ( ( result ) => {
894
+ logger . info ( result ) ;
895
+ return result ;
896
+ } ) ;
897
+ } ,
898
+
818
899
/**
819
900
* @param {Object } certificate the certificate row
820
901
* @param {Boolean } [throw_errors]
@@ -824,7 +905,6 @@ const internalCertificate = {
824
905
logger . info ( 'Revoking Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
825
906
826
907
let cmd = certbot_command + ' revoke --non-interactive ' +
827
- '--config "' + le_config + '" ' +
828
908
'--cert-path "/etc/letsencrypt/live/npm-' + certificate . id + '/fullchain.pem" ' +
829
909
'--delete-after-revoke ' +
830
910
( le_staging ? '--staging' : '' ) ;
0 commit comments