Skip to content

Commit

Permalink
#11 Introduce IClock dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mbican committed May 12, 2020
1 parent b5404b3 commit b886c86
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/LetsEncrypt/Internal/AcmeCertificateLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using McMaster.AspNetCore.LetsEncrypt.Accounts;
using McMaster.AspNetCore.LetsEncrypt.Internal.IO;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -37,6 +38,7 @@ internal class AcmeCertificateLoader : BackgroundService
private readonly IConfiguration _config;
private readonly TermsOfServiceChecker _tosChecker;
private readonly IEnumerable<ICertificateRepository> _certificateRepositories;
private readonly IClock _clock;

private const string ErrorMessage = "Failed to create certificate";

Expand All @@ -50,6 +52,7 @@ public AcmeCertificateLoader(
IConfiguration config,
TermsOfServiceChecker tosChecker,
IEnumerable<ICertificateRepository> certificateRepositories,
IClock clock,
IAccountStore? accountStore = default)
{
_selector = selector;
Expand All @@ -62,6 +65,7 @@ public AcmeCertificateLoader(
_config = config;
_tosChecker = tosChecker;
_certificateRepositories = certificateRepositories;
_clock = clock;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand Down Expand Up @@ -201,7 +205,7 @@ private async Task MonitorRenewal(CancellationToken cancellationToken)
{
if (!_selector.TryGet(domainName, out var cert)
|| cert == null
|| cert.NotAfter <= DateTime.Now + daysInAdvance.Value)
|| cert.NotAfter <= _clock.Now.LocalDateTime + daysInAdvance.Value)
{
await CreateCertificateAsync(domainNames, cancellationToken);
break;
Expand Down
9 changes: 9 additions & 0 deletions src/LetsEncrypt/Internal/IO/IClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace McMaster.AspNetCore.LetsEncrypt.Internal.IO
{
internal interface IClock
{
DateTimeOffset Now { get; }
}
}
9 changes: 9 additions & 0 deletions src/LetsEncrypt/Internal/IO/SystemClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace McMaster.AspNetCore.LetsEncrypt.Internal.IO
{
internal class SystemClock: IClock
{
public DateTimeOffset Now => DateTimeOffset.Now;
}
}

0 comments on commit b886c86

Please sign in to comment.