Skip to content

Commit

Permalink
Avoid race condition when renewing certificates
Browse files Browse the repository at this point in the history
Only single certificate can be processed at the same time. Make sure to
obtain exclusive lock before executing renewal command.
  • Loading branch information
deric committed Mar 31, 2023
1 parent 51878bb commit 40e6fb7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions files/certbot_lock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Managed by Puppet
LOCKFILE="/var/lock/certbot"
LOCKFD=99
# private
function _lock() { flock "$@" $LOCKFD; }
function _release_lock() { _lock -u; _lock -xn && rm -f $LOCKFILE; }
function _prepare_lock() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _release_lock EXIT; }

# on start
_prepare_lock

# public
function exlock() { _lock -x --timeout 30; } # obtain an exclusive lock
function unlock() { _lock -u; } # drop a lock
6 changes: 6 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@

contain letsencrypt::renew

file { '/usr/share/certbot_lock.sh':
ensure => file,
mode => '0544',
content => file("${module_name}/certbot_lock.sh"),
}

$certificates.each |$certificate, $properties| {
letsencrypt::certonly { $certificate: * => $properties }
}
Expand Down
5 changes: 5 additions & 0 deletions templates/renew-script.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
<%- @environment.each do |environment| -%>
export <%= environment %>
<%- end -%>
if [ -f '/usr/share/certbot_lock.sh' ]; then
. '/usr/share/misc/certbot_lock.sh'
fi
exlock
<%= @cron_cmd %>
unlock

0 comments on commit 40e6fb7

Please sign in to comment.