From 7378996d19618cde014ca7a25083743e0b688071 Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Wed, 5 Jun 2024 23:31:49 +0200 Subject: [PATCH] (feature): add cache property to get hosts with upcoming ssl expiration --- .../Entities/LetsEncrypt/RegistrationCache.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/LetsEncrypt/Entities/LetsEncrypt/RegistrationCache.cs b/src/LetsEncrypt/Entities/LetsEncrypt/RegistrationCache.cs index 0c49e77..1228735 100644 --- a/src/LetsEncrypt/Entities/LetsEncrypt/RegistrationCache.cs +++ b/src/LetsEncrypt/Entities/LetsEncrypt/RegistrationCache.cs @@ -26,7 +26,35 @@ public class RegistrationCache { public Uri? Location { get; set; } /// - /// + /// Returns a list of hosts with upcoming SSL expiry + /// + public string[] HostsWithUpcomingSslExpiry { + + get { + + var hostsWithUpcomingSslExpiry = new List(); + + if (CachedCerts == null) + return hostsWithUpcomingSslExpiry.ToArray(); + + foreach (var result in CachedCerts) { + var (subject, cachedChert) = result; + + if (cachedChert.Cert != null) { + var cert = new X509Certificate2(Encoding.ASCII.GetBytes(cachedChert.Cert)); + + // if it is about to expire, we need to refresh + if ((cert.NotAfter - DateTime.UtcNow).TotalDays < 30) + hostsWithUpcomingSslExpiry.Add(subject); + } + } + + return hostsWithUpcomingSslExpiry.ToArray(); + } + } + + /// + /// Returns cached certificate. Certs older than 30 days are not returned /// /// /// @@ -43,7 +71,6 @@ public class RegistrationCache { var cert = new X509Certificate2(Encoding.ASCII.GetBytes(cache.Cert)); - // if it is about to expire, we need to refresh if ((cert.NotAfter - DateTime.UtcNow).TotalDays < 30) return false;