Skip to content

Commit 2d6861e

Browse files
committed
(PUP-11428) Log that we're exiting if a daemonized agent can't get its cert
Previously the state machine printed that it was exiting to stdout, which is fine for foreground runs. But when running daemonized, stdout is redirected to /dev/null, so there wasn't an indication that we were exiting. Now when running daemonized write the message to puppet's logging system, so it ends up in syslog.
1 parent 9f80fd7 commit 2d6861e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/puppet/ssl/state_machine.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def to_error(message, cause)
2727
detail.set_backtrace(cause.backtrace)
2828
Error.new(@machine, message, detail)
2929
end
30+
31+
def log_error(message)
32+
# When running daemonized we set stdout to /dev/null, so write to the log instead
33+
if Puppet[:daemonize]
34+
Puppet.err(message)
35+
else
36+
$stdout.puts(message)
37+
end
38+
end
3039
end
3140

3241
# Load existing CA certs or download them. Transition to NeedCRLs.
@@ -270,10 +279,10 @@ def initialize(machine)
270279
def next_state
271280
time = @machine.waitforcert
272281
if time < 1
273-
puts _("Exiting now because the waitforcert setting is set to 0.")
282+
log_error(_("Exiting now because the waitforcert setting is set to 0."))
274283
exit(1)
275284
elsif Time.now.to_i > @machine.wait_deadline
276-
puts _("Couldn't fetch certificate from CA server; you might still need to sign this agent's certificate (%{name}). Exiting now because the maxwaitforcert timeout has been exceeded.") % {name: Puppet[:certname] }
285+
log_error(_("Couldn't fetch certificate from CA server; you might still need to sign this agent's certificate (%{name}). Exiting now because the maxwaitforcert timeout has been exceeded.") % {name: Puppet[:certname] })
277286
exit(1)
278287
else
279288
Puppet.info(_("Will try again in %{time} seconds.") % {time: time})

spec/unit/ssl/state_machine_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
let(:refused_message) { %r{Connection refused|No connection could be made because the target machine actively refused it} }
2828

2929
before(:each) do
30+
Puppet[:daemonize] = false
3031
Puppet[:ssl_lockfile] = tmpfile('ssllock')
3132
allow(Kernel).to receive(:sleep)
3233
end

0 commit comments

Comments
 (0)