Skip to content

Commit dbd655e

Browse files
committed
Rework letsencrypt::certonly to allow deprovisioning
+ Added $ensure attribute + Renamed $ensure_cron back to $manage_cron + Returned $manage_cron back to a Boolean value + Made execute of "letsencrypt certonly ${title}" conditional to $ensure == 'present' + Added cleanup of directory for domain certs when $ensure == 'absent' + Used global $ensure to signal desired state of cronjobs
1 parent 7026502 commit dbd655e

File tree

3 files changed

+114
-63
lines changed

3 files changed

+114
-63
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ letsencrypt::certonly { 'foo':
189189

190190
#### Cron
191191

192-
* `ensure_cron` can be used to automatically renew the certificate
192+
* `manage_cron` can be used to automatically renew the certificate
193193
* `cron_success_command` can be used to run a shell command on a successful renewal
194194
* `cron_before_command` can be used to run a shell command before a renewal
195195
* `cron_monthday` can be used to specify one or multiple days of the month to run the cron job (defaults to every day)
@@ -200,7 +200,7 @@ letsencrypt::certonly { 'foo':
200200
```puppet
201201
letsencrypt::certonly { 'foo':
202202
domains => ['foo.example.com', 'bar.example.com'],
203-
ensure_cron => 'present',
203+
manage_cron => true,
204204
cron_hour => [0,12],
205205
cron_minute => '30',
206206
cron_before_command => 'service nginx stop',
@@ -209,6 +209,21 @@ letsencrypt::certonly { 'foo':
209209
}
210210
```
211211

212+
#### Deprovisioning
213+
214+
If a domain needs to be removed for any reason this can be done by setting
215+
`ensure` to 'absent', this will remove the certificates for this domain from
216+
the server. If `manage_cron` is set to true, the certificate renewal cronjob
217+
and shell scripts for the domain will also be removed.
218+
219+
```puppet
220+
letsencrypt::certonly { 'foo':
221+
ensure => 'absent',
222+
domains => ['foo.example.com', 'bar.example.com'],
223+
manage_cron => true,
224+
}
225+
```
226+
212227
## Development
213228

214229
1. Fork it

manifests/certonly.pp

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#
66
# === Parameters:
77
#
8+
# [*ensure*]
9+
# Intended state of the resource. Accepts either 'present' or 'absent'.
10+
# Default: 'present'.
11+
# Will remove certificates for specified domains if set to 'absent'. Will
12+
# also remove cronjobs and renewal scripts if `manage_cron` is set to 'true'.
813
# [*domains*]
914
# Namevar. An array of domains to include in the CSR.
1015
# [*custom_plugin*]
@@ -23,9 +28,8 @@
2328
# `letsencrypt-auto` command.
2429
# [*environment*]
2530
# An optional array of environment variables (in addition to VENV_PATH).
26-
# [*ensure_cron*]
27-
# Intended state of the cron and helper script resources. Accepts either
28-
# 'present' or 'absent'. Default: 'absent'
31+
# [*manage_cron*]
32+
# Boolean indicating whether or not to schedule cron job for renewal. Default: 'false'.
2933
# Runs daily but only renews if near expiration, e.g. within 10 days.
3034
# [*cron_before_command*]
3135
# String representation of a command that should be run before renewal command
@@ -40,6 +44,7 @@
4044
# e.g. 0 or '00' or [0,30]. Default - seeded random minute.
4145
#
4246
define letsencrypt::certonly (
47+
Enum['present','absent'] $ensure = 'present',
4348
Array[String[1]] $domains = [$title],
4449
Boolean $custom_plugin = false,
4550
Letsencrypt::Plugin $plugin = 'standalone',
@@ -48,7 +53,7 @@
4853
Integer[2048] $key_size = $letsencrypt::key_size,
4954
Array[String[1]] $additional_args = [],
5055
Array[String[1]] $environment = [],
51-
Enum['present','absent'] $ensure_cron = 'absent',
56+
Boolean $manage_cron = false,
5257
Boolean $suppress_cron_output = false,
5358
Optional[String[1]] $cron_before_command = undef,
5459
Optional[String[1]] $cron_success_command = undef,
@@ -62,10 +67,14 @@
6267
fail("The 'webroot_paths' parameter must be specified when using the 'webroot' plugin")
6368
}
6469

65-
if ($custom_plugin) {
66-
$command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} "
70+
if $ensure == 'present' {
71+
if ($custom_plugin) {
72+
$command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} "
73+
} else {
74+
$command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} -a ${plugin} "
75+
}
6776
} else {
68-
$command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} -a ${plugin} "
77+
$command_start = "${letsencrypt_command} --text --agree-tos --non-interactive delete "
6978
}
7079

