Exemple #1
0
        public HttpResponseMessage HandleResponse(IOnPremiseConnectorRequest request, HttpRequestMessage message, IPrincipal clientUser, IOnPremiseConnectorResponse response)
        {
            if (_responseInterceptor == null)
            {
                return(null);
            }

            _logger?.Verbose("Handling response. request-id={RequestId}", request.RequestId);

            try
            {
                var interceptedRequest = CreateInterceptedRequest(request, message, clientUser);

                var immediateResponse = response == null?_responseInterceptor.OnResponseFailed(interceptedRequest) : _responseInterceptor.OnResponseReceived(interceptedRequest, new InterceptedResponse(response));

                if (immediateResponse != null)
                {
                    immediateResponse.RequestMessage = message;
                }

                return(immediateResponse);
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error while executing the response interceptor. type-name={InterceptorType}, request-id={RequestId}", _requestInceptor?.GetType().Name, request.RequestId);
                return(null);
            }
        }
 internal void AddContentHeaders(IOnPremiseConnectorRequest onPremiseConnectorRequest, HttpRequestMessage request)
 {
     foreach (var httpHeader in request.Content.Headers)
     {
         onPremiseConnectorRequest.HttpHeaders.Add(httpHeader.Key, CombineMultipleHttpHeaderValuesIntoOneCommaSeperatedValue(httpHeader.Value));
     }
 }
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                             connectionId, request.RequestId, request.HttpMethod, request.Url, request.OriginId, request.ContentLength);

            await Connection.Send(connectionId, request).ConfigureAwait(false);
        }
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            try
            {
                _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                                 connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? request.Url : request.Url.StripQueryString(), request.OriginId, request.ContentLength);

                var json = JObject.FromObject(request);
                if (request.Properties != null)
                {
                    json.Remove(nameof(IOnPremiseConnectorRequest.Properties));

                    foreach (var kvp in request.Properties)
                    {
                        json[kvp.Key] = JToken.FromObject(kvp.Value);
                    }
                }

                await Connection.Send(connectionId, json).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "An error occured forwarding request to connection. connection-id={ConnectionId}, request={@Request}", connectionId, request);
            }
        }
 public void Trace(IOnPremiseConnectorRequest request, IOnPremiseConnectorResponse response, IEnumerable <Guid> traceConfigurationIds)
 {
     foreach (var traceConfigurationId in traceConfigurationIds)
     {
         Trace(request, response, traceConfigurationId);
     }
 }
        public void LogRequest(IOnPremiseConnectorRequest request, IOnPremiseConnectorResponse response, Guid linkId, Guid originId, string relayPath, HttpStatusCode?statusCode)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var pathInformation = _pathSplitter.Split(relayPath);

            _logRepository.LogRequest(new RequestLogEntry
            {
                LocalUrl                  = pathInformation.LocalUrl,
                HttpStatusCode            = statusCode ?? HttpStatusCode.InternalServerError,
                ContentBytesIn            = request.ContentLength,
                ContentBytesOut           = response?.ContentLength ?? 0,
                OnPremiseConnectorInDate  = request.RequestStarted,
                OnPremiseConnectorOutDate = request.RequestFinished,
                OnPremiseTargetInDate     = response?.RequestStarted,
                OnPremiseTargetOutDate    = response?.RequestFinished,
                OriginId                  = originId,
                OnPremiseTargetKey        = pathInformation.OnPremiseTargetKey,
                LinkId    = linkId,
                RequestId = request.RequestId,
            });
        }
        private void SendOnPremiseConnectorRequest(Guid linkId, IOnPremiseConnectorRequest request)
        {
            PrepareRequestBodyForRelaying((OnPremiseConnectorRequest)request);

            _logger?.Verbose("Sending on premise connector request. request-id={RequestId}, link-id={LinkId}", request.RequestId, linkId);
            _backendCommunication.SendOnPremiseConnectorRequest(linkId, request);
        }
