-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ public NatsConnection() | |
public NatsConnection(NatsOpts opts) | ||
{ | ||
_logger = opts.LoggerFactory.CreateLogger<NatsConnection>(); | ||
Opts = opts; | ||
Opts = ReadUserInfoFromConnectionString(opts); | ||
ConnectionState = NatsConnectionState.Closed; | ||
_waitForOpenConnection = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
_disposedCancellationTokenSource = new CancellationTokenSource(); | ||
|
@@ -289,6 +289,62 @@ internal ValueTask UnsubscribeAsync(int sid) | |
return default; | ||
} | ||
|
||
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 291 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
|
||
|
||
private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts) | ||
{ | ||
var first = true; | ||
|
||
var natsUris = opts.GetSeedUris(); | ||
var maskedUris = new List<string>(natsUris.Length); | ||
|
||
foreach (var natsUri in natsUris) | ||
{ | ||
var uriBuilder = new UriBuilder(natsUri.Uri); | ||
|
||
if (uriBuilder.UserName is { Length: > 0 } username) | ||
{ | ||
if (first) | ||
{ | ||
first = false; | ||
|
||
if (uriBuilder.Password is { Length: > 0 } password) | ||
{ | ||
opts = opts with | ||
{ | ||
AuthOpts = opts.AuthOpts with | ||
{ | ||
Username = uriBuilder.UserName, | ||
Password = uriBuilder.Password, | ||
}, | ||
}; | ||
|
||
uriBuilder.Password = "***"; // to redact the password from logs | ||
} | ||
else | ||
{ | ||
opts = opts with | ||
{ | ||
AuthOpts = opts.AuthOpts with | ||
{ | ||
Token = uriBuilder.UserName, | ||
}, | ||
}; | ||
|
||
uriBuilder.UserName = "***"; // to redact the token from logs | ||
} | ||
} | ||
|
||
} | ||
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / check
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Windows (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (latest)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (main)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
Check warning on line 337 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / Linux (v2.9)
|
||
|
||
maskedUris.Add(uriBuilder.ToString().TrimEnd('/')); | ||
} | ||
|
||
var combinedUri = string.Join(",", maskedUris); | ||
opts = opts with { Url = combinedUri }; | ||
|
||
return opts; | ||
} | ||
|
||
private async ValueTask InitialConnectAsync() | ||
{ | ||
Debug.Assert(ConnectionState == NatsConnectionState.Connecting, "Connection state"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,22 @@ private NatsServer(ITestOutputHelper outputHelper, NatsServerOpts opts) | |
_ => throw new ArgumentOutOfRangeException(), | ||
}; | ||
|
||
public string ClientUrlWithAuth | ||
{ | ||
get | ||
{ | ||
if (!string.IsNullOrEmpty(Opts.ClientUrlUserName)) | ||
{ | ||
var uriBuilder = new UriBuilder(ClientUrl); | ||
uriBuilder.UserName = Opts.ClientUrlUserName; | ||
uriBuilder.Password = Opts.ClientUrlPassword; | ||
return uriBuilder.ToString().TrimEnd('/'); | ||
} | ||
|
||
return ClientUrl; | ||
} | ||
} | ||
|
||
public int ConnectionPort | ||
{ | ||
get | ||
|
@@ -134,7 +150,7 @@ public static NatsServer StartWithTrace(ITestOutputHelper outputHelper) | |
public static NatsServer Start(ITestOutputHelper outputHelper, TransportType transportType) => | ||
Start(outputHelper, new NatsServerOptsBuilder().UseTransport(transportType).Build()); | ||
|
||
public static NatsServer Start(ITestOutputHelper outputHelper, NatsServerOpts opts, NatsOpts? clientOpts = default) | ||
public static NatsServer Start(ITestOutputHelper outputHelper, NatsServerOpts opts, NatsOpts? clientOpts = default, bool useAuthInUrl = false) | ||
{ | ||
NatsServer? server = null; | ||
NatsConnection? nats = null; | ||
|
@@ -144,7 +160,7 @@ public static NatsServer Start(ITestOutputHelper outputHelper, NatsServerOpts op | |
{ | ||
server = new NatsServer(outputHelper, opts); | ||
server.StartServerProcess(); | ||
nats = server.CreateClientConnection(clientOpts ?? NatsOpts.Default, reTryCount: 3); | ||
nats = server.CreateClientConnection(clientOpts ?? GetDefaultClientOpts(server), reTryCount: 3, useAuthInUrl: useAuthInUrl); | ||
#pragma warning disable CA2012 | ||
return server; | ||
} | ||
|
@@ -162,6 +178,11 @@ public static NatsServer Start(ITestOutputHelper outputHelper, NatsServerOpts op | |
throw new Exception("Can't start nats-server and connect to it"); | ||
} | ||
|
||
private static NatsOpts GetDefaultClientOpts(NatsServer server) | ||
{ | ||
return NatsOpts.Default/* with { Url = server.ClientUrl }*/; | ||
} | ||
|
||
public void StartServerProcess() | ||
Check warning on line 186 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 186 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 186 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
Check warning on line 186 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (main)
|
||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
|
@@ -335,13 +356,13 @@ public async ValueTask DisposeAsync() | |
return (client, proxy); | ||
} | ||
|
||
public NatsConnection CreateClientConnection(NatsOpts? options = default, int reTryCount = 10, bool ignoreAuthorizationException = false, bool testLogger = true) | ||
public NatsConnection CreateClientConnection(NatsOpts? options = default, int reTryCount = 10, bool ignoreAuthorizationException = false, bool testLogger = true, bool useAuthInUrl = false) | ||
{ | ||
for (var i = 0; i < reTryCount; i++) | ||
{ | ||
try | ||
{ | ||
var nats = new NatsConnection(ClientOpts(options ?? NatsOpts.Default, testLogger: testLogger)); | ||
var nats = new NatsConnection(ClientOpts(options ?? GetDefaultClientOpts(this), testLogger: testLogger, useAuthInUrl: useAuthInUrl)); | ||
|
||
try | ||
{ | ||
|
@@ -369,14 +390,14 @@ public NatsConnection CreateClientConnection(NatsOpts? options = default, int re | |
throw new Exception("Can't create a connection to nats-server"); | ||
} | ||
|
||
public NatsConnectionPool CreatePooledClientConnection() => CreatePooledClientConnection(NatsOpts.Default); | ||
public NatsConnectionPool CreatePooledClientConnection() => CreatePooledClientConnection(GetDefaultClientOpts(this)); | ||
|
||
public NatsConnectionPool CreatePooledClientConnection(NatsOpts opts) | ||
{ | ||
return new NatsConnectionPool(4, ClientOpts(opts)); | ||
} | ||
|
||
public NatsOpts ClientOpts(NatsOpts opts, bool testLogger = true) | ||
public NatsOpts ClientOpts(NatsOpts opts, bool testLogger = true, bool useAuthInUrl = false) | ||
{ | ||
var natsTlsOpts = Opts.EnableTls | ||
? opts.TlsOpts with | ||
|
@@ -392,7 +413,7 @@ public NatsOpts ClientOpts(NatsOpts opts, bool testLogger = true) | |
{ | ||
LoggerFactory = testLogger ? _loggerFactory : opts.LoggerFactory, | ||
TlsOpts = natsTlsOpts, | ||
Url = ClientUrl, | ||
Url = useAuthInUrl ? ClientUrlWithAuth : ClientUrl, | ||
}; | ||
} | ||
|
||
|
@@ -431,6 +452,15 @@ private static (string configFileName, string config, string cmd) GetCmd(NatsSer | |
|
||
var cmd = $"{NatsServerPath} -c {configFileName}"; | ||
|
||
//var cmdBuilder = new StringBuilder($"{NatsServerPath} -c {configFileName}"); | ||
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / check
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (v2.9)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (main)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (latest)
Check warning on line 455 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (v2.9)
|
||
|
||
//if (!string.IsNullOrEmpty(opts.UserName) && !string.IsNullOrEmpty(opts.Password)) | ||
Check warning on line 457 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 457 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 457 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
|
||
//{ | ||
Check warning on line 458 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 458 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 458 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
|
||
// cmdBuilder.Append($" --user {opts.UserName} --pass {opts.Password}"); | ||
//} | ||
Check warning on line 460 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 460 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 460 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
|
||
|
||
//var cmd = cmdBuilder.ToString(); | ||
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (v2.9)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (main)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / test (latest)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / check
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (v2.9)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (main)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (latest)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Windows (latest)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (latest)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (main)
Check warning on line 462 in tests/NATS.Client.TestUtilities/NatsServer.cs GitHub Actions / Linux (v2.9)
|
||
|
||
return (configFileName, config, cmd); | ||
} | ||
|
||
|
@@ -464,7 +494,7 @@ public class NatsCluster : IAsyncDisposable | |
{ | ||
private readonly ITestOutputHelper _outputHelper; | ||
|
||
public NatsCluster(ITestOutputHelper outputHelper, TransportType transportType, Action<int, NatsServerOptsBuilder>? configure = default) | ||
public NatsCluster(ITestOutputHelper outputHelper, TransportType transportType, Action<int, NatsServerOptsBuilder>? configure = default, bool useAuthInUrl = false) | ||
{ | ||
_outputHelper = outputHelper; | ||
|
||
|
@@ -516,13 +546,13 @@ public NatsCluster(ITestOutputHelper outputHelper, TransportType transportType, | |
} | ||
|
||
_outputHelper.WriteLine($"Starting server 1..."); | ||
Server1 = NatsServer.Start(outputHelper, opts1); | ||
Server1 = NatsServer.Start(outputHelper, opts1, useAuthInUrl: useAuthInUrl); | ||
|
||
_outputHelper.WriteLine($"Starting server 2..."); | ||
Server2 = NatsServer.Start(outputHelper, opts2); | ||
Server2 = NatsServer.Start(outputHelper, opts2, useAuthInUrl: useAuthInUrl); | ||
|
||
_outputHelper.WriteLine($"Starting server 3..."); | ||
Server3 = NatsServer.Start(outputHelper, opts3); | ||
Server3 = NatsServer.Start(outputHelper, opts3, useAuthInUrl: useAuthInUrl); | ||
} | ||
|
||
public NatsServer Server1 { get; } | ||
|