tidying up configuration creation

This commit is contained in:
Tom Gardham-Pallister 2017-02-08 07:37:04 +00:00
parent 7fffc9827a
commit dbe28d38bc
2 changed files with 40 additions and 41 deletions

View File

@ -108,8 +108,6 @@ namespace Ocelot.Configuration.Creator
//note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain
var loadBalancerKey = $"{fileReRoute.UpstreamPathTemplate}{fileReRoute.UpstreamHttpMethod}";
ReRoute reRoute;
var serviceProviderPort = globalConfiguration?.ServiceDiscoveryProvider?.Port ?? 0;
var serviceProviderConfiguration = new ServiceProviderConfiguraionBuilder()
@ -122,48 +120,41 @@ namespace Ocelot.Configuration.Creator
.WithServiceDiscoveryProviderPort(serviceProviderPort)
.Build();
if (isAuthenticated)
{
var authOptionsForRoute = new AuthenticationOptions(fileReRoute.AuthenticationOptions.Provider,
fileReRoute.AuthenticationOptions.ProviderRootUrl, fileReRoute.AuthenticationOptions.ScopeName,
fileReRoute.AuthenticationOptions.RequireHttps, fileReRoute.AuthenticationOptions.AdditionalScopes,
fileReRoute.AuthenticationOptions.ScopeSecret);
var authOptionsForRoute = new AuthenticationOptionsBuilder()
.WithProvider(fileReRoute.AuthenticationOptions?.Provider)
.WithProviderRootUrl(fileReRoute.AuthenticationOptions?.ProviderRootUrl)
.WithScopeName(fileReRoute.AuthenticationOptions?.ScopeName)
.WithRequireHttps(fileReRoute.AuthenticationOptions.RequireHttps)
.WithAdditionalScopes(fileReRoute.AuthenticationOptions?.AdditionalScopes)
.WithScopeSecret(fileReRoute.AuthenticationOptions?.ScopeSecret)
.Build();
var claimsToHeaders = GetAddThingsToRequest(fileReRoute.AddHeadersToRequest);
var claimsToClaims = GetAddThingsToRequest(fileReRoute.AddClaimsToRequest);
var claimsToQueries = GetAddThingsToRequest(fileReRoute.AddQueriesToRequest);
reRoute = new ReRoute(new PathTemplate(fileReRoute.DownstreamPathTemplate),
new PathTemplate(fileReRoute.UpstreamPathTemplate),
new HttpMethod(fileReRoute.UpstreamHttpMethod), upstreamTemplatePattern, isAuthenticated,
authOptionsForRoute, claimsToHeaders, claimsToClaims,
fileReRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds)
, fileReRoute.DownstreamScheme,
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort, loadBalancerKey,
serviceProviderConfiguration);
//reRoute = new ReRouteBuilder()
// .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
// .WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
// .WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod)
// .WithUpstreamTemplatePattern(upstreamTemplatePattern)
// .WithIsAuthenticated(isAuthenticated)
//.Build();
}
else
{
reRoute = new ReRoute(new PathTemplate(fileReRoute.DownstreamPathTemplate),
new PathTemplate(fileReRoute.UpstreamPathTemplate),
new HttpMethod(fileReRoute.UpstreamHttpMethod), upstreamTemplatePattern, isAuthenticated,
null, new List<ClaimToThing>(), new List<ClaimToThing>(),
fileReRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
requestIdKey, isCached, new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds),
fileReRoute.DownstreamScheme,
fileReRoute.LoadBalancer, fileReRoute.DownstreamHost, fileReRoute.DownstreamPort, loadBalancerKey,
serviceProviderConfiguration);
}
var reRoute = new ReRouteBuilder()
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
.WithUpstreamHttpMethod(fileReRoute.UpstreamHttpMethod)
.WithUpstreamTemplatePattern(upstreamTemplatePattern)
.WithIsAuthenticated(isAuthenticated)
.WithAuthenticationOptions(authOptionsForRoute)
.WithClaimsToHeaders(claimsToHeaders)
.WithClaimsToClaims(claimsToClaims)
.WithRouteClaimsRequirement(fileReRoute.RouteClaimsRequirement)
.WithIsAuthorised(isAuthorised)
.WithClaimsToQueries(claimsToQueries)
.WithRequestIdKey(requestIdKey)
.WithIsCached(isCached)
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds))
.WithDownstreamScheme(fileReRoute.DownstreamScheme)
.WithLoadBalancer(fileReRoute.LoadBalancer)
.WithDownstreamHost(fileReRoute.DownstreamHost)
.WithDownstreamPort(fileReRoute.DownstreamPort)
.WithLoadBalancerKey(loadBalancerKey)
.WithServiceProviderConfiguraion(serviceProviderConfiguration)
.Build();
var loadBalancer = await _loadBalanceFactory.Get(reRoute);
_loadBalancerHouse.Add(reRoute.LoadBalancerKey, loadBalancer);

View File

@ -6,6 +6,7 @@ using Moq;
using Ocelot.Authentication.Handler;
using Ocelot.Authentication.Handler.Creator;
using Ocelot.Authentication.Handler.Factory;
using Ocelot.Configuration.Builder;
using Ocelot.Errors;
using Ocelot.Responses;
using Shouldly;
@ -33,7 +34,11 @@ namespace Ocelot.UnitTests.Authentication
[Fact]
public void should_return_identity_server_access_token_handler()
{
this.Given(x => x.GivenTheAuthenticationOptionsAre(new AuthenticationOptions("IdentityServer", "","",false, new List<string>(), "")))
var authenticationOptions = new AuthenticationOptionsBuilder()
.WithProvider("IdentityServer")
.Build();
this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions))
.And(x => x.GivenTheCreatorReturns())
.When(x => x.WhenIGetFromTheFactory())
.Then(x => x.ThenTheHandlerIsReturned("IdentityServer"))
@ -43,7 +48,10 @@ namespace Ocelot.UnitTests.Authentication
[Fact]
public void should_return_error_if_cannot_create_handler()
{
this.Given(x => x.GivenTheAuthenticationOptionsAre(new AuthenticationOptions("IdentityServer", "", "", false, new List<string>(), "")))
var authenticationOptions = new AuthenticationOptionsBuilder()
.Build();
this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions))
.And(x => x.GivenTheCreatorReturnsAnError())
.When(x => x.WhenIGetFromTheFactory())
.Then(x => x.ThenAnErrorResponseIsReturned())