Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only attempt renewal of certificates that are close to expiry date #111

Merged
merged 3 commits into from
Jan 29, 2018

Conversation

brianlund
Copy link
Contributor

Based on the discussion in #110 on high cpu load when attempting renewal on large amounts of certificates. This code limits renewal attempts to the last 30 days of a certificates validity.

When new certificates are generated, the expiry date is extracted from the certificate with openssl and saved to the storage adapter.
When the renewal job is triggered, it attempts to get the expiry date from storage:

  • if expiry date is not found (certificate was stored before this was implemented) it continues as normal and a renewal is attempted with dehydrate.
  • if expiry date is found, it's evaluated against the current time and if it's less than 30 days out, a renewal is attempted.

I had to adjust the tests in redis.t and file.t as the renewal skipped message is now coming from auto-ssl and not dehydrated. I also moved the "checking certificate renewals for" ngx.NOTICE to the start of the function so it's not only logged when we actually call dehydrated.

The code for extracting and storing the expiry date was taken from the fork at https://github.com/ryokdy/lua-resty-auto-ssl

Stored expiry date is checked before renewal. If less than 30 days out
renewal is attempted. Tests are update to reflect the fact that
dehydrated is not going to be called.
@luto
Copy link
Collaborator

luto commented Dec 20, 2017

Looks good to me.

@GUI what do you think?

@GUI
Copy link
Collaborator

GUI commented Dec 25, 2017

@brianlund: Thanks for the PR! Sorry about the belated response (been swamped with other things), but I think all this looks good and makes sense. I'd just like to do a bit more benchmarking before merging and shipping this (just so I personally have a better sense of where any other bottlenecks might be), but I'm currently tied up with some other things, so it might be a week or two before I can get to it. But I think we definitely want to get this merged in, so thanks again!

local EXPIRY=$(date --date="$(openssl x509 -enddate -noout -in "$CERTFILE"|cut -d= -f 2)" +%s)
if [ $? -ne 0 ]; then
echo "failed to get the expiry date."
exit 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether this should really be a fatal error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree as the certificate deployment will work fine even if EXPIRY is not set.
In addition I see that the check would actually never fail. When declaring and setting a local variable in a single command, it's first set and then restricted to local scope, so $? reflects the local declaration, not the variable assignment.

I propose testing if $EXPIRY is empty and if so logging the problem but skip the fatal error. What do you think?

@luto
Copy link
Collaborator

luto commented Dec 28, 2017 via email

EXPIRY is declared seperately to avoid masking return value
@GUI GUI merged commit cb60bd3 into auto-ssl:master Jan 29, 2018
@GUI GUI added this to the v0.12.0 milestone Jan 29, 2018
@brianlund brianlund deleted the issue-#110 branch January 29, 2018 09:22
@GUI
Copy link
Collaborator

GUI commented Feb 5, 2018

Many thanks for tracking this down and for the pull request! It's now in the v0.12.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants