summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Plugins/ValidationPlugins/Dns/Script/ScriptOptionsFactory.cs
blob: 9f2f2a69fbf4dde2596a125aae96f9f51211a776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using PKISharp.WACS.DomainObjects;
using PKISharp.WACS.Extensions;
using PKISharp.WACS.Plugins.Base.Factories;
using PKISharp.WACS.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

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 Task<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 Task.FromResult(ret);
        }

        public override Task<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 Task.FromResult(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) => true;
    }

}