Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed May 24, 2023
1 parent 16117e0 commit 7fd690c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Orleans.Hosting;

namespace ManagedCode.Orleans.RateLimiting.Client.Extensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ManagedCode.Orleans.RateLimiting.Client.Middlewares;
using Microsoft.Extensions.DependencyInjection;

namespace ManagedCode.Orleans.RateLimiting.Client.Extensions;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddOrleansRateLimiting(this IServiceCollection collection)
{
//collection.AddTransient<OrleansUserRateLimitingMiddleware>();
//collection.AddTransient<OrleansIpRateLimitingMiddleware>();
return collection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task Invoke(HttpContext httpContext)
return (attribute, postfix);
}

protected ILimiterHolder? TryGetLimiterHolder(HttpContext httpContext, string key, string configurationName)
protected ILimiterHolder? TryGetLimiterHolder(string key, string configurationName)
{
var limiter = _client.GetRateLimiterByConfig(key, configurationName, _services.GetService<IEnumerable<RateLimiterConfig>>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares;

public class OrleansIpRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware
{
public OrleansIpRateLimitingMiddleware(ILogger<OrleansIpRateLimitingMiddleware> logger, RequestDelegate next, IClusterClient client, IServiceProvider services) :
public OrleansIpRateLimitingMiddleware(ILogger<OrleansIpRateLimitingMiddleware> logger, IClusterClient client, IServiceProvider services, RequestDelegate next) :
base(logger, next, client, services)
{
}
Expand All @@ -25,8 +25,8 @@ private bool AddIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder
{
var attribute = TryGetAttribute<IpRateLimiterAttribute>(httpContext);
if (attribute.HasValue)
return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
attribute.Value.postfix!));
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
attribute.Value.attribute.ConfigurationName));

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,54 @@ namespace ManagedCode.Orleans.RateLimiting.Client.Middlewares;

public class OrleansUserRateLimitingMiddleware : OrleansBaseRateLimitingMiddleware
{
protected OrleansUserRateLimitingMiddleware(ILogger logger, RequestDelegate next, IClusterClient client, IServiceProvider services) : base(logger, next, client,
services)
public OrleansUserRateLimitingMiddleware(ILogger<OrleansUserRateLimitingMiddleware> logger, IClusterClient client, IServiceProvider services, RequestDelegate next)
: base(logger, next, client, services)
{
}


protected override void AddLimiters(HttpContext httpContext, GroupLimiterHolder holder)
{
AddAnonymousIpRateLimiter(httpContext, holder);
AddAnonymousRateLimiter(httpContext, holder);

// if user is authenticated add in role limiter
if (!AddInRoleIpRateLimiter(httpContext, holder))
if (!AddInRoleRateLimiter(httpContext, holder))
// if user is not authenticated add authorized limiter
AddAuthorizedIpRateLimiter(httpContext, holder);
AddAuthorizedRateLimiter(httpContext, holder);
}

private bool AddAnonymousIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
private bool AddAnonymousRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
{
if (httpContext.User?.Identity?.IsAuthenticated is not true)
{
var attribute = TryGetAttribute<AnonymousIpRateLimiterAttribute>(httpContext);
if (attribute.HasValue)
return holder.AddLimiter(TryGetLimiterHolder(httpContext, CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
attribute.Value.postfix!));
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), attribute.Value.postfix!),
attribute.Value.attribute.ConfigurationName));
}

return false;
}

private bool AddAuthorizedIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
private bool AddAuthorizedRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
{
if (httpContext.User?.Identity?.IsAuthenticated is true)
{
var attribute = TryGetAttribute<AuthorizedIpRateLimiterAttribute>(httpContext);
if (attribute.HasValue)
return holder.AddLimiter(TryGetLimiterHolder(httpContext,
CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.postfix!), attribute.Value.postfix!));
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name ?? "rate-user-name", attribute.Value.postfix!), attribute.Value.attribute.ConfigurationName));
}

return false;
}

private bool AddInRoleIpRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
private bool AddInRoleRateLimiter(HttpContext httpContext, GroupLimiterHolder holder)
{
var attribute = TryGetAttribute<InRoleIpRateLimiterAttribute>(httpContext);
if (attribute.HasValue)
if (httpContext.User?.Identity?.IsAuthenticated is true && httpContext.User.IsInRole(attribute.Value.attribute.Role))
return holder.AddLimiter(TryGetLimiterHolder(httpContext,
CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!),
attribute.Value.postfix!));
return holder.AddLimiter(TryGetLimiterHolder(CreateKey(httpContext.Request.GetClientIpAddress(), httpContext.User.Identity.Name!, attribute.Value.attribute.Role, attribute.Value.postfix!),
attribute.Value.attribute.ConfigurationName));

return false;
}
Expand Down
13 changes: 13 additions & 0 deletions ManagedCode.Orleans.RateLimiting.Tests/SignalRTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ public SignalRTests(TestClusterApplication testApp, ITestOutputHelper outputHelp
[Fact]
public async Task Some()
{
try
{
var anonymousHub11 = _testApp.CreateSignalRClient(nameof(TestHub));
await anonymousHub11.StartAsync();
anonymousHub11.State.Should().Be(HubConnectionState.Connected);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}

var anonymousHub = _testApp.CreateSignalRClient(nameof(TestHub));
await anonymousHub.StartAsync();
anonymousHub.State.Should().Be(HubConnectionState.Connected);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TestController : ControllerBase
[HttpGet("authorize")]
public async Task<ActionResult<string>> Authorize()
{
await Task.Delay(300);
await Task.Delay(500);
return "Authorize";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ManagedCode.Orleans.RateLimiting.Core.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ManagedCode.Orleans.RateLimiting.Tests.TestApp;

Expand All @@ -14,11 +15,14 @@ public static void Main(string[] args)

builder.Services.AddControllers();
builder.Services.AddSignalR();
builder.Services.AddLogging(log=>log.AddSimpleConsole());

builder.Services.AddOrleansRateLimiting();

builder.Services.AddOrleansRateLimiterOptions("ip", new FixedWindowRateLimiterOptions
{
QueueLimit = 5,
PermitLimit = 10,
PermitLimit = 5,
Window = TimeSpan.FromSeconds(1)
});

Expand Down Expand Up @@ -46,7 +50,7 @@ public static void Main(string[] args)
app.UseOrleansIpRateLimiting();
app.UseOrleansUserRateLimiting();

app.UseRateLimiter();
//app.UseRateLimiter();

app.Run();
}
Expand Down

0 comments on commit 7fd690c

Please sign in to comment.