Skip to content

Commit

Permalink
Challenge fails with Connection refused (challenge type http-01) (#37)
Browse files Browse the repository at this point in the history
* New StartUpMode option to define when cert requests/renewals are initiated.
StartUpMode.Manual requires an explicit call to RunFluffySpoonLetsEncrypt.

* service name

* remove startupmode option
use IApplicationLifetime by default for startup

* remove startupmode
restore original hosted service def
  • Loading branch information
YetaWF authored Mar 18, 2020
1 parent 8382d12 commit 65eb855
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class LetsEncryptOptions
/// </summary>
public RenewalFailMode RenewalFailMode { get; set; } = RenewalFailMode.LogAndContinue;

/// <summary>
/// Gets or sets the <see cref="Certes.KeyAlgorithm"/> used to request a new LetsEncrypt certificate.
/// </summary>
public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
/// <summary>
/// Gets or sets the <see cref="Certes.KeyAlgorithm"/> used to request a new LetsEncrypt certificate.
/// </summary>
public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluffySpoon.AspNet.LetsEncrypt.Certificates;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using static FluffySpoon.AspNet.LetsEncrypt.Certificates.CertificateRenewalStatus;

Expand All @@ -14,6 +15,7 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
private readonly ICertificateProvider _certificateProvider;
private readonly IEnumerable<ICertificateRenewalLifecycleHook> _lifecycleHooks;
private readonly ILogger<ILetsEncryptRenewalService> _logger;
private readonly IApplicationLifetime _lifetime;
private readonly SemaphoreSlim _semaphoreSlim;
private readonly LetsEncryptOptions _options;

Expand All @@ -22,11 +24,13 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
public LetsEncryptRenewalService(
ICertificateProvider certificateProvider,
IEnumerable<ICertificateRenewalLifecycleHook> lifecycleHooks,
IApplicationLifetime lifetime,
ILogger<ILetsEncryptRenewalService> logger,
LetsEncryptOptions options)
{
_certificateProvider = certificateProvider;
_lifecycleHooks = lifecycleHooks;
_lifetime = lifetime;
_logger = logger;
_options = options;
_semaphoreSlim = new SemaphoreSlim(1);
Expand All @@ -45,10 +49,12 @@ public async Task StartAsync(CancellationToken cancellationToken)
" which means that the LetsEncrypt certificate will never renew.");
}

_lifetime.ApplicationStarted.Register(() => OnApplicationStarted(cancellationToken));

foreach (var lifecycleHook in _lifecycleHooks)
await lifecycleHook.OnStartAsync();

_timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, TimeSpan.Zero, TimeSpan.FromHours(1));
_timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, Timeout.InfiniteTimeSpan, TimeSpan.FromHours(1));
}

public async Task StopAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -93,19 +99,22 @@ public async Task RunOnceAsync()

private async Task RunOnceWithErrorHandlingAsync()
{
try
{
try {
await RunOnceAsync();
_timer?.Change(TimeSpan.FromHours(1), TimeSpan.FromHours(1));
}
catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled)
{
} catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled) {
_logger.LogWarning(e, $"Exception occured renewing certificates: '{e.Message}.'");
if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry)
if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry) {
_timer?.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
}
}
}

private void OnApplicationStarted(CancellationToken t) {
_logger.LogInformation("Application started");
_timer?.Change(TimeSpan.Zero, TimeSpan.FromHours(1));
}

public void Dispose()
{
_timer?.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ public static void AddFluffySpoonLetsEncrypt(
services.AddSingleton<ILetsEncryptClientFactory, LetsEncryptClientFactory>();
services.AddSingleton<ICertificateValidator, CertificateValidator>();
services.AddSingleton<ICertificateProvider, CertificateProvider>();

services.AddTransient<ILetsEncryptRenewalService, LetsEncryptRenewalService>();
services.AddTransient<IHostedService, LetsEncryptRenewalService>();
services.AddTransient<IHostedService, LetsEncryptRenewalService>();
}

public static void UseFluffySpoonLetsEncrypt(
Expand Down

0 comments on commit 65eb855

Please sign in to comment.