-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
…e approach to wiring up configuration values - like routes and endpoints - based on values coming out of configuration.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Yarp.ReverseProxy.Configuration; | ||
|
||
internal class ConfigurationDrivenFilter : IProxyConfigFilter | ||
{ | ||
// Matches {{env_var_name}} or {{my-name}} or {{123name}} etc. | ||
private readonly Regex _exp = new("\\{\\{(\\w+\\-?\\w+?)\\}\\}"); | ||
private readonly IConfiguration _configuration; | ||
|
||
public ConfigurationDrivenFilter(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public ValueTask<ClusterConfig> ConfigureClusterAsync(ClusterConfig cluster, CancellationToken cancel) | ||
{ | ||
if (cluster.Destinations is null) | ||
{ | ||
return new ValueTask<ClusterConfig>(cluster); | ||
} | ||
|
||
// Each cluster has a dictionary of destinations, which is read-only, so we'll create a new one with our updates | ||
var newDests = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase); | ||
|
||
foreach (var d in cluster.Destinations) | ||
{ | ||
var origAddress = d.Value.Address; | ||
if (_exp.IsMatch(origAddress)) | ||
{ | ||
var lookup = _exp.Matches(origAddress)[0].Groups[1].Value; | ||
var newAddress = _configuration.GetValue<string>(lookup); | ||
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37
|
||
|
||
if (string.IsNullOrWhiteSpace(newAddress)) | ||
{ | ||
throw new System.ArgumentException($"Configuration Filter Error: Substitution for '{lookup}' in cluster '{d.Key}' not found in configuration."); | ||
} | ||
|
||
var modifiedDest = d.Value with { Address = newAddress }; | ||
newDests.Add(d.Key, modifiedDest); | ||
} | ||
else | ||
{ | ||
newDests.Add(d.Key, d.Value); | ||
} | ||
} | ||
|
||
return new ValueTask<ClusterConfig>(cluster with { Destinations = newDests }); | ||
} | ||
|
||
public ValueTask<RouteConfig> ConfigureRouteAsync(RouteConfig route, ClusterConfig? cluster, CancellationToken cancel) | ||
{ | ||
if (route.Order.HasValue && route.Order.Value < 1) | ||
{ | ||
return new ValueTask<RouteConfig>(route with { Order = 1 }); | ||
} | ||
|
||
return new ValueTask<RouteConfig>(route); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Yarp.ReverseProxy.Configuration; | ||
|
||
public static class ConfigurationDrivenFilterReverseProxyBuilderExtensions | ||
{ | ||
public static IReverseProxyBuilder AddConfigurationDrivenProxyFilter(this IReverseProxyBuilder builder) | ||
=> builder.AddConfigFilter<ConfigurationDrivenFilter>(); | ||
} |