-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Azure Private DNS (Experimental) (#530)
* Added support for Azure Private DNS (Experimental) * Update to GA PrivateDns package
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace KeyVault.Acmebot.Options; | ||
|
||
public class AzurePrivateDnsOptions | ||
{ | ||
public string SubscriptionId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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.PrivateDns; | ||
using Azure.ResourceManager.PrivateDns.Models; | ||
|
||
using KeyVault.Acmebot.Internal; | ||
using KeyVault.Acmebot.Options; | ||
|
||
namespace KeyVault.Acmebot.Providers; | ||
|
||
internal class AzurePrivateDnsProvider : IDnsProvider | ||
{ | ||
public AzurePrivateDnsProvider(AzurePrivateDnsOptions options, AzureEnvironment environment) | ||
{ | ||
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions | ||
{ | ||
AuthorityHost = environment.ActiveDirectory | ||
}); | ||
|
||
_armClient = new ArmClient(credential, options.SubscriptionId, new ArmClientOptions { Environment = environment.ResourceManager }); | ||
} | ||
|
||
private readonly ArmClient _armClient; | ||
|
||
public int PropagationSeconds => 10; | ||
|
||
public async Task<IReadOnlyList<DnsZone>> ListZonesAsync() | ||
{ | ||
var zones = new List<DnsZone>(); | ||
|
||
var subscription = await _armClient.GetDefaultSubscriptionAsync(); | ||
|
||
var result = subscription.GetPrivateDnsZonesAsync(); | ||
|
||
await foreach (var zone in result) | ||
{ | ||
zones.Add(new DnsZone(this) { Id = zone.Id, Name = zone.Data.Name }); | ||
} | ||
|
||
return zones; | ||
} | ||
|
||
public Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnumerable<string> values) | ||
{ | ||
// TXT レコードに値をセットする | ||
var txtRecordData = new PrivateDnsTxtRecordData(); | ||
|
||
foreach (var value in values) | ||
{ | ||
txtRecordData.PrivateDnsTxtRecords.Add(new PrivateDnsTxtRecordInfo { Values = { value } }); | ||
} | ||
|
||
var dnsZoneResource = _armClient.GetPrivateDnsZoneResource(new ResourceIdentifier(zone.Id)); | ||
|
||
var dnsTxtRecords = dnsZoneResource.GetPrivateDnsTxtRecords(); | ||
|
||
return dnsTxtRecords.CreateOrUpdateAsync(WaitUntil.Completed, relativeRecordName, txtRecordData); | ||
} | ||
|
||
public async Task DeleteTxtRecordAsync(DnsZone zone, string relativeRecordName) | ||
{ | ||
var dnsZoneResource = _armClient.GetPrivateDnsZoneResource(new ResourceIdentifier(zone.Id)); | ||
|
||
try | ||
{ | ||
PrivateDnsTxtRecordResource dnsTxtRecordResource = await dnsZoneResource.GetPrivateDnsTxtRecordAsync(relativeRecordName); | ||
|
||
await dnsTxtRecordResource.DeleteAsync(WaitUntil.Completed); | ||
} | ||
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound) | ||
{ | ||
// ignored | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters