diff --git a/KeyVault.Acmebot/Internal/AzureSdkExtensions.cs b/KeyVault.Acmebot/Internal/AzureSdkExtensions.cs new file mode 100644 index 00000000..ac31832d --- /dev/null +++ b/KeyVault.Acmebot/Internal/AzureSdkExtensions.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +using Microsoft.Azure.KeyVault; +using Microsoft.Azure.KeyVault.Models; +using Microsoft.Azure.Management.Dns; +using Microsoft.Azure.Management.Dns.Models; + +namespace KeyVault.Acmebot.Internal +{ + internal static class AzureSdkExtensions + { + public static async Task> ListAllAsync(this IZonesOperations operations) + { + var zones = new List(); + + var list = await operations.ListAsync(); + + zones.AddRange(list); + + while (list.NextPageLink != null) + { + list = await operations.ListNextAsync(list.NextPageLink); + + zones.AddRange(list); + } + + return zones; + } + + public static async Task> GetAllCertificatesAsync(this IKeyVaultClient keyVaultClient, string vaultBaseUrl) + { + var certificates = new List(); + + var list = await keyVaultClient.GetCertificatesAsync(vaultBaseUrl); + + certificates.AddRange(list); + + while (list.NextPageLink != null) + { + list = await keyVaultClient.GetCertificatesNextAsync(list.NextPageLink); + + certificates.AddRange(list); + } + + return certificates; + } + } +} diff --git a/KeyVault.Acmebot/SharedFunctions.cs b/KeyVault.Acmebot/SharedFunctions.cs index 5fd739ed..b9f2d056 100644 --- a/KeyVault.Acmebot/SharedFunctions.cs +++ b/KeyVault.Acmebot/SharedFunctions.cs @@ -80,7 +80,7 @@ public async Task IssueCertificate([OrchestrationTrigger] DurableOrchestrationCo [FunctionName(nameof(GetCertificates))] public async Task> GetCertificates([ActivityTrigger] DateTime currentDateTime) { - var certificates = await _keyVaultClient.GetCertificatesAsync(Settings.Default.VaultBaseUrl); + var certificates = await _keyVaultClient.GetAllCertificatesAsync(Settings.Default.VaultBaseUrl); var list = certificates.Where(x => x.Tags != null && x.Tags.TryGetValue("Issuer", out var issuer) && issuer == "letsencrypt.org") .Where(x => (x.Attributes.Expires.Value - currentDateTime).TotalDays < 30) @@ -97,22 +97,9 @@ public async Task> GetCertificates([ActivityTrigger] Da } [FunctionName(nameof(GetZones))] - public async Task> GetZones([ActivityTrigger] object input = null) + public Task> GetZones([ActivityTrigger] object input = null) { - var list = await _dnsManagementClient.Zones.ListAsync(); - - var zones = new List(); - - zones.AddRange(list); - - while (list.NextPageLink != null) - { - list = await _dnsManagementClient.Zones.ListNextAsync(list.NextPageLink); - - zones.AddRange(list); - } - - return zones; + return _dnsManagementClient.Zones.ListAllAsync(); } [FunctionName(nameof(Order))] @@ -127,7 +114,7 @@ public async Task Order([ActivityTrigger] string[] hostNames) public async Task Dns01Precondition([ActivityTrigger] string[] hostNames) { // Azure DNS が存在するか確認 - var zones = await _dnsManagementClient.Zones.ListAsync(); + var zones = await _dnsManagementClient.Zones.ListAllAsync(); foreach (var hostName in hostNames) { @@ -153,7 +140,7 @@ public async Task Dns01Authorization([ActivityTrigger] (string, var challengeValidationDetails = AuthorizationDecoder.ResolveChallengeForDns01(authz, challenge, acmeProtocolClient.Signer); // Azure DNS の TXT レコードを書き換え - var zone = (await _dnsManagementClient.Zones.ListAsync()).First(x => challengeValidationDetails.DnsRecordName.EndsWith(x.Name)); + var zone = (await _dnsManagementClient.Zones.ListAllAsync()).First(x => challengeValidationDetails.DnsRecordName.EndsWith(x.Name)); var resourceId = ParseResourceId(zone.Id);