Skip to content

Commit

Permalink
Merge pull request #2531 from zgcwkj/master
Browse files Browse the repository at this point in the history
Add ALiYun Dns validation
  • Loading branch information
WouterTinus committed Feb 18, 2024
2 parents dd75e2e + ad8f788 commit c7ef072
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 2 deletions.
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ build_script:
- cmd: dotnet publish ./src/main/wacs.csproj -c "ReleaseTrimmed" -r win-arm64 --self-contained /p:warninglevel=0
- cmd: dotnet publish ./src/plugin.store.keyvault/wacs.store.keyvault.csproj -c Release
- cmd: dotnet publish ./src/plugin.store.userstore/wacs.store.userstore.csproj -c "Release"
- cmd: dotnet publish ./src/plugin.validation.dns.aliyun/wacs.validation.dns.aliyun.csproj -c Release
- cmd: dotnet publish ./src/plugin.validation.dns.azure/wacs.validation.dns.azure.csproj -c Release
- cmd: dotnet publish ./src/plugin.validation.dns.cloudflare/wacs.validation.dns.cloudflare.csproj -c Release
- cmd: dotnet publish ./src/plugin.validation.dns.digitalocean/wacs.validation.dns.digitalocean.csproj -c Release
Expand Down
1 change: 1 addition & 0 deletions build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ foreach ($release in @("Release", "ReleaseTrimmed")) {
# Build plugins
& dotnet publish $RepoRoot\src\plugin.store.keyvault\wacs.store.keyvault.csproj -c "Release"
& dotnet publish $RepoRoot\src\plugin.store.userstore\wacs.store.userstore.csproj -c "Release"
& dotnet publish $RepoRoot\src\plugin.validation.dns.aliyun\wacs.validation.dns.aliyun.csproj -c "Release"
& dotnet publish $RepoRoot\src\plugin.validation.dns.azure\wacs.validation.dns.azure.csproj -c "Release"
& dotnet publish $RepoRoot\src\plugin.validation.dns.cloudflare\wacs.validation.dns.cloudflare.csproj -c "Release"
& dotnet publish $RepoRoot\src\plugin.validation.dns.digitalocean\wacs.validation.dns.digitalocean.csproj -c "Release"
Expand Down
18 changes: 18 additions & 0 deletions build/create-artifacts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ PluginRelease plugin.store.keyvault @(
PluginRelease plugin.store.userstore @(
"PKISharp.WACS.Plugins.StorePlugins.UserStore.dll"
)
PluginRelease plugin.validation.dns.aliyun @(
"AlibabaCloud.EndpointUtil.dll",
"AlibabaCloud.GatewaySpi.dll",
"AlibabaCloud.OpenApiClient.dll",
"AlibabaCloud.OpenApiUtil.dll",
"AlibabaCloud.SDK.Alidns20150109.dll",
"AlibabaCloud.TeaUtil.dll",
"AlibabaCloud.TeaXML.dll",
"Aliyun.Credentials.dll",
"Newtonsoft.Json.dll",
"Tea.dll",
"PKISharp.WACS.Plugins.ValidationPlugins.ALiYun.dll"
)
PluginRelease plugin.validation.dns.azure @(
"Azure.Core.dll",
"Azure.Identity.dll",
Expand Down Expand Up @@ -217,6 +230,11 @@ PluginRelease plugin.validation.dns.route53 @(
PluginRelease plugin.validation.dns.simply @(
"PKISharp.WACS.Plugins.ValidationPlugins.Simply.dll"
)
PluginRelease plugin.validation.dns.tencent @(
"Newtonsoft.Json.dll",
"TencentCloudCommon.dll",
"PKISharp.WACS.Plugins.ValidationPlugins.Tencent.dll"
)
PluginRelease plugin.validation.dns.transip @(
"Newtonsoft.Json.dll",
"PKISharp.WACS.Plugins.ValidationPlugins.TransIp.dll"
Expand Down
163 changes: 163 additions & 0 deletions src/plugin.validation.dns.aliyun/ALiYun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.Alidns20150109.Models;
using PKISharp.WACS.Clients.DNS;
using PKISharp.WACS.Plugins.Base.Capabilities;
using PKISharp.WACS.Plugins.Interfaces;
using PKISharp.WACS.Services;
using System;
using System.Linq;
using System.Net.Http;
using System.Runtime.Versioning;
using System.Threading.Tasks;

[assembly: SupportedOSPlatform("windows")]

namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
[IPlugin.Plugin<
ALiYunOptions, ALiYunOptionsFactory,
DnsValidationCapability, ALiYunJson>
("1d4db2ea-ce7c-46ce-b86f-40b356fcf999",
"ALiYun", "Create verification records in ALiYun DNS")]
public class ALiYun : DnsValidation<ALiYun>, IDisposable
{
private ALiYunOptions _options { get; }
private SecretServiceManager _ssm { get; }
private HttpClient _hc { get; }
private AlibabaCloud.SDK.Alidns20150109.Client _client { get; }

public ALiYun(
ALiYunOptions options,
SecretServiceManager ssm,
IProxyService proxyService,
LookupClientProvider dnsClient,
ILogService log,
ISettingsService settings) : base(dnsClient, log, settings)
{
_options = options;
_ssm = ssm;
_hc = proxyService.GetHttpClient();
//New Client
var config = new Config
{
AccessKeyId = _ssm.EvaluateSecret(_options.ApiID),
AccessKeySecret = _ssm.EvaluateSecret(_options.ApiSecret),
Endpoint = _ssm.EvaluateSecret(_options.ApiServer),
};
_client = new AlibabaCloud.SDK.Alidns20150109.Client(config);
}

public override async Task<bool> CreateRecord(DnsValidationRecord record)
{
await Task.Delay(0);
try
{
var identifier = record.Context.Identifier;
var domain = record.Authority.Domain;
var value = record.Value;
//Add Record
return AddRecord(identifier, domain, value);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Out Error
_log.Error($"Unable to add ALiYunDNS record: {ex.Message}");
}
return false;
}

public override async Task DeleteRecord(DnsValidationRecord record)
{
await Task.Delay(0);
try
{
var identifier = record.Context.Identifier;
var domain = record.Authority.Domain;
//Delete Record
DelRecord(identifier, domain);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Out Error
_log.Error($"Unable to delete ALiYunDNS record: {ex.Message}");
}
}

#region PrivateLogic

/// <summary>
/// Add Record
/// </summary>
/// <param name="domain">Domain</param>
/// <param name="subDomain">SubDomain</param>
/// <param name="value">Value</param>
/// <returns></returns>
private bool AddRecord(string domain, string subDomain, string value)
{
subDomain = subDomain.Replace($".{domain}", "");
//Delete Record
DelRecord(domain, subDomain);
//Add Record
var addRecords = new AddDomainRecordRequest
{
DomainName = domain,
RR = subDomain,
Type = "TXT",
Value = value
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
var data = _client.AddDomainRecordWithOptions(addRecords, runtime);
//Console.WriteLine(data);
return true;
}

/// <summary>
/// Delete Record
/// </summary>
/// <param name="domain">Domain</param>
/// <param name="subDomain">SubDomain</param>
/// <returns></returns>
private bool DelRecord(string domain, string subDomain)
{
subDomain = subDomain.Replace($".{domain}", "");
//Get RecordID
var recordId = GetRecordID(domain, subDomain);
if (recordId == default) return false;
//Delete Record
var delRecords = new DeleteDomainRecordRequest
{
RecordId = recordId.ToString(),
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
var data = _client.DeleteDomainRecordWithOptions(delRecords, runtime);
//Console.WriteLine(data);
return true;
}

/// <summary>
/// Get RecordID
/// </summary>
/// <param name="domain">Domain</param>
/// <param name="subDomain">SubDomain</param>
/// <returns></returns>
private string? GetRecordID(string domain, string subDomain)
{
var getRecords = new DescribeDomainRecordsRequest
{
DomainName = domain,
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
var data = _client.DescribeDomainRecordsWithOptions(getRecords, runtime);
//Console.WriteLine(data);
var jsonDataLinq = data.Body.DomainRecords.Record.Where(w => w.RR == subDomain && w.Type == "TXT");
if (jsonDataLinq.Any()) return jsonDataLinq.First().RecordId;
return default;
}

#endregion PrivateLogic

public void Dispose() => _hc.Dispose();
}
}
23 changes: 23 additions & 0 deletions src/plugin.validation.dns.aliyun/ALiYunArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using PKISharp.WACS.Configuration;
using PKISharp.WACS.Configuration.Arguments;

namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
public class ALiYunArguments : BaseArguments
{
public override string Name => "ALiYun";

public override string Group => "Validation";

public override string Condition => "--validation aliyun";

[CommandLine(Description = "DNS Server Domain Name\r\nRefer: https://api.aliyun.com/product/Alidns", Secret = false)]
public string? ALiYunServer { get; set; } = "dns.aliyuncs.com";

[CommandLine(Description = "API ID for ALiYun.", Secret = true)]
public string? ALiYunApiID { get; set; }

[CommandLine(Description = "API Secret for ALiYun.", Secret = true)]
public string? ALiYunApiSecret { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/plugin.validation.dns.aliyun/ALiYunOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using PKISharp.WACS.Plugins.Base.Options;
using PKISharp.WACS.Services.Serialization;
using System.Text.Json.Serialization;

namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
[JsonSerializable(typeof(ALiYunOptions))]
internal partial class ALiYunJson : JsonSerializerContext
{
public ALiYunJson(WacsJsonPluginsOptionsFactory optionsFactory) : base(optionsFactory.Options)
{
}
}

public class ALiYunOptions : ValidationPluginOptions
{
/// <summary>
/// ApiServer
/// </summary>
public string? ApiServer { get; set; }

/// <summary>
/// ApiID
/// </summary>
public ProtectedString? ApiID { get; set; }

/// <summary>
/// ApiSecret
/// </summary>
public ProtectedString? ApiSecret { get; set; }
}
}
49 changes: 49 additions & 0 deletions src/plugin.validation.dns.aliyun/ALiYunOptionsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using PKISharp.WACS.Configuration;
using PKISharp.WACS.Plugins.Base.Factories;
using PKISharp.WACS.Services;
using PKISharp.WACS.Services.Serialization;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
public class ALiYunOptionsFactory : PluginOptionsFactory<ALiYunOptions>
{
private ArgumentsInputService _arguments { get; }

public ALiYunOptionsFactory(ArgumentsInputService arguments) => _arguments = arguments;

private ArgumentResult<string?> ApiServer => _arguments.GetString<ALiYunArguments>(a => a.ALiYunServer).Required();

private ArgumentResult<ProtectedString?> ApiID => _arguments.GetProtectedString<ALiYunArguments>(a => a.ALiYunApiID).Required();

private ArgumentResult<ProtectedString?> ApiSecret => _arguments.GetProtectedString<ALiYunArguments>(a => a.ALiYunApiSecret).Required();

public override async Task<ALiYunOptions?> Aquire(IInputService inputService, RunLevel runLevel)
{
return new ALiYunOptions
{
ApiServer = await ApiServer.Interactive(inputService, "ALiYun Domain Server").GetValue(),
ApiID = await ApiID.Interactive(inputService, "ALiYun AccessKey ID").GetValue(),
ApiSecret = await ApiSecret.Interactive(inputService, "ALiYun AccessKey Secret").GetValue(),
};
}

public override async Task<ALiYunOptions?> Default()
{
return new ALiYunOptions
{
ApiServer = await ApiServer.GetValue(),
ApiID = await ApiID.GetValue(),
ApiSecret = await ApiSecret.GetValue(),
};
}

public override IEnumerable<(CommandLineAttribute, object?)> Describe(ALiYunOptions options)
{
yield return (ApiServer.Meta, options.ApiServer);
yield return (ApiID.Meta, options.ApiID);
yield return (ApiSecret.Meta, options.ApiSecret);
}
}
}
24 changes: 24 additions & 0 deletions src/plugin.validation.dns.aliyun/wacs.validation.dns.aliyun.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>PKISharp.WACS.Plugins.ValidationPlugins</RootNamespace>
<AssemblyName>PKISharp.WACS.Plugins.ValidationPlugins.ALiYun</AssemblyName>
<Version>2.1.0.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AlibabaCloud.SDK.Alidns20150109" Version="3.0.13" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\main.lib\wacs.lib.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>PKISharp.WACS.Plugins.ValidationPlugins</RootNamespace>
<AssemblyName>PKISharp.WACS.Plugins.ValidationPlugins.DnsPod</AssemblyName>
<AssemblyName>PKISharp.WACS.Plugins.ValidationPlugins.Tencent</AssemblyName>
<Version>2.1.0.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
Expand Down
12 changes: 11 additions & 1 deletion src/wacs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wacs.validation.dns.infoman
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wacs.validation.dns.rfc2136", "plugin.validation.dns.rfc2136\wacs.validation.dns.rfc2136.csproj", "{C22EFB37-8CC1-42C2-8F82-6670969DBB0C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wacs.validation.dns.dnspod", "plugin.validation.dns.tencent\wacs.validation.dns.tencent.csproj", "{40C9AE2F-F3AC-41A1-B211-BF1C71880F3D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wacs.validation.dns.tencent", "plugin.validation.dns.tencent\wacs.validation.dns.tencent.csproj", "{40C9AE2F-F3AC-41A1-B211-BF1C71880F3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "wacs.validation.dns.aliyun", "plugin.validation.dns.aliyun\wacs.validation.dns.aliyun.csproj", "{7CBE9529-3C20-4D87-BBAB-F27E9556758E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -255,6 +257,14 @@ Global
{40C9AE2F-F3AC-41A1-B211-BF1C71880F3D}.Release|Any CPU.Build.0 = Release|Any CPU
{40C9AE2F-F3AC-41A1-B211-BF1C71880F3D}.ReleaseTrimmed|Any CPU.ActiveCfg = Release|Any CPU
{40C9AE2F-F3AC-41A1-B211-BF1C71880F3D}.ReleaseTrimmed|Any CPU.Build.0 = Release|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.DebugTrimmed|Any CPU.ActiveCfg = Debug|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.DebugTrimmed|Any CPU.Build.0 = Debug|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.Release|Any CPU.Build.0 = Release|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.ReleaseTrimmed|Any CPU.ActiveCfg = Release|Any CPU
{7CBE9529-3C20-4D87-BBAB-F27E9556758E}.ReleaseTrimmed|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit c7ef072

Please sign in to comment.