-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
315 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
LetsEncrypt.Azure.Core.V2/CertificateStores/NullCertificateStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading.Tasks; | ||
using LetsEncrypt.Azure.Core.V2.Models; | ||
|
||
namespace LetsEncrypt.Azure.Core.V2.CertificateStores | ||
{ | ||
internal class NullCertificateStore : ICertificateStore | ||
{ | ||
public Task<CertificateInfo> GetCertificate(string name, string password) | ||
{ | ||
return Task.FromResult<CertificateInfo>(null); | ||
} | ||
|
||
public Task<string> GetSecret(string name) | ||
{ | ||
return Task.FromResult<string>(null); | ||
} | ||
|
||
public Task SaveCertificate(CertificateInfo certificate) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task SaveSecret(string name, string secret) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,62 @@ | ||
using LetsEncrypt.Azure.Core.V2.CertificateStores; | ||
using LetsEncrypt.Azure.Core.V2.DnsProviders; | ||
using LetsEncrypt.Azure.Core.V2.Models; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace LetsEncrypt.Azure.Core.V2 | ||
{ | ||
public static class LetsencryptService | ||
public class LetsencryptService | ||
{ | ||
public static IServiceCollection AddAcmeClient<TDnsProvider>(this IServiceCollection serviceCollection, object dnsProviderConfig, string azureStorageConnectionString = null) where TDnsProvider : class, IDnsProvider | ||
{ | ||
if (serviceCollection == null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceCollection)); | ||
} | ||
private readonly AcmeClient acmeClient; | ||
private readonly ICertificateStore certificateStore; | ||
private readonly AzureWebAppService azureWebAppService; | ||
private readonly ILogger<LetsencryptService> logger; | ||
|
||
if (dnsProviderConfig == null) | ||
{ | ||
throw new ArgumentNullException(nameof(dnsProviderConfig)); | ||
} | ||
if (string.IsNullOrEmpty(azureStorageConnectionString)) | ||
{ | ||
serviceCollection | ||
.AddTransient<IFileSystem, FileSystem>() | ||
.AddTransient<ICertificateStore, FileSystemCertificateStore>(); | ||
} | ||
else | ||
public LetsencryptService(AcmeClient acmeClient, ICertificateStore certificateStore, AzureWebAppService azureWebAppService, ILogger<LetsencryptService> logger = null) | ||
{ | ||
this.acmeClient = acmeClient; | ||
this.certificateStore = certificateStore; | ||
this.azureWebAppService = azureWebAppService; | ||
this.logger = logger ?? NullLogger<LetsencryptService>.Instance; | ||
} | ||
public async Task Run(AcmeDnsRequest acmeDnsRequest, int renewXNumberOfDaysBeforeExpiration) | ||
{ | ||
try | ||
{ | ||
serviceCollection | ||
.AddTransient<IFileSystem, AzureBlobStorage>(s => | ||
CertificateInstallModel model = null; | ||
|
||
var certname = acmeDnsRequest.Host.Substring(2) + "-" + acmeDnsRequest.AcmeEnvironment.Name; | ||
var cert = await certificateStore.GetCertificate(certname, acmeDnsRequest.PFXPassword); | ||
if (cert == null || cert.Certificate.NotAfter < DateTime.UtcNow.AddDays(renewXNumberOfDaysBeforeExpiration)) //Cert doesnt exist or expires in less than renewXNumberOfDaysBeforeExpiration days, lets renew. | ||
{ | ||
logger.LogInformation("Certificate store didn't contain certificate or certificate was expired starting renewing"); | ||
model = await acmeClient.RequestDnsChallengeCertificate(acmeDnsRequest); | ||
model.CertificateInfo.Name = certname; | ||
await certificateStore.SaveCertificate(model.CertificateInfo); | ||
} | ||
else | ||
{ | ||
logger.LogInformation("Certificate expires in more than {renewXNumberOfDaysBeforeExpiration} days, reusing certificate from certificate store", renewXNumberOfDaysBeforeExpiration); | ||
model = new CertificateInstallModel() | ||
{ | ||
return new AzureBlobStorage(azureStorageConnectionString); | ||
}) | ||
.AddTransient<AzureBlobStorage, AzureBlobStorage>(s => | ||
{ | ||
return new AzureBlobStorage(azureStorageConnectionString); | ||
}) | ||
.AddTransient<ICertificateStore, AzureBlobCertificateStore>(); | ||
} | ||
return serviceCollection | ||
.AddTransient<AcmeClient>() | ||
.AddTransient<DnsLookupService>() | ||
.AddSingleton(dnsProviderConfig.GetType(), dnsProviderConfig) | ||
.AddTransient<IDnsProvider, TDnsProvider>(); | ||
} | ||
CertificateInfo = cert, | ||
Host = acmeDnsRequest.Host | ||
}; | ||
} | ||
await azureWebAppService.Install(model); | ||
|
||
public static IServiceCollection AddAzureAppService(this IServiceCollection serviceCollection, params AzureWebAppSettings[] settings) | ||
{ | ||
if (settings == null || settings.Length == 0) | ||
logger.LogInformation("Removing expired certificates"); | ||
var expired = azureWebAppService.RemoveExpired(); | ||
logger.LogInformation("The following certificates was removed {Thumbprints}", string.Join(", ", expired.ToArray())); | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
logger.LogError(e, "Failed"); | ||
throw; | ||
} | ||
|
||
return serviceCollection | ||
.AddSingleton(settings) | ||
.AddTransient<AzureWebAppService>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.