summaryrefslogtreecommitdiffstats
path: root/src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
diff options
context:
space:
mode:
authorWouterTinus <wouter.tinus@gmail.com>2019-02-19 21:54:29 +0100
committerWouterTinus <wouter.tinus@gmail.com>2019-02-19 21:54:29 +0100
commit26bbc1a9909534dc985dc6a875a45c9b355f42c5 (patch)
tree066ee47d561c4591e4ac8a2dcf49cc9554dd1373 /src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
parent354218e84919372620e63d207fc8d4a7f04b8e5d (diff)
downloadletsencrypt-win-simple-26bbc1a9909534dc985dc6a875a45c9b355f42c5.zip
letsencrypt-win-simple-26bbc1a9909534dc985dc6a875a45c9b355f42c5.tar.gz
letsencrypt-win-simple-26bbc1a9909534dc985dc6a875a45c9b355f42c5.tar.bz2
Pimp DNS script plugin
Diffstat (limited to 'src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs')
-rw-r--r--src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs125
1 files changed, 109 insertions, 16 deletions
diff --git a/src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs b/src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
index 723f70b..68681a3 100644
--- a/src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
+++ b/src/main/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
@@ -3,6 +3,7 @@ using PKISharp.WACS.Extensions;
using PKISharp.WACS.Plugins.Base.Factories;
using PKISharp.WACS.Services;
using System;
+using System.Collections.Generic;
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
@@ -10,42 +11,134 @@ namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns
{
public ScriptOptionsFactory(ILogService log) : base(log, Constants.Dns01ChallengeType) { }
- public override ScriptOptions Aquire(Target target, IArgumentsService options, IInputService input, RunLevel runLevel)
+ public override ScriptOptions Aquire(Target target, IArgumentsService arguments, IInputService input, RunLevel runLevel)
{
- var args = options.GetArguments<ScriptArguments>();
+ var args = arguments.GetArguments<ScriptArguments>();
var ret = new ScriptOptions();
+ var createScript = "";
do
{
- ret.CreateScript = options.TryGetArgument(args.DnsCreateScript, input, "Path to script that creates DNS records. Parameters passed are the hostname, record name and token");
+ createScript = arguments.TryGetArgument(args.DnsCreateScript, input, "Path to script that creates DNS records");
}
- while (!ret.CreateScript.ValidFile(_log));
- do
+ while (!createScript.ValidFile(_log));
+
+ var deleteScript = "";
+ input.ChooseFromList(
+ "How to delete records after validation",
+ new List<Choice<Action>>()
+ {
+ Choice.Create<Action>(() => { deleteScript = createScript; }, "Using the same script"),
+ Choice.Create<Action>(() => {
+ do {
+ deleteScript = arguments.TryGetArgument(args.DnsDeleteScript, input, "Path to script that deletes DNS records");
+ }
+ while (!deleteScript.ValidFile(_log));
+ }, "Using a different script"),
+ Choice.Create<Action>(() => { }, "Do not delete")
+ },
+ false);
+
+ ProcessScripts(ret, null, createScript, deleteScript);
+
+ input.Show("{Identifier}", "Domain that's being validated");
+ input.Show("{RecordName}", "Full TXT record name");
+ input.Show("{Token}", "Expected value in the TXT record");
+ var createArgs = arguments.TryGetArgument(args.DnsCreateScriptArguments, input, $"Input parameters for create script, or enter for default \"{Script.DefaultCreateArguments}\"");
+ var deleteArgs = "";
+ if (!string.IsNullOrWhiteSpace(ret.DeleteScript) ||
+ !string.IsNullOrWhiteSpace(ret.Script))
{
- ret.DeleteScript = options.TryGetArgument(args.DnsDeleteScript, input, "Path to script that deletes DNS records. Parameters passed are the hostname and record name");
+ arguments.TryGetArgument(args.DnsDeleteScriptArguments, input, $"Input parameters for delete script, or enter for default \"{Script.DefaultDeleteArguments}\"");
}
- while (!ret.DeleteScript.ValidFile(_log));
+ ProcessArgs(ret, createArgs, deleteArgs);
+
return ret;
}
public override ScriptOptions Default(Target target, IArgumentsService arguments)
{
var args = arguments.GetArguments<ScriptArguments>();
- var ret = new ScriptOptions
- {
- CreateScript = arguments.TryGetRequiredArgument(nameof(args.DnsCreateScript), args.DnsCreateScript),
- DeleteScript = arguments.TryGetRequiredArgument(nameof(args.DnsDeleteScript), args.DnsDeleteScript)
- };
- if (!ret.CreateScript.ValidFile(_log))
+ var ret = new ScriptOptions();
+ ProcessScripts(ret, args.DnsScript, args.DnsCreateScript, args.DnsDeleteScript);
+ if (!string.IsNullOrEmpty(ret.Script))
{
- throw new ArgumentException(nameof(args.DnsCreateScript));
+ if (!ret.Script.ValidFile(_log))
+ {
+ throw new ArgumentException(nameof(args.DnsCreateScript));
+ }
}
- if (!ret.DeleteScript.ValidFile(_log))
+ else
{
- throw new ArgumentException(nameof(args.DnsDeleteScript));
+ if (!ret.CreateScript.ValidFile(_log))
+ {
+ throw new ArgumentException(nameof(args.DnsCreateScript));
+ }
+ if (!string.IsNullOrEmpty(ret.DeleteScript))
+ {
+ if (!ret.DeleteScript.ValidFile(_log))
+ {
+ throw new ArgumentException(nameof(args.DnsDeleteScript));
+ }
+ }
}
+
+ ProcessArgs(ret, args.DnsCreateScriptArguments, args.DnsDeleteScriptArguments);
return ret;
}
+ /// <summary>
+ /// Choose the right script to run
+ /// </summary>
+ /// <param name="options"></param>
+ /// <param name="commonInput"></param>
+ /// <param name="createInput"></param>
+ /// <param name="deleteInput"></param>
+ private void ProcessScripts(ScriptOptions options, string commonInput, string createInput, string deleteInput)
+ {
+ if (string.IsNullOrWhiteSpace(commonInput) &&
+ string.Equals(createInput, deleteInput, StringComparison.CurrentCultureIgnoreCase))
+ {
+ commonInput = createInput;
+ }
+ if (!string.IsNullOrWhiteSpace(commonInput))
+ {
+ options.Script = createInput;
+ if (!string.IsNullOrWhiteSpace(createInput) &&
+ !string.Equals(createInput, commonInput, StringComparison.InvariantCultureIgnoreCase))
+ {
+ _log.Warning($"Ignoring --dnscreatescript because --dnsscript was provided");
+ }
+ if (!string.IsNullOrWhiteSpace(deleteInput) &&
+ !string.Equals(deleteInput, commonInput, StringComparison.InvariantCultureIgnoreCase))
+ {
+ _log.Warning("Ignoring --dnsdeletescript because --dnsscript was provided");
+ }
+ }
+ else
+ {
+ options.CreateScript = string.IsNullOrWhiteSpace(createInput) ? null : createInput;
+ options.DeleteScript = string.IsNullOrWhiteSpace(deleteInput) ? null : deleteInput;
+ }
+ }
+
+ private void ProcessArgs(ScriptOptions options, string createInput, string deleteInput)
+ {
+ if (!string.IsNullOrWhiteSpace(createInput) &&
+ createInput != Script.DefaultCreateArguments)
+ {
+ options.CreateScriptArguments = createInput;
+ }
+ if (!string.IsNullOrWhiteSpace(options.DeleteScript) ||
+ !string.IsNullOrWhiteSpace(options.Script))
+ {
+ if (!string.IsNullOrWhiteSpace(deleteInput) &&
+ deleteInput != Script.DefaultDeleteArguments)
+ {
+ options.DeleteScriptArguments = deleteInput;
+ }
+ }
+ }
+
public override bool CanValidate(Target target)
{
return true;