refactoring to consolidate configuration code

This commit is contained in:
Tom Pallister 2018-04-13 22:47:50 +01:00
parent f88e1f65ef
commit fe5662f954
32 changed files with 552 additions and 676 deletions

View File

@ -1,230 +1,230 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Ocelot.Cache; using Ocelot.Cache;
using Ocelot.Configuration.Builder; using Ocelot.Configuration.Builder;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Configuration.Validator; using Ocelot.Configuration.Validator;
using Ocelot.DependencyInjection; using Ocelot.DependencyInjection;
using Ocelot.Logging; using Ocelot.Logging;
using Ocelot.Responses; using Ocelot.Responses;
namespace Ocelot.Configuration.Creator namespace Ocelot.Configuration.Creator
{ {
/// <summary> /// <summary>
/// Register as singleton /// Register as singleton
/// </summary> /// </summary>
public class FileOcelotConfigurationCreator : IOcelotConfigurationCreator public class FileInternalConfigurationCreator : IInternalConfigurationCreator
{ {
private readonly IOptions<FileConfiguration> _options; private readonly IOptions<FileConfiguration> _options;
private readonly IConfigurationValidator _configurationValidator; private readonly IConfigurationValidator _configurationValidator;
private readonly IOcelotLogger _logger; private readonly IOcelotLogger _logger;
private readonly IClaimsToThingCreator _claimsToThingCreator; private readonly IClaimsToThingCreator _claimsToThingCreator;
private readonly IAuthenticationOptionsCreator _authOptionsCreator; private readonly IAuthenticationOptionsCreator _authOptionsCreator;
private readonly IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator; private readonly IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator;
private readonly IRequestIdKeyCreator _requestIdKeyCreator; private readonly IRequestIdKeyCreator _requestIdKeyCreator;
private readonly IServiceProviderConfigurationCreator _serviceProviderConfigCreator; private readonly IServiceProviderConfigurationCreator _serviceProviderConfigCreator;
private readonly IQoSOptionsCreator _qosOptionsCreator; private readonly IQoSOptionsCreator _qosOptionsCreator;
private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator; private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator;
private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator; private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator;
private readonly IRegionCreator _regionCreator; private readonly IRegionCreator _regionCreator;
private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator;
private readonly IAdministrationPath _adminPath; private readonly IAdministrationPath _adminPath;
private readonly IHeaderFindAndReplaceCreator _headerFAndRCreator; private readonly IHeaderFindAndReplaceCreator _headerFAndRCreator;
private readonly IDownstreamAddressesCreator _downstreamAddressesCreator; private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
public FileOcelotConfigurationCreator( public FileInternalConfigurationCreator(
IOptions<FileConfiguration> options, IOptions<FileConfiguration> options,
IConfigurationValidator configurationValidator, IConfigurationValidator configurationValidator,
IOcelotLoggerFactory loggerFactory, IOcelotLoggerFactory loggerFactory,
IClaimsToThingCreator claimsToThingCreator, IClaimsToThingCreator claimsToThingCreator,
IAuthenticationOptionsCreator authOptionsCreator, IAuthenticationOptionsCreator authOptionsCreator,
IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator, IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator,
IRequestIdKeyCreator requestIdKeyCreator, IRequestIdKeyCreator requestIdKeyCreator,
IServiceProviderConfigurationCreator serviceProviderConfigCreator, IServiceProviderConfigurationCreator serviceProviderConfigCreator,
IQoSOptionsCreator qosOptionsCreator, IQoSOptionsCreator qosOptionsCreator,
IReRouteOptionsCreator fileReRouteOptionsCreator, IReRouteOptionsCreator fileReRouteOptionsCreator,
IRateLimitOptionsCreator rateLimitOptionsCreator, IRateLimitOptionsCreator rateLimitOptionsCreator,
IRegionCreator regionCreator, IRegionCreator regionCreator,
IHttpHandlerOptionsCreator httpHandlerOptionsCreator, IHttpHandlerOptionsCreator httpHandlerOptionsCreator,
IAdministrationPath adminPath, IAdministrationPath adminPath,
IHeaderFindAndReplaceCreator headerFAndRCreator, IHeaderFindAndReplaceCreator headerFAndRCreator,
IDownstreamAddressesCreator downstreamAddressesCreator IDownstreamAddressesCreator downstreamAddressesCreator
) )
{ {
_downstreamAddressesCreator = downstreamAddressesCreator; _downstreamAddressesCreator = downstreamAddressesCreator;
_headerFAndRCreator = headerFAndRCreator; _headerFAndRCreator = headerFAndRCreator;
_adminPath = adminPath; _adminPath = adminPath;
_regionCreator = regionCreator; _regionCreator = regionCreator;
_rateLimitOptionsCreator = rateLimitOptionsCreator; _rateLimitOptionsCreator = rateLimitOptionsCreator;
_requestIdKeyCreator = requestIdKeyCreator; _requestIdKeyCreator = requestIdKeyCreator;
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator; _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
_authOptionsCreator = authOptionsCreator; _authOptionsCreator = authOptionsCreator;
_options = options; _options = options;
_configurationValidator = configurationValidator; _configurationValidator = configurationValidator;
_logger = loggerFactory.CreateLogger<FileOcelotConfigurationCreator>(); _logger = loggerFactory.CreateLogger<FileInternalConfigurationCreator>();
_claimsToThingCreator = claimsToThingCreator; _claimsToThingCreator = claimsToThingCreator;
_serviceProviderConfigCreator = serviceProviderConfigCreator; _serviceProviderConfigCreator = serviceProviderConfigCreator;
_qosOptionsCreator = qosOptionsCreator; _qosOptionsCreator = qosOptionsCreator;
_fileReRouteOptionsCreator = fileReRouteOptionsCreator; _fileReRouteOptionsCreator = fileReRouteOptionsCreator;
_httpHandlerOptionsCreator = httpHandlerOptionsCreator; _httpHandlerOptionsCreator = httpHandlerOptionsCreator;
} }
public async Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration) public async Task<Response<IInternalConfiguration>> Create(FileConfiguration fileConfiguration)
{ {
var config = await SetUpConfiguration(fileConfiguration); var config = await SetUpConfiguration(fileConfiguration);
return config; return config;
} }
private async Task<Response<IOcelotConfiguration>> SetUpConfiguration(FileConfiguration fileConfiguration) private async Task<Response<IInternalConfiguration>> SetUpConfiguration(FileConfiguration fileConfiguration)
{ {
var response = await _configurationValidator.IsValid(fileConfiguration); var response = await _configurationValidator.IsValid(fileConfiguration);
if (response.Data.IsError) if (response.Data.IsError)
{ {
return new ErrorResponse<IOcelotConfiguration>(response.Data.Errors); return new ErrorResponse<IInternalConfiguration>(response.Data.Errors);
} }
var reRoutes = new List<ReRoute>(); var reRoutes = new List<ReRoute>();
foreach (var reRoute in fileConfiguration.ReRoutes) foreach (var reRoute in fileConfiguration.ReRoutes)
{ {
var downstreamReRoute = SetUpDownstreamReRoute(reRoute, fileConfiguration.GlobalConfiguration); var downstreamReRoute = SetUpDownstreamReRoute(reRoute, fileConfiguration.GlobalConfiguration);
var ocelotReRoute = SetUpReRoute(reRoute, downstreamReRoute); var ocelotReRoute = SetUpReRoute(reRoute, downstreamReRoute);
reRoutes.Add(ocelotReRoute); reRoutes.Add(ocelotReRoute);
} }
foreach (var aggregate in fileConfiguration.Aggregates) foreach (var aggregate in fileConfiguration.Aggregates)
{ {
var ocelotReRoute = SetUpAggregateReRoute(reRoutes, aggregate, fileConfiguration.GlobalConfiguration); var ocelotReRoute = SetUpAggregateReRoute(reRoutes, aggregate, fileConfiguration.GlobalConfiguration);
reRoutes.Add(ocelotReRoute); reRoutes.Add(ocelotReRoute);
} }
var serviceProviderConfiguration = _serviceProviderConfigCreator.Create(fileConfiguration.GlobalConfiguration); var serviceProviderConfiguration = _serviceProviderConfigCreator.Create(fileConfiguration.GlobalConfiguration);
var config = new OcelotConfiguration(reRoutes, _adminPath.Path, serviceProviderConfiguration, fileConfiguration.GlobalConfiguration.RequestIdKey); var config = new InternalConfiguration(reRoutes, _adminPath.Path, serviceProviderConfiguration, fileConfiguration.GlobalConfiguration.RequestIdKey);
return new OkResponse<IOcelotConfiguration>(config); return new OkResponse<IInternalConfiguration>(config);
} }
public ReRoute SetUpAggregateReRoute(List<ReRoute> reRoutes, FileAggregateReRoute aggregateReRoute, FileGlobalConfiguration globalConfiguration) public ReRoute SetUpAggregateReRoute(List<ReRoute> reRoutes, FileAggregateReRoute aggregateReRoute, FileGlobalConfiguration globalConfiguration)
{ {
var applicableReRoutes = reRoutes var applicableReRoutes = reRoutes
.SelectMany(x => x.DownstreamReRoute) .SelectMany(x => x.DownstreamReRoute)
.Where(r => aggregateReRoute.ReRouteKeys.Contains(r.Key)) .Where(r => aggregateReRoute.ReRouteKeys.Contains(r.Key))
.ToList(); .ToList();
if(applicableReRoutes.Count != aggregateReRoute.ReRouteKeys.Count) if(applicableReRoutes.Count != aggregateReRoute.ReRouteKeys.Count)
{ {
//todo - log or throw or return error whatever? //todo - log or throw or return error whatever?
} }
//make another re route out of these //make another re route out of these
var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(aggregateReRoute); var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(aggregateReRoute);
var reRoute = new ReRouteBuilder() var reRoute = new ReRouteBuilder()
.WithUpstreamPathTemplate(aggregateReRoute.UpstreamPathTemplate) .WithUpstreamPathTemplate(aggregateReRoute.UpstreamPathTemplate)
.WithUpstreamHttpMethod(aggregateReRoute.UpstreamHttpMethod) .WithUpstreamHttpMethod(aggregateReRoute.UpstreamHttpMethod)
.WithUpstreamTemplatePattern(upstreamTemplatePattern) .WithUpstreamTemplatePattern(upstreamTemplatePattern)
.WithDownstreamReRoutes(applicableReRoutes) .WithDownstreamReRoutes(applicableReRoutes)
.WithUpstreamHost(aggregateReRoute.UpstreamHost) .WithUpstreamHost(aggregateReRoute.UpstreamHost)
.WithAggregator(aggregateReRoute.Aggregator) .WithAggregator(aggregateReRoute.Aggregator)
.Build(); .Build();
return reRoute; return reRoute;
} }
private ReRoute SetUpReRoute(FileReRoute fileReRoute, DownstreamReRoute downstreamReRoutes) private ReRoute SetUpReRoute(FileReRoute fileReRoute, DownstreamReRoute downstreamReRoutes)
{ {
var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute); var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute);
var reRoute = new ReRouteBuilder() var reRoute = new ReRouteBuilder()
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate) .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
.WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod) .WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod)
.WithUpstreamTemplatePattern(upstreamTemplatePattern) .WithUpstreamTemplatePattern(upstreamTemplatePattern)
.WithDownstreamReRoute(downstreamReRoutes) .WithDownstreamReRoute(downstreamReRoutes)
.WithUpstreamHost(fileReRoute.UpstreamHost) .WithUpstreamHost(fileReRoute.UpstreamHost)
.Build(); .Build();
return reRoute; return reRoute;
} }
private DownstreamReRoute SetUpDownstreamReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration) private DownstreamReRoute SetUpDownstreamReRoute(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration)
{ {
var fileReRouteOptions = _fileReRouteOptionsCreator.Create(fileReRoute); var fileReRouteOptions = _fileReRouteOptionsCreator.Create(fileReRoute);
var requestIdKey = _requestIdKeyCreator.Create(fileReRoute, globalConfiguration); var requestIdKey = _requestIdKeyCreator.Create(fileReRoute, globalConfiguration);
var reRouteKey = CreateReRouteKey(fileReRoute); var reRouteKey = CreateReRouteKey(fileReRoute);
var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute); var upstreamTemplatePattern = _upstreamTemplatePatternCreator.Create(fileReRoute);
var authOptionsForRoute = _authOptionsCreator.Create(fileReRoute); var authOptionsForRoute = _authOptionsCreator.Create(fileReRoute);
var claimsToHeaders = _claimsToThingCreator.Create(fileReRoute.AddHeadersToRequest); var claimsToHeaders = _claimsToThingCreator.Create(fileReRoute.AddHeadersToRequest);
var claimsToClaims = _claimsToThingCreator.Create(fileReRoute.AddClaimsToRequest); var claimsToClaims = _claimsToThingCreator.Create(fileReRoute.AddClaimsToRequest);
var claimsToQueries = _claimsToThingCreator.Create(fileReRoute.AddQueriesToRequest); var claimsToQueries = _claimsToThingCreator.Create(fileReRoute.AddQueriesToRequest);
var qosOptions = _qosOptionsCreator.Create(fileReRoute); var qosOptions = _qosOptionsCreator.Create(fileReRoute);
var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting); var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting);
var region = _regionCreator.Create(fileReRoute); var region = _regionCreator.Create(fileReRoute);
var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute); var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute);
var hAndRs = _headerFAndRCreator.Create(fileReRoute); var hAndRs = _headerFAndRCreator.Create(fileReRoute);
var downstreamAddresses = _downstreamAddressesCreator.Create(fileReRoute); var downstreamAddresses = _downstreamAddressesCreator.Create(fileReRoute);
var reRoute = new DownstreamReRouteBuilder() var reRoute = new DownstreamReRouteBuilder()
.WithKey(fileReRoute.Key) .WithKey(fileReRoute.Key)
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate) .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate) .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
.WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod) .WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod)
.WithUpstreamTemplatePattern(upstreamTemplatePattern) .WithUpstreamTemplatePattern(upstreamTemplatePattern)
.WithIsAuthenticated(fileReRouteOptions.IsAuthenticated) .WithIsAuthenticated(fileReRouteOptions.IsAuthenticated)
.WithAuthenticationOptions(authOptionsForRoute) .WithAuthenticationOptions(authOptionsForRoute)
.WithClaimsToHeaders(claimsToHeaders) .WithClaimsToHeaders(claimsToHeaders)
.WithClaimsToClaims(claimsToClaims) .WithClaimsToClaims(claimsToClaims)
.WithRouteClaimsRequirement(fileReRoute.RouteClaimsRequirement) .WithRouteClaimsRequirement(fileReRoute.RouteClaimsRequirement)
.WithIsAuthorised(fileReRouteOptions.IsAuthorised) .WithIsAuthorised(fileReRouteOptions.IsAuthorised)
.WithClaimsToQueries(claimsToQueries) .WithClaimsToQueries(claimsToQueries)
.WithRequestIdKey(requestIdKey) .WithRequestIdKey(requestIdKey)
.WithIsCached(fileReRouteOptions.IsCached) .WithIsCached(fileReRouteOptions.IsCached)
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region)) .WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region))
.WithDownstreamScheme(fileReRoute.DownstreamScheme) .WithDownstreamScheme(fileReRoute.DownstreamScheme)
.WithLoadBalancer(fileReRoute.LoadBalancer) .WithLoadBalancer(fileReRoute.LoadBalancer)
.WithDownstreamAddresses(downstreamAddresses) .WithDownstreamAddresses(downstreamAddresses)
.WithReRouteKey(reRouteKey) .WithReRouteKey(reRouteKey)
.WithIsQos(fileReRouteOptions.IsQos) .WithIsQos(fileReRouteOptions.IsQos)
.WithQosOptions(qosOptions) .WithQosOptions(qosOptions)
.WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting) .WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting)
.WithRateLimitOptions(rateLimitOption) .WithRateLimitOptions(rateLimitOption)
.WithHttpHandlerOptions(httpHandlerOptions) .WithHttpHandlerOptions(httpHandlerOptions)
.WithServiceName(fileReRoute.ServiceName) .WithServiceName(fileReRoute.ServiceName)
.WithUseServiceDiscovery(fileReRoute.UseServiceDiscovery) .WithUseServiceDiscovery(fileReRoute.UseServiceDiscovery)
.WithUpstreamHeaderFindAndReplace(hAndRs.Upstream) .WithUpstreamHeaderFindAndReplace(hAndRs.Upstream)
.WithDownstreamHeaderFindAndReplace(hAndRs.Downstream) .WithDownstreamHeaderFindAndReplace(hAndRs.Downstream)
.WithUpstreamHost(fileReRoute.UpstreamHost) .WithUpstreamHost(fileReRoute.UpstreamHost)
.WithDelegatingHandlers(fileReRoute.DelegatingHandlers) .WithDelegatingHandlers(fileReRoute.DelegatingHandlers)
.WithAddHeadersToDownstream(hAndRs.AddHeadersToDownstream) .WithAddHeadersToDownstream(hAndRs.AddHeadersToDownstream)
.Build(); .Build();
return reRoute; return reRoute;
} }
private string CreateReRouteKey(FileReRoute fileReRoute) private string CreateReRouteKey(FileReRoute fileReRoute)
{ {
//note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain //note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain
var loadBalancerKey = $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}"; var loadBalancerKey = $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}";
return loadBalancerKey; return loadBalancerKey;
} }
} }
} }

