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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/letsencrypt_hooks
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ clean_challenge() {

deploy_cert() {
local DOMAIN="${1}" KEYFILE="${2}" CERTFILE="${3}" FULLCHAINFILE="${4}" CHAINFILE="${5}" TIMESTAMP="${6}"
local EXPIRY
if ! EXPIRY=$(date --date="$(openssl x509 -enddate -noout -in "$CERTFILE"|cut -d= -f 2)" +%s); then
echo "failed to get the expiry date"
fi

curl --silent --show-error --fail -XPOST \
--header "X-Hook-Secret: $HOOK_SECRET" \
--data-urlencode "domain=$DOMAIN" \
--data-urlencode "privkey@$KEYFILE" \
--data-urlencode "cert@$CERTFILE" \
--data-urlencode "fullchain@$FULLCHAINFILE" \
--data-urlencode "expiry=$EXPIRY" \
"http://127.0.0.1:$HOOK_SERVER_PORT/deploy-cert" || { echo "hook request (deploy_cert) failed" 1>&2; exit 1; }
}

Expand Down
14 changes: 13 additions & 1 deletion lib/resty/auto-ssl/jobs/renewal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ local function renew_check_cert_unlock(domain, storage, local_lock, distributed_
end

local function renew_check_cert(auto_ssl_instance, storage, domain)
ngx.log(ngx.NOTICE, "auto-ssl: checking certificate renewals for ", domain)

-- Attempt to retrieve expiry date from storage. If it is not found try renewal.
-- If expiry date is found, we attempt renewal if it's within 30 days.
local _, _, _, expiry = storage:get_cert(domain)
if expiry then
local now = ngx.now()
if now + (30 * 24 * 60 * 60) < expiry then
ngx.log(ngx.NOTICE, "auto-ssl: expiry date is more than 30 days out, skipping renewal: ", domain)
return
end
end

-- Before issuing a cert, create a local lock to ensure multiple workers
-- don't simultaneously try to register the same cert.
local local_lock, new_local_lock_err = lock:new("auto_ssl", { exptime = 30, timeout = 30 })
Expand Down Expand Up @@ -106,7 +119,6 @@ local function renew_check_cert(auto_ssl_instance, storage, domain)
-- Trigger a normal certificate issuance attempt, which dehydrated will
-- skip if the certificate already exists or renew if it's within the
-- configured time for renewals.
ngx.log(ngx.NOTICE, "auto-ssl: checking certificate renewals for ", domain)
local _, _, issue_err = ssl_provider.issue_cert(auto_ssl_instance, domain)
if issue_err then
ngx.log(ngx.ERR, "auto-ssl: issuing renewal certificate failed: ", err)
Expand Down
3 changes: 2 additions & 1 deletion lib/resty/auto-ssl/servers/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ return function(auto_ssl_instance)
assert(params["domain"])
assert(params["fullchain"])
assert(params["privkey"])
local _, err = storage:set_cert(params["domain"], params["fullchain"], params["privkey"], params["cert"])
assert(params["expiry"])
local _, err = storage:set_cert(params["domain"], params["fullchain"], params["privkey"], params["cert"], tonumber(params["expiry"]))
if err then
ngx.log(ngx.ERR, "auto-ssl: failed to set cert: ", err)
return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
Expand Down
5 changes: 3 additions & 2 deletions lib/resty/auto-ssl/storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ function _M.get_cert(self, domain)
end

local data = cjson.decode(json)
return data["fullchain_pem"], data["privkey_pem"], data["cert_pem"]
return data["fullchain_pem"], data["privkey_pem"], data["cert_pem"], data["expiry"]
end

function _M.set_cert(self, domain, fullchain_pem, privkey_pem, cert_pem)
function _M.set_cert(self, domain, fullchain_pem, privkey_pem, cert_pem, expiry)
-- Store the public certificate and private key as a single JSON string.
--
-- We use a single JSON string so that the storage adapter just has to store
Expand All @@ -44,6 +44,7 @@ function _M.set_cert(self, domain, fullchain_pem, privkey_pem, cert_pem)
fullchain_pem = fullchain_pem,
privkey_pem = privkey_pem,
cert_pem = cert_pem,
expiry = expiry,
})

-- Store the cert with the current timestamp, so the old certs are preserved
Expand Down
2 changes: 1 addition & 1 deletion t/file.t
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ received: Connection: close
received:
received: foo
--- error_log
(Longer than 30 days). Skipping
auto-ssl: checking certificate renewals for
auto-ssl: expiry date is more than 30 days out
--- no_error_log
[warn]
[error]
Expand Down
4 changes: 2 additions & 2 deletions t/redis.t
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ received: Connection: close
received:
received: foo
--- error_log
(Longer than 30 days). Skipping
auto-ssl: checking certificate renewals for
auto-ssl: expiry date is more than 30 days out
--- no_error_log
[warn]
[error]
Expand Down Expand Up @@ -549,8 +549,8 @@ received: Connection: close
received:
received: foo
--- error_log
(Longer than 30 days). Skipping
auto-ssl: checking certificate renewals for
auto-ssl: expiry date is more than 30 days out
--- no_error_log
[warn]
[error]
Expand Down