-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
created configuration-driven proxy filter, which uses a handlebar-lik… #2166
Changes from 3 commits
1b2a2b8
da635a7
24af33c
0f00bb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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; | ||
|
||
public class ConfigSubstitutionFilter : IProxyConfigFilter | ||
{ | ||
// Matches {{env_var_name}} or {{my-name}} or {{123name}} etc. | ||
private readonly Regex _exp = new("\\{\\{(\\w+\\-?\\w+?)\\}\\}"); | ||
private readonly IConfiguration _configuration; | ||
|
||
public ConfigSubstitutionFilter(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; | ||
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why IsMatch and then Matches(..)[0] rather than just using Match (and checking its Success property)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume they'd be equivalent, so that's a good call-out. @samsp-msft do you think this would enable consistent behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IsMatch followed by Matches will run the regex twice rather than once, and Matches will result in allocating an additional collection object. Just using Match, e.g. Match m = _exp.Match(origAddress);
if (m.Success)
{
var lookup = match.Groups[1].Value;
...
} will have exactly the same functional semantics but be a heck of a lot cheaper. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I'll update the PR with that. |
||
var newAddress = _configuration.GetValue<string>(lookup); | ||
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs Azure Pipelines / microsoft-reverse-proxy-cisrc/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37
|
||
Tratcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
=> 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 ConfigSubstitutionFilterProxyBuilderExtensions | ||
{ | ||
public static IReverseProxyBuilder AddConfigurationDrivenProxyFilter(this IReverseProxyBuilder builder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API review? |
||
=> builder.AddConfigFilter<ConfigSubstitutionFilter>(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get a review on this regex? Also this should probably use the regex source generator.
cc @stephentoub
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be easier to read if the expression had fewer slashes:
@"{{(\w+-?\w+?)}}"
Is the intention that this can also match
{{my-}}
with no word after a dash? If not, it'd be better as:@"{{(\w+(?:-\w+)?)}}"