Skip to content

Commit

Permalink
#269, generate documentation in release.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkp committed Nov 13, 2018
1 parent 8969e45 commit d7e419a
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 11 deletions.
4 changes: 2 additions & 2 deletions LetsEncrypt-SiteExtension/LetsEncrypt.SiteExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
<Use64BitIISExpress />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -42,6 +43,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\LetsEncrypt.SiteExtension.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.Graph.RBAC, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down Expand Up @@ -219,8 +221,6 @@
<Compile Include="Models\ARMListEntry.cs" />
<Compile Include="Models\AuthenticationModel.cs" />
<Compile Include="Models\RequestAndInstallModel.cs" />
<Compile Include="Modules\AADOAuth2AccessToken.cs" />
<Compile Include="Modules\ARMOAuthModule.cs" />
<Compile Include="Modules\Extensions.cs" />
<Compile Include="Modules\OpenIdConfiguration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
30 changes: 24 additions & 6 deletions LetsEncrypt.SiteExtension.Core/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Azure.Management.WebSites;
using Microsoft.Azure.Management.WebSites.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -122,7 +123,7 @@ public async Task<List<CertificateInstallModel>> RenewCertificate(bool skipInsta
var response = await httpClient.GetAsync($"/subscriptions/{settings.SubscriptionId}/providers/Microsoft.Web/certificates?api-version=2016-03-01");
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
IEnumerable<Certificate> certs = JsonConvert.DeserializeObject<Certificate[]>(body, JsonHelper.DefaultSerializationSettings);
IEnumerable<Certificate> certs = ExtractCertificates(body);

var expiringCerts = certs.Where(s => s.ExpirationDate < DateTime.UtcNow.AddDays(renewXNumberOfDaysBeforeExpiration) && (s.Issuer.Contains("Let's Encrypt") || s.Issuer.Contains("Fake LE")));

Expand All @@ -143,24 +144,41 @@ 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 ?? ss.FirstOrDefault(s => s.Name == "baseUri").Value,
BaseUri = this.acmeConfig.BaseUri ?? ss.FirstOrDefault(s => s.Name == "baseUri").Value,
AlternateNames = sslStates.Skip(1).Select(s => s.Name).ToList(),
PFXPassword = this.acmeConfig.PFXPassword,
RSAKeyLength = this.acmeConfig.RSAKeyLength

};
if (!skipInstallCertificate)
{
res.Add(await RequestAndInstallInternalAsync(target));
}
}
}
return res;
}
}
}

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

var json = JToken.Parse(body);
var certs = Enumerable.Empty<Certificate>();
// Handle issue #269
if (json.Type == JTokenType.Object && json["value"] != null)
{
certs = JsonConvert.DeserializeObject<Certificate[]>(json["value"].ToString(), JsonHelper.DefaultSerializationSettings);
}
else
{
certs = JsonConvert.DeserializeObject<Certificate[]>(body, JsonHelper.DefaultSerializationSettings);
}

return certs;
}

internal CertificateInstallModel RequestAndInstallInternal(IAcmeConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\LetsEncrypt.Azure.Core.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down
47 changes: 46 additions & 1 deletion LetsEncrypt.SiteExtension.Test/CertificateManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
using LetsEncrypt.Azure.Core.Models;
using LetsEncrypt.Azure.Core;
using LetsEncrypt.Azure.Core.Services;
using System.Collections.Generic;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.IO;

namespace LetsEncrypt.SiteExtension.Test
{
Expand All @@ -18,7 +24,7 @@ public async Task RenewCertificateTest()
{
var result = await new CertificateManager(new AppSettingsAuthConfig()).RenewCertificate();

Assert.AreNotEqual(0, result.Count());
Assert.AreEqual(0, result.Count());
}

[TestCategory("Integration")]
Expand All @@ -33,6 +39,7 @@ public async Task RenewCertificateConstructorTest()
var result = await mgr.RenewCertificate(renewXNumberOfDaysBeforeExpiration: 200);

Assert.AreNotEqual(0, result.Count());
ValidateCertificate(result, "https://letsencrypt.sjkp.dk");
}

[TestCategory("Integration")]
Expand Down Expand Up @@ -70,6 +77,7 @@ public async Task AddCertificateDnsChallengeTest()
var result = await mgr.AddCertificate();

Assert.IsNotNull(result);
ValidateCertificate(new[] { result }, "https://letsencrypt.ai4bots.com");
}

[TestCategory("Integration")]
Expand All @@ -89,5 +97,42 @@ public async Task RequestCertificateDnsChallengeTest()

Assert.IsTrue(res.CertificateInfo.Certificate.Subject.Contains("ai4bots.com"));
}

