@@ -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' : '' ) ;
0 commit comments