mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:18:15 +08:00
started implementing service discovery integration
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Configuration.Builder
|
||||
{
|
||||
@ -23,12 +24,54 @@ namespace Ocelot.Configuration.Builder
|
||||
private string _requestIdHeaderKey;
|
||||
private bool _isCached;
|
||||
private CacheOptions _fileCacheOptions;
|
||||
private bool _useServiceDiscovery;
|
||||
private string _serviceName;
|
||||
private string _serviceDiscoveryProvider;
|
||||
private string _serviceDiscoveryAddress;
|
||||
private string _downstreamScheme;
|
||||
private string _downstreamHost;
|
||||
|
||||
public ReRouteBuilder()
|
||||
{
|
||||
_additionalScopes = new List<string>();
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithDownstreamScheme(string downstreamScheme)
|
||||
{
|
||||
_downstreamScheme = downstreamScheme;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithDownstreamHost(string downstreamHost)
|
||||
{
|
||||
_downstreamHost = downstreamHost;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithServiceDiscoveryAddress(string serviceDiscoveryAddress)
|
||||
{
|
||||
_serviceDiscoveryAddress = serviceDiscoveryAddress;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithServiceDiscoveryProvider(string serviceDiscoveryProvider)
|
||||
{
|
||||
_serviceDiscoveryProvider = serviceDiscoveryProvider;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithServiceName(string serviceName)
|
||||
{
|
||||
_serviceName = serviceName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithUseServiceDiscovery(bool useServiceDiscovery)
|
||||
{
|
||||
_useServiceDiscovery = useServiceDiscovery;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithDownstreamTemplate(string input)
|
||||
{
|
||||
_downstreamTemplate = input;
|
||||
@ -143,10 +186,13 @@ namespace Ocelot.Configuration.Builder
|
||||
|
||||
public ReRoute Build()
|
||||
{
|
||||
Func<string> downstreamHostFunc = () => { return _downstreamHost; };
|
||||
|
||||
return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern,
|
||||
_isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName,
|
||||
_requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement,
|
||||
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions);
|
||||
_isAuthorised, _claimToQueries, _requestIdHeaderKey, _isCached, _fileCacheOptions, _serviceName,
|
||||
_useServiceDiscovery, _serviceDiscoveryAddress, _serviceDiscoveryProvider, downstreamHostFunc, _downstreamScheme);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -91,6 +91,13 @@ namespace Ocelot.Configuration.Creator
|
||||
? globalConfiguration.RequestIdKey
|
||||
: reRoute.RequestIdKey;
|
||||
|
||||
var useServiceDiscovery = !string.IsNullOrEmpty(reRoute.ServiceName)
|
||||
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Address)
|
||||
&& !string.IsNullOrEmpty(globalConfiguration?.ServiceDiscoveryProvider?.Provider);
|
||||
|
||||
|
||||
Func<string> downstreamHostFunc = () => { return reRoute.DownstreamHost; };
|
||||
|
||||
if (isAuthenticated)
|
||||
{
|
||||
var authOptionsForRoute = new AuthenticationOptions(reRoute.AuthenticationOptions.Provider,
|
||||
@ -106,14 +113,18 @@ namespace Ocelot.Configuration.Creator
|
||||
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
||||
authOptionsForRoute, claimsToHeaders, claimsToClaims,
|
||||
reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries,
|
||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds));
|
||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostFunc, reRoute.DownstreamScheme);
|
||||
}
|
||||
|
||||
return new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate,
|
||||
reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
|
||||
null, new List<ClaimToThing>(), new List<ClaimToThing>(),
|
||||
reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>(),
|
||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds));
|
||||
requestIdKey, isCached, new CacheOptions(reRoute.FileCacheOptions.TtlSeconds),
|
||||
reRoute.ServiceName, useServiceDiscovery, globalConfiguration?.ServiceDiscoveryProvider?.Provider,
|
||||
globalConfiguration?.ServiceDiscoveryProvider?.Address, downstreamHostFunc, reRoute.DownstreamScheme);
|
||||
}
|
||||
|
||||
private string BuildUpstreamTemplate(FileReRoute reRoute)
|
||||
|
@ -2,6 +2,11 @@
|
||||
{
|
||||
public class FileGlobalConfiguration
|
||||
{
|
||||
public FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider();
|
||||
}
|
||||
public string RequestIdKey { get; set; }
|
||||
public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;}
|
||||
}
|
||||
}
|
||||
|
@ -25,5 +25,8 @@ namespace Ocelot.Configuration.File
|
||||
public string RequestIdKey { get; set; }
|
||||
public FileCacheOptions FileCacheOptions { get; set; }
|
||||
public bool ReRouteIsCaseSensitive { get; set; }
|
||||
public string ServiceName { get; set; }
|
||||
public string DownstreamScheme {get;set;}
|
||||
public string DownstreamHost {get;set;}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace Ocelot.Configuration.File
|
||||
{
|
||||
public class FileServiceDiscoveryProvider
|
||||
{
|
||||
public string Provider {get;set;}
|
||||
public string Address {get;set;}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ocelot.Configuration
|
||||
{
|
||||
@ -7,7 +8,8 @@ namespace Ocelot.Configuration
|
||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern,
|
||||
bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties,
|
||||
List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised, List<ClaimToThing> claimsToQueries,
|
||||
string requestIdKey, bool isCached, CacheOptions fileCacheOptions)
|
||||
string requestIdKey, bool isCached, CacheOptions fileCacheOptions, string serviceName, bool useServiceDiscovery,
|
||||
string serviceDiscoveryProvider, string serviceDiscoveryAddress, Func<string> downstreamHost, string downstreamScheme)
|
||||
{
|
||||
DownstreamTemplate = downstreamTemplate;
|
||||
UpstreamTemplate = upstreamTemplate;
|
||||
@ -26,6 +28,12 @@ namespace Ocelot.Configuration
|
||||
?? new List<ClaimToThing>();
|
||||
ClaimsToHeaders = configurationHeaderExtractorProperties
|
||||
?? new List<ClaimToThing>();
|
||||
ServiceName = serviceName;
|
||||
UseServiceDiscovery = useServiceDiscovery;
|
||||
ServiceDiscoveryProvider = serviceDiscoveryProvider;
|
||||
ServiceDiscoveryAddress = serviceDiscoveryAddress;
|
||||
DownstreamHost = downstreamHost;
|
||||
DownstreamScheme = downstreamScheme;
|
||||
}
|
||||
|
||||
public string DownstreamTemplate { get; private set; }
|
||||
@ -42,5 +50,11 @@ namespace Ocelot.Configuration
|
||||
public string RequestIdKey { get; private set; }
|
||||
public bool IsCached { get; private set; }
|
||||
public CacheOptions FileCacheOptions { get; private set; }
|
||||
public string ServiceName { get; private set;}
|
||||
public bool UseServiceDiscovery { get; private set;}
|
||||
public string ServiceDiscoveryProvider { get; private set;}
|
||||
public string ServiceDiscoveryAddress { get; private set;}
|
||||
public Func<string> DownstreamHost {get;private set;}
|
||||
public string DownstreamScheme {get;private set;}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.Configuration.Validator
|
||||
{
|
||||
public class DownstreamTemplateContainsHostError : Error
|
||||
{
|
||||
public DownstreamTemplateContainsHostError(string message)
|
||||
: base(message, OcelotErrorCode.DownstreamTemplateContainsHostError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.Configuration.Validator
|
||||
{
|
||||
public class DownstreamTemplateContainsSchemeError : Error
|
||||
{
|
||||
public DownstreamTemplateContainsSchemeError(string message)
|
||||
: base(message, OcelotErrorCode.DownstreamTemplateContainsSchemeError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -26,6 +26,13 @@ namespace Ocelot.Configuration.Validator
|
||||
return new OkResponse<ConfigurationValidationResult>(result);
|
||||
}
|
||||
|
||||
result = CheckForReRoutesContainingDownstreamScheme(configuration);
|
||||
|
||||
if (result.IsError)
|
||||
{
|
||||
return new OkResponse<ConfigurationValidationResult>(result);
|
||||
}
|
||||
|
||||
return new OkResponse<ConfigurationValidationResult>(result);
|
||||
}
|
||||
|
||||
@ -63,6 +70,27 @@ namespace Ocelot.Configuration.Validator
|
||||
return Enum.TryParse(provider, true, out supportedProvider);
|
||||
}
|
||||
|
||||
private ConfigurationValidationResult CheckForReRoutesContainingDownstreamScheme(FileConfiguration configuration)
|
||||
{
|
||||
var errors = new List<Error>();
|
||||
|
||||
foreach(var reRoute in configuration.ReRoutes)
|
||||
{
|
||||
if(reRoute.DownstreamTemplate.Contains("https://")
|
||||
|| reRoute.DownstreamTemplate.Contains("http://"))
|
||||
{
|
||||
errors.Add(new DownstreamTemplateContainsSchemeError($"{reRoute.DownstreamTemplate} contains scheme"));
|
||||
}
|
||||
}
|
||||
|
||||
if(errors.Any())
|
||||
{
|
||||
return new ConfigurationValidationResult(false, errors);
|
||||
}
|
||||
|
||||
return new ConfigurationValidationResult(true, errors);
|
||||
}
|
||||
|
||||
private ConfigurationValidationResult CheckForDupliateReRoutes(FileConfiguration configuration)
|
||||
{
|
||||
var hasDupes = configuration.ReRoutes
|
||||
|
@ -17,6 +17,8 @@
|
||||
InstructionNotForClaimsError,
|
||||
UnauthorizedError,
|
||||
ClaimValueNotAuthorisedError,
|
||||
UserDoesNotHaveClaimError
|
||||
UserDoesNotHaveClaimError,
|
||||
DownstreamTemplateContainsSchemeError,
|
||||
DownstreamTemplateContainsHostError
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user