Skip to content

Commit 87a5017

Browse files
committed
Log successfully issued certificates.
Clean up error handling in the main update loop.
1 parent 6f77882 commit 87a5017

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

src/lib.rs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ async fn ngx_http_acme_update_certificates_for_issuer(
280280
continue;
281281
};
282282

283+
let order_id = order.cache_key();
284+
283285
{
284286
let locked = cert.read();
285287

@@ -290,9 +292,8 @@ async fn ngx_http_acme_update_certificates_for_issuer(
290292
if !locked.is_renewable() {
291293
ngx_log_debug!(
292294
log.as_ptr(),
293-
"acme: certificate \"{}/{}\" is not due for renewal",
294-
issuer.name,
295-
order.cache_key()
295+
"acme: certificate \"{issuer}/{order_id}\" is not due for renewal",
296+
issuer = issuer.name,
296297
);
297298
next = cmp::min(locked.next, next);
298299
continue;
@@ -308,71 +309,76 @@ async fn ngx_http_acme_update_certificates_for_issuer(
308309

309310
// Acme client wants &str and we already validated that the identifiers are valid UTF-8.
310311
let str_order = order.to_str_order(&*alloc);
311-
let res = client.new_certificate(&str_order).await;
312312

313-
let cert_next = match res {
313+
let cert_next = match client.new_certificate(&str_order).await {
314314
Ok(ref val) => {
315315
let pkey = Zeroizing::new(val.pkey.private_key_to_pem_pkcs8()?);
316316
let x509 = X509::from_pem(&val.chain)?;
317+
let now = Time::now();
317318

318-
let valid =
319-
TimeRange::from_x509(&x509).unwrap_or(TimeRange::new(Time::now(), Time::now()));
319+
let valid = TimeRange::from_x509(&x509).unwrap_or(TimeRange::new(now, now));
320320

321-
let next = match cert.write().set(&val.chain, &pkey, valid) {
322-
Ok(x) => x,
321+
let res = cert.write().set(&val.chain, &pkey, valid);
322+
323+
let next = match res {
324+
Ok(x) => {
325+
ngx_log_error!(
326+
NGX_LOG_INFO,
327+
log.as_ptr(),
328+
"acme certificate \"{}/{}\" issued, next update in {:?}",
329+
issuer.name,
330+
order_id,
331+
(x - now)
332+
);
333+
x
334+
}
323335
Err(err) => {
324336
ngx_log_error!(
325337
NGX_LOG_WARN,
326338
log.as_ptr(),
327-
"acme certificate \"{}/{}\" request failed: {}",
328-
issuer.name,
329-
order.cache_key(),
330-
err
339+
"{err} while updating certificate \"{issuer}/{order_id}\"",
340+
issuer = issuer.name,
331341
);
332-
Time::now() + ACME_MIN_INTERVAL
342+
now + ACME_MIN_INTERVAL
333343
}
334344
};
335345

336-
let _ =
337-
issuer.write_state_file(std::format!("{}.crt", order.cache_key()), &val.chain);
346+
// Write files even if we failed to update the shared zone.
347+
348+
let _ = issuer.write_state_file(std::format!("{order_id}.crt"), &val.chain);
338349

339350
if !matches!(order.key, conf::pkey::PrivateKey::File(_)) {
340-
let _ =
341-
issuer.write_state_file(std::format!("{}.key", order.cache_key()), &pkey);
351+
let _ = issuer.write_state_file(std::format!("{order_id}.key"), &pkey);
342352
}
343353

344354
next
345355
}
346-
Err(ref err) => {
347-
if err.is_invalid() {
348-
ngx_log_error!(
349-
NGX_LOG_ERR,
350-
log.as_ptr(),
351-
"acme certificate \"{}/{}\" request is not valid: {}",
352-
issuer.name,
353-
order.cache_key(),
354-
err
355-
);
356-
cert.write().set_invalid(&err);
357-
continue;
358-
}
356+
Err(ref err) if err.is_invalid() => {
357+
ngx_log_error!(
358+
NGX_LOG_ERR,
359+
log.as_ptr(),
360+
"{err} while updating certificate \"{issuer}/{order_id}\"",
361+
issuer = issuer.name,
362+
);
363+
cert.write().set_invalid(&err);
359364

365+
// We marked the order as invalid and will stop attempting to update it until the
366+
// next configuration reload. It should not affect the next update schedule.
367+
368+
continue;
369+
}
370+
Err(ref err) => {
371+
ngx_log_error!(
372+
NGX_LOG_WARN,
373+
log.as_ptr(),
374+
"{err} while updating certificate \"{issuer}/{order_id}\"",
375+
issuer = issuer.name,
376+
);
360377
cert.write().set_error(&err)
361378
}
362379
};
363380

364381
next = cmp::min(cert_next, next);
365-
366-
if let Err(e) = res {
367-
ngx_log_error!(
368-
NGX_LOG_WARN,
369-
log.as_ptr(),
370-
"acme certificate \"{}/{}\" request failed: {}",
371-
issuer.name,
372-
order.cache_key(),
373-
e
374-
);
375-
}
376382
}
377383
Ok(next)
378384
}

0 commit comments

Comments
 (0)