Actually set FAILED_DOMAINS and RENEWED_DOMAINS variables when renewals fail (#10347)

Fixes https://github.com/certbot/certbot/issues/10259

This PR moves post-hook execution from `main.renew` to
`renewal.handle_renewal_request` so that failed and renewed domains
actually get passed into post-hook execution as promised, even when
failures happened.

I suspect the original PR was being overly cautious by putting the whole
thing into a try/finally so that post-hooks definitely happen, but
`handle_renewal_request` is already full of exception catching. I
understand the worry about executing a pre-hook and then failing to
execute its matching post-hook, but the code really is already
structured to make sure that that won't happen. And then when we added
`FAILED_DOMAINS` and `RENEWED_DOMAINS`, we both kept that
overly-cautious hooks execution location, but also kept the error so we
have a summary at the end...which meant that if failures happened, the
env vars were never set.

If we really want to keep the `hooks.run_saved_post_hooks` call on the
outside of everything in main, we can, but then we will have to do one
of the following:
- pass in the output lists to be filled out during execution. not my
favorite pattern
- throw the output lists in the error object or make a wrapper error,
not sure, haven't looked at `errors.py` too closely
- stop raising that final error where we report failures at the very
bottom. it's a little outdated maybe but I do like it and I think people
are used to it
- raise that error in main, returning the number of parse and renewal
failures. this is my favorite of the options, but I still like it less
than what I've implemented here.

Here's the integration/regression test failing on main:

https://dev.azure.com/certbot/certbot/_build/results?buildId=9237&view=logs&j=fca58cec-e7ce-563a-f36f-5c233894d750
You can see here that that branch just has the integration test without
the fix (and removing other tests for efficiency):
https://github.com/certbot/certbot/compare/main...test-fail-env-on-main

It's the default, but just to be clear, this should definitely have two
reviewers.
This commit is contained in:
ohemorange
2025-06-20 07:42:20 -07:00
committed by GitHub
parent a7e4ffb13b
commit 035a6dcc39
4 changed files with 41 additions and 12 deletions
@@ -442,6 +442,40 @@ def test_renew_hook_override(context: IntegrationTestsContext) -> None:
assert_hook_execution(context.hook_probe, 'deploy_override')
def test_renew_hook_env_vars(context: IntegrationTestsContext) -> None:
fail_domain = context.get_domain('fail-env')
context.certbot([
'certonly', '-d', fail_domain,
'--preferred-challenges', 'http-01'
])
context.certbot([
'renew',
'--post-hook', f'printenv RENEWED_DOMAINS >> {context.hook_probe}'
])
assert_hook_execution(context.hook_probe, fail_domain)
# Clear probe
with open(context.hook_probe, 'w'):
pass
# now renew using manual dns, which will fail on renew
# manual_dns_auth_hook from misc is designed to fail if the domain contains 'fail-*'.
with pytest.raises(subprocess.CalledProcessError):
context.certbot([
'renew', '--cert-name', fail_domain,
'--preferred-challenges', 'dns',
'--manual-auth-hook', context.manual_dns_auth_hook,
'--manual-cleanup-hook', context.manual_dns_cleanup_hook,
'-a', 'manual',
'--post-hook', f'printenv FAILED_DOMAINS >> {context.hook_probe}',
'--dry-run', # use dry run here to deactivate previous authz, or this will pass
])
assert_hook_execution(context.hook_probe, fail_domain)
def test_invalid_domain_with_dns_challenge(context: IntegrationTestsContext) -> None:
"""Test certificate issuance failure with DNS-01 challenge."""
# Manual dns auth hooks from misc are designed to fail if the domain contains 'fail-*'.