Skip to content

Merge pull request #1169 from rabbitmq/lukebakken/fix-tls-concourse-ci #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions projects/Unit/TestConnectionRecovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,21 @@ public void TestBasicConnectionRecoveryStopsAfterManualClose()
AutorecoveringConnection c = CreateAutorecoveringConnection();
var latch = new AutoResetEvent(false);
c.ConnectionRecoveryError += (o, args) => latch.Set();
StopRabbitMQ();
latch.WaitOne(30000); // we got the failed reconnection event.
bool triedRecoveryAfterClose = false;
c.Close();
Thread.Sleep(5000);
c.ConnectionRecoveryError += (o, args) => triedRecoveryAfterClose = true;
Thread.Sleep(10000);
Assert.IsFalse(triedRecoveryAfterClose);
StartRabbitMQ();
try
{
StopRabbitMQ();
latch.WaitOne(30000); // we got the failed reconnection event.
bool triedRecoveryAfterClose = false;
c.Close();
Thread.Sleep(5000);
c.ConnectionRecoveryError += (o, args) => triedRecoveryAfterClose = true;
Thread.Sleep(10000);
Assert.False(triedRecoveryAfterClose);
}
finally
{
StartRabbitMQ();
}
}

[Test]
Expand Down Expand Up @@ -767,14 +773,21 @@ public void TestShutdownEventHandlersRecoveryOnConnectionAfterDelayedServerResta
ManualResetEventSlim recoveryLatch = PrepareForRecovery((AutorecoveringConnection)Conn);

Assert.IsTrue(Conn.IsOpen);
StopRabbitMQ();
Console.WriteLine("Stopped RabbitMQ. About to sleep for multiple recovery intervals...");
Thread.Sleep(7000);
StartRabbitMQ();

try
{
StopRabbitMQ();
Console.WriteLine("Stopped RabbitMQ. About to sleep for multiple recovery intervals...");
Thread.Sleep(7000);
}
finally
{
StartRabbitMQ();
}

Wait(shutdownLatch, TimeSpan.FromSeconds(30));
Wait(recoveryLatch, TimeSpan.FromSeconds(30));
Assert.IsTrue(Conn.IsOpen);

Assert.IsTrue(counter >= 1);
}

Expand Down
5 changes: 3 additions & 2 deletions projects/Unit/TestFloodPublishing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using NUnit.Framework;
using RabbitMQ.Client.Events;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;
using RabbitMQ.Client.Events;

