diff options
author | WouterTinus <wouter.tinus@gmail.com> | 2019-09-07 01:36:12 +0200 |
---|---|---|
committer | WouterTinus <wouter.tinus@gmail.com> | 2019-09-07 01:36:12 +0200 |
commit | 7673fa357a81444cf6c216267dfab4e76684ba5c (patch) | |
tree | 73c0bd36e5b6261cd89a168c2a099f6556c59f4d /src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs | |
parent | 42aa0faa4de6ea4184cfe1a5830508777418b11a (diff) | |
download | letsencrypt-win-simple-7673fa357a81444cf6c216267dfab4e76684ba5c.zip letsencrypt-win-simple-7673fa357a81444cf6c216267dfab4e76684ba5c.tar.gz letsencrypt-win-simple-7673fa357a81444cf6c216267dfab4e76684ba5c.tar.bz2 |
move plugins & re-implement WebDav
Diffstat (limited to 'src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs')
-rw-r--r-- | src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs b/src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs new file mode 100644 index 0000000..4ed53f4 --- /dev/null +++ b/src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs @@ -0,0 +1,160 @@ +using PKISharp.WACS.DomainObjects; +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 +{ + internal class ScriptOptionsFactory : ValidationPluginOptionsFactory<Script, ScriptOptions> + { + private readonly ILogService _log; + private readonly IArgumentsService _arguments; + + public ScriptOptionsFactory(ILogService log, IArgumentsService arguments) : base(Constants.Dns01ChallengeType) + { + _log = log; + _arguments = arguments; + } + + public override ScriptOptions Aquire(Target target, IInputService input, RunLevel runLevel) + { + var args = _arguments.GetArguments<ScriptArguments>(); + var ret = new ScriptOptions(); + var createScript = ""; + do + { + createScript = _arguments.TryGetArgument(args.DnsCreateScript, input, "Path to script that creates DNS records"); + } + 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") + }).Invoke(); + + 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)) + { + deleteArgs = _arguments.TryGetArgument(args.DnsDeleteScriptArguments, input, $"Input parameters for delete script, or enter for default \"{Script.DefaultDeleteArguments}\""); + } + ProcessArgs(ret, createArgs, deleteArgs); + + return ret; + } + + public override ScriptOptions Default(Target target) + { + var args = _arguments.GetArguments<ScriptArguments>(); + var ret = new ScriptOptions(); + ProcessScripts(ret, args.DnsScript, args.DnsCreateScript, args.DnsDeleteScript); + if (!string.IsNullOrEmpty(ret.Script)) + { + if (!ret.Script.ValidFile(_log)) + { + _log.Error($"Invalid argument --{nameof(args.DnsScript).ToLower()}"); + return null; + } + } + else + { + if (!ret.CreateScript.ValidFile(_log)) + { + _log.Error($"Invalid argument --{nameof(args.DnsCreateScript).ToLower()}"); + return null; + } + if (!string.IsNullOrEmpty(ret.DeleteScript)) + { + if (!ret.DeleteScript.ValidFile(_log)) + { + _log.Error($"Invalid argument --{nameof(args.DnsDeleteScript).ToLower()}"); + return null; + } + } + } + + 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)) + { + if(!string.IsNullOrWhiteSpace(createInput)) + { + _log.Warning($"Ignoring --dnscreatescript because --dnsscript was provided"); + } + if (!string.IsNullOrWhiteSpace(deleteInput)) + { + _log.Warning("Ignoring --dnsdeletescript because --dnsscript was provided"); + } + } + + + if (string.IsNullOrWhiteSpace(commonInput) && + string.Equals(createInput, deleteInput, StringComparison.CurrentCultureIgnoreCase)) + { + commonInput = createInput; + } + if (!string.IsNullOrWhiteSpace(commonInput)) + { + options.Script = commonInput; + } + 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; + } + } + +} |