Skip to content

Commit

Permalink
#56
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkp committed Jul 18, 2016
1 parent 9791192 commit 2584275
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 19 deletions.
14 changes: 14 additions & 0 deletions LetsEncrypt.SiteExtension.Core/AppSettingsAuthConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public string Tenant
}
}

public int RenewXNumberOfDaysBeforeExpiration
{
get
{
var s = ConfigurationManager.AppSettings["RenewXNumberOfDaysBeforeExpiration"];
int days = 14;
if (string.IsNullOrEmpty(s) || !int.TryParse(s, out days))
{
return 14;
}
return days;
}
}

public Guid SubscriptionId
{
get
Expand Down
22 changes: 13 additions & 9 deletions LetsEncrypt.SiteExtension.Core/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ public void SetupHostnameAndCertificate()
}
}

public void RenewCertificate()
public IEnumerable<Target> RenewCertificate(bool debug = false)
{
Trace.TraceInformation("Checking certificate");
var settings = new AppSettingsAuthConfig();
var ss = SettingsStore.Instance.Load();
using (var client = ArmHelper.GetWebSiteManagementClient(settings))
{
var certs = client.Certificates.GetCertificates(settings.ResourceGroupName).Value;
var expireringIn14Days = certs.Where(s => s.ExpirationDate < DateTime.UtcNow.AddDays(14) && s.Issuer.Contains("Let's Encrypt"));
var certs = client.Certificates.GetCertificates(settings.ServicePlanResourceGroupName).Value;
var expiringCerts = certs.Where(s => s.ExpirationDate < DateTime.UtcNow.AddDays(settings.RenewXNumberOfDaysBeforeExpiration) && (s.Issuer.Contains("Let's Encrypt") || s.Issuer.Contains("Fake LE")));

if (expireringIn14Days.Count() == 0)
if (expiringCerts.Count() == 0)
{
Trace.TraceInformation("No certificates installed issued by Let's Encrypt that are about to expire within the next 14 days. Skipping.");
Trace.TraceInformation(string.Format("No certificates installed issued by Let's Encrypt that are about to expire within the next {0} days. Skipping.", settings.RenewXNumberOfDaysBeforeExpiration));
}

foreach (var toExpireCert in expireringIn14Days)
foreach (var toExpireCert in expiringCerts)
{
Trace.TraceInformation("Starting renew of certificate " + toExpireCert.Name + " expiration date " + toExpireCert.ExpirationDate);
var site = client.Sites.GetSite(settings.ResourceGroupName, settings.WebAppName);
Expand All @@ -111,8 +111,7 @@ public void RenewCertificate()
Trace.TraceInformation(String.Format("Certificate {0} was not assigned any hostname, skipping update", toExpireCert.Thumbprint));
continue;
}

RequestAndInstallInternal(new Target()
var target = new Target()
{
WebAppName = settings.WebAppName,
Tenant = settings.Tenant,
Expand All @@ -125,7 +124,12 @@ public void RenewCertificate()
BaseUri = settings.BaseUri ?? ss.FirstOrDefault(s => s.Name == "baseUri").Value,
ServicePlanResourceGroupName = settings.ServicePlanResourceGroupName,
AlternativeNames = sslStates.Skip(1).Select(s => s.Name).ToList()
});
};
if (!debug)
{
RequestAndInstallInternal(target);
}
yield return target;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion LetsEncrypt.SiteExtension.Core/SettingsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SettingsStore()
else
{
// Use regular App_Data outside of Azure
folder = HostingEnvironment.MapPath("~/App_Data");
folder = HostingEnvironment.MapPath("~/App_Data") ?? Path.Combine(Directory.GetCurrentDirectory(), "App_Data");
}

if (!Directory.Exists(folder))
Expand Down
3 changes: 1 addition & 2 deletions LetsEncrypt.SiteExtension.Test/App.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="WEBSITE_OWNER_NAME" value="688bf064-900b-4e8f-9598-2d9be0718133+Tiimo+.Web-.Dev1-WestEuropewebspace" />
<appSettings configSource="appsettings.dev.config">
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down
22 changes: 22 additions & 0 deletions LetsEncrypt.SiteExtension.Test/App_Data/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"Name": "email",
"Value": "[email protected]",
"Count": 0
},
{
"Name": "baseUri",
"Value": "https://acme-staging.api.letsencrypt.org/",
"Count": 0
},
{
"Name": "email",
"Value": "[email protected]",
"Count": 0
},
{
"Name": "baseUri",
"Value": "https://acme-v01.api.letsencrypt.org/",
"Count": 0
}
]
18 changes: 18 additions & 0 deletions LetsEncrypt.SiteExtension.Test/CertificateManagerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LetsEncrypt.SiteExtension.Test
{
[TestClass]
public class CertificateManagerTest
{
[TestMethod]
public void RenewCertificateTest()
{
var result = new Core.CertificateManager().RenewCertificate(true);

Assert.AreNotEqual(0, result.Count());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,26 @@
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework">
<Private>False</Private>
</Reference>
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CertificateManagerTest.cs" />
<Compile Include="WebAppEnviromentVariablesTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="appsettings.dev.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="App_Data\settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions LetsEncrypt.SiteExtension.Test/appsettings.dev.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="WEBSITE_OWNER_NAME" value="688bf064-900b-4e8f-9598-2d9be0718133+Tiimo+.Web-.Dev1-WestEuropewebspace" />
<add key="RenewXNumberOfDaysBeforeExpiration" value="110"/>
<add key="letsencrypt:ServicePlanResourceGroupName" value="Default-Web-WestEurope" />
<add key="letsencrypt:ResourceGroupName" value="sjkp.testplan"/>
<add key="letsencrypt:Tenant" value=""/>
<add key="letsencrypt:SubscriptionId" value=""/>
<add key="letsencrypt:ClientSecret" value=""/>
<add key="letsencrypt:ClientId" value=""/>
<add key="WEBSITE_SITE_NAME" value="" />
</appSettings>
4 changes: 2 additions & 2 deletions LetsEncrypt.SiteExtension.WebJob/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static void SetupHostNameAndCertificate([TimerTrigger(typeof(MonthlySched
public static void RenewCertificate([TimerTrigger(typeof(MyDailySchedule), RunOnStartup = true)] TimerInfo timerInfo)
{
Console.WriteLine("Renew certificate");
new CertificateManager().RenewCertificate();
Console.WriteLine("Completed renew certificate");
var count = new CertificateManager().RenewCertificate().Count();
Console.WriteLine($"Completed renewal of '{count}' certificates");
}

}
Expand Down
2 changes: 1 addition & 1 deletion LetsEncrypt.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>letsencrypt</id>
<title>Azure Let's Encrypt (x86)</title>
<version>0.4.15</version>
<version>0.4.19</version>
<authors>SJKP</authors>
<licenseUrl>http://opensource.org/licenses/Apache-2.0</licenseUrl>
<projectUrl>https://github.com/sjkp/letsencrypt-siteextension</projectUrl>
Expand Down
2 changes: 1 addition & 1 deletion LetsEncrypt64.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>letsencrypt64</id>
<title>Azure Let's Encrypt (x64)</title>
<version>0.4.15</version>
<version>0.4.19</version>
<authors>SJKP</authors>
<licenseUrl>http://opensource.org/licenses/Apache-2.0</licenseUrl>
<projectUrl>https://github.com/sjkp/letsencrypt-siteextension</projectUrl>
Expand Down
4 changes: 2 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
nuget restore
md artifacts\bin
md artifacts
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt-SiteExtension\LetsEncrypt.SiteExtension.csproj /t:pipelinePreDeployCopyAllFilesToOneFolder /p:Platform=x86 /p:_PackageTempDir="..\artifacts";AutoParameterizationWebConfigConnectionStrings=false;Configuration=Release;SolutionDir="."
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt.SiteExtension.Core\LetsEncrypt.SiteExtension.Core.csproj /p:Platform=x86;Configuration=Release;SolutionDir="."
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt.SiteExtension.WebJob\LetsEncrypt.SiteExtension.WebJob.csproj /p:Platform=x86;Configuration=Release;SolutionDir="."
Expand All @@ -11,7 +11,7 @@ xcopy LetsEncrypt.SiteExtension.WebJob\bin\x64\Release\*.* artifacts\app_data\jo

nuget pack letsencrypt.nuspec

md artifacts64\bin
md artifacts64
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt-SiteExtension\LetsEncrypt.SiteExtension.csproj /t:pipelinePreDeployCopyAllFilesToOneFolder /p:Platform=x64 /p:_PackageTempDir="..\artifacts64";AutoParameterizationWebConfigConnectionStrings=false;Configuration=Release;SolutionDir="."
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt.SiteExtension.Core\LetsEncrypt.SiteExtension.Core.csproj /p:Platform=x64;Configuration=Release;SolutionDir="."
"C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe" LetsEncrypt.SiteExtension.WebJob\LetsEncrypt.SiteExtension.WebJob.csproj /p:Platform=x64;Configuration=Release;SolutionDir="."
Expand Down

0 comments on commit 2584275

Please sign in to comment.