Exemplo n.º 1
0
        public void Connections_CanConnectToServer_ThrowsException_ExpectErrorResult()
        {
            //------------Setup for test--------------------------
            const string address = "http://localhost:3142/dsf";
            var          conn    = new Connection
            {
                ResourceType = "Server",
                Address      = address
            };
            var hubFactory      = new Mock <IHubFactory>();
            var clientException = new HttpClientException(new HttpResponseMessage(HttpStatusCode.BadRequest))
            {
                Response = { ReasonPhrase = "error" }
            };

            hubFactory.Setup(factory => factory.CreateHubProxy(conn)).Throws(new Exception("", clientException));
            var myConnections = new Connections(() => new List <string>(), hubFactory.Object);

            //------------Execute Test---------------------------
            var canConnectToServer = myConnections.CanConnectToServer(conn);

            //------------Assert Results-------------------------
            Assert.IsFalse(canConnectToServer.IsValid);
            Assert.AreEqual("Connection Error : error", canConnectToServer.ErrorMessage);
        }
Exemplo n.º 2
0
        protected virtual string ConnectToServer(Dev2.Data.ServiceModel.Connection connection)
        {
            // we need to grab the principle and impersonate to properly execute in context of the requesting user ;)
            var proxy = CreateHubProxy(connection);

            return("Success");
        }
Exemplo n.º 3
0
        public IHubProxy CreateHubProxy(Connection connection)
        {
            var serverUser = Common.Utilities.OrginalExecutingUser;
            var principle  = serverUser;

            var identity = principle.Identity as WindowsIdentity;
            WindowsImpersonationContext context = null;

            try
            {
                if (identity != null && connection.AuthenticationType == AuthenticationType.Windows)
                {
                    context = identity.Impersonate();
                }

                using (var client = new WebClient())
                {
                    if (connection.AuthenticationType == AuthenticationType.Windows)
                    {
                        client.UseDefaultCredentials = true;
                    }
                    else
                    {
                        client.UseDefaultCredentials = false;

                        //// we to default to the hidden public user name of \, silly know but that is how to get around ntlm auth ;)
                        if (connection.AuthenticationType == AuthenticationType.Public)
                        {
                            connection.UserName = GlobalConstants.PublicUsername;
                            connection.Password = string.Empty;
                        }

                        client.Credentials = new NetworkCredential(connection.UserName, connection.Password);
                    }

                    var connectionAddress = connection.FetchTestConnectionAddress();
                    var hub = new HubConnection(connectionAddress)
                    {
                        Credentials = client.Credentials
                    };
                    hub.Error += exception => { };
                    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
                    var proxy = hub.CreateHubProxy("esb");
                    if (!hub.Start().Wait(GlobalConstants.NetworkTimeOut))
                    {
                        throw new HttpClientException(new HttpResponseMessage(HttpStatusCode.GatewayTimeout));
                    }
                    return(proxy);
                }
            }
            finally
            {
                if (context != null && connection.AuthenticationType == AuthenticationType.Windows)
                {
                    context.Undo();
                }
            }
        }
