Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new version of Azure.ResourceManager.Dns SDK #505

Merged
merged 3 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions KeyVault.Acmebot/Internal/AzureEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;

using Azure.ResourceManager;

namespace KeyVault.Acmebot.Internal;

public class AzureEnvironment
{
public Uri ActiveDirectory { get; private init; }
public Uri ResourceManager { get; private init; }
public ArmEnvironment ResourceManager { get; private init; }

public static AzureEnvironment Get(string name) => s_environments[name];

Expand All @@ -17,23 +19,23 @@ public class AzureEnvironment
new AzureEnvironment
{
ActiveDirectory = new Uri("https://login.microsoftonline.com"),
ResourceManager = new Uri("https://management.azure.com")
ResourceManager = ArmEnvironment.AzurePublicCloud
}
},
{
"AzureChinaCloud",
new AzureEnvironment
{
ActiveDirectory = new Uri("https://login.chinacloudapi.cn"),
ResourceManager = new Uri("https://management.chinacloudapi.cn")
ResourceManager = ArmEnvironment.AzureChina
}
},
{
"AzureUSGovernment",
new AzureEnvironment
{
ActiveDirectory = new Uri("https://login.microsoftonline.us"),
ResourceManager = new Uri("https://management.usgovcloudapi.net")
ResourceManager = ArmEnvironment.AzureGovernment
}
}
};
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,9 +4,9 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Route53" Version="3.7.3.12" />
<PackageReference Include="AWSSDK.Route53" Version="3.7.3.16" />
<PackageReference Include="Azure.Identity" Version="1.6.0" />
<PackageReference Include="Azure.ResourceManager.Dns" Version="1.0.0-preview.1" />
<PackageReference Include="Azure.ResourceManager.Dns" Version="1.0.0-beta.1" />
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.3.0" />
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.3.0" />
<PackageReference Include="DnsClient" Version="1.6.1" />
Expand Down
45 changes: 27 additions & 18 deletions KeyVault.Acmebot/Providers/AzureDnsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Dns;
using Azure.ResourceManager.Dns.Models;

Expand All @@ -20,33 +23,33 @@ public AzureDnsProvider(AzureDnsOptions options, AzureEnvironment environment)
AuthorityHost = environment.ActiveDirectory
});

_dnsManagementClient = new DnsManagementClient(options.SubscriptionId, environment.ResourceManager, credential);
_armClient = new ArmClient(credential, options.SubscriptionId, new ArmClientOptions { Environment = environment.ResourceManager });
}

private readonly DnsManagementClient _dnsManagementClient;
private readonly ArmClient _armClient;

public int PropagationSeconds => 10;

public async Task<IReadOnlyList<DnsZone>> ListZonesAsync()
{
var zones = new List<DnsZone>();

var result = _dnsManagementClient.Zones.ListAsync();
var subscription = await _armClient.GetDefaultSubscriptionAsync();

var result = subscription.GetDnsZonesByDnszoneAsync();

await foreach (var zone in result)
{
zones.Add(new DnsZone(this) { Id = zone.Id, Name = zone.Name, NameServers = zone.NameServers });
zones.Add(new DnsZone(this) { Id = zone.Id, Name = zone.Data.Name, NameServers = zone.Data.NameServers });
}

return zones;
}

public Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnumerable<string> values)
{
var resourceGroup = ExtractResourceGroup(zone.Id);

// TXT レコードに TTL と値をセットする
var recordSet = new RecordSet
var recordSet = new TxtRecordSetData
{
TTL = 60
};
Expand All @@ -56,20 +59,26 @@ public Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnume
recordSet.TxtRecords.Add(new TxtRecord { Value = { value } });
}

return _dnsManagementClient.RecordSets.CreateOrUpdateAsync(resourceGroup, zone.Name, relativeRecordName, RecordType.TXT, recordSet);
}
var dnsZoneResource = _armClient.GetDnsZoneResource(new ResourceIdentifier(zone.Id));

public Task DeleteTxtRecordAsync(DnsZone zone, string relativeRecordName)
{
var resourceGroup = ExtractResourceGroup(zone.Id);
var recordSets = dnsZoneResource.GetRecordSetTxts();

return _dnsManagementClient.RecordSets.DeleteAsync(resourceGroup, zone.Name, relativeRecordName, RecordType.TXT);
return recordSets.CreateOrUpdateAsync(WaitUntil.Completed, relativeRecordName, recordSet);
}

private static string ExtractResourceGroup(string resourceId)
public async Task DeleteTxtRecordAsync(DnsZone zone, string relativeRecordName)
{
var values = resourceId.Split('/', StringSplitOptions.RemoveEmptyEntries);
var dnsZoneResource = _armClient.GetDnsZoneResource(new ResourceIdentifier(zone.Id));

return values[3];
try
{
var recordSets = await dnsZoneResource.GetRecordSetTxtAsync(relativeRecordName);

await recordSets.Value.DeleteAsync(WaitUntil.Completed);
}
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound)
{
// ignored
}
}
}