Skip to content

Commit 5924407

Browse files
committed
Incorporate safety wait into sign_csr
When signing CSRs, it is common that the CSR will have been submitted just prior. Previously, plans which did both would have to pause after submitting the CSR to ensure there was time for the server to have fully processed it before running the sign task, in order to avoid any chance of ready-to-sign timing problems. This commit makes the sign_csr task itself attempt to sign up to six times, waiting one second between attempts, in order to avoid needing to do that in the plan.
1 parent 8571dbe commit 5924407

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

plans/action/install.pp

-4
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@
349349
# Ensure certificate requests have been submitted, then run Puppet
350350
unless ($target in $database_targets) {
351351
run_task('peadm::submit_csr', $target)
352-
# TODO: come up with an intelligent way to validate that the expected CSRs
353-
# have been submitted and are available for signing, prior to signing them.
354-
# For now, waiting a short period of time is necessary to avoid a small race.
355-
ctrl::sleep(5)
356352
run_task('peadm::sign_csr', $master_target, { 'certnames' => [$target.name] } )
357353
run_task('peadm::puppet_runonce', $target)
358354
}

tasks/sign_csr.rb

+20-11
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@ def csr_signed?(certname)
1111
File.exist?(File.join(Puppet.settings[:cadir], 'signed', "#{certname}.pem"))
1212
end
1313

14+
def sign(certnames)
15+
cmd = ['/opt/puppetlabs/bin/puppetserver', 'ca', 'sign',
16+
'--certname', certnames.join(',')]
17+
18+
stdout, status = Open3.capture2(*cmd)
19+
puts stdout
20+
raise StandardError unless status.success?
21+
end
22+
1423
def main
1524
Puppet.initialize_settings
1625
params = JSON.parse(STDIN.read)
17-
unsigned = params['certnames'].reject { |name| csr_signed?(name) }
18-
19-
exit 0 if unsigned.empty?
2026

21-
cmd = ['/opt/puppetlabs/bin/puppetserver', 'ca', 'sign',
22-
'--certname', unsigned.join(',')]
27+
attempts = 0
2328

24-
stdout, status = Open3.capture2(*cmd)
25-
puts stdout
26-
if status.success?
27-
exit 0
28-
else
29-
exit 1
29+
begin
30+
unsigned = params['certnames'].reject { |name| csr_signed?(name) }
31+
exit 0 if unsigned.empty?
32+
sign(unsigned.join(','))
33+
rescue
34+
exit 1 if attempts > 6
35+
attempts += 1
36+
puts "Signing attempt #{attempts} failed; waiting 1s and trying again"
37+
sleep 1
38+
retry
3039
end
3140
end
3241

0 commit comments

Comments
 (0)