[DeploymentItem("certArray.json")]
[DeploymentItem("certArrayWithValue.json")]
[TestMethod]
public void ExtractCertificates()
{
var t1 = File.ReadAllText("certArray.json");
var t2 = File.ReadAllText("certArrayWithValue.json");
var res1 = CertificateManager.ExtractCertificates(t1);
var res2 = CertificateManager.ExtractCertificates(t2);

Assert.AreEqual("A19D760D4D50552DA48B1D493738BD754E5EA8DA", res1.FirstOrDefault().Thumbprint);
Assert.AreEqual("A19D760D4D50552DA48B1D493738BD754E5EA8DA", res2.FirstOrDefault().Thumbprint);
}


private void ValidateCertificate(IEnumerable<CertificateInstallModel> certs, string uri)
{
//Do webrequest to get info on secure site
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();

//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;

//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2 cert2 = new X509Certificate2(cert);

string cn = cert2.Issuer;
Assert.AreEqual("CN=Fake LE Intermediate X1", cn);
string tb = cert2.Thumbprint;
Assert.AreEqual(certs.FirstOrDefault().CertificateInfo.Certificate.Thumbprint, tb);
string cedate = cert2.GetExpirationDateString();
string cpub = cert2.GetPublicKeyString();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
<None Include="App_Data\settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="certArray.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="certArrayWithValue.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="letsencrypt.sjkp.dk-all.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
33 changes: 33 additions & 0 deletions LetsEncrypt.SiteExtension.Test/certArray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"id": "/subscriptions/3f09c367-93e0-4b61-bbe5-dcb5c686bf8a/resourceGroups/LetsEncrypt-SiteExtension/providers/Microsoft.Web/certificates/letsencrypt.sjkp.dk-A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"name": "letsencrypt.sjkp.dk-A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"type": "Microsoft.Web/certificates",
"location": "West US",
"properties": {
"friendlyName": "CN=letsencrypt.sjkp.dk",
"subjectName": "letsencrypt.sjkp.dk",
"hostNames": [
"letsencrypt.sjkp.dk"
],
"pfxBlob": null,
"siteName": null,
"selfLink": null,
"issuer": "Fake LE Intermediate X1",
"issueDate": "2018-11-13T20:19:24+01:00",
"expirationDate": "2019-02-11T20:19:24+01:00",
"password": null,
"thumbprint": "A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"valid": null,
"toDelete": null,
"cerBlob": null,
"publicKeyHash": null,
"hostingEnvironment": null,
"hostingEnvironmentProfile": null,
"keyVaultSecretStatus": "Initialized",
"webSpace": "LetsEncrypt-SiteExtension-WestUSwebspace",
"serverFarmId": null,
"tags": null
}
}
]
35 changes: 35 additions & 0 deletions LetsEncrypt.SiteExtension.Test/certArrayWithValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"value": [
{
"id": "/subscriptions/3f09c367-93e0-4b61-bbe5-dcb5c686bf8a/resourceGroups/LetsEncrypt-SiteExtension/providers/Microsoft.Web/certificates/letsencrypt.sjkp.dk-A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"name": "letsencrypt.sjkp.dk-A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"type": "Microsoft.Web/certificates",
"location": "West US",
"properties": {
"friendlyName": "CN=letsencrypt.sjkp.dk",
"subjectName": "letsencrypt.sjkp.dk",
"hostNames": [
"letsencrypt.sjkp.dk"
],
"pfxBlob": null,
"siteName": null,
"selfLink": null,
"issuer": "Fake LE Intermediate X1",
"issueDate": "2018-11-13T20:19:24+01:00",
"expirationDate": "2019-02-11T20:19:24+01:00",
"password": null,
"thumbprint": "A19D760D4D50552DA48B1D493738BD754E5EA8DA",
"valid": null,
"toDelete": null,
"cerBlob": null,
"publicKeyHash": null,
"hostingEnvironment": null,
"hostingEnvironmentProfile": null,
"keyVaultSecretStatus": "Initialized",
"webSpace": "LetsEncrypt-SiteExtension-WestUSwebspace",
"serverFarmId": null,
"tags": null
}
}
]
}
2 changes: 1 addition & 1 deletion LetsEncrypt.WebAppOnly.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>letsencrypt.webapponly</id>
<title>Azure Let's Encrypt (No Web Jobs)</title>
<version>0.8.7</version>
<version>0.8.8</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 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</title>
<version>0.8.7</version>
<version>0.8.8</version>
<authors>SJKP</authors>
<licenseUrl>http://opensource.org/licenses/Apache-2.0</licenseUrl>
<projectUrl>https://github.com/sjkp/letsencrypt-siteextension</projectUrl>
Expand Down

0 comments on commit d7e419a

Please sign in to comment.