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

failing tests for rate limiter policy validation when 'default' or 'disable' used #2283

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/ReverseProxy/Configuration/ConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,25 @@ private async ValueTask ValidateRateLimiterPolicyAsync(IList<Exception> errors,
return;
}

if (string.Equals(RateLimitingConstants.Default, rateLimiterPolicyName, StringComparison.OrdinalIgnoreCase)
|| string.Equals(RateLimitingConstants.Disable, rateLimiterPolicyName, StringComparison.OrdinalIgnoreCase))
{
var policy = await _rateLimiterPolicyProvider.GetPolicyAsync(rateLimiterPolicyName);
if (policy is not null)
{
// We weren't expecting to find a policy with these names.
errors.Add(new ArgumentException($"The application has registered a RateLimiter policy named '{rateLimiterPolicyName}' that conflicts with the reserved RateLimiter policy name used on this route. The registered policy name needs to be changed for this route to function."));
}
return;
}

try
{
var policy = await _rateLimiterPolicyProvider.GetPolicyAsync(rateLimiterPolicyName);

if (policy is null)
{
errors.Add(new ArgumentException($"RateLimiter policy '{rateLimiterPolicyName}' not found for route '{routeId}'."));
return;
}

if (string.Equals(RateLimitingConstants.Default, rateLimiterPolicyName, StringComparison.OrdinalIgnoreCase)
|| string.Equals(RateLimitingConstants.Disable, rateLimiterPolicyName, StringComparison.OrdinalIgnoreCase))
{
errors.Add(new ArgumentException($"The application has registered a RateLimiter policy named '{rateLimiterPolicyName}' that conflicts with the reserved RateLimiter policy name used on this route. The registered policy name needs to be changed for this route to function."));
}
}
catch (Exception ex)
Expand Down
81 changes: 81 additions & 0 deletions test/ReverseProxy.Tests/Configuration/ConfigValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
#if NET7_0_OR_GREATER
using Microsoft.AspNetCore.Builder;
#endif
using Microsoft.AspNetCore.Cors.Infrastructure;
#if NET7_0_OR_GREATER
using Microsoft.AspNetCore.RateLimiting;
#endif
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;
Expand Down Expand Up @@ -710,6 +716,81 @@ public async Task Rejects_ReservedCorsPolicyIsUsed(string corsPolicy)
Assert.Contains(result, err => err.Message.Equals($"The application has registered a CORS policy named '{corsPolicy}' that conflicts with the reserved CORS policy name used on this route. The registered policy name needs to be changed for this route to function."));
}

#if NET7_0_OR_GREATER
[Theory]
[InlineData("Default")]
[InlineData("Disable")]
public async Task Accepts_BuiltInRateLimiterPolicy(string rateLimiterPolicy)
{
var route = new RouteConfig {
RouteId = "route1",
Match = new RouteMatch
{
Hosts = new[] { "localhost" },
},
ClusterId = "cluster1",
RateLimiterPolicy = rateLimiterPolicy
};

var services = CreateServices();
var validator = services.GetRequiredService<IConfigValidator>();

var result = await validator.ValidateRouteAsync(route);

Assert.Empty(result);
}

[Theory]
[InlineData("Default")]
[InlineData("Disable")]
public async Task Reports_BuildInRateLimiterPolicyNameConflict(string rateLimiterPolicy)
{
var route = new RouteConfig
{
RouteId = "route1",
Match = new RouteMatch
{
Hosts = new[] { "localhost" },
},
ClusterId = "cluster1",
RateLimiterPolicy = rateLimiterPolicy
};

var services = CreateServices(s =>
{
s.AddRateLimiter(o => o.AddConcurrencyLimiter(rateLimiterPolicy, c => { }));
});
var validator = services.GetRequiredService<IConfigValidator>();

var result = await validator.ValidateRouteAsync(route);

Assert.NotEmpty(result);
Assert.Contains(result, err => err.Message.Contains($"The application has registered a RateLimiter policy named '{rateLimiterPolicy}' that conflicts with the reserved RateLimiter policy name used on this route. The registered policy name needs to be changed for this route to function."));
}

[Theory]
[InlineData("NotAPolicy")]
public async Task Rejects_InvalidRateLimiterPolicy(string rateLimiterPolicy)
{
var route = new RouteConfig {
RouteId = "route1",
Match = new RouteMatch
{
Hosts = new[] { "localhost" },
},
ClusterId = "cluster1",
RateLimiterPolicy = rateLimiterPolicy };

var services = CreateServices();
var validator = services.GetRequiredService<IConfigValidator>();

var result = await validator.ValidateRouteAsync(route);

Assert.NotEmpty(result);
Assert.Contains(result, err => err.Message.Contains($"RateLimiter policy '{rateLimiterPolicy}' not found"));
}
#endif

[Fact]
public async Task EmptyCluster_Works()
{
Expand Down
Loading