Skip to content

Commit 40e6fb7

Browse files
committed
Avoid race condition when renewing certificates
Only single certificate can be processed at the same time. Make sure to obtain exclusive lock before executing renewal command.
1 parent 51878bb commit 40e6fb7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

files/certbot_lock.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Managed by Puppet
2+
LOCKFILE="/var/lock/certbot"
3+
LOCKFD=99
4+
# private
5+
function _lock() { flock "$@" $LOCKFD; }
6+
function _release_lock() { _lock -u; _lock -xn && rm -f $LOCKFILE; }
7+
function _prepare_lock() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _release_lock EXIT; }
8+
9+
# on start
10+
_prepare_lock
11+
12+
# public
13+
function exlock() { _lock -x --timeout 30; } # obtain an exclusive lock
14+
function unlock() { _lock -u; } # drop a lock

manifests/init.pp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292

9393
contain letsencrypt::renew
9494

95+
file { '/usr/share/certbot_lock.sh':
96+
ensure => file,
97+
mode => '0544',
98+
content => file("${module_name}/certbot_lock.sh"),
99+
}
100+
95101
$certificates.each |$certificate, $properties| {
96102
letsencrypt::certonly { $certificate: * => $properties }
97103
}

templates/renew-script.sh.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
<%- @environment.each do |environment| -%>
33
export <%= environment %>
44
<%- end -%>
5+
if [ -f '/usr/share/certbot_lock.sh' ]; then
6+
. '/usr/share/misc/certbot_lock.sh'
7+
fi
8+
exlock
59
<%= @cron_cmd %>
10+
unlock

0 commit comments

Comments
 (0)