diff --git a/build.cake b/build.cake index 12fcbdd0..7281eb3a 100644 --- a/build.cake +++ b/build.cake @@ -133,7 +133,7 @@ Task("RunUnitTests") new OpenCoverSettings() { Register="user", - ArgumentCustomization=args=>args.Append(@"-oldstyle -returntargetcode") + ArgumentCustomization=args=>args.Append(@"-oldstyle -returntargetcode -excludebyattribute:*.ExcludeFromCoverage*") } .WithFilter("+[Ocelot*]*") .WithFilter("-[xunit*]*") diff --git a/docs/features/administration.rst b/docs/features/administration.rst index 34983b63..162920f6 100644 --- a/docs/features/administration.rst +++ b/docs/features/administration.rst @@ -6,33 +6,24 @@ using bearer tokens that you request from Ocelot iteself. This is provided by th `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 -initial configuration.json. The value can be anything you want and it is obviously reccomended don't use +initial Startup.cs. + +The path can be anything you want and it is obviously reccomended don't use a url you would like to route through with Ocelot as this will not work. The administration uses the MapWhen functionality of asp.net core and all requests to {root}/administration will be sent there not to the Ocelot middleware. -.. code-block:: json +The secret is the client secret that Ocelot's internal IdentityServer will use to authenticate requests to the administration API. This can be whatever you want it to be! - "GlobalConfiguration": { - "AdministrationPath": "/administration" +.. code-block:: csharp + + public virtual void ConfigureServices(IServiceCollection services) + { + services + .AddOcelot(Configuration) + .AddAdministration("/administration", "secret"); } -This will get the admin area set up but not the authentication. -Please note that this is a very basic approach to -this problem and if needed we can obviously improve on this! - -You need to set 3 environmental variables. - - ``OCELOT_USERNAME`` - - This need to be the admin username you want to use with Ocelot. - ``OCELOT_HASH`` - ``OCELOT_SALT`` - The hash and salt of the password you want to use given hashing algorythm. When requesting bearer tokens for use with the administration api you will need to supply username and password. In order to create a hash and salt of your password please check out HashCreationTests.should_create_hash_and_salt() this technique is based on [this](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing) - using SHA256 rather than SHA1. - - - Now if you went with the configuration options above and want to access the API you can use the postman scripts called ocelot.postman_collection.json in the solution to change the Ocelot configuration. Obviously these will need to be changed if you are running Ocelot on a different url to http://localhost:5000. @@ -40,7 +31,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. @@ -59,21 +49,17 @@ Administration API **POST {adminPath}/connect/token** -This gets a token for use with the admin area using the username and password we talk about setting above. Under the hood this calls into an IdentityServer hosted within Ocelot. +This gets a token for use with the admin area using the client credentials we talk about setting above. Under the hood this calls into an IdentityServer hosted within Ocelot. The body of the request is form-data as follows ``client_id`` set as admin -``client_secret`` set as secret +``client_secret`` set as whatever you used when setting up the administration services. ``scope`` set as admin -``username`` set as whatever you used - -``password`` set aswhatever you used - -``grant_type`` set as password +``grant_type`` set as client_credentials **GET {adminPath}/configuration** diff --git a/docs/features/raft.rst b/docs/features/raft.rst new file mode 100644 index 00000000..a61e2ed1 --- /dev/null +++ b/docs/features/raft.rst @@ -0,0 +1,45 @@ +Raft (EXPERIMENTAL DO NOT USE IN PRODUCTION) +============================================ + +Ocelot has recenely integrated `Rafty `_ which is an implementation of Raft that I have also been working on over the last year. This project is very experimental so please do not use this feature of Ocelot in production until I think it's OK. + +Raft is a distributed concensus algorythm that allows a cluster of servers (Ocelots) to maintain local state without having a centralised database for storing state (e.g. SQL Server). + +In order to enable Rafty in Ocelot you must make the following changes to your Startup.cs. + +.. code-block:: csharp + + public virtual void ConfigureServices(IServiceCollection services) + { + services + .AddOcelot(Configuration) + .AddAdministration("/administration", "secret") + .AddRafty(); + } + +In addition to this you must add a file called peers.json to your main project and it will look as follows + +.. code-block:: json + + { + "Peers": [{ + "HostAndPort": "http://localhost:5000" + }, + { + "HostAndPort": "http://localhost:5002" + }, + { + "HostAndPort": "http://localhost:5003" + }, + { + "HostAndPort": "http://localhost:5004" + }, + { + "HostAndPort": "http://localhost:5001" + } + ] + } + +Each instance of Ocelot must have it's address in the array so that they can communicate using Rafty. + +Once you have made these configuration changes you must deploy and start each instance of Ocelot using the addresses in the peers.json file. The servers should then start communicating with each other! You can test if everything is working by posting a configuration update and checking it has replicated to all servers by getting there configuration. diff --git a/docs/index.rst b/docs/index.rst index 6e410970..0a292bcf 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -24,6 +24,7 @@ Thanks for taking a look at the Ocelot documentation. Please use the left hand n features/authentication features/authorisation features/administration + features/raft features/caching features/qualityofservice features/claimstransformation diff --git a/ocelot.postman_collection.json b/ocelot.postman_collection.json index 155e11bc..28bbeb0c 100644 --- a/ocelot.postman_collection.json +++ b/ocelot.postman_collection.json @@ -1,65 +1,169 @@ { - "id": "23a49657-e24b-b967-7ec0-943ff1368680", - "name": "Ocelot Admin", + "id": "4dbde9fe-89f5-be35-bb9f-d3b438e16375", + "name": "Ocelot", "description": "", "order": [ - "59162efa-27ce-c230-f523-81d31ead603d", - "e0defe09-c1b2-9e95-8237-67df4bbab284", - "30007c41-565c-5b87-ea34-42170dd386d7" + "a1c95935-ed18-d5dc-bcb8-a3db8ba1934f", + "ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2", + "c4494401-3985-a5bf-71fb-6e4171384ac6", + "09af8dda-a9cb-20d2-5ee3-0a3023773a1a", + "e8825dc3-4137-99a7-0000-ef5786610dc3", + "fddfc4fa-5114-69e3-4744-203ed71a526b", + "c45d30d7-d9c4-fa05-8110-d6e769bb6ff9", + "4684c2fa-f38c-c193-5f55-bf563a1978c6", + "5f308240-79e3-cf74-7a6b-fe462f0d54f1", + "178f16da-c61b-c881-1c33-9d64a56851a4", + "26a08569-85f6-7f9a-726f-61be419c7a34" ], "folders": [], - "timestamp": 1488042899799, + "timestamp": 0, "owner": "212120", "public": false, "requests": [ { - "id": "30007c41-565c-5b87-ea34-42170dd386d7", + "folder": null, + "id": "09af8dda-a9cb-20d2-5ee3-0a3023773a1a", + "name": "GET http://localhost:5000/comments?postId=1", + "dataMode": "params", + "data": null, + "rawModeData": null, + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "GET", + "pathVariables": {}, + "url": "http://localhost:5000/comments?postId=1", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "id": "178f16da-c61b-c881-1c33-9d64a56851a4", "headers": "Authorization: Bearer {{AccessToken}}\n", - "url": "http://localhost:5000/admin/configuration", + "url": "http://localhost:5000/administration/configuration", "preRequestScript": null, "pathVariables": {}, "method": "GET", "data": null, "dataMode": "params", - "version": 2, - "tests": null, - "currentHelper": "normal", - "helperAttributes": "{}", - "time": 1487515927978, - "name": "POST http://localhost:5000/admin/configuration", - "description": "", - "collectionId": "23a49657-e24b-b967-7ec0-943ff1368680", - "responses": [], - "isFromCollection": true, - "collectionRequestId": "59162efa-27ce-c230-f523-81d31ead603d" - }, - { - "id": "59162efa-27ce-c230-f523-81d31ead603d", - "headers": "Authorization: Bearer {{AccessToken}}\nContent-Type: application/json\n", - "url": "http://localhost:5000/admin/configuration", - "preRequestScript": null, - "pathVariables": {}, - "method": "POST", - "data": [], - "dataMode": "raw", - "version": 2, "tests": null, "currentHelper": "normal", "helperAttributes": {}, - "time": 1488044268493, + "time": 1508914722969, "name": "GET http://localhost:5000/admin/configuration", "description": "", - "collectionId": "23a49657-e24b-b967-7ec0-943ff1368680", - "responses": [], - "rawModeData": "{\n \"reRoutes\": [\n {\n \"downstreamPathTemplate\": \"/\",\n \"upstreamPathTemplate\": \"/identityserverexample\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": \"IdentityServer\",\n \"providerRootUrl\": \"http://localhost:52888\",\n \"apiName\": \"api\",\n \"requireHttps\": false,\n \"allowedScopes\": [\n \"openid\",\n \"offline_access\"\n ],\n \"apiSecret\": \"secret\"\n },\n \"addHeadersToRequest\": {\n \"CustomerId\": \"Claims[CustomerId] > value\",\n \"LocationId\": \"Claims[LocationId] > value\",\n \"UserId\": \"Claims[sub] > value[1] > |\",\n \"UserType\": \"Claims[sub] > value[0] > |\"\n },\n \"addClaimsToRequest\": {\n \"CustomerId\": \"Claims[CustomerId] > value\",\n \"LocationId\": \"Claims[LocationId] > value\",\n \"UserId\": \"Claims[sub] > value[1] > |\",\n \"UserType\": \"Claims[sub] > value[0] > |\"\n },\n \"routeClaimsRequirement\": {\n \"UserType\": \"registered\"\n },\n \"addQueriesToRequest\": {\n \"CustomerId\": \"Claims[CustomerId] > value\",\n \"LocationId\": \"Claims[LocationId] > value\",\n \"UserId\": \"Claims[sub] > value[1] > |\",\n \"UserType\": \"Claims[sub] > value[0] > |\"\n },\n \"requestIdKey\": \"OcRequestId\",\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"localhost\",\n \"downstreamPort\": 52876,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/\",\n \"upstreamPathTemplate\": \"/posts\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"www.bbc.co.uk\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts/{postId}/comments\",\n \"upstreamPathTemplate\": \"/posts/{postId}/comments\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/comments\",\n \"upstreamPathTemplate\": \"/comments\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts\",\n \"upstreamPathTemplate\": \"/posts\",\n \"upstreamHttpMethod\": \"Post\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamHttpMethod\": \"Put\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamHttpMethod\": \"Patch\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamPathTemplate\": \"/posts/{postId}\",\n \"upstreamHttpMethod\": \"Delete\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/products\",\n \"upstreamPathTemplate\": \"/products\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/products/{productId}\",\n \"upstreamPathTemplate\": \"/products/{productId}\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 0,\n \"durationOfBreak\": 0,\n \"timeoutValue\": 0\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/products\",\n \"upstreamPathTemplate\": \"/products\",\n \"upstreamHttpMethod\": \"Post\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 0\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"products20161126090340.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/products/{productId}\",\n \"upstreamPathTemplate\": \"/products/{productId}\",\n \"upstreamHttpMethod\": \"Put\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"products20161126090340.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/products/{productId}\",\n \"upstreamPathTemplate\": \"/products/{productId}\",\n \"upstreamHttpMethod\": \"Delete\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"products20161126090340.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/customers\",\n \"upstreamPathTemplate\": \"/customers\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"customers20161126090811.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/customers/{customerId}\",\n \"upstreamPathTemplate\": \"/customers/{customerId}\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"customers20161126090811.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/customers\",\n \"upstreamPathTemplate\": \"/customers\",\n \"upstreamHttpMethod\": \"Post\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"customers20161126090811.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/customers/{customerId}\",\n \"upstreamPathTemplate\": \"/customers/{customerId}\",\n \"upstreamHttpMethod\": \"Put\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"customers20161126090811.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/api/customers/{customerId}\",\n \"upstreamPathTemplate\": \"/customers/{customerId}\",\n \"upstreamHttpMethod\": \"Delete\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"customers20161126090811.azurewebsites.net\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n },\n {\n \"downstreamPathTemplate\": \"/posts\",\n \"upstreamPathTemplate\": \"/posts/\",\n \"upstreamHttpMethod\": \"Get\",\n \"authenticationOptions\": {\n \"provider\": null,\n \"providerRootUrl\": null,\n \"apiName\": null,\n \"requireHttps\": false,\n \"allowedScopes\": [],\n \"apiSecret\": null\n },\n \"addHeadersToRequest\": {},\n \"addClaimsToRequest\": {},\n \"routeClaimsRequirement\": {},\n \"addQueriesToRequest\": {},\n \"requestIdKey\": null,\n \"fileCacheOptions\": {\n \"ttlSeconds\": 15\n },\n \"reRouteIsCaseSensitive\": false,\n \"serviceName\": null,\n \"downstreamScheme\": \"http\",\n \"downstreamHost\": \"jsonplaceholder.typicode.com\",\n \"downstreamPort\": 80,\n \"qoSOptions\": {\n \"exceptionsAllowedBeforeBreaking\": 3,\n \"durationOfBreak\": 10,\n \"timeoutValue\": 5000\n },\n \"loadBalancer\": null\n }\n ],\n \"globalConfiguration\": {\n \"requestIdKey\": \"OcRequestId\",\n \"serviceDiscoveryProvider\": {\n \"provider\": null,\n \"host\": null,\n \"port\": 0\n },\n \"administrationPath\": \"/admin\"\n }\n}" + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" }, { - "id": "e0defe09-c1b2-9e95-8237-67df4bbab284", + "id": "26a08569-85f6-7f9a-726f-61be419c7a34", "headers": "", - "url": "http://localhost:5000/admin/connect/token", + "url": "http://localhost:5000/administration/connect/token", "preRequestScript": null, "pathVariables": {}, "method": "POST", + "data": [ + { + "key": "client_id", + "value": "raft", + "type": "text", + "enabled": true + }, + { + "key": "client_secret", + "value": "REALLYHARDPASSWORD", + "type": "text", + "enabled": true + }, + { + "key": "scope", + "value": "admin raft ", + "type": "text", + "enabled": true + }, + { + "key": "username", + "value": "admin", + "type": "text", + "enabled": false + }, + { + "key": "password", + "value": "secret", + "type": "text", + "enabled": false + }, + { + "key": "grant_type", + "value": "client_credentials", + "type": "text", + "enabled": true + } + ], + "dataMode": "params", + "tests": "var jsonData = JSON.parse(responseBody);\npostman.setGlobalVariable(\"AccessToken\", jsonData.access_token);\npostman.setGlobalVariable(\"RefreshToken\", jsonData.refresh_token);", + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1513240031907, + "name": "POST http://localhost:5000/admin/connect/token copy copy", + "description": "", + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "folder": null, + "id": "4684c2fa-f38c-c193-5f55-bf563a1978c6", + "name": "DELETE http://localhost:5000/posts/1", + "dataMode": "params", + "data": null, + "rawModeData": null, + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "DELETE", + "pathVariables": {}, + "url": "http://localhost:5000/posts/1", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "id": "5f308240-79e3-cf74-7a6b-fe462f0d54f1", + "headers": "Authorization: Bearer {{AccessToken}}\n", + "url": "http://localhost:5000/administration/.well-known/openid-configuration", + "preRequestScript": null, + "pathVariables": {}, + "method": "GET", + "data": null, + "dataMode": "params", + "tests": null, + "currentHelper": "normal", + "helperAttributes": "{}", + "time": 1488038888813, + "name": "GET http://localhost:5000/admin/.well-known/openid-configuration", + "description": "", + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375", + "folder": null, + "rawModeData": null, + "descriptionFormat": null, + "queryParams": [], + "headerData": [ + { + "key": "Authorization", + "value": "Bearer {{AccessToken}}", + "description": "", + "enabled": true + } + ], + "pathVariableData": [] + }, + { + "id": "a1c95935-ed18-d5dc-bcb8-a3db8ba1934f", + "folder": null, + "name": "GET http://localhost:5000/posts", + "dataMode": "params", "data": [ { "key": "client_id", @@ -87,7 +191,7 @@ }, { "key": "password", - "value": "secret", + "value": "admin", "type": "text", "enabled": true }, @@ -98,20 +202,113 @@ "enabled": true } ], - "dataMode": "params", - "version": 2, - "tests": "var jsonData = JSON.parse(responseBody);\npostman.setGlobalVariable(\"AccessToken\", jsonData.access_token);\npostman.setGlobalVariable(\"RefreshToken\", jsonData.refresh_token);", + "rawModeData": null, + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "POST", + "pathVariables": {}, + "url": "http://localhost:5000/admin/configuration", + "preRequestScript": null, + "tests": null, "currentHelper": "normal", "helperAttributes": "{}", - "time": 1487515922748, - "name": "POST http://localhost:5000/admin/connect/token", - "description": "", - "collectionId": "23a49657-e24b-b967-7ec0-943ff1368680", - "responses": [], + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "folder": null, + "id": "c4494401-3985-a5bf-71fb-6e4171384ac6", + "name": "GET http://localhost:5000/posts/1/comments", + "dataMode": "params", + "data": null, "rawModeData": null, - "descriptionFormat": null, - "isFromCollection": true, - "collectionRequestId": "e23e29a1-6abb-abd3-141a-f2202e3f582b" + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "GET", + "pathVariables": {}, + "url": "http://localhost:5000/posts/1/comments", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "folder": null, + "id": "c45d30d7-d9c4-fa05-8110-d6e769bb6ff9", + "name": "PATCH http://localhost:5000/posts/1", + "dataMode": "raw", + "data": [], + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "PATCH", + "pathVariables": {}, + "url": "http://localhost:5000/posts/1", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375", + "rawModeData": "{\n \"title\": \"gfdgsgsdgsdfgsdfgdfg\",\n}" + }, + { + "folder": null, + "id": "e8825dc3-4137-99a7-0000-ef5786610dc3", + "name": "POST http://localhost:5000/posts/1", + "dataMode": "raw", + "data": [], + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "POST", + "pathVariables": {}, + "url": "http://localhost:5000/posts", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375", + "rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}" + }, + { + "folder": null, + "id": "ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2", + "name": "GET http://localhost:5000/posts/1", + "dataMode": "params", + "data": null, + "rawModeData": null, + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "GET", + "pathVariables": {}, + "url": "http://localhost:5000/posts/1", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375" + }, + { + "folder": null, + "id": "fddfc4fa-5114-69e3-4744-203ed71a526b", + "name": "PUT http://localhost:5000/posts/1", + "dataMode": "raw", + "data": [], + "descriptionFormat": "html", + "description": "", + "headers": "", + "method": "PUT", + "pathVariables": {}, + "url": "http://localhost:5000/posts/1", + "preRequestScript": null, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375", + "rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}" } ] } \ No newline at end of file diff --git a/src/Ocelot/Authentication/BearerToken.cs b/src/Ocelot/Authentication/BearerToken.cs new file mode 100644 index 00000000..8ac4e200 --- /dev/null +++ b/src/Ocelot/Authentication/BearerToken.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace Ocelot.Authentication +{ + class BearerToken + { + [JsonProperty("access_token")] + public string AccessToken { get; set; } + + [JsonProperty("expires_in")] + public int ExpiresIn { get; set; } + + [JsonProperty("token_type")] + public string TokenType { get; set; } + } +} \ No newline at end of file diff --git a/src/Ocelot/Controllers/OutputCacheController.cs b/src/Ocelot/Cache/OutputCacheController.cs similarity index 95% rename from src/Ocelot/Controllers/OutputCacheController.cs rename to src/Ocelot/Cache/OutputCacheController.cs index 8d5189c7..2dafcb66 100644 --- a/src/Ocelot/Controllers/OutputCacheController.cs +++ b/src/Ocelot/Cache/OutputCacheController.cs @@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Ocelot.Cache; using Ocelot.Configuration.Provider; -namespace Ocelot.Controllers +namespace Ocelot.Cache { [Authorize] [Route("outputcache")] diff --git a/src/Ocelot/Configuration/Authentication/OcelotResourceOwnerPasswordValidator.cs b/src/Ocelot/Configuration/Authentication/OcelotResourceOwnerPasswordValidator.cs deleted file mode 100644 index 416c8ec2..00000000 --- a/src/Ocelot/Configuration/Authentication/OcelotResourceOwnerPasswordValidator.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using IdentityServer4.Models; -using IdentityServer4.Validation; -using Ocelot.Configuration.Provider; - -namespace Ocelot.Configuration.Authentication -{ - public class OcelotResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator - { - private readonly IHashMatcher _matcher; - private readonly IIdentityServerConfiguration _identityServerConfiguration; - - public OcelotResourceOwnerPasswordValidator(IHashMatcher matcher, IIdentityServerConfiguration identityServerConfiguration) - { - _identityServerConfiguration = identityServerConfiguration; - _matcher = matcher; - } - - public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context) - { - try - { - var user = _identityServerConfiguration.Users.FirstOrDefault(u => u.UserName == context.UserName); - - if(user == null) - { - context.Result = new GrantValidationResult( - TokenRequestErrors.InvalidGrant, - "invalid custom credential"); - } - else if(_matcher.Match(context.Password, user.Salt, user.Hash)) - { - context.Result = new GrantValidationResult( - subject: "admin", - authenticationMethod: "custom"); - } - else - { - context.Result = new GrantValidationResult( - TokenRequestErrors.InvalidGrant, - "invalid custom credential"); - } - } - catch(Exception ex) - { - Console.WriteLine(ex); - } - - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs index 5e3f4b44..eed04cdb 100644 --- a/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/FileOcelotConfigurationCreator.cs @@ -9,6 +9,7 @@ using Ocelot.Configuration.Builder; using Ocelot.Configuration.File; using Ocelot.Configuration.Parser; using Ocelot.Configuration.Validator; +using Ocelot.DependencyInjection; using Ocelot.LoadBalancer; using Ocelot.LoadBalancer.LoadBalancers; using Ocelot.Logging; @@ -35,6 +36,8 @@ namespace Ocelot.Configuration.Creator private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator; private readonly IRegionCreator _regionCreator; private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; + private readonly IAdministrationPath _adminPath; + public FileOcelotConfigurationCreator( IOptions options, @@ -49,9 +52,11 @@ namespace Ocelot.Configuration.Creator IReRouteOptionsCreator fileReRouteOptionsCreator, IRateLimitOptionsCreator rateLimitOptionsCreator, IRegionCreator regionCreator, - IHttpHandlerOptionsCreator httpHandlerOptionsCreator + IHttpHandlerOptionsCreator httpHandlerOptionsCreator, + IAdministrationPath adminPath ) { + _adminPath = adminPath; _regionCreator = regionCreator; _rateLimitOptionsCreator = rateLimitOptionsCreator; _requestIdKeyCreator = requestIdKeyCreator; @@ -92,7 +97,7 @@ namespace Ocelot.Configuration.Creator var serviceProviderConfiguration = _serviceProviderConfigCreator.Create(fileConfiguration.GlobalConfiguration); - var config = new OcelotConfiguration(reRoutes, fileConfiguration.GlobalConfiguration.AdministrationPath, serviceProviderConfiguration); + var config = new OcelotConfiguration(reRoutes, _adminPath.Path, serviceProviderConfiguration); return new OkResponse(config); } diff --git a/src/Ocelot/Configuration/Creator/IdentityServerConfigurationCreator.cs b/src/Ocelot/Configuration/Creator/IdentityServerConfigurationCreator.cs index 6ed3b6c0..c414c0a5 100644 --- a/src/Ocelot/Configuration/Creator/IdentityServerConfigurationCreator.cs +++ b/src/Ocelot/Configuration/Creator/IdentityServerConfigurationCreator.cs @@ -8,29 +8,16 @@ namespace Ocelot.Configuration.Creator { public static class IdentityServerConfigurationCreator { - public static IdentityServerConfiguration GetIdentityServerConfiguration() + public static IdentityServerConfiguration GetIdentityServerConfiguration(string secret) { - var username = Environment.GetEnvironmentVariable("OCELOT_USERNAME"); - var hash = Environment.GetEnvironmentVariable("OCELOT_HASH"); - var salt = Environment.GetEnvironmentVariable("OCELOT_SALT"); var credentialsSigningCertificateLocation = Environment.GetEnvironmentVariable("OCELOT_CERTIFICATE"); var credentialsSigningCertificatePassword = Environment.GetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD"); return new IdentityServerConfiguration( "admin", false, - SupportedTokens.Both, - "secret", + secret, new List { "admin", "openid", "offline_access" }, - "Ocelot Administration", - true, - GrantTypes.ResourceOwnerPassword, - AccessTokenType.Jwt, - false, - new List - { - new User("admin", username, hash, salt) - }, credentialsSigningCertificateLocation, credentialsSigningCertificatePassword ); diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index 4d34f6de..4bb9e191 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -12,7 +12,6 @@ namespace Ocelot.Configuration.File public string RequestIdKey { get; set; } public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;} - public string AdministrationPath {get;set;} public FileRateLimitOptions RateLimitOptions { get; set; } } diff --git a/src/Ocelot/Controllers/FileConfigurationController.cs b/src/Ocelot/Configuration/FileConfigurationController.cs similarity index 55% rename from src/Ocelot/Controllers/FileConfigurationController.cs rename to src/Ocelot/Configuration/FileConfigurationController.cs index c0ba43ea..e17d9e5c 100644 --- a/src/Ocelot/Controllers/FileConfigurationController.cs +++ b/src/Ocelot/Configuration/FileConfigurationController.cs @@ -1,11 +1,15 @@ +using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; using Ocelot.Configuration.File; using Ocelot.Configuration.Provider; using Ocelot.Configuration.Setter; +using Ocelot.Raft; +using Rafty.Concensus; -namespace Ocelot.Controllers +namespace Ocelot.Configuration { [Authorize] [Route("configuration")] @@ -13,11 +17,13 @@ namespace Ocelot.Controllers { private readonly IFileConfigurationProvider _configGetter; private readonly IFileConfigurationSetter _configSetter; + private readonly IServiceProvider _serviceProvider; - public FileConfigurationController(IFileConfigurationProvider getFileConfig, IFileConfigurationSetter configSetter) + public FileConfigurationController(IFileConfigurationProvider getFileConfig, IFileConfigurationSetter configSetter, IServiceProvider serviceProvider) { _configGetter = getFileConfig; _configSetter = configSetter; + _serviceProvider = serviceProvider; } [HttpGet] @@ -36,9 +42,23 @@ namespace Ocelot.Controllers [HttpPost] public async Task Post([FromBody]FileConfiguration fileConfiguration) { + //todo - this code is a bit shit sort it out.. + var test = _serviceProvider.GetService(typeof(INode)); + if (test != null) + { + var node = (INode)test; + var result = node.Accept(new UpdateFileConfiguration(fileConfiguration)); + if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse)) + { + return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub."); + } + + return new OkObjectResult(result.Command.Configuration); + } + var response = await _configSetter.Set(fileConfiguration); - - if(response.IsError) + + if (response.IsError) { return new BadRequestObjectResult(response.Errors); } @@ -46,4 +66,4 @@ namespace Ocelot.Controllers return new OkObjectResult(fileConfiguration); } } -} \ No newline at end of file +} diff --git a/src/Ocelot/Configuration/Provider/IIdentityServerConfiguration.cs b/src/Ocelot/Configuration/Provider/IIdentityServerConfiguration.cs index 0a388abb..a01ed751 100644 --- a/src/Ocelot/Configuration/Provider/IIdentityServerConfiguration.cs +++ b/src/Ocelot/Configuration/Provider/IIdentityServerConfiguration.cs @@ -7,16 +7,9 @@ namespace Ocelot.Configuration.Provider public interface IIdentityServerConfiguration { string ApiName { get; } + string ApiSecret { get; } bool RequireHttps { get; } List AllowedScopes { get; } - SupportedTokens SupportedTokens { get; } - string ApiSecret { get; } - string Description {get;} - bool Enabled {get;} - IEnumerable AllowedGrantTypes {get;} - AccessTokenType AccessTokenType {get;} - bool RequireClientSecret {get;} - List Users {get;} string CredentialsSigningCertificateLocation { get; } string CredentialsSigningCertificatePassword { get; } } diff --git a/src/Ocelot/Configuration/Provider/IdentityServerConfiguration.cs b/src/Ocelot/Configuration/Provider/IdentityServerConfiguration.cs index 881d6f5a..6f62e53c 100644 --- a/src/Ocelot/Configuration/Provider/IdentityServerConfiguration.cs +++ b/src/Ocelot/Configuration/Provider/IdentityServerConfiguration.cs @@ -9,27 +9,15 @@ namespace Ocelot.Configuration.Provider public IdentityServerConfiguration( string apiName, bool requireHttps, - SupportedTokens supportedTokens, string apiSecret, List allowedScopes, - string description, - bool enabled, - IEnumerable grantType, - AccessTokenType accessTokenType, - bool requireClientSecret, - List users, string credentialsSigningCertificateLocation, string credentialsSigningCertificatePassword) + string credentialsSigningCertificateLocation, + string credentialsSigningCertificatePassword) { ApiName = apiName; RequireHttps = requireHttps; - SupportedTokens = supportedTokens; ApiSecret = apiSecret; AllowedScopes = allowedScopes; - Description = description; - Enabled = enabled; - AllowedGrantTypes = grantType; - AccessTokenType = accessTokenType; - RequireClientSecret = requireClientSecret; - Users = users; CredentialsSigningCertificateLocation = credentialsSigningCertificateLocation; CredentialsSigningCertificatePassword = credentialsSigningCertificatePassword; } @@ -37,14 +25,7 @@ namespace Ocelot.Configuration.Provider public string ApiName { get; private set; } public bool RequireHttps { get; private set; } public List AllowedScopes { get; private set; } - public SupportedTokens SupportedTokens { get; private set; } public string ApiSecret { get; private set; } - public string Description {get;private set;} - public bool Enabled {get;private set;} - public IEnumerable AllowedGrantTypes {get;private set;} - public AccessTokenType AccessTokenType {get;private set;} - public bool RequireClientSecret {get;private set;} - public List Users {get;private set;} public string CredentialsSigningCertificateLocation { get; private set; } public string CredentialsSigningCertificatePassword { get; private set; } } diff --git a/src/Ocelot/Configuration/Provider/User.cs b/src/Ocelot/Configuration/Provider/User.cs deleted file mode 100644 index f61ff4e5..00000000 --- a/src/Ocelot/Configuration/Provider/User.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Ocelot.Configuration.Provider -{ - public class User - { - public User(string subject, string userName, string hash, string salt) - { - Subject = subject; - UserName = userName; - Hash = hash; - Salt = salt; - } - public string Subject { get; private set; } - public string UserName { get; private set; } - public string Hash { get; private set; } - public string Salt { get; private set; } - } -} \ No newline at end of file diff --git a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs index 90877af1..2f4a58fb 100644 --- a/src/Ocelot/DependencyInjection/IOcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/IOcelotBuilder.cs @@ -7,5 +7,6 @@ namespace Ocelot.DependencyInjection { IOcelotBuilder AddStoreOcelotConfigurationInConsul(); IOcelotBuilder AddCacheManager(Action settings); + IOcelotAdministrationBuilder AddAdministration(string path, string secret); } } diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index b9801a00..b8ebc8a2 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -14,7 +14,6 @@ using Ocelot.Configuration.Provider; using Ocelot.Configuration.Repository; using Ocelot.Configuration.Setter; using Ocelot.Configuration.Validator; -using Ocelot.Controllers; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.DownstreamUrlCreator; @@ -47,6 +46,12 @@ using Ocelot.Configuration.Builder; using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider; using Microsoft.Extensions.DependencyInjection.Extensions; using System.Linq; +using Ocelot.Raft; +using Rafty.Concensus; +using Rafty.FiniteStateMachine; +using Rafty.Infrastructure; +using Rafty.Log; +using Newtonsoft.Json; namespace Ocelot.DependencyInjection { @@ -121,14 +126,6 @@ namespace Ocelot.DependencyInjection _services.AddMemoryCache(); _services.TryAddSingleton(); - //add identity server for admin area - var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(); - - if (identityServerConfiguration != null) - { - AddIdentityServer(identityServerConfiguration); - } - //add asp.net services.. var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly; @@ -141,6 +138,24 @@ namespace Ocelot.DependencyInjection _services.AddLogging(); _services.AddMiddlewareAnalysis(); _services.AddWebEncoders(); + _services.AddSingleton(new NullAdministrationPath()); + } + + public IOcelotAdministrationBuilder AddAdministration(string path, string secret) + { + var administrationPath = new AdministrationPath(path); + + //add identity server for admin area + var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(secret); + + if (identityServerConfiguration != null) + { + AddIdentityServer(identityServerConfiguration, administrationPath); + } + + var descriptor = new ServiceDescriptor(typeof(IAdministrationPath), administrationPath); + _services.Replace(descriptor); + return new OcelotAdministrationBuilder(_services, _configurationRoot); } public IOcelotBuilder AddStoreOcelotConfigurationInConsul() @@ -185,7 +200,7 @@ namespace Ocelot.DependencyInjection return this; } - private void AddIdentityServer(IIdentityServerConfiguration identityServerConfiguration) + private void AddIdentityServer(IIdentityServerConfiguration identityServerConfiguration, IAdministrationPath adminPath) { _services.TryAddSingleton(identityServerConfiguration); _services.TryAddSingleton(); @@ -194,8 +209,7 @@ namespace Ocelot.DependencyInjection o.IssuerUri = "Ocelot"; }) .AddInMemoryApiResources(Resources(identityServerConfiguration)) - .AddInMemoryClients(Client(identityServerConfiguration)) - .AddResourceOwnerValidator(); + .AddInMemoryClients(Client(identityServerConfiguration)); //todo - refactor a method so we know why this is happening var whb = _services.First(x => x.ServiceType == typeof(IWebHostBuilder)); @@ -206,8 +220,7 @@ namespace Ocelot.DependencyInjection _services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(o => { - var adminPath = _configurationRoot.GetValue("GlobalConfiguration:AdministrationPath", string.Empty); - o.Authority = baseSchemeUrlAndPort + adminPath; + o.Authority = baseSchemeUrlAndPort + adminPath.Path; o.ApiName = identityServerConfiguration.ApiName; o.RequireHttpsMetadata = identityServerConfiguration.RequireHttps; o.SupportedTokens = SupportedTokens.Both; @@ -240,7 +253,7 @@ namespace Ocelot.DependencyInjection Value = identityServerConfiguration.ApiSecret.Sha256() } } - } + }, }; } @@ -251,12 +264,65 @@ namespace Ocelot.DependencyInjection new Client { ClientId = identityServerConfiguration.ApiName, - AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, + AllowedGrantTypes = GrantTypes.ClientCredentials, ClientSecrets = new List {new Secret(identityServerConfiguration.ApiSecret.Sha256())}, AllowedScopes = { identityServerConfiguration.ApiName } } }; } + } + public interface IOcelotAdministrationBuilder + { + IOcelotAdministrationBuilder AddRafty(); + } + + public class OcelotAdministrationBuilder : IOcelotAdministrationBuilder + { + private IServiceCollection _services; + private IConfigurationRoot _configurationRoot; + + public OcelotAdministrationBuilder(IServiceCollection services, IConfigurationRoot configurationRoot) + { + _configurationRoot = configurationRoot; + _services = services; + } + + public IOcelotAdministrationBuilder AddRafty() + { + var settings = new InMemorySettings(4000, 5000, 100, 5000); + _services.AddSingleton(); + _services.AddSingleton(); + _services.AddSingleton(settings); + _services.AddSingleton(); + _services.AddSingleton(); + _services.Configure(_configurationRoot); + return this; + } + } + + public interface IAdministrationPath + { + string Path {get;} + } + + public class NullAdministrationPath : IAdministrationPath + { + public NullAdministrationPath() + { + Path = null; + } + + public string Path {get;private set;} + } + + public class AdministrationPath : IAdministrationPath + { + public AdministrationPath(string path) + { + Path = path; + } + + public string Path {get;private set;} } } diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index 4929f9b2..ab029950 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -7,7 +7,6 @@ using Microsoft.Extensions.DependencyInjection; using Ocelot.Authentication.Middleware; using Ocelot.Cache.Middleware; using Ocelot.Claims.Middleware; -using Ocelot.Controllers; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Errors.Middleware; @@ -23,12 +22,15 @@ using Ocelot.RateLimit.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 Ocelot.Configuration; using Ocelot.Configuration.Creator; using Ocelot.Configuration.File; @@ -36,7 +38,10 @@ namespace Ocelot.Middleware using Ocelot.Configuration.Repository; using Ocelot.Configuration.Setter; using Ocelot.LoadBalancer.Middleware; + using Ocelot.Raft; using Ocelot.Responses; + using Rafty.Concensus; + using Rafty.Infrastructure; public static class OcelotMiddlewareExtensions { @@ -64,6 +69,11 @@ namespace Ocelot.Middleware await CreateAdministrationArea(builder, configuration); + if(UsingRafty(builder)) + { + SetUpRafty(builder); + } + ConfigureDiagnosticListener(builder); // This is registered to catch any global exceptions that are not handled @@ -149,6 +159,26 @@ namespace Ocelot.Middleware return builder; } + private static bool UsingRafty(IApplicationBuilder builder) + { + var possible = builder.ApplicationServices.GetService(typeof(INode)) as INode; + if(possible != null) + { + return true; + } + + return false; + } + + private static void SetUpRafty(IApplicationBuilder builder) + { + var applicationLifetime = (IApplicationLifetime)builder.ApplicationServices.GetService(typeof(IApplicationLifetime)); + applicationLifetime.ApplicationStopping.Register(() => OnShutdown(builder)); + var node = (INode)builder.ApplicationServices.GetService(typeof(INode)); + var nodeId = (NodeId)builder.ApplicationServices.GetService(typeof(NodeId)); + node.Start(nodeId.Id); + } + private static async Task CreateConfiguration(IApplicationBuilder builder) { var deps = GetDependencies(builder); @@ -183,7 +213,7 @@ namespace Ocelot.Middleware return response == null || response.IsError; } - private static bool ConfigurationNotSetUp(Response ocelotConfiguration) + private static bool ConfigurationNotSetUp(Ocelot.Responses.Response ocelotConfiguration) { return ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError; } @@ -247,6 +277,7 @@ namespace Ocelot.Middleware return new ErrorResponse(ocelotConfig.Errors); } config = await ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data); + //todo - this starts the poller if it has been registered...please this is so bad. var hack = builder.ApplicationServices.GetService(typeof(ConsulFileConfigurationPoller)); } @@ -292,5 +323,11 @@ namespace Ocelot.Middleware diagnosticListener.SubscribeWithAdapter(listener); } } + + private static void OnShutdown(IApplicationBuilder app) + { + var node = (INode)app.ApplicationServices.GetService(typeof(INode)); + node.Stop(); + } } } diff --git a/src/Ocelot/Ocelot.csproj b/src/Ocelot/Ocelot.csproj index ca664327..adabe885 100644 --- a/src/Ocelot/Ocelot.csproj +++ b/src/Ocelot/Ocelot.csproj @@ -1,5 +1,4 @@ - - + netcoreapp2.0 2.0.0 @@ -11,39 +10,37 @@ Ocelot API Gateway;.NET core https://github.com/TomPallister/Ocelot - https://github.com/TomPallister/Ocelot + https://github.com/TomPallister/Ocelot win10-x64;osx.10.11-x64;osx.10.12-x64;win7-x64 false false - True + True false - Tom Pallister + Tom Pallister - full True - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - + \ No newline at end of file diff --git a/src/Ocelot/Raft/ExcludeFromCoverage.cs b/src/Ocelot/Raft/ExcludeFromCoverage.cs new file mode 100644 index 00000000..9ea5544a --- /dev/null +++ b/src/Ocelot/Raft/ExcludeFromCoverage.cs @@ -0,0 +1,7 @@ +using System; + +namespace Ocelot.Raft +{ + [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property)] + public class ExcludeFromCoverageAttribute : Attribute{} +} \ No newline at end of file diff --git a/src/Ocelot/Raft/FakeCommand.cs b/src/Ocelot/Raft/FakeCommand.cs new file mode 100644 index 00000000..b8699c5e --- /dev/null +++ b/src/Ocelot/Raft/FakeCommand.cs @@ -0,0 +1,15 @@ +using Rafty.FiniteStateMachine; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class FakeCommand : ICommand + { + public FakeCommand(string value) + { + this.Value = value; + + } + public string Value { get; private set; } + } +} diff --git a/src/Ocelot/Raft/FileFsm.cs b/src/Ocelot/Raft/FileFsm.cs new file mode 100644 index 00000000..dbae10da --- /dev/null +++ b/src/Ocelot/Raft/FileFsm.cs @@ -0,0 +1,33 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using Rafty.FiniteStateMachine; +using Rafty.Infrastructure; +using Rafty.Log; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class FileFsm : IFiniteStateMachine + { + private string _id; + + public FileFsm(NodeId nodeId) + { + _id = nodeId.Id.Replace("/","").Replace(":",""); + } + + public void Handle(LogEntry log) + { + try + { + var json = JsonConvert.SerializeObject(log.CommandData); + File.AppendAllText(_id, json); + } + catch(Exception exception) + { + Console.WriteLine(exception); + } + } + } +} diff --git a/src/Ocelot/Raft/FilePeer.cs b/src/Ocelot/Raft/FilePeer.cs new file mode 100644 index 00000000..f983d3cc --- /dev/null +++ b/src/Ocelot/Raft/FilePeer.cs @@ -0,0 +1,8 @@ +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class FilePeer + { + public string HostAndPort { get; set; } + } +} diff --git a/src/Ocelot/Raft/FilePeers.cs b/src/Ocelot/Raft/FilePeers.cs new file mode 100644 index 00000000..0aab1df4 --- /dev/null +++ b/src/Ocelot/Raft/FilePeers.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class FilePeers + { + public FilePeers() + { + Peers = new List(); + } + + public List Peers {get; set;} + } +} diff --git a/src/Ocelot/Raft/FilePeersProvider.cs b/src/Ocelot/Raft/FilePeersProvider.cs new file mode 100644 index 00000000..413fdb42 --- /dev/null +++ b/src/Ocelot/Raft/FilePeersProvider.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Options; +using Ocelot.Configuration; +using Ocelot.Configuration.Provider; +using Rafty.Concensus; +using Rafty.Infrastructure; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class FilePeersProvider : IPeersProvider + { + private readonly IOptions _options; + private List _peers; + private IWebHostBuilder _builder; + private IOcelotConfigurationProvider _provider; + private IIdentityServerConfiguration _identityServerConfig; + + public FilePeersProvider(IOptions options, IWebHostBuilder builder, IOcelotConfigurationProvider provider, IIdentityServerConfiguration identityServerConfig) + { + _identityServerConfig = identityServerConfig; + _provider = provider; + _builder = builder; + _options = options; + _peers = new List(); + //todo - sort out async nonsense.. + var config = _provider.Get().GetAwaiter().GetResult(); + foreach (var item in _options.Value.Peers) + { + var httpClient = new HttpClient(); + //todo what if this errors? + var httpPeer = new HttpPeer(item.HostAndPort, httpClient, _builder, config.Data, _identityServerConfig); + _peers.Add(httpPeer); + } + } + public List Get() + { + return _peers; + } + } +} diff --git a/src/Ocelot/Raft/HttpPeer.cs b/src/Ocelot/Raft/HttpPeer.cs new file mode 100644 index 00000000..8ba8fe70 --- /dev/null +++ b/src/Ocelot/Raft/HttpPeer.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Newtonsoft.Json; +using Ocelot.Authentication; +using Ocelot.Configuration; +using Ocelot.Configuration.Provider; +using Rafty.Concensus; +using Rafty.FiniteStateMachine; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class HttpPeer : IPeer + { + private string _hostAndPort; + private HttpClient _httpClient; + private JsonSerializerSettings _jsonSerializerSettings; + private string _baseSchemeUrlAndPort; + private BearerToken _token; + private IOcelotConfiguration _config; + private IIdentityServerConfiguration _identityServerConfiguration; + + public HttpPeer(string hostAndPort, HttpClient httpClient, IWebHostBuilder builder, IOcelotConfiguration config, IIdentityServerConfiguration identityServerConfiguration) + { + _identityServerConfiguration = identityServerConfiguration; + _config = config; + Id = hostAndPort; + _hostAndPort = hostAndPort; + _httpClient = httpClient; + _jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + _baseSchemeUrlAndPort = builder.GetSetting(WebHostDefaults.ServerUrlsKey); + } + + public string Id {get; private set;} + + public RequestVoteResponse Request(RequestVote requestVote) + { + if(_token == null) + { + SetToken(); + } + + var json = JsonConvert.SerializeObject(requestVote, _jsonSerializerSettings); + var content = new StringContent(json); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult(); + if(response.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); + } + else + { + return new RequestVoteResponse(false, requestVote.Term); + } + } + + public AppendEntriesResponse Request(AppendEntries appendEntries) + { + try + { + if(_token == null) + { + SetToken(); + } + var json = JsonConvert.SerializeObject(appendEntries, _jsonSerializerSettings); + var content = new StringContent(json); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult(); + if(response.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings); + } + else + { + return new AppendEntriesResponse(appendEntries.Term, false); + } + } + catch(Exception ex) + { + Console.WriteLine(ex); + return new AppendEntriesResponse(appendEntries.Term, false); + } + } + + public Response Request(T command) where T : ICommand + { + if(_token == null) + { + SetToken(); + } + var json = JsonConvert.SerializeObject(command, _jsonSerializerSettings); + var content = new StringContent(json); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/command", content).GetAwaiter().GetResult(); + if(response.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings); + } + else + { + return new ErrorResponse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command); + } + } + + private void SetToken() + { + var tokenUrl = $"{_baseSchemeUrlAndPort}{_config.AdministrationPath}/connect/token"; + var formData = new List> + { + new KeyValuePair("client_id", _identityServerConfiguration.ApiName), + new KeyValuePair("client_secret", _identityServerConfiguration.ApiSecret), + new KeyValuePair("scope", _identityServerConfiguration.ApiName), + new KeyValuePair("grant_type", "client_credentials") + }; + var content = new FormUrlEncodedContent(formData); + var response = _httpClient.PostAsync(tokenUrl, content).GetAwaiter().GetResult(); + var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + _token = JsonConvert.DeserializeObject(responseContent); + _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_token.TokenType, _token.AccessToken); + } + } +} diff --git a/src/Ocelot/Raft/OcelotFiniteStateMachine.cs b/src/Ocelot/Raft/OcelotFiniteStateMachine.cs new file mode 100644 index 00000000..96a9ceb1 --- /dev/null +++ b/src/Ocelot/Raft/OcelotFiniteStateMachine.cs @@ -0,0 +1,25 @@ +using Ocelot.Configuration.Setter; +using Rafty.FiniteStateMachine; +using Rafty.Log; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class OcelotFiniteStateMachine : IFiniteStateMachine + { + private IFileConfigurationSetter _setter; + + public OcelotFiniteStateMachine(IFileConfigurationSetter setter) + { + _setter = setter; + } + + public void Handle(LogEntry log) + { + //todo - handle an error + //hack it to just cast as at the moment we know this is the only command :P + var hack = (UpdateFileConfiguration)log.CommandData; + _setter.Set(hack.Configuration).GetAwaiter().GetResult();; + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Raft/RaftController.cs b/src/Ocelot/Raft/RaftController.cs new file mode 100644 index 00000000..08ee0c34 --- /dev/null +++ b/src/Ocelot/Raft/RaftController.cs @@ -0,0 +1,84 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Ocelot.Logging; +using Ocelot.Raft; +using Rafty.Concensus; +using Rafty.FiniteStateMachine; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + [Authorize] + [Route("raft")] + public class RaftController : Controller + { + private readonly INode _node; + private IOcelotLogger _logger; + private string _baseSchemeUrlAndPort; + private JsonSerializerSettings _jsonSerialiserSettings; + + public RaftController(INode node, IOcelotLoggerFactory loggerFactory, IWebHostBuilder builder) + { + _jsonSerialiserSettings = new JsonSerializerSettings { + TypeNameHandling = TypeNameHandling.All + }; + _baseSchemeUrlAndPort = builder.GetSetting(WebHostDefaults.ServerUrlsKey); + _logger = loggerFactory.CreateLogger(); + _node = node; + } + + [Route("appendentries")] + public async Task AppendEntries() + { + using(var reader = new StreamReader(HttpContext.Request.Body)) + { + var json = await reader.ReadToEndAsync(); + var appendEntries = JsonConvert.DeserializeObject(json, _jsonSerialiserSettings); + _logger.LogDebug($"{_baseSchemeUrlAndPort}/appendentries called, my state is {_node.State.GetType().FullName}"); + var appendEntriesResponse = _node.Handle(appendEntries); + return new OkObjectResult(appendEntriesResponse); + } + } + + [Route("requestvote")] + public async Task RequestVote() + { + using(var reader = new StreamReader(HttpContext.Request.Body)) + { + var json = await reader.ReadToEndAsync(); + var requestVote = JsonConvert.DeserializeObject(json, _jsonSerialiserSettings); + _logger.LogDebug($"{_baseSchemeUrlAndPort}/requestvote called, my state is {_node.State.GetType().FullName}"); + var requestVoteResponse = _node.Handle(requestVote); + return new OkObjectResult(requestVoteResponse); + } + } + + [Route("command")] + public async Task Command() + { + try + { + using(var reader = new StreamReader(HttpContext.Request.Body)) + { + var json = await reader.ReadToEndAsync(); + var command = JsonConvert.DeserializeObject(json, _jsonSerialiserSettings); + _logger.LogDebug($"{_baseSchemeUrlAndPort}/command called, my state is {_node.State.GetType().FullName}"); + var commandResponse = _node.Accept(command); + json = JsonConvert.SerializeObject(commandResponse, _jsonSerialiserSettings); + return StatusCode(200, json); + } + } + catch(Exception e) + { + _logger.LogError($"THERE WAS A PROBLEM ON NODE {_node.State.CurrentState.Id}", e); + throw e; + } + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Raft/SqlLiteLog.cs b/src/Ocelot/Raft/SqlLiteLog.cs new file mode 100644 index 00000000..aaa1e726 --- /dev/null +++ b/src/Ocelot/Raft/SqlLiteLog.cs @@ -0,0 +1,279 @@ +using System.IO; +using Rafty.Log; +using Microsoft.Data.Sqlite; +using Newtonsoft.Json; +using System; +using Rafty.Infrastructure; +using System.Collections.Generic; + +namespace Ocelot.Raft +{ + [ExcludeFromCoverage] + public class SqlLiteLog : ILog + { + private string _path; + private readonly object _lock = new object(); + + public SqlLiteLog(NodeId nodeId) + { + _path = $"{nodeId.Id.Replace("/","").Replace(":","")}.db"; + if(!File.Exists(_path)) + { + lock(_lock) + { + FileStream fs = File.Create(_path); + fs.Dispose(); + } + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + var sql = @"create table logs ( + id integer primary key, + data text not null + )"; + using(var command = new SqliteCommand(sql, connection)) + { + var result = command.ExecuteNonQuery(); + } + } + } + } + + public int LastLogIndex + { + get + { + lock(_lock) + { + var result = 1; + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + var sql = @"select id from logs order by id desc limit 1"; + using(var command = new SqliteCommand(sql, connection)) + { + var index = Convert.ToInt32(command.ExecuteScalar()); + if(index > result) + { + result = index; + } + } + } + return result; + } + } + } + + public long LastLogTerm + { + get + { + lock(_lock) + { + long result = 0; + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + var sql = @"select data from logs order by id desc limit 1"; + using(var command = new SqliteCommand(sql, connection)) + { + var data = Convert.ToString(command.ExecuteScalar()); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + if(log != null && log.Term > result) + { + result = log.Term; + } + } + } + return result; + } + } + } + + public int Count + { + get + { + lock(_lock) + { + var result = 0; + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + var sql = @"select count(id) from logs"; + using(var command = new SqliteCommand(sql, connection)) + { + var index = Convert.ToInt32(command.ExecuteScalar()); + if(index > result) + { + result = index; + } + } + } + return result; + } + } + } + + public int Apply(LogEntry log) + { + lock(_lock) + { + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var data = JsonConvert.SerializeObject(log, jsonSerializerSettings); + //todo - sql injection dont copy this.. + var sql = $"insert into logs (data) values ('{data}')"; + using(var command = new SqliteCommand(sql, connection)) + { + var result = command.ExecuteNonQuery(); + } + + sql = "select last_insert_rowid()"; + using(var command = new SqliteCommand(sql, connection)) + { + var result = command.ExecuteScalar(); + return Convert.ToInt32(result); + } + } + } + } + + public void DeleteConflictsFromThisLog(int index, LogEntry logEntry) + { + lock(_lock) + { + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + //todo - sql injection dont copy this.. + var sql = $"select data from logs where id = {index};"; + using(var command = new SqliteCommand(sql, connection)) + { + var data = Convert.ToString(command.ExecuteScalar()); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + if(logEntry != null && log != null && logEntry.Term != log.Term) + { + //todo - sql injection dont copy this.. + var deleteSql = $"delete from logs where id >= {index};"; + using(var deleteCommand = new SqliteCommand(deleteSql, connection)) + { + var result = deleteCommand.ExecuteNonQuery(); + } + } + } + } + } + } + + public LogEntry Get(int index) + { + lock(_lock) + { + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + //todo - sql injection dont copy this.. + var sql = $"select data from logs where id = {index}"; + using(var command = new SqliteCommand(sql, connection)) + { + var data = Convert.ToString(command.ExecuteScalar()); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + return log; + } + } + } + } + + public System.Collections.Generic.List<(int index, LogEntry logEntry)> GetFrom(int index) + { + lock(_lock) + { + var logsToReturn = new List<(int, LogEntry)>(); + + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + //todo - sql injection dont copy this.. + var sql = $"select id, data from logs where id >= {index}"; + using(var command = new SqliteCommand(sql, connection)) + { + using(var reader = command.ExecuteReader()) + { + while(reader.Read()) + { + var id = Convert.ToInt32(reader[0]); + var data = (string)reader[1]; + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + logsToReturn.Add((id, log)); + + } + } + } + } + + return logsToReturn; + } + + } + + public long GetTermAtIndex(int index) + { + lock(_lock) + { + long result = 0; + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + //todo - sql injection dont copy this.. + var sql = $"select data from logs where id = {index}"; + using(var command = new SqliteCommand(sql, connection)) + { + var data = Convert.ToString(command.ExecuteScalar()); + var jsonSerializerSettings = new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }; + var log = JsonConvert.DeserializeObject(data, jsonSerializerSettings); + if(log != null && log.Term > result) + { + result = log.Term; + } + } + } + return result; + } + } + public void Remove(int indexOfCommand) + { + lock(_lock) + { + using(var connection = new SqliteConnection($"Data Source={_path};")) + { + connection.Open(); + //todo - sql injection dont copy this.. + var deleteSql = $"delete from logs where id >= {indexOfCommand};"; + using(var deleteCommand = new SqliteCommand(deleteSql, connection)) + { + var result = deleteCommand.ExecuteNonQuery(); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Raft/UpdateFileConfiguration.cs b/src/Ocelot/Raft/UpdateFileConfiguration.cs new file mode 100644 index 00000000..39ed73f9 --- /dev/null +++ b/src/Ocelot/Raft/UpdateFileConfiguration.cs @@ -0,0 +1,15 @@ +using Ocelot.Configuration.File; +using Rafty.FiniteStateMachine; + +namespace Ocelot.Raft +{ + public class UpdateFileConfiguration : ICommand + { + public UpdateFileConfiguration(FileConfiguration configuration) + { + Configuration = configuration; + } + + public FileConfiguration Configuration {get;private set;} + } +} \ No newline at end of file diff --git a/test/Ocelot.AcceptanceTests/Startup.cs b/test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs similarity index 90% rename from test/Ocelot.AcceptanceTests/Startup.cs rename to test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs index 9c8a6e77..bae6fb34 100644 --- a/test/Ocelot.AcceptanceTests/Startup.cs +++ b/test/Ocelot.AcceptanceTests/AcceptanceTestsStartup.cs @@ -12,9 +12,9 @@ using Ocelot.AcceptanceTests.Caching; namespace Ocelot.AcceptanceTests { - public class Startup + public class AcceptanceTestsStartup { - public Startup(IHostingEnvironment env) + public AcceptanceTestsStartup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) @@ -41,7 +41,7 @@ namespace Ocelot.AcceptanceTests } } - public class Startup_WithCustomCacheHandle : Startup + public class Startup_WithCustomCacheHandle : AcceptanceTestsStartup { public Startup_WithCustomCacheHandle(IHostingEnvironment env) : base(env) { } @@ -60,7 +60,7 @@ namespace Ocelot.AcceptanceTests } } - public class Startup_WithConsul_And_CustomCacheHandle : Startup + public class Startup_WithConsul_And_CustomCacheHandle : AcceptanceTestsStartup { public Startup_WithConsul_And_CustomCacheHandle(IHostingEnvironment env) : base(env) { } diff --git a/test/Ocelot.AcceptanceTests/Steps.cs b/test/Ocelot.AcceptanceTests/Steps.cs index 83fe8456..71d3fb16 100644 --- a/test/Ocelot.AcceptanceTests/Steps.cs +++ b/test/Ocelot.AcceptanceTests/Steps.cs @@ -83,7 +83,7 @@ namespace Ocelot.AcceptanceTests }); _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + .UseStartup()); _ocelotClient = _ocelotServer.CreateClient(); } @@ -103,7 +103,7 @@ namespace Ocelot.AcceptanceTests }); _ocelotServer = new TestServer(_webHostBuilder - .UseStartup()); + .UseStartup()); _ocelotClient = _ocelotServer.CreateClient(); } @@ -157,7 +157,6 @@ namespace Ocelot.AcceptanceTests { var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); - response.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath); response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index 240b7c25..b7c2813e 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -39,13 +39,7 @@ namespace Ocelot.IntegrationTests [Fact] public void should_return_response_401_with_call_re_routes_controller() { - var configuration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - AdministrationPath = "/administration" - } - }; + var configuration = new FileConfiguration(); this.Given(x => GivenThereIsAConfiguration(configuration)) .And(x => GivenOcelotIsRunning()) @@ -57,13 +51,7 @@ namespace Ocelot.IntegrationTests [Fact] public void should_return_response_200_with_call_re_routes_controller() { - var configuration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - AdministrationPath = "/administration" - } - }; + var configuration = new FileConfiguration(); this.Given(x => GivenThereIsAConfiguration(configuration)) .And(x => GivenOcelotIsRunning()) @@ -77,13 +65,7 @@ namespace Ocelot.IntegrationTests [Fact] public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b() { - var configuration = new FileConfiguration - { - GlobalConfiguration = new FileGlobalConfiguration - { - AdministrationPath = "/administration" - } - }; + var configuration = new FileConfiguration(); this.Given(x => GivenThereIsAConfiguration(configuration)) .And(x => GivenIdentityServerSigningEnvironmentalVariablesAreSet()) @@ -102,7 +84,6 @@ namespace Ocelot.IntegrationTests { GlobalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "/administration", RequestIdKey = "RequestId", ServiceDiscoveryProvider = new FileServiceDiscoveryProvider { @@ -160,7 +141,6 @@ namespace Ocelot.IntegrationTests { GlobalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "/administration" }, ReRoutes = new List() { @@ -189,7 +169,6 @@ namespace Ocelot.IntegrationTests { GlobalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "/administration" }, ReRoutes = new List() { @@ -234,7 +213,6 @@ namespace Ocelot.IntegrationTests { GlobalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "/administration" }, ReRoutes = new List() { @@ -289,7 +267,7 @@ namespace Ocelot.IntegrationTests .ConfigureServices(x => { x.AddSingleton(_webHostBuilderTwo); }) - .UseStartup(); + .UseStartup(); _builderTwo = _webHostBuilderTwo.Build(); @@ -327,7 +305,6 @@ namespace Ocelot.IntegrationTests { var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); - response.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath); response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); @@ -356,9 +333,7 @@ namespace Ocelot.IntegrationTests new KeyValuePair("client_id", "admin"), new KeyValuePair("client_secret", "secret"), new KeyValuePair("scope", "admin"), - new KeyValuePair("username", "admin"), - new KeyValuePair("password", "secret"), - new KeyValuePair("grant_type", "password") + new KeyValuePair("grant_type", "client_credentials") }; var content = new FormUrlEncodedContent(formData); @@ -380,7 +355,7 @@ namespace Ocelot.IntegrationTests .ConfigureServices(x => { x.AddSingleton(_webHostBuilder); }) - .UseStartup(); + .UseStartup(); _builder = _webHostBuilder.Build(); diff --git a/test/Ocelot.IntegrationTests/Startup.cs b/test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs similarity index 85% rename from test/Ocelot.IntegrationTests/Startup.cs rename to test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs index 60b99f02..097c1b5c 100644 --- a/test/Ocelot.IntegrationTests/Startup.cs +++ b/test/Ocelot.IntegrationTests/IntegrationTestsStartup.cs @@ -11,9 +11,9 @@ using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBui namespace Ocelot.IntegrationTests { - public class Startup + public class IntegrationTestsStartup { - public Startup(IHostingEnvironment env) + public IntegrationTestsStartup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) @@ -38,7 +38,9 @@ namespace Ocelot.IntegrationTests .WithDictionaryHandle(); }; - services.AddOcelot(Configuration); + services.AddOcelot(Configuration) + .AddCacheManager(settings) + .AddAdministration("/administration", "secret"); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) diff --git a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj index 5d1d4841..bf5b2fe4 100644 --- a/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj +++ b/test/Ocelot.IntegrationTests/Ocelot.IntegrationTests.csproj @@ -1,5 +1,4 @@ - - + 0.0.0-dev netcoreapp2.0 @@ -13,39 +12,35 @@ false false - - + PreserveNewest - - + - - - + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - + \ No newline at end of file diff --git a/test/Ocelot.IntegrationTests/RaftStartup.cs b/test/Ocelot.IntegrationTests/RaftStartup.cs new file mode 100644 index 00000000..25015358 --- /dev/null +++ b/test/Ocelot.IntegrationTests/RaftStartup.cs @@ -0,0 +1,55 @@ +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 IConfigurationRoot 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) + { + + //this is from Ocelot...so we need to move stuff below into it... + loggerFactory.AddConsole(Configuration.GetSection("Logging")); + app.UseOcelot().Wait(); + } + } +} diff --git a/test/Ocelot.IntegrationTests/RaftTests.cs b/test/Ocelot.IntegrationTests/RaftTests.cs new file mode 100644 index 00000000..528b6d20 --- /dev/null +++ b/test/Ocelot.IntegrationTests/RaftTests.cs @@ -0,0 +1,431 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Ocelot.Configuration.File; +using Ocelot.Raft; +using Rafty.Concensus; +using Rafty.FiniteStateMachine; +using Rafty.Infrastructure; +using Shouldly; +using Xunit; +using static Rafty.Infrastructure.Wait; +using Microsoft.Data.Sqlite; + +namespace Ocelot.IntegrationTests +{ + public class RaftTests : IDisposable + { + private List _builders; + private List _webHostBuilders; + private List _threads; + private FilePeers _peers; + private HttpClient _httpClient; + private HttpClient _httpClientForAssertions; + private string _ocelotBaseUrl; + private BearerToken _token; + private HttpResponseMessage _response; + private static object _lock = new object(); + + public RaftTests() + { + _httpClientForAssertions = new HttpClient(); + _httpClient = new HttpClient(); + _ocelotBaseUrl = "http://localhost:5000"; + _httpClient.BaseAddress = new Uri(_ocelotBaseUrl); + _webHostBuilders = new List(); + _builders = new List(); + _threads = new List(); + } + public void Dispose() + { + foreach (var builder in _builders) + { + builder?.Dispose(); + } + + foreach (var peer in _peers.Peers) + { + File.Delete(peer.HostAndPort.Replace("/","").Replace(":","")); + File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"); + } + } + + [Fact] + public void should_persist_command_to_five_servers() + { + var configuration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + } + }; + + var updatedConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHost = "127.0.0.1", + DownstreamPort = 80, + DownstreamScheme = "http", + DownstreamPathTemplate = "/geoffrey", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHost = "123.123.123", + DownstreamPort = 443, + DownstreamScheme = "https", + DownstreamPathTemplate = "/blooper/{productId}", + UpstreamHttpMethod = new List { "post" }, + UpstreamPathTemplate = "/test" + } + } + }; + + var command = new UpdateFileConfiguration(updatedConfiguration); + GivenThereIsAConfiguration(configuration); + GivenFiveServersAreRunning(); + GivenALeaderIsElected(); + GivenIHaveAnOcelotToken("/administration"); + WhenISendACommandIntoTheCluster(command); + ThenTheCommandIsReplicatedToAllStateMachines(command); + } + + [Fact] + public void should_persist_command_to_five_servers_when_using_administration_api() + { + var configuration = new FileConfiguration + { + }; + + var updatedConfiguration = new FileConfiguration + { + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHost = "127.0.0.1", + DownstreamPort = 80, + DownstreamScheme = "http", + DownstreamPathTemplate = "/geoffrey", + UpstreamHttpMethod = new List { "get" }, + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHost = "123.123.123", + DownstreamPort = 443, + DownstreamScheme = "https", + DownstreamPathTemplate = "/blooper/{productId}", + UpstreamHttpMethod = new List { "post" }, + UpstreamPathTemplate = "/test" + } + } + }; + + var command = new UpdateFileConfiguration(updatedConfiguration); + GivenThereIsAConfiguration(configuration); + GivenFiveServersAreRunning(); + GivenALeaderIsElected(); + GivenIHaveAnOcelotToken("/administration"); + GivenIHaveAddedATokenToMyRequest(); + WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration); + ThenTheCommandIsReplicatedToAllStateMachines(command); + } + + private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command) + { + var p = _peers.Peers.First(); + var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }); + var httpContent = new StringContent(json); + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + using(var httpClient = new HttpClient()) + { + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + var result = JsonConvert.DeserializeObject>(content); + result.Command.Configuration.ReRoutes.Count.ShouldBe(2); + } + + //dirty sleep to make sure command replicated... + var stopwatch = Stopwatch.StartNew(); + while(stopwatch.ElapsedMilliseconds < 10000) + { + + } + } + + private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expected) + { + //dirty sleep to give a chance to replicate... + var stopwatch = Stopwatch.StartNew(); + while(stopwatch.ElapsedMilliseconds < 2000) + { + + } + + bool CommandCalledOnAllStateMachines() + { + try + { + var passed = 0; + foreach (var peer in _peers.Peers) + { + var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db"; + using(var connection = new SqliteConnection($"Data Source={path};")) + { + connection.Open(); + var sql = @"select count(id) from logs"; + using(var command = new SqliteCommand(sql, connection)) + { + var index = Convert.ToInt32(command.ExecuteScalar()); + index.ShouldBe(1); + } + } + _httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result; + var json = result.Content.ReadAsStringAsync().Result; + var response = JsonConvert.DeserializeObject(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All}); + response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.Configuration.GlobalConfiguration.RequestIdKey); + response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host); + response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port); + + for (var i = 0; i < response.ReRoutes.Count; i++) + { + response.ReRoutes[i].DownstreamHost.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamHost); + response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamPathTemplate); + response.ReRoutes[i].DownstreamPort.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamPort); + response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamScheme); + response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.Configuration.ReRoutes[i].UpstreamPathTemplate); + response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.Configuration.ReRoutes[i].UpstreamHttpMethod); + } + passed++; + } + + return passed == 5; + } + catch(Exception e) + { + Console.WriteLine(e); + return false; + } + } + + var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines()); + commandOnAllStateMachines.ShouldBeTrue(); + } + + private void ThenTheResponseShouldBe(FileConfiguration expected) + { + var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); + + response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); + response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); + response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); + + for (var i = 0; i < response.ReRoutes.Count; i++) + { + response.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost); + response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate); + response.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort); + response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme); + response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.ReRoutes[i].UpstreamPathTemplate); + response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.ReRoutes[i].UpstreamHttpMethod); + } + } + + private void WhenIGetUrlOnTheApiGateway(string url) + { + _response = _httpClient.GetAsync(url).Result; + } + + private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) + { + var json = JsonConvert.SerializeObject(updatedConfiguration); + var content = new StringContent(json); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + _response = _httpClient.PostAsync(url, content).Result; + } + + private void GivenIHaveAddedATokenToMyRequest() + { + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + } + + private void GivenIHaveAnOcelotToken(string adminPath) + { + var tokenUrl = $"{adminPath}/connect/token"; + var formData = new List> + { + new KeyValuePair("client_id", "admin"), + new KeyValuePair("client_secret", "secret"), + new KeyValuePair("scope", "admin"), + new KeyValuePair("grant_type", "client_credentials") + }; + var content = new FormUrlEncodedContent(formData); + + var response = _httpClient.PostAsync(tokenUrl, content).Result; + var responseContent = response.Content.ReadAsStringAsync().Result; + response.EnsureSuccessStatusCode(); + _token = JsonConvert.DeserializeObject(responseContent); + var configPath = $"{adminPath}/.well-known/openid-configuration"; + response = _httpClient.GetAsync(configPath).Result; + response.EnsureSuccessStatusCode(); + } + + private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration) + { + var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json"; + + var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); + + if (File.Exists(configurationPath)) + { + File.Delete(configurationPath); + } + + File.WriteAllText(configurationPath, jsonConfiguration); + + var text = File.ReadAllText(configurationPath); + + configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; + + if (File.Exists(configurationPath)) + { + File.Delete(configurationPath); + } + + File.WriteAllText(configurationPath, jsonConfiguration); + + text = File.ReadAllText(configurationPath); + } + + private void GivenAServerIsRunning(string url) + { + lock(_lock) + { + IWebHostBuilder webHostBuilder = new WebHostBuilder(); + webHostBuilder.UseUrls(url) + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureServices(x => + { + x.AddSingleton(webHostBuilder); + x.AddSingleton(new NodeId(url)); + }) + .UseStartup(); + + var builder = webHostBuilder.Build(); + builder.Start(); + + _webHostBuilders.Add(webHostBuilder); + _builders.Add(builder); + } + } + + private void GivenFiveServersAreRunning() + { + var bytes = File.ReadAllText("peers.json"); + _peers = JsonConvert.DeserializeObject(bytes); + + foreach (var peer in _peers.Peers) + { + var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort)); + thread.Start(); + _threads.Add(thread); + } + } + + private void GivenALeaderIsElected() + { + //dirty sleep to make sure we have a leader + var stopwatch = Stopwatch.StartNew(); + while(stopwatch.ElapsedMilliseconds < 20000) + { + + } + } + + private void WhenISendACommandIntoTheCluster(FakeCommand command) + { + var p = _peers.Peers.First(); + var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() { + TypeNameHandling = TypeNameHandling.All + }); + var httpContent = new StringContent(json); + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + using(var httpClient = new HttpClient()) + { + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); + var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + var result = JsonConvert.DeserializeObject>(content); + result.Command.Value.ShouldBe(command.Value); + } + + //dirty sleep to make sure command replicated... + var stopwatch = Stopwatch.StartNew(); + while(stopwatch.ElapsedMilliseconds < 10000) + { + + } + } + + private void ThenTheCommandIsReplicatedToAllStateMachines(FakeCommand command) + { + //dirty sleep to give a chance to replicate... + var stopwatch = Stopwatch.StartNew(); + while(stopwatch.ElapsedMilliseconds < 2000) + { + + } + + bool CommandCalledOnAllStateMachines() + { + try + { + var passed = 0; + foreach (var peer in _peers.Peers) + { + string fsmData; + fsmData = File.ReadAllText(peer.HostAndPort.Replace("/","").Replace(":","")); + fsmData.ShouldNotBeNullOrEmpty(); + var fakeCommand = JsonConvert.DeserializeObject(fsmData); + fakeCommand.Value.ShouldBe(command.Value); + passed++; + } + + return passed == 5; + } + catch(Exception e) + { + return false; + } + } + + var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines()); + commandOnAllStateMachines.ShouldBeTrue(); + } + } +} diff --git a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs index 301d21d6..51901026 100644 --- a/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs +++ b/test/Ocelot.IntegrationTests/ThreadSafeHeadersTests.cs @@ -95,7 +95,7 @@ namespace Ocelot.IntegrationTests { x.AddSingleton(_webHostBuilder); }) - .UseStartup(); + .UseStartup(); _builder = _webHostBuilder.Build(); diff --git a/test/Ocelot.IntegrationTests/peers.json b/test/Ocelot.IntegrationTests/peers.json new file mode 100644 index 00000000..d81d183f --- /dev/null +++ b/test/Ocelot.IntegrationTests/peers.json @@ -0,0 +1,18 @@ +{ + "Peers": [{ + "HostAndPort": "http://localhost:5000" + }, + { + "HostAndPort": "http://localhost:5002" + }, + { + "HostAndPort": "http://localhost:5003" + }, + { + "HostAndPort": "http://localhost:5004" + }, + { + "HostAndPort": "http://localhost:5001" + } + ] +} \ No newline at end of file diff --git a/test/Ocelot.ManualTest/Startup.cs b/test/Ocelot.ManualTest/ManualTestStartup.cs similarity index 89% rename from test/Ocelot.ManualTest/Startup.cs rename to test/Ocelot.ManualTest/ManualTestStartup.cs index d18b5baf..ac48f67e 100644 --- a/test/Ocelot.ManualTest/Startup.cs +++ b/test/Ocelot.ManualTest/ManualTestStartup.cs @@ -11,9 +11,9 @@ using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBui namespace Ocelot.ManualTest { - public class Startup + public class ManualTestStartup { - public Startup(IHostingEnvironment env) + public ManualTestStartup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) @@ -45,7 +45,8 @@ namespace Ocelot.ManualTest x.Audience = "test"; }); - services.AddOcelot(Configuration); + services.AddOcelot(Configuration) + .AddAdministration("/administration", "secret"); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) diff --git a/test/Ocelot.ManualTest/Program.cs b/test/Ocelot.ManualTest/Program.cs index 98b1f927..9fe2b4f0 100644 --- a/test/Ocelot.ManualTest/Program.cs +++ b/test/Ocelot.ManualTest/Program.cs @@ -15,7 +15,7 @@ namespace Ocelot.ManualTest builder.UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() - .UseStartup(); + .UseStartup(); var host = builder.Build(); host.Run(); } diff --git a/test/Ocelot.ManualTest/configuration.json b/test/Ocelot.ManualTest/configuration.json index a063fe76..0adac11d 100644 --- a/test/Ocelot.ManualTest/configuration.json +++ b/test/Ocelot.ManualTest/configuration.json @@ -300,12 +300,11 @@ "DownstreamHost": "www.bbc.co.uk", "DownstreamPort": 80, "UpstreamPathTemplate": "/bbc/", - "UpstreamHttpMethod": [ "Get" ], + "UpstreamHttpMethod": [ "Get" ] } ], "GlobalConfiguration": { - "RequestIdKey": "OcRequestId", - "AdministrationPath": "/administration" + "RequestIdKey": "OcRequestId" } } \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs index aef99941..1182a56f 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationCreatorTests.cs @@ -15,6 +15,7 @@ using Xunit; namespace Ocelot.UnitTests.Configuration { + using Ocelot.DependencyInjection; using Ocelot.Errors; using Ocelot.UnitTests.TestData; @@ -36,6 +37,7 @@ namespace Ocelot.UnitTests.Configuration private Mock _rateLimitOptions; private Mock _regionCreator; private Mock _httpHandlerOptionsCreator; + private Mock _adminPath; public FileConfigurationCreatorTests() { @@ -52,13 +54,23 @@ namespace Ocelot.UnitTests.Configuration _rateLimitOptions = new Mock(); _regionCreator = new Mock(); _httpHandlerOptionsCreator = new Mock(); + _adminPath = new Mock(); _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( - _fileConfig.Object, _validator.Object, _logger.Object, + _fileConfig.Object, + _validator.Object, + _logger.Object, _claimsToThingCreator.Object, - _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object, - _serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object, - _rateLimitOptions.Object, _regionCreator.Object, _httpHandlerOptionsCreator.Object); + _authOptionsCreator.Object, + _upstreamTemplatePatternCreator.Object, + _requestIdKeyCreator.Object, + _serviceProviderConfigCreator.Object, + _qosOptionsCreator.Object, + _fileReRouteOptionsCreator.Object, + _rateLimitOptions.Object, + _regionCreator.Object, + _httpHandlerOptionsCreator.Object, + _adminPath.Object); } [Fact] diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs index 63841953..bd955879 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs @@ -91,7 +91,6 @@ namespace Ocelot.UnitTests.Configuration private void ThenTheConfigurationIsStoredAs(FileConfiguration expected) { - _result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath); _result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); _result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); _result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); @@ -126,7 +125,6 @@ namespace Ocelot.UnitTests.Configuration private void ThenTheFollowingIsReturned(FileConfiguration expected) { - _result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath); _result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); _result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); _result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); @@ -155,7 +153,6 @@ namespace Ocelot.UnitTests.Configuration var globalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "asdas", ServiceDiscoveryProvider = new FileServiceDiscoveryProvider { Port = 198, @@ -185,7 +182,6 @@ namespace Ocelot.UnitTests.Configuration var globalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "testy", ServiceDiscoveryProvider = new FileServiceDiscoveryProvider { Port = 198, diff --git a/test/Ocelot.UnitTests/Configuration/IdentityServerConfigurationCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/IdentityServerConfigurationCreatorTests.cs index 8d100e10..93a86743 100644 --- a/test/Ocelot.UnitTests/Configuration/IdentityServerConfigurationCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/IdentityServerConfigurationCreatorTests.cs @@ -9,7 +9,7 @@ namespace Ocelot.UnitTests.Configuration [Fact] public void happy_path_only_exists_for_test_coverage_even_uncle_bob_probably_wouldnt_test_this() { - var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(); + var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration("secret"); result.ApiName.ShouldBe("admin"); } } diff --git a/test/Ocelot.UnitTests/Configuration/OcelotResourceOwnerPasswordValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/OcelotResourceOwnerPasswordValidatorTests.cs deleted file mode 100644 index a8d11713..00000000 --- a/test/Ocelot.UnitTests/Configuration/OcelotResourceOwnerPasswordValidatorTests.cs +++ /dev/null @@ -1,117 +0,0 @@ -using Ocelot.Configuration.Authentication; -using Xunit; -using Shouldly; -using TestStack.BDDfy; -using Moq; -using IdentityServer4.Validation; -using Ocelot.Configuration.Provider; -using System.Collections.Generic; - -namespace Ocelot.UnitTests.Configuration -{ - public class OcelotResourceOwnerPasswordValidatorTests - { - private OcelotResourceOwnerPasswordValidator _validator; - private Mock _matcher; - private string _userName; - private string _password; - private ResourceOwnerPasswordValidationContext _context; - private Mock _config; - private User _user; - - public OcelotResourceOwnerPasswordValidatorTests() - { - _matcher = new Mock(); - _config = new Mock(); - _validator = new OcelotResourceOwnerPasswordValidator(_matcher.Object, _config.Object); - } - - [Fact] - public void should_return_success() - { - this.Given(x => GivenTheUserName("tom")) - .And(x => GivenThePassword("password")) - .And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx"))) - .And(x => GivenTheMatcherReturns(true)) - .When(x => WhenIValidate()) - .Then(x => ThenTheUserIsValidated()) - .And(x => ThenTheMatcherIsCalledCorrectly()) - .BDDfy(); - } - - [Fact] - public void should_return_fail_when_no_user() - { - this.Given(x => GivenTheUserName("bob")) - .And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx"))) - .And(x => GivenTheMatcherReturns(true)) - .When(x => WhenIValidate()) - .Then(x => ThenTheUserIsNotValidated()) - .BDDfy(); - } - - [Fact] - public void should_return_fail_when_password_doesnt_match() - { - this.Given(x => GivenTheUserName("tom")) - .And(x => GivenThePassword("password")) - .And(x => GivenTheUserIs(new User("sub", "tom", "xxx", "xxx"))) - .And(x => GivenTheMatcherReturns(false)) - .When(x => WhenIValidate()) - .Then(x => ThenTheUserIsNotValidated()) - .And(x => ThenTheMatcherIsCalledCorrectly()) - .BDDfy(); - } - - private void ThenTheMatcherIsCalledCorrectly() - { - _matcher - .Verify(x => x.Match(_password, _user.Salt, _user.Hash), Times.Once); - } - - private void GivenThePassword(string password) - { - _password = password; - } - - private void GivenTheUserIs(User user) - { - _user = user; - _config - .Setup(x => x.Users) - .Returns(new List{_user}); - } - - private void GivenTheMatcherReturns(bool expected) - { - _matcher - .Setup(x => x.Match(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns(expected); - } - - private void GivenTheUserName(string userName) - { - _userName = userName; - } - - private void WhenIValidate() - { - _context = new ResourceOwnerPasswordValidationContext - { - UserName = _userName, - Password = _password - }; - _validator.ValidateAsync(_context).Wait(); - } - - private void ThenTheUserIsValidated() - { - _context.Result.IsError.ShouldBe(false); - } - - private void ThenTheUserIsNotValidated() - { - _context.Result.IsError.ShouldBe(true); - } - } -} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs index c54c5ba2..f23a8b15 100644 --- a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs @@ -1,14 +1,20 @@ +using System; using Microsoft.AspNetCore.Mvc; using Moq; using Ocelot.Configuration.File; using Ocelot.Configuration.Setter; -using Ocelot.Controllers; using Ocelot.Errors; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; using Shouldly; using Ocelot.Configuration.Provider; +using Microsoft.Extensions.DependencyInjection; +using Ocelot.Raft; +using Rafty.Concensus; +using Newtonsoft.Json; +using Rafty.FiniteStateMachine; +using Ocelot.Configuration; namespace Ocelot.UnitTests.Controllers { @@ -19,18 +25,21 @@ namespace Ocelot.UnitTests.Controllers private Mock _configSetter; private IActionResult _result; private FileConfiguration _fileConfiguration; + private Mock _provider; + private Mock _node; public FileConfigurationControllerTests() { + _provider = new Mock(); _configGetter = new Mock(); _configSetter = new Mock(); - _controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object); + _controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object, _provider.Object); } [Fact] public void should_get_file_configuration() { - var expected = new OkResponse(new FileConfiguration()); + var expected = new Responses.OkResponse(new FileConfiguration()); this.Given(x => x.GivenTheGetConfigurationReturns(expected)) .When(x => x.WhenIGetTheFileConfiguration()) @@ -41,7 +50,7 @@ namespace Ocelot.UnitTests.Controllers [Fact] public void should_return_error_when_cannot_get_config() { - var expected = new ErrorResponse(It.IsAny()); + var expected = new Responses.ErrorResponse(It.IsAny()); this.Given(x => x.GivenTheGetConfigurationReturns(expected)) .When(x => x.WhenIGetTheFileConfiguration()) @@ -56,26 +65,81 @@ namespace Ocelot.UnitTests.Controllers var expected = new FileConfiguration(); this.Given(x => GivenTheFileConfiguration(expected)) - .And(x => GivenTheConfigSetterReturnsAnError(new OkResponse())) + .And(x => GivenTheConfigSetterReturns(new OkResponse())) .When(x => WhenIPostTheFileConfiguration()) .Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly()) .BDDfy(); } + [Fact] + public void should_post_file_configuration_using_raft_node() + { + var expected = new FileConfiguration(); + + this.Given(x => GivenTheFileConfiguration(expected)) + .And(x => GivenARaftNodeIsRegistered()) + .And(x => GivenTheNodeReturnsOK()) + .And(x => GivenTheConfigSetterReturns(new OkResponse())) + .When(x => WhenIPostTheFileConfiguration()) + .Then(x => x.ThenTheNodeIsCalledCorrectly()) + .BDDfy(); + } + + [Fact] + public void should_return_error_when_cannot_set_config_using_raft_node() + { + var expected = new FileConfiguration(); + + this.Given(x => GivenTheFileConfiguration(expected)) + .And(x => GivenARaftNodeIsRegistered()) + .And(x => GivenTheNodeReturnsError()) + .When(x => WhenIPostTheFileConfiguration()) + .Then(x => ThenTheResponseIs()) + .BDDfy(); + } + [Fact] public void should_return_error_when_cannot_set_config() { var expected = new FileConfiguration(); this.Given(x => GivenTheFileConfiguration(expected)) - .And(x => GivenTheConfigSetterReturnsAnError(new ErrorResponse(new FakeError()))) + .And(x => GivenTheConfigSetterReturns(new ErrorResponse(new FakeError()))) .When(x => WhenIPostTheFileConfiguration()) .Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly()) .And(x => ThenTheResponseIs()) .BDDfy(); } - private void GivenTheConfigSetterReturnsAnError(Response response) + + private void ThenTheNodeIsCalledCorrectly() + { + _node.Verify(x => x.Accept(It.IsAny()), Times.Once); + } + + private void GivenARaftNodeIsRegistered() + { + _node = new Mock(); + _provider + .Setup(x => x.GetService(typeof(INode))) + .Returns(_node.Object); + } + + private void GivenTheNodeReturnsOK() + { + _node + .Setup(x => x.Accept(It.IsAny())) + .Returns(new Rafty.Concensus.OkResponse(new UpdateFileConfiguration(new FileConfiguration()))); + } + + private void GivenTheNodeReturnsError() + { + _node + .Setup(x => x.Accept(It.IsAny())) + .Returns(new Rafty.Concensus.ErrorResponse("error", new UpdateFileConfiguration(new FileConfiguration()))); + } + + private void GivenTheConfigSetterReturns(Response response) { _configSetter .Setup(x => x.Set(It.IsAny())) @@ -103,7 +167,7 @@ namespace Ocelot.UnitTests.Controllers _result.ShouldBeOfType(); } - private void GivenTheGetConfigurationReturns(Response fileConfiguration) + private void GivenTheGetConfigurationReturns(Ocelot.Responses.Response fileConfiguration) { _configGetter .Setup(x => x.Get()) @@ -128,4 +192,4 @@ namespace Ocelot.UnitTests.Controllers } } } -} \ No newline at end of file +} diff --git a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs index d449d280..28818e71 100644 --- a/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/OutputCacheControllerTests.cs @@ -1,10 +1,9 @@ using Xunit; using Shouldly; using TestStack.BDDfy; -using Ocelot.Controllers; +using Ocelot.Cache; using System; using Moq; -using Ocelot.Cache; using System.Net.Http; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; diff --git a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs index 834879c5..818a9e1f 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/OcelotBuilderTests.cs @@ -75,6 +75,16 @@ namespace Ocelot.UnitTests.DependencyInjection .BDDfy(); } + [Fact] + public void should_set_up_rafty() + { + this.Given(x => WhenISetUpOcelotServices()) + .When(x => WhenISetUpRafty()) + .Then(x => ThenAnExceptionIsntThrown()) + .Then(x => ThenTheCorrectAdminPathIsRegitered()) + .BDDfy(); + } + [Fact] public void should_use_logger_factory() { @@ -85,6 +95,13 @@ namespace Ocelot.UnitTests.DependencyInjection .BDDfy(); } + private void ThenTheCorrectAdminPathIsRegitered() + { + _serviceProvider = _services.BuildServiceProvider(); + var path = _serviceProvider.GetService(); + path.Path.ShouldBe("/administration"); + } + private void OnlyOneVersionOfEachCacheIsRegistered() { var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); @@ -111,6 +128,18 @@ namespace Ocelot.UnitTests.DependencyInjection } } + private void WhenISetUpRafty() + { + try + { + _ocelotBuilder.AddAdministration("/administration", "secret").AddRafty(); + } + catch (Exception e) + { + _ex = e; + } + } + private void ThenAnOcelotBuilderIsReturned() { _ocelotBuilder.ShouldBeOfType(); diff --git a/test/Ocelot.UnitTests/Raft/OcelotFiniteStateMachineTests.cs b/test/Ocelot.UnitTests/Raft/OcelotFiniteStateMachineTests.cs new file mode 100644 index 00000000..1451e839 --- /dev/null +++ b/test/Ocelot.UnitTests/Raft/OcelotFiniteStateMachineTests.cs @@ -0,0 +1,45 @@ +using Moq; +using Ocelot.Configuration.Setter; +using Ocelot.Raft; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Raft +{ + public class OcelotFiniteStateMachineTests + { + private UpdateFileConfiguration _command; + private OcelotFiniteStateMachine _fsm; + private Mock _setter; + + public OcelotFiniteStateMachineTests() + { + _setter = new Mock(); + _fsm = new OcelotFiniteStateMachine(_setter.Object); + } + + [Fact] + public void should_handle_update_file_configuration_command() + { + this.Given(x => GivenACommand(new UpdateFileConfiguration(new Ocelot.Configuration.File.FileConfiguration()))) + .When(x => WhenTheCommandIsHandled()) + .Then(x => ThenTheStateIsUpdated()) + .BDDfy(); + } + + private void GivenACommand(UpdateFileConfiguration command) + { + _command = command; + } + + private void WhenTheCommandIsHandled() + { + _fsm.Handle(new Rafty.Log.LogEntry(_command, _command.GetType(), 0)); + } + + private void ThenTheStateIsUpdated() + { + _setter.Verify(x => x.Set(_command.Configuration), Times.Once); + } + } +} \ No newline at end of file