7180
case $plugin {
@@ -93,10 +102,13 @@
93102
}
94103

95104
default: {
96-
$_command_domains = join($domains, ' -d ')
97-
$command_domains = "--cert-name ${title} -d ${_command_domains}"
105+
if $ensure == 'present' {
106+
$_command_domains = join($domains, ' -d ')
107+
$command_domains = "--cert-name ${title} -d ${_command_domains}"
108+
} else {
109+
$command_domains = "--cert-name ${title}"
110+
}
98111
}
99-
100112
}
101113

102114
if empty($additional_args) {
@@ -106,29 +118,35 @@
106118
$command_end = join(['',] + $additional_args, ' ')
107119
}
108120

109-
$command = "${command_start}${command_domains}${command_end}"
110-
111121
# certbot uses --cert-name to generate the file path
112122
$live_path_certname = regsubst($title, '^\*\.', '')
113123
$live_path = "${config_dir}/live/${live_path_certname}/cert.pem"
114124

115125
$execution_environment = [ "VENV_PATH=${letsencrypt::venv_path}", ] + $environment
116126
$verify_domains = join(unique($domains), ' ')
127+
128+
if $ensure == 'present' {
129+
$exec_ensure = { 'unless' => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}" }
130+
} else {
131+
$exec_ensure = { 'onlyif' => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}" }
132+
}
133+
117134
exec { "letsencrypt certonly ${title}":
118-
command => $command,
135+
command => "${command_start}${command_domains}${command_end}",
136+
* => $exec_ensure,
119137
path => $facts['path'],
120138
environment => $execution_environment,
121-
unless => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}",
122139
provider => 'shell',
123140
require => [
124141
Class['letsencrypt'],
125142
File['/usr/local/sbin/letsencrypt-domain-validation'],
126143
],
127144
}
128145

129-
if $ensure_cron == 'present' {
146+
if $manage_cron {
130147
$maincommand = "${command_start}--keep-until-expiring ${command_domains}${command_end}"
131-
$cron_script_ensure = 'file'
148+
$cron_script_ensure = $ensure ? { 'present' => 'file', default => 'absent' }
149+
$cron_ensure = $ensure
132150

133151
if $suppress_cron_output {
134152
$croncommand = "${maincommand} > /dev/null 2>&1"
@@ -145,25 +163,22 @@
145163
} else {
146164
$cron_cmd = $renewcommand
147165
}
148-
} else {
149-
$cron_script_ensure = 'absent'
150-
}
151166

152-
file { "${letsencrypt::cron_scripts_path}/renew-${title}.sh":
153-
ensure => $cron_script_ensure,
154-
mode => '0755',
155-
owner => 'root',
156-
group => $letsencrypt::cron_owner_group,
157-
content => template('letsencrypt/renew-script.sh.erb'),
158-
}
167+
file { "${letsencrypt::cron_scripts_path}/renew-${title}.sh":
168+
ensure => $cron_script_ensure,
169+
mode => '0755',
170+
owner => 'root',
171+
group => $letsencrypt::cron_owner_group,
172+
content => template('letsencrypt/renew-script.sh.erb'),
173+
}
159174

160-
cron { "letsencrypt renew cron ${title}":
161-
ensure => $ensure_cron,
162-
command => "\"${letsencrypt::cron_scripts_path}/renew-${title}.sh\"",
163-
user => root,
164-
hour => $cron_hour,
165-
minute => $cron_minute,
166-
monthday => $cron_monthday,
175+
cron { "letsencrypt renew cron ${title}":
176+
ensure => $cron_ensure,
177+
command => "\"${letsencrypt::cron_scripts_path}/renew-${title}.sh\"",
178+
user => root,
179+
hour => $cron_hour,
180+
minute => $cron_minute,
181+
monthday => $cron_monthday,
182+
}
167183
}
168-
169184
}

0 commit comments

Comments
 (0)