public void TestClone() { var c = new Config(); var backoffStrategy = new FullJitterStrategy(); var tlsConfig = new TlsConfig(); c.Set("dial_timeout", "50ms"); c.Set("read_timeout", "5m"); c.Set("heartbeat_interval", "2s"); c.Set("rdy_redistribute_interval", "3s"); c.Set("backoff_strategy", backoffStrategy); c.Set("rdy_redistribute_on_idle", true); c.Set("tls_config", tlsConfig); c.Validate(); var c2 = c.Clone(); Assert.AreEqual(TimeSpan.FromMilliseconds(50), c2.DialTimeout, "dial_timeout"); Assert.AreEqual(TimeSpan.FromMinutes(5), c2.ReadTimeout, "read_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(10), c2.WriteTimeout, "write_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(60), c2.LookupdPollInterval, "lookupd_poll_interval"); Assert.AreEqual(0.3, c2.LookupdPollJitter, "lookupd_poll_jitter"); Assert.AreEqual(TimeSpan.FromMinutes(15), c2.MaxRequeueDelay, "max_requeue_delay"); Assert.AreEqual(TimeSpan.FromSeconds(90), c2.DefaultRequeueDelay, "default_requeue_delay"); Assert.AreEqual(backoffStrategy, c2.BackoffStrategy, "backoff_strategy"); Assert.AreEqual(TimeSpan.FromMinutes(2), c2.MaxBackoffDuration, "max_backoff_duration"); Assert.AreEqual(TimeSpan.FromSeconds(1), c2.BackoffMultiplier, "backoff_multiplier"); Assert.AreEqual(5, c2.MaxAttempts, "max_attempts"); Assert.AreEqual(TimeSpan.FromSeconds(10), c2.LowRdyIdleTimeout, "low_rdy_idle_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(3), c2.RDYRedistributeInterval, "rdy_redistribute_interval"); Assert.AreEqual(true, c2.RDYRedistributeOnIdle, "rdy_redistribute_on_idle"); Assert.AreEqual(OS.Hostname().Split('.')[0], c2.ClientID, "client_id"); Assert.AreEqual(OS.Hostname(), c2.Hostname, "hostname"); Assert.AreEqual(string.Format("{0}/{1}", ClientInfo.ClientName, ClientInfo.Version), c2.UserAgent, "user_agent"); Assert.AreEqual(TimeSpan.FromSeconds(2), c2.HeartbeatInterval, "heartbeat_interval"); Assert.AreEqual(0, c2.SampleRate, "sample_rate"); Assert.AreEqual(tlsConfig, c2.TlsConfig, "tls_config"); //Assert.AreEqual(false, c2.Deflate, "deflate"); // TODO: Deflate //Assert.AreEqual(6, c2.DeflateLevel, "deflate_level"); // TODO: Deflate //Assert.AreEqual(false, c2.Snappy, "snappy"); // TODO: Snappy Assert.AreEqual(16384, c2.OutputBufferSize, "output_buffer_size"); Assert.AreEqual(TimeSpan.FromMilliseconds(250), c2.OutputBufferTimeout, "output_buffer_timeout"); Assert.AreEqual(1, c2.MaxInFlight, "max_in_flight"); Assert.AreEqual(TimeSpan.Zero, c2.MessageTimeout, "msg_timeout"); Assert.IsNull(c2.AuthSecret, "auth_secret"); }
public void UpgradeTls(TlsConfig tlsConfig) { if (tlsConfig == null) { throw new ArgumentNullException("tlsConfig"); } lock (_readLocker) { lock (_writeLocker) { const bool leaveInnerStreamOpen = false; var enabledSslProtocols = tlsConfig.GetEnabledSslProtocols(); string errorMessage = null; var sslStream = new SslStream( _networkStream, leaveInnerStreamOpen, (sender, certificate, chain, sslPolicyErrors) => ValidateCertificates(chain, sslPolicyErrors, tlsConfig, out errorMessage) ); try { var certCollection = new X509Certificate2Collection(); if (tlsConfig.ClientCertificate != null) { certCollection.Add(tlsConfig.ClientCertificate); } sslStream.AuthenticateAsClient(_hostname, new X509Certificate2Collection(), enabledSslProtocols, tlsConfig.CheckCertificateRevocation); } catch (Exception ex) { throw new Exception(string.Format("{0} - {1}", ex.Message, errorMessage), ex); } _networkStream = sslStream; } } }
private static bool ValidateCertificates(X509Chain chain, SslPolicyErrors sslPolicyErrors, TlsConfig tlsConfig, out string errorMessage) { errorMessage = null; if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable) { errorMessage = chain.ChainStatus.GetErrors(); return(false); } if (tlsConfig.InsecureSkipVerify || sslPolicyErrors == SslPolicyErrors.None) { return(true); } else { errorMessage = chain.ChainStatus.GetErrors(); return(false); } }
private static bool ValidateCertificates(X509Chain chain, SslPolicyErrors sslPolicyErrors, TlsConfig tlsConfig, out string errorMessage) { errorMessage = null; if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable) { errorMessage = chain.ChainStatus.GetErrors(); return false; } if (tlsConfig.InsecureSkipVerify || sslPolicyErrors == SslPolicyErrors.None) { return true; } else { errorMessage = chain.ChainStatus.GetErrors(); return false; } }
public void UpgradeTls(TlsConfig tlsConfig) { if (tlsConfig == null) throw new ArgumentNullException("tlsConfig"); lock (_readLocker) { lock (_writeLocker) { const bool leaveInnerStreamOpen = false; var enabledSslProtocols = tlsConfig.GetEnabledSslProtocols(); string errorMessage = null; var sslStream = new SslStream( _networkStream, leaveInnerStreamOpen, (sender, certificate, chain, sslPolicyErrors) => ValidateCertificates(chain, sslPolicyErrors, tlsConfig, out errorMessage) ); try { sslStream.AuthenticateAsClient(_hostname, new X509Certificate2Collection(), enabledSslProtocols, tlsConfig.CheckCertificateRevocation); } catch (Exception ex) { throw new Exception(string.Format("{0} - {1}", ex.Message, errorMessage), ex); } _networkStream = sslStream; } } }
public void TestMaxValues() { var c = new Config(); var tlsConfig = new TlsConfig(); c.Set("read_timeout", TimeSpan.FromMinutes(5)); c.Set("write_timeout", TimeSpan.FromMinutes(5)); c.Set("lookupd_poll_interval", TimeSpan.FromMinutes(5)); c.Set("lookupd_poll_jitter", 1); c.Set("max_requeue_delay", TimeSpan.FromMinutes(60)); c.Set("default_requeue_delay", TimeSpan.FromMinutes(60)); c.Set("backoff_strategy", "full_jitter"); c.Set("max_backoff_duration", TimeSpan.FromMinutes(60)); c.Set("backoff_multiplier", TimeSpan.FromMinutes(60)); c.Set("max_attempts", 65535); c.Set("low_rdy_idle_timeout", TimeSpan.FromMinutes(5)); c.Set("rdy_redistribute_interval", TimeSpan.FromSeconds(5)); c.Set("client_id", "my"); c.Set("hostname", "my.host.name.com"); c.Set("user_agent", "user-agent/1.0"); c.Set("heartbeat_interval", TimeSpan.MaxValue); c.Set("sample_rate", 99); c.Set("tls_config", tlsConfig); //c.Set("deflate", true); // TODO: Deflate //c.Set("deflate_level", 9); // TODO: Deflate //c.Set("snappy", true); // TODO: Snappy c.Set("output_buffer_size", Int64.MaxValue); c.Set("output_buffer_timeout", TimeSpan.MaxValue); c.Set("max_in_flight", int.MaxValue); c.Set("msg_timeout", TimeSpan.MaxValue); c.Set("auth_secret", "!@#@#$#%"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.ReadTimeout, "read_timeout"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.WriteTimeout, "write_timeout"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.LookupdPollInterval, "lookupd_poll_interval"); Assert.AreEqual(1, c.LookupdPollJitter, "lookupd_poll_jitter"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.MaxRequeueDelay, "max_requeue_delay"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.DefaultRequeueDelay, "default_requeue_delay"); Assert.AreEqual(typeof(FullJitterStrategy), c.BackoffStrategy.GetType(), "backoff_strategy"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.MaxBackoffDuration, "max_backoff_duration"); Assert.AreEqual(TimeSpan.FromMinutes(60), c.BackoffMultiplier, "backoff_multiplier"); Assert.AreEqual(65535, c.MaxAttempts, "max_attempts"); Assert.AreEqual(TimeSpan.FromMinutes(5), c.LowRdyIdleTimeout, "low_rdy_idle_timeout"); Assert.AreEqual(TimeSpan.FromSeconds(5), c.RDYRedistributeInterval, "rdy_redistribute_interval"); Assert.AreEqual("my", c.ClientID, "client_id"); Assert.AreEqual("my.host.name.com", c.Hostname, "hostname"); Assert.AreEqual("user-agent/1.0", c.UserAgent, "user_agent"); Assert.AreEqual(TimeSpan.MaxValue, c.HeartbeatInterval, "heartbeat_interval"); Assert.AreEqual(99, c.SampleRate, "sample_rate"); Assert.AreEqual(tlsConfig, c.TlsConfig, "tls_config"); //Assert.AreEqual(true, c.Deflate, "deflate"); // TODO: Deflate //Assert.AreEqual(9, c.DeflateLevel, "deflate_level"); // TODO: Deflate //Assert.AreEqual(true, c.Snappy, "snappy"); // TODO: Snappy Assert.AreEqual(Int64.MaxValue, c.OutputBufferSize, "output_buffer_size"); Assert.AreEqual(TimeSpan.MaxValue, c.OutputBufferTimeout, "output_buffer_timeout"); Assert.AreEqual(int.MaxValue, c.MaxInFlight, "max_in_flight"); Assert.AreEqual(TimeSpan.MaxValue, c.MessageTimeout, "msg_timeout"); Assert.AreEqual("!@#@#$#%", c.AuthSecret, "auth_secret"); }