Skip to content

Commit

Permalink
Fixing TLS connection test
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Oct 16, 2023
1 parent 330251d commit f8a75f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/NATS.Client.Core/Internal/NatsUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public NatsUri(string urlString, bool isSeed, string defaultScheme = DefaultSche
Uri = uriBuilder.Uri;
}

public NatsUri CloneWith(string host, int? port = default)
{
var newUri = new UriBuilder(Uri)
{
Host = host,
Port = port ?? Port,
}.Uri.ToString();

return new NatsUri(newUri, IsSeed);
}

public Uri Uri { get; }

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (main)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / test (main)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 54 in src/NATS.Client.Core/Internal/NatsUri.cs

View workflow job for this annotation

GitHub Actions / memory test (main)


public bool IsSeed { get; }
Expand Down
17 changes: 6 additions & 11 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,15 @@ private async ValueTask SetupReaderWriterAsync(bool reconnect)
// do TLS upgrade
// if the current URI is not a seed URI and is not a DNS hostname, check the server cert against the
// last seed hostname if it was a DNS hostname
var targetHost = _currentConnectUri.Host;
var targetUri = _currentConnectUri;
if (!_currentConnectUri.IsSeed
&& Uri.CheckHostName(targetHost) != UriHostNameType.Dns
&& Uri.CheckHostName(targetUri.Host) != UriHostNameType.Dns
&& Uri.CheckHostName(_lastSeedConnectUri!.Host) == UriHostNameType.Dns)
{
targetHost = _lastSeedConnectUri.Host;
targetUri = targetUri.CloneWith(_lastSeedConnectUri.Host);
}

_logger.LogDebug("Perform TLS Upgrade to " + targetHost);
_logger.LogDebug("Perform TLS Upgrade to " + targetUri);

// cancel INFO parsed signal and dispose current socket reader
infoParsedSignal.SetCanceled();
Expand All @@ -378,7 +378,7 @@ private async ValueTask SetupReaderWriterAsync(bool reconnect)

// upgrade TcpConnection to SslConnection
var sslConnection = tcpConnection.UpgradeToSslStreamConnection(Opts.TlsOpts, _tlsCerts);
await sslConnection.AuthenticateAsClientAsync(_currentConnectUri).ConfigureAwait(false);
await sslConnection.AuthenticateAsClientAsync(targetUri).ConfigureAwait(false);
_socket = sslConnection;

// create new socket reader
Expand Down Expand Up @@ -474,12 +474,7 @@ private async void ReconnectLoop()

if (newTarget.Host != target.Host || newTarget.Port != target.Port)
{
var newUri = new UriBuilder(url.Uri)
{
Host = newTarget.Host,
Port = newTarget.Port,
}.Uri.ToString();
url = new NatsUri(newUri, url.IsSeed);
url = url.CloneWith(newTarget.Host, newTarget.Port);
}
}

Expand Down
5 changes: 0 additions & 5 deletions tests/NATS.Client.TestUtilities/NatsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,16 @@ public NatsCluster(ITestOutputHelper outputHelper, TransportType transportType)
var opts1 = new NatsServerOptsBuilder()
.UseTransport(transportType)
.EnableClustering()
.Trace() // for debugging
.Build();


var opts2 = new NatsServerOptsBuilder()
.UseTransport(transportType)
.EnableClustering()
.Trace() // for debugging
.Build();


var opts3 = new NatsServerOptsBuilder()
.UseTransport(transportType)
.EnableClustering()
.Trace() // for debugging
.Build();

// By querying the ports we set the values lazily on all the opts.
Expand Down

0 comments on commit f8a75f4

Please sign in to comment.