-
Notifications
You must be signed in to change notification settings - Fork 0
/
bncert-letsencrypt-functions.xml
435 lines (432 loc) · 26.9 KB
/
bncert-letsencrypt-functions.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<componentGroup>
<name>bncertletsencryptfunctions</name>
<show>0</show>
<functionDefinitionList>
<!-- Parameters for updating metadata -->
<actionDefinition name="bncertAddLetsEncryptModeChanges">
<actionList>
<!-- Let's Encrypt -->
<if>
<conditionRuleList>
<isTrue value="${use_existing_letsencrypt_certificate}"/>
</conditionRuleList>
<actionList>
<setInstallerVariable name="changes_to_perform_metadata" value="${changes_to_perform_metadata} "Configure web server to use an existing Let's Encrypt certificate and renew: ${existing_letsencrypt_certificate_crt}""/>
</actionList>
<elseActionList>
<setInstallerVariable name="changes_to_perform_metadata" value="${changes_to_perform_metadata} "Revoke existing Let's Encrypt certificate: ${existing_letsencrypt_certificate_crt}"">
<ruleList>
<isTrue value="${revoke_existing_letsencrypt_certificate}"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="changes_to_perform_metadata" value="${changes_to_perform_metadata} "Configure web server to use a free Let's Encrypt certificate for the domains: ${all_domains}""/>
</elseActionList>
</if>
<!-- Cron job -->
<setInstallerVariable name="changes_to_perform_metadata" value="${changes_to_perform_metadata} "Disable existing Let's Encrypt cron jobs configured with the tool" "Configure a cron job to automatically renew the certificates each month"">
<ruleList>
<isTrue value="${configure_cron}"/>
</ruleList>
</setInstallerVariable>
</actionList>
</actionDefinition>
<!-- Rules -->
<ruleDefinition name="bncertIsCronConfigured">
<actionList>
<setInstallerVariable name="sudo_prefix" value=""/>
<setInstallerVariable name="sudo_prefix" value="sudo ">
<ruleList>
<userTest username="bitnami"/>
</ruleList>
</setInstallerVariable>
<runCron cronArguments="-l" stdoutVariable="cron_stdout"/>
</actionList>
<ruleList>
<compareText text="${cron_stdout}" logic="contains" value="# bncert-autorenew"/>
</ruleList>
</ruleDefinition>
<ruleDefinition name="bncertIsLegoUpdated">
<actionList>
<runProgram program="${lego_binary}" programArguments="--version" abortOnError="0" showMessageOnError="0"/>
<setInstallerVariableFromRegEx name="existing_lego_version" pattern=".*lego version ([0-9.]+) .*" substitution="\1" text="${program_stdout}"/>
</actionList>
<ruleList>
<regExMatch text="${existing_lego_version}" logic="matches" pattern="^[0-9]+\.[0-9]+\.[0-9]+$"/>
<compareVersions version1="${existing_lego_version}" logic="greater_or_equal" version2="${bundled_lego_version}"/>
</ruleList>
</ruleDefinition>
<!-- Validations -->
<actionDefinition name="bncertValidateLetsEncryptCertificate">
<actionList>
<baseGetFirstElement list="${domains}" variable="first_domain"/>
<setInstallerVariable name="existing_letsencrypt_certificate_crt" value="${certificates_dir}/${first_domain}.crt"/>
<setInstallerVariable name="existing_letsencrypt_certificate_key" value="${certificates_dir}/${first_domain}.key"/>
<actionGroup>
<actionList>
<throwError text="A certificate was found but it is missing its associated .key file:
${existing_letsencrypt_certificate_crt}
Please fix this issue manually by adding the .key file, or remove the .crt file.">
<ruleList>
<fileTest condition="not_exists" path="${existing_letsencrypt_certificate_key}"/>
</ruleList>
</throwError>
<getDomainsFromCertificate path="${existing_letsencrypt_certificate_crt}" domainsVariable="detected_certificate_domains"/>
<throwError text="Could not get domains from existing certificate ${existing_letsencrypt_certificate_crt}!
Please enter a different domain list, or revoke it: https://docs.bitnami.com/general/how-to/understand-bncert/#manually-revoking-an-existing-certificate ">
<ruleList>
<stringTest text="${detected_certificate_domains}" type="empty"/>
</ruleList>
</throwError>
<!-- If a certificate was detected, do not allow the user to add additional domains afterwards -->
<actionGroup>
<actionList>
<setInstallerVariable name="use_existing_letsencrypt_certificate" value="1"/>
<!-- If we use an existing certificate, we cannot add new domains -->
<setInstallerVariable name="add_missing_domains" value="0"/>
<setInstallerVariable name="parameter(additional_configuration).parameter(add_missing_domains).ask" value="0"/>
<setInstallerVariable name="missing_domains" value=""/>
<!-- If no www domain was added into the certificate we cannot enable any of the redirections -->
<foreach variables="key" values="enable_nonwww_to_www_redirection enable_www_to_nonwww_redirection">
<actionList>
<setInstallerVariable name="${key}" value="0"/>
<setInstallerVariable name="parameter(additional_configuration).parameter(${key}).ask" value="0"/>
</actionList>
<ruleList>
<isWwwDomainProvided list="${domains}" negate="1"/>
</ruleList>
</foreach>
</actionList>
<ruleList>
<listMatch list1="${domains}" list2="${detected_certificate_domains}"/>
</ruleList>
</actionGroup>
<actionGroup>
<actionList>
<setInstallerVariable name="use_existing_letsencrypt_certificate" value="1"/>
<setInstallerVariable name="add_missing_domains" value="1"/>
<setInstallerVariable name="parameter(additional_configuration).parameter(add_missing_domains).ask" value="0"/>
<setInstallerVariable name="domains" value="${domains} ${missing_domains}"/>
<setInstallerVariable name="missing_domains" value=""/>
</actionList>
<ruleList>
<isFalse value="${use_existing_letsencrypt_certificate}"/>
<listMatch list1="${domains} ${missing_domains}" list2="${detected_certificate_domains}"/>
</ruleList>
</actionGroup>
<if>
<conditionRuleList>
<isTrue value="${use_existing_letsencrypt_certificate}"/>
</conditionRuleList>
<actionList>
<showWarning text="A certificate for the list of domains you entered already exists. It will be used instead of generating a new one."/>
</actionList>
<elseActionList>
<showQuestion title="Certificate domain mismatch" text="A certificate was found at ${existing_letsencrypt_certificate_crt}
It is registered for a different set of domains: ${detected_certificate_domains}.
Do you want to disable/revoke the existing certificate and create a new one?" variable="revoke_existing_letsencrypt_certificate" default="no"/>
<throwError text="Please fix the list of domains!">
<ruleList>
<isFalse value="${revoke_existing_letsencrypt_certificate}"/>
</ruleList>
</throwError>
</elseActionList>
</if>
</actionList>
<ruleList>
<fileExists path="${existing_letsencrypt_certificate_crt}"/>
</ruleList>
</actionGroup>
</actionList>
</actionDefinition>
<!-- Lego-related actions -->
<actionDefinition name="bncertFormatLegoOpts">
<parameterList>
<stringParameter name="action"/>
<stringParameter name="challenge" default="tls"/>
<stringParameter name="variable"/>
</parameterList>
<actionList>
<globalVariables names="${variable}"/>
<setInstallerVariable name="args" value="--path ${lego_dir} --email="${email}""/>
<setInstallerVariable name="args" value="${args} --accept-tos">
<ruleList>
<compareText text="${action}" logic="does_not_equal" value="renew"/>
</ruleList>
</setInstallerVariable>
<if>
<conditionRuleList>
<compareText text="${challenge}" logic="equals" value="http"/>
</conditionRuleList>
<actionList>
<!-- We only use HTTP for renew because it could fail due to the user's configuration -->
<!-- In such cases we show them a warning, but they're able to generate certificates properly -->
<setInstallerVariable name="args" value="${args} --http --http-timeout 30 --http.webroot ${lego_app_dir}"/>
</actionList>
<elseActionList>
<setInstallerVariable name="args" value="${args} --tls"/>
</elseActionList>
</if>
<foreach variables="domain" values="${all_domains}">
<actionList>
<setInstallerVariable name="args" value="${args} --domains=${domain}"/>
<!-- Revoke and renew actions only need the main domain -->
<break>
<ruleList>
<compareText text="${action}" logic="does_not_equal" value="run"/>
</ruleList>
</break>
</actionList>
</foreach>
<setInstallerVariable name="args" value="${args} --server=https://acme-staging-v02.api.letsencrypt.org/directory">
<ruleList>
<isTrue value="${use_staging}"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="${variable}" value="${args} ${action}"/>
</actionList>
</actionDefinition>
<actionDefinition name="bncertRunLego">
<parameterList>
<stringParameter name="action"/>
</parameterList>
<actionList>
<setInstallerVariable name="lego_action_text" value="creating">
<ruleList>
<compareText text="${action}" logic="equals" value="run"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="lego_action_text" value="revoking">
<ruleList>
<compareText text="${action}" logic="equals" value="revoke"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="lego_action_text" value="renewing">
<ruleList>
<compareText text="${action}" logic="equals" value="renew"/>
</ruleList>
</setInstallerVariable>
<bncertFormatLegoOpts action="${action}" variable="lego_args"/>
<runProgram program="${lego_binary}" programArguments="${lego_args}" abortOnError="0" showMessageOnError="0">
<onErrorActionList>
<grepText nocase="1" pattern="error" text="${program_stdout}" variable="lego_errors"/>
<stringModify text="${lego_errors}" logic="trim" variable="lego_errors"/>
<tailText text="${program_stdout}" lines="2" variable="lego_errors_tail"/>
<stringModify text="${lego_errors_tail}" logic="trim" variable="lego_errors_tail"/>
<setInstallerVariable name="lego_errors" value="${lego_errors_tail}
${program_stderr}">
<ruleList>
<stringTest text="${lego_errors}" type="empty"/>
</ruleList>
</setInstallerVariable>
<showText>
<text>
An error occurred ${lego_action_text} certificates with Let's Encrypt:
${lego_errors}
Please check our documentation and support forums, we'll be happy to help!
* Bitnami Documentation: ${bitnami_docs_url}
* Bitnami Community: ${bitnami_support_url}
</text>
</showText>
</onErrorActionList>
</runProgram>
<if>
<conditionRuleList>
<compareText text="${program_exit_code}" logic="equals" value="0"/>
</conditionRuleList>
<actionList>
<!-- This message will only be shown if the configuration is restored to the original state -->
<setInstallerVariable name="report_nonreversible_metadata" value="${report_nonreversible_metadata} "The original Let's Encrypt certificate was revoked, so it is not valid anymore"">
<ruleList>
<compareText text="${action}" logic="equals" value="revoke"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="report_nonreversible_metadata" value="${report_nonreversible_metadata} "The Let's Encrypt certificate was generated, but not revoked"">
<ruleList>
<compareText text="${action}" logic="equals" value="run"/>
</ruleList>
</setInstallerVariable>
<setInstallerVariable name="report_nonreversible_metadata" value="${report_nonreversible_metadata} "The Let's Encrypt certificate was revoked, so it cannot be used anymore"">
<ruleList>
<compareText text="${action}" logic="equals" value="renew"/>
</ruleList>
</setInstallerVariable>
</actionList>
<elseActionList>
<!-- Abort and force restore of configuration -->
<setInstallerVariable name="report_aborted" value="1"/>
<setInstallerVariable name="report_failures_metadata" value="${report_failures_metadata} "Running Let's Encrypt: Error ${lego_action_text} certificates""/>
</elseActionList>
</if>
</actionList>
</actionDefinition>
<actionDefinition name="bncertRemoveCronEntries">
<actionList>
<setInstallerVariable name="sudo_prefix" value=""/>
<setInstallerVariable name="sudo_prefix" value="sudo ">
<ruleList>
<userTest username="bitnami"/>
</ruleList>
</setInstallerVariable>
<!-- We won't consider failures in this step as fatal -->
<removeCronEntries id="bncert-autorenew" exitCodeVariable="cron_exitcode"/>
<setInstallerVariable name="report_failures_metadata" value="${report_failures_metadata} "Removing cron entry"">
<ruleList>
<compareText text="${cron_exitcode}" logic="does_not_equal" value="0"/>
</ruleList>
</setInstallerVariable>
</actionList>
</actionDefinition>
<actionDefinition name="bncertAddCronEntry">
<actionList>
<setInstallerVariable name="sudo_prefix" value=""/>
<setInstallerVariable name="sudo_prefix" value="sudo ">
<ruleList>
<userTest username="bitnami"/>
</ruleList>
</setInstallerVariable>
<bncertFormatLegoOpts action="renew" challenge="http" variable="lego_args"/>
<setInstallerVariable name="cron_entry" value="0 0 1 * * ${sudo_prefix}${lego_binary} ${lego_args} && ${sudo_prefix}${web_server_reload_command}"/>
<addCronEntry id="bncert-autorenew" entry="${cron_entry}" exitCodeVariable="cron_exitcode"/>
<actionGroup>
<actionList>
<setInstallerVariable name="report_aborted" value="1"/>
<setInstallerVariable name="report_failures_metadata" value="${report_failures_metadata} "Adding cron entry""/>
</actionList>
<ruleList>
<compareText text="${cron_exitcode}" logic="does_not_equal" value="0"/>
</ruleList>
</actionGroup>
</actionList>
</actionDefinition>
<actionDefinition name="bncertConfigureLetsEncryptChallengeDir">
<actionList>
<createDirectory path="${lego_app_dir}/.well-known"/>
<!-- Variables used by Apache base functions are different than the ones used by this project -->
<globalVariables names="web_server_configuration_directory lego_installation_directory"/>
<setInstallerVariable name="web_server_configuration_directory" value="${web_server_conf_dir}"/>
<setInstallerVariable name="lego_installation_directory" value="${lego_app_dir}"/>
<apacheRegisterApp applicationId="lego"/>
<apacheConfigurationForApp appName=".well-known" appDirectory="${lego_app_dir}" appWebDirectory="${lego_app_dir}/.well-known" apacheAllowOverride="None" disableBitNamiBanner="1"/>
<deleteFile path="${lego_app_dir}/conf/httpd-vhosts.conf"/>
</actionList>
</actionDefinition>
<actionDefinition name="bncertCreateLetsEncryptCertificates">
<actionList>
<baseGetFirstElement list="${domains}" variable="first_domain"/>
<!-- Create new certificates with Lego (or renew existing ones) -->
<if>
<conditionRuleList>
<isTrue value="${use_existing_letsencrypt_certificate}"/>
</conditionRuleList>
<actionList>
<bncertRunLego action="renew" abortOnError="0"/>
</actionList>
<elseActionList>
<bncertRunLego action="revoke" abortOnError="0">
<ruleList>
<isTrue value="${revoke_existing_letsencrypt_certificate}"/>
</ruleList>
</bncertRunLego>
<bncertRunLego action="run" abortOnError="0">
<ruleList>
<isFalse value="${report_aborted}"/>
</ruleList>
</bncertRunLego>
</elseActionList>
</if>
<!-- Check that Lego was executed properly -->
<actionGroup>
<actionList>
<throwError abortOnError="0" text="Something went wrong when creating the certificates, and there is not any valid one at the certificates folder ${certificates_dir}"/>
<setInstallerVariable name="report_failures_metadata" value="${report_failures_metadata} "Creating Let's Encrypt certificate: Certificates were not created""/>
<setInstallerVariable name="report_aborted" value="1"/>
</actionList>
<ruleList>
<fileTest condition="not_exists" path="${certificates_dir}/${first_domain}.crt"/>
<isFalse value="${report_aborted}"/>
</ruleList>
</actionGroup>
<!-- If it succeeded, fix permissions and configure web server to use those certificates -->
<actionGroup>
<actionList>
<!-- Fix permissions for certificates -->
<changePermissions permissions="755" files="${certificates_dir}"/>
<changePermissions permissions="644" files="${certificates_dir}/${first_domain}.*"/>
<!-- Configure web server to use the new certificates -->
<bncertConfigureCertificates keyFile="${certificates_dir}/${first_domain}.key" crtFile="${certificates_dir}/${first_domain}.crt"/>
</actionList>
<ruleList>
<isFalse value="${report_aborted}"/>
</ruleList>
</actionGroup>
</actionList>
</actionDefinition>
<actionDefinition name="bncertPerformActionsForLetsEncryptMode">
<actionList>
<showProgressDialog title="Configuring server">
<actionList>
<createDirectory path="${lego_dir}"/>
<unpackFile component="lego" folder="lego" origin="lego" destination="${lego_binary}" progressText="Extracting Lego">
<ruleList>
<bncertIsLegoUpdated negate="1"/>
</ruleList>
</unpackFile>
<bncertConfigureLetsEncryptChallengeDir>
<ruleList>
<fileTest condition="not_exists" path="${lego_app_dir}"/>
</ruleList>
</bncertConfigureLetsEncryptChallengeDir>
<bncertCreateLetsEncryptCertificates progressText="Configuring Let's Encrypt certificates"/>
<!-- Fix permissions so the "bitnami" user can run cron jobs -->
<baseRecursiveChown owner="bitnami" files="${lego_dir}"/>
<baseRecursiveChown owner="bitnami" files="${lego_app_dir}"/>
<baseRecursiveChmod type="files" permissions="664" path="${lego_app_dir}"/>
<baseRecursiveChmod type="directories" permissions="775" path="${lego_app_dir}"/>
</actionList>
<ruleList>
<isFalse value="${report_aborted}"/>
</ruleList>
</showProgressDialog>
<!-- Remove existing cron entries -->
<bncertRemoveCronEntries>
<ruleList>
<isFalse value="${report_aborted}"/>
<isTrue value="${configure_cron}"/>
</ruleList>
</bncertRemoveCronEntries>
<!-- Add new cron entries -->
<bncertAddCronEntry>
<ruleList>
<isFalse value="${report_aborted}"/>
<isTrue value="${configure_cron}"/>
</ruleList>
</bncertAddCronEntry>
</actionList>
</actionDefinition>
<actionDefinition name="bncertPerformFinalChecksForLetsEncryptMode">
<actionList>
<generateRandomValue length="10" variable="tmpFile"/>
<generateRandomValue length="10" variable="randomString"/>
<setInstallerVariable name="success" value="1"/>
<writeFile path="${lego_app_dir}/.well-known/${tmpFile}" text="${randomString}"/>
<foreach variables="domain" values="${domains}">
<actionList>
<httpGet abortOnError="0" showMessageOnError="0" url="https://${domain}/.well-known/${tmpFile}" filename="${system_temp_directory}/${tmpFile}"/>
<readFile abortOnError="0" showMessageOnError="0" path="${system_temp_directory}/${tmpFile}" name="result"/>
<actionGroup>
<actionList>
<logMessage text="Domain ${domain} did not pass HTTP challenge validation"/>
<setInstallerVariable name="success" value="0"/>
<setInstallerVariable name="report_failures_metadata" value="${report_failures_metadata} "Creating Let's Encrypt certificate: Automatic renewal not working""/>
<break/>
</actionList>
<ruleList>
<compareText text="${result}" logic="does_not_contain" value="${randomString}"/>
</ruleList>
</actionGroup>
</actionList>
</foreach>
<showWarning text="Certificates may not renew automatically, due to a web server configuration issue. For more information see: https://docs.bitnami.com/general/how-to/understand-bncert/#certificates-not-renewed-automatically">
<ruleList>
<isFalse value="${success}"/>
</ruleList>
</showWarning>
<deleteFile path="${lego_app_dir}/.well-known/${tmpFile}"/>
</actionList>
</actionDefinition>
</functionDefinitionList>
</componentGroup>