Skip to content

Commit

Permalink
Improvement dns zones dropdown list (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Aug 16, 2024
1 parent 0215b57 commit d34b9e6
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion KeyVault.Acmebot/Functions/GetDnsZones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public GetDnsZones(IHttpContextAccessor httpContextAccessor)
}

[FunctionName($"{nameof(GetDnsZones)}_{nameof(Orchestrator)}")]
public Task<IReadOnlyList<DnsZoneItem>> Orchestrator([OrchestrationTrigger] IDurableOrchestrationContext context)
public Task<IReadOnlyList<DnsZoneGroup>> Orchestrator([OrchestrationTrigger] IDurableOrchestrationContext context)
{
var activity = context.CreateActivityProxy<ISharedActivity>();

Expand Down
2 changes: 1 addition & 1 deletion KeyVault.Acmebot/Functions/ISharedActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface ISharedActivity

Task<IReadOnlyList<CertificateItem>> GetAllCertificates(object input = null);

Task<IReadOnlyList<DnsZoneItem>> GetAllDnsZones(object input = null);
Task<IReadOnlyList<DnsZoneGroup>> GetAllDnsZones(object input = null);

Task<CertificatePolicyItem> GetCertificatePolicy(string certificateName);

Expand Down
16 changes: 10 additions & 6 deletions KeyVault.Acmebot/Functions/SharedActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,21 @@ public async Task<IReadOnlyList<CertificateItem>> GetAllCertificates([ActivityTr
}

[FunctionName(nameof(GetAllDnsZones))]
public async Task<IReadOnlyList<DnsZoneItem>> GetAllDnsZones([ActivityTrigger] object input = null)
public async Task<IReadOnlyList<DnsZoneGroup>> GetAllDnsZones([ActivityTrigger] object input = null)
{
try
{
var zones = await _dnsProviders.ListAllZonesAsync();

return zones.OrderBy(x => x.DnsProvider.Name).Select(x => x.ToDnsZoneItem()).ToArray();
return zones.Select(x => new DnsZoneGroup
{
DnsProviderName = x.Item1,
DnsZones = x.Item2?.Select(xs => xs.ToDnsZoneItem()).OrderBy(xs => xs.Name).ToArray()
}).ToArray();
}
catch
{
return Array.Empty<DnsZoneItem>();
return Array.Empty<DnsZoneGroup>();
}
}

Expand Down Expand Up @@ -142,7 +146,7 @@ public async Task<string> Dns01Precondition([ActivityTrigger] (string, IReadOnly
var (dnsProviderName, dnsNames) = input;

// DNS zone の一覧を各 Provider から取得
var zones = await _dnsProviders.ListAllZonesAsync();
var zones = await _dnsProviders.FlattenAllZonesAsync();

// DNS zone が存在するか確認
var foundZones = new HashSet<DnsZone>();
Expand Down Expand Up @@ -253,7 +257,7 @@ public async Task<string> Dns01Precondition([ActivityTrigger] (string, IReadOnly
}

// DNS zone の一覧を各 Provider から取得
var zones = await (string.IsNullOrEmpty(dnsProviderName) ? _dnsProviders.ListAllZonesAsync() : _dnsProviders.ListZonesAsync(dnsProviderName));
var zones = (await (string.IsNullOrEmpty(dnsProviderName) ? _dnsProviders.FlattenAllZonesAsync() : _dnsProviders.ListZonesAsync(dnsProviderName)));

var propagationSeconds = 0;

Expand Down Expand Up @@ -455,7 +459,7 @@ public async Task CleanupDnsChallenge([ActivityTrigger] (string, IReadOnlyList<A
var (dnsProviderName, challengeResults) = input;

// DNS zone の一覧を各 Provider から取得
var zones = await (string.IsNullOrEmpty(dnsProviderName) ? _dnsProviders.ListAllZonesAsync() : _dnsProviders.ListZonesAsync(dnsProviderName));
var zones = (await (string.IsNullOrEmpty(dnsProviderName) ? _dnsProviders.FlattenAllZonesAsync() : _dnsProviders.ListZonesAsync(dnsProviderName)));

// DNS-01 の検証レコード名毎に DNS から TXT レコードを削除
foreach (var lookup in challengeResults.ToLookup(x => x.DnsRecordName))
Expand Down
31 changes: 27 additions & 4 deletions KeyVault.Acmebot/Internal/DnsProvidersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ namespace KeyVault.Acmebot.Internal;

internal static class DnsProvidersExtensions
{
public static async Task<IReadOnlyList<DnsZone>> ListAllZonesAsync(this IEnumerable<IDnsProvider> dnsProviders)
public static async Task<IReadOnlyList<(string, IReadOnlyList<DnsZone>)>> ListAllZonesAsync(this IEnumerable<IDnsProvider> dnsProviders)
{
var zones = await Task.WhenAll(dnsProviders.Select(x => x.ListZonesAsync()));
async Task<(string, IReadOnlyList<DnsZone>)> ListDnsZones(IDnsProvider dnsProvider)
{
try
{
var dnsZones = await dnsProvider.ListZonesAsync();

return (dnsProvider.Name, dnsZones);
}
catch
{
return (dnsProvider.Name, null);
}
}

return zones.SelectMany(x => x).ToArray();
var zones = await Task.WhenAll(dnsProviders.Select(ListDnsZones));

return zones;
}

public static async Task<IReadOnlyList<DnsZone>> ListZonesAsync(this IEnumerable<IDnsProvider> dnsProviders, string dnsProviderName)
Expand All @@ -25,7 +39,16 @@ public static async Task<IReadOnlyList<DnsZone>> ListZonesAsync(this IEnumerable
return Array.Empty<DnsZone>();
}

return await dnsProvider.ListZonesAsync();
var dnsZones = await dnsProvider.ListZonesAsync();

return dnsZones;
}

public static async Task<IReadOnlyList<DnsZone>> FlattenAllZonesAsync(this IEnumerable<IDnsProvider> dnsProviders)
{
var zones = await dnsProviders.ListAllZonesAsync();

return zones.Where(x => x.Item2 is not null).SelectMany(x => x.Item2).ToArray();
}

public static void TryAdd<TOption>(this IList<IDnsProvider> dnsProviders, TOption options, Func<TOption, IDnsProvider> factory)
Expand Down
2 changes: 1 addition & 1 deletion KeyVault.Acmebot/Internal/DnsZoneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class DnsZoneExtensions
{
public static DnsZoneItem ToDnsZoneItem(this DnsZone dnsZone)
{
return new DnsZoneItem { Name = dnsZone.Name, DnsProviderName = dnsZone.DnsProvider.Name };
return new DnsZoneItem { Name = dnsZone.Name };
}

public static DnsZone FindDnsZone(this IEnumerable<DnsZone> dnsZones, string dnsName)
Expand Down
4 changes: 2 additions & 2 deletions KeyVault.Acmebot/KeyVault.Acmebot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Route53" Version="3.7.401.2" />
<PackageReference Include="AWSSDK.Route53" Version="3.7.401.4" />
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.ResourceManager.Dns" Version="1.1.1" />
<PackageReference Include="Azure.ResourceManager.PrivateDns" Version="1.1.1" />
Expand All @@ -14,7 +14,7 @@
<PackageReference Include="DurableTask.TypedProxy" Version="2.2.2" />
<PackageReference Include="Google.Apis.Dns.v1" Version="1.68.0.3487" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.13.4" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.13.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="[6.0.*,7.0.0)" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" />
<PackageReference Include="WebJobs.Extensions.HttpApi" Version="2.1.0" />
Expand Down
10 changes: 9 additions & 1 deletion KeyVault.Acmebot/Models/DnsZoneItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using Newtonsoft.Json;
using System.Collections.Generic;

using Newtonsoft.Json;

namespace KeyVault.Acmebot.Models;

public class DnsZoneItem
{
[JsonProperty("name")]
public string Name { get; set; }
}

public class DnsZoneGroup
{
[JsonProperty("dnsProviderName")]
public string DnsProviderName { get; set; }

[JsonProperty("dnsZones")]
public IReadOnlyList<DnsZoneItem> DnsZones { get; set; }
}
22 changes: 9 additions & 13 deletions KeyVault.Acmebot/wwwroot/dashboard/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ <h2 class="title is-4">Unmanaged certificates</h2>
<div class="select" :class="{ 'is-loading': add.loading }">
<select v-model="add.zone">
<option disabled :value="null">Please select one</option>
<optgroup v-for="(zones, dnsProviderName) in add.zones" :label="dnsProviderName">
<option v-for="zone in zones" :value="zone">{{ toUnicode(zone.name) }}</option>
<optgroup v-for="zones in add.zones" :label="zones.dnsProviderName">
<option v-if="zones.dnsZones === null" disabled :value="null">fetch error</option>
<option v-else-if="!zones.dnsZones.length" disabled :value="null">not found</option>
<option v-else v-for="zone in zones.dnsZones" :value="zone">{{ toUnicode(zone.name) }}</option>
</optgroup>
</select>
</div>
Expand Down Expand Up @@ -546,17 +548,7 @@ <h2 class="title is-4">Unmanaged certificates</h2>
const response = await axios.get("/api/dns-zones");

if (response.status === 200) {
this.add.zones = {};

for (var i = 0; i < response.data.length; i++) {
const item = response.data[i];

if (!this.add.zones[item.dnsProviderName]) {
this.add.zones[item.dnsProviderName] = [];
}

this.add.zones[item.dnsProviderName].push(item);
}
this.add.zones = response.data;
}
} catch (error) {
this.handleHttpError(error);
Expand Down Expand Up @@ -631,6 +623,10 @@ <h2 class="title is-4">Unmanaged certificates</h2>
await this.refresh();
},
async openAdd() {
this.add.recordName = "";
this.add.dnsNames = [];
this.add.dnsProviderName = "";
this.add.certificateName = "";
this.add.modalActive = true;

await this.loadDnsZones();
Expand Down

0 comments on commit d34b9e6

Please sign in to comment.