Exemple #8
0
        public void LogRequest(IOnPremiseConnectorRequest onPremiseConnectorRequest, IOnPremiseTargetReponse onPremiseTargetReponse, HttpStatusCode responseStatusCode, Guid linkId, string originId, string relayPath)
        {
            if (onPremiseConnectorRequest == null)
            {
                throw new ArgumentNullException(nameof(onPremiseConnectorRequest), "A client request must be set.");
            }

            var onPremiseTargetInformation = GetOnPremiseTargetInformation(onPremiseTargetReponse);
            var pathInformation            = _pathSplitter.Split(relayPath);

            _logRepository.LogRequest(new RequestLogEntry
            {
                LocalUrl                  = pathInformation.LocalUrl,
                HttpStatusCode            = responseStatusCode,
                ContentBytesIn            = GetContentByteCount(onPremiseConnectorRequest.Body),
                ContentBytesOut           = onPremiseTargetInformation.ContentBytesOut,
                OnPremiseConnectorInDate  = onPremiseConnectorRequest.RequestStarted,
                OnPremiseConnectorOutDate = onPremiseConnectorRequest.RequestFinished,
                OnPremiseTargetInDate     = onPremiseTargetInformation.OnPremiseTargetInDate,
                OnPremiseTargetOutDate    = onPremiseTargetInformation.OnPremiseTargetOutDate,
                OriginId                  = Guid.Parse(originId),
                OnPremiseTargetKey        = pathInformation.OnPremiseTargetKey,
                LinkId = linkId
            });
        }
        private async Task ForwardClientRequest(string connectionId, IOnPremiseConnectorRequest onPremiseConnectorRequest)
        {
            _logger.Debug("Forwarding client request to connection '{0}'", connectionId);
            _logger.Trace("Forwarding client request to connection '{0}'. request-id={1}, http-method={2}, url={3}, origin-id={4}, body-length={5}",
                          connectionId, onPremiseConnectorRequest.RequestId, onPremiseConnectorRequest.HttpMethod, onPremiseConnectorRequest.Url, onPremiseConnectorRequest.OriginId, onPremiseConnectorRequest.Body?.Length ?? 0);

            var onPremiseTargetRequest = new OnPremiseTargetRequest
            {
                RequestId   = onPremiseConnectorRequest.RequestId,
                HttpMethod  = onPremiseConnectorRequest.HttpMethod,
                Url         = onPremiseConnectorRequest.Url,
                HttpHeaders = onPremiseConnectorRequest.HttpHeaders,
                OriginId    = onPremiseConnectorRequest.OriginId
            };

            if (onPremiseConnectorRequest.Body != null)
            {
                if (onPremiseConnectorRequest.Body.Length > 0x20000) // 128k
                {
                    _temporaryStore.Save(onPremiseConnectorRequest.RequestId, onPremiseConnectorRequest.Body);
                    onPremiseTargetRequest.Body = string.Empty;
                }
                else
                {
                    onPremiseTargetRequest.Body = Convert.ToBase64String(onPremiseConnectorRequest.Body);
                }
            }

            await Connection.Send(connectionId, onPremiseTargetRequest);
        }
        public void SendOnPremiseConnectorRequest(Guid linkId, IOnPremiseConnectorRequest request)
        {
            CheckDisposed();

            _logger?.Debug("Dispatching request. request-id={RequestId}, link-id={LinkId}", request.RequestId, linkId);

            _messageDispatcher.DispatchRequest(linkId, request);
        }
        public override async Task SendOnPremiseConnectorRequest(string onPremiseId, IOnPremiseConnectorRequest onPremiseConnectorRequest)
        {
            CheckDisposed();

            _logger.Debug("Sending client request for On-Premise Connector '{0}'", onPremiseId);

            var queue = DeclareOnPremiseQueue(onPremiseId);
            await _bus.Advanced.PublishAsync(Exchange.GetDefault(), queue.Name, false, false, new Message <string>(JsonConvert.SerializeObject(onPremiseConnectorRequest)));
        }
Exemple #12
0
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            var uri = new Uri(new Uri("http://localhost"), request.Url);

            _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                             connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? uri.PathAndQuery : uri.AbsolutePath, request.OriginId, request.ContentLength);

            await Connection.Send(connectionId, request).ConfigureAwait(false);
        }
Exemple #13
0
 private InterceptedRequest CreateInterceptedRequest(IOnPremiseConnectorRequest request, HttpRequestMessage message, IPrincipal clientUser)
 {
     return(new InterceptedRequest(request)
     {
         ClientIpAddress = GetRemoteIpAddress(request, message),
         ClientUser = clientUser,
         ClientRequestUri = message.RequestUri,
     });
 }
        public void DispatchRequest(Guid linkId, IOnPremiseConnectorRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            CheckDisposed();
            _logger?.Debug("Dispatching request. link-id={LinkId}, request-id={RequestId}, method={HttpMethod}, url={RequestUrl}", linkId, request.RequestId, request.HttpMethod, request.Url);

            TryGetRequestSubject(linkId)?.OnNext(request);
        }
