88require 'shellwords'
99require 'fileutils'
1010
11+ def debug_log ( message )
12+ # rubocop:disable Style/GlobalVars
13+ puts "DEBUG: #{ message } " if $DEBUG_MODE
14+ # rubocop:enable Style/GlobalVars
15+ end
16+
1117def run_domain_validation_hook ( hook , dn , subject_alternative_names = [ ] )
1218 if hook && File . exist? ( hook ) && File . executable? ( hook )
1319 domains = [ dn ] + subject_alternative_names
1420 # The hook script is responsible for its own argument parsing.
1521 # Pass domains as separate arguments for robustness.
1622 cmd = [ hook , *domains ]
23+ debug_log ( "Running domain validation hook: #{ cmd . join ( ' ' ) } " )
24+
1725 stdout , stderr , status = Open3 . capture3 ( cmd )
1826 status = status . to_i >> 8
1927 else
2028 stdout = stderr = 'domain validation hook not found or not executable'
2129 status = 255
2230 end
31+ debug_log ( "Hook stdout: #{ stdout } " )
32+ debug_log ( "Hook stderr: #{ stderr } " )
33+ debug_log ( "Hook status: #{ status } " )
2334 [ stdout , stderr , status ]
2435end
2536
@@ -33,7 +44,12 @@ def run_dehydrated(dehydrated_config, command)
3344 cmd_parts = [ DEHYDRATED , '--config' , dehydrated_config ]
3445 cmd_parts . concat ( Shellwords . split ( command ) )
3546
47+ debug_log ( "Running dehydrated: #{ cmd_parts . join ( ' ' ) } " )
3648 stdout , stderr , status = Open3 . capture3 ( *cmd_parts )
49+ status_code = status . to_i >> 8
50+ debug_log ( "Dehydrated stdout: #{ stdout } " )
51+ debug_log ( "Dehydrated stderr: #{ stderr } " )
52+ debug_log ( "Dehydrated status: #{ status_code } " )
3753
3854 [ stdout , stderr , status . to_i >> 8 ]
3955end
@@ -119,13 +135,15 @@ def update_csr(csr_content, csr_file, crt_file, ca_file)
119135end
120136
121137def handle_request ( fqdn , dn , config )
138+ debug_log ( "Handling request for fqdn: #{ fqdn } , dn: #{ dn } " )
122139 # set environment from config
123140 env = config [ 'dehydrated_environment' ]
124141 old_env = { }
125142 env . each do |key , value |
126143 old_env [ key ] = value
127144 ENV [ key ] = value
128145 end
146+ debug_log ( "Set environment: #{ env . inspect } " )
129147
130148 # set paths/filenames
131149 dehydrated_config = config [ 'dehydrated_config' ]
@@ -142,6 +160,7 @@ def handle_request(fqdn, dn, config)
142160 dehydrated_domain_validation_hook_script = config [ 'dehydrated_domain_validation_hook_script' ]
143161 dehydrated_hook_script = config [ 'dehydrated_hook_script' ]
144162
163+ debug_log ( "Using dehydrated config: #{ dehydrated_config } " )
145164 new_dn_config = {
146165 'letsencrypt_ca_hash' => letsencrypt_ca_hash ,
147166 'dn' => dn ,
@@ -154,6 +173,7 @@ def handle_request(fqdn, dn, config)
154173 new_dn_config
155174 end
156175
176+ debug_log ( "Current DN config: #{ current_dn_config . inspect } " )
157177 # clean up OCSP files as they are not supported by letsencrypt anymore.
158178 ocsp_file = "#{ crt_file } .ocsp"
159179 FileUtils . rm_f ( ocsp_file )
@@ -173,6 +193,7 @@ def handle_request(fqdn, dn, config)
173193 end
174194 end
175195 if needs_registration
196+ debug_log ( "Account registration needed for CA hash #{ letsencrypt_ca_hash } " )
176197 stdout , stderr , status = register_account ( dehydrated_config )
177198 return [ 'Account registration failed' , stdout , stderr , status ] if status . positive?
178199 end
@@ -192,10 +213,12 @@ def handle_request(fqdn, dn, config)
192213 end
193214 )
194215
216+ debug_log ( "Force update due to config change: #{ force_update } " )
195217 # update csr and force to
196218 force_update ||= update_csr ( csr_content , csr_file , crt_file , ca_file )
197219
198220 if !cert_still_valid? ( crt_file ) || force_update || !cert_still_valid? ( ca_file )
221+ debug_log ( "Certificate requires update. cert_still_valid?(crt_file): #{ cert_still_valid? ( crt_file ) } , force_update: #{ force_update } , cert_still_valid?(ca_file): #{ cert_still_valid? ( ca_file ) } " )
199222 if dehydrated_domain_validation_hook_script && !dehydrated_domain_validation_hook_script . empty?
200223 stdout , stderr , status = run_domain_validation_hook (
201224 dehydrated_domain_validation_hook_script ,
@@ -222,6 +245,7 @@ def handle_request(fqdn, dn, config)
222245 # we do this before the OCSP stuff as we have a valid cert already.
223246 File . write ( dn_config_file , JSON . generate ( new_dn_config ) )
224247
248+ debug_log ( "Restoring environment: #{ old_env . inspect } " )
225249 old_env . each do |key , value |
226250 ENV [ key ] = value
227251 end
@@ -239,6 +263,7 @@ def prepare_files(request_config)
239263 dehydrated_config = request_config [ 'dehydrated_config' ]
240264 dehydrated_config_content = request_config [ 'dehydrated_config_content' ]
241265
266+ debug_log ( "Preparing files in #{ request_base_dir } " )
242267 FileUtils . mkdir_p request_base_dir
243268 File . write ( dehydrated_config , dehydrated_config_content )
244269end
@@ -289,8 +314,14 @@ def write_status_file(requests_status, status_file, monitoring_status_file)
289314 File . write ( monitoring_status_file , output . join ( "\n " ) )
290315end
291316
317+ # rubocop:disable Style/GlobalVars
318+ $DEBUG_MODE = ARGV . delete ( '--debug' )
319+ # rubocop:enable Style/GlobalVars
320+
292321raise ArgumentError , 'Need to specify config.json as argument' if ARGV . empty?
293322
323+ debug_log ( "Starting dehydrated job runner. ARGV: #{ ARGV . inspect } " )
324+
294325dehydrated_host_config_file = ARGV [ 0 ]
295326dehydrated_host_config = JSON . parse ( File . read ( dehydrated_host_config_file ) )
296327dehydrated_requests_config_file = dehydrated_host_config [ 'dehydrated_requests_config' ]
@@ -300,13 +331,15 @@ def write_status_file(requests_status, status_file, monitoring_status_file)
300331dehydrated_status_file = dehydrated_host_config [ 'dehydrated_status_file' ]
301332dehydrated_monitoring_status_file = dehydrated_host_config [ 'dehydrated_monitoring_status_file' ]
302333DEHYDRATED = File . join ( dehydrated_git_dir , 'dehydrated' )
334+ debug_log ( "DEHYDRATED script path: #{ DEHYDRATED } " )
303335
304336request_status = run_config ( dehydrated_requests_config )
305337write_status_file (
306338 request_status ,
307339 dehydrated_status_file ,
308340 dehydrated_monitoring_status_file
309341)
342+ debug_log ( 'Dehydrated job runner finished.' )
310343
311344# rubocop:disable all
312345#{
0 commit comments