View File

@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Ocelot.Configuration.File;
using Ocelot.Responses;
namespace Ocelot.Configuration.Creator
{
public interface IInternalConfigurationCreator
{
Task<Response<IInternalConfiguration>> Create(FileConfiguration fileConfiguration);
}
}

View File

@ -1,11 +0,0 @@
using System.Threading.Tasks;
using Ocelot.Configuration.File;
using Ocelot.Responses;
namespace Ocelot.Configuration.Creator
{
public interface IOcelotConfigurationCreator
{
Task<Response<IOcelotConfiguration>> Create(FileConfiguration fileConfiguration);
}
}

View File

@ -1,16 +1,14 @@
using System.Collections.Generic; namespace Ocelot.Configuration
using IdentityServer4.AccessTokenValidation; {
using IdentityServer4.Models; using System.Collections.Generic;
namespace Ocelot.Configuration.Provider public interface IIdentityServerConfiguration
{ {
public interface IIdentityServerConfiguration string ApiName { get; }
{ string ApiSecret { get; }
string ApiName { get; } bool RequireHttps { get; }
string ApiSecret { get; } List<string> AllowedScopes { get; }
bool RequireHttps { get; } string CredentialsSigningCertificateLocation { get; }
List<string> AllowedScopes { get; } string CredentialsSigningCertificatePassword { get; }
string CredentialsSigningCertificateLocation { get; } }
string CredentialsSigningCertificatePassword { get; } }
}
}

