forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigValidator.cs
More file actions
86 lines (68 loc) · 3.09 KB
/
ConfigValidator.cs
File metadata and controls
86 lines (68 loc) · 3.09 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Configuration.ClusterValidators;
using Yarp.ReverseProxy.Configuration.RouteValidators;
using Yarp.ReverseProxy.Transforms.Builder;
namespace Yarp.ReverseProxy.Configuration;
internal sealed class ConfigValidator : IConfigValidator
{
private readonly ITransformBuilder _transformBuilder;
private readonly IRouteValidator[] _routeValidators;
private readonly IClusterValidator[] _clusterValidators;
public ConfigValidator(ITransformBuilder transformBuilder,
IEnumerable<IRouteValidator> routeValidators,
IEnumerable<IClusterValidator> clusterValidators)
{
ArgumentNullException.ThrowIfNull(transformBuilder);
ArgumentNullException.ThrowIfNull(routeValidators);
ArgumentNullException.ThrowIfNull(clusterValidators);
_transformBuilder = transformBuilder;
_routeValidators = routeValidators.ToArray();
_clusterValidators = clusterValidators.ToArray();
}
// Note this performs all validation steps without short-circuiting in order to report all possible errors.
public async ValueTask<IList<Exception>> ValidateRouteAsync(RouteConfig route)
{
ArgumentNullException.ThrowIfNull(route);
var errors = new List<Exception>();
if (string.IsNullOrEmpty(route.RouteId))
{
errors.Add(new ArgumentException("Missing Route Id."));
}
errors.AddRange(_transformBuilder.ValidateRoute(route));
if (route.Match is null)
{
errors.Add(new ArgumentException($"Route '{route.RouteId}' did not set any match criteria, it requires Hosts or Path specified. Set the Path to '/{{**catchall}}' to match all requests."));
return errors;
}
if ((route.Match.Hosts is null || route.Match.Hosts.All(string.IsNullOrEmpty)) && string.IsNullOrEmpty(route.Match.Path))
{
errors.Add(new ArgumentException($"Route '{route.RouteId}' requires Hosts or Path specified. Set the Path to '/{{**catchall}}' to match all requests."));
}
foreach (var routeValidator in _routeValidators)
{
await routeValidator.ValidateAsync(route, errors);
}
return errors;
}
// Note this performs all validation steps without short-circuiting in order to report all possible errors.
public async ValueTask<IList<Exception>> ValidateClusterAsync(ClusterConfig cluster)
{
ArgumentNullException.ThrowIfNull(cluster);
var errors = new List<Exception>();
if (string.IsNullOrEmpty(cluster.ClusterId))
{
errors.Add(new ArgumentException("Missing Cluster Id."));
}
errors.AddRange(_transformBuilder.ValidateCluster(cluster));
foreach (var clusterValidator in _clusterValidators)
{
await clusterValidator.ValidateAsync(cluster, errors);
}
return errors;
}
}