renamed and removed some stuff that wasnt needed

This commit is contained in:
TomPallister 2016-10-18 16:22:51 +01:00
parent 84256e7bac
commit 707f1d6908
19 changed files with 87 additions and 107 deletions

View File

@ -25,8 +25,6 @@ namespace Ocelot.Library.Authentication
ScopeSecret = authOptions.ScopeSecret
});
builder.UseMvc();
var authenticationNext = builder.Build();
return new OkResponse<RequestDelegate>(authenticationNext);

View File

@ -1,10 +1,8 @@
using Ocelot.Library.RequestBuilder;
using System.Collections.Generic;
using Ocelot.Library.Configuration;
namespace Ocelot.Library.Builder
{
using System.Collections.Generic;
using Configuration;
public class ReRouteBuilder
{
private string _downstreamTemplate;
@ -18,7 +16,7 @@ namespace Ocelot.Library.Builder
private List<string> _additionalScopes;
private bool _requireHttps;
private string _scopeSecret;
private List<ConfigurationHeaderExtractorProperties> _configHeaderExtractorProperties;
private List<ClaimToHeader> _configHeaderExtractorProperties;
public ReRouteBuilder()
{
@ -89,7 +87,7 @@ namespace Ocelot.Library.Builder
return this;
}
public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> input)
public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ClaimToHeader> input)
{
_configHeaderExtractorProperties = input;
return this;

View File

@ -1,8 +1,8 @@
namespace Ocelot.Library.RequestBuilder
namespace Ocelot.Library.Configuration
{
public class ConfigurationHeaderExtractorProperties
public class ClaimToHeader
{
public ConfigurationHeaderExtractorProperties(string headerKey, string claimKey, string delimiter, int index)
public ClaimToHeader(string headerKey, string claimKey, string delimiter, int index)
{
ClaimKey = claimKey;
Delimiter = delimiter;

View File

@ -2,17 +2,18 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Ocelot.Library.Errors;
using Ocelot.Library.RequestBuilder;
using Ocelot.Library.Responses;
namespace Ocelot.Library.RequestBuilder
namespace Ocelot.Library.Configuration
{
public class ConfigurationHeaderExtrator : IConfigurationHeaderExtrator
public class ClaimToHeaderConfigurationParser : IClaimToHeaderConfigurationParser
{
private readonly Regex _claimRegex = new Regex("Claims\\[.*\\]");
private readonly Regex _indexRegex = new Regex("value\\[.*\\]");
private const string SplitToken = ">";
public Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value)
public Response<ClaimToHeader> Extract(string headerKey, string value)
{
try
{
@ -20,7 +21,7 @@ namespace Ocelot.Library.RequestBuilder
if (instructions.Length <= 1)
{
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
return new ErrorResponse<ClaimToHeader>(
new List<Error>
{
new NoInstructionsError(SplitToken)
@ -31,7 +32,7 @@ namespace Ocelot.Library.RequestBuilder
if (!claimMatch)
{
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
return new ErrorResponse<ClaimToHeader>(
new List<Error>
{
new InstructionNotForClaimsError()
@ -48,12 +49,12 @@ namespace Ocelot.Library.RequestBuilder
delimiter = instructions[2].Trim();
}
return new OkResponse<ConfigurationHeaderExtractorProperties>(
new ConfigurationHeaderExtractorProperties(headerKey, claimKey, delimiter, index));
return new OkResponse<ClaimToHeader>(
new ClaimToHeader(headerKey, claimKey, delimiter, index));
}
catch (Exception exception)
{
return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
return new ErrorResponse<ClaimToHeader>(
new List<Error>
{
new ParsingConfigurationHeaderError(exception)

View File

@ -0,0 +1,9 @@
using Ocelot.Library.Responses;
namespace Ocelot.Library.Configuration
{
public interface IClaimToHeaderConfigurationParser
{
Response<ClaimToHeader> Extract(string headerKey, string value);
}
}

View File

@ -16,17 +16,17 @@ namespace Ocelot.Library.Configuration
private readonly List<ReRoute> _reRoutes;
private const string RegExMatchEverything = ".*";
private const string RegExMatchEndString = "$";
private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
private readonly ILogger<OcelotConfiguration> _logger;
public OcelotConfiguration(IOptions<YamlConfiguration> options,
IConfigurationValidator configurationValidator,
IConfigurationHeaderExtrator configurationHeaderExtrator,
IClaimToHeaderConfigurationParser claimToHeaderConfigurationParser,
ILogger<OcelotConfiguration> logger)
{
_options = options;
_configurationValidator = configurationValidator;
_configurationHeaderExtrator = configurationHeaderExtrator;
_claimToHeaderConfigurationParser = claimToHeaderConfigurationParser;
_logger = logger;
_reRoutes = new List<ReRoute>();
SetUpConfiguration();
@ -92,17 +92,17 @@ namespace Ocelot.Library.Configuration
else
{
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod,
upstreamTemplate, isAuthenticated, null, new List<ConfigurationHeaderExtractorProperties>()));
upstreamTemplate, isAuthenticated, null, new List<ClaimToHeader>()));
}
}
private List<ConfigurationHeaderExtractorProperties> GetHeadersToAddToRequest(YamlReRoute reRoute)
private List<ClaimToHeader> GetHeadersToAddToRequest(YamlReRoute reRoute)
{
var configHeaders = new List<ConfigurationHeaderExtractorProperties>();
var configHeaders = new List<ClaimToHeader>();
foreach (var add in reRoute.AddHeadersToRequest)
{
var configurationHeader = _configurationHeaderExtrator.Extract(add.Key, add.Value);
var configurationHeader = _claimToHeaderConfigurationParser.Extract(add.Key, add.Value);
if (configurationHeader.IsError)
{

View File

@ -1,11 +1,10 @@
using System.Collections.Generic;
using Ocelot.Library.RequestBuilder;
namespace Ocelot.Library.Configuration
{
public class ReRoute
{
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties)
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToHeader> configurationHeaderExtractorProperties)
{
DownstreamTemplate = downstreamTemplate;
UpstreamTemplate = upstreamTemplate;
@ -13,8 +12,8 @@ namespace Ocelot.Library.Configuration
UpstreamTemplatePattern = upstreamTemplatePattern;
IsAuthenticated = isAuthenticated;
AuthenticationOptions = authenticationOptions;
ConfigurationHeaderExtractorProperties = configurationHeaderExtractorProperties
?? new List<ConfigurationHeaderExtractorProperties>();
ClaimsToHeaders = configurationHeaderExtractorProperties
?? new List<ClaimToHeader>();
}
public string DownstreamTemplate { get; private set; }
@ -23,6 +22,6 @@ namespace Ocelot.Library.Configuration
public string UpstreamHttpMethod { get; private set; }
public bool IsAuthenticated { get; private set; }
public AuthenticationOptions AuthenticationOptions { get; private set; }
public List<ConfigurationHeaderExtractorProperties> ConfigurationHeaderExtractorProperties { get; private set; }
public List<ClaimToHeader> ClaimsToHeaders { get; private set; }
}
}

View File

@ -18,12 +18,18 @@
{
public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot)
{
// framework services
services.AddOptions();
services.AddMvcCore().AddJsonFormatters();
services.AddLogging();
// initial configuration from yaml
services.Configure<YamlConfiguration>(configurationRoot);
// Add framework services.
// ocelot services.
services.AddSingleton<IAddHeadersToRequest, AddHeadersToRequest>();
services.AddSingleton<IClaimsParser, ClaimsParser>();
services.AddSingleton<IConfigurationHeaderExtrator, ConfigurationHeaderExtrator>();
services.AddSingleton<IClaimToHeaderConfigurationParser, ClaimToHeaderConfigurationParser>();
services.AddSingleton<IConfigurationValidator, ConfigurationValidator>();
services.AddSingleton<IOcelotConfiguration, OcelotConfiguration>();
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
@ -38,6 +44,7 @@
services.AddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
// could maybe use a scoped data repository
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IScopedRequestDataRepository, ScopedRequestDataRepository>();

View File

@ -30,9 +30,9 @@ namespace Ocelot.Library.Middleware
{
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
if (downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties.Any())
if (downstreamRoute.Data.ReRoute.ClaimsToHeaders.Any())
{
_addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties, context);
_addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ClaimsToHeaders, context);
}
await _next.Invoke(context);

View File

@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Ocelot.Library.Configuration;
using Ocelot.Library.Responses;
namespace Ocelot.Library.RequestBuilder
@ -15,7 +16,7 @@ namespace Ocelot.Library.RequestBuilder
_claimsParser = claimsParser;
}
public Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties, HttpContext context)
public Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties, HttpContext context)
{
foreach (var config in configurationHeaderExtractorProperties)
{

View File

@ -1,12 +1,13 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Configuration;
using Ocelot.Library.Responses;
namespace Ocelot.Library.RequestBuilder
{
public interface IAddHeadersToRequest
{
Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties,
Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties,
HttpContext context);
}
}

View File

@ -1,10 +0,0 @@
using System.Collections.Generic;
using Ocelot.Library.Responses;
namespace Ocelot.Library.RequestBuilder
{
public interface IConfigurationHeaderExtrator
{
Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value);
}
}

View File

@ -1,22 +0,0 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Ocelot.Controllers
{
/// <summary>
/// This controller is a catch all for requests so that if we build it into a pipeline
/// the requests get authorised.
/// </summary>
public class HomeController : Controller
{
//[Authorize]
[Route("{*url}")]
public void Index()
{
if (true == true)
{
}
}
}
}

View File

@ -28,11 +28,6 @@ namespace Ocelot
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMvc();
services.AddMvcCore().AddAuthorization().AddJsonFormatters();
services.AddAuthentication();
services.AddLogging();
services.AddOcelot(Configuration);
}

View File

@ -16,7 +16,7 @@ namespace Ocelot.AcceptanceTests
{
using Library.Configuration.Yaml;
public class OcelotTests : IDisposable
public class RoutingTests : IDisposable
{
private TestServer _server;
private HttpClient _client;
@ -28,7 +28,7 @@ namespace Ocelot.AcceptanceTests
// Sadly we need to change this when we update the netcoreapp version to make the test update the config correctly
private double _netCoreAppVersion = 1.4;
public OcelotTests()
public RoutingTests()
{
_configurationPath = $"./bin/Debug/netcoreapp{_netCoreAppVersion}/configuration.yaml";
}

View File

@ -20,13 +20,13 @@ namespace Ocelot.UnitTests.Configuration
private readonly Mock<IConfigurationValidator> _validator;
private OcelotConfiguration _config;
private YamlConfiguration _yamlConfiguration;
private readonly Mock<IConfigurationHeaderExtrator> _configExtractor;
private readonly Mock<IClaimToHeaderConfigurationParser> _configExtractor;
private readonly Mock<ILogger<OcelotConfiguration>> _logger;
public OcelotConfigurationTests()
{
_logger = new Mock<ILogger<OcelotConfiguration>>();
_configExtractor = new Mock<IConfigurationHeaderExtrator>();
_configExtractor = new Mock<IClaimToHeaderConfigurationParser>();
_validator = new Mock<IConfigurationValidator>();
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
}
@ -75,9 +75,9 @@ namespace Ocelot.UnitTests.Configuration
.WithRequireHttps(false)
.WithScopeSecret("secret")
.WithAuthenticationProviderScopeName("api")
.WithConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
{
new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0),
new ClaimToHeader("CustomerId", "CustomerId", "", 0),
})
.Build()
};
@ -108,18 +108,18 @@ namespace Ocelot.UnitTests.Configuration
}
}))
.And(x => x.GivenTheYamlConfigIsValid())
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0)))
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ClaimToHeader("CustomerId", "CustomerId", "", 0)))
.When(x => x.WhenIInstanciateTheOcelotConfig())
.Then(x => x.ThenTheReRoutesAre(expected))
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
.BDDfy();
}
private void GivenTheConfigHeaderExtractorReturns(ConfigurationHeaderExtractorProperties expected)
private void GivenTheConfigHeaderExtractorReturns(ClaimToHeader expected)
{
_configExtractor
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
.Returns(new OkResponse<ConfigurationHeaderExtractorProperties>(expected));
.Returns(new OkResponse<ClaimToHeader>(expected));
}
[Fact]