View File

@ -1,12 +1,12 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace Ocelot.Configuration namespace Ocelot.Configuration
{ {
public interface IOcelotConfiguration public interface IInternalConfiguration
{ {
List<ReRoute> ReRoutes { get; } List<ReRoute> ReRoutes { get; }
string AdministrationPath {get;} string AdministrationPath {get;}
ServiceProviderConfiguration ServiceProviderConfiguration {get;} ServiceProviderConfiguration ServiceProviderConfiguration {get;}
string RequestId {get;} string RequestId {get;}
} }
} }

View File

@ -1,32 +1,30 @@
using System.Collections.Generic; namespace Ocelot.Configuration
using IdentityServer4.AccessTokenValidation; {
using IdentityServer4.Models; using System.Collections.Generic;
namespace Ocelot.Configuration.Provider public class IdentityServerConfiguration : IIdentityServerConfiguration
{ {
public class IdentityServerConfiguration : IIdentityServerConfiguration public IdentityServerConfiguration(
{ string apiName,
public IdentityServerConfiguration( bool requireHttps,
string apiName, string apiSecret,
bool requireHttps, List<string> allowedScopes,
string apiSecret, string credentialsSigningCertificateLocation,
List<string> allowedScopes, string credentialsSigningCertificatePassword)
string credentialsSigningCertificateLocation, {
string credentialsSigningCertificatePassword) ApiName = apiName;
{ RequireHttps = requireHttps;
ApiName = apiName; ApiSecret = apiSecret;
RequireHttps = requireHttps; AllowedScopes = allowedScopes;
ApiSecret = apiSecret; CredentialsSigningCertificateLocation = credentialsSigningCertificateLocation;
AllowedScopes = allowedScopes; CredentialsSigningCertificatePassword = credentialsSigningCertificatePassword;
CredentialsSigningCertificateLocation = credentialsSigningCertificateLocation; }
CredentialsSigningCertificatePassword = credentialsSigningCertificatePassword;
} public string ApiName { get; }
public bool RequireHttps { get; }
public string ApiName { get; private set; } public List<string> AllowedScopes { get; }
public bool RequireHttps { get; private set; } public string ApiSecret { get; }
public List<string> AllowedScopes { get; private set; } public string CredentialsSigningCertificateLocation { get; }
public string ApiSecret { get; private set; } public string CredentialsSigningCertificatePassword { get; }
public string CredentialsSigningCertificateLocation { get; private set; } }
public string CredentialsSigningCertificatePassword { get; private set; } }
}
}

View File

@ -1,20 +1,20 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace Ocelot.Configuration namespace Ocelot.Configuration
{ {
public class OcelotConfiguration : IOcelotConfiguration public class InternalConfiguration : IInternalConfiguration
{ {
public OcelotConfiguration(List<ReRoute> reRoutes, string administrationPath, ServiceProviderConfiguration serviceProviderConfiguration, string requestId) public InternalConfiguration(List<ReRoute> reRoutes, string administrationPath, ServiceProviderConfiguration serviceProviderConfiguration, string requestId)
{ {
ReRoutes = reRoutes; ReRoutes = reRoutes;
AdministrationPath = administrationPath; AdministrationPath = administrationPath;
ServiceProviderConfiguration = serviceProviderConfiguration; ServiceProviderConfiguration = serviceProviderConfiguration;
RequestId = requestId; RequestId = requestId;
} }
public List<ReRoute> ReRoutes { get; } public List<ReRoute> ReRoutes { get; }
public string AdministrationPath {get;} public string AdministrationPath {get;}
public ServiceProviderConfiguration ServiceProviderConfiguration {get;} public ServiceProviderConfiguration ServiceProviderConfiguration {get;}
public string RequestId {get;} public string RequestId {get;}
} }
} }

View File

@ -1,9 +0,0 @@
using Ocelot.Responses;
namespace Ocelot.Configuration.Provider
{
public interface IOcelotConfigurationProvider
{
Response<IOcelotConfiguration> Get();
}
}

View File

@ -1,30 +0,0 @@
using Ocelot.Configuration.Repository;
using Ocelot.Responses;
namespace Ocelot.Configuration.Provider
{
/// <summary>
/// Register as singleton
/// </summary>
public class OcelotConfigurationProvider : IOcelotConfigurationProvider
{
private readonly IOcelotConfigurationRepository _config;
public OcelotConfigurationProvider(IOcelotConfigurationRepository repo)
{
_config = repo;
}
public Response<IOcelotConfiguration> Get()
{
var repoConfig = _config.Get();
if (repoConfig.IsError)
{
return new ErrorResponse<IOcelotConfiguration>(repoConfig.Errors);
}
return new OkResponse<IOcelotConfiguration>(repoConfig.Data);
}
}
}

View File

