public async Task Invoke(HttpContext context)
        {
            var upstreamUrlPath = context.Request.Path.ToString();

            //todo make this getting config its own middleware one day?
            var configuration = await _configProvider.Get();

            if (configuration.IsError)
            {
                _logger.LogError($"{MiddlewareName} setting pipeline errors. IOcelotConfigurationProvider returned {configuration.Errors.ToErrorString()}");
                SetPipelineError(configuration.Errors);
            }

            SetServiceProviderConfigurationForThisRequest(configuration.Data.ServiceProviderConfiguration);

            _logger.LogDebug("upstream url path is {upstreamUrlPath}", upstreamUrlPath);

            var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method, configuration.Data);

            if (downstreamRoute.IsError)
            {
                _logger.LogError($"{MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");

                SetPipelineError(downstreamRoute.Errors);
                return;
            }

            _logger.LogDebug("downstream template is {downstreamRoute.Data.ReRoute.DownstreamPath}", downstreamRoute.Data.ReRoute.DownstreamPathTemplate);

            SetDownstreamRouteForThisRequest(downstreamRoute.Data);

            await _next.Invoke(context);
        }
Beispiel #2
0
        public async Task <Response <DownstreamRoute> > FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
        {
            var configuration = await _configProvider.Get();

            var applicableReRoutes = configuration.Data.ReRoutes.Where(r => r.UpstreamHttpMethod.Count == 0 || r.UpstreamHttpMethod.Select(x => x.Method.ToLower()).Contains(upstreamHttpMethod.ToLower()));

            foreach (var reRoute in applicableReRoutes)
            {
                if (upstreamUrlPath == reRoute.UpstreamTemplatePattern)
                {
                    var templateVariableNameAndValues = _urlPathPlaceholderNameAndValueFinder.Find(upstreamUrlPath, reRoute.UpstreamPathTemplate.Value);

                    return(new OkResponse <DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, reRoute)));
                }

                var urlMatch = _urlMatcher.Match(upstreamUrlPath, reRoute.UpstreamTemplatePattern);

                if (urlMatch.Data.Match)
                {
                    var templateVariableNameAndValues = _urlPathPlaceholderNameAndValueFinder.Find(upstreamUrlPath, reRoute.UpstreamPathTemplate.Value);

                    return(new OkResponse <DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, reRoute)));
                }
            }

            return(new ErrorResponse <DownstreamRoute>(new List <Error>
            {
                new UnableToFindDownstreamRouteError()
            }));
        }
        private async Task TrySetGlobalRequestId(DownstreamContext context)
        {
            //try and get the global request id and set it for logs...
            //should this basically be immutable per request...i guess it should!
            //first thing is get config
            var configuration = await _provider.Get();

            //if error throw to catch below..
            if (configuration.IsError)
            {
                throw new Exception($"{MiddlewareName} setting pipeline errors. IOcelotConfigurationProvider returned {configuration.Errors.ToErrorString()}");
            }

            //else set the request id?
            var key = configuration.Data.RequestId;

            StringValues upstreamRequestIds;

            if (!string.IsNullOrEmpty(key) && context.HttpContext.Request.Headers.TryGetValue(key, out upstreamRequestIds))
            {
                //todo fix looking in both places
                context.HttpContext.TraceIdentifier = upstreamRequestIds.First();
                _repo.Add <string>("RequestId", context.HttpContext.TraceIdentifier);
            }
        }
        public async Task <Response <DownstreamRoute> > FindDownstreamRoute(string upstreamUrlPath, string upstreamHttpMethod)
        {
            var configuration = await _configProvider.Get();

            var applicableReRoutes = configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod.Method, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase));

            foreach (var reRoute in applicableReRoutes)
            {
                if (upstreamUrlPath == reRoute.UpstreamTemplatePattern)
                {
                    var templateVariableNameAndValues = _urlPathPlaceholderNameAndValueFinder.Find(upstreamUrlPath, reRoute.UpstreamPathTemplate.Value);

                    return(new OkResponse <DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, reRoute)));
                }

                var urlMatch = _urlMatcher.Match(upstreamUrlPath, reRoute.UpstreamTemplatePattern);

                if (urlMatch.Data.Match)
                {
                    var templateVariableNameAndValues = _urlPathPlaceholderNameAndValueFinder.Find(upstreamUrlPath, reRoute.UpstreamPathTemplate.Value);

                    return(new OkResponse <DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, reRoute)));
                }
            }

            return(new ErrorResponse <DownstreamRoute>(new List <Error>
            {
                new UnableToFindDownstreamRouteError()
            }));
        }
        private static async Task <IOcelotConfiguration> GetOcelotConfigAndReturn(IOcelotConfigurationProvider provider)
        {
            var ocelotConfiguration = await provider.Get();

            if (ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError)
            {
                ThrowToStopOcelotStarting(ocelotConfiguration);
            }

            return(ocelotConfiguration.Data);
        }