Exemple #15
0
        private IPAddress GetRemoteIpAddress(IOnPremiseConnectorRequest request, HttpRequestMessage message)
        {
            try
            {
                return(message.GetRemoteIpAddress());
            }
            catch (Exception ex)
            {
                _logger?.Warning(ex, "Could not fetch remote IP address. request-id={RequestId}", request.RequestId);
            }

            return(null);
        }
        public void DispatchRequest(Guid linkId, IOnPremiseConnectorRequest request)
        {
            var content = Serialize(request, out var props);

            if (request.Expiration != TimeSpan.Zero)
            {
                _logger?.Verbose("Setting RabbitMQ message TTL. request-id={RequestId}, request-expiration={RequestExpiration}", request.RequestId, request.Expiration);
                props.Expiration = request.Expiration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
            }

            lock (_model)
            {
                _model.BasicPublish(_EXCHANGE_NAME, $"{_REQUEST_QUEUE_PREFIX}{linkId}", false, props, content);
            }
        }
        private void FinishRequest(IOnPremiseConnectorRequest request, IOnPremiseConnectorResponse response, Guid linkId, string path, HttpStatusCode statusCode)
        {
            request.RequestFinished = DateTime.UtcNow;

            _logger?.Verbose("Finishing request. request-id={RequestId}, link-id={LinkId}, on-premise-duration={RemoteDuration}, global-duration={GlobalDuration}", request.RequestId, linkId, response?.RequestFinished - response?.RequestStarted, request.RequestFinished - request.RequestStarted);

            // TODO this may be debounced for e.g. 5 minutes to skip querying on each request in future release
            var currentTraceConfigurationId = _traceManager.GetCurrentTraceConfigurationId(linkId);

            if (currentTraceConfigurationId != null)
            {
                _traceManager.Trace(request, response, currentTraceConfigurationId.Value);
            }

            _requestLogger.LogRequest(request, response, linkId, _backendCommunication.OriginId, path, statusCode);
        }
Exemple #18
0
 public InterceptedRequest(IOnPremiseConnectorRequest other)
 {
     RequestId                      = other.RequestId;
     OriginId                       = other.OriginId;
     AcknowledgeId                  = other.AcknowledgeId;
     RequestStarted                 = other.RequestStarted;
     RequestFinished                = other.RequestFinished;
     HttpMethod                     = other.HttpMethod;
     Url                            = other.Url;
     HttpHeaders                    = other.HttpHeaders;
     Body                           = other.Body;
     AcknowledgmentMode             = other.AcknowledgmentMode;
     Stream                         = other.Stream;
     ContentLength                  = other.ContentLength;
     AlwaysSendToOnPremiseConnector = other.AlwaysSendToOnPremiseConnector;
     Expiration                     = other.Expiration;
     AcknowledgeOriginId            = other.AcknowledgeOriginId;
 }
        public HttpResponseMessage HandleResponse(IOnPremiseConnectorRequest request, HttpRequestMessage message, IPrincipal clientUser, IOnPremiseConnectorResponse response, bool forwardOnPremiseTargetErrorResponse)
        {
            if (_responseInterceptor == null)
            {
                return(_httpResponseMessageBuilder.BuildFromConnectorResponse(response, forwardOnPremiseTargetErrorResponse, request.RequestId));
            }

            _logger.Verbose("Handling response. request-id={RequestId}", request.RequestId);

            try
            {
                var interceptedRequest = CreateInterceptedRequest(request, message, clientUser);

                HttpResponseMessage immediateResponse = null;
                if (response == null)
                {
                    immediateResponse = _responseInterceptor.OnResponseFailed(interceptedRequest);
                }
                else
                {
                    var interceptedResponse = new InterceptedResponse(_logger.ForContext <IInterceptedResponse>(), response);
                    immediateResponse = _responseInterceptor.OnResponseReceived(interceptedRequest, interceptedResponse);

                    if (immediateResponse == null)
                    {
                        return(_httpResponseMessageBuilder.BuildFromConnectorResponse(interceptedResponse, forwardOnPremiseTargetErrorResponse, request.RequestId));
                    }
                }

                if (immediateResponse != null)
                {
                    immediateResponse.RequestMessage = message;
                }

                return(immediateResponse ?? _httpResponseMessageBuilder.BuildFromConnectorResponse(null, forwardOnPremiseTargetErrorResponse, request.RequestId));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error while executing the response interceptor. type-name={InterceptorType}, request-id={RequestId}", _requestInterceptor?.GetType().Name, request.RequestId);
                return(null);
            }
        }
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            var uri = new Uri(new Uri("http://localhost"), request.Url);

            _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                             connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? uri.PathAndQuery : uri.AbsolutePath, request.OriginId, request.ContentLength);

            var json = JObject.FromObject(request);

            if (request.Properties != null)
            {
                json.Remove(nameof(IOnPremiseConnectorRequest.Properties));

                foreach (var kvp in request.Properties)
                {
                    json[kvp.Key] = JToken.FromObject(kvp.Value);
                }
            }

            await Connection.Send(connectionId, json).ConfigureAwait(false);
        }
