Skip to content

Commit

Permalink
Support using subdomains with cloudflare integration (#26)
Browse files Browse the repository at this point in the history
* Support using subdomains with cloudflare integration
* Convert library to netstandard 2.1 and only run tests in .netcore 3.0
  • Loading branch information
Kencdk authored Sep 5, 2020
1 parent c04d78e commit 91580e6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/Examples/CloudflareIntegration/OrderDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public OrderDomains(Options options)

public async Task ValidateCloudflareConnection()
{
var cleanedupDomains = options.Domains.Select(x => (x.StartsWith('*') ? x.Substring(2) : x).ToLowerInvariant())
var cleanedupDomains = options.Domains.Select(GetRootDomain)
.Distinct()
.ToList();

Expand Down Expand Up @@ -262,6 +262,17 @@ public async Task RetrieveCertificates(Order order)
Program.LogLine($"Private certificate written to file {privateKeyFilename}");
}

/// <summary>
/// Get the root (domain.tld) from a given domain (eg: subdomain.domain.tld)
/// </summary>
/// <param name="domain">Target domain</param>
/// <returns>A string with the root domain incl the top-level-domain</returns>
private static string GetRootDomain(string domain)
{
var parts = domain.ToLowerInvariant().Split('.');
return string.Join('.', parts.TakeLast(2));
}

private static string FixFilename(string filename)
{
return filename.Replace("*", "");
Expand Down Expand Up @@ -301,7 +312,7 @@ private async Task<string> AddDNSEntry(string domain, string value)
var dnsRecordName = $"_acme-challenge.{domain}";

Program.LogLine($"Adding TXT entry {dnsRecordName} with value {value}", true);
Zone cloudflareZone = CloudflareZones.GetValueOrDefault(domain);
Zone cloudflareZone = CloudflareZones.GetValueOrDefault(GetRootDomain(domain));

DNSRecord dnsRecord;
try
Expand Down
5 changes: 3 additions & 2 deletions src/Examples/CloudflareIntegration/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Threading.Tasks;
using CommandLine;
using Kenc.ACMELib.ACMEEntities;

class Program
{
Expand Down Expand Up @@ -35,10 +36,10 @@ static async Task RunOptions(Options options)
var orderDomains = new OrderDomains(options);
await orderDomains.ValidateCloudflareConnection();
await orderDomains.ValidateACMEConnection();
var order = await orderDomains.ValidateDomains();

Order order = await orderDomains.ValidateDomains();
order = await orderDomains.ValidateOrder(order);
await orderDomains.RetrieveCertificates(order);

}

static void HandleParseError(IEnumerable<Error> errs)
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/ACMELib.Tests/Kenc.ACMELibCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;netcoreapp3.0</TargetFrameworks>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
<LangVersion>8.0</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/ACMELib/Kenc.ACMELib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<TargetFrameworks>netcoreapp2.2;netcoreapp3.0</TargetFrameworks>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<Description>.net core library for communicating with Let’s Encrypt ACME servers for certificate management.</Description>
Expand Down

0 comments on commit 91580e6

Please sign in to comment.