View File

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Builder;
using Ocelot.Library.Configuration;
using Ocelot.Library.DownstreamRouteFinder;
using Ocelot.Library.Middleware;
using Ocelot.Library.Repository;
@ -60,9 +61,9 @@ namespace Ocelot.UnitTests.Middleware
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
new ReRouteBuilder()
.WithDownstreamTemplate("any old string")
.WithConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
{
new ConfigurationHeaderExtractorProperties("UserId", "Subject", "", 0)
new ClaimToHeader("UserId", "Subject", "", 0)
})
.Build());
@ -76,7 +77,7 @@ namespace Ocelot.UnitTests.Middleware
private void GivenTheAddHeadersToRequestReturns(string claimValue)
{
_addHeaders
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ConfigurationHeaderExtractorProperties>>(),
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
It.IsAny<HttpContext>()))
.Returns(new OkResponse());
}
@ -84,7 +85,7 @@ namespace Ocelot.UnitTests.Middleware
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
{
_addHeaders
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ConfigurationHeaderExtractorProperties>>(),
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
It.IsAny<HttpContext>()), Times.Once);
}

View File

@ -4,6 +4,7 @@ using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Moq;
using Ocelot.Library.Configuration;
using Ocelot.Library.Errors;
using Ocelot.Library.RequestBuilder;
using Ocelot.Library.Responses;
@ -17,7 +18,7 @@ namespace Ocelot.UnitTests.RequestBuilder
{
private readonly AddHeadersToRequest _addHeadersToRequest;
private readonly Mock<IClaimsParser> _parser;
private List<ConfigurationHeaderExtractorProperties> _configuration;
private List<ClaimToHeader> _configuration;
private HttpContext _context;
private Response _result;
private Response<string> _claimValue;
@ -40,9 +41,9 @@ namespace Ocelot.UnitTests.RequestBuilder
};
this.Given(
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
{
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
new ClaimToHeader("header-key", "", "", 0)
}))
.Given(x => x.GivenHttpContext(context))
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
@ -66,9 +67,9 @@ namespace Ocelot.UnitTests.RequestBuilder
context.Request.Headers.Add("header-key", new StringValues("initial"));
this.Given(
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
{
new ConfigurationHeaderExtractorProperties("header-key", "", "", 0)
new ClaimToHeader("header-key", "", "", 0)
}))
.Given(x => x.GivenHttpContext(context))
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
@ -82,9 +83,9 @@ namespace Ocelot.UnitTests.RequestBuilder
public void should_return_error()
{
this.Given(
x => x.GivenConfigurationHeaderExtractorProperties(new List<ConfigurationHeaderExtractorProperties>
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
{
new ConfigurationHeaderExtractorProperties("", "", "", 0)
new ClaimToHeader("", "", "", 0)
}))
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
@ -102,7 +103,7 @@ namespace Ocelot.UnitTests.RequestBuilder
header.Value.First().ShouldBe(_claimValue.Data);
}
private void GivenConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> configuration)
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToHeader> configuration)
{
_configuration = configuration;
}

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Configuration;
using Ocelot.Library.Errors;
using Ocelot.Library.RequestBuilder;
using Ocelot.Library.Responses;
@ -12,12 +13,12 @@ namespace Ocelot.UnitTests.RequestBuilder
public class ConfigurationHeadersExtractorTests
{
private Dictionary<string, string> _dictionary;
private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
private Response<ConfigurationHeaderExtractorProperties> _result;
private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
private Response<ClaimToHeader> _result;
public ConfigurationHeadersExtractorTests()
{
_configurationHeaderExtrator = new ConfigurationHeaderExtrator();
_claimToHeaderConfigurationParser = new ClaimToHeaderConfigurationParser();
}
[Fact]
@ -30,7 +31,7 @@ namespace Ocelot.UnitTests.RequestBuilder
.When(x => x.WhenICallTheExtractor())
.Then(
x =>
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
new List<Error>
{
new NoInstructionsError(">")
@ -48,7 +49,7 @@ namespace Ocelot.UnitTests.RequestBuilder
.When(x => x.WhenICallTheExtractor())
.Then(
x =>
x.ThenAnErrorIsReturned(new ErrorResponse<ConfigurationHeaderExtractorProperties>(
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToHeader>(
new List<Error>
{
new InstructionNotForClaimsError()
@ -67,8 +68,8 @@ namespace Ocelot.UnitTests.RequestBuilder
.Then(
x =>
x.ThenTheClaimParserPropertiesAreReturned(
new OkResponse<ConfigurationHeaderExtractorProperties>(
new ConfigurationHeaderExtractorProperties("CustomerId", "CustomerId", "", 0))))
new OkResponse<ClaimToHeader>(
new ClaimToHeader("CustomerId", "CustomerId", "", 0))))
.BDDfy();
}
@ -83,18 +84,18 @@ namespace Ocelot.UnitTests.RequestBuilder
.Then(
x =>
x.ThenTheClaimParserPropertiesAreReturned(
new OkResponse<ConfigurationHeaderExtractorProperties>(
new ConfigurationHeaderExtractorProperties("UserId", "Subject", "|", 0))))
new OkResponse<ClaimToHeader>(
new ClaimToHeader("UserId", "Subject", "|", 0))))
.BDDfy();
}
private void ThenAnErrorIsReturned(Response<ConfigurationHeaderExtractorProperties> expected)
private void ThenAnErrorIsReturned(Response<ClaimToHeader> expected)
{
_result.IsError.ShouldBe(expected.IsError);
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
}
private void ThenTheClaimParserPropertiesAreReturned(Response<ConfigurationHeaderExtractorProperties> expected)
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToHeader> expected)
{
_result.Data.ClaimKey.ShouldBe(expected.Data.ClaimKey);
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
@ -105,7 +106,7 @@ namespace Ocelot.UnitTests.RequestBuilder
private void WhenICallTheExtractor()
{
var first = _dictionary.First();
_result = _configurationHeaderExtrator.Extract(first.Key, first.Value);
_result = _claimToHeaderConfigurationParser.Extract(first.Key, first.Value);
}
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)