コード例 #1
0
        private bool IsMatch(RouteConfig routeConfig, Message message, HookInformation hookInfo)
        {
            try
            {
                if (!string.Equals(routeConfig.Type, hookInfo.Type, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(false);
                }

                if (!string.IsNullOrWhiteSpace(routeConfig.SubType))
                {
                    if (!string.Equals(routeConfig.SubType, hookInfo.SubType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                log.Error($"Error checking validity of route {routeConfig.Name} on message {message.SystemProperties.SequenceNumber}", e);
                return(false);
            }
        }
コード例 #2
0
        private static async Task <HttpResponseMessage> Run(string type, HttpRequestMessage req, TraceWriter log, IBinder binder)
        {
            try
            {
                log.Info($"{type} WebHook Received");

                var queryParams = req.GetQueryNameValuePairs().ToList();
                var tenant      = queryParams.FirstOrDefault(x => string.Equals(x.Key, "tenant", StringComparison.OrdinalIgnoreCase)).Value;

                if (string.IsNullOrWhiteSpace(tenant))
                {
                    log.Error($"{type} - Missing tenant parameter");
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Please provide tenant parameter"));
                }

                var validTenants = Environment.GetEnvironmentVariable("ValidTenants")?.Split(';');

                if (validTenants == null || !validTenants.Any(x => string.Equals(x, tenant, StringComparison.InvariantCultureIgnoreCase)))
                {
                    log.Error($"{type} - Tenant {tenant} is not allowed");
                    return(req.CreateErrorResponse(HttpStatusCode.Forbidden, "Please provide valid tenant parameter"));
                }

                var subType = queryParams.FirstOrDefault(x => string.Equals(x.Key, "subType", StringComparison.OrdinalIgnoreCase)).Value;

                if (string.IsNullOrWhiteSpace(subType))
                {
                    log.Error($"{type} - Missing subType parameter");
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Please provide subType parameter"));
                }

                log.Info($"{type} WebHook (tenant: {tenant} | subType: {subType})");

                var jsonData = await req.Content.ReadAsStringAsync();

                var hookInformation = new HookInformation {
                    Type = type, SubType = subType, JsonData = jsonData, RequestHeaders = req.Headers.ToList()
                };

                var serviceBusQueueAttribute = new ServiceBusAttribute($"hooks_{tenant}", AccessRights.Manage)
                {
                    Connection = "AzureServiceBus"
                };

                var queueMessageJson = JsonConvert.SerializeObject(hookInformation);
                var outputMessages   = await binder.BindAsync <IAsyncCollector <string> >(serviceBusQueueAttribute);

                await outputMessages.AddAsync(queueMessageJson);

                return(req.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                log.Error($"Error Processing {type} Hook", e);
                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
コード例 #3
0
        private void DebugOutput(Message message, HookInformation hookInfo)
        {
            if (!config.DebugOutput || string.IsNullOrWhiteSpace(config.DebugOutputPath))
            {
                return;
            }

            var directory = Path.Combine(config.DebugOutputPath, DateTime.UtcNow.ToString("yy_MM_dd_HH"));

            Directory.CreateDirectory(directory);

            var fileName = $"{hookInfo.Type}_{hookInfo.SubType}_{message.SystemProperties.SequenceNumber}.json";

            File.WriteAllText(Path.Combine(directory, fileName), Encoding.UTF8.GetString(message.Body), Encoding.UTF8);
        }
コード例 #4
0
        private async Task ProcessRoute(Message message, CancellationToken token, RouteConfig route, HookInformation hookInfo)
        {
            try
            {
                log.Debug($"Found maching route {route.Name} - Posting payload to {route.PostDestinationHost}");
                var request = new HttpRequestMessage(HttpMethod.Post, route.PostDestination);

                foreach (var header in hookInfo.RequestHeaders)
                {
                    request.Headers.Add(header.Key, header.Value);
                }

                if (!string.IsNullOrWhiteSpace(route.AuthenticationScheme) && !string.IsNullOrWhiteSpace(route.AuthenticationHeader))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue(route.AuthenticationScheme, route.AuthenticationHeader);
                }

                request.Content = new StringContent(hookInfo.JsonData);

                var response = await httpClient.SendAsync(request, token);

                var responseContent = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    log.Debug($"HTTP response from {route.PostDestinationHost}.  - Status: {response.StatusCode}, Content: {responseContent}");
                }
                else
                {
                    log.Error($"HTTP response from {route.PostDestinationHost}. - Status: {response.StatusCode}, Content: {responseContent}");
                }
            }
            catch (Exception e)
            {
                log.Error($"Error processing route {route.Name} for message {message.SystemProperties.SequenceNumber}.", e);
            }
        }