Remove Ocelot specific Middleware to make Ocelot more compatible with kestrel middleware and get ready for YARP

This commit is contained in:
Tom Pallister
2020-05-23 15:48:51 +01:00
committed by GitHub
parent 99a15d8668
commit fe3e8bd23a
214 changed files with 9574 additions and 9919 deletions

View File

@ -1,36 +1,37 @@
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Middleware;
using Ocelot.Request.Middleware;
using Ocelot.Responses;
using Ocelot.Security.IPSecurity;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Security
namespace Ocelot.UnitTests.Security
{
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.DownstreamRouteFinder.Middleware;
using Ocelot.Middleware;
using Ocelot.Request.Middleware;
using Ocelot.Responses;
using Ocelot.Security.IPSecurity;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using TestStack.BDDfy;
using Xunit;
public class IPSecurityPolicyTests
{
private readonly DownstreamContext _downstreamContext;
private readonly DownstreamReRouteBuilder _downstreamReRouteBuilder;
private readonly IPSecurityPolicy _ipSecurityPolicy;
private Response response;
private HttpContext _httpContext;
public IPSecurityPolicyTests()
{
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
_httpContext = new DefaultHttpContext();
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com")));
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
_downstreamReRouteBuilder = new DownstreamReRouteBuilder();
_ipSecurityPolicy = new IPSecurityPolicy();
}
[Fact]
private void should_No_blocked_Ip_and_allowed_Ip()
public void should_No_blocked_Ip_and_allowed_Ip()
{
this.Given(x => x.GivenSetDownstreamReRoute())
.When(x => x.WhenTheSecurityPolicy())
@ -39,9 +40,9 @@ namespace Ocelot.UnitTests.Security
}
[Fact]
private void should_blockedIp_clientIp_block()
public void should_blockedIp_clientIp_block()
{
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
this.Given(x => x.GivenSetBlockedIP())
.Given(x => x.GivenSetDownstreamReRoute())
.When(x => x.WhenTheSecurityPolicy())
@ -50,9 +51,9 @@ namespace Ocelot.UnitTests.Security
}
[Fact]
private void should_blockedIp_clientIp_Not_block()
public void should_blockedIp_clientIp_Not_block()
{
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
this.Given(x => x.GivenSetBlockedIP())
.Given(x => x.GivenSetDownstreamReRoute())
.When(x => x.WhenTheSecurityPolicy())
@ -61,9 +62,9 @@ namespace Ocelot.UnitTests.Security
}
[Fact]
private void should_allowedIp_clientIp_block()
public void should_allowedIp_clientIp_block()
{
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
this.Given(x => x.GivenSetAllowedIP())
.Given(x => x.GivenSetDownstreamReRoute())
.When(x => x.WhenTheSecurityPolicy())
@ -72,9 +73,9 @@ namespace Ocelot.UnitTests.Security
}
[Fact]
private void should_allowedIp_clientIp_Not_block()
public void should_allowedIp_clientIp_Not_block()
{
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
_httpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
this.Given(x => x.GivenSetAllowedIP())
.Given(x => x.GivenSetDownstreamReRoute())
.When(x => x.WhenTheSecurityPolicy())
@ -94,12 +95,12 @@ namespace Ocelot.UnitTests.Security
private void GivenSetDownstreamReRoute()
{
_downstreamContext.DownstreamReRoute = _downstreamReRouteBuilder.Build();
_httpContext.Items.UpsertDownstreamReRoute(_downstreamReRouteBuilder.Build());
}
private void WhenTheSecurityPolicy()
{
response = this._ipSecurityPolicy.Security(_downstreamContext).GetAwaiter().GetResult();
response = _ipSecurityPolicy.Security(_httpContext.Items.DownstreamReRoute(), _httpContext).GetAwaiter().GetResult();
}
private void ThenSecurityPassing()

View File

@ -1,106 +1,110 @@
using Microsoft.AspNetCore.Http;
using Moq;
using Ocelot.Errors;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Request.Middleware;
using Ocelot.Responses;
using Ocelot.Security;
using Ocelot.Security.Middleware;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Security
using Microsoft.AspNetCore.Http;
using Moq;
using Ocelot.Errors;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Request.Middleware;
using Ocelot.Responses;
using Ocelot.Security;
using Ocelot.Security.Middleware;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Security
{
public class SecurityMiddlewareTests
{
private List<Mock<ISecurityPolicy>> _securityPolicyList;
private Mock<IOcelotLoggerFactory> _loggerFactory;
private Mock<IOcelotLogger> _logger;
private readonly SecurityMiddleware _middleware;
private readonly DownstreamContext _downstreamContext;
private readonly OcelotRequestDelegate _next;
using Ocelot.DownstreamRouteFinder.Middleware;
using Ocelot.Infrastructure.RequestData;
using Shouldly;
public SecurityMiddlewareTests()
public class SecurityMiddlewareTests
{
private List<Mock<ISecurityPolicy>> _securityPolicyList;
private Mock<IOcelotLoggerFactory> _loggerFactory;
private Mock<IOcelotLogger> _logger;
private readonly SecurityMiddleware _middleware;
private readonly RequestDelegate _next;
private HttpContext _httpContext;
public SecurityMiddlewareTests()
{
_httpContext = new DefaultHttpContext();
_loggerFactory = new Mock<IOcelotLoggerFactory>();
_logger = new Mock<IOcelotLogger>();
_loggerFactory.Setup(x => x.CreateLogger<SecurityMiddleware>()).Returns(_logger.Object);
_securityPolicyList = new List<Mock<ISecurityPolicy>>();
_securityPolicyList.Add(new Mock<ISecurityPolicy>());
_securityPolicyList.Add(new Mock<ISecurityPolicy>());
_next = context =>
{
return Task.CompletedTask;
};
_middleware = new SecurityMiddleware(_next, _loggerFactory.Object, _securityPolicyList.Select(f => f.Object).ToList());
_httpContext.Items.UpsertDownstreamRequest(new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com")));
}
[Fact]
public void should_legal_request()
{
this.Given(x => x.GivenPassingSecurityVerification())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheRequestIsPassingSecurity())
.BDDfy();
}
[Fact]
public void should_verification_failed_request()
{
this.Given(x => x.GivenNotPassingSecurityVerification())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheRequestIsNotPassingSecurity())
.BDDfy();
}
private void GivenPassingSecurityVerification()
{
foreach (var item in _securityPolicyList)
{
Response response = new OkResponse();
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
}
}
private void GivenNotPassingSecurityVerification()
{
for (int i = 0; i < _securityPolicyList.Count; i++)
{
Mock<ISecurityPolicy> item = _securityPolicyList[i];
if (i == 0)
{
Error error = new UnauthenticatedError($"Not passing security verification");
Response response = new ErrorResponse(error);
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
}
else
{
Response response = new OkResponse();
item.Setup(x => x.Security(_httpContext.Items.DownstreamReRoute(), _httpContext)).Returns(Task.FromResult(response));
}
}
}
private void WhenICallTheMiddleware()
{
_middleware.Invoke(_httpContext).GetAwaiter().GetResult();
}
private void ThenTheRequestIsPassingSecurity()
{
_loggerFactory = new Mock<IOcelotLoggerFactory>();
_logger = new Mock<IOcelotLogger>();
_loggerFactory.Setup(x => x.CreateLogger<SecurityMiddleware>()).Returns(_logger.Object);
_securityPolicyList = new List<Mock<ISecurityPolicy>>();
_securityPolicyList.Add(new Mock<ISecurityPolicy>());
_securityPolicyList.Add(new Mock<ISecurityPolicy>());
_next = context =>
{
return Task.CompletedTask;
};
_middleware = new SecurityMiddleware(_loggerFactory.Object, _securityPolicyList.Select(f => f.Object).ToList(), _next);
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
_downstreamContext.DownstreamRequest = new DownstreamRequest(new HttpRequestMessage(HttpMethod.Get, "http://test.com"));
}
[Fact]
public void should_legal_request()
_httpContext.Items.Errors().Count.ShouldBe(0);
}
private void ThenTheRequestIsNotPassingSecurity()
{
this.Given(x => x.GivenPassingSecurityVerification())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheRequestIsPassingSecurity())
.BDDfy();
}
[Fact]
public void should_verification_failed_request()
{
this.Given(x => x.GivenNotPassingSecurityVerification())
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheRequestIsNotPassingSecurity())
.BDDfy();
}
private void GivenPassingSecurityVerification()
{
foreach (var item in _securityPolicyList)
{
Response response = new OkResponse();
item.Setup(x => x.Security(_downstreamContext)).Returns(Task.FromResult(response));
}
}
private void GivenNotPassingSecurityVerification()
{
for (int i = 0; i < _securityPolicyList.Count; i++)
{
Mock<ISecurityPolicy> item = _securityPolicyList[i];
if (i == 0)
{
Error error = new UnauthenticatedError($"Not passing security verification");
Response response = new ErrorResponse(error);
item.Setup(x => x.Security(_downstreamContext)).Returns(Task.FromResult(response));
}
else
{
Response response = new OkResponse();
item.Setup(x => x.Security(_downstreamContext)).Returns(Task.FromResult(response));
}
}
}
private void WhenICallTheMiddleware()
{
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
}
private void ThenTheRequestIsPassingSecurity()
{
Assert.False(_downstreamContext.IsError);
}
private void ThenTheRequestIsNotPassingSecurity()
{
Assert.True(_downstreamContext.IsError);
}
}
}
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
}
}
}