@ -1,35 +1,45 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Consul;
using Newtonsoft.Json;
using Ocelot.Configuration.File;
using Ocelot.Infrastructure.Consul;
using Ocelot.Logging;
using Ocelot.Responses;
using Ocelot.ServiceDiscovery.Configuration;
namespace Ocelot.Configuration.Repository namespace Ocelot.Configuration.Repository
{ {
using System;
using System.Text;
using System.Threading.Tasks;
using Consul;
using Newtonsoft.Json;
using Ocelot.Configuration.File;
using Ocelot.Infrastructure.Consul;
using Ocelot.Logging;
using Ocelot.Responses;
using Ocelot.ServiceDiscovery.Configuration;
public class ConsulFileConfigurationRepository : IFileConfigurationRepository public class ConsulFileConfigurationRepository : IFileConfigurationRepository
{ {
private readonly ConsulClient _consul; private readonly ConsulClient _consul;
private const string OcelotConfiguration = "OcelotConfiguration"; private const string OcelotConfiguration = "InternalConfiguration";
private readonly Cache.IOcelotCache<FileConfiguration> _cache; private readonly Cache.IOcelotCache<FileConfiguration> _cache;
private readonly IOcelotLogger _logger; private readonly IOcelotLogger _logger;
public ConsulFileConfigurationRepository( public ConsulFileConfigurationRepository(
Cache.IOcelotCache<FileConfiguration> cache, Cache.IOcelotCache<FileConfiguration> cache,
ServiceProviderConfiguration serviceProviderConfiguration, IInternalConfigurationRepository repo,
IConsulClientFactory factory, IConsulClientFactory factory,
IOcelotLoggerFactory loggerFactory) IOcelotLoggerFactory loggerFactory)
{ {
_logger = loggerFactory.CreateLogger<ConsulFileConfigurationRepository>(); _logger = loggerFactory.CreateLogger<ConsulFileConfigurationRepository>();
_cache = cache; _cache = cache;
var consulHost = string.IsNullOrEmpty(serviceProviderConfiguration?.Host) ? "localhost" : serviceProviderConfiguration?.Host; var internalConfig = repo.Get();
var consulPort = serviceProviderConfiguration?.Port ?? 8500;
var token = serviceProviderConfiguration?.Token; var consulHost = "localhost";
var consulPort = 8500;
string token = null;
if (!internalConfig.IsError)
{
consulHost = string.IsNullOrEmpty(internalConfig.Data.ServiceProviderConfiguration?.Host) ? consulHost : internalConfig.Data.ServiceProviderConfiguration?.Host;
consulPort = internalConfig.Data.ServiceProviderConfiguration?.Port ?? consulPort;
token = internalConfig.Data.ServiceProviderConfiguration?.Token;
}
var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, token); var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, token);
_consul = factory.Get(config); _consul = factory.Get(config);

View File

@ -0,0 +1,10 @@
using Ocelot.Responses;
namespace Ocelot.Configuration.Repository
{
public interface IInternalConfigurationRepository
{
Response<IInternalConfiguration> Get();
Response AddOrReplace(IInternalConfiguration internalConfiguration);
}
}

View File

@ -1,10 +0,0 @@
using Ocelot.Responses;
namespace Ocelot.Configuration.Repository
{
public interface IOcelotConfigurationRepository
{
Response<IOcelotConfiguration> Get();
Response AddOrReplace(IOcelotConfiguration ocelotConfiguration);
}
}

View File

@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Ocelot.Responses;
namespace Ocelot.Configuration.Repository
{
/// <summary>
/// Register as singleton
/// </summary>
public class InMemoryInternalConfigurationRepository : IInternalConfigurationRepository
{
private static readonly object LockObject = new object();
private IInternalConfiguration _internalConfiguration;
public Response<IInternalConfiguration> Get()
{
return new OkResponse<IInternalConfiguration>(_internalConfiguration);
}
public Response AddOrReplace(IInternalConfiguration internalConfiguration)
{
lock (LockObject)
{
_internalConfiguration = internalConfiguration;
}
return new OkResponse();
}
}
}

View File

@ -1,30 +0,0 @@
using System.Threading.Tasks;
using Ocelot.Responses;
namespace Ocelot.Configuration.Repository
{
/// <summary>
/// Register as singleton
/// </summary>
public class InMemoryOcelotConfigurationRepository : IOcelotConfigurationRepository
{
private static readonly object LockObject = new object();
private IOcelotConfiguration _ocelotConfiguration;
public Response<IOcelotConfiguration> Get()
{
return new OkResponse<IOcelotConfiguration>(_ocelotConfiguration);
}
public Response AddOrReplace(IOcelotConfiguration ocelotConfiguration)
{
lock (LockObject)
{
_ocelotConfiguration = ocelotConfiguration;
}
return new OkResponse();
}
}
}

View File

@ -8,13 +8,13 @@ namespace Ocelot.Configuration.Setter
{ {
public class FileConfigurationSetter : IFileConfigurationSetter public class FileConfigurationSetter : IFileConfigurationSetter
{ {
private readonly IOcelotConfigurationRepository _configRepo; private readonly IInternalConfigurationRepository _configRepo;
private readonly IOcelotConfigurationCreator _configCreator; private readonly IInternalConfigurationCreator _configCreator;
private readonly IFileConfigurationRepository _repo; private readonly IFileConfigurationRepository _repo;
public FileConfigurationSetter( public FileConfigurationSetter(
IOcelotConfigurationRepository configRepo, IInternalConfigurationRepository configRepo,
IOcelotConfigurationCreator configCreator, IInternalConfigurationCreator configCreator,
IFileConfigurationRepository repo) IFileConfigurationRepository repo)
{ {
_configRepo = configRepo; _configRepo = configRepo;

View File

@ -73,8 +73,8 @@ namespace Ocelot.DependencyInjection
_services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>(); _services.TryAddSingleton<IHttpResponseHeaderReplacer, HttpResponseHeaderReplacer>();
_services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>(); _services.TryAddSingleton<IHttpContextRequestHeaderReplacer, HttpContextRequestHeaderReplacer>();
_services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>(); _services.TryAddSingleton<IHeaderFindAndReplaceCreator, HeaderFindAndReplaceCreator>();
_services.TryAddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>(); _services.TryAddSingleton<IInternalConfigurationCreator, FileInternalConfigurationCreator>();
_services.TryAddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>(); _services.TryAddSingleton<IInternalConfigurationRepository, InMemoryInternalConfigurationRepository>();
_services.TryAddSingleton<IConfigurationValidator, FileConfigurationFluentValidator>(); _services.TryAddSingleton<IConfigurationValidator, FileConfigurationFluentValidator>();
_services.TryAddSingleton<IClaimsToThingCreator, ClaimsToThingCreator>(); _services.TryAddSingleton<IClaimsToThingCreator, ClaimsToThingCreator>();
_services.TryAddSingleton<IAuthenticationOptionsCreator, AuthenticationOptionsCreator>(); _services.TryAddSingleton<IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
@ -96,7 +96,6 @@ namespace Ocelot.DependencyInjection
_services.TryAddSingleton<ILoadBalancerHouse, LoadBalancerHouse>(); _services.TryAddSingleton<ILoadBalancerHouse, LoadBalancerHouse>();
_services.TryAddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>(); _services.TryAddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
_services.TryAddSingleton<IRemoveOutputHeaders, RemoveOutputHeaders>(); _services.TryAddSingleton<IRemoveOutputHeaders, RemoveOutputHeaders>();
_services.TryAddSingleton<IOcelotConfigurationProvider, OcelotConfigurationProvider>();
_services.TryAddSingleton<IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>(); _services.TryAddSingleton<IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
_services.TryAddSingleton<IClaimsAuthoriser, ClaimsAuthoriser>(); _services.TryAddSingleton<IClaimsAuthoriser, ClaimsAuthoriser>();
_services.TryAddSingleton<IScopesAuthoriser, ScopesAuthoriser>(); _services.TryAddSingleton<IScopesAuthoriser, ScopesAuthoriser>();
@ -247,17 +246,6 @@ namespace Ocelot.DependencyInjection
public IOcelotBuilder AddStoreOcelotConfigurationInConsul() public IOcelotBuilder AddStoreOcelotConfigurationInConsul()
{ {
var serviceDiscoveryPort = _configurationRoot.GetValue("GlobalConfiguration:ServiceDiscoveryProvider:Port", 0);
var serviceDiscoveryHost = _configurationRoot.GetValue("GlobalConfiguration:ServiceDiscoveryProvider:Host", string.Empty);
var serviceDiscoveryToken = _configurationRoot.GetValue("GlobalConfiguration:ServiceDiscoveryProvider:Token", string.Empty);
var config = new ServiceProviderConfigurationBuilder()
.WithPort(serviceDiscoveryPort)
.WithHost(serviceDiscoveryHost)
.WithToken(serviceDiscoveryToken)
.Build();
_services.AddSingleton<ServiceProviderConfiguration>(config);
_services.AddSingleton<ConsulFileConfigurationPoller>(); _services.AddSingleton<ConsulFileConfigurationPoller>();
_services.AddSingleton<IFileConfigurationRepository, ConsulFileConfigurationRepository>(); _services.AddSingleton<IFileConfigurationRepository, ConsulFileConfigurationRepository>();
return this; return this;
@ -273,12 +261,12 @@ namespace Ocelot.DependencyInjection
_services.AddSingleton<ICacheManager<CachedResponse>>(cacheManagerOutputCache); _services.AddSingleton<ICacheManager<CachedResponse>>(cacheManagerOutputCache);
_services.AddSingleton<IOcelotCache<CachedResponse>>(ocelotOutputCacheManager); _services.AddSingleton<IOcelotCache<CachedResponse>>(ocelotOutputCacheManager);
var ocelotConfigCacheManagerOutputCache = CacheFactory.Build<IOcelotConfiguration>("OcelotConfigurationCache", settings); var ocelotConfigCacheManagerOutputCache = CacheFactory.Build<IInternalConfiguration>("OcelotConfigurationCache", settings);
var ocelotConfigCacheManager = new OcelotCacheManagerCache<IOcelotConfiguration>(ocelotConfigCacheManagerOutputCache); var ocelotConfigCacheManager = new OcelotCacheManagerCache<IInternalConfiguration>(ocelotConfigCacheManagerOutputCache);
_services.RemoveAll(typeof(ICacheManager<IOcelotConfiguration>)); _services.RemoveAll(typeof(ICacheManager<IInternalConfiguration>));
_services.RemoveAll(typeof(IOcelotCache<IOcelotConfiguration>)); _services.RemoveAll(typeof(IOcelotCache<IInternalConfiguration>));
_services.AddSingleton<ICacheManager<IOcelotConfiguration>>(ocelotConfigCacheManagerOutputCache); _services.AddSingleton<ICacheManager<IInternalConfiguration>>(ocelotConfigCacheManagerOutputCache);
_services.AddSingleton<IOcelotCache<IOcelotConfiguration>>(ocelotConfigCacheManager); _services.AddSingleton<IOcelotCache<IInternalConfiguration>>(ocelotConfigCacheManager);
var fileConfigCacheManagerOutputCache = CacheFactory.Build<FileConfiguration>("FileConfigurationCache", settings); var fileConfigCacheManagerOutputCache = CacheFactory.Build<FileConfiguration>("FileConfigurationCache", settings);
var fileConfigCacheManager = new OcelotCacheManagerCache<FileConfiguration>(fileConfigCacheManagerOutputCache); var fileConfigCacheManager = new OcelotCacheManagerCache<FileConfiguration>(fileConfigCacheManagerOutputCache);

View File

@ -18,7 +18,7 @@ namespace Ocelot.DownstreamRouteFinder.Finder
_placeholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder; _placeholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder;
} }
public Response<DownstreamRoute> FindDownstreamRoute(string path, string httpMethod, IOcelotConfiguration configuration, string upstreamHost) public Response<DownstreamRoute> FindDownstreamRoute(string path, string httpMethod, IInternalConfiguration configuration, string upstreamHost)
{ {
var downstreamRoutes = new List<DownstreamRoute>(); var downstreamRoutes = new List<DownstreamRoute>();

View File

@ -6,6 +6,6 @@ namespace Ocelot.DownstreamRouteFinder.Finder
{ {
public interface IDownstreamRouteFinder public interface IDownstreamRouteFinder
{ {
Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod, IOcelotConfiguration configuration, string upstreamHost); Response<DownstreamRoute> FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost);
} }
} }

View File

@ -2,6 +2,7 @@ using System.Threading.Tasks;
using System.Linq; using System.Linq;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Configuration.Provider; using Ocelot.Configuration.Provider;
using Ocelot.Configuration.Repository;
using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.Finder;
using Ocelot.Infrastructure.Extensions; using Ocelot.Infrastructure.Extensions;
using Ocelot.Logging; using Ocelot.Logging;
@ -14,17 +15,17 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
{ {
private readonly OcelotRequestDelegate _next; private readonly OcelotRequestDelegate _next;
private readonly IDownstreamRouteFinder _downstreamRouteFinder; private readonly IDownstreamRouteFinder _downstreamRouteFinder;
private readonly IOcelotConfigurationProvider _configProvider; private readonly IInternalConfigurationRepository _repo;
private readonly IMultiplexer _multiplexer; private readonly IMultiplexer _multiplexer;
public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next, public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next,
IOcelotLoggerFactory loggerFactory, IOcelotLoggerFactory loggerFactory,
IDownstreamRouteFinder downstreamRouteFinder, IDownstreamRouteFinder downstreamRouteFinder,
IOcelotConfigurationProvider configProvider, IInternalConfigurationRepository repo,
IMultiplexer multiplexer) IMultiplexer multiplexer)
:base(loggerFactory.CreateLogger<DownstreamRouteFinderMiddleware>()) :base(loggerFactory.CreateLogger<DownstreamRouteFinderMiddleware>())
{ {
_configProvider = configProvider; _repo = repo;
_multiplexer = multiplexer; _multiplexer = multiplexer;
_next = next; _next = next;
_downstreamRouteFinder = downstreamRouteFinder; _downstreamRouteFinder = downstreamRouteFinder;
@ -36,7 +37,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
var upstreamHost = context.HttpContext.Request.Headers["Host"]; var upstreamHost = context.HttpContext.Request.Headers["Host"];
var configuration = _configProvider.Get(); var configuration = _repo.Get();
if (configuration.IsError) if (configuration.IsError)
{ {

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Configuration.Provider; using Ocelot.Configuration.Repository;
using Ocelot.Infrastructure.Extensions; using Ocelot.Infrastructure.Extensions;
using Ocelot.Infrastructure.RequestData; using Ocelot.Infrastructure.RequestData;
using Ocelot.Logging; using Ocelot.Logging;
@ -15,16 +15,16 @@ namespace Ocelot.Errors.Middleware
public class ExceptionHandlerMiddleware : OcelotMiddleware public class ExceptionHandlerMiddleware : OcelotMiddleware
{ {
private readonly OcelotRequestDelegate _next; private readonly OcelotRequestDelegate _next;
private readonly IOcelotConfigurationProvider _provider; private readonly IInternalConfigurationRepository _configRepo;
private readonly IRequestScopedDataRepository _repo; private readonly IRequestScopedDataRepository _repo;
public ExceptionHandlerMiddleware(OcelotRequestDelegate next, public ExceptionHandlerMiddleware(OcelotRequestDelegate next,
IOcelotLoggerFactory loggerFactory, IOcelotLoggerFactory loggerFactory,
IOcelotConfigurationProvider provider, IInternalConfigurationRepository configRepo,
IRequestScopedDataRepository repo) IRequestScopedDataRepository repo)
: base(loggerFactory.CreateLogger<ExceptionHandlerMiddleware>()) : base(loggerFactory.CreateLogger<ExceptionHandlerMiddleware>())
{ {
_provider = provider; _configRepo = configRepo;
_repo = repo; _repo = repo;
_next = next; _next = next;
} }
@ -58,7 +58,7 @@ namespace Ocelot.Errors.Middleware
//try and get the global request id and set it for logs... //try and get the global request id and set it for logs...
//should this basically be immutable per request...i guess it should! //should this basically be immutable per request...i guess it should!
//first thing is get config //first thing is get config
var configuration = _provider.Get(); var configuration = _configRepo.Get();
if(configuration.IsError) if(configuration.IsError)
{ {

View File

@ -10,7 +10,6 @@
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Configuration.Creator; using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Configuration.Provider;
using Ocelot.Configuration.Repository; using Ocelot.Configuration.Repository;
using Ocelot.Configuration.Setter; using Ocelot.Configuration.Setter;
using Ocelot.Responses; using Ocelot.Responses;
@ -84,63 +83,108 @@
node.Start(nodeId.Id); node.Start(nodeId.Id);
} }
private static async Task<IOcelotConfiguration> CreateConfiguration(IApplicationBuilder builder) private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder)
{ {
var deps = GetDependencies(builder); // make configuration from file system?
// earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this
var fileConfig = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
// now create the config
var internalConfigCreator = (IInternalConfigurationCreator)builder.ApplicationServices.GetService(typeof(IInternalConfigurationCreator));
var internalConfig = await internalConfigCreator.Create(fileConfig.Value);
var ocelotConfiguration = deps.provider.Get(); // now save it in memory
var internalConfigRepo = (IInternalConfigurationRepository)builder.ApplicationServices.GetService(typeof(IInternalConfigurationRepository));
internalConfigRepo.AddOrReplace(internalConfig.Data);
if (ConfigurationNotSetUp(ocelotConfiguration)) var fileConfigSetter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
var fileConfigRepo = (IFileConfigurationRepository)builder.ApplicationServices.GetService(typeof(IFileConfigurationRepository));
if (UsingConsul(fileConfigRepo))
{ {
var response = await SetConfig(builder, deps.fileConfiguration, deps.setter, deps.provider, deps.repo); await SetFileConfigInConsul(builder, fileConfigRepo, fileConfig, internalConfigCreator, internalConfigRepo);
}
if (UnableToSetConfig(response)) else
{
await SetFileConfig(fileConfigSetter, fileConfig);
}
return GetOcelotConfigAndReturn(internalConfigRepo);
}
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
IFileConfigurationRepository fileConfigRepo, IOptions<FileConfiguration> fileConfig,
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
{
// get the config from consul.
var fileConfigFromConsul = await fileConfigRepo.Get();
if (IsError(fileConfigFromConsul))
{
ThrowToStopOcelotStarting(fileConfigFromConsul);
}
else if (ConfigNotStoredInConsul(fileConfigFromConsul))
{
//there was no config in consul set the file in config in consul
await fileConfigRepo.Set(fileConfig.Value);
}
else
{
// create the internal config from consul data
var internalConfig = await internalConfigCreator.Create(fileConfigFromConsul.Data);
if (IsError(internalConfig))
{ {
ThrowToStopOcelotStarting(response); ThrowToStopOcelotStarting(internalConfig);
}
else
{
// add the internal config to the internal repo
var response = internalConfigRepo.AddOrReplace(internalConfig.Data);
if (IsError(response))
{
ThrowToStopOcelotStarting(response);
}
}
if (IsError(internalConfig))
{
ThrowToStopOcelotStarting(internalConfig);
} }
} }
return GetOcelotConfigAndReturn(deps.provider); //todo - this starts the poller if it has been registered...please this is so bad.
var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller));
} }
private static async Task<Response> SetConfig(IApplicationBuilder builder, IOptions<FileConfiguration> fileConfiguration, IFileConfigurationSetter setter, IOcelotConfigurationProvider provider, IFileConfigurationRepository repo) private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptions<FileConfiguration> fileConfig)
{ {
if (UsingConsul(repo)) Response response;
response = await fileConfigSetter.Set(fileConfig.Value);
if (IsError(response))
{ {
return await SetUpConfigFromConsul(builder, repo, setter, fileConfiguration); ThrowToStopOcelotStarting(response);
} }
return await setter.Set(fileConfiguration.Value);
} }
private static bool UnableToSetConfig(Response response) private static bool ConfigNotStoredInConsul(Responses.Response<FileConfiguration> fileConfigFromConsul)
{
return fileConfigFromConsul.Data == null;
}
private static bool IsError(Response response)
{ {
return response == null || response.IsError; return response == null || response.IsError;
} }
private static bool ConfigurationNotSetUp(Ocelot.Responses.Response<IOcelotConfiguration> ocelotConfiguration) private static IInternalConfiguration GetOcelotConfigAndReturn(IInternalConfigurationRepository provider)
{
return ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError;
}
private static (IOptions<FileConfiguration> fileConfiguration, IFileConfigurationSetter setter, IOcelotConfigurationProvider provider, IFileConfigurationRepository repo) GetDependencies(IApplicationBuilder builder)
{
var fileConfiguration = (IOptions<FileConfiguration>)builder.ApplicationServices.GetService(typeof(IOptions<FileConfiguration>));
var setter = (IFileConfigurationSetter)builder.ApplicationServices.GetService(typeof(IFileConfigurationSetter));
var provider = (IOcelotConfigurationProvider)builder.ApplicationServices.GetService(typeof(IOcelotConfigurationProvider));
var repo = (IFileConfigurationRepository)builder.ApplicationServices.GetService(typeof(IFileConfigurationRepository));
return (fileConfiguration, setter, provider, repo);
}
private static IOcelotConfiguration GetOcelotConfigAndReturn(IOcelotConfigurationProvider provider)
{ {
var ocelotConfiguration = provider.Get(); var ocelotConfiguration = provider.Get();
if(ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError) if(ocelotConfiguration?.Data == null || ocelotConfiguration.IsError)
{ {
ThrowToStopOcelotStarting(ocelotConfiguration); ThrowToStopOcelotStarting(ocelotConfiguration);
} }
@ -158,49 +202,7 @@
return fileConfigRepo.GetType() == typeof(ConsulFileConfigurationRepository); return fileConfigRepo.GetType() == typeof(ConsulFileConfigurationRepository);
} }
private static async Task<Response> SetUpConfigFromConsul(IApplicationBuilder builder, IFileConfigurationRepository consulFileConfigRepo, IFileConfigurationSetter setter, IOptions<FileConfiguration> fileConfig) private static void CreateAdministrationArea(IApplicationBuilder builder, IInternalConfiguration configuration)
{
Response config = null;
var ocelotConfigurationRepository =
(IOcelotConfigurationRepository) builder.ApplicationServices.GetService(
typeof(IOcelotConfigurationRepository));
var ocelotConfigurationCreator =
(IOcelotConfigurationCreator) builder.ApplicationServices.GetService(
typeof(IOcelotConfigurationCreator));
var fileConfigFromConsul = await consulFileConfigRepo.Get();
if (fileConfigFromConsul.Data == null)
{
config = await setter.Set(fileConfig.Value);
var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller));
}
else
{
var ocelotConfig = await ocelotConfigurationCreator.Create(fileConfigFromConsul.Data);
if(ocelotConfig.IsError)
{
return new ErrorResponse(ocelotConfig.Errors);
}
config = ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data);
if (config.IsError)
{
return new ErrorResponse(config.Errors);
}
//todo - this starts the poller if it has been registered...please this is so bad.
var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller));
}
return new OkResponse();
}
private static void CreateAdministrationArea(IApplicationBuilder builder, IOcelotConfiguration configuration)
{ {
if(!string.IsNullOrEmpty(configuration.AdministrationPath)) if(!string.IsNullOrEmpty(configuration.AdministrationPath))
{ {

View File

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Configuration.Provider; using Ocelot.Configuration.Provider;
using Ocelot.Configuration.Repository;
using Ocelot.Middleware; using Ocelot.Middleware;
using Rafty.Concensus; using Rafty.Concensus;
using Rafty.Infrastructure; using Rafty.Infrastructure;
@ -15,20 +16,20 @@ namespace Ocelot.Raft
public class FilePeersProvider : IPeersProvider public class FilePeersProvider : IPeersProvider
{ {
private readonly IOptions<FilePeers> _options; private readonly IOptions<FilePeers> _options;
private List<IPeer> _peers; private readonly List<IPeer> _peers;
private IBaseUrlFinder _finder; private IBaseUrlFinder _finder;
private IOcelotConfigurationProvider _provider; private IInternalConfigurationRepository _repo;
private IIdentityServerConfiguration _identityServerConfig; private IIdentityServerConfiguration _identityServerConfig;
public FilePeersProvider(IOptions<FilePeers> options, IBaseUrlFinder finder, IOcelotConfigurationProvider provider, IIdentityServerConfiguration identityServerConfig) public FilePeersProvider(IOptions<FilePeers> options, IBaseUrlFinder finder, IInternalConfigurationRepository repo, IIdentityServerConfiguration identityServerConfig)
{ {
_identityServerConfig = identityServerConfig; _identityServerConfig = identityServerConfig;
_provider = provider; _repo = repo;
_finder = finder; _finder = finder;
_options = options; _options = options;
_peers = new List<IPeer>(); _peers = new List<IPeer>();
var config = _provider.Get(); var config = _repo.Get();
foreach (var item in _options.Value.Peers) foreach (var item in _options.Value.Peers)
{ {
var httpClient = new HttpClient(); var httpClient = new HttpClient();

View File

@ -21,10 +21,10 @@ namespace Ocelot.Raft
private JsonSerializerSettings _jsonSerializerSettings; private JsonSerializerSettings _jsonSerializerSettings;
private string _baseSchemeUrlAndPort; private string _baseSchemeUrlAndPort;
private BearerToken _token; private BearerToken _token;
private IOcelotConfiguration _config; private IInternalConfiguration _config;
private IIdentityServerConfiguration _identityServerConfiguration; private IIdentityServerConfiguration _identityServerConfiguration;
public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IOcelotConfiguration config, IIdentityServerConfiguration identityServerConfiguration) public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IInternalConfiguration config, IIdentityServerConfiguration identityServerConfiguration)
{ {
_identityServerConfiguration = identityServerConfiguration; _identityServerConfiguration = identityServerConfiguration;
_config = config; _config = config;

View File

@ -303,7 +303,7 @@ namespace Ocelot.AcceptanceTests
{ {
app.Run(async context => app.Run(async context =>
{ {
if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration") if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/InternalConfiguration")
{ {
var json = JsonConvert.SerializeObject(_config); var json = JsonConvert.SerializeObject(_config);
@ -315,7 +315,7 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteJsonAsync(new FakeConsulGetResponse[] { kvp }); await context.Response.WriteJsonAsync(new FakeConsulGetResponse[] { kvp });
} }
else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration") else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/InternalConfiguration")
{ {
try try
{ {
@ -352,7 +352,7 @@ namespace Ocelot.AcceptanceTests
public int CreateIndex => 100; public int CreateIndex => 100;
public int ModifyIndex => 200; public int ModifyIndex => 200;
public int LockIndex => 200; public int LockIndex => 200;
public string Key => "OcelotConfiguration"; public string Key => "InternalConfiguration";
public int Flags => 0; public int Flags => 0;
public string Value { get; private set; } public string Value { get; private set; }
public string Session => "adf4238a-882b-9ddc-4a9d-5b6758e4159e"; public string Session => "adf4238a-882b-9ddc-4a9d-5b6758e4159e";

View File

@ -26,10 +26,10 @@ namespace Ocelot.UnitTests.Configuration
{ {
private readonly Mock<IOptions<FileConfiguration>> _fileConfig; private readonly Mock<IOptions<FileConfiguration>> _fileConfig;
private readonly Mock<IConfigurationValidator> _validator; private readonly Mock<IConfigurationValidator> _validator;
private Response<IOcelotConfiguration> _config; private Response<IInternalConfiguration> _config;
private FileConfiguration _fileConfiguration; private FileConfiguration _fileConfiguration;
private readonly Mock<IOcelotLoggerFactory> _logger; private readonly Mock<IOcelotLoggerFactory> _logger;
private readonly FileOcelotConfigurationCreator _ocelotConfigurationCreator; private readonly FileInternalConfigurationCreator _internalConfigurationCreator;
private Mock<IClaimsToThingCreator> _claimsToThingCreator; private Mock<IClaimsToThingCreator> _claimsToThingCreator;
private Mock<IAuthenticationOptionsCreator> _authOptionsCreator; private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator; private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
@ -63,7 +63,7 @@ namespace Ocelot.UnitTests.Configuration
_headerFindAndReplaceCreator = new Mock<IHeaderFindAndReplaceCreator>(); _headerFindAndReplaceCreator = new Mock<IHeaderFindAndReplaceCreator>();
_downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>(); _downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>();
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator( _internalConfigurationCreator = new FileInternalConfigurationCreator(
_fileConfig.Object, _fileConfig.Object,
_validator.Object, _validator.Object,
_logger.Object, _logger.Object,
@ -807,7 +807,7 @@ namespace Ocelot.UnitTests.Configuration
private void WhenICreateTheConfig() private void WhenICreateTheConfig()
{ {
_config = _ocelotConfigurationCreator.Create(_fileConfiguration).Result; _config = _internalConfigurationCreator.Create(_fileConfiguration).Result;
} }
private void ThenTheReRoutesAre(List<ReRoute> expectedReRoutes) private void ThenTheReRoutesAre(List<ReRoute> expectedReRoutes)

View File

@ -19,17 +19,17 @@ namespace Ocelot.UnitTests.Configuration
{ {
private FileConfiguration _fileConfiguration; private FileConfiguration _fileConfiguration;
private FileConfigurationSetter _configSetter; private FileConfigurationSetter _configSetter;
private Mock<IOcelotConfigurationRepository> _configRepo; private Mock<IInternalConfigurationRepository> _configRepo;
private Mock<IOcelotConfigurationCreator> _configCreator; private Mock<IInternalConfigurationCreator> _configCreator;
private Response<IOcelotConfiguration> _configuration; private Response<IInternalConfiguration> _configuration;
private object _result; private object _result;
private Mock<IFileConfigurationRepository> _repo; private Mock<IFileConfigurationRepository> _repo;
public FileConfigurationSetterTests() public FileConfigurationSetterTests()
{ {
_repo = new Mock<IFileConfigurationRepository>(); _repo = new Mock<IFileConfigurationRepository>();
_configRepo = new Mock<IOcelotConfigurationRepository>(); _configRepo = new Mock<IInternalConfigurationRepository>();
_configCreator = new Mock<IOcelotConfigurationCreator>(); _configCreator = new Mock<IInternalConfigurationCreator>();
_configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object, _repo.Object); _configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object, _repo.Object);
} }
@ -38,11 +38,11 @@ namespace Ocelot.UnitTests.Configuration
{ {
var fileConfig = new FileConfiguration(); var fileConfig = new FileConfiguration();
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build(); var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
var config = new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, "asdf"); var config = new InternalConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, "asdf");
this.Given(x => GivenTheFollowingConfiguration(fileConfig)) this.Given(x => GivenTheFollowingConfiguration(fileConfig))
.And(x => GivenTheRepoReturns(new OkResponse())) .And(x => GivenTheRepoReturns(new OkResponse()))
.And(x => GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(config))) .And(x => GivenTheCreatorReturns(new OkResponse<IInternalConfiguration>(config)))
.When(x => WhenISetTheConfiguration()) .When(x => WhenISetTheConfiguration())
.Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly()) .Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly())
.BDDfy(); .BDDfy();
@ -67,7 +67,7 @@ namespace Ocelot.UnitTests.Configuration
this.Given(x => GivenTheFollowingConfiguration(fileConfig)) this.Given(x => GivenTheFollowingConfiguration(fileConfig))
.And(x => GivenTheRepoReturns(new OkResponse())) .And(x => GivenTheRepoReturns(new OkResponse()))
.And(x => GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>()))) .And(x => GivenTheCreatorReturns(new ErrorResponse<IInternalConfiguration>(It.IsAny<Error>())))
.When(x => WhenISetTheConfiguration()) .When(x => WhenISetTheConfiguration())
.And(x => ThenAnErrorResponseIsReturned()) .And(x => ThenAnErrorResponseIsReturned())
.BDDfy(); .BDDfy();
@ -85,7 +85,7 @@ namespace Ocelot.UnitTests.Configuration
_result.ShouldBeOfType<ErrorResponse>(); _result.ShouldBeOfType<ErrorResponse>();
} }
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> configuration) private void GivenTheCreatorReturns(Response<IInternalConfiguration> configuration)
{ {
_configuration = configuration; _configuration = configuration;
_configCreator _configCreator

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Configuration.Builder; using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Repository; using Ocelot.Configuration.Repository;
@ -14,14 +12,14 @@ namespace Ocelot.UnitTests.Configuration
{ {
public class InMemoryConfigurationRepositoryTests public class InMemoryConfigurationRepositoryTests
{ {
private readonly InMemoryOcelotConfigurationRepository _repo; private readonly InMemoryInternalConfigurationRepository _repo;
private IOcelotConfiguration _config; private IInternalConfiguration _config;
private Response _result; private Response _result;
private Response<IOcelotConfiguration> _getResult; private Response<IInternalConfiguration> _getResult;
public InMemoryConfigurationRepositoryTests() public InMemoryConfigurationRepositoryTests()
{ {
_repo = new InMemoryOcelotConfigurationRepository(); _repo = new InMemoryInternalConfigurationRepository();
} }
[Fact] [Fact]
@ -58,7 +56,7 @@ namespace Ocelot.UnitTests.Configuration
WhenIAddOrReplaceTheConfig(); WhenIAddOrReplaceTheConfig();
} }
private void GivenTheConfigurationIs(IOcelotConfiguration config) private void GivenTheConfigurationIs(IInternalConfiguration config)
{ {
_config = config; _config = config;
} }
@ -73,7 +71,7 @@ namespace Ocelot.UnitTests.Configuration
_result.IsError.ShouldBeFalse(); _result.IsError.ShouldBeFalse();
} }
class FakeConfig : IOcelotConfiguration class FakeConfig : IInternalConfiguration
{ {
private readonly string _downstreamTemplatePath; private readonly string _downstreamTemplatePath;

View File

@ -1,80 +0,0 @@
using System.Collections.Generic;
using Moq;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.Provider;
using Ocelot.Configuration.Repository;
using Ocelot.Errors;
using Ocelot.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
public class OcelotConfigurationProviderTests
{
private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider;
private readonly Mock<IOcelotConfigurationRepository> _configurationRepository;
private Response<IOcelotConfiguration> _result;
public OcelotConfigurationProviderTests()
{
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object);
}
[Fact]
public void should_get_config()
{
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
.When(x => x.WhenIGetTheConfig())
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
.BDDfy();
}
[Fact]
public void should_return_error()
{
this.Given(x => x.GivenTheRepoReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
{
new AnyError()
})))
.When(x => x.WhenIGetTheConfig())
.Then(x => x.TheFollowingIsReturned(
new ErrorResponse<IOcelotConfiguration>(new List<Error>
{
new AnyError()
})))
.BDDfy();
}
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
{
_configurationRepository
.Setup(x => x.Get())
.Returns(config);
}
private void WhenIGetTheConfig()
{
_result = _ocelotConfigurationProvider.Get();
}
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)
{
_result.IsError.ShouldBe(expected.IsError);
}
class AnyError : Error
{
public AnyError()
: base("blamo", OcelotErrorCode.UnknownError)
{
}
}
}
}

View File

@ -325,8 +325,8 @@ namespace Ocelot.UnitTests.DependencyInjection
var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>)); var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>)); var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>));
var instance = (ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance; var instance = (ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance;
var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IOcelotConfiguration>)); var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IInternalConfiguration>));
var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IOcelotConfiguration>)); var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IInternalConfiguration>));
var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>)); var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>));
var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>)); var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>));