namespace RabbitMQ.Client.Unit
{
[TestFixture]
Expand Down
21 changes: 12 additions & 9 deletions projects/Unit/TestHeartbeats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

using NUnit.Framework;
Expand Down Expand Up @@ -58,8 +59,7 @@ public void TestThatHeartbeatWriterWithTLSEnabled()
{
if (!LongRunningTestsEnabled())
{
Console.WriteLine("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
return;
Assert.Ignore("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
}

var cf = new ConnectionFactory()
Expand All @@ -70,15 +70,18 @@ public void TestThatHeartbeatWriterWithTLSEnabled()
};

string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
string certPassphrase = Environment.GetEnvironmentVariable("PASSWORD");
bool sslConfigured = Directory.Exists(sslDir) &&
(false == string.IsNullOrEmpty(certPassphrase));
if (false == sslConfigured)
{
Console.WriteLine("SSL_CERTS_DIR is not configured, skipping test");
return;
Assert.Ignore("SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
}
cf.Ssl.ServerName = System.Net.Dns.GetHostName();
Assert.IsNotNull(sslDir);
cf.Ssl.CertPath = $"{sslDir}/client_key.p12";
cf.Ssl.CertPassphrase = Environment.GetEnvironmentVariable("PASSWORD");

string hostName = System.Net.Dns.GetHostName();
cf.Ssl.ServerName = hostName;
cf.Ssl.CertPath = $"{sslDir}/client_{hostName}_key.p12";
cf.Ssl.CertPassphrase = certPassphrase;
cf.Ssl.Enabled = true;

RunSingleConnectionTest(cf);
Expand Down
88 changes: 48 additions & 40 deletions projects/Unit/TestSsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
//---------------------------------------------------------------------------

using System;
using System.IO;
using System.Net.Security;
using System.Reflection;
using System.Security.Authentication;

using NUnit.Framework;
Expand All @@ -40,37 +42,25 @@ namespace RabbitMQ.Client.Unit
[TestFixture]
public class TestSsl
{
public void SendReceive(ConnectionFactory cf)
{
using (IConnection conn = cf.CreateConnection())
{
IModel ch = conn.CreateModel();

ch.ExchangeDeclare("Exchange_TestSslEndPoint", ExchangeType.Direct);
string qName = ch.QueueDeclare();
ch.QueueBind(qName, "Exchange_TestSslEndPoint", "Key_TestSslEndpoint", null);

string message = "Hello C# SSL Client World";
byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);
ch.BasicPublish("Exchange_TestSslEndPoint", "Key_TestSslEndpoint", null, msgBytes);

bool autoAck = false;
BasicGetResult result = ch.BasicGet(qName, autoAck);
byte[] body = result.Body.ToArray();
string resultMessage = System.Text.Encoding.UTF8.GetString(body);
private readonly string _testDisplayName;
private readonly string _sslDir;
private readonly string _certPassphrase;
private readonly bool _sslConfigured;

Assert.AreEqual(message, resultMessage);
}
public TestSsl()
{
_sslDir = IntegrationFixture.CertificatesDirectory();
_certPassphrase = Environment.GetEnvironmentVariable("PASSWORD");
_sslConfigured = Directory.Exists(_sslDir) &&
(false == string.IsNullOrEmpty(_certPassphrase));
}

[Test]
public void TestServerVerifiedIgnoringNameMismatch()
{
string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
if (false == _sslConfigured)
{
Console.WriteLine("SSL_CERTS_DIR is not configured, skipping test");
return;
Assert.Ignore("SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
}

ConnectionFactory cf = new ConnectionFactory { Port = 5671 };
Expand All @@ -83,11 +73,9 @@ public void TestServerVerifiedIgnoringNameMismatch()
[Test]
public void TestServerVerified()
{
string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
if (false == _sslConfigured)
{
Console.WriteLine("SSL_CERTS_DIR is not configured, skipping test");
return;
Assert.Ignore("SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
}

ConnectionFactory cf = new ConnectionFactory { Port = 5671 };
Expand All @@ -99,18 +87,16 @@ public void TestServerVerified()
[Test]
public void TestClientAndServerVerified()
{
string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
if (false == _sslConfigured)
{
Console.WriteLine("SSL_CERTS_DIR is not configured, skipping test");
return;
Assert.Ignore("SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
}

string hostName = System.Net.Dns.GetHostName();
ConnectionFactory cf = new ConnectionFactory { Port = 5671 };
cf.Ssl.ServerName = System.Net.Dns.GetHostName();
Assert.IsNotNull(sslDir);
cf.Ssl.CertPath = $"{sslDir}/client_key.p12";
cf.Ssl.CertPassphrase = Environment.GetEnvironmentVariable("PASSWORD");
cf.Ssl.ServerName = hostName;
cf.Ssl.CertPath = $"{_sslDir}/client_{hostName}_key.p12";
cf.Ssl.CertPassphrase = _certPassphrase;
cf.Ssl.Enabled = true;
SendReceive(cf);
}
Expand All @@ -119,11 +105,9 @@ public void TestClientAndServerVerified()
[Test]
public void TestNoClientCertificate()
{
string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
if (false == _sslConfigured)
{
Console.WriteLine("SSL_CERTS_DIR is not configured, skipping test");
return;
Assert.Ignore("SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
}

ConnectionFactory cf = new ConnectionFactory
Expand All @@ -143,5 +127,29 @@ public void TestNoClientCertificate()

SendReceive(cf);
}

private void SendReceive(ConnectionFactory cf)
{
using (IConnection conn = cf.CreateConnection($"{_testDisplayName}:{Guid.NewGuid()}"))
{
using (IModel ch = conn.CreateModel())
{
ch.ExchangeDeclare("Exchange_TestSslEndPoint", ExchangeType.Direct);
string qName = ch.QueueDeclare();
ch.QueueBind(qName, "Exchange_TestSslEndPoint", "Key_TestSslEndpoint", null);

string message = "Hello C# SSL Client World";
byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);
ch.BasicPublish("Exchange_TestSslEndPoint", "Key_TestSslEndpoint", true, null, msgBytes);

bool autoAck = false;
BasicGetResult result = ch.BasicGet(qName, autoAck);
byte[] body = result.Body.ToArray();
string resultMessage = System.Text.Encoding.UTF8.GetString(body);

Assert.AreEqual(message, resultMessage);
}
}
}
}
}