Skip to content

Commit

Permalink
created configuration-driven proxy filter, which uses a handlebar-lik…
Browse files Browse the repository at this point in the history
…e approach to wiring up configuration values - like routes and endpoints - based on values coming out of configuration.
  • Loading branch information
bradygaster committed Jun 23, 2023
1 parent 83fda84 commit 1b2a2b8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
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

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

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

0 comments on commit 1b2a2b8

Please sign in to comment.