diff --git a/docs/features/administration.rst b/docs/features/administration.rst index 298e410e2..425c958e8 100644 --- a/docs/features/administration.rst +++ b/docs/features/administration.rst @@ -1,8 +1,39 @@ Administration ============== -Ocelot supports changing configuration during runtime via an authenticated HTTP API. The API is authenticated -using bearer tokens that you request from Ocelot iteself. This is provided by the amazing +Ocelot supports changing configuration during runtime via an authenticated HTTP API. This can be authenticated in two ways either using Ocelot's +internal IdentityServer (for authenticating requests to the administration API only) or hooking the administration API authentication into your own +IdentityServer. + +Providing your own IdentityServer +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +All you need to do to hook into your own IdentityServer is add the following to your ConfigureServices method. + +.. code-block:: csharp + + public virtual void ConfigureServices(IServiceCollection services) + { + Action options = o => { + // o.Authority = ; + // o.ApiName = ; + // etc.... + }; + + services + .AddOcelot(Configuration) + .AddAdministration("/administration", options); + } + +You now need to get a token from your IdentityServer and use in subsequent requests to Ocelot's administration API. + +This feature was implemented for `issue 228 `_. It is useful because the IdentityServer authentication +middleware needs the URL of the IdentityServer. If you are using the internal IdentityServer it might not alaways be possible to have the Ocelot URL. + +Internal IdentityServer +^^^^^^^^^^^^^^^^^^^^^^^ + +The API is authenticated using bearer tokens that you request from Ocelot iteself. This is provided by the amazing `Identity Server `_ project that I have been using for a few years now. Check them out. In order to enable the administration section you need to do a few things. First of all add this to your @@ -31,8 +62,6 @@ will need to be changed if you are running Ocelot on a different url to http://l The scripts show you how to request a bearer token from ocelot and then use it to GET the existing configuration and POST a configuration. -Administration running multiple Ocelot's -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If you are running multiple Ocelot's in a cluster then you need to use a certificate to sign the bearer tokens used to access the administration API. In order to do this you need to add two more environmental variables for each Ocelot in the cluster. @@ -44,6 +73,7 @@ In order to do this you need to add two more environmental variables for each Oc Normally Ocelot just uses temporary signing credentials but if you set these environmental variables then it will use the certificate. If all the other Ocelots in the cluster have the same certificate then you are good! + Administration API ^^^^^^^^^^^^^^^^^^ diff --git a/docs/features/authentication.rst b/docs/features/authentication.rst index ff349bdd9..dd78fcfa2 100644 --- a/docs/features/authentication.rst +++ b/docs/features/authentication.rst @@ -135,3 +135,10 @@ Then map the authentication provider key to a ReRoute in your configuration e.g. "AllowedScopes": [] } }] + +Allowed Scopes +^^^^^^^^^^^^^ + +If you add scopes to AllowedScopes Ocelot will get all the user claims (from the token) of the type scope and make sure that the user has all of the scopes in the list. + +This is a way to restrict access to a ReRoute on a per scope basis. \ No newline at end of file diff --git a/docs/introduction/gettingstarted.rst b/docs/introduction/gettingstarted.rst index 65ff432dd..8ced66eba 100644 --- a/docs/introduction/gettingstarted.rst +++ b/docs/introduction/gettingstarted.rst @@ -29,8 +29,8 @@ The following is a very basic configuration.json. It won't do anything but shoul **Program** -Then in your Program.cs you will want to have the following. This can be changed if you -don't wan't to use the default url e.g. UseUrls(someUrls) and should work as long as you keep the WebHostBuilder registration. +Then in your Program.cs you will want to have the following. The main things to note are AddOcelotBaseUrl("http://localhost:5000") (adds the url this instance of Ocelot will run under), +AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot middleware). It is important to call AddOcelotBaseUrl as Ocelot needs to know the URL that it is exposed to the outside world on. .. code-block:: csharp @@ -38,51 +38,33 @@ don't wan't to use the default url e.g. UseUrls(someUrls) and should work as lon { public static void Main(string[] args) { - IWebHostBuilder builder = new WebHostBuilder(); - builder.ConfigureServices(s => { - s.AddSingleton(builder); - }); - builder.UseKestrel() + new WebHostBuilder() + .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); - config.AddJsonFile("configuration.json"); - config.AddEnvironmentVariables(); + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddJsonFile("configuration.json") + .AddEnvironmentVariables() + .AddOcelotBaseUrl("http://localhost:5000"); + }) + .ConfigureServices(s => { + s.AddOcelot(); }) .ConfigureLogging((hostingContext, logging) => { - logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); - logging.AddConsole(); + //add your logging }) .UseIISIntegration() - .UseStartup(); - var host = builder.Build(); - host.Run(); - } - } - -Sadly we need to inject the IWebHostBuilder interface to get the applications scheme, url and port later. I cannot find a better way of doing this at the moment without setting this in a static or some kind of config. - -**Startup** - -An example startup using a json file for configuration can be seen below. This is the most basic startup and Ocelot has quite a few more options. Detailed in the rest of these docs! If you get a stuck a good place to look is at the ManualTests project in the source code. - -.. code-block:: csharp - - public class Startup - { - public void ConfigureServices(IServiceCollection services) - { - services.AddOcelot(); - } - - public void Configure(IApplicationBuilder app) - { - app.UseOcelot().Wait(); + .Configure(app => + { + app.UseOcelot().Wait(); + }) + .Build() + .Run(); } } @@ -109,8 +91,7 @@ The following is a very basic configuration.json. It won't do anything but shoul **Program** -Then in your Program.cs you will want to have the following. This can be changed if you -don't wan't to use the default url e.g. UseUrls(someUrls) and should work as long as you keep the WebHostBuilder registration. +Then in your Program.cs you will want to have the following. .. code-block:: csharp @@ -121,7 +102,6 @@ don't wan't to use the default url e.g. UseUrls(someUrls) and should work as lon IWebHostBuilder builder = new WebHostBuilder(); builder.ConfigureServices(s => { - s.AddSingleton(builder); }); builder.UseKestrel() @@ -134,8 +114,6 @@ don't wan't to use the default url e.g. UseUrls(someUrls) and should work as lon } } -Sadly we need to inject the IWebHostBuilder interface to get the applications scheme, url and port later. I cannot find a better way of doing this at the moment without setting this in a static or some kind of config. - **Startup** An example startup using a json file for configuration can be seen below. @@ -151,7 +129,8 @@ An example startup using a json file for configuration can be seen below. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile("configuration.json") - .AddEnvironmentVariables(); + .AddEnvironmentVariables() + .AddOcelotBaseUrl("http://localhost:5000"); Configuration = builder.Build(); } diff --git a/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs b/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs index 3820de4e2..7ab33e907 100644 --- a/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs +++ b/src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.cs @@ -8,15 +8,13 @@ namespace Ocelot.Configuration.Creator public class HeaderFindAndReplaceCreator : IHeaderFindAndReplaceCreator { private IBaseUrlFinder _finder; - private Dictionary> _placeholders; + private readonly Dictionary> _placeholders; public HeaderFindAndReplaceCreator(IBaseUrlFinder finder) { _finder = finder; _placeholders = new Dictionary>(); - _placeholders.Add("{BaseUrl}", () => { - return _finder.Find(); - }); + _placeholders.Add("{BaseUrl}", () => _finder.Find()); } public HeaderTransformations Create(FileReRoute fileReRoute) diff --git a/src/Ocelot/Configuration/OcelotConfiguration.cs b/src/Ocelot/Configuration/OcelotConfiguration.cs index 35f956ecf..1ab73b87d 100644 --- a/src/Ocelot/Configuration/OcelotConfiguration.cs +++ b/src/Ocelot/Configuration/OcelotConfiguration.cs @@ -17,4 +17,4 @@ public OcelotConfiguration(List reRoutes, string administrationPath, Se public ServiceProviderConfiguration ServiceProviderConfiguration {get;} public string RequestId {get;} } -} \ No newline at end of file +} diff --git a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs new file mode 100644 index 000000000..c525f67ff --- /dev/null +++ b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Memory; + +namespace Ocelot.DependencyInjection +{ + public static class ConfigurationBuilderExtensions + { + public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder builder, string baseUrl) + { + var memorySource = new MemoryConfigurationSource(); + memorySource.InitialData = new List> + { + new KeyValuePair("BaseUrl", baseUrl) + }; + builder.Add(memorySource); + return builder; + } + } +} diff --git a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs index d2f92101a..48db5ef7a 100644 --- a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs @@ -2,6 +2,7 @@ using CacheManager.Core; using System; using System.Net.Http; +using IdentityServer4.AccessTokenValidation; namespace Ocelot.DependencyInjection { @@ -9,8 +10,9 @@ public interface IOcelotBuilder { IOcelotBuilder AddStoreOcelotConfigurationInConsul(); IOcelotBuilder AddCacheManager(Action settings); - IOcelotBuilder AddOpenTracing(Action settings); + IOcelotBuilder AddOpenTracing(Action settings); IOcelotAdministrationBuilder AddAdministration(string path, string secret); + IOcelotAdministrationBuilder AddAdministration(string path, Action configOptions); IOcelotBuilder AddDelegatingHandler(Func delegatingHandler); } } diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 10f9d082b..d51b8a8dd 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -1,66 +1,68 @@ -using CacheManager.Core; -using IdentityServer4.Models; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Ocelot.Authorisation; -using Ocelot.Cache; -using Ocelot.Claims; -using Ocelot.Configuration.Authentication; -using Ocelot.Configuration.Creator; -using Ocelot.Configuration.File; -using Ocelot.Configuration.Parser; -using Ocelot.Configuration.Provider; -using Ocelot.Configuration.Repository; -using Ocelot.Configuration.Setter; -using Ocelot.Configuration.Validator; -using Ocelot.DownstreamRouteFinder.Finder; -using Ocelot.DownstreamRouteFinder.UrlMatcher; -using Ocelot.DownstreamUrlCreator; -using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; -using Ocelot.Headers; -using Ocelot.Infrastructure.Claims.Parser; -using Ocelot.Infrastructure.RequestData; -using Ocelot.LoadBalancer.LoadBalancers; -using Ocelot.Logging; -using Ocelot.Middleware; -using Ocelot.QueryStrings; -using Ocelot.RateLimit; -using Ocelot.Request.Builder; -using Ocelot.Request.Mapper; -using Ocelot.Requester; -using Ocelot.Requester.QoS; -using Ocelot.Responder; -using Ocelot.ServiceDiscovery; -using System; -using System.Collections.Generic; -using System.IdentityModel.Tokens.Jwt; -using System.Reflection; -using System.Security.Cryptography.X509Certificates; -using IdentityServer4.AccessTokenValidation; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Ocelot.Configuration; -using Ocelot.Configuration.Builder; -using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider; -using Microsoft.Extensions.DependencyInjection.Extensions; -using System.Linq; -using System.Net.Http; -using Butterfly.Client.AspNetCore; +using Microsoft.Extensions.Options; namespace Ocelot.DependencyInjection { + using CacheManager.Core; + using IdentityServer4.Models; + using Microsoft.AspNetCore.Http; + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.DependencyInjection; + using Ocelot.Authorisation; + using Ocelot.Cache; + using Ocelot.Claims; + using Ocelot.Configuration.Authentication; + using Ocelot.Configuration.Creator; + using Ocelot.Configuration.File; + using Ocelot.Configuration.Parser; + using Ocelot.Configuration.Provider; + using Ocelot.Configuration.Repository; + using Ocelot.Configuration.Setter; + using Ocelot.Configuration.Validator; + using Ocelot.DownstreamRouteFinder.Finder; + using Ocelot.DownstreamRouteFinder.UrlMatcher; + using Ocelot.DownstreamUrlCreator; + using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; + using Ocelot.Headers; + using Ocelot.Infrastructure.Claims.Parser; + using Ocelot.Infrastructure.RequestData; + using Ocelot.LoadBalancer.LoadBalancers; + using Ocelot.Logging; + using Ocelot.Middleware; + using Ocelot.QueryStrings; + using Ocelot.RateLimit; + using Ocelot.Request.Builder; + using Ocelot.Request.Mapper; + using Ocelot.Requester; + using Ocelot.Requester.QoS; + using Ocelot.Responder; + using Ocelot.ServiceDiscovery; + using System; + using System.Collections.Generic; + using System.IdentityModel.Tokens.Jwt; + using System.Reflection; + using System.Security.Cryptography.X509Certificates; + using IdentityServer4.AccessTokenValidation; + using Microsoft.AspNetCore.Builder; + using Microsoft.AspNetCore.Hosting; + using Ocelot.Configuration; + using Ocelot.Configuration.Builder; + using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider; + using Microsoft.Extensions.DependencyInjection.Extensions; + using System.Linq; + using System.Net.Http; + using Butterfly.Client.AspNetCore; + public class OcelotBuilder : IOcelotBuilder { private readonly IServiceCollection _services; private readonly IConfiguration _configurationRoot; - private IDelegatingHandlerHandlerProvider _provider; - + private readonly IDelegatingHandlerHandlerProvider _provider; + public OcelotBuilder(IServiceCollection services, IConfiguration configurationRoot) { _configurationRoot = configurationRoot; _services = services; - + //add default cache settings... Action defaultCachingSettings = x => { @@ -109,7 +111,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); - _services.TryAddSingleton(); + _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); @@ -166,6 +168,21 @@ public IOcelotAdministrationBuilder AddAdministration(string path, string secret return new OcelotAdministrationBuilder(_services, _configurationRoot); } + public IOcelotAdministrationBuilder AddAdministration(string path, Action configureOptions) + { + var administrationPath = new AdministrationPath(path); + + if (configureOptions != null) + { + AddIdentityServer(configureOptions); + } + + //todo - hack because we add this earlier so it always exists for some reason...investigate.. + var descriptor = new ServiceDescriptor(typeof(IAdministrationPath), administrationPath); + _services.Replace(descriptor); + return new OcelotAdministrationBuilder(_services, _configurationRoot); + } + public IOcelotBuilder AddDelegatingHandler(Func delegatingHandler) { _provider.Add(delegatingHandler); @@ -221,6 +238,13 @@ public IOcelotBuilder AddCacheManager(Action sett return this; } + private void AddIdentityServer(Action configOptions) + { + _services + .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) + .AddIdentityServerAuthentication(configOptions); + } + private void AddIdentityServer(IIdentityServerConfiguration identityServerConfiguration, IAdministrationPath adminPath) { _services.TryAddSingleton(identityServerConfiguration); @@ -232,11 +256,10 @@ private void AddIdentityServer(IIdentityServerConfiguration identityServerConfig .AddInMemoryApiResources(Resources(identityServerConfiguration)) .AddInMemoryClients(Client(identityServerConfiguration)); - //todo - refactor a method so we know why this is happening - var whb = _services.First(x => x.ServiceType == typeof(IWebHostBuilder)); - var urlFinder = new BaseUrlFinder((IWebHostBuilder)whb.ImplementationInstance); + var urlFinder = new BaseUrlFinder(_configurationRoot); var baseSchemeUrlAndPort = urlFinder.Find(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); + _services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(o => diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index 51caf3bbc..e3f906653 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -1,6 +1,6 @@ -using Microsoft.Extensions.Configuration; +using System; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using System.Linq; namespace Ocelot.DependencyInjection diff --git a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs index 54461bbd1..a08031ea8 100644 --- a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs @@ -21,7 +21,6 @@ public class ExceptionHandlerMiddleware : OcelotMiddleware private readonly IRequestScopedDataRepository _requestScopedDataRepository; private readonly IOcelotConfigurationProvider _configProvider; - public ExceptionHandlerMiddleware(RequestDelegate next, IOcelotLoggerFactory loggerFactory, IRequestScopedDataRepository requestScopedDataRepository, diff --git a/src/Ocelot/Middleware/BaseUrlFinder.cs b/src/Ocelot/Middleware/BaseUrlFinder.cs index 63672501c..de24e6827 100644 --- a/src/Ocelot/Middleware/BaseUrlFinder.cs +++ b/src/Ocelot/Middleware/BaseUrlFinder.cs @@ -1,21 +1,21 @@ -using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; namespace Ocelot.Middleware { public class BaseUrlFinder : IBaseUrlFinder { - private readonly IWebHostBuilder _webHostBuilder; + private readonly IConfiguration _config; - public BaseUrlFinder(IWebHostBuilder webHostBuilder) + public BaseUrlFinder(IConfiguration config) { - _webHostBuilder = webHostBuilder; + _config = config; } public string Find() { - var baseSchemeUrlAndPort = _webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey); + var baseUrl = _config.GetValue("BaseUrl", ""); - return string.IsNullOrEmpty(baseSchemeUrlAndPort) ? "http://localhost:5000" : baseSchemeUrlAndPort; + return string.IsNullOrEmpty(baseUrl) ? "http://localhost:5000" : baseUrl; } } } diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index 5ac19cf2f..249dceb5b 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -1,36 +1,14 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Reflection; -using IdentityServer4.AccessTokenValidation; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Ocelot.Authentication.Middleware; -using Ocelot.Cache.Middleware; -using Ocelot.Claims.Middleware; -using Ocelot.DownstreamRouteFinder.Middleware; -using Ocelot.DownstreamUrlCreator.Middleware; -using Ocelot.Errors.Middleware; -using Ocelot.Headers.Middleware; -using Ocelot.Logging; -using Ocelot.QueryStrings.Middleware; -using Ocelot.Request.Middleware; -using Ocelot.Requester.Middleware; -using Ocelot.RequestId.Middleware; -using Ocelot.Responder.Middleware; -using Ocelot.RateLimit.Middleware; - -namespace Ocelot.Middleware +namespace Ocelot.Middleware { using System; - using System.IO; using System.Linq; using System.Threading.Tasks; using Authorisation.Middleware; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; - using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; - using Newtonsoft.Json; + using System.Diagnostics; + using Microsoft.AspNetCore.Builder; using Ocelot.Configuration; using Ocelot.Configuration.Creator; using Ocelot.Configuration.File; @@ -38,8 +16,21 @@ namespace Ocelot.Middleware using Ocelot.Configuration.Repository; using Ocelot.Configuration.Setter; using Ocelot.LoadBalancer.Middleware; - using Ocelot.Raft; using Ocelot.Responses; + using Ocelot.Authentication.Middleware; + using Ocelot.Cache.Middleware; + using Ocelot.Claims.Middleware; + using Ocelot.DownstreamRouteFinder.Middleware; + using Ocelot.DownstreamUrlCreator.Middleware; + using Ocelot.Errors.Middleware; + using Ocelot.Headers.Middleware; + using Ocelot.Logging; + using Ocelot.QueryStrings.Middleware; + using Ocelot.Request.Middleware; + using Ocelot.Requester.Middleware; + using Ocelot.RequestId.Middleware; + using Ocelot.Responder.Middleware; + using Ocelot.RateLimit.Middleware; using Rafty.Concensus; using Rafty.Infrastructure; @@ -67,7 +58,7 @@ public static async Task UseOcelot(this IApplicationBuilder { var configuration = await CreateConfiguration(builder); - await CreateAdministrationArea(builder, configuration); + CreateAdministrationArea(builder, configuration); if(UsingRafty(builder)) { @@ -290,15 +281,19 @@ private static async Task SetUpConfigFromConsul(IApplicationBuilder bu return new OkResponse(); } - private static async Task CreateAdministrationArea(IApplicationBuilder builder, IOcelotConfiguration configuration) + private static void CreateAdministrationArea(IApplicationBuilder builder, IOcelotConfiguration configuration) { - var identityServerConfiguration = (IIdentityServerConfiguration)builder.ApplicationServices.GetService(typeof(IIdentityServerConfiguration)); - - if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null) + if(!string.IsNullOrEmpty(configuration.AdministrationPath)) { builder.Map(configuration.AdministrationPath, app => { - app.UseIdentityServer(); + //todo - hack so we know that we are using internal identity server + var identityServerConfiguration = (IIdentityServerConfiguration)builder.ApplicationServices.GetService(typeof(IIdentityServerConfiguration)); + if (identityServerConfiguration != null) + { + app.UseIdentityServer(); + } + app.UseAuthentication(); app.UseMvc(); }); diff --git a/src/Ocelot/Raft/FilePeersProvider.cs b/src/Ocelot/Raft/FilePeersProvider.cs index 2718ae189..aeca7b2ce 100644 --- a/src/Ocelot/Raft/FilePeersProvider.cs +++ b/src/Ocelot/Raft/FilePeersProvider.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using Ocelot.Configuration; using Ocelot.Configuration.Provider; +using Ocelot.Middleware; using Rafty.Concensus; using Rafty.Infrastructure; @@ -15,15 +16,15 @@ public class FilePeersProvider : IPeersProvider { private readonly IOptions _options; private List _peers; - private IWebHostBuilder _builder; + private IBaseUrlFinder _finder; private IOcelotConfigurationProvider _provider; private IIdentityServerConfiguration _identityServerConfig; - public FilePeersProvider(IOptions options, IWebHostBuilder builder, IOcelotConfigurationProvider provider, IIdentityServerConfiguration identityServerConfig) + public FilePeersProvider(IOptions options, IBaseUrlFinder finder, IOcelotConfigurationProvider provider, IIdentityServerConfiguration identityServerConfig) { _identityServerConfig = identityServerConfig; _provider = provider; - _builder = builder; + _finder = finder; _options = options; _peers = new List(); //todo - sort out async nonsense.. @@ -32,7 +33,7 @@ public FilePeersProvider(IOptions options, IWebHostBuilder builder, I { var httpClient = new HttpClient(); //todo what if this errors? - var httpPeer = new HttpPeer(item.HostAndPort, httpClient, _builder, config.Data, _identityServerConfig); + var httpPeer = new HttpPeer(item.HostAndPort, httpClient, _finder, config.Data, _identityServerConfig); _peers.Add(httpPeer); } } diff --git a/src/Ocelot/Raft/HttpPeer.cs b/src/Ocelot/Raft/HttpPeer.cs index 67cc8e151..ef76a5836 100644 --- a/src/Ocelot/Raft/HttpPeer.cs +++ b/src/Ocelot/Raft/HttpPeer.cs @@ -7,6 +7,7 @@ using Ocelot.Authentication; using Ocelot.Configuration; using Ocelot.Configuration.Provider; +using Ocelot.Middleware; using Rafty.Concensus; using Rafty.FiniteStateMachine; @@ -23,7 +24,7 @@ public class HttpPeer : IPeer private IOcelotConfiguration _config; private IIdentityServerConfiguration _identityServerConfiguration; - public HttpPeer(string hostAndPort, HttpClient httpClient, IWebHostBuilder builder, IOcelotConfiguration config, IIdentityServerConfiguration identityServerConfiguration) + public HttpPeer(string hostAndPort, HttpClient httpClient, IBaseUrlFinder finder, IOcelotConfiguration config, IIdentityServerConfiguration identityServerConfiguration) { _identityServerConfiguration = identityServerConfiguration; _config = config; @@ -33,7 +34,7 @@ public HttpPeer(string hostAndPort, HttpClient httpClient, IWebHostBuilder build _jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }; - _baseSchemeUrlAndPort = builder.GetSetting(WebHostDefaults.ServerUrlsKey); + _baseSchemeUrlAndPort = finder.Find(); } public string Id {get; private set;} diff --git a/src/Ocelot/Raft/RaftController.cs b/src/Ocelot/Raft/RaftController.cs index 7177044b1..eb91e86f6 100644 --- a/src/Ocelot/Raft/RaftController.cs +++ b/src/Ocelot/Raft/RaftController.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Ocelot.Logging; +using Ocelot.Middleware; using Ocelot.Raft; using Rafty.Concensus; using Rafty.FiniteStateMachine; @@ -23,12 +24,12 @@ public class RaftController : Controller private string _baseSchemeUrlAndPort; private JsonSerializerSettings _jsonSerialiserSettings; - public RaftController(INode node, IOcelotLoggerFactory loggerFactory, IWebHostBuilder builder) + public RaftController(INode node, IOcelotLoggerFactory loggerFactory, IBaseUrlFinder finder) { _jsonSerialiserSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; - _baseSchemeUrlAndPort = builder.GetSetting(WebHostDefaults.ServerUrlsKey); + _baseSchemeUrlAndPort = finder.Find(); _logger = loggerFactory.CreateLogger(); _node = node; } @@ -81,4 +82,4 @@ public async Task Command() } } } -} \ No newline at end of file +} diff --git a/test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs b/test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs deleted file mode 100644 index c3cbf1d05..000000000 --- a/test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; - -namespace Ocelot.AcceptanceTests -{ - public class AcceptanceTestsStartup - { - public AcceptanceTestsStartup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("configuration.json") - .AddEnvironmentVariables(); - - Configuration = builder.Build(); - } - - public IConfiguration Configuration { get; } - - public virtual void ConfigureServices(IServiceCollection services) - { - services.AddOcelot(Configuration); - } - - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) - { - app.UseOcelot().Wait(); - } - } -} diff --git a/test/Ocelot.AcceptanceTests/ConsulStartup.cs b/test/Ocelot.AcceptanceTests/ConsulStartup.cs deleted file mode 100644 index b7eb569b2..000000000 --- a/test/Ocelot.AcceptanceTests/ConsulStartup.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using CacheManager.Core; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; - -namespace Ocelot.AcceptanceTests -{ - public class ConsulStartup - { - public ConsulStartup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("configuration.json") - .AddEnvironmentVariables(); - - Config = builder.Build(); - } - - public static IConfiguration Config { get; private set; } - - public void ConfigureServices(IServiceCollection services) - { - services.AddOcelot(Config).AddStoreOcelotConfigurationInConsul(); - } - - public void Configure(IApplicationBuilder app) - { - app.UseOcelot().Wait(); - } - } -} diff --git a/test/Ocelot.AcceptanceTests/StartupWithConsulAndCustomCacheHandle.cs b/test/Ocelot.AcceptanceTests/StartupWithConsulAndCustomCacheHandle.cs deleted file mode 100644 index e48efcdb5..000000000 --- a/test/Ocelot.AcceptanceTests/StartupWithConsulAndCustomCacheHandle.cs +++ /dev/null @@ -1,29 +0,0 @@ -using CacheManager.Core; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Ocelot.DependencyInjection; -using Ocelot.AcceptanceTests.Caching; - -namespace Ocelot.AcceptanceTests -{ - public class StartupWithConsulAndCustomCacheHandle : AcceptanceTestsStartup - { - public StartupWithConsulAndCustomCacheHandle(IHostingEnvironment env) : base(env) { } - - public override void ConfigureServices(IServiceCollection services) - { - services.AddOcelot(Configuration) - .AddCacheManager((x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithJsonSerializer() - .WithHandle(typeof(InMemoryJsonHandle<>)); - }) - .AddStoreOcelotConfigurationInConsul(); - } - } -} diff --git a/test/Ocelot.AcceptanceTests/StartupWithCustomCacheHandle.cs b/test/Ocelot.AcceptanceTests/StartupWithCustomCacheHandle.cs deleted file mode 100644 index 2570bc93f..000000000 --- a/test/Ocelot.AcceptanceTests/StartupWithCustomCacheHandle.cs +++ /dev/null @@ -1,28 +0,0 @@ -using CacheManager.Core; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Ocelot.DependencyInjection; -using Ocelot.AcceptanceTests.Caching; - -namespace Ocelot.AcceptanceTests -{ - public class StartupWithCustomCacheHandle : AcceptanceTestsStartup - { - public StartupWithCustomCacheHandle(IHostingEnvironment env) : base(env) { } - - public override void ConfigureServices(IServiceCollection services) - { - services.AddOcelot(Configuration) - .AddCacheManager((x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithJsonSerializer() - .WithHandle(typeof(InMemoryJsonHandle<>)); - }); - } - } -} diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index 218e58e73..b33778e77 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -38,9 +38,11 @@ public class Steps : IDisposable public string RequestIdKey = "OcRequestId"; private readonly Random _random; private IWebHostBuilder _webHostBuilder; + private readonly string _baseUrl; public Steps() { + _baseUrl = "http://localhost:5000"; _random = new Random(); } @@ -77,13 +79,26 @@ public void GivenOcelotIsRunning() { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddOcelot(); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); - _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); } @@ -95,25 +110,27 @@ public void GivenOcelotIsRunningWithHandlers(DelegatingHandler handlerOne, Deleg { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - s.AddOcelot() - .AddDelegatingHandler(() => handlerOne) - .AddDelegatingHandler(() => handlerTwo); - }); - _webHostBuilder.ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); - config.AddJsonFile("configuration.json"); - config.AddEnvironmentVariables(); - }).Configure(a => - { - a.UseOcelot().Wait(); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddSingleton(_webHostBuilder); + s.AddOcelot() + .AddDelegatingHandler(() => handlerOne) + .AddDelegatingHandler(() => handlerTwo); + }) + .Configure(a => + { + a.UseOcelot().Wait(); + }); _ocelotServer = new TestServer(_webHostBuilder); @@ -127,15 +144,29 @@ public void GivenOcelotIsRunning(Action opt { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - s.AddAuthentication() - .AddIdentityServerAuthentication(authenticationProviderKey, options); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_baseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddOcelot(); + s.AddAuthentication() + .AddIdentityServerAuthentication(authenticationProviderKey, options); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); - _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); } @@ -150,13 +181,36 @@ public void GivenOcelotIsRunningUsingJsonSerializedCache() { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_baseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddOcelot() + .AddCacheManager((x) => + { + x.WithMicrosoftLogging(log => + { + log.AddConsole(LogLevel.Debug); + }) + .WithJsonSerializer() + .WithHandle(typeof(InMemoryJsonHandle<>)); + }); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); - _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); } @@ -165,13 +219,27 @@ public void GivenOcelotIsRunningUsingConsulToStoreConfig() { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_baseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddOcelot().AddStoreOcelotConfigurationInConsul(); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); - _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); } @@ -180,13 +248,37 @@ public void GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache() { _webHostBuilder = new WebHostBuilder(); - _webHostBuilder.ConfigureServices(s => - { - s.AddSingleton(_webHostBuilder); - }); + _webHostBuilder + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_baseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(s => + { + s.AddOcelot() + .AddCacheManager((x) => + { + x.WithMicrosoftLogging(log => + { + log.AddConsole(LogLevel.Debug); + }) + .WithJsonSerializer() + .WithHandle(typeof(InMemoryJsonHandle<>)); + }) + .AddStoreOcelotConfigurationInConsul(); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); - _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + _ocelotServer = new TestServer(_webHostBuilder); _ocelotClient = _ocelotServer.CreateClient(); } diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index 44f394c0b..cddd43689 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -4,11 +4,21 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Security.Claims; +using CacheManager.Core; +using IdentityServer4.AccessTokenValidation; +using IdentityServer4.Models; +using IdentityServer4.Test; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Ocelot.Cache; using Ocelot.Configuration.File; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; using Shouldly; using TestStack.BDDfy; using Xunit; @@ -18,15 +28,16 @@ namespace Ocelot.IntegrationTests { public class AdministrationTests : IDisposable { - private readonly HttpClient _httpClient; + private HttpClient _httpClient; private readonly HttpClient _httpClientTwo; private HttpResponseMessage _response; private IWebHost _builder; private IWebHostBuilder _webHostBuilder; - private readonly string _ocelotBaseUrl; + private string _ocelotBaseUrl; private BearerToken _token; private IWebHostBuilder _webHostBuilderTwo; private IWebHost _builderTwo; + private IWebHost _identityServerBuilder; public AdministrationTests() { @@ -62,6 +73,24 @@ public void should_return_response_200_with_call_re_routes_controller() .BDDfy(); } + [Fact] + public void should_return_response_200_with_call_re_routes_controller_using_base_url_added_in_memory_with_no_webhostbuilder_registered() + { + _httpClient = new HttpClient(); + _ocelotBaseUrl = "http://localhost:5011"; + _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); + + var configuration = new FileConfiguration(); + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenOcelotIsRunningWithNoWebHostBuilder(_ocelotBaseUrl)) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + [Fact] public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b() { @@ -304,6 +333,115 @@ public void should_clear_region() .BDDfy(); } + [Fact] + public void should_return_response_200_with_call_re_routes_controller_when_using_own_identity_server_to_secure_admin_area() + { + var configuration = new FileConfiguration(); + + var identityServerRootUrl = "http://localhost:5123"; + + Action options = o => { + o.Authority = identityServerRootUrl; + o.ApiName = "api"; + o.RequireHttpsMetadata = false; + o.SupportedTokens = SupportedTokens.Both; + o.ApiSecret = "secret"; + }; + + this.Given(x => GivenThereIsAConfiguration(configuration)) + .And(x => GivenThereIsAnIdentityServerOn(identityServerRootUrl, "api")) + .And(x => GivenOcelotIsRunningWithIdentityServerSettings(options)) + .And(x => GivenIHaveAToken(identityServerRootUrl)) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .BDDfy(); + } + + private void GivenIHaveAToken(string url) + { + var formData = new List> + { + new KeyValuePair("client_id", "api"), + new KeyValuePair("client_secret", "secret"), + new KeyValuePair("scope", "api"), + new KeyValuePair("username", "test"), + new KeyValuePair("password", "test"), + new KeyValuePair("grant_type", "password") + }; + var content = new FormUrlEncodedContent(formData); + + using (var httpClient = new HttpClient()) + { + var response = httpClient.PostAsync($"{url}/connect/token", content).Result; + var responseContent = response.Content.ReadAsStringAsync().Result; + response.EnsureSuccessStatusCode(); + _token = JsonConvert.DeserializeObject(responseContent); + } + } + + private void GivenThereIsAnIdentityServerOn(string url, string apiName) + { + _identityServerBuilder = new WebHostBuilder() + .UseUrls(url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureServices(services => + { + services.AddLogging(); + services.AddIdentityServer() + .AddDeveloperSigningCredential() + .AddInMemoryApiResources(new List + { + new ApiResource + { + Name = apiName, + Description = apiName, + Enabled = true, + DisplayName = apiName, + Scopes = new List() + { + new Scope(apiName) + } + } + }) + .AddInMemoryClients(new List + { + new Client + { + ClientId = apiName, + AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, + ClientSecrets = new List {new Secret("secret".Sha256())}, + AllowedScopes = new List { apiName }, + AccessTokenType = AccessTokenType.Jwt, + Enabled = true + } + }) + .AddTestUsers(new List + { + new TestUser + { + Username = "test", + Password = "test", + SubjectId = "1231231" + } + }); + }) + .Configure(app => + { + app.UseIdentityServer(); + }) + .Build(); + + _identityServerBuilder.Start(); + + using (var httpClient = new HttpClient()) + { + var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result; + response.EnsureSuccessStatusCode(); + } + } + private void GivenAnotherOcelotIsRunning(string baseUrl) { _httpClientTwo.BaseAddress = new Uri(baseUrl); @@ -312,10 +450,36 @@ private void GivenAnotherOcelotIsRunning(string baseUrl) .UseUrls(baseUrl) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureServices(x => { - x.AddSingleton(_webHostBuilderTwo); + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(baseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + Action settings = (s) => + { + s.WithMicrosoftLogging(log => + { + log.AddConsole(LogLevel.Debug); + }) + .WithDictionaryHandle(); + }; + + x.AddOcelot() + .AddCacheManager(settings) + .AddAdministration("/administration", "secret"); }) - .UseStartup(); + .Configure(app => + { + app.UseOcelot().Wait(); + }); + _builderTwo = _webHostBuilderTwo.Build(); @@ -400,18 +564,110 @@ private void GivenIHaveAnOcelotToken(string adminPath) response.EnsureSuccessStatusCode(); } + private void GivenOcelotIsRunningWithIdentityServerSettings(Action configOptions) + { + _webHostBuilder = new WebHostBuilder() + .UseUrls(_ocelotBaseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => { + x.AddSingleton(_webHostBuilder); + x.AddOcelot() + .AddCacheManager(c => + { + c.WithDictionaryHandle(); + }) + .AddAdministration("/administration", configOptions); + }) + .Configure(app => { + app.UseOcelot().Wait(); + }); + + _builder = _webHostBuilder.Build(); + + _builder.Start(); + } + private void GivenOcelotIsRunning() { _webHostBuilder = new WebHostBuilder() .UseUrls(_ocelotBaseUrl) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_ocelotBaseUrl); + config.AddEnvironmentVariables(); + }) + .ConfigureServices(x => + { + Action settings = (s) => + { + s.WithMicrosoftLogging(log => + { + log.AddConsole(LogLevel.Debug); + }) + .WithDictionaryHandle(); + }; + + x.AddOcelot() + .AddCacheManager(settings) + .AddAdministration("/administration", "secret"); + }) + .Configure(app => + { + app.UseOcelot().Wait(); + }); + + _builder = _webHostBuilder.Build(); + + _builder.Start(); + } + + private void GivenOcelotIsRunningWithNoWebHostBuilder(string baseUrl) + { + _webHostBuilder = new WebHostBuilder() + .UseUrls(_ocelotBaseUrl) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(baseUrl); + config.AddEnvironmentVariables(); + }) .ConfigureServices(x => { x.AddSingleton(_webHostBuilder); + x.AddOcelot() + .AddCacheManager(c => + { + c.WithDictionaryHandle(); + }) + .AddAdministration("/administration", "secret"); }) - .UseStartup(); + .Configure(app => { + app.UseOcelot().Wait(); + }); - _builder = _webHostBuilder.Build(); + _builder = _webHostBuilder.Build(); _builder.Start(); } @@ -464,6 +720,7 @@ public void Dispose() Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", ""); _builder?.Dispose(); _httpClient?.Dispose(); + _identityServerBuilder?.Dispose(); } } } diff --git a/test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs b/test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs deleted file mode 100644 index 8f1906d03..000000000 --- a/test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using CacheManager.Core; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; - -namespace Ocelot.IntegrationTests -{ - public class IntegrationTestsStartup - { - public IntegrationTestsStartup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("configuration.json") - .AddEnvironmentVariables(); - - Configuration = builder.Build(); - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - Action settings = (x) => - { - x.WithMicrosoftLogging(log => - { - log.AddConsole(LogLevel.Debug); - }) - .WithDictionaryHandle(); - }; - - services.AddOcelot(Configuration) - .AddCacheManager(settings) - .AddAdministration("/administration", "secret"); - } - - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) - { - app.UseOcelot().Wait(); - } - } -} \ No newline at end of file diff --git a/test/Ocelot.IntegrationTests/RaftStartup.cs b/test/Ocelot.IntegrationTests/RaftStartup.cs deleted file mode 100644 index bb9f26d9a..000000000 --- a/test/Ocelot.IntegrationTests/RaftStartup.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; -using Ocelot.Raft; -using Rafty.Concensus; -using Rafty.FiniteStateMachine; -using Rafty.Infrastructure; -using Rafty.Log; -using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder; - -namespace Ocelot.IntegrationTests -{ - public class RaftStartup - { - public RaftStartup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("peers.json", optional: true, reloadOnChange: true) - .AddJsonFile("configuration.json") - .AddEnvironmentVariables(); - - Configuration = builder.Build(); - } - - public IConfiguration Configuration { get; } - - public virtual void ConfigureServices(IServiceCollection services) - { - services - .AddOcelot(Configuration) - .AddAdministration("/administration", "secret") - .AddRafty() - ; - } - - public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) - { - app.UseOcelot().Wait(); - } - } -} diff --git a/test/Ocelot.IntegrationTests/RaftTests.cs b/test/Ocelot.IntegrationTests/RaftTests.cs index 2e1752de6..1517e879e 100644 --- a/test/Ocelot.IntegrationTests/RaftTests.cs +++ b/test/Ocelot.IntegrationTests/RaftTests.cs @@ -17,6 +17,9 @@ using Xunit; using static Rafty.Infrastructure.Wait; using Microsoft.Data.Sqlite; +using Microsoft.Extensions.Configuration; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; namespace Ocelot.IntegrationTests { @@ -28,7 +31,6 @@ public class RaftTests : IDisposable private FilePeers _peers; private readonly HttpClient _httpClient; private readonly HttpClient _httpClientForAssertions; - private string _ocelotBaseUrl; private BearerToken _token; private HttpResponseMessage _response; private static readonly object _lock = new object(); @@ -37,8 +39,8 @@ public RaftTests() { _httpClientForAssertions = new HttpClient(); _httpClient = new HttpClient(); - _ocelotBaseUrl = "http://localhost:5000"; - _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); + var ocelotBaseUrl = "http://localhost:5000"; + _httpClient.BaseAddress = new Uri(ocelotBaseUrl); _webHostBuilders = new List(); _builders = new List(); _threads = new List(); @@ -331,12 +333,29 @@ private void GivenAServerIsRunning(string url) webHostBuilder.UseUrls(url) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddJsonFile("peers.json", optional: true, reloadOnChange: true); + config.AddOcelotBaseUrl(url); + config.AddEnvironmentVariables(); + }) .ConfigureServices(x => { - x.AddSingleton(webHostBuilder); x.AddSingleton(new NodeId(url)); + x + .AddOcelot() + .AddAdministration("/administration", "secret") + .AddRafty(); }) - .UseStartup(); + .Configure(app => + { + app.UseOcelot().Wait(); + }); var builder = webHostBuilder.Build(); builder.Start(); diff --git a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs index cb233625b..5b360358b 100644 --- a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs +++ b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs @@ -13,6 +13,11 @@ using Microsoft.AspNetCore.Http; using System.Threading.Tasks; using System.Collections.Concurrent; +using CacheManager.Core; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; namespace Ocelot.IntegrationTests { @@ -97,11 +102,35 @@ private void GivenOcelotIsRunning() .UseUrls(_ocelotBaseUrl) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); + var env = hostingContext.HostingEnvironment; + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + config.AddJsonFile("configuration.json"); + config.AddOcelotBaseUrl(_ocelotBaseUrl); + config.AddEnvironmentVariables(); + }) .ConfigureServices(x => { - x.AddSingleton(_webHostBuilder); + Action settings = (s) => + { + s.WithMicrosoftLogging(log => + { + log.AddConsole(LogLevel.Debug); + }) + .WithDictionaryHandle(); + }; + + x.AddOcelot() + .AddCacheManager(settings) + .AddAdministration("/administration", "secret"); }) - .UseStartup(); + .Configure(app => + { + app.UseOcelot().Wait(); + }); _builder = _webHostBuilder.Build(); diff --git a/test/Ocelot.ManualTest/ManualTestStartup.cs b/test/Ocelot.ManualTest/ManualTestStartup.cs deleted file mode 100644 index d23731c14..000000000 --- a/test/Ocelot.ManualTest/ManualTestStartup.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Ocelot.DependencyInjection; -using Ocelot.Middleware; - -namespace Ocelot.ManualTest -{ - public class ManualTestStartup - { - public void ConfigureServices(IServiceCollection services) - { - services.AddAuthentication() - .AddJwtBearer("TestKey", x => - { - x.Authority = "test"; - x.Audience = "test"; - }); - - services.AddOcelot() - .AddCacheManager(x => - { - x.WithDictionaryHandle(); - }) - .AddOpenTracing(option => - { - option.CollectorUrl = "http://localhost:9618"; - option.Service = "Ocelot.ManualTest"; - }) - .AddAdministration("/administration", "secret"); - } - - public void Configure(IApplicationBuilder app) - { - app.UseOcelot().Wait(); - } - } -} diff --git a/test/Ocelot.ManualTest/Program.cs b/test/Ocelot.ManualTest/Program.cs index 53cf41759..a7c5bc61f 100644 --- a/test/Ocelot.ManualTest/Program.cs +++ b/test/Ocelot.ManualTest/Program.cs @@ -3,6 +3,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; namespace Ocelot.ManualTest { @@ -10,20 +12,39 @@ public class Program { public static void Main(string[] args) { - IWebHostBuilder builder = new WebHostBuilder(); - builder.ConfigureServices(s => { - s.AddSingleton(builder); - }); - builder.UseKestrel() + new WebHostBuilder() + .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { - config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); - var env = hostingContext.HostingEnvironment; - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); - config.AddJsonFile("configuration.json"); - config.AddEnvironmentVariables(); + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddJsonFile("configuration.json") + .AddEnvironmentVariables() + .AddOcelotBaseUrl("http://localhost:5000"); + }) + .ConfigureServices(s => { + + s.AddAuthentication() + .AddJwtBearer("TestKey", x => + { + x.Authority = "test"; + x.Audience = "test"; + }); + + s.AddOcelot() + .AddCacheManager(x => + { + x.WithDictionaryHandle(); + }) + .AddOpenTracing(option => + { + option.CollectorUrl = "http://localhost:9618"; + option.Service = "Ocelot.ManualTest"; + }) + .AddAdministration("/administration", "secret"); }) .ConfigureLogging((hostingContext, logging) => { @@ -31,9 +52,12 @@ public static void Main(string[] args) logging.AddConsole(); }) .UseIISIntegration() - .UseStartup(); - var host = builder.Build(); - host.Run(); + .Configure(app => + { + app.UseOcelot().Wait(); + }) + .Build() + .Run(); } } } diff --git a/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs new file mode 100644 index 000000000..0fcbf66e3 --- /dev/null +++ b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.Configuration; +using Ocelot.DependencyInjection; +using Shouldly; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.DependencyInjection +{ + public class ConfigurationBuilderExtensionsTests + { + private IConfigurationRoot _configuration; + private string _result; + + [Fact] + public void should_add_base_url_to_config() + { + this.Given(x => GivenTheBaseUrl("test")) + .When(x => WhenIGet("BaseUrl")) + .Then(x => ThenTheResultIs("test")) + .BDDfy(); + } + + private void GivenTheBaseUrl(string baseUrl) + { + var builder = new ConfigurationBuilder() + .AddOcelotBaseUrl(baseUrl); + + _configuration = builder.Build(); + } + + private void WhenIGet(string key) + { + _result = _configuration.GetValue("BaseUrl", ""); + } + + private void ThenTheResultIs(string expected) + { + _result.ShouldBe(expected); + } + } +} diff --git a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs index e58398564..6f256d7f7 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs @@ -16,6 +16,7 @@ using Ocelot.UnitTests.Requester; using Shouldly; using System; +using IdentityServer4.AccessTokenValidation; using TestStack.BDDfy; using Xunit; @@ -31,13 +32,11 @@ public class OcelotBuilderTests public OcelotBuilderTests() { - IWebHostBuilder builder = new WebHostBuilder(); - _configRoot = new ConfigurationRoot(new List()); - _services = new ServiceCollection(); - _services.AddSingleton(builder); - _services.AddSingleton(); - _services.AddSingleton(_configRoot); - _maxRetries = 100; + _configRoot = new ConfigurationRoot(new List()); + _services = new ServiceCollection(); + _services.AddSingleton(); + _services.AddSingleton(_configRoot); + _maxRetries = 100; } private Exception _ex; @@ -100,6 +99,40 @@ public void should_set_up_rafty() .BDDfy(); } + [Fact] + public void should_set_up_administration_with_identity_server_options() + { + Action options = o => { + + }; + + this.Given(x => WhenISetUpOcelotServices()) + .When(x => WhenISetUpAdministration(options)) + .Then(x => ThenAnExceptionIsntThrown()) + .Then(x => ThenTheCorrectAdminPathIsRegitered()) + .BDDfy(); + } + + [Fact] + public void should_set_up_administration() + { + this.Given(x => WhenISetUpOcelotServices()) + .When(x => WhenISetUpAdministration()) + .Then(x => ThenAnExceptionIsntThrown()) + .Then(x => ThenTheCorrectAdminPathIsRegitered()) + .BDDfy(); + } + + private void WhenISetUpAdministration() + { + _ocelotBuilder.AddAdministration("/administration", "secret"); + } + + private void WhenISetUpAdministration(Action options) + { + _ocelotBuilder.AddAdministration("/administration", options); + } + [Fact] public void should_use_logger_factory() { @@ -255,6 +288,7 @@ private void WhenIAccessLoggerFactory() { try { + _serviceProvider = _services.BuildServiceProvider(); var logger = _serviceProvider.GetService(); } catch (Exception e) diff --git a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs index f685168c2..1ea5fcf8e 100644 --- a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs @@ -15,6 +15,7 @@ namespace Ocelot.UnitTests.Errors using Moq; using Ocelot.Configuration; using Rafty.Concensus; + using Ocelot.Errors; public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest { @@ -40,11 +41,6 @@ public void NoDownstreamException() .BDDfy(); } - private void TheRequestIdIsNotSet() - { - ScopedRepository.Verify(x => x.Add(It.IsAny(), It.IsAny()), Times.Never); - } - [Fact] public void DownstreamException() { @@ -83,6 +79,55 @@ public void ShouldNotSetRequestId() .BDDfy(); } + [Fact] + public void should_throw_exception_if_config_provider_returns_error() + { + this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) + .And(_ => GivenTheConfigReturnsError()) + .When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234")) + .Then(_ => ThenAnExceptionIsThrown()) + .BDDfy(); + } + + [Fact] + public void should_throw_exception_if_config_provider_throws() + { + this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream()) + .And(_ => GivenTheConfigThrows()) + .When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234")) + .Then(_ => ThenAnExceptionIsThrown()) + .BDDfy(); + } + + private void GivenTheConfigThrows() + { + var ex = new Exception("outer", new Exception("inner")); + _provider + .Setup(x => x.Get()).ThrowsAsync(ex); + } + + private void ThenAnExceptionIsThrown() + { + ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); + } + + private void GivenTheConfigReturnsError() + { + var config = new OcelotConfiguration(null, null, null, null); + + var response = new Ocelot.Responses.ErrorResponse(new FakeError()); + _provider + .Setup(x => x.Get()).ReturnsAsync(response); + } + + public class FakeError : Error + { + public FakeError() + : base("meh", OcelotErrorCode.CannotAddDataError) + { + } + } + private void TheRequestIdIsSet(string key, string value) { ScopedRepository.Verify(x => x.Add(key, value), Times.Once); @@ -140,5 +185,10 @@ private void ThenTheResponseIsError() { ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError); } + + private void TheRequestIdIsNotSet() + { + ScopedRepository.Verify(x => x.Add(It.IsAny(), It.IsAny()), Times.Never); + } } } \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs b/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs index d44921e2e..f22107a5f 100644 --- a/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs +++ b/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Memory; using Moq; using Ocelot.Middleware; using Shouldly; @@ -14,38 +16,41 @@ namespace Ocelot.UnitTests.Middleware public class BaseUrlFinderTests { private readonly BaseUrlFinder _baseUrlFinder; - private readonly Mock _webHostBuilder; + private readonly Mock _config; + private string _result; public BaseUrlFinderTests() { - _webHostBuilder = new Mock(); - _baseUrlFinder = new BaseUrlFinder(_webHostBuilder.Object); - } - - [Fact] - public void should_find_base_url_based_on_webhostbuilder() - { - this.Given(x => GivenTheWebHostBuilderReturns("http://localhost:7000")) - .When(x => WhenIFindTheUrl()) - .Then(x => ThenTheUrlIs("http://localhost:7000")) - .BDDfy(); + _config = new Mock(); + _baseUrlFinder = new BaseUrlFinder(_config.Object); } [Fact] public void should_use_default_base_url() { - this.Given(x => GivenTheWebHostBuilderReturns("")) + this.Given(x => GivenTheConfigBaseUrlIs("")) + .And(x => GivenTheConfigBaseUrlIs("")) .When(x => WhenIFindTheUrl()) .Then(x => ThenTheUrlIs("http://localhost:5000")) .BDDfy(); } - private void GivenTheWebHostBuilderReturns(string url) + [Fact] + public void should_use_file_config_base_url() + { + this.Given(x => GivenTheConfigBaseUrlIs("http://localhost:7000")) + .And(x => GivenTheConfigBaseUrlIs("http://baseurlfromconfig.com:5181")) + .When(x => WhenIFindTheUrl()) + .Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181")) + .BDDfy(); + } + + private void GivenTheConfigBaseUrlIs(string configValue) { - _webHostBuilder - .Setup(x => x.GetSetting(WebHostDefaults.ServerUrlsKey)) - .Returns(url); + var configSection = new ConfigurationSection(new ConfigurationRoot(new List{new MemoryConfigurationProvider(new MemoryConfigurationSource())}), ""); + configSection.Value = configValue; + _config.Setup(x => x.GetSection(It.IsAny())).Returns(configSection); } private void WhenIFindTheUrl()