Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThrowOnRenewalFailure field added, skip if renewal fails in foreach - only for webjob #364

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions LetsEncrypt-SiteExtension/Models/AuthenticationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public bool UseIPBasedSSL
get; set;
}

public bool ThrowOnRenewalFailure
{
get; set;
}

[Required]
public string DashboardConnectionString
{
Expand Down
15 changes: 15 additions & 0 deletions LetsEncrypt.SiteExtension.Core/AppSettingsAuthConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class AppSettingsAuthConfig : IAzureWebAppEnvironment, IAcmeConfig
public const string authorizationChallengeBlobStorageAccount = "letsencrypt:AuthorizationChallengeBlobStorageAccount";
public const string authorizationChallengeBlobStorageContainer = "letsencrypt:AuthorizationChallengeBlobStorageContainer";
public const string disableVirtualApplication = "letsencrypt:DisableVirtualApplication";
public const string throwOnRenewalFailure = "letsencrypt:ThrowOnRenewalFailure";
public const string webjobDashboard = "AzureWebJobsDashboard";
public const string webjobStorage = "AzureWebJobsStorage";

Expand Down Expand Up @@ -195,6 +196,20 @@ public bool UseIPBasedSSL
}
}

public bool ThrowOnRenewalFailure
{
get
{
bool b;
if (bool.TryParse(ConfigurationManager.AppSettings[throwOnRenewalFailure], out b))
{
return b;
}

return true;
}
}

public bool DisableWebConfigUpdate
{
get
Expand Down
27 changes: 18 additions & 9 deletions LetsEncrypt.SiteExtension.Core/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class CertificateManager
/// <param name="config"></param>
public CertificateManager(AppSettingsAuthConfig config)
{

this.settings = config;
this.acmeConfig = config;
string storageAccount = AuthorizationChallengeBlobStorageAccount();

if (string.IsNullOrEmpty(storageAccount))
{
this.challengeProvider = new KuduFileSystemAuthorizationChallengeProvider(this.settings, new AuthorizationChallengeProviderConfig()
Expand All @@ -46,7 +46,7 @@ public CertificateManager(AppSettingsAuthConfig config)
UseIPBasedSSL = config.UseIPBasedSSL
});

}
}

public CertificateManager(IAzureWebAppEnvironment settings, IAcmeConfig acmeConfig, ICertificateService certificateService, IAuthorizationChallengeProvider challengeProvider)
{
Expand Down Expand Up @@ -117,7 +117,10 @@ public async Task<CertificateInstallModel> AddCertificate()
return null;
}

public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInstallCertificate = false, int renewXNumberOfDaysBeforeExpiration = 0)
public async Task<List<CertificateInstallModel>> RenewCertificate(
bool skipInstallCertificate = false,
int renewXNumberOfDaysBeforeExpiration = 0,
bool throwOnRenewalFailure = true)
{
Trace.TraceInformation("Checking certificate");
var ss = SettingsStore.Instance.Load();
Expand All @@ -131,8 +134,6 @@ public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInsta
var response = await retryPolicy.ExecuteAsync(async () =>
{
return await httpClient.GetAsync($"/subscriptions/{settings.SubscriptionId}/providers/Microsoft.Web/certificates?api-version=2016-03-01");


});
response.EnsureSuccessStatusCode();
body = await response.Content.ReadAsStringAsync();
Expand All @@ -157,7 +158,6 @@ public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInsta
}
var target = new AcmeConfig()
{

RegistrationEmail = this.acmeConfig.RegistrationEmail ?? ss.FirstOrDefault(s => s.Name == "email").Value,
Host = sslStates.First().Name,
BaseUri = this.acmeConfig.BaseUri,
Expand All @@ -169,7 +169,16 @@ public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInsta
};
if (!skipInstallCertificate)
{
res.Add(await RequestAndInstallInternalAsync(target));
try
{
res.Add(await RequestAndInstallInternalAsync(target));
}
catch (Exception e) when (throwOnRenewalFailure)
{
Console.WriteLine($"Error during Request or install certificate {e.ToString()}");
Trace.TraceError($"Error during Request or install certificate {e.ToString()}");
throw;
}
}
}
return res;
Expand All @@ -178,7 +187,7 @@ public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInsta

internal static IEnumerable<Certificate> ExtractCertificates(string body)
{

var json = JToken.Parse(body);
var certs = Enumerable.Empty<Certificate>();
// Handle issue #269
Expand All @@ -196,7 +205,7 @@ internal static IEnumerable<Certificate> ExtractCertificates(string body)

internal CertificateInstallModel RequestAndInstallInternal(IAcmeConfig config)
{
return RequestAndInstallInternalAsync(config).GetAwaiter().GetResult();
return RequestAndInstallInternalAsync(config).GetAwaiter().GetResult();
}

internal async Task<CertificateInstallModel> RequestInternalAsync(IAcmeConfig config)
Expand Down
11 changes: 9 additions & 2 deletions LetsEncrypt.SiteExtension.Core/IAzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public interface IAzureEnvironment
}

public interface IAzureWebAppEnvironment : IAzureEnvironment
{

{
string WebAppName { get; }

string ResourceGroupName { get; }

string ServicePlanResourceGroupName { get; }
Expand All @@ -31,6 +31,8 @@ public interface IAzureWebAppEnvironment : IAzureEnvironment

string AzureWebSitesDefaultDomainName { get; }

bool ThrowOnRenewalFailure { get; }

string WebRootPath { get; }

bool RunFromPackage { get; }
Expand Down Expand Up @@ -212,5 +214,10 @@ public bool RunFromPackage
{
get;set;
}

public bool ThrowOnRenewalFailure
{
get; set;
} = true;
}
}
7 changes: 5 additions & 2 deletions LetsEncrypt.SiteExtension.WebJob/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public static async Task RenewCertificate([TimerTrigger(typeof(MyDailySchedule),
{
Console.WriteLine("Renew certificate");
var config = new AppSettingsAuthConfig();
var count = (await new CertificateManager(new AppSettingsAuthConfig()).RenewCertificate(renewXNumberOfDaysBeforeExpiration: config.RenewXNumberOfDaysBeforeExpiration)).Count();
Console.WriteLine($"Completed renewal of '{count}' certificates");
var certManager = new CertificateManager(new AppSettingsAuthConfig());

var renewedCerts = await certManager.RenewCertificate(renewXNumberOfDaysBeforeExpiration: config.RenewXNumberOfDaysBeforeExpiration, throwOnRenewalFailure: config.ThrowOnRenewalFailure);

Console.WriteLine($"Completed renewal of '{renewedCerts.Count()}' certificates");
}

public static async Task Cleanup([TimerTrigger(typeof(MyDailySchedule), RunOnStartup = true)] TimerInfo timerInfo)
Expand Down