mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 18:22:49 +08:00
add a security module (#628) (#629)
* Add security mechanisms, IP whitelists, blacklists, and extended security interfaces. * Optimized configuration name * Fix IP Security Bug * Repair Security Protection Module Bug * Add security module unit test * Optimize log prompts
This commit is contained in:
parent
aa14b2f877
commit
4a8f4c2e03
@ -38,7 +38,7 @@ namespace Ocelot.Configuration.Builder
|
|||||||
private List<AddHeader> _addHeadersToDownstream;
|
private List<AddHeader> _addHeadersToDownstream;
|
||||||
private List<AddHeader> _addHeadersToUpstream;
|
private List<AddHeader> _addHeadersToUpstream;
|
||||||
private bool _dangerousAcceptAnyServerCertificateValidator;
|
private bool _dangerousAcceptAnyServerCertificateValidator;
|
||||||
|
private SecurityOptions _securityOptions;
|
||||||
public DownstreamReRouteBuilder()
|
public DownstreamReRouteBuilder()
|
||||||
{
|
{
|
||||||
_downstreamAddresses = new List<DownstreamHostAndPort>();
|
_downstreamAddresses = new List<DownstreamHostAndPort>();
|
||||||
@ -227,6 +227,12 @@ namespace Ocelot.Configuration.Builder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DownstreamReRouteBuilder WithSecurityOptions(SecurityOptions securityOptions)
|
||||||
|
{
|
||||||
|
_securityOptions = securityOptions;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public DownstreamReRoute Build()
|
public DownstreamReRoute Build()
|
||||||
{
|
{
|
||||||
return new DownstreamReRoute(
|
return new DownstreamReRoute(
|
||||||
@ -258,7 +264,8 @@ namespace Ocelot.Configuration.Builder
|
|||||||
_delegatingHandlers,
|
_delegatingHandlers,
|
||||||
_addHeadersToDownstream,
|
_addHeadersToDownstream,
|
||||||
_addHeadersToUpstream,
|
_addHeadersToUpstream,
|
||||||
_dangerousAcceptAnyServerCertificateValidator);
|
_dangerousAcceptAnyServerCertificateValidator,
|
||||||
|
_securityOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/Ocelot/Configuration/Creator/ISecurityOptionsCreator.cs
Normal file
12
src/Ocelot/Configuration/Creator/ISecurityOptionsCreator.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Ocelot.Configuration.File;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ocelot.Configuration.Creator
|
||||||
|
{
|
||||||
|
public interface ISecurityOptionsCreator
|
||||||
|
{
|
||||||
|
SecurityOptions Create(FileSecurityOptions securityOptions);
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
private readonly IHeaderFindAndReplaceCreator _headerFAndRCreator;
|
private readonly IHeaderFindAndReplaceCreator _headerFAndRCreator;
|
||||||
private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
|
private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
|
||||||
private readonly IReRouteKeyCreator _reRouteKeyCreator;
|
private readonly IReRouteKeyCreator _reRouteKeyCreator;
|
||||||
|
private readonly ISecurityOptionsCreator _securityOptionsCreator;
|
||||||
|
|
||||||
public ReRoutesCreator(
|
public ReRoutesCreator(
|
||||||
IClaimsToThingCreator claimsToThingCreator,
|
IClaimsToThingCreator claimsToThingCreator,
|
||||||
@ -35,7 +36,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
IHeaderFindAndReplaceCreator headerFAndRCreator,
|
IHeaderFindAndReplaceCreator headerFAndRCreator,
|
||||||
IDownstreamAddressesCreator downstreamAddressesCreator,
|
IDownstreamAddressesCreator downstreamAddressesCreator,
|
||||||
ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
|
ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
|
||||||
IReRouteKeyCreator reRouteKeyCreator
|
IReRouteKeyCreator reRouteKeyCreator,
|
||||||
|
ISecurityOptionsCreator securityOptionsCreator
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_reRouteKeyCreator = reRouteKeyCreator;
|
_reRouteKeyCreator = reRouteKeyCreator;
|
||||||
@ -52,6 +54,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
||||||
_httpHandlerOptionsCreator = httpHandlerOptionsCreator;
|
_httpHandlerOptionsCreator = httpHandlerOptionsCreator;
|
||||||
_loadBalancerOptionsCreator = loadBalancerOptionsCreator;
|
_loadBalancerOptionsCreator = loadBalancerOptionsCreator;
|
||||||
|
_securityOptionsCreator = securityOptionsCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ReRoute> Create(FileConfiguration fileConfiguration)
|
public List<ReRoute> Create(FileConfiguration fileConfiguration)
|
||||||
@ -97,6 +100,8 @@ namespace Ocelot.Configuration.Creator
|
|||||||
|
|
||||||
var lbOptions = _loadBalancerOptionsCreator.Create(fileReRoute.LoadBalancerOptions);
|
var lbOptions = _loadBalancerOptionsCreator.Create(fileReRoute.LoadBalancerOptions);
|
||||||
|
|
||||||
|
var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions);
|
||||||
|
|
||||||
var reRoute = new DownstreamReRouteBuilder()
|
var reRoute = new DownstreamReRouteBuilder()
|
||||||
.WithKey(fileReRoute.Key)
|
.WithKey(fileReRoute.Key)
|
||||||
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
|
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
|
||||||
@ -128,6 +133,7 @@ namespace Ocelot.Configuration.Creator
|
|||||||
.WithAddHeadersToDownstream(hAndRs.AddHeadersToDownstream)
|
.WithAddHeadersToDownstream(hAndRs.AddHeadersToDownstream)
|
||||||
.WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
|
.WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
|
||||||
.WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
|
.WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
|
||||||
|
.WithSecurityOptions(securityOptions)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
return reRoute;
|
return reRoute;
|
||||||
|
15
src/Ocelot/Configuration/Creator/SecurityOptionsCreator.cs
Normal file
15
src/Ocelot/Configuration/Creator/SecurityOptionsCreator.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using Ocelot.Configuration.File;
|
||||||
|
|
||||||
|
namespace Ocelot.Configuration.Creator
|
||||||
|
{
|
||||||
|
public class SecurityOptionsCreator : ISecurityOptionsCreator
|
||||||
|
{
|
||||||
|
public SecurityOptions Create(FileSecurityOptions securityOptions)
|
||||||
|
{
|
||||||
|
return new SecurityOptions(securityOptions.IPAllowedList, securityOptions.IPBlockedList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -35,7 +35,8 @@ namespace Ocelot.Configuration
|
|||||||
List<string> delegatingHandlers,
|
List<string> delegatingHandlers,
|
||||||
List<AddHeader> addHeadersToDownstream,
|
List<AddHeader> addHeadersToDownstream,
|
||||||
List<AddHeader> addHeadersToUpstream,
|
List<AddHeader> addHeadersToUpstream,
|
||||||
bool dangerousAcceptAnyServerCertificateValidator)
|
bool dangerousAcceptAnyServerCertificateValidator,
|
||||||
|
SecurityOptions securityOptions)
|
||||||
{
|
{
|
||||||
DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
|
DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
|
||||||
AddHeadersToDownstream = addHeadersToDownstream;
|
AddHeadersToDownstream = addHeadersToDownstream;
|
||||||
@ -66,6 +67,7 @@ namespace Ocelot.Configuration
|
|||||||
DownstreamPathTemplate = downstreamPathTemplate;
|
DownstreamPathTemplate = downstreamPathTemplate;
|
||||||
LoadBalancerKey = loadBalancerKey;
|
LoadBalancerKey = loadBalancerKey;
|
||||||
AddHeadersToUpstream = addHeadersToUpstream;
|
AddHeadersToUpstream = addHeadersToUpstream;
|
||||||
|
SecurityOptions = securityOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Key { get; }
|
public string Key { get; }
|
||||||
@ -97,5 +99,6 @@ namespace Ocelot.Configuration
|
|||||||
public List<AddHeader> AddHeadersToDownstream { get; }
|
public List<AddHeader> AddHeadersToDownstream { get; }
|
||||||
public List<AddHeader> AddHeadersToUpstream { get; }
|
public List<AddHeader> AddHeadersToUpstream { get; }
|
||||||
public bool DangerousAcceptAnyServerCertificateValidator { get; }
|
public bool DangerousAcceptAnyServerCertificateValidator { get; }
|
||||||
|
public SecurityOptions SecurityOptions { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ namespace Ocelot.Configuration.File
|
|||||||
DownstreamHostAndPorts = new List<FileHostAndPort>();
|
DownstreamHostAndPorts = new List<FileHostAndPort>();
|
||||||
DelegatingHandlers = new List<string>();
|
DelegatingHandlers = new List<string>();
|
||||||
LoadBalancerOptions = new FileLoadBalancerOptions();
|
LoadBalancerOptions = new FileLoadBalancerOptions();
|
||||||
|
SecurityOptions = new FileSecurityOptions();
|
||||||
Priority = 1;
|
Priority = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,5 +51,6 @@ namespace Ocelot.Configuration.File
|
|||||||
public int Priority { get;set; }
|
public int Priority { get;set; }
|
||||||
public int Timeout { get; set; }
|
public int Timeout { get; set; }
|
||||||
public bool DangerousAcceptAnyServerCertificateValidator { get; set; }
|
public bool DangerousAcceptAnyServerCertificateValidator { get; set; }
|
||||||
|
public FileSecurityOptions SecurityOptions { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
src/Ocelot/Configuration/File/FileSecurityOptions.cs
Normal file
19
src/Ocelot/Configuration/File/FileSecurityOptions.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ocelot.Configuration.File
|
||||||
|
{
|
||||||
|
public class FileSecurityOptions
|
||||||
|
{
|
||||||
|
public FileSecurityOptions()
|
||||||
|
{
|
||||||
|
IPAllowedList = new List<string>();
|
||||||
|
IPBlockedList = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> IPAllowedList { get; set; }
|
||||||
|
|
||||||
|
public List<string> IPBlockedList { get; set; }
|
||||||
|
}
|
||||||
|
}
|
19
src/Ocelot/Configuration/SecurityOptions.cs
Normal file
19
src/Ocelot/Configuration/SecurityOptions.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ocelot.Configuration
|
||||||
|
{
|
||||||
|
public class SecurityOptions
|
||||||
|
{
|
||||||
|
public SecurityOptions(List<string> allowedList, List<string> blockedList)
|
||||||
|
{
|
||||||
|
this.IPAllowedList = allowedList;
|
||||||
|
this.IPBlockedList = blockedList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> IPAllowedList { get; private set; }
|
||||||
|
|
||||||
|
public List<string> IPBlockedList { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
@ -35,6 +35,8 @@ namespace Ocelot.DependencyInjection
|
|||||||
using Ocelot.Infrastructure;
|
using Ocelot.Infrastructure;
|
||||||
using Ocelot.Middleware.Multiplexer;
|
using Ocelot.Middleware.Multiplexer;
|
||||||
using Ocelot.Request.Creator;
|
using Ocelot.Request.Creator;
|
||||||
|
using Ocelot.Security.IPSecurity;
|
||||||
|
using Ocelot.Security;
|
||||||
|
|
||||||
public class OcelotBuilder : IOcelotBuilder
|
public class OcelotBuilder : IOcelotBuilder
|
||||||
{
|
{
|
||||||
@ -125,6 +127,9 @@ namespace Ocelot.DependencyInjection
|
|||||||
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
|
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
|
||||||
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
|
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
|
||||||
|
|
||||||
|
//add security
|
||||||
|
this.AddSecurity();
|
||||||
|
|
||||||
//add asp.net services..
|
//add asp.net services..
|
||||||
var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly;
|
var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly;
|
||||||
|
|
||||||
@ -153,6 +158,12 @@ namespace Ocelot.DependencyInjection
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AddSecurity()
|
||||||
|
{
|
||||||
|
Services.TryAddSingleton<ISecurityOptionsCreator, SecurityOptionsCreator>();
|
||||||
|
Services.TryAddSingleton<ISecurityPolicy, IPSecurityPolicy>();
|
||||||
|
}
|
||||||
|
|
||||||
public IOcelotBuilder AddDelegatingHandler<THandler>(bool global = false)
|
public IOcelotBuilder AddDelegatingHandler<THandler>(bool global = false)
|
||||||
where THandler : DelegatingHandler
|
where THandler : DelegatingHandler
|
||||||
{
|
{
|
||||||
|
@ -15,6 +15,7 @@ using Ocelot.Request.Middleware;
|
|||||||
using Ocelot.Requester.Middleware;
|
using Ocelot.Requester.Middleware;
|
||||||
using Ocelot.RequestId.Middleware;
|
using Ocelot.RequestId.Middleware;
|
||||||
using Ocelot.Responder.Middleware;
|
using Ocelot.Responder.Middleware;
|
||||||
|
using Ocelot.Security.Middleware;
|
||||||
using Ocelot.WebSockets.Middleware;
|
using Ocelot.WebSockets.Middleware;
|
||||||
|
|
||||||
namespace Ocelot.Middleware.Pipeline
|
namespace Ocelot.Middleware.Pipeline
|
||||||
@ -48,6 +49,9 @@ namespace Ocelot.Middleware.Pipeline
|
|||||||
// Then we get the downstream route information
|
// Then we get the downstream route information
|
||||||
builder.UseDownstreamRouteFinderMiddleware();
|
builder.UseDownstreamRouteFinderMiddleware();
|
||||||
|
|
||||||
|
// This security module, IP whitelist blacklist, extended security mechanism
|
||||||
|
builder.UseSecurityMiddleware();
|
||||||
|
|
||||||
//Expand other branch pipes
|
//Expand other branch pipes
|
||||||
if (pipelineConfiguration.MapWhenOcelotPipeline != null)
|
if (pipelineConfiguration.MapWhenOcelotPipeline != null)
|
||||||
{
|
{
|
||||||
|
44
src/Ocelot/Security/IPSecurity/IPSecurityPolicy.cs
Normal file
44
src/Ocelot/Security/IPSecurity/IPSecurityPolicy.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
|
||||||
|
namespace Ocelot.Security.IPSecurity
|
||||||
|
{
|
||||||
|
public class IPSecurityPolicy : ISecurityPolicy
|
||||||
|
{
|
||||||
|
public async Task<Response> Security(DownstreamContext context)
|
||||||
|
{
|
||||||
|
IPAddress clientIp = context.HttpContext.Connection.RemoteIpAddress;
|
||||||
|
SecurityOptions securityOptions = context.DownstreamReRoute.SecurityOptions;
|
||||||
|
if (securityOptions == null)
|
||||||
|
{
|
||||||
|
return new OkResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (securityOptions.IPBlockedList != null)
|
||||||
|
{
|
||||||
|
if (securityOptions.IPBlockedList.Exists(f => f == clientIp.ToString()))
|
||||||
|
{
|
||||||
|
var error = new UnauthenticatedError($" This request rejects access to {clientIp.ToString()} IP");
|
||||||
|
return new ErrorResponse(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (securityOptions.IPAllowedList != null && securityOptions.IPAllowedList.Count > 0)
|
||||||
|
{
|
||||||
|
if (!securityOptions.IPAllowedList.Exists(f => f == clientIp.ToString()))
|
||||||
|
{
|
||||||
|
var error = new UnauthenticatedError($"{clientIp.ToString()} does not allow access, the request is invalid");
|
||||||
|
return new ErrorResponse(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await Task.FromResult(new OkResponse());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/Ocelot/Security/ISecurityPolicy.cs
Normal file
14
src/Ocelot/Security/ISecurityPolicy.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using Ocelot.Middleware;
|
||||||
|
using Ocelot.Responses;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ocelot.Security
|
||||||
|
{
|
||||||
|
public interface ISecurityPolicy
|
||||||
|
{
|
||||||
|
Task<Response> Security(DownstreamContext context);
|
||||||
|
}
|
||||||
|
}
|
43
src/Ocelot/Security/Middleware/SecurityMiddleware.cs
Normal file
43
src/Ocelot/Security/Middleware/SecurityMiddleware.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using Ocelot.Logging;
|
||||||
|
using Ocelot.Middleware;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ocelot.Security.Middleware
|
||||||
|
{
|
||||||
|
public class SecurityMiddleware : OcelotMiddleware
|
||||||
|
{
|
||||||
|
private readonly OcelotRequestDelegate _next;
|
||||||
|
private readonly IOcelotLogger _logger;
|
||||||
|
private readonly IEnumerable<ISecurityPolicy> _securityPolicies;
|
||||||
|
public SecurityMiddleware(IOcelotLoggerFactory loggerFactory,
|
||||||
|
IEnumerable<ISecurityPolicy> securityPolicies,
|
||||||
|
OcelotRequestDelegate next)
|
||||||
|
: base(loggerFactory.CreateLogger<SecurityMiddleware>())
|
||||||
|
{
|
||||||
|
_logger = loggerFactory.CreateLogger<SecurityMiddleware>();
|
||||||
|
_securityPolicies = securityPolicies;
|
||||||
|
_next = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Invoke(DownstreamContext context)
|
||||||
|
{
|
||||||
|
if (_securityPolicies != null)
|
||||||
|
{
|
||||||
|
foreach (var policie in _securityPolicies)
|
||||||
|
{
|
||||||
|
var result = await policie.Security(context);
|
||||||
|
if (!result.IsError)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.SetPipelineError(context, result.Errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await _next.Invoke(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using Ocelot.Middleware.Pipeline;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ocelot.Security.Middleware
|
||||||
|
{
|
||||||
|
public static class SecurityMiddlewareExtensions
|
||||||
|
{
|
||||||
|
public static IOcelotPipelineBuilder UseSecurityMiddleware(this IOcelotPipelineBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseMiddleware<SecurityMiddleware>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,7 @@
|
|||||||
private Mock<IDownstreamAddressesCreator> _daCreator;
|
private Mock<IDownstreamAddressesCreator> _daCreator;
|
||||||
private Mock<ILoadBalancerOptionsCreator> _lboCreator;
|
private Mock<ILoadBalancerOptionsCreator> _lboCreator;
|
||||||
private Mock<IReRouteKeyCreator> _rrkCreator;
|
private Mock<IReRouteKeyCreator> _rrkCreator;
|
||||||
|
private Mock<ISecurityOptionsCreator> _soCreator;
|
||||||
private FileConfiguration _fileConfig;
|
private FileConfiguration _fileConfig;
|
||||||
private ReRouteOptions _rro;
|
private ReRouteOptions _rro;
|
||||||
private string _requestId;
|
private string _requestId;
|
||||||
@ -45,6 +46,7 @@
|
|||||||
private List<DownstreamHostAndPort> _dhp;
|
private List<DownstreamHostAndPort> _dhp;
|
||||||
private LoadBalancerOptions _lbo;
|
private LoadBalancerOptions _lbo;
|
||||||
private List<ReRoute> _result;
|
private List<ReRoute> _result;
|
||||||
|
private SecurityOptions _securityOptions;
|
||||||
|
|
||||||
public ReRoutesCreatorTests()
|
public ReRoutesCreatorTests()
|
||||||
{
|
{
|
||||||
@ -61,6 +63,7 @@
|
|||||||
_daCreator = new Mock<IDownstreamAddressesCreator>();
|
_daCreator = new Mock<IDownstreamAddressesCreator>();
|
||||||
_lboCreator = new Mock<ILoadBalancerOptionsCreator>();
|
_lboCreator = new Mock<ILoadBalancerOptionsCreator>();
|
||||||
_rrkCreator = new Mock<IReRouteKeyCreator>();
|
_rrkCreator = new Mock<IReRouteKeyCreator>();
|
||||||
|
_soCreator = new Mock<ISecurityOptionsCreator>();
|
||||||
|
|
||||||
_creator = new ReRoutesCreator(
|
_creator = new ReRoutesCreator(
|
||||||
_cthCreator.Object,
|
_cthCreator.Object,
|
||||||
@ -75,7 +78,8 @@
|
|||||||
_hfarCreator.Object,
|
_hfarCreator.Object,
|
||||||
_daCreator.Object,
|
_daCreator.Object,
|
||||||
_lboCreator.Object,
|
_lboCreator.Object,
|
||||||
_rrkCreator.Object
|
_rrkCreator.Object,
|
||||||
|
_soCreator.Object
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,6 +270,7 @@
|
|||||||
_hfarCreator.Verify(x => x.Create(fileReRoute), Times.Once);
|
_hfarCreator.Verify(x => x.Create(fileReRoute), Times.Once);
|
||||||
_daCreator.Verify(x => x.Create(fileReRoute), Times.Once);
|
_daCreator.Verify(x => x.Create(fileReRoute), Times.Once);
|
||||||
_lboCreator.Verify(x => x.Create(fileReRoute.LoadBalancerOptions), Times.Once);
|
_lboCreator.Verify(x => x.Create(fileReRoute.LoadBalancerOptions), Times.Once);
|
||||||
|
_soCreator.Verify(x => x.Create(fileReRoute.SecurityOptions), Times.Once);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
using Ocelot.Configuration;
|
||||||
|
using Ocelot.Configuration.Creator;
|
||||||
|
using Ocelot.Configuration.File;
|
||||||
|
using Shouldly;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Configuration
|
||||||
|
{
|
||||||
|
public class SecurityOptionsCreatorTests
|
||||||
|
{
|
||||||
|
private FileReRoute _fileReRoute;
|
||||||
|
private FileGlobalConfiguration _fileGlobalConfig;
|
||||||
|
private SecurityOptions _result;
|
||||||
|
private ISecurityOptionsCreator _creator;
|
||||||
|
|
||||||
|
public SecurityOptionsCreatorTests()
|
||||||
|
{
|
||||||
|
_creator = new SecurityOptionsCreator();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_create_security_config()
|
||||||
|
{
|
||||||
|
var ipAllowedList = new List<string>() { "127.0.0.1", "192.168.1.1" };
|
||||||
|
var ipBlockedList = new List<string>() { "127.0.0.1", "192.168.1.1" };
|
||||||
|
var fileReRoute = new FileReRoute
|
||||||
|
{
|
||||||
|
SecurityOptions = new FileSecurityOptions()
|
||||||
|
{
|
||||||
|
IPAllowedList = ipAllowedList,
|
||||||
|
IPBlockedList = ipBlockedList
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var expected = new SecurityOptions(ipAllowedList, ipBlockedList);
|
||||||
|
|
||||||
|
this.Given(x => x.GivenThe(fileReRoute))
|
||||||
|
.When(x => x.WhenICreate())
|
||||||
|
.Then(x => x.ThenTheResultIs(expected))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenThe(FileReRoute reRoute)
|
||||||
|
{
|
||||||
|
_fileReRoute = reRoute;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void WhenICreate()
|
||||||
|
{
|
||||||
|
_result = _creator.Create(_fileReRoute.SecurityOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheResultIs(SecurityOptions expected)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < expected.IPAllowedList.Count; i++)
|
||||||
|
{
|
||||||
|
_result.IPAllowedList[i].ShouldBe(expected.IPAllowedList[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < expected.IPBlockedList.Count; i++)
|
||||||
|
{
|
||||||
|
_result.IPBlockedList[i].ShouldBe(expected.IPBlockedList[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
117
test/Ocelot.UnitTests/Security/IPSecurityPolicyTests.cs
Normal file
117
test/Ocelot.UnitTests/Security/IPSecurityPolicyTests.cs
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
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;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Ocelot.UnitTests.Security
|
||||||
|
{
|
||||||
|
public class IPSecurityPolicyTests
|
||||||
|
{
|
||||||
|
private readonly DownstreamContext _downstreamContext;
|
||||||
|
private readonly DownstreamReRouteBuilder _downstreamReRouteBuilder;
|
||||||
|
private readonly IPSecurityPolicy _ipSecurityPolicy;
|
||||||
|
private Response response;
|
||||||
|
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];
|
||||||
|
_downstreamReRouteBuilder = new DownstreamReRouteBuilder();
|
||||||
|
_ipSecurityPolicy = new IPSecurityPolicy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
private void should_No_blocked_Ip_and_allowed_Ip()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenSetDownstreamReRoute())
|
||||||
|
.When(x => x.WhenTheSecurityPolicy())
|
||||||
|
.Then(x => x.ThenSecurityPassing())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
private void should_blockedIp_clientIp_block()
|
||||||
|
{
|
||||||
|
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
|
||||||
|
this.Given(x => x.GivenSetBlockedIP())
|
||||||
|
.Given(x => x.GivenSetDownstreamReRoute())
|
||||||
|
.When(x => x.WhenTheSecurityPolicy())
|
||||||
|
.Then(x => x.ThenNotSecurityPassing())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
private void should_blockedIp_clientIp_Not_block()
|
||||||
|
{
|
||||||
|
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
|
||||||
|
this.Given(x => x.GivenSetBlockedIP())
|
||||||
|
.Given(x => x.GivenSetDownstreamReRoute())
|
||||||
|
.When(x => x.WhenTheSecurityPolicy())
|
||||||
|
.Then(x => x.ThenSecurityPassing())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
private void should_allowedIp_clientIp_block()
|
||||||
|
{
|
||||||
|
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.1")[0];
|
||||||
|
this.Given(x => x.GivenSetAllowedIP())
|
||||||
|
.Given(x => x.GivenSetDownstreamReRoute())
|
||||||
|
.When(x => x.WhenTheSecurityPolicy())
|
||||||
|
.Then(x => x.ThenSecurityPassing())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
private void should_allowedIp_clientIp_Not_block()
|
||||||
|
{
|
||||||
|
_downstreamContext.HttpContext.Connection.RemoteIpAddress = Dns.GetHostAddresses("192.168.1.2")[0];
|
||||||
|
this.Given(x => x.GivenSetAllowedIP())
|
||||||
|
.Given(x => x.GivenSetDownstreamReRoute())
|
||||||
|
.When(x => x.WhenTheSecurityPolicy())
|
||||||
|
.Then(x => x.ThenNotSecurityPassing())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenSetAllowedIP()
|
||||||
|
{
|
||||||
|
_downstreamReRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string> { "192.168.1.1" }, new List<string>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenSetBlockedIP()
|
||||||
|
{
|
||||||
|
_downstreamReRouteBuilder.WithSecurityOptions(new SecurityOptions(new List<string>(), new List<string> { "192.168.1.1" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenSetDownstreamReRoute()
|
||||||
|
{
|
||||||
|
_downstreamContext.DownstreamReRoute = _downstreamReRouteBuilder.Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenTheSecurityPolicy()
|
||||||
|
{
|
||||||
|
response = this._ipSecurityPolicy.Security(_downstreamContext).GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenSecurityPassing()
|
||||||
|
{
|
||||||
|
Assert.False(response.IsError);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenNotSecurityPassing()
|
||||||
|
{
|
||||||
|
Assert.True(response.IsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
108
test/Ocelot.UnitTests/Security/SecurityMiddlewareTests.cs
Normal file
108
test/Ocelot.UnitTests/Security/SecurityMiddlewareTests.cs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
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.IPSecurity;
|
||||||
|
using Ocelot.Security.Middleware;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
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;
|
||||||
|
|
||||||
|
public SecurityMiddlewareTests()
|
||||||
|
{
|
||||||
|
_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()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user