Beispiel #6
0
        public FilePeersProvider(IOptions <FilePeers> options, IBaseUrlFinder finder, IOcelotConfigurationProvider provider, IIdentityServerConfiguration identityServerConfig)
        {
            _identityServerConfig = identityServerConfig;
            _provider             = provider;
            _finder  = finder;
            _options = options;
            _peers   = new List <IPeer>();
            //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, _finder, config.Data, _identityServerConfig);
                _peers.Add(httpPeer);
            }
        }
Beispiel #7
0
        public async Task Invoke(DownstreamContext context)
        {
            var upstreamUrlPath = context.HttpContext.Request.Path.ToString();

            var upstreamHost = context.HttpContext.Request.Headers["Host"];

            var configuration = await _configProvider.Get();

            if (configuration.IsError)
            {
                Logger.LogWarning($"{MiddlewareName} setting pipeline errors. IOcelotConfigurationProvider returned {configuration.Errors.ToErrorString()}");
                SetPipelineError(context, configuration.Errors);
                return;
            }

            context.ServiceProviderConfiguration = configuration.Data.ServiceProviderConfiguration;

            Logger.LogDebug($"Upstream url path is {upstreamUrlPath}");

            var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.HttpContext.Request.Method, configuration.Data, upstreamHost);

            if (downstreamRoute.IsError)
            {
                Logger.LogWarning($"{MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");

                SetPipelineError(context, downstreamRoute.Errors);
                return;
            }

            var downstreamPathTemplates = string.Join(", ", downstreamRoute.Data.ReRoute.DownstreamReRoute.Select(r => r.DownstreamPathTemplate.Value));

            Logger.LogDebug($"downstream templates are {downstreamPathTemplates}");

            context.TemplatePlaceholderNameAndValues = downstreamRoute.Data.TemplatePlaceholderNameAndValues;

            await _multiplexer.Multiplex(context, downstreamRoute.Data.ReRoute, _next);
        }
Beispiel #8
0
        public async Task Invoke(HttpContext context)
        {
            DownstreamRouteFinder downstreamRouteFinder = new DownstreamRouteFinder(_urlMatcher, _urlPathPlaceholderNameAndValueFinder);
            DownstreamRoute       route = downstreamRouteFinder.FindDownstreamRoute(context.Request.Path.ToString(), context.Request.Method, _ocelotConfigurationProvider.Get().Result.Data, context.Request.Headers["Host"]).Data;

            if (route.ReRoute.IsAuthenticated)
            {
                string token = context.Request.Headers["Token"];
                if (string.IsNullOrEmpty(token))
                {
                    AuthenticateResult.Fail("Unauthorized");
                    context.Response.StatusCode = 401; //Unauthorized

                    return;
                }

                var claims = new List <Claim>();
                claims.Add(new Claim(ClaimTypes.Name, "Shanzm"));
                claims.Add(new Claim(ClaimTypes.Role, "Users"));
                var identity = new ClaimsIdentity(claims, "WapIdentity");

                context.User = new ClaimsPrincipal(identity);
                var ticket = new AuthenticationTicket(context.User, new AuthenticationProperties(), "WapScheme");
                AuthenticateResult.Success(ticket);
            }

            await _next(context);
        }