|
1 | | -// Licensed to the .NET Foundation under one or more agreements. |
2 | | -// The .NET Foundation licenses this file to you under the MIT license. |
3 | | -// See the LICENSE file in the project root for more information. |
4 | | - |
5 | | -using System.CommandLine; |
6 | | -using Microsoft.Extensions.Configuration; |
7 | | -using Microsoft.Extensions.Logging; |
8 | | - |
9 | | -namespace DevProxy.Abstractions; |
10 | | - |
11 | | -public abstract class BaseProxyPlugin : IProxyPlugin |
12 | | -{ |
13 | | - protected ISet<UrlToWatch> UrlsToWatch { get; } |
14 | | - protected ILogger Logger { get; } |
15 | | - protected IConfigurationSection? ConfigSection { get; } |
16 | | - protected IPluginEvents PluginEvents { get; } |
17 | | - protected IProxyContext Context { get; } |
18 | | - |
19 | | - public virtual string Name => throw new NotImplementedException(); |
20 | | - |
21 | | - public virtual Option[] GetOptions() => []; |
22 | | - public virtual Command[] GetCommands() => []; |
23 | | - |
24 | | - public BaseProxyPlugin(IPluginEvents pluginEvents, |
25 | | - IProxyContext context, |
26 | | - ILogger logger, |
27 | | - ISet<UrlToWatch> urlsToWatch, |
28 | | - IConfigurationSection? configSection = null) |
29 | | - { |
30 | | - ArgumentNullException.ThrowIfNull(pluginEvents); |
31 | | - ArgumentNullException.ThrowIfNull(context); |
32 | | - ArgumentNullException.ThrowIfNull(logger); |
33 | | - |
34 | | - if (urlsToWatch is null || !urlsToWatch.Any()) |
35 | | - { |
36 | | - throw new ArgumentException($"{nameof(urlsToWatch)} cannot be null or empty", nameof(urlsToWatch)); |
37 | | - } |
38 | | - |
39 | | - UrlsToWatch = urlsToWatch; |
40 | | - Context = context; |
41 | | - Logger = logger; |
42 | | - ConfigSection = configSection; |
43 | | - PluginEvents = pluginEvents; |
44 | | - } |
45 | | - |
46 | | - public virtual Task RegisterAsync() |
47 | | - { |
48 | | - return Task.CompletedTask; |
49 | | - } |
50 | | -} |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.CommandLine; |
| 6 | +using System.Globalization; |
| 7 | +using System.Text.Json; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | + |
| 11 | +namespace DevProxy.Abstractions; |
| 12 | + |
| 13 | +public abstract class BaseProxyPlugin : IProxyPlugin |
| 14 | +{ |
| 15 | + protected ISet<UrlToWatch> UrlsToWatch { get; } |
| 16 | + protected ILogger Logger { get; } |
| 17 | + protected IConfigurationSection? ConfigSection { get; } |
| 18 | + protected IPluginEvents PluginEvents { get; } |
| 19 | + protected IProxyContext Context { get; } |
| 20 | + |
| 21 | + public virtual string Name => throw new NotImplementedException(); |
| 22 | + |
| 23 | + public virtual Option[] GetOptions() => []; |
| 24 | + public virtual Command[] GetCommands() => []; |
| 25 | + |
| 26 | + public BaseProxyPlugin(IPluginEvents pluginEvents, |
| 27 | + IProxyContext context, |
| 28 | + ILogger logger, |
| 29 | + ISet<UrlToWatch> urlsToWatch, |
| 30 | + IConfigurationSection? configSection = null) |
| 31 | + { |
| 32 | + ArgumentNullException.ThrowIfNull(pluginEvents); |
| 33 | + ArgumentNullException.ThrowIfNull(context); |
| 34 | + ArgumentNullException.ThrowIfNull(logger); |
| 35 | + |
| 36 | + if (urlsToWatch is null || !urlsToWatch.Any()) |
| 37 | + { |
| 38 | + throw new ArgumentException($"{nameof(urlsToWatch)} cannot be null or empty", nameof(urlsToWatch)); |
| 39 | + } |
| 40 | + |
| 41 | + UrlsToWatch = urlsToWatch; |
| 42 | + Context = context; |
| 43 | + Logger = logger; |
| 44 | + ConfigSection = configSection; |
| 45 | + PluginEvents = pluginEvents; |
| 46 | + } |
| 47 | + |
| 48 | + public virtual async Task RegisterAsync() |
| 49 | + { |
| 50 | + var (IsValid, ValidationErrors) = await ValidatePluginConfig(); |
| 51 | + if (!IsValid) |
| 52 | + { |
| 53 | + Logger.LogError("Plugin configuration validation failed with the following errors: {Errors}", string.Join(", ", ValidationErrors)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected async Task<(bool IsValid, IEnumerable<string> ValidationErrors)> ValidatePluginConfig() |
| 58 | + { |
| 59 | + if (!Context.Configuration.ValidateSchemas || ConfigSection is null) |
| 60 | + { |
| 61 | + Logger.LogDebug("Schema validation is disabled or no configuration section specified"); |
| 62 | + return (true, []); |
| 63 | + } |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + var schemaUrl = ConfigSection.GetValue<string>("$schema"); |
| 68 | + if (string.IsNullOrWhiteSpace(schemaUrl)) |
| 69 | + { |
| 70 | + Logger.LogDebug("No schema URL found in configuration file"); |
| 71 | + return (true, []); |
| 72 | + } |
| 73 | + |
| 74 | + var configSectionName = ConfigSection.Key; |
| 75 | + var configFile = await File.ReadAllTextAsync(Context.Configuration.ConfigFile); |
| 76 | + |
| 77 | + using var document = JsonDocument.Parse(configFile); |
| 78 | + var root = document.RootElement; |
| 79 | + |
| 80 | + if (!root.TryGetProperty(configSectionName, out var configSection)) |
| 81 | + { |
| 82 | + Logger.LogError("Configuration section {SectionName} not found in configuration file", configSectionName); |
| 83 | + return (false, [string.Format(CultureInfo.InvariantCulture, "Configuration section {0} not found in configuration file", configSectionName)]); |
| 84 | + } |
| 85 | + |
| 86 | + return await ProxyUtils.ValidateJson(configSection.GetRawText(), schemaUrl, Logger); |
| 87 | + } |
| 88 | + catch (Exception ex) |
| 89 | + { |
| 90 | + return (false, [ex.Message]); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments