Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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+?)\\}\\}");
Copy link
Contributor

@davidfowl davidfowl Jun 27, 2023

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

Copy link
Member

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+)?)}}"

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
Copy link
Member

Choose a reason for hiding this comment

The 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)?

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build Ubuntu)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci (Build macOS latest)

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.

Check failure on line 37 in src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs#L37

src/ReverseProxy/Configuration/Filters/ConfigSubstitutionFilter.cs(37,34): error IL2026: (NETCORE_ENGINEERING_TELEMETRY=Build) Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API review?

=> builder.AddConfigFilter<ConfigSubstitutionFilter>();
}
2 changes: 1 addition & 1 deletion src/ReverseProxy/Yarp.ReverseProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand Down