View File

@ -1,7 +1,4 @@
using Ocelot.Middleware; namespace Ocelot.UnitTests.DownstreamRouteFinder
using Ocelot.Middleware.Multiplexer;
namespace Ocelot.UnitTests.DownstreamRouteFinder
{ {
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,7 +6,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
using Moq; using Moq;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Configuration.Builder; using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Provider;
using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder;
using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.Finder;
using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamRouteFinder.Middleware;
@ -19,23 +15,26 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
using Ocelot.Configuration.Repository;
using Ocelot.Middleware;
using Ocelot.Middleware.Multiplexer;
public class DownstreamRouteFinderMiddlewareTests public class DownstreamRouteFinderMiddlewareTests
{ {
private readonly Mock<IDownstreamRouteFinder> _finder; private readonly Mock<IDownstreamRouteFinder> _finder;
private readonly Mock<IOcelotConfigurationProvider> _provider; private readonly Mock<IInternalConfigurationRepository> _repo;
private Response<DownstreamRoute> _downstreamRoute; private Response<DownstreamRoute> _downstreamRoute;
private IOcelotConfiguration _config; private IInternalConfiguration _config;
private Mock<IOcelotLoggerFactory> _loggerFactory; private Mock<IOcelotLoggerFactory> _loggerFactory;
private Mock<IOcelotLogger> _logger; private Mock<IOcelotLogger> _logger;
private DownstreamRouteFinderMiddleware _middleware; private readonly DownstreamRouteFinderMiddleware _middleware;
private DownstreamContext _downstreamContext; private readonly DownstreamContext _downstreamContext;
private OcelotRequestDelegate _next; private OcelotRequestDelegate _next;
private readonly Mock<IMultiplexer> _multiplexer; private readonly Mock<IMultiplexer> _multiplexer;
public DownstreamRouteFinderMiddlewareTests() public DownstreamRouteFinderMiddlewareTests()
{ {
_provider = new Mock<IOcelotConfigurationProvider>(); _repo = new Mock<IInternalConfigurationRepository>();
_finder = new Mock<IDownstreamRouteFinder>(); _finder = new Mock<IDownstreamRouteFinder>();
_downstreamContext = new DownstreamContext(new DefaultHttpContext()); _downstreamContext = new DownstreamContext(new DefaultHttpContext());
_loggerFactory = new Mock<IOcelotLoggerFactory>(); _loggerFactory = new Mock<IOcelotLoggerFactory>();
@ -43,13 +42,13 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_loggerFactory.Setup(x => x.CreateLogger<DownstreamRouteFinderMiddleware>()).Returns(_logger.Object); _loggerFactory.Setup(x => x.CreateLogger<DownstreamRouteFinderMiddleware>()).Returns(_logger.Object);
_next = context => Task.CompletedTask; _next = context => Task.CompletedTask;
_multiplexer = new Mock<IMultiplexer>(); _multiplexer = new Mock<IMultiplexer>();
_middleware = new DownstreamRouteFinderMiddleware(_next, _loggerFactory.Object, _finder.Object, _provider.Object, _multiplexer.Object); _middleware = new DownstreamRouteFinderMiddleware(_next, _loggerFactory.Object, _finder.Object, _repo.Object, _multiplexer.Object);
} }
[Fact] [Fact]
public void should_call_scoped_data_repository_correctly() public void should_call_scoped_data_repository_correctly()
{ {
var config = new OcelotConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), ""); var config = new InternalConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "");
var downstreamReRoute = new DownstreamReRouteBuilder() var downstreamReRoute = new DownstreamReRouteBuilder()
.WithDownstreamPathTemplate("any old string") .WithDownstreamPathTemplate("any old string")
@ -74,19 +73,19 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_middleware.Invoke(_downstreamContext).GetAwaiter().GetType(); _middleware.Invoke(_downstreamContext).GetAwaiter().GetType();
} }
private void GivenTheFollowingConfig(IOcelotConfiguration config) private void GivenTheFollowingConfig(IInternalConfiguration config)
{ {
_config = config; _config = config;
_provider _repo
.Setup(x => x.Get()) .Setup(x => x.Get())
.Returns(new OkResponse<IOcelotConfiguration>(_config)); .Returns(new OkResponse<IInternalConfiguration>(_config));
} }
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute) private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
{ {
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute); _downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
_finder _finder
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IOcelotConfiguration>(), It.IsAny<string>())) .Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IInternalConfiguration>(), It.IsAny<string>()))
.Returns(_downstreamRoute); .Returns(_downstreamRoute);
} }

