Skip to content
Prev Previous commit
Next Next commit
set username on multiple uris to the actual used one + better redaction
  • Loading branch information
Mrxx99 committed Nov 2, 2024
commit 894faf3dfec370c81bdd1984d24a930f55f035a1
15 changes: 15 additions & 0 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
var natsUris = opts.GetSeedUris(suppressRandomization: true);
var maskedUris = new List<string>(natsUris.Length);

var usesPasswordInUrl = false;
var usesTokenInUrl = false;

foreach (var natsUri in natsUris)
{
var uriBuilder = new UriBuilder(natsUri.Uri);
Expand All @@ -306,6 +309,7 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
if (first)
{
first = false;
usesPasswordInUrl = true;

opts = opts with
{
Expand All @@ -324,6 +328,7 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
if (first)
{
first = false;
usesTokenInUrl = true;

opts = opts with
{
Expand All @@ -338,6 +343,16 @@ private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts)
}
}

if (usesPasswordInUrl)
{
uriBuilder.UserName = opts.AuthOpts.Username; // show actual used user name in logs
}
else if (usesTokenInUrl)
{
uriBuilder.UserName = "***"; // to redact the token from logs
uriBuilder.Password = null; // when token is used remove password
}

maskedUris.Add(uriBuilder.ToString().TrimEnd('/'));
}

Expand Down