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 1 commit
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,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
bradygaster marked this conversation as resolved.
Show resolved Hide resolved
{
// Matches {{env_var_name}} or {{my-name}} or {{123name}} etc.
private readonly Regex _exp = new("\\{\\{(\\w+\\-?\\w+?)\\}\\}");
Copy link
Contributor

Choose a reason for hiding this comment

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

is using {{ }} too similar to route paths?

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 don't think it's a bad thing; i think the handlebars are something most web folks are accustomed to using.

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))
Copy link
Contributor

Choose a reason for hiding this comment

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

substitution here is only against full values, and for destinations. should this be more generic?

  • should I be able to have a destination = "{{hostname}}:1234" where only hostname is replaced?
  • should I be able to use this syntax in other places in configuration?

Copy link
Member Author

Choose a reason for hiding this comment

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

We discussed this in a call; I think we landed on being okay with this current approach and it mainly being used for destinations. Correct me if I'm wrong. :)

{
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

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

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

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs

View check run for this annotation

Azure Pipelines / microsoft-reverse-proxy-ci

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.cs#L37

src/ReverseProxy/Configuration/ConfigurationDrivenFilter/ConfigurationDrivenFilter.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.

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 });
}
Copy link
Member

Choose a reason for hiding this comment

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

This is unrelated, it's copy-pasted from a different sample, right? Remove.


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>();
}
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