Exemplo n.º 4
0
        // POST: Service/Connections/Test

        #endregion

        public ValidationResult CanConnectToServer(Dev2.Data.ServiceModel.Connection connection)
        {
            var result = new ValidationResult
            {
                ErrorFields = new ArrayList(new[] { "address" }),
            };

            try
            {
                // Validate URI, ports, etc...
                // ReSharper disable ObjectCreationAsStatement
                new Uri(connection.Address);
                // ReSharper restore ObjectCreationAsStatement

                var connectResult = ConnectToServer(connection);
                if (!string.IsNullOrEmpty(connectResult))
                {
                    if (connectResult.Contains("FatalError"))
                    {
                        var error = XElement.Parse(connectResult);
                        result.IsValid      = false;
                        result.ErrorMessage = string.Join(" - ", error.Nodes().Cast <XElement>().Select(n => n.Value));
                    }
                }
            }
            catch (Exception ex)
            {
                var hex = ex.InnerException as HttpClientException;
                if (hex != null)
                {
                    result.IsValid      = false; // This we know how to handle this
                    result.ErrorMessage = "Connection Error : " + hex.Response.ReasonPhrase;
                    return(result);
                }

                result.IsValid = false;
                // get something more relevant ;)
                if (ex.Message == "One or more errors occurred." && ex.InnerException != null)
                {
                    result.ErrorMessage = "Connection Error : " + GetLastExeptionMessage(ex);
                }
                else
                {
                    var msg = ex.Message;
                    if (msg.IndexOf("Connection Error : ", StringComparison.Ordinal) >= 0 || msg.IndexOf("Invalid URI:", StringComparison.Ordinal) >= 0)
                    {
                        result.ErrorMessage = ex.Message;
                    }
                    else
                    {
                        result.ErrorMessage = "Connection Error : " + ex.Message;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        public ValidationResult CanConnectToServer(Dev2.Data.ServiceModel.Connection connection)
        {
            var result = new ValidationResult
            {
                ErrorFields = new ArrayList(new[] { "address" }),
            };

            try
            {
                // Validate URI, ports, etc...
#pragma warning disable S1848 // Objects should not be created to be dropped immediately without being used
                new Uri(connection.Address);
#pragma warning restore S1848 // Objects should not be created to be dropped immediately without being used

                var connectResult = ConnectToServer(connection);
                if (!string.IsNullOrEmpty(connectResult) && connectResult.Contains("FatalError"))
                {
                    var error = XElement.Parse(connectResult);
                    result.IsValid      = false;
                    result.ErrorMessage = string.Join(" - ", error.Nodes().Cast <XElement>().Select(n => n.Value));
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException is HttpClientException hex)
                {
                    result.IsValid      = false; // This we know how to handle this
                    result.ErrorMessage = Resources.ConnectionError + hex.Response.ReasonPhrase;
                    return(result);
                }

                result.IsValid = false;
                // get something more relevant ;)
                if (ex.Message == "One or more errors occurred." && ex.InnerException != null)
                {
                    result.ErrorMessage = Resources.ConnectionError + GetLastExeptionMessage(ex);
                }
                else
                {
                    var msg = ex.Message;
                    result.ErrorMessage = msg.IndexOf(Resources.ConnectionError, StringComparison.Ordinal) >= 0 || msg.IndexOf("Invalid URI:", StringComparison.Ordinal) >= 0 ? ex.Message : Resources.ConnectionError + ex.Message;
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        // POST: Service/Connections/Get
        public Dev2.Data.ServiceModel.Connection Get(string resourceID, Guid workspaceID, Guid dataListID)
        {
            var result = new Dev2.Data.ServiceModel.Connection { ResourceID = Guid.Empty, ResourceType = ResourceType.Server, WebServerPort = Dev2.Data.ServiceModel.Connection.DefaultWebServerPort };
            try
            {

                var contents = ResourceCatalog.Instance.GetResourceContents(workspaceID, Guid.Parse(resourceID));
                if(contents != null && contents.Length > 0)
                {
                    var xml = contents.ToXElement();
                    result = new Dev2.Data.ServiceModel.Connection(xml);
                }
            }
            catch(Exception ex)
            {
                RaiseError(ex);
            }
            return result;
        }
Exemplo n.º 7
0
        public void Connections_CanConnectToServer_InvalidAddress_ExpectErrorResult()
        {
            //------------Setup for test--------------------------
            const string address = "http:://localhost:3142/dsf";
            var          conn    = new Connection
            {
                ResourceType = "Server",
                Address      = address
            };
            var hubProxy      = new Mock <IHubFactory>();
            var myConnections = new Connections(() => new List <string>(), hubProxy.Object);


            //------------Execute Test---------------------------
            var canConnectToServer = myConnections.CanConnectToServer(conn);

            //------------Assert Results-------------------------
            Assert.AreEqual("Invalid URI: The Authority/Host could not be parsed.", canConnectToServer.ErrorMessage);
            Assert.IsFalse(canConnectToServer.IsValid);
        }
Exemplo n.º 8
0
        // POST: Service/Connections/Get
        public Dev2.Data.ServiceModel.Connection Get(string resourceID, Guid workspaceID, Guid dataListID)
        {
            var result = new Dev2.Data.ServiceModel.Connection {
                ResourceID = Guid.Empty, ResourceType = ResourceType.Server, WebServerPort = Dev2.Data.ServiceModel.Connection.DefaultWebServerPort
            };

            try
            {
                var contents = ResourceCatalog.Instance.GetResourceContents(workspaceID, Guid.Parse(resourceID));
                if (contents != null && contents.Length > 0)
                {
                    var xml = contents.ToXElement();
                    result = new Dev2.Data.ServiceModel.Connection(xml);
                }
            }
            catch (Exception ex)
            {
                RaiseError(ex);
            }
            return(result);
        }
Exemplo n.º 9
0
        public void Connections_CanConnectToServer_ValidAddress_ExpectPassResult()
        {
            //------------Setup for test--------------------------
            const string address = "http://localhost:3142/dsf";
            var          conn    = new Connection
            {
                ResourceType = "Server",
                Address      = address
            };
            var hubFactory = new Mock <IHubFactory>();
            var hubProxy   = new Mock <IHubProxy>();

            hubFactory.Setup(factory => factory.CreateHubProxy(conn)).Returns(hubProxy.Object);
            var myConnections = new Connections(() => new List <string>(), hubFactory.Object);


            //------------Execute Test---------------------------
            var canConnectToServer = myConnections.CanConnectToServer(conn);

            //------------Assert Results-------------------------
            Assert.IsTrue(canConnectToServer.IsValid);
        }
Exemplo n.º 10
0
 public IHubProxy CreateHubProxy(Dev2.Data.ServiceModel.Connection connection) => _hubFactory.CreateHubProxy(connection);
Exemplo n.º 11
0
        protected virtual string ConnectToServer(Dev2.Data.ServiceModel.Connection connection)
        {
            // we need to grab the principle and impersonate to properly execute in context of the requesting user ;)
            var principle = System.Threading.Thread.CurrentPrincipal;
            var identity  = principle.Identity as WindowsIdentity;
            WindowsImpersonationContext context = null;

            try
            {
                if (identity != null && connection.AuthenticationType == AuthenticationType.Windows)
                {
                    context = identity.Impersonate();
                }

                using (var client = new WebClient())
                {
                    if (connection.AuthenticationType == AuthenticationType.Windows)
                    {
                        client.UseDefaultCredentials = true;
                    }
                    else
                    {
                        client.UseDefaultCredentials = false;

                        //// we to default to the hidden public user name of \, silly know but that is how to get around ntlm auth ;)
                        if (connection.AuthenticationType == AuthenticationType.Public)
                        {
                            connection.UserName = GlobalConstants.PublicUsername;
                            connection.Password = string.Empty;
                        }

                        client.Credentials = new NetworkCredential(connection.UserName, connection.Password);
                    }

                    // Need to do hub connect here to get true permissions ;)
                    HubConnection hub = null;
                    try
                    {
                        // Credentials = client.Credentials
                        hub = new HubConnection(connection.FetchTestConnectionAddress())
                        {
                            Credentials = client.Credentials
                        };
                        ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
#pragma warning disable 168
                        var proxy = hub.CreateHubProxy("esb"); // this is the magic line that causes proper validation
#pragma warning restore 168
                        hub.Start().Wait();

                        Dev2Logger.Log.Debug("Hub State : " + hub.State);

                        return("Success");
                    }
                    finally
                    {
                        if (hub != null)
                        {
                            hub.Stop();
                            hub.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (context != null && connection.AuthenticationType == AuthenticationType.Windows)
                {
                    context.Undo();
                }
            }
        }