Exemple #21
0
        public void Trace(IOnPremiseConnectorRequest request, IOnPremiseConnectorResponse response, Guid traceConfigurationId)
        {
            try
            {
                if (!Directory.Exists(_configuration.TraceFileDirectory))
                {
                    Directory.CreateDirectory(_configuration.TraceFileDirectory);
                }

                var filenamePrefix = $"{Path.Combine(_configuration.TraceFileDirectory, traceConfigurationId.ToString())}-{DateTime.Now.Ticks}";
                _traceFileWriter.WriteHeaderFileAsync(filenamePrefix + _ON_PREMISE_CONNECTOR_HEADER_EXTENSION, request.HttpHeaders);
                _traceFileWriter.WriteContentFileAsync(filenamePrefix + _ON_PREMISE_CONNECTOR_CONTENT_EXTENSION, request.Body);

                _traceFileWriter.WriteHeaderFileAsync(filenamePrefix + _ON_PREMISE_TARGET_HEADER_EXTENSION, response.HttpHeaders);
                _traceFileWriter.WriteContentFileAsync(filenamePrefix + _ON_PREMISE_TARGET_CONTENT_EXTENSION, response.Body);
            }
            catch (Exception ex)
            {
                _logger?.Warning(ex, "Could not create trace");
            }
        }
Exemple #22
0
        public void Trace(IOnPremiseConnectorRequest onPremiseConnectorRequest, IOnPremiseTargetReponse onPremiseTargetReponse, Guid traceConfigurationId)
        {
            try
            {
                if (!Directory.Exists(_configuration.TraceFileDirectory))
                {
                    Directory.CreateDirectory(_configuration.TraceFileDirectory);
                }

                var filenamePrefix = $"{Path.Combine(_configuration.TraceFileDirectory, traceConfigurationId.ToString())}-{DateTime.Now.Ticks}";

                _traceFileWriter.WriteHeaderFile(filenamePrefix + OnPremiseConnectorHeaderExtension, onPremiseConnectorRequest.HttpHeaders);
                _traceFileWriter.WriteContentFile(filenamePrefix + OnPremiseConnectorContentExtension, onPremiseConnectorRequest.Body);

                _traceFileWriter.WriteHeaderFile(filenamePrefix + OnPremiseTargetHeaderExtension, onPremiseTargetReponse.HttpHeaders);
                _traceFileWriter.WriteContentFile(filenamePrefix + OnPremiseTargetContentExtension, onPremiseTargetReponse.Body);
            }
            catch (Exception ex)
            {
                _logger.Warn("Could not create trace", ex);
            }
        }
 public void LogRequest(IOnPremiseConnectorRequest request, IOnPremiseConnectorResponse response, Guid linkId, Guid originId, String relayPath, HttpStatusCode?statusCode)
 {
     // Noop
 }
 public abstract Task SendOnPremiseConnectorRequest(string onPremiseId, IOnPremiseConnectorRequest onPremiseConnectorRequest);
Exemple #25
0
 public void DispatchRequest(Guid linkId, IOnPremiseConnectorRequest request)
 {
     EnsureRequestChannel(linkId.ToString()).Dispatch(request);
 }