From 6f177fbf5b1ecf4d2b3d9f70140664a2cb163fe2 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Tue, 13 Feb 2018 23:00:41 +0000 Subject: [PATCH 1/3] messing around (#230) --- src/Ocelot/Cache/CachedResponse.cs | 11 +- .../Cache/Middleware/OutputCacheMiddleware.cs | 13 +- test/Ocelot.AcceptanceTests/CachingTests.cs | 1 + test/Ocelot.AcceptanceTests/Steps.cs | 5 + test/Ocelot.ManualTest/ManualTestStartup.cs | 2 +- test/Ocelot.ManualTest/configuration.json | 3 +- .../OutputCacheMiddlewareRealCacheTests.cs | 131 ++++++++++++++++++ .../Cache/OutputCacheMiddlewareTests.cs | 6 +- 8 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs diff --git a/src/Ocelot/Cache/CachedResponse.cs b/src/Ocelot/Cache/CachedResponse.cs index 02b327b3..e4360e0d 100644 --- a/src/Ocelot/Cache/CachedResponse.cs +++ b/src/Ocelot/Cache/CachedResponse.cs @@ -6,13 +6,16 @@ namespace Ocelot.Cache public class CachedResponse { public CachedResponse( - HttpStatusCode statusCode = HttpStatusCode.OK, - Dictionary> headers = null, - string body = null + HttpStatusCode statusCode, + Dictionary> headers, + string body, + Dictionary> contentHeaders + ) { StatusCode = statusCode; Headers = headers ?? new Dictionary>(); + ContentHeaders = contentHeaders ?? new Dictionary>(); Body = body ?? ""; } @@ -20,6 +23,8 @@ namespace Ocelot.Cache public Dictionary> Headers { get; private set; } + public Dictionary> ContentHeaders { get; private set; } + public string Body { get; private set; } } } diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 717283f5..07f7445e 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -82,13 +82,21 @@ namespace Ocelot.Cache.Middleware } var response = new HttpResponseMessage(cached.StatusCode); + foreach (var header in cached.Headers) { response.Headers.Add(header.Key, header.Value); } + var content = new MemoryStream(Convert.FromBase64String(cached.Body)); + response.Content = new StreamContent(content); + foreach (var header in cached.ContentHeaders) + { + response.Content.Headers.Add(header.Key, header.Value); + } + return response; } @@ -109,7 +117,10 @@ namespace Ocelot.Cache.Middleware body = Convert.ToBase64String(content); } - var cached = new CachedResponse(statusCode, headers, body); + var contentHeaders = response?.Content?.Headers.ToDictionary(v => v.Key, v => v.Value); + + + var cached = new CachedResponse(statusCode, headers, body, contentHeaders); return cached; } } diff --git a/test/Ocelot.AcceptanceTests/CachingTests.cs b/test/Ocelot.AcceptanceTests/CachingTests.cs index 244a872f..755237a5 100644 --- a/test/Ocelot.AcceptanceTests/CachingTests.cs +++ b/test/Ocelot.AcceptanceTests/CachingTests.cs @@ -61,6 +61,7 @@ namespace Ocelot.AcceptanceTests .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) .And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura")) + .And(x => _steps.ThenTheContentLengthIs(16)) .BDDfy(); } diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index bcd017b4..218e58e7 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -457,5 +457,10 @@ namespace Ocelot.AcceptanceTests { _response.Headers.GetValues(RequestIdKey).First().ShouldBe(expected); } + + public void ThenTheContentLengthIs(int expected) + { + _response.Content.Headers.ContentLength.ShouldBe(expected); + } } } diff --git a/test/Ocelot.ManualTest/ManualTestStartup.cs b/test/Ocelot.ManualTest/ManualTestStartup.cs index dd5cf979..d23731c1 100644 --- a/test/Ocelot.ManualTest/ManualTestStartup.cs +++ b/test/Ocelot.ManualTest/ManualTestStartup.cs @@ -34,4 +34,4 @@ namespace Ocelot.ManualTest app.UseOcelot().Wait(); } } -} \ No newline at end of file +} diff --git a/test/Ocelot.ManualTest/configuration.json b/test/Ocelot.ManualTest/configuration.json index 45f184bc..93bf6ac6 100644 --- a/test/Ocelot.ManualTest/configuration.json +++ b/test/Ocelot.ManualTest/configuration.json @@ -76,8 +76,7 @@ "UpstreamHttpMethod": [ "Get" ], "HttpHandlerOptions": { "AllowAutoRedirect": true, - "UseCookieContainer": true, - "UseTracing": true + "UseCookieContainer": true }, "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs new file mode 100644 index 00000000..70619c6b --- /dev/null +++ b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareRealCacheTests.cs @@ -0,0 +1,131 @@ +using Ocelot.Infrastructure.RequestData; + +namespace Ocelot.UnitTests.Cache +{ + using System.Linq; + using System.Net; + using System.Net.Http.Headers; + using CacheManager.Core; + using Shouldly; + using System.Collections.Generic; + using System.Net.Http; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using Ocelot.Cache; + using Ocelot.Cache.Middleware; + using Ocelot.Configuration; + using Ocelot.Configuration.Builder; + using Ocelot.DownstreamRouteFinder; + using Ocelot.DownstreamRouteFinder.UrlMatcher; + using Ocelot.Logging; + using Ocelot.Responses; + using TestStack.BDDfy; + using Xunit; + + public class OutputCacheMiddlewareRealCacheTests : ServerHostedMiddlewareTest + { + private IOcelotCache _cacheManager; + private CachedResponse _response; + private IRequestScopedDataRepository _repo; + + public OutputCacheMiddlewareRealCacheTests() + { + ScopedRepository + .Setup(sr => sr.Get("DownstreamRequest")) + .Returns(new OkResponse(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123"))); + + GivenTheTestServerIsConfigured(); + } + + [Fact] + public void should_cache_content_headers() + { + var content = new StringContent("{\"Test\": 1}") + { + Headers = { ContentType = new MediaTypeHeaderValue("application/json")} + }; + + var response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = content, + }; + + this.Given(x => x.GivenResponseIsNotCached(response)) + .And(x => x.GivenTheDownstreamRouteIs()) + .And(x => x.GivenThereAreNoErrors()) + .And(x => x.GivenThereIsADownstreamUrl()) + .When(x => x.WhenICallTheMiddleware()) + .Then(x => x.ThenTheContentTypeHeaderIsCached()) + .BDDfy(); + } + + private void ThenTheContentTypeHeaderIsCached() + { + var result = _cacheManager.Get("GET-https://some.url/blah?abcd=123", "kanken"); + var header = result.ContentHeaders["Content-Type"]; + header.First().ShouldBe("application/json"); + } + + protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services) + { + var cacheManagerOutputCache = CacheFactory.Build("OcelotOutputCache", x => + { + x.WithDictionaryHandle(); + }); + + _cacheManager = new OcelotCacheManagerCache(cacheManagerOutputCache); + + services.AddSingleton>(cacheManagerOutputCache); + services.AddSingleton>(_cacheManager); + + services.AddSingleton(); + + services.AddLogging(); + services.AddSingleton(_cacheManager); + services.AddSingleton(ScopedRepository.Object); + services.AddSingleton(); + } + + protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app) + { + app.UseOutputCacheMiddleware(); + } + + private void GivenResponseIsNotCached(HttpResponseMessage message) + { + ScopedRepository + .Setup(x => x.Get("HttpResponseMessage")) + .Returns(new OkResponse(message)); + } + + private void GivenTheDownstreamRouteIs() + { + var reRoute = new ReRouteBuilder() + .WithIsCached(true) + .WithCacheOptions(new CacheOptions(100, "kanken")) + .WithUpstreamHttpMethod(new List { "Get" }) + .Build(); + + var downstreamRoute = new DownstreamRoute(new List(), reRoute); + + ScopedRepository + .Setup(x => x.Get(It.IsAny())) + .Returns(new OkResponse(downstreamRoute)); + } + + private void GivenThereAreNoErrors() + { + ScopedRepository + .Setup(x => x.Get("OcelotMiddlewareError")) + .Returns(new OkResponse(false)); + } + + private void GivenThereIsADownstreamUrl() + { + ScopedRepository + .Setup(x => x.Get("DownstreamUrl")) + .Returns(new OkResponse("anything")); + } + } +} diff --git a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs index 71839a5a..2a60e376 100644 --- a/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Cache/OutputCacheMiddlewareTests.cs @@ -1,4 +1,6 @@ -namespace Ocelot.UnitTests.Cache +using System.Net; + +namespace Ocelot.UnitTests.Cache { using System; using System.Collections.Generic; @@ -36,7 +38,7 @@ [Fact] public void should_returned_cached_item_when_it_is_in_cache() { - var cachedResponse = new CachedResponse(); + var cachedResponse = new CachedResponse(HttpStatusCode.OK, new Dictionary>(), "", new Dictionary>()); this.Given(x => x.GivenThereIsACachedResponse(cachedResponse)) .And(x => x.GivenTheDownstreamRouteIs()) .And(x => x.GivenThereIsADownstreamUrl()) From 05481f3af372eca15fb678730f419536040c1159 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Wed, 14 Feb 2018 18:53:18 +0000 Subject: [PATCH 2/3] Feature/use any id server for admin area (#232) * initial commits around using any id servers * add your own id server for admin area * lots of refactoring, now instead of injecting IWebHostBuilder we just set the Ocelot base url as a configuration extension method..this means people can pass it in on the command line aswell as hardcode which is OK I guess, also can now use your own IdentityServer to authenticate admin area * updated docs for #231 * some tests that hopefully bump up coverage --- docs/features/administration.rst | 38 ++- docs/features/authentication.rst | 7 + docs/introduction/gettingstarted.rst | 69 ++--- .../Creator/HeaderFindAndReplaceCreator.cs | 6 +- .../Configuration/OcelotConfiguration.cs | 2 +- .../ConfigurationBuilderExtensions.cs | 20 ++ .../DependencyInjection/IOcelotBuilder.cs | 4 +- .../DependencyInjection/OcelotBuilder.cs | 135 +++++---- .../ServiceCollectionExtensions.cs | 4 +- .../Middleware/ExceptionHandlerMiddleware.cs | 1 - src/Ocelot/Middleware/BaseUrlFinder.cs | 12 +- .../Middleware/OcelotMiddlewareExtensions.cs | 59 ++-- src/Ocelot/Raft/FilePeersProvider.cs | 9 +- src/Ocelot/Raft/HttpPeer.cs | 5 +- src/Ocelot/Raft/RaftController.cs | 7 +- .../AcceptanceTestsStartup.cs | 39 --- test/Ocelot.AcceptanceTests/ConsulStartup.cs | 40 --- .../StartupWithConsulAndCustomCacheHandle.cs | 29 -- .../StartupWithCustomCacheHandle.cs | 28 -- test/Ocelot.AcceptanceTests/Steps.cs | 194 +++++++++---- .../AdministrationTests.cs | 271 +++++++++++++++++- .../IntegrationTestsStartup.cs | 51 ---- test/Ocelot.IntegrationTests/RaftStartup.cs | 53 ---- test/Ocelot.IntegrationTests/RaftTests.cs | 29 +- .../ThreadSafeHeadersTests.cs | 33 ++- test/Ocelot.ManualTest/ManualTestStartup.cs | 37 --- test/Ocelot.ManualTest/Program.cs | 52 +++- .../ConfigurationBuilderExtensionsTests.cs | 41 +++ .../DependencyInjection/OcelotBuilderTests.cs | 48 +++- .../Errors/ExceptionHandlerMiddlewareTests.cs | 60 +++- .../Middleware/BaseUrlFinderTests.cs | 39 +-- 31 files changed, 876 insertions(+), 546 deletions(-) create mode 100644 src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs delete mode 100644 test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs delete mode 100644 test/Ocelot.AcceptanceTests/ConsulStartup.cs delete mode 100644 test/Ocelot.AcceptanceTests/StartupWithConsulAndCustomCacheHandle.cs delete mode 100644 test/Ocelot.AcceptanceTests/StartupWithCustomCacheHandle.cs delete mode 100644 test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs delete mode 100644 test/Ocelot.IntegrationTests/RaftStartup.cs delete mode 100644 test/Ocelot.ManualTest/ManualTestStartup.cs create mode 100644 test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs diff --git a/docs/features/administration.rst b/docs/features/administration.rst index 298e410e..425c958e 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 ff349bdd..dd78fcfa 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 65ff432d..8ced66eb 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 3820de4e..7ab33e90 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 35f956ec..1ab73b87 100644 --- a/src/Ocelot/Configuration/OcelotConfiguration.cs +++ b/src/Ocelot/Configuration/OcelotConfiguration.cs @@ -17,4 +17,4 @@ namespace Ocelot.Configuration 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 00000000..c525f67f --- /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 d2f92101..48db5ef7 100644 --- a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs @@ -2,6 +2,7 @@ using Butterfly.Client.AspNetCore; using CacheManager.Core; using System; using System.Net.Http; +using IdentityServer4.AccessTokenValidation; namespace Ocelot.DependencyInjection { @@ -9,8 +10,9 @@ namespace Ocelot.DependencyInjection { 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 10f9d082..d51b8a8d 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 @@ namespace Ocelot.DependencyInjection _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); - _services.TryAddSingleton(); + _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); @@ -166,6 +168,21 @@ namespace Ocelot.DependencyInjection 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 @@ namespace Ocelot.DependencyInjection 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 @@ namespace Ocelot.DependencyInjection .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 51caf3bb..e3f90665 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 54461bbd..a08031ea 100644 --- a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs @@ -21,7 +21,6 @@ namespace Ocelot.Errors.Middleware 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 63672501..de24e682 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 5ac19cf2..249dceb5 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 @@ namespace Ocelot.Middleware { var configuration = await CreateConfiguration(builder); - await CreateAdministrationArea(builder, configuration); + CreateAdministrationArea(builder, configuration); if(UsingRafty(builder)) { @@ -290,15 +281,19 @@ namespace Ocelot.Middleware 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 2718ae18..aeca7b2c 100644 --- a/src/Ocelot/Raft/FilePeersProvider.cs +++ b/src/Ocelot/Raft/FilePeersProvider.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Options; using Ocelot.Configuration; using Ocelot.Configuration.Provider; +using Ocelot.Middleware; using Rafty.Concensus; using Rafty.Infrastructure; @@ -15,15 +16,15 @@ namespace Ocelot.Raft { 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 @@ namespace Ocelot.Raft { 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 67cc8e15..ef76a583 100644 --- a/src/Ocelot/Raft/HttpPeer.cs +++ b/src/Ocelot/Raft/HttpPeer.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json; using Ocelot.Authentication; using Ocelot.Configuration; using Ocelot.Configuration.Provider; +using Ocelot.Middleware; using Rafty.Concensus; using Rafty.FiniteStateMachine; @@ -23,7 +24,7 @@ namespace Ocelot.Raft 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 @@ namespace Ocelot.Raft _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 7177044b..eb91e86f 100644 --- a/src/Ocelot/Raft/RaftController.cs +++ b/src/Ocelot/Raft/RaftController.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc; 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 @@ namespace Ocelot.Raft 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 @@ namespace Ocelot.Raft } } } -} \ 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 c3cbf1d0..00000000 --- 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 b7eb569b..00000000 --- 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 e48efcdb..00000000 --- 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 2570bc93..00000000 --- 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 218e58e7..b33778e7 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -38,9 +38,11 @@ namespace Ocelot.AcceptanceTests 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 @@ namespace Ocelot.AcceptanceTests { _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 @@ namespace Ocelot.AcceptanceTests { _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 @@ namespace Ocelot.AcceptanceTests { _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 @@ namespace Ocelot.AcceptanceTests { _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 @@ namespace Ocelot.AcceptanceTests { _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 @@ namespace Ocelot.AcceptanceTests { _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 44f394c0..cddd4368 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -4,11 +4,21 @@ using System.IO; 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 @@ namespace Ocelot.IntegrationTests .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 @@ namespace Ocelot.IntegrationTests .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 @@ namespace Ocelot.IntegrationTests .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(); }) - .UseStartup(); + .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(); + }); + _builderTwo = _webHostBuilderTwo.Build(); @@ -400,18 +564,110 @@ namespace Ocelot.IntegrationTests 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 @@ namespace Ocelot.IntegrationTests 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 8f1906d0..00000000 --- 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 bb9f26d9..00000000 --- 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 2e1752de..1517e879 100644 --- a/test/Ocelot.IntegrationTests/RaftTests.cs +++ b/test/Ocelot.IntegrationTests/RaftTests.cs @@ -17,6 +17,9 @@ using Shouldly; 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 @@ namespace Ocelot.IntegrationTests 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 @@ namespace Ocelot.IntegrationTests { _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 @@ namespace Ocelot.IntegrationTests 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 cb233625..5b360358 100644 --- a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs +++ b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs @@ -13,6 +13,11 @@ using Xunit; 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 @@ namespace Ocelot.IntegrationTests .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 d23731c1..00000000 --- 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 53cf4175..a7c5bc61 100644 --- a/test/Ocelot.ManualTest/Program.cs +++ b/test/Ocelot.ManualTest/Program.cs @@ -3,6 +3,8 @@ using Microsoft.AspNetCore.Hosting; 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 @@ namespace Ocelot.ManualTest { 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 @@ namespace Ocelot.ManualTest 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 00000000..0fcbf66e --- /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 e5839856..6f256d7f 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs @@ -16,6 +16,7 @@ using Ocelot.Requester; using Ocelot.UnitTests.Requester; using Shouldly; using System; +using IdentityServer4.AccessTokenValidation; using TestStack.BDDfy; using Xunit; @@ -31,13 +32,11 @@ namespace Ocelot.UnitTests.DependencyInjection 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 @@ namespace Ocelot.UnitTests.DependencyInjection .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 @@ namespace Ocelot.UnitTests.DependencyInjection { 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 f685168c..1ea5fcf8 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 @@ namespace Ocelot.UnitTests.Errors .BDDfy(); } - private void TheRequestIdIsNotSet() - { - ScopedRepository.Verify(x => x.Add(It.IsAny(), It.IsAny()), Times.Never); - } - [Fact] public void DownstreamException() { @@ -83,6 +79,55 @@ namespace Ocelot.UnitTests.Errors .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 @@ namespace Ocelot.UnitTests.Errors { 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 d44921e2..f22107a5 100644 --- a/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs +++ b/test/Ocelot.UnitTests/Middleware/BaseUrlFinderTests.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; 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() { - _webHostBuilder - .Setup(x => x.GetSetting(WebHostDefaults.ServerUrlsKey)) - .Returns(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) + { + 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() From d6a86b929511f8dd90c769b9c35e73619ac5f213 Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Wed, 14 Feb 2018 19:01:08 +0000 Subject: [PATCH 3/3] +semver: feature --- docs/introduction/gettingstarted.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/introduction/gettingstarted.rst b/docs/introduction/gettingstarted.rst index 8ced66eb..840505f1 100644 --- a/docs/introduction/gettingstarted.rst +++ b/docs/introduction/gettingstarted.rst @@ -68,6 +68,15 @@ AddOcelot() (adds ocelot services), UseOcelot().Wait() (sets up all the Ocelot m } } +AddOcelotBaseUrl +^^^^^^^^^^^^^^^^ + +The most important thing to note here is AddOcelotBaseUrl. Ocelot needs to know the URL it is running under +in order to do Header find & replace and for certain administration configurations. When setting this URL it should be the external URL that clients will see Ocelot running on e.g. If you are running containers Ocelot might run on the url http://123.12.1.1:6543 but has something like nginx in front of it responding on https://api.mybusiness.com. In this case the Ocelot base url should be https://api.mybusiness.com. + +If for some reason you are using containers and do want Ocelot to respond to client on http://123.12.1.1:6543 +then you can do this but if you are deploying multiple Ocelot's you will probably want to pass this on the command line in some kind of script. Hopefully whatever scheduler you are using can pass the IP. + .NET Core 1.0 ^^^^^^^^^^^^^