View File

@ -23,7 +23,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
private string _upstreamUrlPath; private string _upstreamUrlPath;
private Response<DownstreamRoute> _result; private Response<DownstreamRoute> _result;
private List<ReRoute> _reRoutesConfig; private List<ReRoute> _reRoutesConfig;
private OcelotConfiguration _config; private InternalConfiguration _config;
private Response<UrlMatch> _match; private Response<UrlMatch> _match;
private string _upstreamHttpMethod; private string _upstreamHttpMethod;
private string _upstreamHost; private string _upstreamHost;
@ -711,7 +711,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
private void GivenTheConfigurationIs(List<ReRoute> reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig) private void GivenTheConfigurationIs(List<ReRoute> reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig)
{ {
_reRoutesConfig = reRoutesConfig; _reRoutesConfig = reRoutesConfig;
_config = new OcelotConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, ""); _config = new InternalConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "");
} }
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath) private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)

View File

@ -9,17 +9,17 @@ namespace Ocelot.UnitTests.Errors
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.Provider;
using Moq; using Moq;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Errors; using Ocelot.Errors;
using Ocelot.Infrastructure.RequestData; using Ocelot.Infrastructure.RequestData;
using Ocelot.Middleware; using Ocelot.Middleware;
using Ocelot.Configuration.Repository;
public class ExceptionHandlerMiddlewareTests public class ExceptionHandlerMiddlewareTests
{ {
bool _shouldThrowAnException; bool _shouldThrowAnException;
private readonly Mock<IOcelotConfigurationProvider> _provider; private readonly Mock<IInternalConfigurationRepository> _configRepo;
private readonly Mock<IRequestScopedDataRepository> _repo; private readonly Mock<IRequestScopedDataRepository> _repo;
private Mock<IOcelotLoggerFactory> _loggerFactory; private Mock<IOcelotLoggerFactory> _loggerFactory;
private Mock<IOcelotLogger> _logger; private Mock<IOcelotLogger> _logger;
@ -29,7 +29,7 @@ namespace Ocelot.UnitTests.Errors
public ExceptionHandlerMiddlewareTests() public ExceptionHandlerMiddlewareTests()
{ {
_provider = new Mock<IOcelotConfigurationProvider>(); _configRepo = new Mock<IInternalConfigurationRepository>();
_repo = new Mock<IRequestScopedDataRepository>(); _repo = new Mock<IRequestScopedDataRepository>();
_downstreamContext = new DownstreamContext(new DefaultHttpContext()); _downstreamContext = new DownstreamContext(new DefaultHttpContext());
_loggerFactory = new Mock<IOcelotLoggerFactory>(); _loggerFactory = new Mock<IOcelotLoggerFactory>();
@ -45,13 +45,13 @@ namespace Ocelot.UnitTests.Errors
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK; context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
}; };
_middleware = new ExceptionHandlerMiddleware(_next, _loggerFactory.Object, _provider.Object, _repo.Object); _middleware = new ExceptionHandlerMiddleware(_next, _loggerFactory.Object, _configRepo.Object, _repo.Object);
} }
[Fact] [Fact]
public void NoDownstreamException() public void NoDownstreamException()
{ {
var config = new OcelotConfiguration(null, null, null, null); var config = new InternalConfiguration(null, null, null, null);
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
.And(_ => GivenTheConfigurationIs(config)) .And(_ => GivenTheConfigurationIs(config))
@ -64,7 +64,7 @@ namespace Ocelot.UnitTests.Errors
[Fact] [Fact]
public void DownstreamException() public void DownstreamException()
{ {
var config = new OcelotConfiguration(null, null, null, null); var config = new InternalConfiguration(null, null, null, null);
this.Given(_ => GivenAnExceptionWillBeThrownDownstream()) this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
.And(_ => GivenTheConfigurationIs(config)) .And(_ => GivenTheConfigurationIs(config))
@ -76,7 +76,7 @@ namespace Ocelot.UnitTests.Errors
[Fact] [Fact]
public void ShouldSetRequestId() public void ShouldSetRequestId()
{ {
var config = new OcelotConfiguration(null, null, null, "requestidkey"); var config = new InternalConfiguration(null, null, null, "requestidkey");
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
.And(_ => GivenTheConfigurationIs(config)) .And(_ => GivenTheConfigurationIs(config))
@ -89,7 +89,7 @@ namespace Ocelot.UnitTests.Errors
[Fact] [Fact]
public void ShouldSetAspDotNetRequestId() public void ShouldSetAspDotNetRequestId()
{ {
var config = new OcelotConfiguration(null, null, null, null); var config = new InternalConfiguration(null, null, null, null);
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
.And(_ => GivenTheConfigurationIs(config)) .And(_ => GivenTheConfigurationIs(config))
@ -133,7 +133,7 @@ namespace Ocelot.UnitTests.Errors
private void GivenTheConfigThrows() private void GivenTheConfigThrows()
{ {
var ex = new Exception("outer", new Exception("inner")); var ex = new Exception("outer", new Exception("inner"));
_provider _configRepo
.Setup(x => x.Get()).Throws(ex); .Setup(x => x.Get()).Throws(ex);
} }
@ -144,8 +144,8 @@ namespace Ocelot.UnitTests.Errors
private void GivenTheConfigReturnsError() private void GivenTheConfigReturnsError()
{ {
var response = new Responses.ErrorResponse<IOcelotConfiguration>(new FakeError()); var response = new Responses.ErrorResponse<IInternalConfiguration>(new FakeError());
_provider _configRepo
.Setup(x => x.Get()).Returns(response); .Setup(x => x.Get()).Returns(response);
} }
@ -154,10 +154,10 @@ namespace Ocelot.UnitTests.Errors
_repo.Verify(x => x.Add(key, value), Times.Once); _repo.Verify(x => x.Add(key, value), Times.Once);
} }
private void GivenTheConfigurationIs(IOcelotConfiguration config) private void GivenTheConfigurationIs(IInternalConfiguration config)
{ {
var response = new Responses.OkResponse<IOcelotConfiguration>(config); var response = new Responses.OkResponse<IInternalConfiguration>(config);
_provider _configRepo
.Setup(x => x.Get()).Returns(response); .Setup(x => x.Get()).Returns(response);
} }